Skip to content

fix: guard consumed nonce 0 in nonceManager stale-source check - #5244

Merged
jxom merged 1 commit into
wevm:mainfrom
erkancamli:fix/nonce-manager-zero-nonce
Oct 22, 2026
Merged

jxom merged 1 commit into
wevm:mainfrom
erkancamli:fix/nonce-manager-zero-nonce

Conversation

@erkancamli

Copy link
Copy Markdown
Contributor

createNonceManager remembers the last nonce it handed out, so that a source which has not caught up yet (eth_getTransactionCount at pending before the previous transaction is broadcast or seen by the node) does not get the same nonce again:

const previousNonce = nonceMap.get(key) ?? 0
if (previousNonce > 0 && nonce <= previousNonce)
  return previousNonce + 1

A missing entry defaults to 0 and the guard is > 0, so a consumed nonce of 0 looks the same as "nothing consumed yet". For an account with no transactions, the next consume() against a source that still says 0 hands out 0 again. Every other account gets n + 1.

Repro:

const nonceManager = createNonceManager({ source: { get: () => 0, set() {} } })
await nonceManager.consume(args) // 0
await nonceManager.consume(args) // 0, expected 1

With get: () => 5 the same calls return 5, 6.

The existing prepareTransactionRequest "behavior: nonce manager" test shows the flow: prepare twice, then three in parallel, giving 953 to 957 for the fork account. The same steps against a local anvil for an account at nonce 0 give [0, 0, 0, 1, 2] on main and [0, 1, 2, 3, 4] with this change. With a transport whose pending count lags, three sequential sendTransaction calls from a fresh account broadcast nonces [0, 0, 0] (the same mock at count 5 gives [5, 6, 7]).

This PR compares against undefined instead of 0, which is the change v3 already has in NonceManager (87b581c).

Tests: src/utils/nonceManager.test.ts adds a stale source case that fails on main (expected +0 to be 1) and passes with this change. The rest of the file passes on both (run against a local anvil). A changeset is included.

@vercel

vercel Bot commented Oct 19, 2026

Copy link
Copy Markdown

@erkancamli is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Oct 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 58f26de

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/viem@5244

commit: 58f26de

@jxom
jxom merged commit 55ee45c into wevm:main Oct 22, 2026
22 of 35 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants