Skip to content

fix(btc): prepend coinbase placeholder leaf in stratum merkle branches - #570

Merged
frstrtr merged 2 commits into
masterfrom
btc/stratum-merkle-coinbase-leaf
Jun 27, 2026
Merged

fix(btc): prepend coinbase placeholder leaf in stratum merkle branches#570
frstrtr merged 2 commits into
masterfrom
btc/stratum-merkle-coinbase-leaf

Conversation

@frstrtr

@frstrtr frstrtr commented Jun 27, 2026

Copy link
Copy Markdown
Owner

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)

  • PRODUCER (coin/rpc.cpp, coin/template_builder.hpp) fills WorkData::m_hashes as the pure tx-hash list [tx1..txN] with NO coinbase slot.
  • CONSUMER get_stratum_merkle_branches assumed m_hashes[0] IS the coinbase placeholder.
  • For any populated (>=1 mempool tx) block it folded tx2 as the first sibling and dropped tx1 -> stratum header merkle root diverges from serialized body -> bad-txnmrklroot rejection. Coinbase-only blocks early-return {} so the bug stayed latent until the first populated won block (G3b live-proof).

Fix (fenced, zero coinbase-byte reshape)

  1. get_stratum_merkle_branches: prepend uint256::ZERO coinbase placeholder leaf before the branch fold; empty-mempool early return unchanged.
  2. mining_submit: non-fatal body-vs-header merkle self-check (recompute compute_merkle_root([coinbase_txid, tx1..txN]), log OK / loud DIVERGENCE) before submit.

Smoke

  • make btc exit 0; btc_share_test 39/39 green.

Held for operator tap — I do not self-merge.

frstrtr added 2 commits June 27, 2026 10:23
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.
@frstrtr
frstrtr merged commit 083071d into master Jun 27, 2026
22 of 26 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant