cache registry reverses in local storage (#4182)
* cache reverses in localStorage * bugfixes 🐛, linting fixes 👕 * registry caching: store last block * registry caching: store per chain * localStorage -> store * code style ✨ * code style 👕
This commit is contained in:
parent
f12bd17c20
commit
df9110dd5e
@ -14,6 +14,8 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import { debounce } from 'lodash';
|
||||||
|
import store from 'store';
|
||||||
import Contracts from '~/contracts';
|
import Contracts from '~/contracts';
|
||||||
import subscribeToEvents from '~/util/subscribe-to-events';
|
import subscribeToEvents from '~/util/subscribe-to-events';
|
||||||
|
|
||||||
@ -21,6 +23,27 @@ import registryABI from '~/contracts/abi/registry.json';
|
|||||||
|
|
||||||
import { setReverse, startCachingReverses } from './actions';
|
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) => {
|
export default (api) => (store) => {
|
||||||
let contract, subscription, timeout, interval;
|
let contract, subscription, timeout, interval;
|
||||||
|
|
||||||
@ -47,7 +70,9 @@ export default (api) => (store) => {
|
|||||||
.instance
|
.instance
|
||||||
.reverse
|
.reverse
|
||||||
.call({}, [ address ])
|
.call({}, [ address ])
|
||||||
.then((reverse) => store.dispatch(setReverse(address, reverse)));
|
.then((reverse) => {
|
||||||
|
store.dispatch(setReverse(address, reverse));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
addressesToCheck = {};
|
addressesToCheck = {};
|
||||||
@ -63,15 +88,26 @@ export default (api) => (store) => {
|
|||||||
case 'startCachingReverses':
|
case 'startCachingReverses':
|
||||||
const { registry } = Contracts.get();
|
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()
|
registry.getInstance()
|
||||||
.then((instance) => api.newContract(registryABI, instance.address))
|
.then((instance) => api.newContract(registryABI, instance.address))
|
||||||
.then((_contract) => {
|
.then((_contract) => {
|
||||||
contract = _contract;
|
contract = _contract;
|
||||||
|
|
||||||
subscription = subscribeToEvents(_contract, ['ReverseConfirmed', 'ReverseRemoved']);
|
subscription = subscribeToEvents(_contract, [
|
||||||
|
'ReverseConfirmed', 'ReverseRemoved'
|
||||||
|
], {
|
||||||
|
from: cached ? cached.lastBlock : 0
|
||||||
|
});
|
||||||
subscription.on('log', onLog);
|
subscription.on('log', onLog);
|
||||||
|
|
||||||
timeout = setTimeout(checkReverses, 5000);
|
timeout = setTimeout(checkReverses, 10000);
|
||||||
interval = setInterval(checkReverses, 20000);
|
interval = setInterval(checkReverses, 20000);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
@ -91,7 +127,18 @@ export default (api) => (store) => {
|
|||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
write.flush();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'setReverse':
|
||||||
|
write(
|
||||||
|
() => store.getState().nodeStatus.netChain,
|
||||||
|
() => store.getState().registry.reverse,
|
||||||
|
() => +store.getState().nodeStatus.blockNumber
|
||||||
|
);
|
||||||
|
next(action);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
next(action);
|
next(action);
|
||||||
}
|
}
|
||||||
|
@ -24,9 +24,11 @@ export default (state = initialState, action) => {
|
|||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
return { ...state, reverse: {
|
return {
|
||||||
|
reverse: {
|
||||||
...state.reverse, [ action.address ]: action.reverse
|
...state.reverse, [ action.address ]: action.reverse
|
||||||
} };
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
|
@ -111,4 +111,15 @@ describe('util/subscribe-to-events', () => {
|
|||||||
expect(onBar.callCount).to.be.at.least(1);
|
expect(onBar.callCount).to.be.at.least(1);
|
||||||
expect(onBar.firstCall.args).to.eql([ liveLogs[0] ]);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user