WIP: net: introduce block tracker to retry to download blocks after failure #35113

Closed
optout21 wants to merge 6 commits from block-dl into master AGit
optout21 commented 2026-04-19 09:01:25 +00:00 (Migrated from github.com)

Revival of closed/stale #27837 . The change is taken (as of b18c72cfcd) and rebased to current master. Original description follows.

Coming from #27652, part of #29183.

The general idea is to keep track of the user requested blocks so, in
case of a bad behaving peer or a network disconnection, they can be
fetched from another one automatically without any further user interaction.

This was requested by users because the getblockfrompeer RPC command
lacks the functionality to notify them about block request failures or peer
disconnections (which is expected due to the asynchronous nature of the block
requests).

Currently, this new functionality is limited to blocks requested by the
user via the 'getblockfrompeer' RPC command.

In the future, this class could expand its scope and be utilized in the
regular chain synchronization process. Or, even could be employed in
special procedures like a prune node rescan that uses BIP158 block filters,
or even into BIP157 itself.

Revival of closed/stale #27837 . The change is taken (as of b18c72cfcd6f27070928bc0ba16db6897960f211) and rebased to current master. Original description follows. Coming from #27652, part of #29183. The general idea is to keep track of the user requested blocks so, in case of a bad behaving peer or a network disconnection, they can be fetched from another one automatically without any further user interaction. This was requested by users because the `getblockfrompeer` RPC command lacks the functionality to notify them about block request failures or peer disconnections (which is expected due to the asynchronous nature of the block requests). Currently, this new functionality is limited to blocks requested by the user via the 'getblockfrompeer' RPC command. In the future, this class could expand its scope and be utilized in the regular chain synchronization process. Or, even could be employed in special procedures like a prune node rescan that uses BIP158 block filters, or even into BIP157 itself.
DrahtBot commented 2026-04-19 09:01:50 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #35498 (net: move cs_main up in FetchBlock to fix rpc assert crash by Crypt-iQ)
  • #34565 (refactor: extract BlockDownloadManager from PeerManagerImpl by w0xlt)
  • #28690 (build: Introduce internal kernel library by sedited)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/35113 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35113.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35113#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#35498](https://github.com/bitcoin/bitcoin/pull/35498) (net: move cs_main up in FetchBlock to fix rpc assert crash by Crypt-iQ) * [#34565](https://github.com/bitcoin/bitcoin/pull/34565) (refactor: extract BlockDownloadManager from PeerManagerImpl by w0xlt) * [#28690](https://github.com/bitcoin/bitcoin/pull/28690) (build: Introduce internal kernel library by sedited) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
luke-jr (Migrated from github.com) requested changes 2026-04-25 16:32:12 +00:00
luke-jr (Migrated from github.com) commented 2026-04-25 16:31:09 +00:00
                        "If omitted, the node will fetch the block from any available peer. (requires retry=true)"},

...or fix it to work without retry

```suggestion "If omitted, the node will fetch the block from any available peer. (requires retry=true)"}, ``` ...or fix it to work without retry
luke-jr (Migrated from github.com) commented 2026-04-25 16:31:46 +00:00

Booleans should ideally not be positional parameters, named only (eg, with an options Object)

Booleans should ideally not be positional parameters, named only (eg, with an options Object)
luke-jr (Migrated from github.com) commented 2026-04-25 16:29:37 +00:00

Sounds like that's a default of true (not RPCArg::Optional::OMITTED with a description noting it)?

Sounds like that's a default of true (not RPCArg::Optional::OMITTED with a description noting it)?
optout21 (Migrated from github.com) reviewed 2026-04-28 10:07:14 +00:00
optout21 (Migrated from github.com) left a comment

The last commit re-introduces behavior "what we had before". I will see how changes could be reordered to avoid removing some behavior and then re-introducing again.

The last commit re-introduces behavior "what we had before". I will see how changes could be reordered to avoid removing some behavior and then re-introducing again.
optout21 commented 2026-04-30 13:32:04 +00:00 (Migrated from github.com)

