Skip to content

Commit c4b9d85

Browse files
authored
Merge pull request #153 from plebhash/2025-08-26-bip141-conventions
add BIP141 conventions for serialized coinbase
2 parents f5638e9 + 7251e64 commit c4b9d85

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

03-Protocol-Overview.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,65 @@ Upon receiving the message, the client re-initiates the Noise handshake and uses
286286
For security reasons, it is not possible to reconnect to a server with a certificate signed by a different pool authority key.
287287
The message intentionally does not contain a **pool public key** and thus cannot be used to reconnect to a different pool.
288288
This ensures that an attacker will not be able to redirect hashrate to an arbitrary server should the pool server get compromised and instructed to send reconnects to a new location.
289+
290+
## 3.7 BIP141
291+
292+
[BIP141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki) introduced the notion of Segregated Witness (SegWit) into the Bitcoin protocol.
293+
294+
This deserves some consideration in the Stratum V2 protocol design, mainly because this affects how the Coinbase transaction is serialized across different messages.
295+
296+
For a block that contains any SegWit transactions (in practice almost any non-empty block), the Coinbase transaction MUST have a witness as well as an `OP_RETURN` output carrying the witness commitment. For an empty block, the Coinbase transaction MAY have a witness and the `OP_RETURN` output with the witness commitment anyway.
297+
298+
The `OP_RETURN` output with the witness commitment is provided by Sv2 Template Providers in the `coinbase_tx_outputs` field of `NewTemplate` message.
299+
300+
On a serialized SegWit transaction, the BIP141 fields are:
301+
- marker
302+
- flag
303+
- witness count
304+
- witness length
305+
- witness
306+
307+
The presence or absence of these fields on a serialized Coinbase has important implications on the Stratum V2 protocol. More specifically, the following messages are affected:
308+
- `NewExtendedMiningJob` of Mining Protocol
309+
- `DeclareMiningJob` of Job Declaration Protocol
310+
- `SubmitSolution` of Template Distribution Protocol
311+
- `NewTemplate` of Template Distribution Protocol
312+
313+
### 3.7.1 BIP141 on `NewExtendedMiningJob`
314+
315+
On the Mining Protocol's `NewExtendedMiningJob` there are two fields affected by BIP141:
316+
- `coinbase_tx_prefix`
317+
- `coinbase_tx_suffix`
318+
319+
When concatenated with `extranonce_prefix` + `extranonce`, these fields form a serialized Coinbase transaction that is then hashed to produce a `txid`. Together with `merkle_path`, this `txid`
320+
will then form the `merkle_root` of the Block Header.
321+
322+
In case the Template's Coinbase is a SegWit transaction, BIP141 fields MUST be stripped away from `coinbase_tx_prefix` and `coinbase_tx_suffix`, otherwise clients would be calculating a `merkle_root` with the Coinbase's `wtxid`, which goes against Bitcoin Consensus.
323+
324+
The server MUST retain the original value of those fields so that it can reconstruct the Coinbase as a SegWit transaction whenever it needs to propagate a block.
325+
326+
### 3.7.2 BIP141 on `DeclareMiningJob`
327+
328+
On the Job Declaration Protocol's `DeclareMiningJob` there are two fields affected by BIP141:
329+
- `coinbase_tx_prefix`
330+
- `coinbase_tx_suffix`
331+
332+
Differently from `NewExtendedMiningJob`, in case the Template's Coinbase is a SegWit transaction, BIP141 fields MUST NOT be stripped from `DeclareMiningJob`'s `coinbase_tx_prefix` and `coinbase_tx_suffix`.
333+
334+
That's because JDS needs to be able to reconstruct the Coinbase as a SegWit transaction whenever it needs to propagate a block.
335+
336+
### 3.7.3 BIP141 on `SubmitSolution`
337+
338+
On the Template Distribution Protocol's `SubmitSolution` there is one field affected by BIP141:
339+
- `coinbase_tx`
340+
341+
Differently from `NewExtendedMiningJob`, in case the Template's Coinbase is a SegWit transaction, BIP141 fields MUST NOT be stripped from `SubmitSolution`'s `coinbase_tx`.
342+
343+
That's because the Template Distribution Server would not be able to propagate a block without that data.
344+
345+
### 3.7.4. BIP141 on `NewTemplate`
346+
347+
On the Template Distribution Protocol's `NewTemplate` there is one field affected by BIP141:
348+
- `coinbase_tx_outputs`
349+
350+
In case of blocks containing SegWit transactions (and optionally blocks that don't as well), this field carries the `OP_RETURN` output with the witness commitment. The `witness reserved value` (Coinbase witness) used for calculating this witness commitment is assumed to be 32 bytes of `0x00`, as it currently holds no consensus-critical meaning. This [may change in future soft-forks](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#extensible-commitment-structure).

05-Mining-Protocol.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ A proxy MUST translate the message for all downstream channels belonging to the
427427
- For a **standard channel**: `extranonce_prefix`
428428
- For an **extended channel**: `extranonce_prefix + extranonce (=N bytes)`, where `N` is the negotiated extranonce space for the channel (`OpenMiningChannel.Success.extranonce_size`)
429429

430+
\*If the original coinbase is a SegWit transaction, `coinbase_tx_prefix` and `coinbase_tx_suffix` MUST be stripped of BIP141 fields (marker, flag, witness count, witness length and witness reserved value).
431+
430432
### 5.3.17 `SetNewPrevHash` (Server -> Client, broadcast)
431433

432434
Prevhash is distributed whenever a new block is detected in the network by an upstream node or when a new downstream opens a channel.

06-Job-Declaration-Protocol.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ A request sent by JDC that proposes a selected set of transactions to JDS.
204204
| coinbase_tx_suffix | B0_64K | Serialized bytes representing the final part of the coinbase transaction (after extranonce) |
205205
| tx_ids_list | SEQ0_64K[U256] | List of hashes of the transaction set contained in the template. JDS checks the list against its mempool and requests missing txs via `ProvideMissingTransactions`. Does not include the coinbase transaction (as there is no corresponding full data for it yet). |
206206
| excess_data | B0_64K | Extra data which the Pool may require to validate the work (as defined in the Template Distribution Protocol) |
207+
\*Differently from `NewExtendedMiningJob`, if the original coinbase is a SegWit transaction, `coinbase_tx_prefix` and `coinbase_tx_suffix` MUST NOT be stripped of BIP141 fields (marker, flag, witness count, witness length and witness reserved value).
208+
207209

208210
### 6.4.5 `DeclareMiningJob.Success` (Server -> Client)
209211

07-Template-Distribution-Protocol.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ The primary template-providing function. Note that the `coinbase_tx_outputs` byt
4747

4848
Please note that differently from `SetCustomMiningJob.coinbase_tx_outputs` and `AllocateMiningJobToken.Success.coinbase_tx_outputs`, `NewTemplate.coinbase_tx_outputs` MUST NOT be serialized as a CompactSize-prefixed array. This field must simply carry the ordered sequence of consensus‑serialized outputs, but the number of outputs MUST be inferred from `NewTemplate.coinbase_tx_outputs_count`. This is the equivalent of taking a CompactSize-prefixed array and dropping its (outer) prefix.
4949

50+
Please also note that in case the block contains SegWit transactions (and optionally blocks that don't as well), `NewTemplate.coinbase_tx_outputs` MUST carry the witness commitment. The `witness reserved value` (Coinbase witness) used for calculating this witness commitment is assumed to be 32 bytes of `0x00`, as it currently holds no consensus-critical meaning. This [may change in future soft-forks](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#extensible-commitment-structure).
51+
5052
## 7.3 `SetNewPrevHash` (Server -> Client)
5153

5254
Upon successful validation of a new best block, the server MUST immediately provide a `SetNewPrevHash` message.
@@ -118,3 +120,5 @@ Upon finding a coinbase transaction/nonce pair which double-SHA256 hashes at or
118120
| header_timestamp | U32 | The nTime field in the block header. This MUST be greater than or equal to the header_timestamp field in the latest SetNewPrevHash message and lower than or equal to that value plus the number of seconds since the receipt of that message. |
119121
| header_nonce | U32 | The nonce field in the header |
120122
| coinbase_tx | B0_64K | The full serialized coinbase transaction, meeting all the requirements of the NewTemplate message, above |
123+
124+
\*Differently from `NewExtendedMiningJob`, if the original coinbase is a SegWit transaction, `coinbase_tx` MUST NOT be stripped of BIP141 fields (marker, flag, witness count, witness length and witness reserved value).

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ This repository contains the Stratum V2 protocol specification.
1919
- [3.6.3. `SetupConnection.Error` (Server -> Client)](./03-Protocol-Overview.md#363-setupconnectionerror-server---client)
2020
- [3.6.4. `ChannelEndpointChanged` (Server -> Client)](./03-Protocol-Overview.md#364-channelendpointchanged-server---client)
2121
- [3.6.5. `Reconnect` (Server -> Client)](./03-Protocol-Overview.md#365-reconnect-server---client)
22+
- [3.7. BIP141](./03-Protocol-Overview.md#37-bip141)
23+
- [3.7.1. BIP141 on `NewExtendedMiningJob`](./03-Protocol-Overview.md#371-bip141-on-newextendedminingjob)
24+
- [3.7.2. BIP141 on `DeclareMiningJob`](./03-Protocol-Overview.md#372-bip141-on-declareminingjob)
25+
- [3.7.3. BIP141 on `SubmitSolution`](./03-Protocol-Overview.md#373-bip141-on-submitsolution)
26+
- [3.7.4. BIP141 on `NewTemplate`](./03-Protocol-Overview.md#374-bip141-on-newtemplate)
2227
- [4. Protocol Security](./04-Protocol-Security.md#4-protocol-security)
2328
- [4.1. Motivation for Authenticated Encryption with Associated Data](./04-Protocol-Security.md#41-motivation-for-authenticated-encryption-with-associated-data)
2429
- [4.2. Motivation for Using the Noise Protocol Framework](./04-Protocol-Security.md#42-motivation-for-using-the-noise-protocol-framework)

0 commit comments

Comments
 (0)