fix(btc): prepend coinbase placeholder leaf in stratum merkle branches - #570
Merged
Conversation
get_stratum_merkle_branches treated wd->m_hashes[0] as the coinbase
placeholder, but the producers (rpc.cpp, coin/template_builder.hpp) fill
m_hashes as the pure tx-hash list [tx1..txN] with NO coinbase slot. For
any populated (>=1 mempool tx) block this folded tx2 as the first sibling
and dropped tx1, so the stratum header merkle root diverged from the
serialized body -> bad-txnmrklroot rejection. Coinbase-only blocks
(empty m_hashes) early-return {} and collapse to coinbase_txid, so the
bug was latent until the first populated won block.
Fix (fenced to stratum/work_source.cpp, zero coinbase-byte reshape):
- get_stratum_merkle_branches: prepend uint256::ZERO coinbase placeholder
leaf before the branch fold; empty-mempool early return unchanged.
- mining_submit: non-fatal body-vs-header merkle self-check recomputing
compute_merkle_root([coinbase_txid, tx1..txN]) and logging OK/DIVERGENCE
before submit, so the leaf-set class is caught loudly not silently.
Mirrors bch-embedded-steward fenced fix; per-coin isolation preserved.
btc_share_test 39/39 green; make btc exit 0.
Extract the stratum coinbase branch fold into a pure SSOT helper
btc::coin::stratum_merkle_siblings() (template_builder.hpp) and delegate
get_stratum_merkle_branches() to it, so the wire path, the mining_submit
self-check, and tests all exercise one fold.
Add btc_share_test gtest stratum_merkle_branch_test.cpp (CI-bound, 39->42):
- FoldReconstructsBodyRoot: production siblings folded against a coinbase
txid equal compute_merkle_root([coinbase, tx1..txN]) for N in
{0,1,2,3,4,5,7,16} (covers the N=1 case the latent bug corrupted + odd
last-element duplication).
- CoinbaseOnlyHasNoBranches: empty template -> no branches, coinbase IS root.
- PreFixBuilderDivergesOnPopulated: the pre-#570 builder (no leaf-0
placeholder) diverges from the body root on every populated template ->
flip-RED proof the guard is not blind.
No consensus change; test + refactor only, BTC-fenced.
This was referenced Jun 27, 2026
frstrtr
added a commit
that referenced
this pull request
Jun 28, 2026
…T helper The segwit witness-commitment merkle root was computed inline in work_source.cpp with the coinbase wtxid placeholder (BIP141 32 zero bytes) at leaf 0 -- the same coinbase leaf-0 contract that PR #570 just fixed for the stratum txid merkle, but untested. A populated segwit block that dropped the first tx wtxid (or omitted the placeholder) would be rejected bad-witness-merkle-match by bitcoind, the witness-tree analogue of the #570 bug class, latent because the pool had never connected a populated won block. Extract btc::coin::witness_merkle_root() as the single source of truth for the leaf-0 placeholder + ordering, refactor work_source.cpp to call it (behaviour-identical), and add witness_commitment_merkle_test.cpp (4 KATs, flip-RED proven: removing the placeholder reds the two leaf-0 contract tests). btc_share_test 39 -> 43, BTC-fenced.
frstrtr
added a commit
that referenced
this pull request
Jul 25, 2026
… (reconstructor slice 4/7) (#838) Port the FRAMING half of p2pool data.py Share.as_block into btc::coin::assemble_won_block: reconstruct the full block header from the share's stored SmallBlockHeader + gentx txid walked up the (segwit-resolved) merkle link via the sealed btc::check_merkle_link, frame [gentx] ++ other_txs with the coinbase at index 0, and emit {bytes, hex} through the live BlockType TX_WITH_WITNESS conditional codec (the NodeRPC::submit_block wire path). BTC divergence from the DGB slice: BTC commits the BIP141 witness root inside the coinbase at gentx-BUILD time (share_check.hpp:2166/:2296), so slices 1-3 hand this framer an already-committed gentx. It therefore does NOT re-inject a witness commitment — doing so would mutate the coinbase, change its txid, invalidate the share-committed header root, and open a second merkle path the sealed merkle family (#570/#574/#579) forbids. The segwit-vs-legacy link selection stays in the caller (slice 5); this framer takes a resolved MerkleLink. Adds a fail-closed body/header guard: body_merkle_root folds [gentx_hash] ++ the non-witness other-tx txids through the sealed btc::coin::compute_merkle_root and assemble_won_block throws rather than emit a bad-txnmrklroot block. On a template_capture miss this passes for empty-link (coinbase-only) shares and correctly refuses the never-valid non-empty-link coinbase-only case. Slice cut off origin/master. btc-tree-only. btc_share_test 82/82 (10 new BtcBlockAssembly KATs, self-derived merkle branches). Co-authored-by: frstrtr <frstrtr@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes a latent bad-txnmrklroot in src/impl/btc/stratum/work_source.cpp (cross-coin flag routed from bch-embedded-steward; per-coin isolation preserved — only my BTC file touched).
Bug (producer/consumer m_hashes contract mismatch)
Fix (fenced, zero coinbase-byte reshape)
Smoke
Held for operator tap — I do not self-merge.