@luke-jr , thanks for the comments on the RPC parameters, I will consider them with upcoming changes.

@luke-jr , thanks for the comments on the RPC parameters, I will consider them with upcoming changes.
DrahtBot commented 2026-06-14 14:33:38 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
DrahtBot commented 2026-09-11 01:10:29 +00:00 (Migrated from github.com)

⌛ There hasn't been much activity lately and the patch still needs rebase. What is the status here?

  • Is it still relevant? ➡️ Please solve the conflicts to make it ready for review and to ensure the CI passes.
  • Is it no longer relevant? ➡️ Please close.
  • Did the author lose interest or time to work on this? ➡️ Please close it and mark it with one of the labels 'Up for grabs' or 'Insufficient Review', so that it can be picked up in the future.
<!--13523179cfe9479db18ec6c5d236f789--> ⌛ There hasn't been much activity lately and the patch still needs rebase. What is the status here? * Is it still relevant? ➡️ Please solve the conflicts to make it ready for review and to ensure the CI passes. * Is it no longer relevant? ➡️ Please close. * Did the author lose interest or time to work on this? ➡️ Please close it and mark it with one of the labels 'Up for grabs' or 'Insufficient Review', so that it can be picked up in the future.
optout21 commented 2026-10-22 14:46:22 +00:00 (Migrated from github.com)

Closing for now, due to lack of focus, limited personal resources. Could be marked "Up for grabs"!

Closing for now, due to lack of focus, limited personal resources. Could be marked "Up for grabs"!
optout21 commented 2026-10-22 14:48:12 +00:00 (Migrated from github.com)

Closing for now, due to lack of focus, limited personal resources. Could be marked "Up for grabs"!

Closing for now, due to lack of focus, limited personal resources. Could be marked "Up for grabs"!
First-time contributor

Base: f655a8df755330911c6c109c8b1d04ef5dd4ea09
Head: 2034c609f3111a874f6d10122e2fbe602bdf1dd9

Findings

Major

A mutated response can leave a request permanently tracked (src/net_processing.cpp:1744, head)

For a peerless request, the tracker records the peer assigned to download the block. If that peer returns a mutated block, the BLOCK handler removes its in-flight request and returns without calling untrack_request or untrack. Because RemoveBlockRequest erases the entry from vBlocksInFlight, the added FinalizeNode cleanup cannot reach it. The tracker remains assigned to the failed peer, does not retry, and rejects another peerless getblockfrompeer call as already tracked.

Pending selection can request witness blocks from peers that cannot serve them (src/net_processing.cpp:1428, head)

For a SegWit-active requested block, this branch accepts a non-limited peer based only on height and does not check CanServeWitnesses. The send path omits MSG_WITNESS_FLAG for that peer, while the ordinary block scan rejects blocks active under SegWit when the peer cannot serve witnesses. A compliant stripped response then fails the witness-mutation check and is treated as misbehavior. Apply the equivalent deployment-aware witness-capability check before POP.

Minor

A pending block can be selected twice for one peer (src/net_processing.cpp:1429, head)

If capacity remains and a requested missing side-branch block is also eligible in the peer's ordinary sync scan, POP appends it to vBlocks without marking it in mapBlocksInFlight. FindNextBlocks therefore does not see it as in flight and can append the same index again. SendMessages adds each entry to vGetData before calling BlockRequested, so it sends a duplicate GETDATA even though the second BlockRequested returns false. This requires the on-demand request to overlap ordinary sync; no concrete production workflow producing that overlap was identified.

Suggestions

Remove the unreachable duplicate-peer check (src/block_request_tracker.cpp:19, head)

A successful emplace creates a new default BlockRequest with an empty in_flight vector. If the block was already present, track has already returned at line 14. Therefore, Contains(req.in_flight, *peer_id) can never be true. Remove this unreachable branch and clarify the documented return conditions so the method does not suggest that it can add or check a peer for an already-tracked block.

