Skip to content

perf: await the nonce manager nonce with the other parameters - #5528

Closed
jxom wants to merge 2 commits into
mainfrom
perf/nonce-manager-concurrency
Closed

jxom wants to merge 2 commits into
mainfrom
perf/nonce-manager-concurrency

Conversation

@jxom

@jxom jxom commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

With a nonceManager, prepareTransactionRequest awaited nonceManager.consume before it requested the block and fees, so the nonce cost a round trip of its own.

consume is still called at the same point, so concurrent prepares sharing a nonce manager receive nonces in call order. The returned promise is now awaited with the other parameters, the same way as the eth_getTransactionCount nonce since #5505.

Effect

Real in-process node:http JSON-RPC server adding 50 ms per request, real Viem clients over http(url), eth_fillTransaction unsupported.

Case main this PR
Local account + nonceManager, EIP-1559 network 153 ms, 4 requests, depth 3 102 ms, 4 requests, depth 2
3 concurrent prepares sharing a nonce manager nonces 7, 8, 9 nonces 7, 8, 9

Behavior

  • Nonce order: unchanged, because consume reserves the nonce synchronously when it is called, and it is called at the same point as before.
  • eth_fillTransaction and chain prepareTransactionRequest hooks: both read the nonce from the request, so the nonce is still awaited before them, and the decision to attempt eth_fillTransaction is the same as on main.
  • Fee hooks: chains with a function under fees still await the nonce before the fee requests.
  • Error precedence: a nonce manager error still takes precedence over errors from the other parameters. The promise has .catch(() => {}) attached when it is created.

Tests

  • Recording test (Anvil, real http transport): the block and priority fee requests are sent while the nonce manager's eth_getTransactionCount is pending, and 3 concurrent prepares then get 954, 955, 956 in call order.
  • eth_fillTransaction test: the fill request is sent only after the nonce manager's eth_getTransactionCount resolves. It fails when the early await is removed.
  • Error precedence: a createRpcProxy test delays a failing eth_getTransactionCount and fails the block right away; the nonce error is thrown.
  • createRecordingClient moved to the top of the test file so the eth_fillTransaction describe block can use it.
  • Only with eth_fillTransaction > args: maxFeePerGas (under default tip) fails, and it fails the same way on main with Anvil 1.9.0.

Independent of #5527 and #5529, which also touch prepareTransactionRequest. All three merge cleanly together.

With a nonceManager, prepareTransactionRequest awaited the consumed nonce
before requesting the block and fees. consume is still called at the same
point, so concurrent prepares keep receiving nonces in call order, but the
nonce is now awaited together with the other parameters. It is still
awaited first when eth_fillTransaction is attempted or the chain has a
prepareTransactionRequest hook, since both read it from the request, and a
nonce error still takes precedence over the errors of the other
parameters.

Co-authored-by: jxom <[email protected]>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e1b3-ba60-755b-bfd0-1a31a4a5bf1e
@vercel

vercel Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
viem Error Error Nov 16, 2026 1:14am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 11e1f87

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T01:12:36.434079Z 11e1f87 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7851af227b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +25 to +26
} from '../../utils/index.js'
import { jsonRpc } from '../../utils/nonceManager.js'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Import the nonce-manager APIs through package entrypoints

The newly added createNonceManager and jsonRpc imports use relative source paths even though both are publicly exported from viem/nonce. This violates AGENTS.md's requirement to “Import public APIs from package entrypoints in tests” and bypasses the public entrypoint in the new regression coverage. Import these APIs from viem/nonce; internal helpers can remain relative.

AGENTS.md reference: AGENTS.md:L188-L193

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 11e1f87: createNonceManager now comes from 'viem'. jsonRpc stays relative, because no entrypoint mapped in the test config exports it (viem/nonce has no path alias in test/tsconfig.json).


const request = await prepareTransactionRequest(recordingClient, args)

expect(request.nonce).toMatchInlineSnapshot(`953`)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Snapshot the complete prepared transaction

The new regression test snapshots only request.nonce, contrary to AGENTS.md's explicit requirement to snapshot whole objects rather than cherry-picking individual fields. The RPC-recording snapshot does not cover the returned transaction's other properties, so it cannot confirm that overlapping nonce consumption preserves the prepared result. Snapshot the complete request after omitting any nondeterministic properties, and apply the same approach to the new fill-transaction test.

AGENTS.md reference: AGENTS.md:L212-L215

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 11e1f87: both new tests now snapshot the whole prepared request, minus account.

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Published [email protected] under the pr-5528 dist-tag, which follows this pull request until it closes.

pnpm add viem@pr-5528
pnpm add [email protected]

jxom commented Nov 16, 2026

Copy link
Copy Markdown
Member Author

Bundled into #5543 as its own commit, so this PR is superseded. Closing in favour of the bundle.

@jxom jxom closed this Nov 16, 2026

This branch had an error being deployed

1 failed deployment
Preview — 11e1f875 Deployed Nov 16, 2026 by vercel[bot]
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