refresh certifications automatically (#3878)

* certifications: eth.getLogs -> filters

* linting 👕, indentation

* certifications: fetch filter changes

* certifications: prevent overlapping

* certifications: watch blockNumber to refresh

* fix email certification contract

See ethcore/contracts@d86490e302

* update email certification contract
This commit is contained in:
Jannis Redmann 2016-12-19 13:17:15 +01:00 committed by Jaco Greeff
parent 670be41b62
commit 7185fb0f37
4 changed files with 119 additions and 67 deletions

View File

@ -135,6 +135,7 @@
"blockies": "0.0.2", "blockies": "0.0.2",
"brace": "0.9.0", "brace": "0.9.0",
"bytes": "2.4.0", "bytes": "2.4.0",
"debounce": "1.0.0",
"es6-error": "4.0.0", "es6-error": "4.0.0",
"es6-promise": "4.0.5", "es6-promise": "4.0.5",
"ethereumjs-tx": "1.1.4", "ethereumjs-tx": "1.1.4",

View File

@ -23,7 +23,7 @@ import VerificationStore, {
} from './store'; } from './store';
import { postToServer } from '../../3rdparty/email-verification'; import { postToServer } from '../../3rdparty/email-verification';
const EMAIL_VERIFICATION = 4; // id in the `BadgeReg.sol` contract const EMAIL_VERIFICATION = 7; // id in the `BadgeReg.sol` contract
export default class EmailVerificationStore extends VerificationStore { export default class EmailVerificationStore extends VerificationStore {
@observable email = ''; @observable email = '';

View File

@ -15,12 +15,41 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
import { uniq } from 'lodash'; import { uniq } from 'lodash';
import debounce from 'debounce';
import ABI from '~/contracts/abi/certifier.json'; import ABI from '~/contracts/abi/certifier.json';
import Contract from '~/api/contract'; import Contract from '~/api/contract';
import Contracts from '~/contracts'; import Contracts from '~/contracts';
import { addCertification, removeCertification } from './actions'; import { addCertification, removeCertification } from './actions';
// TODO: move this to a more general place
const updatableFilter = (api, onFilter) => {
let filter = null;
const update = (address, topics) => {
if (filter) {
filter = filter.then((filterId) => {
api.eth.uninstallFilter(filterId);
});
}
filter = (filter || Promise.resolve())
.then(() => api.eth.newFilter({
fromBlock: 0,
toBlock: 'latest',
address,
topics
}))
.then((filterId) => {
onFilter(filterId);
return filterId;
})
.catch((err) => {
console.error('Failed to create certifications filter:', err);
});
};
return update;
};
export default class CertificationsMiddleware { export default class CertificationsMiddleware {
toMiddleware () { toMiddleware () {
const api = Contracts.get()._api; const api = Contracts.get()._api;
@ -29,19 +58,9 @@ export default class CertificationsMiddleware {
const Confirmed = contract.events.find((e) => e.name === 'Confirmed'); const Confirmed = contract.events.find((e) => e.name === 'Confirmed');
const Revoked = contract.events.find((e) => e.name === 'Revoked'); const Revoked = contract.events.find((e) => e.name === 'Revoked');
let certifiers = []; return (store) => {
let accounts = []; // these are addresses const onLogs = (logs) => {
logs = contract.parseEventLogs(logs);
const fetchConfirmedEvents = (dispatch) => {
if (certifiers.length === 0 || accounts.length === 0) return;
api.eth.getLogs({
fromBlock: 0,
toBlock: 'latest',
address: certifiers.map((c) => c.address),
topics: [ [ Confirmed.signature, Revoked.signature ], accounts ]
})
.then((logs) => contract.parseEventLogs(logs))
.then((logs) => {
logs.forEach((log) => { logs.forEach((log) => {
const certifier = certifiers.find((c) => c.address === log.address); const certifier = certifiers.find((c) => c.address === log.address);
if (!certifier) { if (!certifier) {
@ -50,18 +69,48 @@ export default class CertificationsMiddleware {
const { id, name, title, icon } = certifier; const { id, name, title, icon } = certifier;
if (log.event === 'Revoked') { if (log.event === 'Revoked') {
dispatch(removeCertification(log.params.who.value, id)); store.dispatch(removeCertification(log.params.who.value, id));
} else { } else {
dispatch(addCertification(log.params.who.value, id, name, title, icon)); store.dispatch(addCertification(log.params.who.value, id, name, title, icon));
} }
}); });
})
.catch((err) => {
console.error('Failed to fetch Confirmed events:', err);
});
}; };
return (store) => (next) => (action) => { let filter = null;
const onFilter = (filterId) => {
filter = filterId;
api.eth.getFilterLogs(filterId)
.then(onLogs)
.catch((err) => {
console.error('Failed to fetch certifier events:', err);
});
};
const fetchChanges = debounce(() => {
api.eth.getFilterChanges(filter)
.then(onLogs)
.catch((err) => {
console.error('Failed to fetch new certifier events:', err);
});
}, 10 * 1000, true);
api.subscribe('eth_blockNumber', (err) => {
if (err) return;
fetchChanges();
});
const updateFilter = updatableFilter(api, onFilter);
let certifiers = [];
let accounts = []; // these are addresses
const fetchConfirmedEvents = () => {
updateFilter(certifiers.map((c) => c.address), [
[ Confirmed.signature, Revoked.signature ],
accounts
]);
};
return (next) => (action) => {
switch (action.type) { switch (action.type) {
case 'fetchCertifiers': case 'fetchCertifiers':
badgeReg.certifierCount().then((count) => { badgeReg.certifierCount().then((count) => {
@ -70,7 +119,7 @@ export default class CertificationsMiddleware {
.then((cert) => { .then((cert) => {
if (!certifiers.some((c) => c.id === cert.id)) { if (!certifiers.some((c) => c.id === cert.id)) {
certifiers = certifiers.concat(cert); certifiers = certifiers.concat(cert);
fetchConfirmedEvents(store.dispatch); fetchConfirmedEvents();
} }
}) })
.catch((err) => { .catch((err) => {
@ -85,19 +134,20 @@ export default class CertificationsMiddleware {
if (!accounts.includes(address)) { if (!accounts.includes(address)) {
accounts = accounts.concat(address); accounts = accounts.concat(address);
fetchConfirmedEvents(store.dispatch); fetchConfirmedEvents();
} }
break; break;
case 'setVisibleAccounts': case 'setVisibleAccounts':
const { addresses } = action; const { addresses } = action;
accounts = uniq(accounts.concat(addresses)); accounts = uniq(accounts.concat(addresses));
fetchConfirmedEvents(store.dispatch); fetchConfirmedEvents();
break; break;
default: default:
next(action); next(action);
} }
}; };
};
} }
} }

View File

@ -22,6 +22,7 @@
.certification { .certification {
display: inline-block; display: inline-block;
position: relative; position: relative;
margin-top: 1em;
margin-right: .5em; margin-right: .5em;
padding: .3em .6em .2em 2.6em; padding: .3em .6em .2em 2.6em;
border-radius: 1em; border-radius: 1em;