Review coverage was incomplete.

Review limitations
  • adversarial_deepseek: Stage did not complete (ValueError).
  • adversarial_gemini: Repository inspection timed out.
  • design: Model API request timed out.
  • verifier: The linked upstream #27837 discussion could not be inspected, so its conceptual-history claims are not independently verified. All supplied code candidates and the named request-selection, tracking, and validation interactions were checked against the checkout.
  • verifier: Finding withheld: Source evidence does not match the snapshot
  • verifier: Decision withheld: invalid candidate ID accounting
  • verifier: Finding withheld: Unknown candidate IDs: build:3

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:b41bdf4ebdd51f70cc4e325a472af5d26b399a00336f341a1856879961e0f932:f359184720b19d8f4199eb38e750c73a24dbe6263b5428fbe549b1858030cb9b --> Base: `f655a8df755330911c6c109c8b1d04ef5dd4ea09` Head: `2034c609f3111a874f6d10122e2fbe602bdf1dd9` ##### Findings **Major** **A mutated response can leave a request permanently tracked** (src/net_processing.cpp:1744, head) For a peerless request, the tracker records the peer assigned to download the block. If that peer returns a mutated block, the BLOCK handler removes its in-flight request and returns without calling `untrack_request` or `untrack`. Because `RemoveBlockRequest` erases the entry from `vBlocksInFlight`, the added `FinalizeNode` cleanup cannot reach it. The tracker remains assigned to the failed peer, does not retry, and rejects another peerless `getblockfrompeer` call as already tracked. **Pending selection can request witness blocks from peers that cannot serve them** (src/net_processing.cpp:1428, head) For a SegWit-active requested block, this branch accepts a non-limited peer based only on height and does not check `CanServeWitnesses`. The send path omits `MSG_WITNESS_FLAG` for that peer, while the ordinary block scan rejects blocks active under SegWit when the peer cannot serve witnesses. A compliant stripped response then fails the witness-mutation check and is treated as misbehavior. Apply the equivalent deployment-aware witness-capability check before POP. **Minor** **A pending block can be selected twice for one peer** (src/net_processing.cpp:1429, head) If capacity remains and a requested missing side-branch block is also eligible in the peer's ordinary sync scan, POP appends it to `vBlocks` without marking it in `mapBlocksInFlight`. `FindNextBlocks` therefore does not see it as in flight and can append the same index again. `SendMessages` adds each entry to `vGetData` before calling `BlockRequested`, so it sends a duplicate GETDATA even though the second `BlockRequested` returns false. This requires the on-demand request to overlap ordinary sync; no concrete production workflow producing that overlap was identified. **Suggestions** **Remove the unreachable duplicate-peer check** (src/block_request_tracker.cpp:19, head) A successful `emplace` creates a new default `BlockRequest` with an empty `in_flight` vector. If the block was already present, `track` has already returned at line 14. Therefore, `Contains(req.in_flight, *peer_id)` can never be true. Remove this unreachable branch and clarify the documented return conditions so the method does not suggest that it can add or check a peer for an already-tracked block. Review coverage was incomplete. <details> <summary>Review limitations</summary> - adversarial_deepseek: Stage did not complete (ValueError). - adversarial_gemini: Repository inspection timed out. - design: Model API request timed out. - verifier: The linked upstream #27837 discussion could not be inspected, so its conceptual-history claims are not independently verified. All supplied code candidates and the named request-selection, tracking, and validation interactions were checked against the checkout. - verifier: Finding withheld: Source evidence does not match the snapshot - verifier: Decision withheld: invalid candidate ID accounting - verifier: Finding withheld: Unknown candidate IDs: build:3 </details> [Full review report](<https://review.fish.foo/traces/35113-2034c609f3111a874f6d10122e2fbe602bdf1dd9-1119-1.html>)
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!35113
No description provided.