mining: add precious option to IPC block submission #35300

Open
w0xlt wants to merge 7 commits from ipc-submit-block-precious into master AGit
w0xlt commented 2026-05-16 03:23:48 +00:00 (Migrated from github.com)

This PR adds a precious flag to the IPC Mining submitBlock and submitSolution methods, as suggested in https://github.com/bitcoin/bitcoin/pull/34644#discussion_r3247072448. Built on top https://github.com/bitcoin/bitcoin/pull/34644.

With this change, IPC mining clients can submit and prefer a block in one IPC call, instead of submitting it over IPC and then using the preciousblock RPC separately.

By default, same-work side blocks keep the existing behavior: submitBlock reports duplicate/inconclusive cases as failure with a reason, while submitSolution continues to return true when ProcessNewBlock accepts or already knows the block.

Includes unit and IPC functional coverage for default behavior, precious reorgs, active-tip no-op submissions, and invalid-block rejection.

This PR adds a `precious` flag to the IPC Mining submitBlock and submitSolution methods, as suggested in https://github.com/bitcoin/bitcoin/pull/34644#discussion_r3247072448. Built on top https://github.com/bitcoin/bitcoin/pull/34644. With this change, IPC mining clients can submit and prefer a block in one IPC call, instead of submitting it over IPC and then using the `preciousblock` RPC separately. By default, same-work side blocks keep the existing behavior: `submitBlock` reports duplicate/inconclusive cases as failure with a reason, while `submitSolution` continues to return true when `ProcessNewBlock` accepts or already knows the block. Includes unit and IPC functional coverage for default behavior, precious reorgs, active-tip no-op submissions, and invalid-block rejection.
DrahtBot commented 2026-05-16 03:23:55 +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 Sjors, pablomartin4btc, ViniciusCestarii, johnnyasantoss

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:

  • #36469 (refactor: deduplicate block validation state catcher by enirox001)
  • #36097 (mining: replace interrupt methods with cancellation arguments by xyzconstant)
  • #36074 (scripted-diff: [test] Add util/check.h includes for assertions by maflcko)
  • #35671 (mining: add TxCollection to bandwidth-efficiently validate external block templates by Sjors)
  • #33922 (mining: add getMemoryLoad() and track template non-mempool memory footprint by Sjors)

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.

<!--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/35300 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35300.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35300#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 | [Sjors](https://github.com/bitcoin/bitcoin/pull/35300#pullrequestreview-4773259302), [pablomartin4btc](https://github.com/bitcoin/bitcoin/pull/35300#pullrequestreview-4776665286), [ViniciusCestarii](https://github.com/bitcoin/bitcoin/pull/35300#pullrequestreview-5320629901), [johnnyasantoss](https://github.com/bitcoin/bitcoin/pull/35300#issuecomment-5837061558) | 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: * [#36469](https://github.com/bitcoin/bitcoin/pull/36469) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36469.svg"></sub> (refactor: deduplicate block validation state catcher by enirox001) * [#36097](https://github.com/bitcoin/bitcoin/pull/36097) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36097.svg"></sub> (mining: replace interrupt methods with cancellation arguments by xyzconstant) * [#36074](https://github.com/bitcoin/bitcoin/pull/36074) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36074.svg"></sub> (scripted-diff: [test] Add util/check.h includes for assertions by maflcko) * [#35671](https://github.com/bitcoin/bitcoin/pull/35671) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35671.svg"></sub> (mining: add TxCollection to bandwidth-efficiently validate external block templates by Sjors) * [#33922](https://github.com/bitcoin/bitcoin/pull/33922) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33922.svg"></sub> (mining: add getMemoryLoad() and track template non-mempool memory footprint by Sjors) 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-->
DrahtBot commented 2026-05-16 04:02:47 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/25951528963/job/76290253269
LLM reason (✨ experimental): CI failed due to a C++ build error: src/node/miner.cpp would not compile (clang: passing CBlockIndex* where const CBlockIndex& is required in chainman.ActiveChain().Contains).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `test ancestor commits`: https://github.com/bitcoin/bitcoin/actions/runs/25951528963/job/76290253269</sub> <sub>LLM reason (✨ experimental): CI failed due to a C++ build error: `src/node/miner.cpp` would not compile (clang: passing `CBlockIndex*` where `const CBlockIndex&` is required in `chainman.ActiveChain().Contains`).</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
Sjors commented 2026-05-16 09:39:16 +00:00 (Migrated from github.com)

