From 3c540d2fc0fcd98a604947cbcb7f57779218fe6e Mon Sep 17 00:00:00 2001 From: Alexander Filippov Date: Wed, 5 Jun 2024 16:54:12 +0300 Subject: [PATCH] Fix potential indexer gap --- zp-relayer/pool/BasePool.ts | 18 ++++++------------ zp-relayer/pool/DefaultPool.ts | 3 ++- zp-relayer/pool/FinalizerPool.ts | 4 ++-- zp-relayer/pool/IndexerPool.ts | 6 +++--- zp-relayer/services/indexer/init.ts | 5 +++-- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/zp-relayer/pool/BasePool.ts b/zp-relayer/pool/BasePool.ts index 3868025d..ff90742b 100644 --- a/zp-relayer/pool/BasePool.ts +++ b/zp-relayer/pool/BasePool.ts @@ -143,7 +143,7 @@ export abstract class BasePool { return lastBlockNumber } - async syncState(startBlock?: number, indexerUrl?: string) { + async syncState(startBlock?: number, lastBlock?: number, indexerUrl?: string) { logger.debug('Syncing state; starting from block %d', startBlock) const localIndex = this.state.getNextIndex() @@ -166,10 +166,10 @@ export abstract class BasePool { if (indexerUrl) { await this.syncStateFromIndexer(indexerUrl) - } else if (startBlock) { - await this.syncStateFromContract(startBlock, contractIndex, localIndex) + } else if (startBlock && lastBlock) { + await this.syncStateFromContract(startBlock, lastBlock, contractIndex, localIndex) } else { - throw new Error('Either startBlock or indexerUrl should be provided for sync') + throw new Error('Either (startBlock, lastBlock) or indexerUrl should be provided for sync') } const newLocalIndex = this.state.getNextIndex() @@ -217,23 +217,17 @@ export abstract class BasePool { }) } - async syncStateFromContract(startBlock: number, contractIndex: number, localIndex: number) { + async syncStateFromContract(startBlock: number, lastBlock: number, contractIndex: number, localIndex: number) { const numTxs = Math.floor((contractIndex - localIndex) / OUTPLUSONE) if (numTxs < 0) { // TODO: rollback state throw new Error('State is corrupted, contract index is less than local index') } - const missedIndices = Array(numTxs) - for (let i = 0; i < numTxs; i++) { - missedIndices[i] = localIndex + (i + 1) * OUTPLUSONE - } - - const lastBlockNumber = (await this.getLastBlockToProcess()) + 1 for await (const batch of this.network.getEvents({ contract: this.network.pool, startBlock, - lastBlock: lastBlockNumber, + lastBlock, event: 'Message', batchSize: this.config.eventsBatchSize, })) { diff --git a/zp-relayer/pool/DefaultPool.ts b/zp-relayer/pool/DefaultPool.ts index 42a37f16..96d4c411 100644 --- a/zp-relayer/pool/DefaultPool.ts +++ b/zp-relayer/pool/DefaultPool.ts @@ -75,7 +75,8 @@ export class DefaultPool extends BasePool { } await this.permitRecover?.initializeDomain() if (startBlock) { - await this.syncState(startBlock) + const lastBlock = await this.getLastBlockToProcess() + await this.syncState(startBlock, lastBlock) } this.isInitialized = true } diff --git a/zp-relayer/pool/FinalizerPool.ts b/zp-relayer/pool/FinalizerPool.ts index 3c744776..029a8110 100644 --- a/zp-relayer/pool/FinalizerPool.ts +++ b/zp-relayer/pool/FinalizerPool.ts @@ -28,7 +28,7 @@ export class FinalizerPool extends BasePool { this.denominator = toBN(await this.network.pool.call('denominator')) this.poolId = toBN(await this.network.pool.call('pool_id')) - await this.syncState(undefined, indexerUrl) + await this.syncState(undefined, undefined, indexerUrl) this.isInitialized = true } @@ -38,7 +38,7 @@ export class FinalizerPool extends BasePool { async buildFinalizeTx({ transaction: { outCommit }, }: PoolTx): Promise> { - await this.syncState(undefined, this.indexerUrl) + await this.syncState(undefined, undefined, this.indexerUrl) const func = 'proveTreeUpdate(uint256,uint256[8],uint256)' diff --git a/zp-relayer/pool/IndexerPool.ts b/zp-relayer/pool/IndexerPool.ts index 4cbc29fe..a673331e 100644 --- a/zp-relayer/pool/IndexerPool.ts +++ b/zp-relayer/pool/IndexerPool.ts @@ -6,14 +6,14 @@ import { type PermitRecover } from '@/utils/permit/types' export class IndexerPool extends BasePool { public permitRecover: PermitRecover | null = null - async init(startBlock: number | null = null) { + async init(startBlock: number | null = null, lastBlock: number | null = null) { if (this.isInitialized) return this.denominator = toBN(await this.network.pool.call('denominator')) this.poolId = toBN(await this.network.pool.call('pool_id')) - if (startBlock) { - await this.syncState(startBlock) + if (startBlock && lastBlock) { + await this.syncState(startBlock, lastBlock) } this.isInitialized = true } diff --git a/zp-relayer/services/indexer/init.ts b/zp-relayer/services/indexer/init.ts index 163b3d30..12abd7a2 100644 --- a/zp-relayer/services/indexer/init.ts +++ b/zp-relayer/services/indexer/init.ts @@ -12,9 +12,10 @@ export async function init() { eventsBatchSize: config.base.COMMON_EVENTS_PROCESSING_BATCH_SIZE, }) - await Promise.all([networkBackend.init(), pool.init(config.base.COMMON_START_BLOCK)]) + const lastInitialSyncBlock = await pool.getLastBlockToProcess() + await Promise.all([networkBackend.init(), pool.init(config.base.COMMON_START_BLOCK, lastInitialSyncBlock)]) - const startBlock = await pool.getLastBlockToProcess() + const startBlock = lastInitialSyncBlock + 1 const watcher = new Watcher(networkBackend, networkBackend.pool, 'pool-indexer', { event: 'allEvents', blockConfirmations: parseInt(process.env.INDEXER_BLOCK_CONFIRMATIONS || '1'),