Skip to content

Commit c122473

Browse files
authored
fix: check oldest state for onChain nonce equality (#689)
1 parent 99cbd84 commit c122473

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

src/worker/tasks/nonceHealthCheckWorker.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,6 @@ interface NonceState {
2727
onchainNonce: number;
2828
largestSentNonce: number;
2929
}
30-
31-
interface WalletHealth {
32-
walletAddress: Address;
33-
chainId: number;
34-
isStuck: boolean;
35-
onchainNonce: number;
36-
largestSentNonce: number;
37-
}
38-
3930
// Initialize the worker
4031
export const initNonceHealthCheckWorker = () => {
4132
NonceHealthCheckQueue.q.add("cron", "", {
@@ -51,7 +42,7 @@ export const initNonceHealthCheckWorker = () => {
5142
};
5243

5344
// 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>) => {
5546
const allWallets = await getUsedBackendWallets();
5647

5748
for (let i = 0; i < allWallets.length; i += BATCH_SIZE) {
@@ -91,16 +82,19 @@ async function isQueueStuck(
9182
// ensure we have enough data to check
9283
if (historicalStates.length < CHECK_PERIODS) return false;
9384

85+
const oldestOnchainNonce = historicalStates.at(-1)?.onchainNonce;
86+
9487
// if for every period, the onchain nonce has not changed, and the internal nonce has strictly increased
9588
// then the queue is stuck
9689
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;
9892

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;
10498
});
10599

106100
return isStuckForAllPeriods;
@@ -126,7 +120,9 @@ function nonceHistoryKey(walletAddress: Address, chainId: number) {
126120
return `nonce-history:${chainId}:${getAddress(walletAddress)}`;
127121
}
128122

129-
// Get historical nonce states
123+
/**
124+
* Get historical nonce states, ordered from newest to oldest
125+
*/
130126
async function getHistoricalNonceStates(
131127
walletAddress: Address,
132128
chainId: number,

0 commit comments

Comments
 (0)