Maybe do this after #34672?

Maybe do this after #34672?
DrahtBot commented 2026-05-17 18:04:58 +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).
Sjors (Migrated from github.com) reviewed 2026-07-24 12:59:52 +00:00
Sjors (Migrated from github.com) left a comment

Concept ACK

There's some game theory miners should contemplate before using this though:

It's clearly rational to prefer your own block for p2p relay, but it's less obvious that you should mine the next block on it.

That might be a sunk-cost fallacy. If you saw the other block first, it's a good assumption that the rest of the network also saw it first.

In a Job Declaration scenario, the main criterion for which block to build on should be the side of the fork the pool picked. Unfortunately afaik the sv2 protocol doesn't currently communicate that (cc @plebhash).

Concept ACK There's some game theory miners should contemplate before using this though: It's clearly rational to prefer your own block _for p2p relay_, but it's less obvious that you should mine the next block on it. That might be a sunk-cost fallacy. If you saw the other block first, it's a good assumption that the rest of the network also saw it first. In a Job Declaration scenario, the main criterion for which block to build on should be the side of the fork the pool picked. Unfortunately afaik the sv2 protocol doesn't currently communicate that (cc @plebhash).
Sjors (Migrated from github.com) commented 2026-07-24 12:47:09 +00:00

In a1f8966316dcd0807d0ee612327d4a822c89177f test: cover precious mining submissions: mmm, I don't think precious should change the behavior if the block is duplicate, so just return duplicate?

Similarly, if we already had it, but it wasn't the tip, and we then switch the tip over, duplicate still seems like the correct response.

In a1f8966316dcd0807d0ee612327d4a822c89177f _test: cover precious mining submissions_: mmm, I don't think `precious` should change the behavior if the block is duplicate, so just return `duplicate`? Similarly, if we already had it, but it wasn't the tip, and we then switch the tip over, `duplicate` still seems like the correct response.
Sjors (Migrated from github.com) commented 2026-07-24 12:48:51 +00:00

In a1f8966316dcd0807d0ee612327d4a822c89177f test: cover precious mining submissions: this seems to be the main use case, so maybe cover it at the start of the test?

In general this test is massive, so maybe split it?

In a1f8966316dcd0807d0ee612327d4a822c89177f _test: cover precious mining submissions_: this seems to be the main use case, so maybe cover it at the start of the test? In general this test is massive, so maybe split it?
Sjors (Migrated from github.com) commented 2026-07-24 12:50:20 +00:00

In a1f8966316dcd0807d0ee612327d4a822c89177f test: cover precious mining submissions: since we expect more or less the same behavior as SubmitBlock, perhaps it's better to interleave the test? (and then have separate tests for each scenario, rather than for each method)

In a1f8966316dcd0807d0ee612327d4a822c89177f _test: cover precious mining submissions_: since we expect more or less the same behavior as `SubmitBlock`, perhaps it's better to interleave the test? (and then have separate tests for each scenario, rather than for each method)
pablomartin4btc (Migrated from github.com) reviewed 2026-07-24 21:33:05 +00:00
pablomartin4btc (Migrated from github.com) left a comment

Concept ACK

Adding precious=true to submitBlock and submitSolution, allows IPC mining clients atomically submit and prefer a same-work block in one go instead of a separate preciousblock RPC call.

The ActivateBestChain_ refactor is a clean prerequisite (so in commit 2 PreciousBlock can call it directly).

Left some nits (none blocking).

Concept ACK Adding `precious=true` to submitBlock and submitSolution, allows IPC mining clients atomically submit and prefer a same-work block in one go instead of a separate `preciousblock` RPC call. The `ActivateBestChain_` refactor is a clean prerequisite (so in commit 2 `PreciousBlock` can call it directly). Left some nits (none blocking).
pablomartin4btc (Migrated from github.com) commented 2026-07-24 21:29:20 +00:00

nit: perhaps worth clarifying that this includes blocks that are ancestors of the active tip, not just the tip itself, so callers don't assume result=true implies a reorg occurred? (If I understand it correctly)

nit: perhaps worth clarifying that this includes blocks that are ancestors of the active tip, not just the tip itself, so callers don't assume `result=true` implies a reorg occurred? (If I understand it correctly)
Lines 187-189
pablomartin4btc (Migrated from github.com) commented 2026-07-24 21:29:33 +00:00

