mirror of
https://github.com/embarklabs/subspace.git
synced 2025-01-10 22:36:16 +00:00
fix: check if collection exists and handle reorgs in log syncer (#40)
This commit is contained in:
parent
6da7c605f8
commit
e5ec647e44
@ -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) {
|
||||||
|
@ -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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user