Skip to content

fix(batch): return the node error when a whole batch is rejected - #1769

Open
wabicai wants to merge 1 commit into
starknet-io:developfrom
wabicai:fix/batch-level-error-reply
Open

wabicai wants to merge 1 commit into
starknet-io:developfrom
wabicai:fix/batch-level-error-reply

Conversation

@wabicai

@wabicai wabicai commented Nov 27, 2026

Copy link
Copy Markdown

Motivation and Resolution

When batching is enabled (new RpcProvider({ batch: 0 })), a node that rejects the batch as a whole answers with a single JSON-RPC response object instead of an array, as JSON-RPC 2.0 §6 requires (and as public RPC providers do for batch-size or rate limits), e.g.:

{ "jsonrpc": "2.0", "id": null, "error": { "code": -32005, "message": "Rate limit exceeded" } }

BatchClient.fetch() called results.find(...) on that object, so every request in the batch failed with Error: results.find is not a function, and the node's actual error was lost.

Also, delete this.batchPromises[requestId] only ran when the batch succeeded, so every request of a failed batch (network error, transport rejection) left an entry in batchPromises for the lifetime of the provider.

Changes in src/utils/batch/index.ts:

  • the batch promise is awaited in a try/finally, so its entry is always deleted;
  • a non-array reply carrying an error is handed to every caller of the batch, so fetchEndpoint raises the usual RpcError with the node's message; any other non-array reply raises an explicit error instead of a TypeError.

Successful batches behave exactly as before.

Usage related changes

  • With batch enabled, a batch rejected by the node now rejects with an RpcError carrying the node's code and message (e.g. -32005: Rate limit exceeded) instead of results.find is not a function.

Development related changes

  • New __tests__/utils/batchClient.test.ts, which needs no node (mocked transport) and so runs in npm run test:unit: a batch-level error reaches every caller, the channel turns it into an RpcError, and a failed batch leaves no entry in batchPromises. The three tests fail on develop and pass with this change.
npm run test:unit -- __tests__/utils/batchClient.test.ts   # develop: 3 failed / this branch: 3 passed
npm run test:unit                                          # Test Suites: 99 passed, Tests: 2438 passed
npm run ts:check                                           # no errors
npx eslint / prettier --check on the changed files         # clean

I could not run the devnet-backed integration suite (npm test) locally; the change does not touch the success path that __tests__/utils/batch.test.ts covers.

Checklist:

  • Performed a self-review of the code
  • Rebased to the last commit of the target branch (or merged it into my branch)
  • Linked the issues which this PR resolves (no existing issue)
  • Documented the changes in code (API docs will be generated automatically)
  • Updated the tests
  • All tests are passing (unit suite passing; devnet integration suite not run locally)

This PR was prepared with the help of an AI assistant (Claude) and reviewed for correctness.

🤖 Generated with Claude Code

A node answers a batch it rejects as a whole (size or rate limit) with a
single JSON-RPC error object, as JSON-RPC 2.0 requires. BatchClient called
.find() on it and every caller got 'results.find is not a function'
instead of the node's error. The batch promises of a failed batch were
also never deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@PhilippeR26 PhilippeR26 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for this fix: the bug is real, and the change is small and clear.

One request about the tests. We keep one test file per module, named after it: the tests of src/utils/batch/ live in __tests__/utils/batch.test.ts. That file already contains a test with a mocked transport (sends the batch through an injected transport instead of baseFetch). Could you move your three tests into batch.test.ts, in their own describe block, and delete batchClient.test.ts? They will then run with the devnet suite, like the existing mocked test.

There is also a small change to request on a code comment: see the inline comment.

Comment thread src/utils/batch/index.ts
Comment on lines +141 to +142
// JSON-RPC 2.0: a batch rejected as a whole (size limit, rate limit...) gets a single
// response object. Every caller of that batch gets its error.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The JSON-RPC 2.0 spec does not say this. Section 6 (Batch) asks for a single response object only when the batch itself is invalid:

If the batch rpc call itself fails to be recognized as an valid JSON or as an Array with at least one value, the response from the Server MUST be a single Response object.

BatchClient never sends such a batch. A size or rate limit is a choice of the node, not a rule of the spec. Could you reword the comment, for example:

Suggested change
// JSON-RPC 2.0: a batch rejected as a whole (size limit, rate limit...) gets a single
// response object. Every caller of that batch gets its error.
// A node can reject the whole batch, for example when it is too large.
// It then answers with a single error object instead of an array.
// Every caller of the batch gets this error.

@PhilippeR26

Copy link
Copy Markdown
Collaborator

Unrelated to this PR, but it may interest the OneKey team. We had a look at how the OneKey app uses starknet.js (app-monorepo at ec7a680). We think the flat client in ClientStarknet.ts, written to avoid RpcProvider, costs you more than it saves. Here are the facts.

1. No memory or size gain

ClientStarknet.ts L79-L82 says that RpcProvider would pull the account/contract/abi stack into the bg bundle. But starknet.js is already loaded with your Starknet vault:

  • The vault is loaded on demand (factory.ts L229). It imports KeyringHardware (Vault.ts L110), which imports ec from starknet (KeyringHardware.ts L1).

  • On iOS and Android, your Metro config resolves starknet to dist/index.js (metro.config.js L228-L234 and L278-L286). This is one CommonJS file. When the vault loads, Metro runs the whole file, so RpcProvider is in memory anyway.

  • Your extension is built with rspack, which can drop unused ESM code. We measured starknet 10.8.0 with esbuild (ESM, minified):

    Imported from starknet 10.8.0 Minified Gzip
    The names your Starknet code imports today 216 KB 65 KB
    The same names + RpcProvider 233 KB 70 KB
    ClientStarknet.ts alone, for comparison 5.9 KB 2.1 KB
  • Your vendored privacy SDK imports and uses RpcProvider itself (L6, L108), so it is in your build anyway.

2. Following the changes of Starknet

The RPC spec keeps changing: starknet.js 10.8.0 has channels for 0.9 and 0.10.2 to 0.10.4, and 11.x adds 0.10.5. RpcProvider.create() reads the spec version of the node and picks the matching channel. ClientStarknet.ts does part of this work by hand: a spec version probe with a cache and a backoff, and a pending / pre_confirmed fallback (L142-L185). Each new revision is work you must redo alone.

Fair point: starknet 10.8.0 has no channel for spec 0.8, and RpcProvider.create() throws on a 0.8 node. If supporting user nodes still on 0.8 is the reason for this fallback, please tell us.

3. Reliability

Our CI runs the test suite on devnet for every PR, and on Pathfinder and Juno (spec 0.9 and 0.10) before each release to main. Fixes, like the one in this PR, reach every RpcProvider user.

A hand-written client must find its own edge cases. One example, from reading the code (we did not run it):

  • isRejectedPreConfirmedError takes any -32602 (invalid params) error as a rejected pre_confirmed tag (L218-L229), and the call is then retried with pending.
  • If this retry fails for another reason, a timeout for example, pending is cached and never reset (L251-L256). But 0.9+ nodes reject pending (your comment at L148-L149).
  • When the retry fails, the error thrown is the one of the retry, not the original error (L256).

If RpcProvider lacks something you need, feel free to tell us.

This branch has not been deployed

No deployments
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