nit: same as the above.

nit: same as the [above](https://github.com/bitcoin/bitcoin/pull/35300/changes#r3648263045).
pablomartin4btc (Migrated from github.com) commented 2026-07-24 21:05:42 +00:00

nit: connected is silently unused. A [[maybe_unused]] attribute or a brief comment (// RPC callers don't need to distinguish connected vs not) would help the next reader I think...

nit: `connected` is silently unused. A` [[maybe_unused]]` attribute or a brief comment `(// RPC callers don't need to distinguish connected vs not`) would help the next reader I think...
pablomartin4btc (Migrated from github.com) commented 2026-07-24 20:55:30 +00:00

nit: make_block_variant, assert_chain_tip, and assert_submit_block none of them access self — all should be @staticmethod

nit: `make_block_variant`, `assert_chain_tip`, and `assert_submit_block` none of them access self — all should be `@staticmethod`
DrahtBot commented 2026-08-14 15:42:55 +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).
w0xlt (Migrated from github.com) reviewed 2026-09-22 20:19:51 +00:00
w0xlt (Migrated from github.com) commented 2026-09-22 20:19:51 +00:00

Done. Thanks.

Done. Thanks.
w0xlt (Migrated from github.com) reviewed 2026-09-22 20:19:55 +00:00
w0xlt (Migrated from github.com) commented 2026-09-22 20:19:55 +00:00

Done. Thanks.

Done. Thanks.
w0xlt (Migrated from github.com) reviewed 2026-09-22 20:20:03 +00:00
w0xlt (Migrated from github.com) commented 2026-09-22 20:20:03 +00:00

Done. Thanks.

Done. Thanks.
w0xlt (Migrated from github.com) reviewed 2026-09-22 20:21:03 +00:00
w0xlt (Migrated from github.com) commented 2026-09-22 20:21:03 +00:00

Done. Thanks.

Done. Thanks.
w0xlt (Migrated from github.com) reviewed 2026-09-22 20:21:07 +00:00
w0xlt (Migrated from github.com) commented 2026-09-22 20:21:07 +00:00

Done. Thanks.

Done. Thanks.
w0xlt (Migrated from github.com) reviewed 2026-09-22 20:21:09 +00:00
w0xlt (Migrated from github.com) commented 2026-09-22 20:21:09 +00:00

Done. Thanks.

Done. Thanks.
w0xlt (Migrated from github.com) reviewed 2026-09-22 20:21:11 +00:00
Lines 187-189
w0xlt (Migrated from github.com) commented 2026-09-22 20:21:11 +00:00

Done. Thanks.

Done. Thanks.
w0xlt commented 2026-09-22 20:22:20 +00:00 (Migrated from github.com)

Rebased. All suggestions addressed. Ready for review.

Rebased. All suggestions addressed. Ready for review.
DrahtBot commented 2026-09-24 05:52:43 +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).
ViniciusCestarii (Migrated from github.com) reviewed 2026-09-25 17:27:02 +00:00
ViniciusCestarii (Migrated from github.com) left a comment

Concept ACK

Reviewed with @johnnyasantoss.

That might be a sunk-cost fallacy. If you saw the other block first, it's a good assumption that the rest of the network also saw it first. If the majority of hash rate is mining on the other side, and if you (the miner, not the pool) can't tip that majority over, it may be a waste of hash power.

There's no issue with the majority of the hash rate on the other side because a miner (or pool) is always mining against the rest of the network hash rate. So the odds of finding the next block are the same on either side, only the payout differs: 2 blocks on your own tip vs 1 on the other.

Concept ACK Reviewed with @johnnyasantoss. > That might be a sunk-cost fallacy. If you saw the other block first, it's a good assumption that the rest of the network also saw it first. If the majority of hash rate is mining on the other side, and if you (the miner, not the pool) can't tip that majority over, it may be a waste of hash power. There's no issue with the majority of the hash rate on the other side because a miner (or pool) is always mining against the rest of the network hash rate. So the odds of finding the next block are the same on either side, only the payout differs: 2 blocks on your own tip vs 1 on the other.
johnnyasantoss commented 2026-09-25 17:59:54 +00:00 (Migrated from github.com)

Concept ACK 22aeff71e8

I've reviewed the code but I'm not that confident in my cpp skills so I'll refrain from commenting it.


