Skip to content

Commit

Permalink
Fix potential indexer gap
Browse files Browse the repository at this point in the history
  • Loading branch information
AllFi committed Jun 5, 2024
1 parent f0a7988 commit 3c540d2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
18 changes: 6 additions & 12 deletions zp-relayer/pool/BasePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export abstract class BasePool<N extends Network = Network> {
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()
Expand All @@ -166,10 +166,10 @@ export abstract class BasePool<N extends Network = Network> {

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()
Expand Down Expand Up @@ -217,23 +217,17 @@ export abstract class BasePool<N extends Network = Network> {
})
}

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,
})) {
Expand Down
3 changes: 2 additions & 1 deletion zp-relayer/pool/DefaultPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions zp-relayer/pool/FinalizerPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -38,7 +38,7 @@ export class FinalizerPool extends BasePool {
async buildFinalizeTx({
transaction: { outCommit },
}: PoolTx<WorkerTxType.Finalize>): Promise<ProcessResult<FinalizerPool>> {
await this.syncState(undefined, this.indexerUrl)
await this.syncState(undefined, undefined, this.indexerUrl)

const func = 'proveTreeUpdate(uint256,uint256[8],uint256)'

Expand Down
6 changes: 3 additions & 3 deletions zp-relayer/pool/IndexerPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions zp-relayer/services/indexer/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 3c540d2

Please sign in to comment.