tornado-core 1.0.9

optimize getting events
This commit is contained in:
Tornado Contrib 2024-09-22 03:16:12 +00:00
parent 2bd991aa45
commit 0bd87f9b67
Signed by: tornadocontrib
GPG Key ID: 60B4DF1A076C64B1
6 changed files with 22 additions and 82 deletions

@ -82,7 +82,7 @@ export declare class BaseEventsService<EventType extends MinimalEvents> {
*/ */
updateEvents(): Promise<{ updateEvents(): Promise<{
events: EventType[]; events: EventType[];
lastBlock: number | null; lastBlock: number;
}>; }>;
} }
export type BaseTornadoServiceConstructor = { export type BaseTornadoServiceConstructor = {

22
dist/index.js vendored

@ -2907,12 +2907,6 @@ class BaseEventsService {
const events = yield this.formatEvents( const events = yield this.formatEvents(
yield this.batchEventsService.getBatchEvents({ fromBlock, toBlock, type: this.getType() }) yield this.batchEventsService.getBatchEvents({ fromBlock, toBlock, type: this.getType() })
); );
if (!events.length) {
return {
events,
lastBlock: toBlock
};
}
return { return {
events, events,
lastBlock: toBlock lastBlock: toBlock
@ -2928,16 +2922,12 @@ class BaseEventsService {
} }
getLatestEvents(_0) { getLatestEvents(_0) {
return __async$8(this, arguments, function* ({ fromBlock }) { return __async$8(this, arguments, function* ({ fromBlock }) {
const allEvents = [];
const graphEvents = yield this.getEventsFromGraph({ fromBlock }); const graphEvents = yield this.getEventsFromGraph({ fromBlock });
const lastSyncBlock = graphEvents.lastBlock && graphEvents.lastBlock >= fromBlock ? graphEvents.lastBlock : fromBlock; const lastSyncBlock = graphEvents.lastBlock && graphEvents.lastBlock >= fromBlock ? graphEvents.lastBlock : fromBlock;
const rpcEvents = yield this.getEventsFromRpc({ fromBlock: lastSyncBlock }); const rpcEvents = yield this.getEventsFromRpc({ fromBlock: lastSyncBlock });
allEvents.push(...graphEvents.events);
allEvents.push(...rpcEvents.events);
const lastBlock = rpcEvents ? rpcEvents.lastBlock : allEvents[allEvents.length - 1] ? allEvents[allEvents.length - 1].blockNumber : fromBlock;
return { return {
events: allEvents, events: [...graphEvents.events, ...rpcEvents.events],
lastBlock lastBlock: rpcEvents.lastBlock
}; };
}); });
} }
@ -2957,6 +2947,7 @@ class BaseEventsService {
*/ */
updateEvents() { updateEvents() {
return __async$8(this, null, function* () { return __async$8(this, null, function* () {
var _a;
const savedEvents = yield this.getSavedEvents(); const savedEvents = yield this.getSavedEvents();
let fromBlock = this.deployedBlock; let fromBlock = this.deployedBlock;
if (savedEvents && savedEvents.lastBlock) { if (savedEvents && savedEvents.lastBlock) {
@ -2964,10 +2955,7 @@ class BaseEventsService {
} }
const newEvents = yield this.getLatestEvents({ fromBlock }); const newEvents = yield this.getLatestEvents({ fromBlock });
const eventSet = /* @__PURE__ */ new Set(); const eventSet = /* @__PURE__ */ new Set();
let allEvents = []; const allEvents = [...savedEvents.events, ...newEvents.events].sort((a, b) => {
allEvents.push(...savedEvents.events);
allEvents.push(...newEvents.events);
allEvents = allEvents.sort((a, b) => {
if (a.blockNumber === b.blockNumber) { if (a.blockNumber === b.blockNumber) {
return a.logIndex - b.logIndex; return a.logIndex - b.logIndex;
} }
@ -2978,7 +2966,7 @@ class BaseEventsService {
eventSet.add(eventKey); eventSet.add(eventKey);
return !hasEvent; return !hasEvent;
}); });
const lastBlock = newEvents ? newEvents.lastBlock : allEvents[allEvents.length - 1] ? allEvents[allEvents.length - 1].blockNumber : null; const lastBlock = newEvents.lastBlock || ((_a = allEvents[allEvents.length - 1]) == null ? void 0 : _a.blockNumber);
this.validateEvents({ events: allEvents, lastBlock }); this.validateEvents({ events: allEvents, lastBlock });
if (savedEvents.fromCache || newEvents.events.length) { if (savedEvents.fromCache || newEvents.events.length) {
yield this.saveEvents({ events: allEvents, lastBlock }); yield this.saveEvents({ events: allEvents, lastBlock });

22
dist/index.mjs vendored

@ -2886,12 +2886,6 @@ class BaseEventsService {
const events = yield this.formatEvents( const events = yield this.formatEvents(
yield this.batchEventsService.getBatchEvents({ fromBlock, toBlock, type: this.getType() }) yield this.batchEventsService.getBatchEvents({ fromBlock, toBlock, type: this.getType() })
); );
if (!events.length) {
return {
events,
lastBlock: toBlock
};
}
return { return {
events, events,
lastBlock: toBlock lastBlock: toBlock
@ -2907,16 +2901,12 @@ class BaseEventsService {
} }
getLatestEvents(_0) { getLatestEvents(_0) {
return __async$8(this, arguments, function* ({ fromBlock }) { return __async$8(this, arguments, function* ({ fromBlock }) {
const allEvents = [];
const graphEvents = yield this.getEventsFromGraph({ fromBlock }); const graphEvents = yield this.getEventsFromGraph({ fromBlock });
const lastSyncBlock = graphEvents.lastBlock && graphEvents.lastBlock >= fromBlock ? graphEvents.lastBlock : fromBlock; const lastSyncBlock = graphEvents.lastBlock && graphEvents.lastBlock >= fromBlock ? graphEvents.lastBlock : fromBlock;
const rpcEvents = yield this.getEventsFromRpc({ fromBlock: lastSyncBlock }); const rpcEvents = yield this.getEventsFromRpc({ fromBlock: lastSyncBlock });
allEvents.push(...graphEvents.events);
allEvents.push(...rpcEvents.events);
const lastBlock = rpcEvents ? rpcEvents.lastBlock : allEvents[allEvents.length - 1] ? allEvents[allEvents.length - 1].blockNumber : fromBlock;
return { return {
events: allEvents, events: [...graphEvents.events, ...rpcEvents.events],
lastBlock lastBlock: rpcEvents.lastBlock
}; };
}); });
} }
@ -2936,6 +2926,7 @@ class BaseEventsService {
*/ */
updateEvents() { updateEvents() {
return __async$8(this, null, function* () { return __async$8(this, null, function* () {
var _a;
const savedEvents = yield this.getSavedEvents(); const savedEvents = yield this.getSavedEvents();
let fromBlock = this.deployedBlock; let fromBlock = this.deployedBlock;
if (savedEvents && savedEvents.lastBlock) { if (savedEvents && savedEvents.lastBlock) {
@ -2943,10 +2934,7 @@ class BaseEventsService {
} }
const newEvents = yield this.getLatestEvents({ fromBlock }); const newEvents = yield this.getLatestEvents({ fromBlock });
const eventSet = /* @__PURE__ */ new Set(); const eventSet = /* @__PURE__ */ new Set();
let allEvents = []; const allEvents = [...savedEvents.events, ...newEvents.events].sort((a, b) => {
allEvents.push(...savedEvents.events);
allEvents.push(...newEvents.events);
allEvents = allEvents.sort((a, b) => {
if (a.blockNumber === b.blockNumber) { if (a.blockNumber === b.blockNumber) {
return a.logIndex - b.logIndex; return a.logIndex - b.logIndex;
} }
@ -2957,7 +2945,7 @@ class BaseEventsService {
eventSet.add(eventKey); eventSet.add(eventKey);
return !hasEvent; return !hasEvent;
}); });
const lastBlock = newEvents ? newEvents.lastBlock : allEvents[allEvents.length - 1] ? allEvents[allEvents.length - 1].blockNumber : null; const lastBlock = newEvents.lastBlock || ((_a = allEvents[allEvents.length - 1]) == null ? void 0 : _a.blockNumber);
this.validateEvents({ events: allEvents, lastBlock }); this.validateEvents({ events: allEvents, lastBlock });
if (savedEvents.fromCache || newEvents.events.length) { if (savedEvents.fromCache || newEvents.events.length) {
yield this.saveEvents({ events: allEvents, lastBlock }); yield this.saveEvents({ events: allEvents, lastBlock });

22
dist/index.umd.js vendored

@ -59255,12 +59255,6 @@ class BaseEventsService {
const events = yield this.formatEvents( const events = yield this.formatEvents(
yield this.batchEventsService.getBatchEvents({ fromBlock, toBlock, type: this.getType() }) yield this.batchEventsService.getBatchEvents({ fromBlock, toBlock, type: this.getType() })
); );
if (!events.length) {
return {
events,
lastBlock: toBlock
};
}
return { return {
events, events,
lastBlock: toBlock lastBlock: toBlock
@ -59276,16 +59270,12 @@ class BaseEventsService {
} }
getLatestEvents(_0) { getLatestEvents(_0) {
return __async(this, arguments, function* ({ fromBlock }) { return __async(this, arguments, function* ({ fromBlock }) {
const allEvents = [];
const graphEvents = yield this.getEventsFromGraph({ fromBlock }); const graphEvents = yield this.getEventsFromGraph({ fromBlock });
const lastSyncBlock = graphEvents.lastBlock && graphEvents.lastBlock >= fromBlock ? graphEvents.lastBlock : fromBlock; const lastSyncBlock = graphEvents.lastBlock && graphEvents.lastBlock >= fromBlock ? graphEvents.lastBlock : fromBlock;
const rpcEvents = yield this.getEventsFromRpc({ fromBlock: lastSyncBlock }); const rpcEvents = yield this.getEventsFromRpc({ fromBlock: lastSyncBlock });
allEvents.push(...graphEvents.events);
allEvents.push(...rpcEvents.events);
const lastBlock = rpcEvents ? rpcEvents.lastBlock : allEvents[allEvents.length - 1] ? allEvents[allEvents.length - 1].blockNumber : fromBlock;
return { return {
events: allEvents, events: [...graphEvents.events, ...rpcEvents.events],
lastBlock lastBlock: rpcEvents.lastBlock
}; };
}); });
} }
@ -59305,6 +59295,7 @@ class BaseEventsService {
*/ */
updateEvents() { updateEvents() {
return __async(this, null, function* () { return __async(this, null, function* () {
var _a;
const savedEvents = yield this.getSavedEvents(); const savedEvents = yield this.getSavedEvents();
let fromBlock = this.deployedBlock; let fromBlock = this.deployedBlock;
if (savedEvents && savedEvents.lastBlock) { if (savedEvents && savedEvents.lastBlock) {
@ -59312,10 +59303,7 @@ class BaseEventsService {
} }
const newEvents = yield this.getLatestEvents({ fromBlock }); const newEvents = yield this.getLatestEvents({ fromBlock });
const eventSet = /* @__PURE__ */ new Set(); const eventSet = /* @__PURE__ */ new Set();
let allEvents = []; const allEvents = [...savedEvents.events, ...newEvents.events].sort((a, b) => {
allEvents.push(...savedEvents.events);
allEvents.push(...newEvents.events);
allEvents = allEvents.sort((a, b) => {
if (a.blockNumber === b.blockNumber) { if (a.blockNumber === b.blockNumber) {
return a.logIndex - b.logIndex; return a.logIndex - b.logIndex;
} }
@ -59326,7 +59314,7 @@ class BaseEventsService {
eventSet.add(eventKey); eventSet.add(eventKey);
return !hasEvent; return !hasEvent;
}); });
const lastBlock = newEvents ? newEvents.lastBlock : allEvents[allEvents.length - 1] ? allEvents[allEvents.length - 1].blockNumber : null; const lastBlock = newEvents.lastBlock || ((_a = allEvents[allEvents.length - 1]) == null ? void 0 : _a.blockNumber);
this.validateEvents({ events: allEvents, lastBlock }); this.validateEvents({ events: allEvents, lastBlock });
if (savedEvents.fromCache || newEvents.events.length) { if (savedEvents.fromCache || newEvents.events.length) {
yield this.saveEvents({ events: allEvents, lastBlock }); yield this.saveEvents({ events: allEvents, lastBlock });

@ -1,6 +1,6 @@
{ {
"name": "@tornado/core", "name": "@tornado/core",
"version": "1.0.8", "version": "1.0.9",
"description": "An SDK for building applications on top of Privacy Pools", "description": "An SDK for building applications on top of Privacy Pools",
"main": "./dist/index.js", "main": "./dist/index.js",
"module": "./dist/index.mjs", "module": "./dist/index.mjs",

@ -244,13 +244,6 @@ export class BaseEventsService<EventType extends MinimalEvents> {
await this.batchEventsService.getBatchEvents({ fromBlock, toBlock, type: this.getType() }), await this.batchEventsService.getBatchEvents({ fromBlock, toBlock, type: this.getType() }),
); );
if (!events.length) {
return {
events,
lastBlock: toBlock,
};
}
return { return {
events, events,
lastBlock: toBlock, lastBlock: toBlock,
@ -265,22 +258,13 @@ export class BaseEventsService<EventType extends MinimalEvents> {
} }
async getLatestEvents({ fromBlock }: { fromBlock: number }): Promise<BaseEvents<EventType>> { async getLatestEvents({ fromBlock }: { fromBlock: number }): Promise<BaseEvents<EventType>> {
const allEvents = [];
const graphEvents = await this.getEventsFromGraph({ fromBlock }); const graphEvents = await this.getEventsFromGraph({ fromBlock });
const lastSyncBlock = const lastSyncBlock =
graphEvents.lastBlock && graphEvents.lastBlock >= fromBlock ? graphEvents.lastBlock : fromBlock; graphEvents.lastBlock && graphEvents.lastBlock >= fromBlock ? graphEvents.lastBlock : fromBlock;
const rpcEvents = await this.getEventsFromRpc({ fromBlock: lastSyncBlock }); const rpcEvents = await this.getEventsFromRpc({ fromBlock: lastSyncBlock });
allEvents.push(...graphEvents.events);
allEvents.push(...rpcEvents.events);
const lastBlock = rpcEvents
? rpcEvents.lastBlock
: allEvents[allEvents.length - 1]
? allEvents[allEvents.length - 1].blockNumber
: fromBlock;
return { return {
events: allEvents, events: [...graphEvents.events, ...rpcEvents.events],
lastBlock, lastBlock: rpcEvents.lastBlock,
}; };
} }
@ -311,12 +295,7 @@ export class BaseEventsService<EventType extends MinimalEvents> {
const eventSet = new Set(); const eventSet = new Set();
let allEvents: EventType[] = []; const allEvents: EventType[] = [...savedEvents.events, ...newEvents.events]
allEvents.push(...savedEvents.events);
allEvents.push(...newEvents.events);
allEvents = allEvents
.sort((a, b) => { .sort((a, b) => {
if (a.blockNumber === b.blockNumber) { if (a.blockNumber === b.blockNumber) {
return a.logIndex - b.logIndex; return a.logIndex - b.logIndex;
@ -329,11 +308,8 @@ export class BaseEventsService<EventType extends MinimalEvents> {
eventSet.add(eventKey); eventSet.add(eventKey);
return !hasEvent; return !hasEvent;
}); });
const lastBlock = newEvents
? newEvents.lastBlock const lastBlock = newEvents.lastBlock || allEvents[allEvents.length - 1]?.blockNumber;
: allEvents[allEvents.length - 1]
? allEvents[allEvents.length - 1].blockNumber
: null;
this.validateEvents({ events: allEvents, lastBlock }); this.validateEvents({ events: allEvents, lastBlock });