That might be a sunk-cost fallacy. If you saw the other block first, it's a good assumption that the rest of the network also saw it first. If the majority of hash rate is mining on the other side, and if you (the miner, not the pool) can't tip that majority over, it may be a waste of hash power.

cc @Sjors @0xB10C

Ran some thought experiments together with @ViniciusCestarii to understand whether precious=true always would be ideal. Thinking with context from this tweet1 regarding deep reorg viabtc vs. antpool2.

Seems it would be extremely unlikely to lose reward from working on a template on top of your own past work. Given that if the network finds a block the miner would revert to it and switch the template to the new best chain.

image

The miner would have to find an alternative block in the same timing as the rest of the network and then find another subsequent block on right after the network extends the other chain(!) to then get reorg'd and lose work. This seems like a black swan event. Chances of finding a block are related to hashrate and not prevhash so there's no waste in doubling down on your template AFAICT. Your odds at prevhash 0A and 0B are the same.
Electricity spent trying to find 1A and finding 1B would be the same regardless if the network sends you 1B, but on the other side reward finding 1A is roughly twice of that working on 1B.

maybe there's something we are not grasping, but intuitively precious=true makes a lot of sense.

Concept ACK 22aeff71e81186c714a4f7529e9922f5155e2de4 I've reviewed the code but I'm not that confident in my cpp skills so I'll refrain from commenting it. --- > That might be a sunk-cost fallacy. If you saw the other block first, it's a good assumption that the rest of the network also saw it first. If the majority of hash rate is mining on the other side, and if you (the miner, not the pool) can't tip that majority over, it may be a waste of hash power. cc @Sjors @0xB10C Ran some thought experiments together with @ViniciusCestarii to understand whether `precious=true` always would be ideal. Thinking with context from this tweet[^1] regarding deep reorg viabtc vs. antpool[^2]. Seems it would be extremely unlikely to lose reward from working on a template on top of your own past work. Given that if the network finds a block the miner would revert to it and switch the template to the new best chain. <img width="1130" height="892" alt="image" src="https://github.com/user-attachments/assets/25267aa8-5f99-46dc-8f75-ba7194883ca4" /> The miner would have to find an alternative block in the same timing as the rest of the network and then find another subsequent block on right after the network extends the other chain(!) to then get reorg'd and lose work. This seems like a black swan event. Chances of finding a block are related to hashrate and not prevhash so there's no waste in doubling down on your template AFAICT. Your odds at prevhash 0A and 0B are the same. Electricity spent trying to find 1A and finding 1B would be the _same_ regardless if the network sends you 1B, but on the other side reward finding 1A is roughly twice of that working on 1B. maybe there's something we are not grasping, but intuitively `precious=true` makes a lot of sense. [^1]: https://x.com/0xB10C/status/1804143652962185531 [^2]: https://b10c.me/blog/014-mining-pool-behavior-during-forks/
0xB10C commented 2026-09-29 21:45:48 +00:00 (Migrated from github.com)

I don't have the full context of the PR, but I agree with @johnnyasantoss @ViniciusCestarii here. If you don't mine on your own block, the maximum you can win is the next block. If you mine on your own block, you can win the last and the next block. Chances of finding the next block don't change, in both cases it's equal to your share of network hashrate.

I don't have the full context of the PR, but I agree with @johnnyasantoss @ViniciusCestarii here. If you don't mine on your own block, the maximum you can win is the next block. If you mine on your own block, you can win the last and the next block. Chances of finding the next block don't change, in both cases it's equal to your share of network hashrate.
First-time contributor

Base: acdb32693417c1b6596edfbdd19ab793c010d34f
Head: cac6117bfa3e5f70028affff9155cbb96466e8b4

No verified findings are available from this review.

Review coverage was incomplete. adversarial: Stage budget exhausted before the review completed. adversarial_glm: Stage budget exhausted before the review completed.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `acdb32693417c1b6596edfbdd19ab793c010d34f` Head: `cac6117bfa3e5f70028affff9155cbb96466e8b4` No verified findings are available from this review. Review coverage was incomplete. adversarial: Stage budget exhausted before the review completed. adversarial_glm: Stage budget exhausted before the review completed. [Full review report](<https://review.fish.foo/traces/35300-cac6117bfa3e5f70028affff9155cbb96466e8b4-624-1.html>)
DrahtBot commented 2026-10-14 08:23:41 +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).
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!35300
No description provided.