WIP: p2p: serve and fetch stale-branch blocks (BIP 332)(Stale Tip Relay) #36933

Draft
pseudoramdom wants to merge 28 commits from bip332-staletips-pt3-blocksrelay into master AGit
pseudoramdom commented 2026-11-20 19:43:46 +00:00 (Migrated from github.com)

Part of #36833.
Based on #36920; review from refactor: extract block serving limit checks.

This PR lets peers fetch the blocks behind the stale tips announced in #36920. We now serve blocks on the stale branches we track, and the new -staletips=blocks mode downloads the branches peers offer.

Key changes

  • -staletips=blocks advertises prefers_blocks=true and fetches offered branches. For peers that prefer blocks, we wait to announce a tip until we've fetched it, unless the fetch stalls.
  • Fetches are best-effort and separate from normal block download:
    • They start at the tip, since that's all have_block promises.
    • Each block is requested from one peer at a time, and only while that peer has fewer than 8 blocks in flight.
  • This also fixes how #36920 handles a higher-work staletip. It used to go through normal block download, which expected every ancestor from that peer and could disconnect it. Now it's fetched best-effort, in both modes.
Part of #36833. Based on #36920; review from `refactor: extract block serving limit checks`. This PR lets peers fetch the blocks behind the stale tips announced in #36920. We now serve blocks on the stale branches we track, and the new -staletips=blocks mode downloads the branches peers offer. ### Key changes * `-staletips=blocks` advertises `prefers_blocks=true` and fetches offered branches. For peers that prefer blocks, we wait to announce a tip until we've fetched it, unless the fetch stalls. * Fetches are best-effort and separate from normal block download: * They start at the tip, since that's all `have_block` promises. * Each block is requested from one peer at a time, and only while that peer has fewer than 8 blocks in flight. * This also fixes how #36920 handles a higher-work `staletip`. It used to go through normal block download, which expected every ancestor from that peer and could disconnect it. Now it's fetched best-effort, in both modes.
DrahtBot commented 2026-11-20 19:43:56 +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.

LLM Linter (✨ experimental)

Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

  • src/test/staletiprelayman_tests.cpp: GetBlockRequestsToSend(peer.id, tree.active_chain, 0s, 1, AnyBlock)
  • src/test/staletiprelayman_tests.cpp: GetBlockRequestsToSend(0, chain, 0s, 2, AnyBlock)
  • src/test/staletiprelayman_tests.cpp: GetBlockRequestsToSend(0, chain, 0s, 0, AnyBlock)
  • src/test/staletiprelayman_tests.cpp: GetBlockRequestsToSend(0, chain, 0s, 1, AnyBlock) (block request lifecycle; four occurrences)
  • src/test/staletiprelayman_tests.cpp: GetBlockRequestsToSend(1, chain, 9s, 1, AnyBlock)
  • src/test/staletiprelayman_tests.cpp: GetBlockRequestsToSend(0, chain, 10s, 1, AnyBlock)
  • src/test/staletiprelayman_tests.cpp: GetBlockRequestsToSend(1, chain, 10s, 1, AnyBlock)
  • src/test/staletiprelayman_tests.cpp: GetBlockRequestsToSend(0, chain, 0s, 1, AnyBlock) (higher-work block requests)
  • src/test/staletiprelayman_tests.cpp: GetBlockRequestsToSend(0, chain, 0s, 1, [](const CBlockIndex&) { return false; })
  • src/test/staletiprelayman_tests.cpp: GetBlockRequestsToSend(0, chain, 0s, 2, AnyBlock)
  • src/test/staletiprelayman_tests.cpp: GetBlockRequestsToSend(0, chain, 0s, 1, AnyBlock) (defers while requesting)

Possible places where comparison-specific test macros should replace generic comparisons:

  • src/test/staletipmessage_tests.cpp: BOOST_CHECK_THROW(DecodeExact(trailing), std::ios_base::failure) → Prefer BOOST_CHECK_EXCEPTION with a predicate checking the expected failure message.
  • src/test/staletipmessage_tests.cpp: BOOST_CHECK_THROW(DecodeExact(invalid_bool), std::ios_base::failure) → Prefer BOOST_CHECK_EXCEPTION with a predicate checking the expected failure message.
  • src/test/staletipmessage_tests.cpp: BOOST_CHECK_THROW(DecodeExact(truncated), std::ios_base::failure) → Prefer BOOST_CHECK_EXCEPTION with a predicate checking the expected failure message.
  • src/test/staletipmessage_tests.cpp: BOOST_CHECK_THROW(DecodeExact(noncanonical), std::ios_base::failure) → Prefer BOOST_CHECK_EXCEPTION with a predicate checking the expected failure message.
  • src/test/staletipmessage_tests.cpp: BOOST_CHECK_THROW(DecodeExact(empty_headers), std::ios_base::failure) → Prefer BOOST_CHECK_EXCEPTION with a predicate checking the expected failure message.
  • test/functional/p2p_staletip.py: assert len(stale_features) == 1 → Prefer assert_equal(len(stale_features), 1).
  • test/functional/p2p_staletip.py: assert len(tracked) > 0 → Prefer assert_greater_than(len(tracked), 0).
  • test/functional/p2p_staletip.py: assert len([msg for msg in peer.staletips if msg.tip_hash() == block.hash_int]) == 1 → Prefer assert_equal(len([msg for msg in peer.staletips if msg.tip_hash() == block.hash_int]), 1).

