Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
cache registry reverses in local storage (#4182)
Browse files Browse the repository at this point in the history
* cache reverses in localStorage

* bugfixes 🐛, linting fixes 👕

* registry caching: store last block

* registry caching: store per chain

* localStorage -> store

* code style ✨

* code style 👕
  • Loading branch information
derhuerst authored and jacogr committed Jan 20, 2017
1 parent f12bd17 commit df9110d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
53 changes: 50 additions & 3 deletions js/src/redux/providers/registry/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,36 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import { debounce } from 'lodash';
import store from 'store';
import Contracts from '~/contracts';
import subscribeToEvents from '~/util/subscribe-to-events';

import registryABI from '~/contracts/abi/registry.json';

import { setReverse, startCachingReverses } from './actions';

const STORE_KEY = '_parity::reverses';

const read = (chain) => {
const reverses = store.get(`${STORE_KEY}::${chain}::data`);
const lastBlock = store.get(`${STORE_KEY}::${chain}::lastBlock`);

if (!reverses || !lastBlock) {
return null;
}
return { reverses, lastBlock };
};

const write = debounce((getChain, getReverses, getLastBlock) => {
const chain = getChain();
const reverses = getReverses();
const lastBlock = getLastBlock();

store.set(`${STORE_KEY}::${chain}::data`, reverses);
store.set(`${STORE_KEY}::${chain}::lastBlock`, lastBlock);
}, 20000);

export default (api) => (store) => {
let contract, subscription, timeout, interval;

Expand All @@ -47,7 +70,9 @@ export default (api) => (store) => {
.instance
.reverse
.call({}, [ address ])
.then((reverse) => store.dispatch(setReverse(address, reverse)));
.then((reverse) => {
store.dispatch(setReverse(address, reverse));
});
});

addressesToCheck = {};
Expand All @@ -63,15 +88,26 @@ export default (api) => (store) => {
case 'startCachingReverses':
const { registry } = Contracts.get();

const cached = read(store.getState().nodeStatus.netChain);
if (cached) {
Object
.entries(cached.reverses)
.forEach(([ address, reverse ]) => store.dispatch(setReverse(address, reverse)));
}

registry.getInstance()
.then((instance) => api.newContract(registryABI, instance.address))
.then((_contract) => {
contract = _contract;

subscription = subscribeToEvents(_contract, ['ReverseConfirmed', 'ReverseRemoved']);
subscription = subscribeToEvents(_contract, [
'ReverseConfirmed', 'ReverseRemoved'
], {
from: cached ? cached.lastBlock : 0
});
subscription.on('log', onLog);

timeout = setTimeout(checkReverses, 5000);
timeout = setTimeout(checkReverses, 10000);
interval = setInterval(checkReverses, 20000);
})
.catch((err) => {
Expand All @@ -91,7 +127,18 @@ export default (api) => (store) => {
clearTimeout(timeout);
}

write.flush();
break;

case 'setReverse':
write(
() => store.getState().nodeStatus.netChain,
() => store.getState().registry.reverse,
() => +store.getState().nodeStatus.blockNumber
);
next(action);
break;

default:
next(action);
}
Expand Down
8 changes: 5 additions & 3 deletions js/src/redux/providers/registry/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ export default (state = initialState, action) => {
return state;
}

return { ...state, reverse: {
...state.reverse, [ action.address ]: action.reverse
} };
return {
reverse: {
...state.reverse, [ action.address ]: action.reverse
}
};
}

return state;
Expand Down
11 changes: 11 additions & 0 deletions js/src/util/subscribe-to-events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,15 @@ describe('util/subscribe-to-events', () => {
expect(onBar.callCount).to.be.at.least(1);
expect(onBar.firstCall.args).to.eql([ liveLogs[0] ]);
});

it('accepts a custom block range', async function () {
const { api, contract } = this;

subscribeToEvents(contract, [ 'Foo' ], { from: 123, to: 321 });

await delay(0);
expect(api.eth.newFilter.callCount).to.equal(1);
expect(api.eth.newFilter.firstCall.args[0].fromBlock).to.equal(123);
expect(api.eth.newFilter.firstCall.args[0].toBlock).to.equal(321);
});
});

0 comments on commit df9110d

Please sign in to comment.