Skip to content

fix(electrum): error on batch response length mismatch - #2334

Open
Brijesh-Thakkar wants to merge 1 commit into
bitcoindevkit:masterfrom
Brijesh-Thakkar:fix/electrum-batch-length-check
Open

Brijesh-Thakkar wants to merge 1 commit into
bitcoindevkit:masterfrom
Brijesh-Thakkar:fix/electrum-batch-length-check

Conversation

@Brijesh-Thakkar

Copy link
Copy Markdown

Description

BdkElectrumClient paired every batch request with its response via zip without checking that the response had one entry per request. A short response (possible with any non-stock ElectrumApi implementation) silently dropped trailing scripts, txids and anchors while sync still returned Ok. A short batch_block_header response left height_to_hash incomplete, so height_to_hash[&h] panicked.

This adds a private check_batch_len helper and calls it after each of the five batch calls (batch_script_get_history x3, batch_block_header, batch_transaction_get_merkle). It returns Error::Message on any length mismatch, in line with the other server-misbehaviour checks in this crate. There are no public API changes.

Fixes #2305

Notes to the reviewers

  • The stock electrum_client::Client can't return a short batch, because its batch_call waits for one response per request id. This only hardens custom ElectrumApi implementations (custom transports, proxies, mocks).
  • Regression tests are in crates/electrum/tests/test_short_batch.rs. They use a mock ElectrumApi that returns one entry too few from each of the three batch methods, and need neither bitcoind nor electrs. Without the fix, one test panics at height_to_hash and two return Ok. With it, all pass.
  • serde_json is added as a dev-dependency of bdk_electrum only for the mock's trait signatures.
  • This issue was found by AI, and I used Claude Code to assist with this PR. I reviewed and tested the changes myself.

Changelog notice

Fixed: bdk_electrum now returns an error instead of silently truncating results or panicking when an Electrum batch response length doesn't match the request.

Checklists

All Submissions:

Bugfixes:

  • This pull request breaks the existing API
  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

`BdkElectrumClient` paired every batch request with its response via
`zip` without checking that the response had one entry per request. A
short response (possible with any non-stock `ElectrumApi` implementation)
silently dropped trailing scripts, txids and anchors while `sync` still
returned `Ok`, and a short `batch_block_header` response left
`height_to_hash` incomplete so `height_to_hash[&h]` panicked.

Add `check_batch_len` and call it after each of the five batch calls
(`batch_script_get_history` x3, `batch_block_header`,
`batch_transaction_get_merkle`), returning `Error::Message` on any
length mismatch, in line with the other server-misbehaviour checks in
this crate.

Add regression tests using a mock `ElectrumApi` that returns one entry
too few from each of the three batch methods. They need neither bitcoind
nor electrs. This adds `serde_json` as a dev-dependency of `bdk_electrum`
for the mock's trait signatures.

Fixes bitcoindevkit#2305

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings September 30, 2026 19:23

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

Electrum batch responses are paired with zip without length checks

2 participants