1
1
import type { ethers } from 'ethers'
2
- import { polling , sleep , timeoutWithRetry } from '@ora-io/utils'
2
+ import { polling , timeoutWithRetry } from '@ora-io/utils'
3
3
import { ETH_BLOCK_INTERVAL } from '../../constants'
4
4
import type { Providers } from '../../types/w3'
5
5
import { debug } from '../../debug'
@@ -53,20 +53,21 @@ export class AutoCrossChecker extends BaseCrossChecker {
53
53
* @param options
54
54
*/
55
55
async start ( options : AutoCrossCheckParam ) {
56
+ debug ( 'auto crosscheck start with options: %O' , options )
56
57
this . validate ( options )
57
58
58
59
this . cache = new CrossCheckerCacheManager ( options ?. store , { keyPrefix : options ?. storeKeyPrefix , ttl : options ?. storeTtl } )
59
60
60
- const latestblocknum = await timeoutWithRetry ( ( ) => this . provider . provider ?. getBlockNumber ( ) , 15 * 1000 , 3 )
61
+ let latestblocknum = await timeoutWithRetry ( ( ) => this . provider . provider ?. getBlockNumber ( ) , 15 * 1000 , 3 )
61
62
62
63
// 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 )
64
65
65
66
const {
66
67
fromBlock = defaultInitCheckpoint ,
67
68
batchBlocksCount = 10 ,
68
69
pollingInterval = 3000 ,
69
- blockInterval = ETH_BLOCK_INTERVAL ,
70
+ // blockInterval = ETH_BLOCK_INTERVAL,
70
71
delayBlockFromLatest = 1 ,
71
72
toBlock,
72
73
} = options
@@ -86,17 +87,20 @@ export class AutoCrossChecker extends BaseCrossChecker {
86
87
}
87
88
88
89
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 )
90
91
if ( ccrOptions . toBlock + delayBlockFromLatest > latestblocknum ) {
91
92
// sleep until the toBlock
92
- await sleep ( ( ccrOptions . toBlock + delayBlockFromLatest - latestblocknum ) * blockInterval )
93
+ // await sleep((ccrOptions.toBlock + delayBlockFromLatest - latestblocknum) * blockInterval)
93
94
return false
94
95
}
95
96
return true
96
97
}
97
98
98
99
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
+ }
100
104
: waitNextCrosscheck
101
105
102
106
const updateCCROptions = async ( ccrOptions : CrossCheckRangeParam ) => {
@@ -119,12 +123,15 @@ export class AutoCrossChecker extends BaseCrossChecker {
119
123
// TODO: replace polling with schedule cron
120
124
await polling ( async ( ) => {
121
125
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 )
123
127
if ( wait ) {
124
128
await this . crossCheckRange ( ccrOptions )
125
129
// only update options after cc succ
126
130
await updateCCROptions ( ccrOptions )
127
131
}
132
+ else {
133
+ debug ( 'Because the latest block %d is too old, skip this cross check' , latestblocknum )
134
+ }
128
135
return endingCondition ( )
129
136
} , pollingInterval )
130
137
}
0 commit comments