fix(electrum): error on batch response length mismatch - #2334
Open
Brijesh-Thakkar wants to merge 1 commit into
Open
Brijesh-Thakkar wants to merge 1 commit into
Brijesh-Thakkar wants to merge 1 commit into
Conversation
`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>
Brijesh-Thakkar
requested review from
evanlinjin and
oleonardolima
as code owners
September 30, 2026 19:23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
BdkElectrumClientpaired every batch request with its response viazipwithout checking that the response had one entry per request. A short response (possible with any non-stockElectrumApiimplementation) silently dropped trailing scripts, txids and anchors whilesyncstill returnedOk. A shortbatch_block_headerresponse leftheight_to_hashincomplete, soheight_to_hash[&h]panicked.This adds a private
check_batch_lenhelper and calls it after each of the five batch calls (batch_script_get_historyx3,batch_block_header,batch_transaction_get_merkle). It returnsError::Messageon 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
electrum_client::Clientcan't return a short batch, because itsbatch_callwaits for one response per request id. This only hardens customElectrumApiimplementations (custom transports, proxies, mocks).crates/electrum/tests/test_short_batch.rs. They use a mockElectrumApithat returns one entry too few from each of the three batch methods, and need neither bitcoind nor electrs. Without the fix, one test panics atheight_to_hashand two returnOk. With it, all pass.serde_jsonis added as a dev-dependency ofbdk_electrumonly for the mock's trait signatures.Changelog notice
Fixed:
bdk_electrumnow 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: