From 129b9e71f05d000f18ae86a2e4914c59073f178e Mon Sep 17 00:00:00 2001 From: nikugogoi Date: Wed, 13 Jul 2022 14:40:42 +0530 Subject: [PATCH] Add option for no check in max events block range (#142) --- packages/mobymask-watcher/src/hooks.ts | 22 +++++++++++----------- packages/mobymask-watcher/src/indexer.ts | 4 +++- packages/util/src/indexer.ts | 8 ++++---- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/mobymask-watcher/src/hooks.ts b/packages/mobymask-watcher/src/hooks.ts index c06962fb..0bcd8a58 100644 --- a/packages/mobymask-watcher/src/hooks.ts +++ b/packages/mobymask-watcher/src/hooks.ts @@ -83,16 +83,16 @@ export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Pr // Perform indexing for PhisherStatusUpdated and MemberStatusUpdated. if (['PhisherStatusUpdatedEvent', 'MemberStatusUpdatedEvent'].includes(eventData.event.__typename)) { const txData = await indexer.getFullTransaction(eventData.tx.hash); - const tx = getTx(indexer, KIND_PHISHERREGISTRY, txData.input); - let txs = [tx]; + const txDescription = getTxDescription(indexer, KIND_PHISHERREGISTRY, txData.input); + let txDescriptions = [txDescription]; - if (tx.signature === INVOKE_SIGNATURE) { + if (txDescription.signature === INVOKE_SIGNATURE) { // Parse transactions from batches if it is an invoke method in Delegatable contract. - txs = tx.args.signedInvocations + txDescriptions = txDescription.args.signedInvocations .reduce((txs: utils.TransactionDescription[], signedInvocation: any) => { // Get transactions from signed invocations batch. const batchTxs = signedInvocation.invocations.batch.map((invocation: any) => { - return getTx(indexer, KIND_PHISHERREGISTRY, invocation.transaction.data); + return getTxDescription(indexer, KIND_PHISHERREGISTRY, invocation.transaction.data); }); txs.push(...batchTxs); @@ -102,19 +102,19 @@ export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Pr } // Filter transactions for claimIfMember and claimIsPhisher methods. - txs = txs.filter((tx: utils.TransactionDescription) => { + txDescriptions = txDescriptions.filter((tx: utils.TransactionDescription) => { return [CLAIM_IF_MEMBER_SIGNATURE, CLAIM_IF_PHISHER_SIGNATURE].includes(tx.signature); }); - for (const tx of txs) { - switch (tx.signature) { + for (const txDescription of txDescriptions) { + switch (txDescription.signature) { case CLAIM_IF_MEMBER_SIGNATURE: // Update isMember entry for the identifier in database. - await indexer.isMember(eventData.block.hash, eventData.contract, tx.args.identifier, true); + await indexer.isMember(eventData.block.hash, eventData.contract, txDescription.args.identifier, true); break; case CLAIM_IF_PHISHER_SIGNATURE: // Update isPhisher entry for the identifier in database. - await indexer.isPhisher(eventData.block.hash, eventData.contract, tx.args.identifier, true); + await indexer.isPhisher(eventData.block.hash, eventData.contract, txDescription.args.identifier, true); break; } } @@ -122,7 +122,7 @@ export async function handleEvent (indexer: Indexer, eventData: ResultEvent): Pr } // Get transaction details from input data. -const getTx = (indexer: Indexer, contractKind: string, data: string): utils.TransactionDescription => { +const getTxDescription = (indexer: Indexer, contractKind: string, data: string): utils.TransactionDescription => { const contractInterface = indexer.getContractInterface(contractKind); assert(contractInterface); return contractInterface.parseTransaction({ data }); diff --git a/packages/mobymask-watcher/src/indexer.ts b/packages/mobymask-watcher/src/indexer.ts index 38c613a0..f3f5e03d 100644 --- a/packages/mobymask-watcher/src/indexer.ts +++ b/packages/mobymask-watcher/src/indexer.ts @@ -50,6 +50,8 @@ const MEMBERSTATUSUPDATED_EVENT = 'MemberStatusUpdated'; const OWNERSHIPTRANSFERRED_EVENT = 'OwnershipTransferred'; const PHISHERSTATUSUPDATED_EVENT = 'PhisherStatusUpdated'; +const MAX_EVENTS_BLOCK_RANGE = -1; + export type ResultEvent = { block: { cid: string; @@ -711,7 +713,7 @@ export class Indexer implements IPLDIndexerInterface { } async getEventsInRange (fromBlockNumber: number, toBlockNumber: number): Promise> { - return this._baseIndexer.getEventsInRange(fromBlockNumber, toBlockNumber); + return this._baseIndexer.getEventsInRange(fromBlockNumber, toBlockNumber, MAX_EVENTS_BLOCK_RANGE); } async getSyncStatus (): Promise { diff --git a/packages/util/src/indexer.ts b/packages/util/src/indexer.ts index 3d8e8272..17bd1cad 100644 --- a/packages/util/src/indexer.ts +++ b/packages/util/src/indexer.ts @@ -15,7 +15,7 @@ import { UNKNOWN_EVENT_NAME, JOB_KIND_CONTRACT, QUEUE_EVENT_PROCESSING } from '. import { JobQueue } from './job-queue'; import { Where, QueryOptions } from './database'; -const MAX_EVENTS_BLOCK_RANGE = 1000; +const DEFAULT_MAX_EVENTS_BLOCK_RANGE = 1000; const log = debug('vulcanize:indexer'); @@ -292,13 +292,13 @@ export class Indexer { return this._db.getProcessedBlockCountForRange(fromBlockNumber, toBlockNumber); } - async getEventsInRange (fromBlockNumber: number, toBlockNumber: number): Promise> { + async getEventsInRange (fromBlockNumber: number, toBlockNumber: number, maxBlockRange: number = DEFAULT_MAX_EVENTS_BLOCK_RANGE): Promise> { if (toBlockNumber <= fromBlockNumber) { throw new Error('toBlockNumber should be greater than fromBlockNumber'); } - if ((toBlockNumber - fromBlockNumber) > MAX_EVENTS_BLOCK_RANGE) { - throw new Error(`Max range (${MAX_EVENTS_BLOCK_RANGE}) exceeded`); + if (maxBlockRange > -1 && (toBlockNumber - fromBlockNumber) > maxBlockRange) { + throw new Error(`Max range (${maxBlockRange}) exceeded`); } return this._db.getEventsInRange(fromBlockNumber, toBlockNumber);