Make tree generation optional

This commit is contained in:
Tornado Contrib 2024-10-21 22:00:14 +00:00
parent 6549fac5dd
commit 9f4044d110
Signed by: tornadocontrib
GPG Key ID: 60B4DF1A076C64B1
6 changed files with 89 additions and 28 deletions

10
dist/events/base.d.ts vendored

@ -78,7 +78,9 @@ export declare class BaseEventsService<EventType extends MinimalEvents> {
getLatestEvents({ fromBlock }: { getLatestEvents({ fromBlock }: {
fromBlock: number; fromBlock: number;
}): Promise<BaseEvents<EventType>>; }): Promise<BaseEvents<EventType>>;
validateEvents<S>({ events, lastBlock }: BaseEvents<EventType>): Promise<S>; validateEvents<S>({ events, lastBlock, hasNewEvents, }: BaseEvents<EventType> & {
hasNewEvents?: boolean;
}): Promise<S>;
/** /**
* Handle saving events * Handle saving events
*/ */
@ -96,6 +98,7 @@ export interface BaseTornadoServiceConstructor extends Omit<BaseEventsServiceCon
Tornado: Tornado; Tornado: Tornado;
amount: string; amount: string;
currency: string; currency: string;
optionalTree?: boolean;
merkleTreeService?: MerkleTreeService; merkleTreeService?: MerkleTreeService;
} }
export interface DepositsGraphParams extends BaseGraphParams { export interface DepositsGraphParams extends BaseGraphParams {
@ -105,6 +108,7 @@ export interface DepositsGraphParams extends BaseGraphParams {
export declare class BaseTornadoService extends BaseEventsService<DepositsEvents | WithdrawalsEvents> { export declare class BaseTornadoService extends BaseEventsService<DepositsEvents | WithdrawalsEvents> {
amount: string; amount: string;
currency: string; currency: string;
optionalTree?: boolean;
merkleTreeService?: MerkleTreeService; merkleTreeService?: MerkleTreeService;
batchTransactionService: BatchTransactionService; batchTransactionService: BatchTransactionService;
batchBlockService: BatchBlockService; batchBlockService: BatchBlockService;
@ -113,8 +117,8 @@ export declare class BaseTornadoService extends BaseEventsService<DepositsEvents
getGraphMethod(): string; getGraphMethod(): string;
getGraphParams(): DepositsGraphParams; getGraphParams(): DepositsGraphParams;
formatEvents(events: EventLog[]): Promise<(DepositsEvents | WithdrawalsEvents)[]>; formatEvents(events: EventLog[]): Promise<(DepositsEvents | WithdrawalsEvents)[]>;
validateEvents<S>({ events }: { validateEvents<S>({ events, hasNewEvents, }: BaseEvents<DepositsEvents | WithdrawalsEvents> & {
events: (DepositsEvents | WithdrawalsEvents)[]; hasNewEvents?: boolean;
}): Promise<S>; }): Promise<S>;
getLatestEvents({ fromBlock }: { getLatestEvents({ fromBlock }: {
fromBlock: number; fromBlock: number;

26
dist/index.js vendored

@ -2948,10 +2948,15 @@ class BaseEventsService {
lastBlock: rpcEvents.lastBlock lastBlock: rpcEvents.lastBlock
}; };
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars /* eslint-disable @typescript-eslint/no-unused-vars */
async validateEvents({ events, lastBlock }) { async validateEvents({
events,
lastBlock,
hasNewEvents
}) {
return void 0; return void 0;
} }
/* eslint-enable @typescript-eslint/no-unused-vars */
/** /**
* Handle saving events * Handle saving events
*/ */
@ -2981,7 +2986,11 @@ class BaseEventsService {
return !hasEvent; return !hasEvent;
}); });
const lastBlock = newEvents.lastBlock || allEvents[allEvents.length - 1]?.blockNumber; const lastBlock = newEvents.lastBlock || allEvents[allEvents.length - 1]?.blockNumber;
const validateResult = await this.validateEvents({ events: allEvents, lastBlock }); const validateResult = await this.validateEvents({
events: allEvents,
lastBlock,
hasNewEvents: Boolean(newEvents.events.length)
});
if (savedEvents.fromCache || newEvents.events.length) { if (savedEvents.fromCache || newEvents.events.length) {
await this.saveEvents({ events: allEvents, lastBlock }); await this.saveEvents({ events: allEvents, lastBlock });
} }
@ -2995,17 +3004,19 @@ class BaseEventsService {
class BaseTornadoService extends BaseEventsService { class BaseTornadoService extends BaseEventsService {
amount; amount;
currency; currency;
optionalTree;
merkleTreeService; merkleTreeService;
batchTransactionService; batchTransactionService;
batchBlockService; batchBlockService;
constructor(serviceConstructor) { constructor(serviceConstructor) {
const { Tornado: contract, amount, currency, provider, merkleTreeService } = serviceConstructor; const { Tornado: contract, amount, currency, provider, optionalTree, merkleTreeService } = serviceConstructor;
super({ super({
...serviceConstructor, ...serviceConstructor,
contract contract
}); });
this.amount = amount; this.amount = amount;
this.currency = currency; this.currency = currency;
this.optionalTree = optionalTree;
this.merkleTreeService = merkleTreeService; this.merkleTreeService = merkleTreeService;
this.batchTransactionService = new BatchTransactionService({ this.batchTransactionService = new BatchTransactionService({
provider, provider,
@ -3080,7 +3091,10 @@ class BaseTornadoService extends BaseEventsService {
}); });
} }
} }
async validateEvents({ events }) { async validateEvents({
events,
hasNewEvents
}) {
if (events.length && this.getType().toLowerCase() === DEPOSIT) { if (events.length && this.getType().toLowerCase() === DEPOSIT) {
const depositEvents = events; const depositEvents = events;
const lastEvent = depositEvents[depositEvents.length - 1]; const lastEvent = depositEvents[depositEvents.length - 1];
@ -3088,7 +3102,7 @@ class BaseTornadoService extends BaseEventsService {
const errMsg = `Deposit events invalid wants ${depositEvents.length - 1} leafIndex have ${lastEvent.leafIndex}`; const errMsg = `Deposit events invalid wants ${depositEvents.length - 1} leafIndex have ${lastEvent.leafIndex}`;
throw new Error(errMsg); throw new Error(errMsg);
} }
if (this.merkleTreeService) { if (this.merkleTreeService && (!this.optionalTree || hasNewEvents)) {
return await this.merkleTreeService.verifyTree(depositEvents); return await this.merkleTreeService.verifyTree(depositEvents);
} }
} }

26
dist/index.mjs vendored

@ -2927,10 +2927,15 @@ class BaseEventsService {
lastBlock: rpcEvents.lastBlock lastBlock: rpcEvents.lastBlock
}; };
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars /* eslint-disable @typescript-eslint/no-unused-vars */
async validateEvents({ events, lastBlock }) { async validateEvents({
events,
lastBlock,
hasNewEvents
}) {
return void 0; return void 0;
} }
/* eslint-enable @typescript-eslint/no-unused-vars */
/** /**
* Handle saving events * Handle saving events
*/ */
@ -2960,7 +2965,11 @@ class BaseEventsService {
return !hasEvent; return !hasEvent;
}); });
const lastBlock = newEvents.lastBlock || allEvents[allEvents.length - 1]?.blockNumber; const lastBlock = newEvents.lastBlock || allEvents[allEvents.length - 1]?.blockNumber;
const validateResult = await this.validateEvents({ events: allEvents, lastBlock }); const validateResult = await this.validateEvents({
events: allEvents,
lastBlock,
hasNewEvents: Boolean(newEvents.events.length)
});
if (savedEvents.fromCache || newEvents.events.length) { if (savedEvents.fromCache || newEvents.events.length) {
await this.saveEvents({ events: allEvents, lastBlock }); await this.saveEvents({ events: allEvents, lastBlock });
} }
@ -2974,17 +2983,19 @@ class BaseEventsService {
class BaseTornadoService extends BaseEventsService { class BaseTornadoService extends BaseEventsService {
amount; amount;
currency; currency;
optionalTree;
merkleTreeService; merkleTreeService;
batchTransactionService; batchTransactionService;
batchBlockService; batchBlockService;
constructor(serviceConstructor) { constructor(serviceConstructor) {
const { Tornado: contract, amount, currency, provider, merkleTreeService } = serviceConstructor; const { Tornado: contract, amount, currency, provider, optionalTree, merkleTreeService } = serviceConstructor;
super({ super({
...serviceConstructor, ...serviceConstructor,
contract contract
}); });
this.amount = amount; this.amount = amount;
this.currency = currency; this.currency = currency;
this.optionalTree = optionalTree;
this.merkleTreeService = merkleTreeService; this.merkleTreeService = merkleTreeService;
this.batchTransactionService = new BatchTransactionService({ this.batchTransactionService = new BatchTransactionService({
provider, provider,
@ -3059,7 +3070,10 @@ class BaseTornadoService extends BaseEventsService {
}); });
} }
} }
async validateEvents({ events }) { async validateEvents({
events,
hasNewEvents
}) {
if (events.length && this.getType().toLowerCase() === DEPOSIT) { if (events.length && this.getType().toLowerCase() === DEPOSIT) {
const depositEvents = events; const depositEvents = events;
const lastEvent = depositEvents[depositEvents.length - 1]; const lastEvent = depositEvents[depositEvents.length - 1];
@ -3067,7 +3081,7 @@ class BaseTornadoService extends BaseEventsService {
const errMsg = `Deposit events invalid wants ${depositEvents.length - 1} leafIndex have ${lastEvent.leafIndex}`; const errMsg = `Deposit events invalid wants ${depositEvents.length - 1} leafIndex have ${lastEvent.leafIndex}`;
throw new Error(errMsg); throw new Error(errMsg);
} }
if (this.merkleTreeService) { if (this.merkleTreeService && (!this.optionalTree || hasNewEvents)) {
return await this.merkleTreeService.verifyTree(depositEvents); return await this.merkleTreeService.verifyTree(depositEvents);
} }
} }

26
dist/tornado.umd.js vendored

@ -59366,10 +59366,15 @@ class BaseEventsService {
lastBlock: rpcEvents.lastBlock lastBlock: rpcEvents.lastBlock
}; };
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars /* eslint-disable @typescript-eslint/no-unused-vars */
async validateEvents({ events, lastBlock }) { async validateEvents({
events,
lastBlock,
hasNewEvents
}) {
return void 0; return void 0;
} }
/* eslint-enable @typescript-eslint/no-unused-vars */
/** /**
* Handle saving events * Handle saving events
*/ */
@ -59399,7 +59404,11 @@ class BaseEventsService {
return !hasEvent; return !hasEvent;
}); });
const lastBlock = newEvents.lastBlock || allEvents[allEvents.length - 1]?.blockNumber; const lastBlock = newEvents.lastBlock || allEvents[allEvents.length - 1]?.blockNumber;
const validateResult = await this.validateEvents({ events: allEvents, lastBlock }); const validateResult = await this.validateEvents({
events: allEvents,
lastBlock,
hasNewEvents: Boolean(newEvents.events.length)
});
if (savedEvents.fromCache || newEvents.events.length) { if (savedEvents.fromCache || newEvents.events.length) {
await this.saveEvents({ events: allEvents, lastBlock }); await this.saveEvents({ events: allEvents, lastBlock });
} }
@ -59413,17 +59422,19 @@ class BaseEventsService {
class BaseTornadoService extends BaseEventsService { class BaseTornadoService extends BaseEventsService {
amount; amount;
currency; currency;
optionalTree;
merkleTreeService; merkleTreeService;
batchTransactionService; batchTransactionService;
batchBlockService; batchBlockService;
constructor(serviceConstructor) { constructor(serviceConstructor) {
const { Tornado: contract, amount, currency, provider, merkleTreeService } = serviceConstructor; const { Tornado: contract, amount, currency, provider, optionalTree, merkleTreeService } = serviceConstructor;
super({ super({
...serviceConstructor, ...serviceConstructor,
contract contract
}); });
this.amount = amount; this.amount = amount;
this.currency = currency; this.currency = currency;
this.optionalTree = optionalTree;
this.merkleTreeService = merkleTreeService; this.merkleTreeService = merkleTreeService;
this.batchTransactionService = new _batch__WEBPACK_IMPORTED_MODULE_1__/* .BatchTransactionService */ .AF({ this.batchTransactionService = new _batch__WEBPACK_IMPORTED_MODULE_1__/* .BatchTransactionService */ .AF({
provider, provider,
@ -59498,7 +59509,10 @@ class BaseTornadoService extends BaseEventsService {
}); });
} }
} }
async validateEvents({ events }) { async validateEvents({
events,
hasNewEvents
}) {
if (events.length && this.getType().toLowerCase() === DEPOSIT) { if (events.length && this.getType().toLowerCase() === DEPOSIT) {
const depositEvents = events; const depositEvents = events;
const lastEvent = depositEvents[depositEvents.length - 1]; const lastEvent = depositEvents[depositEvents.length - 1];
@ -59506,7 +59520,7 @@ class BaseTornadoService extends BaseEventsService {
const errMsg = `Deposit events invalid wants ${depositEvents.length - 1} leafIndex have ${lastEvent.leafIndex}`; const errMsg = `Deposit events invalid wants ${depositEvents.length - 1} leafIndex have ${lastEvent.leafIndex}`;
throw new Error(errMsg); throw new Error(errMsg);
} }
if (this.merkleTreeService) { if (this.merkleTreeService && (!this.optionalTree || hasNewEvents)) {
return await this.merkleTreeService.verifyTree(depositEvents); return await this.merkleTreeService.verifyTree(depositEvents);
} }
} }

File diff suppressed because one or more lines are too long

@ -295,10 +295,15 @@ export class BaseEventsService<EventType extends MinimalEvents> {
}; };
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars /* eslint-disable @typescript-eslint/no-unused-vars */
async validateEvents<S>({ events, lastBlock }: BaseEvents<EventType>): Promise<S> { async validateEvents<S>({
events,
lastBlock,
hasNewEvents,
}: BaseEvents<EventType> & { hasNewEvents?: boolean }): Promise<S> {
return undefined as S; return undefined as S;
} }
/* eslint-enable @typescript-eslint/no-unused-vars */
/** /**
* Handle saving events * Handle saving events
@ -340,7 +345,11 @@ export class BaseEventsService<EventType extends MinimalEvents> {
const lastBlock = newEvents.lastBlock || allEvents[allEvents.length - 1]?.blockNumber; const lastBlock = newEvents.lastBlock || allEvents[allEvents.length - 1]?.blockNumber;
const validateResult = await this.validateEvents<S>({ events: allEvents, lastBlock }); const validateResult = await this.validateEvents<S>({
events: allEvents,
lastBlock,
hasNewEvents: Boolean(newEvents.events.length),
});
// If the events are loaded from cache or we have found new events, save them // If the events are loaded from cache or we have found new events, save them
if ((savedEvents as CachedEvents<EventType>).fromCache || newEvents.events.length) { if ((savedEvents as CachedEvents<EventType>).fromCache || newEvents.events.length) {
@ -359,6 +368,7 @@ export interface BaseTornadoServiceConstructor extends Omit<BaseEventsServiceCon
Tornado: Tornado; Tornado: Tornado;
amount: string; amount: string;
currency: string; currency: string;
optionalTree?: boolean;
merkleTreeService?: MerkleTreeService; merkleTreeService?: MerkleTreeService;
} }
@ -371,12 +381,13 @@ export class BaseTornadoService extends BaseEventsService<DepositsEvents | Withd
amount: string; amount: string;
currency: string; currency: string;
optionalTree?: boolean;
merkleTreeService?: MerkleTreeService; merkleTreeService?: MerkleTreeService;
batchTransactionService: BatchTransactionService; batchTransactionService: BatchTransactionService;
batchBlockService: BatchBlockService; batchBlockService: BatchBlockService;
constructor(serviceConstructor: BaseTornadoServiceConstructor) { constructor(serviceConstructor: BaseTornadoServiceConstructor) {
const { Tornado: contract, amount, currency, provider, merkleTreeService } = serviceConstructor; const { Tornado: contract, amount, currency, provider, optionalTree, merkleTreeService } = serviceConstructor;
super({ super({
...serviceConstructor, ...serviceConstructor,
@ -386,6 +397,7 @@ export class BaseTornadoService extends BaseEventsService<DepositsEvents | Withd
this.amount = amount; this.amount = amount;
this.currency = currency; this.currency = currency;
this.optionalTree = optionalTree;
this.merkleTreeService = merkleTreeService; this.merkleTreeService = merkleTreeService;
this.batchTransactionService = new BatchTransactionService({ this.batchTransactionService = new BatchTransactionService({
@ -475,7 +487,10 @@ export class BaseTornadoService extends BaseEventsService<DepositsEvents | Withd
} }
} }
async validateEvents<S>({ events }: { events: (DepositsEvents | WithdrawalsEvents)[] }) { async validateEvents<S>({
events,
hasNewEvents,
}: BaseEvents<DepositsEvents | WithdrawalsEvents> & { hasNewEvents?: boolean }) {
if (events.length && this.getType().toLowerCase() === DEPOSIT) { if (events.length && this.getType().toLowerCase() === DEPOSIT) {
const depositEvents = events as DepositsEvents[]; const depositEvents = events as DepositsEvents[];
@ -486,7 +501,7 @@ export class BaseTornadoService extends BaseEventsService<DepositsEvents | Withd
throw new Error(errMsg); throw new Error(errMsg);
} }
if (this.merkleTreeService) { if (this.merkleTreeService && (!this.optionalTree || hasNewEvents)) {
return (await this.merkleTreeService.verifyTree(depositEvents)) as S; return (await this.merkleTreeService.verifyTree(depositEvents)) as S;
} }
} }