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

cache registry reverses in local storage #4182

Merged
merged 10 commits into from
Jan 20, 2017
53 changes: 50 additions & 3 deletions js/src/redux/providers/registry/middleware.js
Original file line number Diff line number Diff line change
@@ -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`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would probably make sense to const-ify data & lastBlock since it is used in multiple places. (Not a crisis either way)

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty line before return.

};

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for blank line before.


case 'setReverse':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line before.

write(
() => store.getState().nodeStatus.netChain,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we just pass the values in as opposed to passing in a function that retrieves the values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Premature optimization. 😉 write gets called quite often. For all past & new reverse events.

() => store.getState().registry.reverse,
() => +store.getState().nodeStatus.blockNumber
);
next(action);
break;

default:
next(action);
}
8 changes: 5 additions & 3 deletions js/src/redux/providers/registry/reducer.js
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 11 additions & 0 deletions js/src/util/subscribe-to-events.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});