Skip to content

Commit 9bee088

Browse files
committed
chore: clean up nonce resync worker
1 parent ba59e74 commit 9bee088

File tree

1 file changed

+29
-57
lines changed

1 file changed

+29
-57
lines changed

src/worker/tasks/nonceResyncWorker.ts

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "../../db/wallets/walletNonce";
99
import { getConfig } from "../../utils/cache/getConfig";
1010
import { getChain } from "../../utils/chain";
11+
import { prettifyError } from "../../utils/error";
1112
import { logger } from "../../utils/logger";
1213
import { redis } from "../../utils/redis/redis";
1314
import { thirdwebClient } from "../../utils/sdk";
@@ -43,75 +44,46 @@ const handler: Processor<any, void, string> = async (job: Job<string>) => {
4344
job.log(`Found ${sentNoncesKeys.length} nonce-sent* keys`);
4445

4546
for (const sentNonceKey of sentNoncesKeys) {
46-
const { chainId, walletAddress } = splitSentNoncesKey(sentNonceKey);
47+
try {
48+
const { chainId, walletAddress } = splitSentNoncesKey(sentNonceKey);
4749

48-
const rpcRequest = getRpcClient({
49-
client: thirdwebClient,
50-
chain: await getChain(chainId),
51-
});
52-
53-
const [transactionCount, lastUsedNonceDb] = await Promise.all([
54-
eth_getTransactionCount(rpcRequest, {
55-
address: walletAddress,
56-
blockTag: "latest",
57-
}),
58-
inspectNonce(chainId, walletAddress),
59-
]);
50+
const rpcRequest = getRpcClient({
51+
client: thirdwebClient,
52+
chain: await getChain(chainId),
53+
});
54+
const lastUsedNonceOnchain =
55+
(await eth_getTransactionCount(rpcRequest, {
56+
address: walletAddress,
57+
blockTag: "latest",
58+
})) - 1;
59+
const lastUsedNonceDb = await inspectNonce(chainId, walletAddress);
6060

61-
if (Number.isNaN(transactionCount)) {
6261
job.log(
63-
`Received invalid onchain transaction count for ${walletAddress}: ${transactionCount}`,
62+
`wallet=${chainId}:${walletAddress} lastUsedNonceOnchain=${lastUsedNonceOnchain} lastUsedNonceDb=${lastUsedNonceDb}`,
6463
);
65-
logger({
66-
level: "error",
67-
message: `[nonceResyncWorker] Received invalid onchain transaction count for ${walletAddress}: ${transactionCount}`,
68-
service: "worker",
69-
});
70-
continue;
71-
}
72-
73-
const lastUsedNonceOnchain = transactionCount - 1;
74-
75-
job.log(
76-
`${walletAddress} last used onchain nonce: ${lastUsedNonceOnchain} and last used db nonce: ${lastUsedNonceDb}`,
77-
);
78-
logger({
79-
level: "debug",
80-
message: `[nonceResyncWorker] last used onchain nonce: ${transactionCount} and last used db nonce: ${lastUsedNonceDb}`,
81-
service: "worker",
82-
});
83-
84-
// If the last used nonce onchain is the same as or ahead of the last used nonce in the db,
85-
// There is no need to resync the nonce.
86-
if (lastUsedNonceOnchain >= lastUsedNonceDb) {
87-
job.log(`No need to resync nonce for ${walletAddress}`);
8864
logger({
8965
level: "debug",
90-
message: `[nonceResyncWorker] No need to resync nonce for ${walletAddress}`,
66+
message: `[nonceResyncWorker] wallet=${chainId}:${walletAddress} lastUsedNonceOnchain=${lastUsedNonceOnchain} lastUsedNonceDb=${lastUsedNonceDb}`,
9167
service: "worker",
9268
});
93-
continue;
94-
}
9569

96-
// for each nonce between last used db nonce and last used onchain nonce
97-
// check if nonce exists in nonce-sent set
98-
// if it does not exist, recycle it
99-
for (
100-
let _nonce = lastUsedNonceOnchain + 1;
101-
_nonce < lastUsedNonceDb;
102-
_nonce++
103-
) {
104-
const exists = await isSentNonce(chainId, walletAddress, _nonce);
70+
// Recycle all nonces between (onchain nonce, db nonce] if they aren't in-flight ("sent nonce").
71+
for (
72+
let nonce = lastUsedNonceOnchain + 1;
73+
nonce <= lastUsedNonceDb;
74+
nonce++
75+
) {
76+
const exists = await isSentNonce(chainId, walletAddress, nonce);
77+
if (!exists) {
78+
await recycleNonce(chainId, walletAddress, nonce);
79+
}
80+
}
81+
} catch (error) {
10582
logger({
106-
level: "debug",
107-
message: `[nonceResyncWorker] nonce ${_nonce} exists in nonce-sent set: ${exists}`,
83+
level: "error",
84+
message: `[nonceResyncWorker] ${prettifyError(error)}`,
10885
service: "worker",
10986
});
110-
111-
// If nonce does not exist in nonce-sent set, recycle it
112-
if (!exists) {
113-
await recycleNonce(chainId, walletAddress, _nonce);
114-
}
11587
}
11688
}
11789
};

0 commit comments

Comments
 (0)