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

pull pseudoramdom wants to merge 28 commits into bitcoin:master from pseudoramdom:bip332-staletips-pt3-blocksrelay changing 43 files +4080 −71
  1. pseudoramdom commented at 7:43 PM on November 20, 2026: contributor

    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.
  2. validation: add AcceptedStaleTip notification
    Notify validation interface listeners when a header or block is
    accepted that is not on the active chain and has no more work than
    the active tip. BIP 332 stale-tip tracking, added in later commits,
    uses this.
    
    Headers from one ProcessNewBlockHeaders() call are coalesced into one
    notification for the last new header. A block is notified again when
    its data is stored.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    97512c7963
  3. staletips: add bounded stale-tip tracker
    Add StaleTipTracker, which keeps up to MAX_ADVERTISED_STALETIPS
    recent stale tips that are eligible for relay under BIP 332. When
    full, it evicts the tip with the least work.
    
    Add BlockTree to test/util/blocktree.h so later tests can share it.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    d567ff5de9
  4. staletips: apply test-network tracking policies
    Test networks are less protected by proof of work, so BIP 332
    recommends stricter stale-tip policies there. On signet, only track
    tips whose block has been validated, and one header variant per
    block. On testnet3 and testnet4, only track tips with a difficulty
    of at least about 1,000,000.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    00660cb87a
  5. staletips: seed tracker from the block index
    The tracker is not persisted, so add SeedFromBlockIndex() to rebuild
    it at startup from the eligible stale tips in the block index.
    Candidates go through AddStaleTip(), so the same policies apply as
    at runtime.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    01f8a8f8f3
  6. init: add -staletips option
    Add -staletips=<mode> to opt in to BIP 332 stale-tip relay. For now
    'headers' only enables stale-tip tracking, which the next commits
    add; later PRs add relay behind the same mode.
    
    Make the PeerManager ApplyArgsManOptions() return util::Result, like
    the other ApplyArgsManOptions() helpers, so an invalid mode is
    reported at startup.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    c497ae1291
  7. net_processing: track local stale tips
    When -staletips is enabled, track the stale tips this node learns
    about.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    9cf113f3cc
  8. rpc: add max_depth and fork_point to getchaintips
    Add an optional max_depth argument that limits getchaintips to tips at
    most that many blocks below the active tip, and report fork_point for
    each tip that is not on the active chain.
    
    This lets callers find recent forks without listing every old stale
    branch on mainnet or walking back each branch to its fork point.
    61c349f95a
  9. rpc: report tracked stale tips in getchaintips
    Add a staletips_only argument to getchaintips that lists only the
    recent stale tips the node tracks with -staletips, taken from the
    stale-tip tracker.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    4173d9332c
  10. doc: add release note for stale-tip tracking 10fa8b7a08
  11. p2p: add staletip message type and payload
    Define the BIP 332 staletip message. Move MAX_STALETIP_HEADERS from
    the tracker to the message header, as the limit comes from the
    message format.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    882eaaf466
  12. refactor: move stale-tip tracker into StaleTipRelayManager
    Add StaleTipRelayManager to hold the stale-tip relay state and policy,
    starting with the tracker that PeerManager has owned so far. It
    requires cs_main instead of a lock of its own, as it holds CBlockIndex
    pointers.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    00237c9647
  13. staletips: build staletip messages for tracked forks
    Add MakeStaleTipMessage() to build the staletip message announcing a
    tracked fork. Make HasAncestor() public so it can check the fork.
    3580cd51dc
  14. net_processing: negotiate staletip relay via BIP 434
    Advertise the BIP 434 stale-tip relay feature when -staletips is
    enabled, and record the feature data of peers that advertise it.
    Malformed feature data is ignored. On signet, set prefers_blocks, as
    stale tips there can only be validated with their blocks.
    
    Add NetFeatures to hold the recognized BIP 434 features, so that their
    data is parsed and serialized in one place.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    eaa62d00de
  15. rpc: report BIP 434 features in getnetworkinfo and getpeerinfo
    Add "localfeatures" to getnetworkinfo and "features" to getpeerinfo,
    listing the BIP 434 features the node and each peer advertise, with
    their decoded data.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    1936b06089
  16. refactor: add window param to GetAntiDoSWorkThreshold
    Name the 144-block anti-DoS buffer ANTI_DOS_WORK_WINDOW and let
    callers pass a different window. A later commit checks inbound
    staletip messages against a wider window.
    7d352e13dd
  17. net_processing: validate and track inbound staletip messages
    Accept staletip messages from peers that negotiated the feature, and
    add their headers and track the tip if they pass validation and
    policy checks.
    
    Use STALETIP_RECENT_WINDOW for the work threshold rather than the
    default 144 blocks, so stale tips that peers may advertise are not
    discarded.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    e8e7377114
  18. net_processing: fetch higher-work blocks announced via staletip
    If a staletip branch has more work than our active tip, for example
    because we are behind, handle it as for a headers message: update the
    peer's header state and request the blocks through the direct-fetch
    path. Only do so when the peer set have_block, as otherwise it has not
    said it can serve the blocks.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    2418e2aa8a
  19. net_processing: announce active tips for stale-tip relay
    Queue an inv for our new active tip to peers that negotiated staletip,
    even when the normal announcement is skipped because the peer already
    has the header. A peer only sends us a staletip message whose fork
    point it knows we have, which it infers from the blocks we announce,
    so BIP 332 asks nodes to always announce their active tip.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    10b9f3ecd4
  20. net_processing: relay stale-tip announcements
    Announce each eligible stale tip once to negotiated peers that have its
    fork point but not the tip. On signet, wait until we can serve the
    block, which peers need to validate the tip.
    
    Remember per peer the tips it announced to us, so we don't echo them
    back. Remember the tips we announced to a peer for as long as they stay
    tracked, so a reorg doesn't make us announce them again.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    13d0ffb7e2
  21. net_processing: don't punish peers for invalid stale-branch blocks
    BIP 332 lets a peer serve blocks on a stale branch it never connected,
    so their scripts may be unchecked. Remember which tips a peer announced
    with have_block set, and, as for BIP 152 compact blocks, don't punish
    it for a block on such a branch that only fails once connected, or that
    already turned out invalid, through an ancestor, when it arrives.
    
    Still punish it for a block failing the checks done before storing it,
    as the peer must have passed them to store the block.
    9343e63c44
  22. doc: add BIP 332 to bips.md and release note de7a810827
  23. refactor: extract block serving limit checks
    Move the NODE_NETWORK_LIMITED and historical block serving checks out
    of ProcessGetBlockData into BelowLimitedServingThreshold and
    HistoricalServingLimitReached, so a later commit can avoid offering
    stale-branch blocks that getdata would refuse.
    b6a5c40021
  24. net_processing: serve tracked stale-branch blocks
    Peers that negotiated stale-tip relay can now fetch the blocks we have
    on a tracked stale branch, even ones normal block serving would refuse.
    
    We only set have_block if we have the tip and can serve the whole
    branch to that peer. Otherwise it might ask for a block we'd refuse,
    and get disconnected for it.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    Co-authored-by: Anthony Towns <aj@erisian.com.au>
    4d72063552
  25. staletips: schedule best-effort block requests
    Teach StaleTipRelayManager to pick which blocks to request when a peer
    offers a branch with have_block. have_block only promises the tip, so
    we start there and work back.
    
    Each block is asked of one peer at a time, and at most once per offer.
    If a request times out, another peer can be asked. These requests stay
    out of normal block download, so they never hold up the active chain.
    Nothing sends them yet.
    4535cae851
  26. refactor: name the fields of mapBlockSource
    Replace the pair in mapBlockSource with a BlockSource struct, so a
    later commit can add a field. No behavior change.
    43eb345d58
  27. net_processing: send best-effort staletip requests
    When a staletip had more work than our tip, we treated it like a
    headers message. Block download then expected every missing ancestor
    from that peer, and disconnected it if one never came, even though
    have_block only promises the tip.
    
    Send the manager's best-effort requests instead. If one times out, we
    move on without disconnecting anyone, and normal block download can
    still fetch the same block. A peer that sends an invalid block we asked
    for isn't punished, even if we've since forgotten its offer.
    
    One catch, a second peer offering the same tip is only asked once the
    first request times out.
    bf6e9f62a5
  28. net_processing: add -staletips=blocks
    In this mode we also download the missing blocks of stale branches
    that peers offer with have_block, and advertise prefers_blocks.
    
    For peers that prefer blocks, we hold off announcing a tip while we're
    fetching its branch. They still get a headers-only announcement if the
    fetch has to wait, or if the block offer arrives after we announced
    the tip.
    f467ebccb6
  29. DrahtBot added the label P2P on Nov 20, 2026
  30. DrahtBot commented at 7:43 PM on November 20, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy 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>

  31. doc: add release note for stale-tip block relay
    Describe serving stale-branch blocks, the new blocks mode, and the
    best-effort fetch of a branch with more work than the active tip.
    028ea5f845
  32. pseudoramdom force-pushed on Nov 20, 2026
  33. DrahtBot added the label CI failed on Nov 20, 2026
  34. DrahtBot added the label Needs rebase on Nov 23, 2026
  35. DrahtBot commented at 10:21 AM on November 23, 2026: contributor

    <!--cf906140f33d8803c4a75a2196329ecb-->

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


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-12-04 12:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me