Skip to content

fix(channels_sv2)!: coinbase witness handling gaps - #2457

Merged
plebhash merged 2 commits into
stratum-mining:mainfrom
Jolah1:fix-coinbase-witness-handling
Nov 3, 2026
Merged

plebhash merged 2 commits into
stratum-mining:mainfrom
Jolah1:fix-coinbase-witness-handling

Conversation

@Jolah1

@Jolah1 Jolah1 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

close #2397
close https://github.com/project-loupe/audit-stratum/issues/240
close https://github.com/project-loupe/audit-stratum/issues/252

Two commits, one per defect in the issue.

try_strip_bip141 now returns the new StripBip141Error::UnexpectedInputCount unless the coinbase has exactly one input, since it only strips the last witness stack. The existing test fixtures gain the input count byte.

The coinbase built by JobFactory and by the client's on_set_custom_mining_job_success only gets the witness reserved value when its outputs carry a witness commitment. Without a witness there's no marker and flag either, so
the prefix/suffix split indexes count them only when present, and the strip sites use bytes with nothing to strip as they are. Standard channels get this through the factory's coinbase since #2445. Prebuilt jobs are left alone.

Tests:

  • bip141: input counts other than one are rejected, including a real two-input SegWit transaction.
  • Server standard and extended channels (pre-mined shares): a block found on a template without a commitment has no coinbase witness.
  • Factory and client custom jobs: the split lands exactly around the extranonce when there's no witness.
  • On Core v29 regtest, such a block was rejected as unexpected-witness on main and is accepted with this branch.

Side effects:

  • Breaking: a new StripBip141Error variant, and the now unused ExtendedChannelError::FailedToStripBip141 is removed. channels_sv2 is already at 9.0.0 vs 8.0.0 on crates.io, and sv2-apps uses neither; I ran sv2-apps' tests
    against this branch locally.
  • Conflicts with fix(channels_sv2)!: bound the client custom job scriptSig #2422 in the client custom job; whichever lands second gets rebased.

Comment thread sv2/channels-sv2/src/bip141.rs Resolved
@Jolah1
Jolah1 force-pushed the fix-coinbase-witness-handling branch from d2a61bc to a9a3b67 Compare November 3, 2026 13:28
@plebhash plebhash added the ready-to-be-merged triggers auto rebase bot label Nov 3, 2026
try_strip_bip141 strips the marker, the flag and the last witness
stack, and only checked that this stack is framed like a coinbase's.
A two-input transaction whose inputs each carry one 32-byte witness
item passed that check: the first input's witness stayed in the
"stripped" suffix and the prefix kept the two-input count, so the
client built jobs from bytes that are not a transaction.

The stripped stack is the whole witness section only when there is a
single input, so read the input count after the marker and flag and
return the new StripBip141Error::UnexpectedInputCount for anything
other than one.

stratum-mining#2397
The job factory and the client custom job always gave the coinbase
input the 32-byte witness reserved value. BIP141 only allows a block
to leave out the witness commitment when none of its transactions has
witness data, so a template without a commitment output turned into
blocks Bitcoin Core rejects as unexpected-witness, from both extended
and standard channels.

Attach the reserved value only when the outputs carry a commitment.
Without a witness the coinbase serializes without the marker and flag,
so the prefix/suffix split indexes count them only when there is one,
and the strip sites use bytes that have nothing to strip as they are.
ExtendedChannelError::FailedToStripBip141 had no other use and goes
away.

Prebuilt jobs are untouched: their bytes come from the application,
which owns their witness.

stratum-mining#2397
@SV2-bot
SV2-bot force-pushed the fix-coinbase-witness-handling branch from a9a3b67 to 7f20b56 Compare November 3, 2026 15:05
@plebhash
plebhash merged commit 59ba5d2 into stratum-mining:main Nov 3, 2026
19 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-be-merged triggers auto rebase bot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

channels_sv2: coinbase witness handling gaps (try_strip_bip141 accepts multi-input transactions; the reserved witness is attached without a commitment)

2 participants