Skip to content

fix(esplora): stop full_scan at stop_gap with parallel requests - #2472

Open
debojyoti10CC wants to merge 3 commits into
bitcoindevkit:masterfrom
debojyoti10CC:fix/esplora-full-scan-stop-gap
Open

debojyoti10CC wants to merge 3 commits into
bitcoindevkit:masterfrom
debojyoti10CC:fix/esplora-full-scan-stop-gap

Conversation

@debojyoti10CC

Copy link
Copy Markdown

Description

With parallel_requests > 1, the Esplora full_scan (blocking and async) only checked the stop gap after processing a whole batch of script results. If the gap was reached partway through a batch, a later script with a transaction reset the unused counter and its transactions went into the update, so the result depended on parallel_requests.

The fix factors the gap bookkeeping into a small helper, track_spk_activity, in crates/esplora/src/lib.rs. Both loops now call it per result and stop processing the batch as soon as stop_gap consecutive unused scripts have been seen. With parallel_requests = 1 the behaviour is unchanged.

Notes to the reviewers

I added unit tests for the helper (same-batch active script past the gap is ignored, revealed indices don't count as unused, activity within the gap resets the counter). They don't need electrsd. I did not add an electrsd integration test.

I could not build or run anything locally: Windows Application Control blocked the cargo build scripts here, so the tests, cargo fmt check and clippy have not been run. Please rely on CI for those. The commit is also unsigned for now, since I haven't set up GPG signing.

Used Claude Code to help write the fix and tests; I reviewed and tested the changes.

Changelog notice

Fixed: Esplora full_scan no longer processes scripts beyond stop_gap when parallel_requests > 1.

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

Fixes #2471

@github-project-automation github-project-automation Bot moved this to Triage in BDK Chain Nov 17, 2026
@debojyoti10CC
debojyoti10CC marked this pull request as ready for review November 17, 2026 14:15
@debojyoti10CC
debojyoti10CC requested a review from a team as a code owner November 17, 2026 14:15
Check the stop gap per script result instead of once per batch, so
results past the gap in the same batch are not processed and the
update no longer depends on parallel_requests. Gap bookkeeping moves
into a small helper with unit tests.

Fixes bitcoindevkit#2471
@debojyoti10CC
debojyoti10CC force-pushed the fix/esplora-full-scan-stop-gap branch from 7142fd9 to b7a3a56 Compare November 18, 2026 00:14
@debojyoti10CC

Copy link
Copy Markdown
Author

Bumping this in case it got buried. It is still ready for review, and I am happy to make any changes you would like. Thanks!

@Jean-Regis-M Jean-Regis-M 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.

Concept ACK. This makes Esplora behave like Electrum: populate_with_spks in bdk_electrum_client.rs already returns as soon as unused_spk_count >= stop_gap, partway through a batch, so with this change both backends stop at the same script no matter how the requests are batched.

Since you mentioned you couldn't build locally, I tried it on Linux (rustc 1.100). It currently doesn't compile once the blocking or async feature is on:

$ cargo test -p bdk_esplora --lib --all-features
error[E0425]: cannot find function `track_spk_activity` in this scope
error[E0425]: cannot find function `track_spk_activity` in this scope
error: could not compile `bdk_esplora` (lib test) due to 2 previous errors

blocking_ext.rs and async_ext.rs only import use crate::{insert_anchor_or_seen_at_from_status, insert_prevouts};, so the new helper also needs adding there:

use crate::{insert_anchor_or_seen_at_from_status, insert_prevouts, track_spk_activity};

cargo clippy -p bdk_esplora --no-default-features passes only because both modules are compiled out, which also explains why the #[allow(dead_code)] hid it. cargo fmt --all -- --check passes.

Two more things, not blocking:

  • same_result_regardless_of_batching runs the helper on one sequential list, so it doesn't exercise batching. An electrsd test would cover the real path: same setup as test_update_tx_graph_stop_gap (tx on index 3), with full_scan(request, 3, p) asserting no txs and no last_active_indices for p in [1, 2, 5]. On master, p = 2 and p = 5 should find the tx; with this PR none should.
  • After the break, blocking and async now handle errors differently. In blocking_ext.rs, the remaining JoinHandles are dropped unjoined, so an error from a request past the gap is ignored. In async_ext.rs, try_collect has already awaited every request in the batch, so the same error still fails the whole scan. Probably fine either way, but it may be worth a sentence in the commit message, or joining the leftover handles so both behave the same.

@Jean-Regis-M

Copy link
Copy Markdown

Follow-up with an end-to-end check against electrsd (bitcoind + electrs), using the setup I suggested above: tx on index 3, full_scan(request, stop_gap = 3, p).

master (c5e87cc):

parallel_requests=1: txs=0 last_active={}
parallel_requests=2: txs=1 last_active={0: 3}   <- tx past the gap is returned

This PR, with the missing track_spk_activity import added:

parallel_requests=1: txs=0 last_active={}
parallel_requests=2: txs=0 last_active={}
parallel_requests=5: txs=0 last_active={}

So the bug is real and the fix works once it compiles. With the import added, cargo test -p bdk_esplora --lib --features blocking,async passes (9/9, including the four new gap_tests).

cargo clippy -p bdk_esplora --features blocking,async --all-targets -- -D warnings also flags one thing from this PR:

error: items after a test module

mod gap_tests sits in the middle of lib.rs, above insert_anchor_or_seen_at_from_status / insert_prevouts, so clippy::items_after_test_module fires. Moving the test module to the end of the file fixes it. (Clippy 1.100 also reports redundant reference in format! in the existing tests/*.rs. That's unrelated to this PR.)

Feel free to turn the electrsd check into a test in crates/esplora/tests/blocking_ext.rs (and async).

The helper was not in scope in blocking_ext.rs or async_ext.rs, so
`--features blocking,async` failed to compile. Import it in both
modules, drop the dead_code allow that hid the error, and move the
gap_tests module to the end of lib.rs to satisfy
clippy::items_after_test_module.

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

Copy link
Copy Markdown
Author

Thanks for the detailed review and for running the electrsd check, @Jean-Regis-M. That was the bug I needed to confirm.nnPushed 63766c9:n- blocking_ext.rs and async_ext.rs now import track_spk_activity, so --features blocking,async should compile.n- Removed the dead_code allow on the helper, since it is used now.n- Moved mod gap_tests to the end of lib.rs for items_after_test_module.nnI could not run cargo test or clippy on my machine because the build scripts are blocked there, so I am relying on CI for the compile and test check. cargo fmt --check passes.nnNot in this commit: the electrsd end-to-end test and the note on blocking vs async error handling after the break. I will look at both next.

@Jean-Regis-M Jean-Regis-M 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.

Since you couldn't build locally, I ran 63766c9 on Linux (WSL, Rust 1.100):

  • cargo test -p bdk_esplora --all-features: lib 9/9 (incl. the 4 gap_tests), blocking_ext 7/7, async_ext 7/7, doc 1/1. The electrsd tests (test_stop_gap_past_last_revealed, test_update_tx_graph_stop_gap and their async versions) pass, so --features blocking,async now compiles and the import fix is complete.
  • cargo clippy -p bdk_esplora --all-features --all-targets -- -D warnings: the only findings are useless_borrows_in_formatting on the format!("http://{}", &env.electrsd.esplora_url...) lines in the existing test setup (src/*_ext.rs test modules and tests/*.rs). This PR doesn't touch those lines, and they're new lints in 1.100, so they aren't expected on the pinned 1.96 toolchain. Nothing is flagged in the changed code, and items_after_test_module is gone.

tACK 63766c9 on the gap logic. It now behaves the same for parallel_requests 1, 2 and 5 in the electrsd setup from my earlier comment.

@debojyoti10CC

Copy link
Copy Markdown
Author

Thanks @Jean-Regis-M for running the electrsd tests and the Linux build, and for the tACK. That check caught the compile error and confirmed the stop-gap behavior matches Electrum for batches 1, 2 and 5.

In the blocking client, handles after the stop-gap break were dropped unjoined, so errors from requests past the gap were ignored, while the async client awaits the whole batch and fails on them. Join every request first so both clients behave the same. Add electrsd tests checking that full_scan stops at stop_gap for parallel_requests 1, 2 and 5 (blocking and async).
@debojyoti10CC

Copy link
Copy Markdown
Author

Following up on the two non-blocking points, @Jean-Regis-M. Pushed 4a00615:

  • Electrsd tests: test_full_scan_stop_gap_ignores_parallel_requests (blocking) and test_async_full_scan_stop_gap_ignores_parallel_requests (async) use your setup: a tx on index 3, then for parallel_requests in [1, 2, 5], stop_gap = 3 returns no txs and no last_active_indices, while stop_gap = 4 finds the tx at index 3.
  • Error handling after the break: the blocking client now joins every handle in the batch before applying the gap, so an error from a request past the gap fails the scan, the same as try_collect in the async client.

As before, I can't compile locally, so CI (or another run on your side, if you have time) will be the check. rustfmt --check passes.

@Jean-Regis-M

Copy link
Copy Markdown

Ran 4a00615 on Linux (Rust 1.100), since you can't build locally:

  • cargo test -p bdk_esplora --all-features: lib 9/9, blocking_ext 8/8, async_ext 8/8, doc 1/1. That includes the two new tests, test_full_scan_stop_gap_ignores_parallel_requests and test_async_full_scan_stop_gap_ignores_parallel_requests.
  • With crates/esplora/src reset to the merge base (c5e87cc) and the new tests kept, both fail (blocking_ext.rs:514, async_ext.rs:513). So they do catch the original bug for parallel_requests > 1.
  • Clippy: nothing new apart from one more useless_borrows_in_formatting hit at tests/blocking_ext.rs:486. The new test copies the existing format!("http://{}", &env.electrsd.esplora_url...) line, so it's the same Rust 1.100 lint as the others and not something to fix in this PR.

Joining the whole batch before applying the gap in the blocking client also makes the error behaviour match try_collect on the async side, as you said. tACK 4a00615.

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.

Esplora full_scan can process transactions beyond stop_gap when using parallel requests

2 participants