2017-01-05 16:56:33 +01:00
|
|
|
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-01-20 10:07:47 +01:00
|
|
|
import { debounce } from 'lodash';
|
|
|
|
import store from 'store';
|
2017-01-05 16:56:33 +01:00
|
|
|
import Contracts from '~/contracts';
|
2017-01-09 19:03:19 +01:00
|
|
|
import subscribeToEvents from '~/util/subscribe-to-events';
|
2017-01-05 16:56:33 +01:00
|
|
|
|
|
|
|
import registryABI from '~/contracts/abi/registry.json';
|
|
|
|
|
|
|
|
import { setReverse, startCachingReverses } from './actions';
|
|
|
|
|
2017-01-20 10:07:47 +01:00
|
|
|
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);
|
|
|
|
|
2017-01-09 16:59:33 +01:00
|
|
|
export default (api) => (store) => {
|
2017-01-23 13:39:52 +01:00
|
|
|
let contract;
|
|
|
|
let subscription;
|
|
|
|
let timeout;
|
|
|
|
let interval;
|
2017-01-09 16:59:33 +01:00
|
|
|
|
|
|
|
let addressesToCheck = {};
|
|
|
|
|
|
|
|
const onLog = (log) => {
|
|
|
|
switch (log.event) {
|
|
|
|
case 'ReverseConfirmed':
|
|
|
|
addressesToCheck[log.params.reverse.value] = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'ReverseRemoved':
|
|
|
|
delete addressesToCheck[log.params.reverse.value];
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const checkReverses = () => {
|
|
|
|
Object
|
|
|
|
.keys(addressesToCheck)
|
|
|
|
.forEach((address) => {
|
|
|
|
contract
|
|
|
|
.instance
|
|
|
|
.reverse
|
|
|
|
.call({}, [ address ])
|
2017-01-20 10:07:47 +01:00
|
|
|
.then((reverse) => {
|
|
|
|
store.dispatch(setReverse(address, reverse));
|
|
|
|
});
|
2017-01-09 16:59:33 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
addressesToCheck = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
return (next) => (action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case 'initAll':
|
|
|
|
next(action);
|
|
|
|
store.dispatch(startCachingReverses());
|
|
|
|
|
|
|
|
break;
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2017-01-09 16:59:33 +01:00
|
|
|
case 'startCachingReverses':
|
|
|
|
const { registry } = Contracts.get();
|
2017-01-20 10:07:47 +01:00
|
|
|
const cached = read(store.getState().nodeStatus.netChain);
|
2017-01-23 13:39:52 +01:00
|
|
|
|
2017-01-20 10:07:47 +01:00
|
|
|
if (cached) {
|
|
|
|
Object
|
|
|
|
.entries(cached.reverses)
|
|
|
|
.forEach(([ address, reverse ]) => store.dispatch(setReverse(address, reverse)));
|
|
|
|
}
|
|
|
|
|
2017-01-09 16:59:33 +01:00
|
|
|
registry.getInstance()
|
|
|
|
.then((instance) => api.newContract(registryABI, instance.address))
|
|
|
|
.then((_contract) => {
|
|
|
|
contract = _contract;
|
|
|
|
|
2017-01-20 10:07:47 +01:00
|
|
|
subscription = subscribeToEvents(_contract, [
|
|
|
|
'ReverseConfirmed', 'ReverseRemoved'
|
|
|
|
], {
|
|
|
|
from: cached ? cached.lastBlock : 0
|
|
|
|
});
|
2017-01-09 19:03:19 +01:00
|
|
|
subscription.on('log', onLog);
|
2017-01-09 16:59:33 +01:00
|
|
|
|
2017-01-20 10:07:47 +01:00
|
|
|
timeout = setTimeout(checkReverses, 10000);
|
2017-01-09 16:59:33 +01:00
|
|
|
interval = setInterval(checkReverses, 20000);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error('Failed to start caching reverses:', err);
|
|
|
|
throw err;
|
2017-01-05 16:56:33 +01:00
|
|
|
});
|
|
|
|
|
2017-01-09 16:59:33 +01:00
|
|
|
break;
|
|
|
|
case 'stopCachingReverses':
|
2017-01-09 19:03:19 +01:00
|
|
|
if (subscription) {
|
|
|
|
subscription.unsubscribe();
|
2017-01-09 16:59:33 +01:00
|
|
|
}
|
|
|
|
if (interval) {
|
|
|
|
clearInterval(interval);
|
|
|
|
}
|
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
2017-01-20 10:07:47 +01:00
|
|
|
write.flush();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'setReverse':
|
|
|
|
write(
|
|
|
|
() => store.getState().nodeStatus.netChain,
|
|
|
|
() => store.getState().registry.reverse,
|
|
|
|
() => +store.getState().nodeStatus.blockNumber
|
|
|
|
);
|
|
|
|
next(action);
|
2017-01-09 16:59:33 +01:00
|
|
|
break;
|
2017-01-20 10:07:47 +01:00
|
|
|
|
2017-01-09 16:59:33 +01:00
|
|
|
default:
|
|
|
|
next(action);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|