WIP: p2p: Relay stale tip announcements (BIP 332)

bitcoin/bitcoin: PR #36920

3 minor findings · 5 suggestions

Findings

Link

[minor] · have_block advertises blocks excluded by per-peer serving limits

-staletips=headers can advertise retained pruned-node blocks whose requests disconnect ordinary peers; no current Core caller was found that automatically requests these deep stale tips.

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

Details and suggested fix

can_serve_branch checks only BLOCK_HAVE_DATA and BlockRequestAllowed(). With manual pruning or sufficient retention, a reorged-out, script-valid stale tip can satisfy these checks 291 to 1000 blocks below the active tip while its branch remains short. Nevertheless, ProcessGetBlockData() disconnects requesters lacking NoBan because the pruned node advertises NODE_NETWORK_LIMITED without NODE_NETWORK. The callback also omits the upload-target restriction, which disconnects peers lacking Download when the requested block's timestamp is more than seven days older than the best header. A collecting client or manual getblockfrompeer request can therefore act on an availability offer that the sender will not honor. The flag also exposes retained-data presence below the existing privacy guard, although its fingerprinting significance is unclear. Share the recipient-specific serving conditions between this callback and ProcessGetBlockData(), and cover a retained stale tip beyond 290 blocks in a pruned-node test.


Link

[minor] · staletip creates requests while block import discards replies

-staletips=headers with -loadblock over a recent chain can request a higher-work announced block whose import-time reply is discarded without clearing the request; production frequency is unconfirmed.

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

Details and suggested fix

ProcessStaleTip() lacks the LoadingBlocks() guard used by HEADERS. Startup runs imports concurrently with networking once genesis is available, and CanDirectFetch() checks only tip recency. A negotiated higher-work announcement with have_block=true can therefore reach HeadersDirectFetchBlocks(), register an in-flight request, and send GETDATA during import. If the BLOCK reply arrives before import finishes, its handler returns before RemoveBlockRequest(). Other download paths then skip the in-flight block, and the announcing peer can reach the download timeout despite having replied. Disconnection clears its requests. Return early from ProcessStaleTip() while LoadingBlocks() is true, matching the existing header and block handlers.


Link

[minor] · FIFO eviction removes an outstanding stale-branch punishment exemption

Twenty later distinct announcements can erase a pending branch's punishment exemption before a full-block response is checked; no honest-peer sequence producing this churn was identified.

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

Details and suggested fix

ReceivedStaleTip() records announcements before tracker admission and evicts the oldest entry from its 20-entry FIFO regardless of outstanding requests. If a peer offers a higher-work branch with have_block=true, receives a request, and announces twenty unrelated tips before returning the full BLOCK, the original offer disappears. BlockChecked() recomputes the exemption from that FIFO rather than request provenance. A block that passes storage checks but fails ConnectBlock() then causes Misbehaving; an outbound peer can similarly lose the exemption for a requested cached-invalid descendant. Preserve stale-offer provenance with the request and carry it into block-source attribution, retaining the existing guard that leaves pre-storage failures punishable. Add a case that evicts the offer before delivering the response.


Optional improvements

Link

[suggestion] · Newly learned signet stale tips cannot be forwarded in headers mode

For a signet stale tip whose block data is unknown and whose work does not exceed the active tip's, headers mode advertises prefers_blocks=true but cannot acquire, track, or forward it.

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

Details and suggested fix

Signet tracking requires BLOCK_VALID_TRANSACTIONS, but HandleStaleTip() requests blocks only for strictly higher-work tips. An ordinary stale-tip announcement therefore cannot supply the block validation needed for admission. Serving creates another restriction: GetStaleTipsToAnnounce() requires have_block=true on signet, while BlockRequestAllowed() requires off-chain blocks to be script-valid. A race-losing block stored only as transaction-valid is tracked but not announced; previously connected, reorged-out blocks can be announced. The signet_announces_with_block unit test supplies transaction-valid data and mocks serving, so it does not establish end-to-end propagation. The headers-only scope may intentionally defer block retrieval. If so, defer BIP332 advertisement on signet and document the restriction until bounded acquisition and a compatible serving policy are supported.


Link

[suggestion] · A peer's prefers_blocks never affects announcements

In headers mode, peers advertising prefers_blocks=true receive the same one-shot announcement as other peers and cannot learn a tracked tip's later availability through an upgrade announcement.

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

Details and suggested fix

MaybeSendStaleTips() uses NegotiatedStaleTip(peer) only as a presence check and passes no preference to the relay manager. GetStaleTipsToAnnounce() suppresses previously announced tips before computing have_block; its only availability-based delay depends on the local chain being signet. Simply delaying all announcements for block-preferring peers would withhold useful headers indefinitely when a race-losing block never becomes serveable. Instead, pass the preference into the manager, remember the advertised have_block state, and permit one availability upgrade for those peers while the tip remains tracked. This preserves early header relay and bounded state without treating every repeat as a duplicate. If ignoring the preference is the intended scope of this mode, document that explicitly.


Link

[suggestion] · The release note overstates the invalid-block punishment exemption

The release note implies a blanket exemption for announced stale branches, but the implementation still punishes failures of checks performed before block storage.

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

Details and suggested fix

IsExemptStaleBranchFailure() requires stored block data or BLOCK_CACHED_INVALID, and IsAnnouncedStaleBranchBlock() additionally requires a remembered covering offer with have_block=true. The functional test test_early_invalid_stale_branch_block_punished explicitly expects disconnection for a duplicate-input failure in CheckBlock() despite such an offer. Change the note to distinguish offered blocks that fail when connected, or become invalid through an ancestor, from blocks that fail checks required before storage.


Link

[suggestion] · have_block fetching is separated from its punishment exemption

The series introduces higher-work fetching separately from its stale-branch rejection policy, so validating the fetch behavior requires reading ahead to the later exemption.

Location: src/net_processing.cpp:6061 at e9af700bc172 (commit) (commit).

Details and suggested fix

In e9af700bc17276b63ed5f1024eda3891a9522050, HandleStaleTip() starts direct fetching, but BlockChecked() still uses only !it->second.second; an offered full block that fails ConnectBlock() remains punishable. 89b6001408d6f4df4cadd9120abf30142bc8671c adds active-tip announcements, and 871d10b780b044f4982b6dfeaaa25f17a3b81188 adds per-peer relay history before f0a09269e4f10d9be2933476a279db966cdbf564 adds offer provenance, the exemption, and its punishment tests. Move fetching directly after the history commit and combine it with the exemption so the request and rejection policies can be validated together. Retain the HandleStaleTip() extraction and shared active_block() test helper before their first uses when rebasing, keeping intermediate commits buildable and testable. This changes review flow, not the final tree's exemption.


Link

[suggestion] · Header arrival order leaves eligible stale tips untracked until rediscovery

An otherwise eligible header-only fork can remain absent from live stale-tip tracking because it arrived before a competing active block, yet startup seeding can include it.

Location: src/validation.cpp:3851 (head).

Details and suggested fix

MaybeNotifyAcceptedStaleTip() excludes headers with more work than the active tip at acceptance, and UpdatedBlockTip() does not revisit them when the active chain catches up. With tracker capacity available, the same header can become eligible without entering relay or getchaintips(staletips_only=true), whereas SeedFromBlockIndex() considers it after restart. The already-known-tip functional case explicitly constructs this omission and repairs it with a staletip message. Block-data arrival also repairs the gap by generating another notification, so the limitation concerns headers whose data does not arrive. Recheck a small bounded set of pending higher-work candidates on tip changes using the existing eligibility and network-specific policies. If this event-order limitation is intentional, document it rather than leaving restart seeding to change which observations become visible.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

Stale-tip relay is optional observability infrastructure, not a requirement for chain selection. BIP 332 identifies stale rates as evidence about propagation delays, partitions, and adversarial mining, and notes that pre-downloading equal-work competing blocks can accelerate a subsequent reorg. Because these benefits are nonessential, it explicitly calls for low performance demands and strict resource limits.

This explains why the pinned change opts into bounded header relay rather than making every node collect and validate all stale blocks. A valid stale header is useful evidence without establishing that its corresponding block is valid.

Source 1