Skip to content

Commit 08e2549

Browse files
committed
fix(crosscheck): should skip crosscheck when toBlock + delayBlockFromLatest > latestblocknum
1 parent 9dd30c8 commit 08e2549

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

packages/orap/src/signal/event.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ export class EventSignal implements Signal {
124124
&& !(provider instanceof ethers.WebSocketProvider)
125125
)
126126
throw new Error('crosscheckProvider must be an instance of RekuProviderManager or ethers.JsonRpcProvider or ethers.WebSocketProvider')
127-
128127
this.crosschecker = new AutoCrossChecker(provider)
129-
await this.crosschecker.start(this.crosscheckerOptions)
128+
this.crosschecker.start(this.crosscheckerOptions)
130129
}
131130
}

packages/reku/src/event/crosschecker/autochecker.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ethers } from 'ethers'
2-
import { polling, sleep, timeoutWithRetry } from '@ora-io/utils'
2+
import { polling, timeoutWithRetry } from '@ora-io/utils'
33
import { ETH_BLOCK_INTERVAL } from '../../constants'
44
import type { Providers } from '../../types/w3'
55
import { debug } from '../../debug'
@@ -53,20 +53,21 @@ export class AutoCrossChecker extends BaseCrossChecker {
5353
* @param options
5454
*/
5555
async start(options: AutoCrossCheckParam) {
56+
debug('auto crosscheck start with options: %O', options)
5657
this.validate(options)
5758

5859
this.cache = new CrossCheckerCacheManager(options?.store, { keyPrefix: options?.storeKeyPrefix, ttl: options?.storeTtl })
5960

60-
const latestblocknum = await timeoutWithRetry(() => this.provider.provider?.getBlockNumber(), 15 * 1000, 3)
61+
let latestblocknum = await timeoutWithRetry(() => this.provider.provider?.getBlockNumber(), 15 * 1000, 3)
6162

6263
// resume checkpoint priority: options.fromBlock > cache > latestblocknum + 1
63-
const defaultInitCheckpoint = await this.cache.getCheckpoint() ?? (latestblocknum + 1)
64+
const defaultInitCheckpoint = await this.cache.getCheckpoint() ?? (latestblocknum)
6465

6566
const {
6667
fromBlock = defaultInitCheckpoint,
6768
batchBlocksCount = 10,
6869
pollingInterval = 3000,
69-
blockInterval = ETH_BLOCK_INTERVAL,
70+
// blockInterval = ETH_BLOCK_INTERVAL,
7071
delayBlockFromLatest = 1,
7172
toBlock,
7273
} = options
@@ -86,17 +87,20 @@ export class AutoCrossChecker extends BaseCrossChecker {
8687
}
8788

8889
const waitNextCrosscheck = async (): Promise<boolean> => {
89-
const latestblocknum = await timeoutWithRetry(() => this.provider.provider?.getBlockNumber(), 15 * 1000, 3)
90+
latestblocknum = await timeoutWithRetry(() => this.provider.provider?.getBlockNumber(), 15 * 1000, 3)
9091
if (ccrOptions.toBlock + delayBlockFromLatest > latestblocknum) {
9192
// sleep until the toBlock
92-
await sleep((ccrOptions.toBlock + delayBlockFromLatest - latestblocknum) * blockInterval)
93+
// await sleep((ccrOptions.toBlock + delayBlockFromLatest - latestblocknum) * blockInterval)
9394
return false
9495
}
9596
return true
9697
}
9798

9899
const waitOrUpdateToBlock = toBlock
99-
? () => { ccrOptions.toBlock = Math.min(ccrOptions.toBlock, toBlock); return true }
100+
? () => {
101+
ccrOptions.toBlock = Math.min(ccrOptions.toBlock, toBlock)
102+
return true
103+
}
100104
: waitNextCrosscheck
101105

102106
const updateCCROptions = async (ccrOptions: CrossCheckRangeParam) => {
@@ -119,12 +123,15 @@ export class AutoCrossChecker extends BaseCrossChecker {
119123
// TODO: replace polling with schedule cron
120124
await polling(async () => {
121125
const wait = await waitOrUpdateToBlock()
122-
debug('polling interval: %d, wait: %s', pollingInterval, wait)
126+
debug('polling interval: %d, wait: %s, from block: %d, to block: %d', pollingInterval, wait, ccrOptions.fromBlock, ccrOptions.toBlock)
123127
if (wait) {
124128
await this.crossCheckRange(ccrOptions)
125129
// only update options after cc succ
126130
await updateCCROptions(ccrOptions)
127131
}
132+
else {
133+
debug('Because the latest block %d is too old, skip this cross check', latestblocknum)
134+
}
128135
return endingCondition()
129136
}, pollingInterval)
130137
}

0 commit comments

Comments
 (0)