fix: check if collection exists and handle reorgs in log syncer (#40)

This commit is contained in:
Richard Ramos 2019-10-02 22:09:17 -04:00 committed by GitHub
parent 6da7c605f8
commit e5ec647e44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

View File

@ -76,8 +76,13 @@ class Database {
} }
eventExists(eventKey, eventId) { eventExists(eventKey, eventId) {
let children = this.db.getCollection(eventKey); let collection = this.db.getCollection(eventKey);
return (children.find({ 'id': eventId }).length > 0); if(!collection){
this.db.addCollection(eventKey);
return false;
}
return (collection.find({ 'id': eventId }).length > 0);
} }
recordEvent(eventKey, values) { recordEvent(eventKey, values) {

View File

@ -10,10 +10,11 @@ class LogSyncer {
this.subscriptions = []; this.subscriptions = [];
} }
track(options){ track(options, gteBlockNum){
const eventKey = 'logs-' + hash(options || {}); const eventKey = 'logs-' + hash(options || {});
const filterConditions = Object.assign({fromBlock: 0, toBlock: "latest"}, options || {}); const filterConditions = Object.assign({fromBlock: 0, toBlock: "latest"}, options || {});
this.db.deleteNewestBlocks(eventKey, gteBlockNum);
const eventSummary = this.db.getLastKnownEvent(eventKey); const eventSummary = this.db.getLastKnownEvent(eventKey);
const sub = new ReplaySubject(); const sub = new ReplaySubject();
@ -22,16 +23,26 @@ class LogSyncer {
logObserver.subscribe((e) => { logObserver.subscribe((e) => {
if(!e) return; if(!e) return;
const id = hash({eventName: eventKey, blockNumber: e.blockNumber, transactionIndex: e.transactionIndex, logIndex: e.logIndex});
// TODO: would be nice if this was smart enough to understand the type of returnValues and do the needed conversions // TODO: would be nice if this was smart enough to understand the type of returnValues and do the needed conversions
const eventData = { const eventData = {
id: hash({eventName: eventKey, blockNumber: e.blockNumber, transactionIndex: e.transactionIndex, logIndex: e.logIndex}), id: hash({eventName: eventKey, blockNumber: e.blockNumber, transactionIndex: e.transactionIndex, logIndex: e.logIndex}),
data: e.data, data: e.data,
address: e.address, address: e.address,
topics: e.topics topics: e.topics,
removed: e.removed
} }
// TODO: test reorgs
sub.next({blockNumber: e.blockNumber, data: e.data, address: e.address, topics: e.topics}); sub.next({blockNumber: e.blockNumber, data: e.data, address: e.address, topics: e.topics});
if(e.removed){
this.db.deleteEvent(eventKey, id);
return;
}
if (this.db.eventExists(eventKey, eventData.id)) return; if (this.db.eventExists(eventKey, eventData.id)) return;
this.db.recordEvent(eventKey, eventData); this.db.recordEvent(eventKey, eventData);