2026-11-20 19:47:30

<!--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/36933 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36933.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36933#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. <!--5faf32d7da4f0f540f40219e4f7537a3--> ### LLM Linter (✨ experimental) Possible places where named args for integral literals may be used (e.g. `func(x, /*named_arg=*/0)` in C++, and `func(x, named_arg=0)` in Python): - `src/test/staletiprelayman_tests.cpp`: `GetBlockRequestsToSend(peer.id, tree.active_chain, 0s, 1, AnyBlock)` - `src/test/staletiprelayman_tests.cpp`: `GetBlockRequestsToSend(0, chain, 0s, 2, AnyBlock)` - `src/test/staletiprelayman_tests.cpp`: `GetBlockRequestsToSend(0, chain, 0s, 0, AnyBlock)` - `src/test/staletiprelayman_tests.cpp`: `GetBlockRequestsToSend(0, chain, 0s, 1, AnyBlock)` (block request lifecycle; four occurrences) - `src/test/staletiprelayman_tests.cpp`: `GetBlockRequestsToSend(1, chain, 9s, 1, AnyBlock)` - `src/test/staletiprelayman_tests.cpp`: `GetBlockRequestsToSend(0, chain, 10s, 1, AnyBlock)` - `src/test/staletiprelayman_tests.cpp`: `GetBlockRequestsToSend(1, chain, 10s, 1, AnyBlock)` - `src/test/staletiprelayman_tests.cpp`: `GetBlockRequestsToSend(0, chain, 0s, 1, AnyBlock)` (higher-work block requests) - `src/test/staletiprelayman_tests.cpp`: `GetBlockRequestsToSend(0, chain, 0s, 1, [](const CBlockIndex&) { return false; })` - `src/test/staletiprelayman_tests.cpp`: `GetBlockRequestsToSend(0, chain, 0s, 2, AnyBlock)` - `src/test/staletiprelayman_tests.cpp`: `GetBlockRequestsToSend(0, chain, 0s, 1, AnyBlock)` (defers while requesting) Possible places where comparison-specific test macros should replace generic comparisons: - `src/test/staletipmessage_tests.cpp`: `BOOST_CHECK_THROW(DecodeExact(trailing), std::ios_base::failure)` → Prefer `BOOST_CHECK_EXCEPTION` with a predicate checking the expected failure message. - `src/test/staletipmessage_tests.cpp`: `BOOST_CHECK_THROW(DecodeExact(invalid_bool), std::ios_base::failure)` → Prefer `BOOST_CHECK_EXCEPTION` with a predicate checking the expected failure message. - `src/test/staletipmessage_tests.cpp`: `BOOST_CHECK_THROW(DecodeExact(truncated), std::ios_base::failure)` → Prefer `BOOST_CHECK_EXCEPTION` with a predicate checking the expected failure message. - `src/test/staletipmessage_tests.cpp`: `BOOST_CHECK_THROW(DecodeExact(noncanonical), std::ios_base::failure)` → Prefer `BOOST_CHECK_EXCEPTION` with a predicate checking the expected failure message. - `src/test/staletipmessage_tests.cpp`: `BOOST_CHECK_THROW(DecodeExact(empty_headers), std::ios_base::failure)` → Prefer `BOOST_CHECK_EXCEPTION` with a predicate checking the expected failure message. - `test/functional/p2p_staletip.py`: `assert len(stale_features) == 1` → Prefer `assert_equal(len(stale_features), 1)`. - `test/functional/p2p_staletip.py`: `assert len(tracked) > 0` → Prefer `assert_greater_than(len(tracked), 0)`. - `test/functional/p2p_staletip.py`: `assert len([msg for msg in peer.staletips if msg.tip_hash() == block.hash_int]) == 1` → Prefer `assert_equal(len([msg for msg in peer.staletips if msg.tip_hash() == block.hash_int]), 1)`. <sup>2026-11-20 19:47:30</sup>
First-time contributor

🟠 Minor · Re-offering a tip bypasses the stalled-fetch announcement fallback

With -staletips=blocks, a negotiated peer can repeatedly reset its offer and suppress a tracked tip's announcements to prefers_blocks peers for its tracked lifetime when it is the only peer with an announcement history.

Location: src/node/staletiprelayman.cpp:236 (head).

🔵 Suggestion · Signet headers mode cannot track header-only stale-tip offers

