Skip to content

Commit 5ec9e09

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 6ce96cc commit 5ec9e09

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/sv2/messages.cpp

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

1414
m_coinbase_tx_version = coinbase_tx->CURRENT_VERSION;
1515
m_coinbase_prefix = coinbase_tx->vin[0].scriptSig;
16+
if (coinbase_tx->HasWitness()) {
17+
const auto& witness_stack{coinbase_tx->vin[0].scriptWitness.stack};
18+
Assert(witness_stack.size() == 1 || witness_stack[0].size() == 32);
19+
m_coinbase_witness = uint256(witness_stack[0]);
20+
} else {
21+
m_coinbase_witness = uint256(0);
22+
}
1623
m_coinbase_tx_input_sequence = coinbase_tx->vin[0].nSequence;
1724

1825
// 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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ BOOST_AUTO_TEST_CASE(Sv2NewTemplate_test)
113113
// U32 02000000 coinbase tx version
114114
// B0_255 04 coinbase_prefix len
115115
// 03012100 coinbase prefix
116+
// U256 0000000000000000000000000000000000000000000000000000000000000000 - witness
116117
// U32 ffffffff coinbase tx input sequence
117118
// U64 0040075af0750700 coinbase tx value remaining
118119
// U32 01000000 coinbase tx outputs count
@@ -122,7 +123,7 @@ BOOST_AUTO_TEST_CASE(Sv2NewTemplate_test)
122123
// U32 dbc80d00 coinbase lock time (height 903,387)
123124
// SEQ0_255[U256] 01 merkle path length
124125
// 1a6240823de4c8d6aaf826851bdf2b0e8d5acf7c31e8578cff4c394b5a32bd4e - merkle path
125-
std::string expected{"01000000000000000000000030020000000403012100ffffffff0040075af0750700010000000c000100000000000000036a012adbc80d00011a6240823de4c8d6aaf826851bdf2b0e8d5acf7c31e8578cff4c394b5a32bd4e"};
126+
std::string expected{"010000000000000000000000300200000004030121000000000000000000000000000000000000000000000000000000000000000000ffffffff0040075af0750700010000000c000100000000000000036a012adbc80d00011a6240823de4c8d6aaf826851bdf2b0e8d5acf7c31e8578cff4c394b5a32bd4e"};
126127

127128
node::Sv2NewTemplateMsg new_template;
128129
new_template.m_template_id = 1;
@@ -134,6 +135,8 @@ BOOST_AUTO_TEST_CASE(Sv2NewTemplate_test)
134135
CScript prefix(coinbase_prefix.begin(), coinbase_prefix.end());
135136
new_template.m_coinbase_prefix = prefix;
136137

138+
new_template.m_coinbase_witness = uint256(0);
139+
137140
new_template.m_coinbase_tx_input_sequence = 4294967295;
138141
new_template.m_coinbase_tx_value_remaining = MAX_MONEY;
139142

@@ -167,6 +170,7 @@ BOOST_AUTO_TEST_CASE(Sv2NewTemplate_MultipleOutputs_test)
167170
// U32 02000000 coinbase tx version
168171
// B0_255 04 coinbase_prefix len
169172
// 03012100 coinbase prefix
173+
// U256 0000000000000000000000000000000000000000000000000000000000000000 - witness
170174
// U32 ffffffff coinbase tx input sequence
171175
// U64 0040075af0750700 coinbase tx value remaining
172176
// U32 03000000 coinbase tx outputs count (3 OP_RETURN outputs, dummy filtered)
@@ -180,7 +184,7 @@ BOOST_AUTO_TEST_CASE(Sv2NewTemplate_MultipleOutputs_test)
180184
// U32 dbc80d00 coinbase lock time (height 903,387)
181185
// SEQ0_255[U256] 01 merkle path length
182186
// 1a6240823de4c8d6aaf826851bdf2b0e8d5acf7c31e8578cff4c394b5a32bd4e - merkle path
183-
std::string expected{"01000000000000000000000030020000000403012100ffffffff0040075af07507000300000021006400000000000000026a51c800000000000000026a522c01000000000000026a53dbc80d00011a6240823de4c8d6aaf826851bdf2b0e8d5acf7c31e8578cff4c394b5a32bd4e"};
187+
std::string expected{"010000000000000000000000300200000004030121000000000000000000000000000000000000000000000000000000000000000000ffffffff0040075af07507000300000021006400000000000000026a51c800000000000000026a522c01000000000000026a53dbc80d00011a6240823de4c8d6aaf826851bdf2b0e8d5acf7c31e8578cff4c394b5a32bd4e"};
184188

185189
// Create realistic coinbase transaction with dummy anyone-can-spend output
186190
CMutableTransaction coinbase_tx;

src/test/sv2_template_provider_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ BOOST_AUTO_TEST_CASE(client_tests)
6363
4 + // version
6464
4 + // coinbase_tx_version
6565
2 + // coinbase_prefix (CompactSize(1) + 1-byte OP_0)
66+
32 + // coinbase_witness (fixed-size reserved value)
6667
4 + // coinbase_tx_input_sequence
6768
8 + // coinbase_tx_value_remaining
6869
4 + // coinbase_tx_outputs_count (2 - mock creates 3, only 2 OP_RETURN outputs pass filter)

0 commit comments

Comments
 (0)