@@ -27,15 +27,6 @@ interface NonceState {
27
27
onchainNonce : number ;
28
28
largestSentNonce : number ;
29
29
}
30
-
31
- interface WalletHealth {
32
- walletAddress : Address ;
33
- chainId : number ;
34
- isStuck : boolean ;
35
- onchainNonce : number ;
36
- largestSentNonce : number ;
37
- }
38
-
39
30
// Initialize the worker
40
31
export const initNonceHealthCheckWorker = ( ) => {
41
32
NonceHealthCheckQueue . q . add ( "cron" , "" , {
@@ -51,7 +42,7 @@ export const initNonceHealthCheckWorker = () => {
51
42
} ;
52
43
53
44
// Main handler function
54
- const handler : Processor < any , void , string > = async ( job : Job < string > ) => {
45
+ const handler : Processor < null , void , string > = async ( _job : Job < null > ) => {
55
46
const allWallets = await getUsedBackendWallets ( ) ;
56
47
57
48
for ( let i = 0 ; i < allWallets . length ; i += BATCH_SIZE ) {
@@ -91,16 +82,19 @@ async function isQueueStuck(
91
82
// ensure we have enough data to check
92
83
if ( historicalStates . length < CHECK_PERIODS ) return false ;
93
84
85
+ const oldestOnchainNonce = historicalStates . at ( - 1 ) ?. onchainNonce ;
86
+
94
87
// if for every period, the onchain nonce has not changed, and the internal nonce has strictly increased
95
88
// then the queue is stuck
96
89
const isStuckForAllPeriods = historicalStates . every ( ( state , index ) => {
97
- if ( index === historicalStates . length - 1 ) return true ; // Last (oldest) state
90
+ // check if the onchain nonce has changed, if yes, fail the check early
91
+ if ( state . onchainNonce !== oldestOnchainNonce ) return false ;
98
92
99
- const prevState = historicalStates [ index + 1 ] ;
100
- return (
101
- state . onchainNonce === prevState . onchainNonce &&
102
- state . largestSentNonce > prevState . largestSentNonce
103
- ) ;
93
+ // if the current state is the oldest state, we don't need to check if engine nonce has increased
94
+ if ( index === historicalStates . length - 1 ) return true ;
95
+
96
+ const previousState = historicalStates [ index + 1 ] ;
97
+ return state . largestSentNonce > previousState . largestSentNonce ;
104
98
} ) ;
105
99
106
100
return isStuckForAllPeriods ;
@@ -126,7 +120,9 @@ function nonceHistoryKey(walletAddress: Address, chainId: number) {
126
120
return `nonce-history:${ chainId } :${ getAddress ( walletAddress ) } ` ;
127
121
}
128
122
129
- // Get historical nonce states
123
+ /**
124
+ * Get historical nonce states, ordered from newest to oldest
125
+ */
130
126
async function getHistoricalNonceStates (
131
127
walletAddress : Address ,
132
128
chainId : number ,
0 commit comments