From df9110dd5e676002cfbaa0304c0cf3724624bed2 Mon Sep 17 00:00:00 2001 From: Jannis Redmann Date: Fri, 20 Jan 2017 10:07:47 +0100 Subject: [PATCH] cache registry reverses in local storage (#4182) * cache reverses in localStorage * bugfixes :bug:, linting fixes :shirt: * registry caching: store last block * registry caching: store per chain * localStorage -> store * code style :sparkles: * code style :shirt: --- js/src/redux/providers/registry/middleware.js | 53 +++++++++++++++++-- js/src/redux/providers/registry/reducer.js | 8 +-- js/src/util/subscribe-to-events.spec.js | 11 ++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/js/src/redux/providers/registry/middleware.js b/js/src/redux/providers/registry/middleware.js index 136a9eb08..da05c33df 100644 --- a/js/src/redux/providers/registry/middleware.js +++ b/js/src/redux/providers/registry/middleware.js @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . +import { debounce } from 'lodash'; +import store from 'store'; import Contracts from '~/contracts'; import subscribeToEvents from '~/util/subscribe-to-events'; @@ -21,6 +23,27 @@ 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; @@ -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 = {}; @@ -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) => { @@ -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); } diff --git a/js/src/redux/providers/registry/reducer.js b/js/src/redux/providers/registry/reducer.js index 5c267d822..d3a548e22 100644 --- a/js/src/redux/providers/registry/reducer.js +++ b/js/src/redux/providers/registry/reducer.js @@ -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; diff --git a/js/src/util/subscribe-to-events.spec.js b/js/src/util/subscribe-to-events.spec.js index a14d2b36e..06e934b2d 100644 --- a/js/src/util/subscribe-to-events.spec.js +++ b/js/src/util/subscribe-to-events.spec.js @@ -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); + }); });