On signet, -staletips=headers advertises prefers_blocks=true but cannot track an unfamiliar equal- or lower-work offer without separately received block data, a limitation absent from the help and release note.

Location: src/node/staletiprelayman.cpp:262 (head).

🔵 Suggestion · Best-effort active-chain deliveries receive no block-delivery credit

A STALETIP reply that activates a higher-work branch never updates m_last_block_time, so it receives less block-delivery credit for eviction than the same newly stored block sent unsolicited.

Location: src/net_processing.cpp:3849 (head).

🔵 Suggestion · The best-effort fetch switch mixes plumbing and behavior changes

The best-effort switch in bf6e9f6 mixes receive-side plumbing with replacement of the fetch path introduced by 2418e2a, making the behavioral transition harder to validate separately.

Location: src/net_processing.cpp:6151 (commit).

🔵 Suggestion · No functional test covers lower-work storage followed by have_block relay

The functional suite does not verify that a best-effort reply containing a strictly lower-work block is stored and releases a deferred have_block=true announcement.

Location: test/functional/p2p_staletip.py:763 (head).

🔵 Suggestion · The stale-branch release note overstates the punishment exemption

The release note promises no disconnect for an invalid announced-branch block, but the implementation intentionally still punishes failures detectable before storage.

Location: doc/release-notes-36920.md:9 (head).

Review coverage was incomplete.


Reviewed: 028ea5f8455b · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:3705f06c6a27c475c77f2533c4d016ac03c829c839b7b538bba5e4768ccf54f1:1ed10c4979af6fc548d6b8d4534d505e02e84ac4b0aa4f8633555b2ba75119e8 --> <!-- ralph-base:a690300ee0d523de0b91e59b00a16fe47297cb85 --> <!-- ralph-head:028ea5f8455b24745c3b38f71a355556e8962b25 --> <!-- ralph-case:d53e13dad59386426660b052dea3ca4d8fd074620414fe8105d107872f3a3f12 --> **🟠 Minor · Re-offering a tip bypasses the stalled-fetch announcement fallback** With `-staletips=blocks`, a negotiated peer can repeatedly reset its offer and suppress a tracked tip's announcements to `prefers_blocks` peers for its tracked lifetime when it is the only peer with an announcement history. Location: [`src/node/staletiprelayman.cpp:236`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/028ea5f8455b24745c3b38f71a355556e8962b25/src/node/staletiprelayman.cpp#L236>) (head). **🔵 Suggestion · Signet `headers` mode cannot track header-only stale-tip offers** On signet, `-staletips=headers` advertises `prefers_blocks=true` but cannot track an unfamiliar equal- or lower-work offer without separately received block data, a limitation absent from the help and release note. Location: [`src/node/staletiprelayman.cpp:262`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/028ea5f8455b24745c3b38f71a355556e8962b25/src/node/staletiprelayman.cpp#L262>) (head). **🔵 Suggestion · Best-effort active-chain deliveries receive no block-delivery credit** A `STALETIP` reply that activates a higher-work branch never updates `m_last_block_time`, so it receives less block-delivery credit for eviction than the same newly stored block sent unsolicited. Location: [`src/net_processing.cpp:3849`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/028ea5f8455b24745c3b38f71a355556e8962b25/src/net_processing.cpp#L3849>) (head). **🔵 Suggestion · The best-effort fetch switch mixes plumbing and behavior changes** The best-effort switch in `bf6e9f6` mixes receive-side plumbing with replacement of the fetch path introduced by `2418e2a`, making the behavioral transition harder to validate separately. Location: src/net_processing.cpp:6151 (commit). **🔵 Suggestion · No functional test covers lower-work storage followed by `have_block` relay** The functional suite does not verify that a best-effort reply containing a strictly lower-work block is stored and releases a deferred `have_block=true` announcement. Location: [`test/functional/p2p_staletip.py:763`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/028ea5f8455b24745c3b38f71a355556e8962b25/test/functional/p2p_staletip.py#L763>) (head). **🔵 Suggestion · The stale-branch release note overstates the punishment exemption** The release note promises no disconnect for an invalid announced-branch block, but the implementation intentionally still punishes failures detectable before storage. Location: [`doc/release-notes-36920.md:9`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/028ea5f8455b24745c3b38f71a355556e8962b25/doc/release-notes-36920.md#L9>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`028ea5f8455b`](<https://git.fish.foo/bitcoin/bitcoin/commit/028ea5f8455b24745c3b38f71a355556e8962b25>) · [Review report](<https://review.fish.foo/report/36933-028ea5f8455b24745c3b38f71a355556e8962b25-2895-2.html>) · [Previous reports](<https://review.fish.foo/traces/36933/>) [permalink](<https://review.fish.foo/report/36933-028ea5f8455b24745c3b38f71a355556e8962b25-2895-2.html>)
DrahtBot commented 2026-11-23 10:21:50 +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).
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!36933
No description provided.