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

Open
pseudoramdom wants to merge 9 commits from bip332-staletips-pt1-tracking into master AGit
pseudoramdom commented 2026-11-19 21:48:43 +00:00 (Migrated from github.com)

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"
  }
]
Part of #36833 This PR adds local tracking of recent stale tips for [BIP 332](https://github.com/bitcoin/bips/blob/master/bip-0332.md). 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. <details> <summary>Sample output (regtest, after a reorg replaced the tip at height 111)</summary> ``` $ 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" } ] ``` </details>
DrahtBot commented 2026-11-19 21:48:54 +00:00 (Migrated from github.com)

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

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36919 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36919.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36919#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Concept ACK | [optout21](https://github.com/bitcoin/bitcoin/pull/36919#pullrequestreview-6073375822) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36598](https://github.com/bitcoin/bitcoin/pull/36598) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36598.svg"></sub> ([Demo] validation: remove AssumeUTXO by l0rinc) * [#35713](https://github.com/bitcoin/bitcoin/pull/35713) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35713.svg"></sub> (Remove boost as a unit test runner by rustaceanrob) * [#35570](https://github.com/bitcoin/bitcoin/pull/35570) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35570.svg"></sub> (refactor: Change some validation.cpp methods to return BlockValidationState by optout21) * [#35474](https://github.com/bitcoin/bitcoin/pull/35474) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35474.svg"></sub> (node: move index ownership to NodeContext by w0xlt) * [#31260](https://github.com/bitcoin/bitcoin/pull/31260) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/31260.svg"></sub> (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. <!--5faf32d7da4f0f540f40219e4f7537a3--> ### 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)`. <sup>2026-11-20 18:38:21</sup>
First-time contributor

Earlier concerns remain unresolved; see the Run archive linked from the Review report.

🟠 Minor · Signet solution variants bypass the tracker’s merkle-root deduplication

On signet with -staletips=headers, a peer can reuse a signed block and mine solution variants that bypass deduplication, displacing genuine lower-work tips from the ten-entry tracker.

Location: src/node/staletiptracker.cpp:40 (head).

🟠 Minor · Ahead-of-tip headers are not reconsidered when they become stale

With -staletips=headers, an eligible competitor can remain untracked after the active chain catches up unless block data is accepted or startup seeding runs; the frequency of this headers-only sequence in production is unknown.

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

🟠 Minor · Reindex startup seeds the tracker before chain activation

With -staletips=headers, -reindex-chainstate and a fresh unpruned -reindex seed before chain activation, so eligible pre-existing forks can remain missing from getchaintips staletips_only until an ordinary restart.

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

🔵 Suggestion · max_depth help overpromises active-tip inclusion

The RPC help promises the active tip unconditionally, but getchaintips excludes it whenever staletips_only=true.

Location: src/rpc/blockchain.cpp:1611 (head).

🔵 Suggestion · Testnet4 difficulty coverage lacks a positive admission case

A too-strict testnet4 threshold would leave the new difficulty-policy assertions passing because successful admission near the boundary is checked only for TESTNET.

Location: src/test/staletiptracker_tests.cpp:345 (head).

🔵 Suggestion · Tracker wiring is separated from its observable integration tests

The independent getchaintips extension separates the PeerManager wiring from its first functional observation tests, making the series harder to validate commit by commit.

Location: src/net_processing.cpp:2239 (commit).

Review coverage was incomplete.


