Skip to content

p2p: Track recent stale tips (BIP 332)(Stale Tip Relay) - #36919

Open
pseudoramdom wants to merge 9 commits into
bitcoin:masterfrom
pseudoramdom:bip332-staletips-pt1-tracking
Open

pseudoramdom wants to merge 9 commits into
bitcoin:masterfrom
pseudoramdom:bip332-staletips-pt1-tracking

Conversation

@pseudoramdom

Copy link
Copy Markdown
Contributor

Part of #36833

This PR adds local tracking of recent stale tips for BIP 332.
The feature is behind a new -staletips=<mode> option (default none). There is no P2P change yet: with -staletips=headers the node only tracks tips.

Key changes

  • A new AcceptedStaleTip validation notification feeds the staletips tracker.
  • The tracker is built from the block index at startup, keeping the tips with the most work.
    • At most 10 tips are tracked. When the tracker is full, a new tip evicts the one with the least work, but only if the new tip has more work.
  • getchaintips RPC updates
    • Add new options max_depth and staletips_only
    • Report fork_point for each non-active tip.

Test networks get the stricter policies the BIP recommends:

  • signet: only tips whose block has been validated, and one header variant per block.
  • testnet3/4: only tips with a difficulty of at least about 1,000,000.
Sample output (regtest, after a reorg replaced the tip at height 111)
$ bitcoin-cli getchaintips
[
  {
    "height": 112,
    "hash": "544a1c7f23c96990e2db9f4415f498d1ee61effac8e7416ec53fb91f538b2d2c",
    "branchlen": 0,
    "status": "active"
  },
  {
    "height": 111,
    "hash": "3a0797803400d80abdbf34f73df33f760c1e24696531746f975e3041055b6967",
    "branchlen": 1,
    "fork_point": "43765e057b6cde12df1701595d90ae51cf80490fef1343edd2d3fbe2edf3958e",
    "status": "valid-fork"
  }
]

$ bitcoin-cli -named getchaintips staletips_only=true
[
  {
    "height": 111,
    "hash": "3a0797803400d80abdbf34f73df33f760c1e24696531746f975e3041055b6967",
    "branchlen": 1,
    "fork_point": "43765e057b6cde12df1701595d90ae51cf80490fef1343edd2d3fbe2edf3958e",
    "status": "valid-fork"
  }
]

@DrahtBot DrahtBot added the P2P label Nov 19, 2026
@DrahtBot

DrahtBot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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.

Type Reviewers
Concept ACK optout21

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36598 ([Demo] validation: remove AssumeUTXO by l0rinc)
  • #35713 (Remove boost as a unit test runner by rustaceanrob)
  • #35570 (refactor: Change some validation.cpp methods to return BlockValidationState by optout21)
  • #35474 (node: move index ownership to NodeContext by w0xlt)
  • #31260 (scripted-diff: Type-safe settings retrieval by ryanofsky)

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:

  • test/functional/p2p_staletip.py: assert len(tracked) > 0 → Prefer assert_greater_than(len(tracked), 0).

2026-11-20 18:38:21

Comment thread src/test/util/blocktree.h Resolved
@pseudoramdom
pseudoramdom force-pushed the bip332-staletips-pt1-tracking branch 2 times, most recently from 8190e67 to 84f9805 Compare November 20, 2026 02:07
pseudoramdom and others added 9 commits November 20, 2026 10:33
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]>
@DrahtBot

Copy link
Copy Markdown
Contributor

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

@optout21 optout21 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK
Performed first pass review. Left some comments inline, and here some generic ones:

  • I suggest using CBlockIndex& instead of CBlockIndex* 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.

Comment thread src/validationinterface.h
*
* Called on a background thread.
*/
virtual void AcceptedStaleTip(const CBlockIndex* pindex) {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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__,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d567ff5 staletips: add bounded stale-tip tracker:

I suggest adding an assert here on the size of m_tips (<= MAX_ADVERTISED_STALETIPS).

Comment on lines +95 to +96
std::vector<StaleFork> tips;
tips.reserve(m_tips.size());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

00660cb staletips: apply test-network tracking policies:

IIUC, it's the lowest possible work, not the highest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants