Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NonceManager): off-by-one error resyncing onchain nonce #27

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ethers-middleware/src/nonce_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ where
tracing::debug!(?nonce, "Sent transaction");
let tx_count_for_resync = self.get_tx_count_for_resync();
if new_txs_since_resync >= tx_count_for_resync {
let onchain_nonce = self.get_transaction_count(self.address, block).await?;
// subtract 1 because the nonce returned by `get_transaction_count`
// is the next one to be used, whereas we want the last one that landed
let onchain_nonce = self
.get_transaction_count(self.address, block)
.await?
.saturating_sub(1.into());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's an edge case here if it's the first ever tx, where get_transaction_count returns 0. Think it's a concern or that it's fine for now?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this branch would only be hit after tx_count_for_resync txs which is >0 so we're good, but it would be a concern if tx_count_for_resync was 0

Copy link
Author

@daniel-savu daniel-savu Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually if none of the tx_count_for_resync land this would be a problem, but I think it's very unlikely we run into that
even if we store 0, because of next() we end up using 1 so we're good even if all initial tx_count_for_resync txs get stuck

self.nonce.store(onchain_nonce.as_u64(), Ordering::SeqCst);
self.txs_since_resync.store(0, Ordering::SeqCst);
tracing::debug!(?nonce, "Resynced internal nonce with onchain nonce");
Expand Down
Loading