Skip to content

sv2: don't read the coinbase in RequestTransactionData - #138

Closed
xyephy wants to merge 2 commits into
stratum-mining:masterfrom
xyephy:2026/10/submit-solution-coinbase-check
Closed

xyephy wants to merge 2 commits into
stratum-mining:masterfrom
xyephy:2026/10/submit-solution-coinbase-check

Conversation

@xyephy

@xyephy xyephy commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

After a SubmitSolution, Bitcoin Core stores the submitted coinbase in the template, even if it rejects the block. RequestTransactionData then read vin[0] of that coinbase without checking that it has an input, so a coinbase with no inputs led to an out-of-bounds read and crashed sv2-tp.

  • Let the mock store a submitted coinbase like Bitcoin Core, with a tester helper to send it
  • Stop reading the coinbase in RequestTransactionData and send excess_data empty, like bitcoin_core_sv2 in sv2-apps. The coinbase was already excluded from transaction_list. The solution is still forwarded, as in Never drop SubmitSolution in the network layer  #120.

The new regression test crashes on master and passes with the fix. Also ran on signet with jd-client and the SRI pool's JDS: every RequestTransactionData succeeded and the JDS accepted every declared job.

@Sjors

Sjors commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Wouldn't it be easier to disallow the coinbase for RequestTransactionData?

The spec requires excluding it: https://stratumprotocol.org/specification/07-template-distribution-protocol/#75-requesttransactiondata-client---server

@xyephy

xyephy commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Wouldn't it be easier to disallow the coinbase for RequestTransactionData?

Yes, that's easier. The coinbase is already left out of transaction_list, and the only coinbase data RequestTransactionData still sends is the witness reserved value in excess_data. I'll drop that and send excess_data empty, so it never reads the coinbase.

@xyephy
xyephy force-pushed the 2026/10/submit-solution-coinbase-check branch from bdc1ee6 to 28a2c2f Compare October 14, 2026 05:30
@xyephy xyephy changed the title sv2: don't assume the coinbase has an input in RequestTransactionData sv2: don't read the coinbase in RequestTransactionData Oct 14, 2026
@xyephy
xyephy force-pushed the 2026/10/submit-solution-coinbase-check branch from 28a2c2f to 7e83533 Compare October 14, 2026 05:41
@xyephy

xyephy commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

@Sjors pushed updates and rebased on master to pick up #139.

@Sjors

Sjors commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Needs another rebase, probably because I merged #136.

Bitcoin Core's submitSolution() stores the submitted coinbase in the
template whether or not the block is accepted. Add a mock switch that
does the same, and a tester helper to send SubmitSolution with a given
coinbase.
After a SubmitSolution the template holds the client's coinbase, which
may have no inputs. Stop reading its witness and send excess_data
empty, like bitcoin_core_sv2 in sv2-apps. The coinbase was already
excluded from transaction_list.

Add a regression test.
@xyephy
xyephy force-pushed the 2026/10/submit-solution-coinbase-check branch from 7e83533 to 87b3307 Compare October 14, 2026 12:24
@xyephy

xyephy commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on top of #136.

@Sjors Sjors left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this a bit more:

  1. I think it's better to check if the coinbase transaction is present and we can obtain its witness. Only if not, set excess data to empty.

  2. We should rethink the excess data field in the spec, there's been discussion about that before.

//! Make submitSolution() store the submitted coinbase in the template, like
//! Bitcoin Core does (AddMerkleRootAndCoinbase) whether or not the block is
//! accepted.
std::atomic<bool> store_submitted_coinbase{false};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even need a switch here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Core stores it unconditionally, so the switch only hid that from tests. One difference to keep in mind: the mock's getCoinbaseTx() is derived from block.vtx[0], so with an always-store mock it follows the submitted coinbase, while Core's returns the unchanged m_coinbase_tx. Nothing calls it after a submit today.

@Sjors

Sjors commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

Let's await the discussion in stratum-mining/sv2-spec#241. I'll open a PR to implement that approach, but you can leave this open.

@Sjors Sjors mentioned this pull request Oct 15, 2026
@xyephy

xyephy commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Let's await the discussion in stratum-mining/sv2-spec#241. I'll open a PR to implement that approach, but you can leave this open.

Sounds good, happy to wait for sv2-spec#241.

If #241 doesn't go through, it might be better to take the witness from getCoinbaseTx() rather than getBlock(). The coinbase in getBlock() "should not be used" (interfaces/mining.h), and after a SubmitSolution it's the client's. getCoinbaseTx().witness is the value the commitment was built from, and submitSolution() doesn't change it. I tried that locally, with the mock keeping its own m_coinbase_tx like Core: the crash test passes, and on signet excess_data stays the 32-byte witness with every declared job accepted by the SRI JDS.

That would also fix the crash now without changing what goes on the wire, in case you'd rather not have it wait on the spec discussion. Happy to push that here, or keep this open as is, will go with one you prefer.

@Sjors

Sjors commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

it might be better to take the witness from getCoinbaseTx() rather than getBlock()

That's a good idea.

in case you'd rather not have it wait on the spec discussion.

Let's give it a week or so. Since SRI leave the field empty, we could do #141 anyway, but I prefer to change the spec.

Sjors added a commit to Sjors/sv2-tp that referenced this pull request Oct 16, 2026
Submit an empty coinbase over the SV2 connection, then request transaction
data for the same template. Check that the provider responds successfully
with empty excess_data and no transactions instead of reading vin[0].

Adapt the regression test and SendSubmitSolutionCoinbase helper from stratum-mining#138,
omitting the mock switch because submitted coinbases are already retained
unconditionally.

Co-authored-by: Толли <ziphyr.xyeag@gmail.com>
Sjors added a commit to Sjors/sv2-tp that referenced this pull request Oct 29, 2026
No excess data is currently defined, and the coinbase witness does not
belong in this field. Always send empty excess_data, as proposed in:
stratum-mining/sv2-spec#241

This also avoids reading the coinbase after a submitted solution has
replaced it with a transaction that may have no inputs, fixing the crash
described in stratum-mining#138.
Sjors added a commit to Sjors/sv2-tp that referenced this pull request Oct 29, 2026
Submit an empty coinbase over the SV2 connection, then request transaction
data for the same template. Check that the provider responds successfully
with empty excess_data and no transactions instead of reading vin[0].

Adapt the regression test and SendSubmitSolutionCoinbase helper from stratum-mining#138,
omitting the mock switch because submitted coinbases are already retained
unconditionally.

Co-authored-by: Толли <ziphyr.xyeag@gmail.com>
Sjors added a commit that referenced this pull request Nov 11, 2026
a32129b test: cover transaction requests after an inputless coinbase (Sjors Provoost)
6fa70e6 sv2: leave transaction response excess_data empty (Sjors Provoost)
4d6920d test: retain submitted coinbases in the mining mock (Sjors Provoost)

Pull request description:

  Depends on:
  - stratum-mining/sv2-spec#241

  The SRI version already does this.

  Alternative to #138.

ACKs for top commit:
  xyephy:
    tACK a32129b

Tree-SHA512: 1b788dda5e490f4d9a75ff341395ff6b45013f87aa5a0600fb2dbef1e0bb35af9dfdd0c6872e57d0f3ea374c38db3e519e0c3b84843e49062a42a56786444052
@Sjors

Sjors commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

I ended up merging #141, so closing this.

@Sjors Sjors closed this Nov 11, 2026
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.

2 participants