Repository navigation
p2p: Track recent stale tips (BIP 332)(Stale Tip Relay) - #36919
pseudoramdom wants to merge 9 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. LLM Linter (✨ experimental)Possible places where comparison-specific test macros should replace generic comparisons:
2026-11-20 18:38:21 |
14140c8 to
165dcca
Compare
165dcca to
dcbf154
Compare
8190e67 to
84f9805
Compare
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 <[email protected]> Co-authored-by: Anthony Towns <[email protected]>
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 <[email protected]> Co-authored-by: Anthony Towns <[email protected]>
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 <[email protected]> Co-authored-by: Anthony Towns <[email protected]>
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 <[email protected]> Co-authored-by: Anthony Towns <[email protected]>
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 <[email protected]> Co-authored-by: Anthony Towns <[email protected]>
When -staletips is enabled, track the stale tips this node learns about. Co-authored-by: woltx <[email protected]> Co-authored-by: Anthony Towns <[email protected]>
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.
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 <[email protected]> Co-authored-by: Anthony Towns <[email protected]>
84f9805 to
10fa8b7
Compare
|
🐙 This pull request conflicts with the target branch and needs rebase. |
There was a problem hiding this comment.
Concept ACK
Performed first pass review. Left some comments inline, and here some generic ones:
- I suggest using
CBlockIndex&instead ofCBlockIndex*wherever possible. - It's unclear to me why is it needed to rebuild the stale tracker at startup?
- Has the cost and worst-case cost of the rebuilding been considered? It may be high as it involves the full whole chain.
| * | ||
| * Called on a background thread. | ||
| */ | ||
| virtual void AcceptedStaleTip(const CBlockIndex* pindex) {} |
There was a problem hiding this comment.
97512c7 validation: add AcceptedStaleTip notification:
Could we use const CBlockIndex& index here? I know that other methods in this interface use pointer type for the index, but a reference would be more appropriate here (less error prone, more idiomatic).
|
|
||
| void ValidationSignals::AcceptedStaleTip(const CBlockIndex* pindex) | ||
| { | ||
| auto log_msg = LOG_MSG("%s: block hash=%s block height=%d", __func__, |
There was a problem hiding this comment.
97512c7 validation: add AcceptedStaleTip notification:
Nit: The second "block" could be omitted from the message string.
| * it forks off the active chain. */ | ||
| struct StaleFork { | ||
| //! Last common ancestor of the stale tip and the active chain. Never null. | ||
| const CBlockIndex* fork_point; |
There was a problem hiding this comment.
d567ff5 staletips: add bounded stale-tip tracker:
I suggest using reference types here (as well), that better advertizes and enforces the "Never null" documented constraint.
| m_tips.erase(evict); | ||
| } | ||
|
|
||
| m_tips.push_back(&stale_tip); |
There was a problem hiding this comment.
d567ff5 staletips: add bounded stale-tip tracker:
I suggest adding an assert here on the size of m_tips (<= MAX_ADVERTISED_STALETIPS).
| std::vector<StaleFork> tips; | ||
| tips.reserve(m_tips.size()); |
There was a problem hiding this comment.
d567ff5 staletips: add bounded stale-tip tracker:
Nit: combine in one line
| //! Track at most one header variant of a block, where variants have the | ||
| //! same previous block and merkle root. Set on signet. | ||
| bool m_dedup_header_variants{false}; | ||
| //! Highest proof-of-work target a tracked tip may have, if limited. Set |
There was a problem hiding this comment.
00660cb staletips: apply test-network tracking policies:
IIUC, it's the lowest possible work, not the highest.

Part of #36833
This PR adds local tracking of recent stale tips for BIP 332.
The feature is behind a new
-staletips=<mode>option (defaultnone). There is no P2P change yet: with-staletips=headersthe node only tracks tips.Key changes
AcceptedStaleTipvalidation notification feeds the staletips tracker.getchaintipsRPC updatesmax_depthandstaletips_onlyfork_pointfor each non-active tip.Test networks get the stricter policies the BIP recommends:
Sample output (regtest, after a reorg replaced the tip at height 111)