Skip to content

perf: read bridge, fee, and token data concurrently in zksync deposit - #5531

Closed
jxom wants to merge 2 commits into
mainfrom
perf/zksync-deposit-concurrency
Closed

jxom wants to merge 2 commits into
mainfrom
perf/zksync-deposit-concurrency

Conversation

@jxom

@jxom jxom commented Nov 16, 2026

Copy link
Copy Markdown
Member

The ZKsync deposit Action read the bridge addresses, the bridgehub, the base token, the fees, the token metadata and the asset id one after another, and requested zks_getBridgeContracts twice. It took 12 round trips before it could build the transaction.

  • deposit starts the bridge addresses, the bridgehub and base token, and the fee estimate together.
  • getBridgeAddresses reads a missing L1_NULLIFIER and nativeTokenVault together.
  • getL1DepositTx starts the second bridge calldata (assetId) before it awaits the fees.
  • encodeDefaultBridgeData reads name, symbol and decimals together, and the custom bridge path reads l2BridgeAddress alongside them.
  • The default bridge L2 gas limit reuses the bridge addresses instead of requesting zks_getBridgeContracts again.

Effect

Real in-process node:http JSON-RPC server adding 50 ms per request, canned responses, real Viem clients over http(url). ERC-20 deposit to an ETH-based chain with a local account, end to end.

main this PR
Reads before the transaction is built 612 ms, depth 12 256 ms, depth 5
Whole deposit 820 ms, 16 requests, depth 16 464 ms, 15 requests, depth 9
Whole deposit, node without l1Nullifier/l1NativeTokenVault 924 ms, 18 requests, depth 18 468 ms, 17 requests, depth 9

The signed transaction is byte-for-byte the same as on main.

Behavior

  • Error precedence: every early-started read has .catch(() => {}) attached when it is created and is awaited in its original order, so a read whose error used to surface first still does. For example, BaseFeeHigherThanValueError is still thrown before an assetId error.
  • Wasted reads on failure: when an earlier read fails, the reads started alongside it have already been sent.
  • Approvals, estimateGas and sendTransaction are unchanged.

Tests

  • New test: a createRpcProxy server answers every request after 20 ms and records each request's depth. It snapshots the depths and the signed transaction. On main the transaction snapshot matches and the depth snapshot fails.
  • Error precedence: a delayed failing zks_getBridgeContracts still wins over failing zks_getBridgehubContract and eth_getBlockByNumber.
  • deposit.test.ts passes.

deposit read the bridge addresses, the bridgehub, the base token, the
fees, the token metadata and the asset id one after another, and
requested zks_getBridgeContracts twice. The independent reads now start
together and are awaited in their original order, so errors surface in
the same order, and the bridge addresses are reused for the L2 gas limit
estimate.

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: 5357729

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:51.676460Z 5357729 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: 77be8ce1a2

ℹ️ 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 +195 to +199
if (fn)
return {
result: encodeFunctionResult({
abi: [fn],
functionName: fn.name,

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 Replace canned RPC replies with real chain behavior

The new concurrency test intercepts every RPC request and supplies canned contract results, gas estimates, and a transaction hash without executing the transaction. Although the HTTP server is real, these responses are stubs, which the root AGENTS.md explicitly prohibits. Forward requests to the configured test chain, adding delays where needed, and verify the resulting deposit behavior rather than substituting all RPC results.

AGENTS.md reference: AGENTS.md:L155-L158

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.

Keeping this. The suite has no ZKsync L1 bridge to run against: anvilZksync has no fork, and the existing deposit tests replace client.request outright. createRpcProxy is a real HTTP server driven by a real Viem client, the same pattern the repo already uses for RPC failure cases. To show behavior is unchanged, the test snapshots the signed transaction, which is byte-for-byte the same as on main.

"transaction": "0x02f902b501078405f5e100845f5e10008303a98094303a465b659cbb0ab36ee643ea362c509eeb521384075bcd15b9024424fd57fb0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000014400000000000000000000000000000000000000000000000000000000075bcd150000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007a12000000000000000000000000000000000000000000000000000000000000003200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d7f9f54194c633f36ccd5f3da84ad4a1c38cb2cb0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000c101abababababababababababababababababababababababababababababababab0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000f4240000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb4800000000000000000000000000000000000000000000000000000000000000c080a0d8cf95db4d162958b68f8ff0e6835f38d8673ec33e062cc1e934f6f8d8a6b5d6a013374e2232332f2ead963be2f95f02d179118328e4bf277683ae5677144d5ef5",
}
`)
expect(rounds.map((names) => names.sort())).toMatchInlineSnapshot(`

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 Assert concurrency behavior without snapshotting call tracking

The rounds snapshot asserts the exact sequence and grouping of internal RPC calls, so changes such as batching or using another equivalent fee-estimation path can fail this test even when concurrency and the resulting transaction remain correct. The root AGENTS.md explicitly requires testing observable behavior instead of call-tracking. Keep the transaction assertion and replace the method-round snapshot with a behavioral concurrency check that does not prescribe the internal call sequence.

AGENTS.md reference: AGENTS.md:L204-L208

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.

Keeping this. Recording which requests overlap through a real http transport is how the concurrency rule for these changes is enforced (same approach as the landed #5505 tests). The result alone can't tell a concurrent implementation from a sequential one, since both return the same value. The snapshot records rounds of requests, not hook invocations, so it fails exactly when a waterfall comes back.

Comment thread src/zksync/actions/deposit.test.ts Outdated
http,
parseAbi,
toFunctionSelector,
} from '../../index.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 new public test APIs through package aliases

The newly added public API imports, including createClient, http, and encodeFunctionResult, use the relative source entrypoint instead of the required viem alias. The root AGENTS.md explicitly requires package-entrypoint aliases in public-surface tests; change this import to 'viem' and import the newly added erc20Abi through that entrypoint as well. The exception for relative chain definitions does not cover these APIs.

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 5357729: the new public imports, including erc20Abi and parseAbi, now come from 'viem'.

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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

pnpm add viem@pr-5531
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 — 53577293 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