Reviewed: 10fa8b7a08ff · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:3705f06c6a27c475c77f2533c4d016ac03c829c839b7b538bba5e4768ccf54f1:1ed10c4979af6fc548d6b8d4534d505e02e84ac4b0aa4f8633555b2ba75119e8 --> <!-- ralph-base:a690300ee0d523de0b91e59b00a16fe47297cb85 --> <!-- ralph-head:10fa8b7a08ff214c197e27c022d6cfeb1b893ce4 --> <!-- ralph-case:389bad0c2f77b16f62463cd030dec538fb8a42d5b3fba7853bc307a31d31b64e --> Earlier concerns remain unresolved; see the Run archive linked from the Review report. **🟠 Minor · Signet solution variants bypass the tracker’s merkle-root deduplication** On signet with `-staletips=headers`, a peer can reuse a signed block and mine solution variants that bypass deduplication, displacing genuine lower-work tips from the ten-entry tracker. Location: [`src/node/staletiptracker.cpp:40`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/10fa8b7a08ff214c197e27c022d6cfeb1b893ce4/src/node/staletiptracker.cpp#L40>) (head). **🟠 Minor · Ahead-of-tip headers are not reconsidered when they become stale** With `-staletips=headers`, an eligible competitor can remain untracked after the active chain catches up unless block data is accepted or startup seeding runs; the frequency of this headers-only sequence in production is unknown. Location: [`src/validation.cpp:3851`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/10fa8b7a08ff214c197e27c022d6cfeb1b893ce4/src/validation.cpp#L3851>) (head). **🟠 Minor · Reindex startup seeds the tracker before chain activation** With `-staletips=headers`, `-reindex-chainstate` and a fresh unpruned `-reindex` seed before chain activation, so eligible pre-existing forks can remain missing from `getchaintips staletips_only` until an ordinary restart. Location: [`src/net_processing.cpp:2171`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/10fa8b7a08ff214c197e27c022d6cfeb1b893ce4/src/net_processing.cpp#L2171>) (head). **🔵 Suggestion · `max_depth` help overpromises active-tip inclusion** The RPC help promises the active tip unconditionally, but `getchaintips` excludes it whenever `staletips_only=true`. Location: [`src/rpc/blockchain.cpp:1611`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/10fa8b7a08ff214c197e27c022d6cfeb1b893ce4/src/rpc/blockchain.cpp#L1611>) (head). **🔵 Suggestion · Testnet4 difficulty coverage lacks a positive admission case** A too-strict testnet4 threshold would leave the new difficulty-policy assertions passing because successful admission near the boundary is checked only for `TESTNET`. Location: [`src/test/staletiptracker_tests.cpp:345`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/10fa8b7a08ff214c197e27c022d6cfeb1b893ce4/src/test/staletiptracker_tests.cpp#L345>) (head). **🔵 Suggestion · Tracker wiring is separated from its observable integration tests** The independent `getchaintips` extension separates the `PeerManager` wiring from its first functional observation tests, making the series harder to validate commit by commit. Location: src/net_processing.cpp:2239 (commit). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`10fa8b7a08ff`](<https://git.fish.foo/bitcoin/bitcoin/commit/10fa8b7a08ff214c197e27c022d6cfeb1b893ce4>) · [Review report](<https://review.fish.foo/report/36919-10fa8b7a08ff214c197e27c022d6cfeb1b893ce4-2889-5.html>) · [Previous reports](<https://review.fish.foo/traces/36919/>) [permalink](<https://review.fish.foo/report/36919-10fa8b7a08ff214c197e27c022d6cfeb1b893ce4-2889-5.html>)
w0xlt (Migrated from github.com) reviewed 2026-11-20 00:56:23 +00:00
w0xlt (Migrated from github.com) commented 2026-11-20 00:56:23 +00:00

This should fix the related CI errors.
The "macOS native, fuzz" is a CI environment problem.

diff --git a/src/test/util/blocktree.h b/src/test/util/blocktree.h
index f750e95669..0231db753e 100644
--- a/src/test/util/blocktree.h
+++ b/src/test/util/blocktree.h
@@ -25,7 +25,7 @@ struct BlockTreeAddOptions {
     bool have_data{false};
     uint32_t bits{0x1d00ffff};
     //! Merkle root of the header, or a value unique to the block if unset.
-    std::optional<uint256> merkle_root;
+    std::optional<uint256> merkle_root{};
 };
 
 /** Build a header on `prev` whose hash is unique for each `nonce`. */
This should fix the related CI errors. The "macOS native, fuzz" is a CI environment problem. ```diff diff --git a/src/test/util/blocktree.h b/src/test/util/blocktree.h index f750e95669..0231db753e 100644 --- a/src/test/util/blocktree.h +++ b/src/test/util/blocktree.h @@ -25,7 +25,7 @@ struct BlockTreeAddOptions { bool have_data{false}; uint32_t bits{0x1d00ffff}; //! Merkle root of the header, or a value unique to the block if unset. - std::optional<uint256> merkle_root; + std::optional<uint256> merkle_root{}; }; /** Build a header on `prev` whose hash is unique for each `nonce`. */ ```
pseudoramdom (Migrated from github.com) reviewed 2026-11-20 02:09:57 +00:00
pseudoramdom (Migrated from github.com) commented 2026-11-20 02:09:57 +00:00

Thanks! Fixed

Thanks! Fixed
DrahtBot commented 2026-11-23 10:22:33 +00:00 (Migrated from github.com)

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

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
optout21 (Migrated from github.com) reviewed 2026-11-26 12:13:56 +00:00
optout21 (Migrated from github.com) left a comment

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.
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.
optout21 (Migrated from github.com) commented 2026-11-26 11:47:00 +00:00

d567ff5 staletips: add bounded stale-tip tracker:

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

> [d567ff5](https://github.com/bitcoin/bitcoin/pull/36919/changes/d567ff5de9c4ac64531787723d4693e414111617) _staletips: add bounded stale-tip tracker_: I suggest adding an assert here on the size of m_tips (`<= MAX_ADVERTISED_STALETIPS`).
optout21 (Migrated from github.com) commented 2026-11-26 11:47:40 +00:00

d567ff5 staletips: add bounded stale-tip tracker:

Nit: combine in one line

> [d567ff5](https://github.com/bitcoin/bitcoin/pull/36919/changes/d567ff5de9c4ac64531787723d4693e414111617) _staletips: add bounded stale-tip tracker_: Nit: combine in one line
optout21 (Migrated from github.com) commented 2026-11-26 11:39:47 +00:00

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.

> [d567ff5](https://github.com/bitcoin/bitcoin/pull/36919/changes/d567ff5de9c4ac64531787723d4693e414111617) _staletips: add bounded stale-tip tracker_: I suggest using reference types here (as well), that better advertizes and enforces the "Never null" documented constraint.
optout21 (Migrated from github.com) commented 2026-11-26 11:51:58 +00:00

00660cb staletips: apply test-network tracking policies:

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

> [00660cb](https://github.com/bitcoin/bitcoin/pull/36919/changes/00660cb87af2b681c50fdd16131a5ecf7a652fbe) _staletips: apply test-network tracking policies_: IIUC, it's the lowest possible work, not the highest.
optout21 (Migrated from github.com) commented 2026-11-26 11:30:25 +00:00

97512c7 validation: add AcceptedStaleTip notification:

Nit: The second "block" could be omitted from the message string.

> [97512c7](https://github.com/bitcoin/bitcoin/pull/36919/changes/97512c7963a6ab8885031b8c4fcf9be3a271a8af) _validation: add AcceptedStaleTip notification_: Nit: The second "block" could be omitted from the message string.
optout21 (Migrated from github.com) commented 2026-11-26 11:29:14 +00:00

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

> [97512c7](https://github.com/bitcoin/bitcoin/pull/36919/changes/97512c7963a6ab8885031b8c4fcf9be3a271a8af) _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).
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36919
No description provided.