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

bitcoin/bitcoin: PR #36933

1 minor finding · 5 suggestions

Findings

Link

[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).

Details and suggested fix

For a tracked header-only tip T within serving limits, a request-eligible peer can offer T with have_block=true, leave the request unanswered, then evict its offer with 20 staletip messages containing known active-chain headers. The known-tip path records these messages, and deque eviction discards attempted. Re-offering T before expiry creates a fresh clear bitset. At expiry, the source's scheduling call removes and recreates the request atomically under cs_main. Other peers with empty offer deques return before expiry cleanup, while announcement selection checks only m_requests.contains(). Repeating this each block-interval timeout therefore prevents the stalled-fetch fallback. Another peer with an announcement history can interrupt the sequence by clearing expiry first; downloading T, disconnecting the source or losing tracker eligibility also ends suppression.

Bound the announcement hold-back independently of retries, for example by announcing headers-only after one timeout measured from the first request. Add a regression case that evicts and re-offers T across expiry.


Optional improvements

Link

[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).

Details and suggested fix

The signet tracker requires BLOCK_VALID_TRANSACTIONS, while GetFetchableForkPoint() returns null for these offers when fetch_blocks=false. Valid announced headers can still enter the block index, but the announcement alone cannot make the tip tracked or relayed. Locally validated tips and higher-work offers remain supported. Document in the -staletips help and release-notes-36920.md that learning equal- or lower-work stale tips this way on signet requires -staletips=blocks or block data from another path, rather than describing peer-announced tips as tracked unconditionally.


Link

[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).

Details and suggested fix

ProcessBlock() suppresses m_last_block_time solely because the reply is classified as STALETIP, even after ProcessNewBlock() has activated its block. Both relay modes request higher-work branches through this path. An otherwise identical unsolicited equal- or higher-work block passes AcceptBlock() and gets credited. Extra block-relay-only eviction and inbound protection compare this timestamp, and getpeerinfo exposes it as last_block. Keep the anti-gaming exclusion for genuinely stale downloads, but credit a newly stored reply if its block is on the active chain after processing.


Link

[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 at 43eb345d58ca (parent of bf6e9f62a599) (commit).

Details and suggested fix

2418e2aa8a179c27aa14c36c83ff923638e1945a adds UpdatePeerStateForReceivedHeaders() and HeadersDirectFetchBlocks() for higher-work offers. bf6e9f62a5992ae6f3d8b65fa681eab9307f75ec replaces them with best-effort requests after the scheduler from 4535cae851ca209e54b87c9f86309aff69dcdb3c. Its direct parent, 43eb345d58cab6928e96e1a1a6127912ee868e86, still contains the old calls. The earlier fetch design is already replaced at the head; this is a review-flow concern.

Extract the inert BlockRequestKind/ProcessBlock() conversions and default-false BlockSource::requested_staletip plumbing into a preparatory commit after that parent. Leave request classification, scheduling activation, peer-state replacement and their functional tests together in the corrective commit. Reviewers could then validate the unchanged receive path independently before inspecting the smaller behavioral switch.


Link

[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).

Details and suggested fix

test_normal_download_takes_over receives both bodies from normal_peer with ordinary requests outstanding. Replies in the higher-work cases have at least the active tip's work, so AcceptBlock() can store them without forced processing. The manager tests set BLOCK_HAVE_DATA directly. Add a blocks-mode case offering a strictly lower-work tip. Once its request is outstanding, connect a negotiated observer advertising prefers_blocks=true and establish that it knows the fork point. Check that the observer gets no announcement, deliver the body from the offering peer, verify that getblock succeeds without changing the active tip, then require the deferred announcement with have_block=true. An equal-work fixture would not distinguish loss of STALETIP forced storage.


Link

[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).

Details and suggested fix

IsExemptStaleBranchFailure() rejects an unstored block unless the result is BLOCK_CACHED_INVALID, before considering its announced branch or request provenance. A duplicate-input block therefore still follows the ordinary punishment path, which test_early_invalid_stale_branch_block_punished explicitly expects to disconnect. Qualify release-notes-36920.md to describe failures discovered when connecting a stored block or through cached invalidity, and state that pre-storage validation failures remain punishable.


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

The 1000-block window is a propagation/resource compromise, not a consensus boundary. Towns described it as a round number influenced by the interval between mainnet stale blocks, so new participants can discover useful data. The authors retained the wider window to improve propagation while adoption is low, despite discussion that 144 or 288 blocks might often suffice.

The affected acceptance path uses the stale-tip window instead of ordinary header admission’s approximately 144-block work buffer. The historical discussion explicitly identified the incompatibility of advertising older tips and then discarding them through normal header admission; the author committed to documenting compatible admission policy.

Source 1 · Source 2 · Source 3 · Source 4

BIP 434 exists to allow permissionless, implementation-independent feature deployment without risking disconnections or partitions from unknown messages. Feature negotiation is gated at protocol version 70017, occurs before verack, and new feature messages must not be sent to peers that have not advertised support. Unknown valid feature identifiers must be ignored.

Stale-tip relay must remain negotiated and optional, including its relaxed serving behavior. The BIP 332 discussion explicitly chose an exactly-one-byte boolean because versioned feature identifiers provide a clean mechanism for future changes, rather than accepting ambiguous extra configuration bytes.

Source 1 · Source 2

Unresolved disagreements

The record does not establish that relaying stale block bodies, rather than only headers, has a settled mining-incentive justification. Towns questioned the wisdom of sharing bodies in the early discussion, while the later tracking issue explicitly leaves mining-incentive effects open.

Faster potential reorganization is a documented benefit of prefetching, but it should not be treated as resolving all incentive consequences of blocks mode. No conclusion about the current change follows from this unresolved historical question.

Source 1 · Source 2