Update registry middleware (#5585)

This commit is contained in:
Nicolas Gotchac 2017-05-10 14:42:26 +02:00 committed by Jaco Greeff
parent 860d6c01ed
commit 076b602343
2 changed files with 111 additions and 97 deletions

View File

@ -38,7 +38,7 @@ export default function (api, browserHistory, forEmbed = false) {
if (!forEmbed) {
const certifications = new CertificationsMiddleware().toMiddleware();
const registry = new RegistryMiddleware(api);
const registry = new RegistryMiddleware(api).toMiddleware();
middleware.push(certifications, registry);
}

View File

@ -15,7 +15,7 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { debounce } from 'lodash';
import store from 'store';
import lsstore from 'store';
import Contracts from '~/contracts';
import subscribeToEvents from '~/util/subscribe-to-events';
@ -25,117 +25,50 @@ 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;
let subscription;
let timeout;
let interval;
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 ])
.then((reverse) => {
store.dispatch(setReverse(address, reverse));
});
});
export default class RegistryMiddleware {
contract;
interval;
store;
subscription;
timeout;
addressesToCheck = {};
};
constructor (api) {
this._api = api;
}
toMiddleware () {
return (store) => {
this.store = store;
return (next) => (action) => {
switch (action.type) {
case 'initAll':
next(action);
store.dispatch(startCachingReverses());
break;
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'
], {
from: cached ? cached.lastBlock : 0
});
subscription.on('log', onLog);
timeout = setTimeout(checkReverses, 10000);
interval = setInterval(checkReverses, 20000);
})
.catch((err) => {
console.error('Failed to start caching reverses:', err);
throw err;
});
this.cacheReverses();
break;
case 'stopCachingReverses':
if (subscription) {
subscription.unsubscribe();
if (this.subscription) {
this.subscription.unsubscribe();
}
if (interval) {
clearInterval(interval);
if (this.interval) {
clearInterval(this.interval);
}
if (timeout) {
clearTimeout(timeout);
if (this.timeout) {
clearTimeout(this.timeout);
}
write.flush();
this.write.flush();
break;
case 'setReverse':
write(
this.write(
() => store.getState().nodeStatus.netChain,
() => store.getState().registry.reverse,
() => +store.getState().nodeStatus.blockNumber
@ -148,3 +81,84 @@ export default (api) => (store) => {
}
};
};
}
cacheReverses () {
const { registry } = Contracts.get();
const cached = this.read(this.store.getState().nodeStatus.netChain);
if (cached) {
Object
.entries(cached.reverses)
.forEach(([ address, reverse ]) => this.store.dispatch(setReverse(address, reverse)));
}
registry.getInstance()
.then((instance) => this._api.newContract(registryABI, instance.address))
.then((_contract) => {
this.contract = _contract;
this.subscription = subscribeToEvents(this.contract, [
'ReverseConfirmed', 'ReverseRemoved'
], {
from: cached ? cached.lastBlock : 0
});
this.subscription.on('log', this.onLog);
this.timeout = setTimeout(this.checkReverses, 10000);
this.interval = setInterval(this.checkReverses, 20000);
})
.catch((err) => {
console.error('Failed to start caching reverses:', err);
throw err;
});
}
checkReverses = () => {
Object
.keys(this.addressesToCheck)
.forEach((address) => {
this.contract
.instance
.reverse
.call({}, [ address ])
.then((reverse) => {
this.store.dispatch(setReverse(address, reverse));
});
});
this.addressesToCheck = {};
};
onLog = (log) => {
switch (log.event) {
case 'ReverseConfirmed':
this.addressesToCheck[log.params.reverse.value] = true;
break;
case 'ReverseRemoved':
delete this.addressesToCheck[log.params.reverse.value];
break;
}
};
read = (chain) => {
const reverses = lsstore.get(`${STORE_KEY}::${chain}::data`);
const lastBlock = lsstore.get(`${STORE_KEY}::${chain}::lastBlock`);
if (!reverses || !lastBlock) {
return null;
}
return { reverses, lastBlock };
};
write = debounce((getChain, getReverses, getLastBlock) => {
const chain = getChain();
const reverses = getReverses();
const lastBlock = getLastBlock();
lsstore.set(`${STORE_KEY}::${chain}::data`, reverses);
lsstore.set(`${STORE_KEY}::${chain}::lastBlock`, lastBlock);
}, 20000);
}