Skip to content

Commit ab3024b

Browse files
committed
Sv2NewTemplate: add coinbase_witness
Adding coinbase_witness to the Sv2NewTemplate message so that it is automatically propagated to other roles in the Stratum v2 ecosystem, rather than assumed to be 0x00...00. This ensures that if this value ever gets consensus meaning, miners only need to upgrade their node software, not the rest of the mining stack. This is a breaking change requiring an updated Stratum v2 spec as well as support on the SRI side.
1 parent d2e3b42 commit ab3024b

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

src/sv2/messages.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ node::Sv2NewTemplateMsg::Sv2NewTemplateMsg(const CBlockHeader& header, const CTr
1111

1212
m_coinbase_tx_version = coinbase_tx->CURRENT_VERSION;
1313
m_coinbase_prefix = coinbase_tx->vin[0].scriptSig;
14+
if (coinbase_tx->HasWitness()) {
15+
const auto& witness_stack{coinbase_tx->vin[0].scriptWitness.stack};
16+
Assert(witness_stack.size() == 1 || witness_stack[0].size() == 32);
17+
m_coinbase_witness = uint256(witness_stack[0]);
18+
} else {
19+
m_coinbase_witness = uint256(0);
20+
}
1421
m_coinbase_tx_input_sequence = coinbase_tx->vin[0].nSequence;
1522

1623
// The coinbase nValue already contains the nFee + the Block Subsidy when built using CreateBlock().

src/sv2/messages.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,18 @@ struct Sv2NewTemplateMsg
282282
*/
283283
CScript m_coinbase_prefix;
284284

285+
/**
286+
* 32 byte array of the first (and only) witness stack element of the coinbase.
287+
*
288+
* If there is no segwit commitment in m_coinbase_tx_outputs this value
289+
* must be ignored.
290+
*
291+
* This is the BIP 141 witness reserved value. A future soft fork may move
292+
* the witness reserved value elsewhere. In that case this field still
293+
* represents the coinbase witness, for backward compatibility.
294+
*/
295+
uint256 m_coinbase_witness;
296+
285297
/**
286298
* The coinbase transaction input’s nSequence field.
287299
*/
@@ -324,6 +336,7 @@ struct Sv2NewTemplateMsg
324336
<< m_version
325337
<< m_coinbase_tx_version
326338
<< m_coinbase_prefix
339+
<< m_coinbase_witness
327340
<< m_coinbase_tx_input_sequence
328341
<< m_coinbase_tx_value_remaining
329342
<< m_coinbase_tx_outputs_count;

src/test/sv2_messages_tests.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ BOOST_AUTO_TEST_CASE(Sv2NewTemplate_test)
110110
// U32 02000000 coinbase tx version
111111
// B0_255 04 coinbase_prefix len
112112
// 03012100 coinbase prefix
113+
// U256 0000000000000000000000000000000000000000000000000000000000000000 - witness reserved value
113114
// U32 ffffffff coinbase tx input sequence
114115
// U64 0040075af0750700 coinbase tx value remaining
115116
// U32 01000000 coinbase tx outputs count
@@ -119,7 +120,7 @@ BOOST_AUTO_TEST_CASE(Sv2NewTemplate_test)
119120
// U32 dbc80d00 coinbase lock time (height 903,387)
120121
// SEQ0_255[U256] 01 merkle path length
121122
// 1a6240823de4c8d6aaf826851bdf2b0e8d5acf7c31e8578cff4c394b5a32bd4e - merkle path
122-
std::string expected{"01000000000000000000000030020000000403012100ffffffff0040075af0750700010000000c000100000000000000036a012adbc80d00011a6240823de4c8d6aaf826851bdf2b0e8d5acf7c31e8578cff4c394b5a32bd4e"};
123+
std::string expected{"010000000000000000000000300200000004030121000000000000000000000000000000000000000000000000000000000000000000ffffffff0040075af0750700010000000c000100000000000000036a012adbc80d00011a6240823de4c8d6aaf826851bdf2b0e8d5acf7c31e8578cff4c394b5a32bd4e"};
123124

124125
node::Sv2NewTemplateMsg new_template;
125126
new_template.m_template_id = 1;
@@ -131,6 +132,8 @@ BOOST_AUTO_TEST_CASE(Sv2NewTemplate_test)
131132
CScript prefix(coinbase_prefix.begin(), coinbase_prefix.end());
132133
new_template.m_coinbase_prefix = prefix;
133134

135+
new_template.m_coinbase_witness = uint256(0);
136+
134137
new_template.m_coinbase_tx_input_sequence = 4294967295;
135138
new_template.m_coinbase_tx_value_remaining = MAX_MONEY;
136139

src/test/sv2_template_provider_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
BOOST_FIXTURE_TEST_SUITE(sv2_template_provider_tests, TestChain100Setup)
2020

21-
static constexpr size_t SV2_NEW_TEMPLATE_MESSAGE_SIZE{91};
21+
static constexpr size_t SV2_NEW_TEMPLATE_MESSAGE_SIZE{91 + 32};
2222

2323
/**
2424
* A class for testing the Template Provider. Each TPTester encapsulates a

0 commit comments

Comments
 (0)