-
Notifications
You must be signed in to change notification settings - Fork 41
add BIP141 conventions for serialized coinbase #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -286,3 +286,65 @@ Upon receiving the message, the client re-initiates the Noise handshake and uses | |
| For security reasons, it is not possible to reconnect to a server with a certificate signed by a different pool authority key. | ||
| The message intentionally does not contain a **pool public key** and thus cannot be used to reconnect to a different pool. | ||
| 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. | ||
|
|
||
| ## 3.7 BIP141 | ||
|
|
||
| [BIP141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki) introduced the notion of Segregated Witness (SegWit) into the Bitcoin protocol. | ||
|
|
||
| This deserves some consideration in the Stratum V2 protocol design, mainly because this affects how the Coinbase transaction is serialized across different messages. | ||
|
|
||
| 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. | ||
|
|
||
| The `OP_RETURN` output with the witness commitment is provided by Sv2 Template Providers in the `coinbase_tx_outputs` field of `NewTemplate` message. | ||
|
|
||
| On a serialized SegWit transaction, the BIP141 fields are: | ||
| - marker | ||
| - flag | ||
| - witness count | ||
| - witness length | ||
| - witness | ||
|
|
||
| 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: | ||
| - `NewExtendedMiningJob` of Mining Protocol | ||
| - `DeclareMiningJob` of Job Declaration Protocol | ||
| - `SubmitSolution` of Template Distribution Protocol | ||
| - `NewTemplate` of Template Distribution Protocol | ||
|
|
||
| ### 3.7.1 BIP141 on `NewExtendedMiningJob` | ||
|
|
||
| On the Mining Protocol's `NewExtendedMiningJob` there are two fields affected by BIP141: | ||
| - `coinbase_tx_prefix` | ||
| - `coinbase_tx_suffix` | ||
|
|
||
| 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` | ||
| will then form the `merkle_root` of the Block Header. | ||
|
|
||
| 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. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So IIUC
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, first some recap on Sv2 Standard vs Extended Jobs: Standard Jobs have a Extended Jobs are closer to Sv1, in the sense that there's a
AFAIK most Sv1 servers send their Until recently, SRI was sending and expecting
We could leave it up for the client to figure out whether I agree with you that it's a bit confusing to follow one pattern for one message and a different pattern in other messages. @Fi3 also pointed this out here, which motivated the addition of the new BIP141 section into Overall, when it comes to But I'm strongly opinionated that it's of upmost importance to have clear (even if somewhat counter-intuitive) conventions on the spec, which is why I'm pushing for these discussions. Not having anything is a recipe for interoperability hell, as we already started to see with stratum-mining/stratum#1399
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should have a clear distinction between Stratum v2 and Stratum v1, with the Translator role taking care of serialisation changes.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in fairness, the receiving end of so one could argue that forcing as much as I value clarity and cleanness in the spec, this feels like a reasonable tradeoff?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I generally think that a tiny increase in message size is worth the reduction in complexity, unless it you're at the edge of some network packet size and want to prevent roundtrips. But for the purpose of this PR it may be better to just document things as they are now. |
||
|
|
||
| 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. | ||
|
|
||
| ### 3.7.2 BIP141 on `DeclareMiningJob` | ||
|
|
||
| On the Job Declaration Protocol's `DeclareMiningJob` there are two fields affected by BIP141: | ||
| - `coinbase_tx_prefix` | ||
| - `coinbase_tx_suffix` | ||
|
|
||
| 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`. | ||
|
|
||
| That's because JDS needs to be able to reconstruct the Coinbase as a SegWit transaction whenever it needs to propagate a block. | ||
|
|
||
| ### 3.7.3 BIP141 on `SubmitSolution` | ||
|
|
||
| On the Template Distribution Protocol's `SubmitSolution` there is one field affected by BIP141: | ||
| - `coinbase_tx` | ||
|
|
||
| Differently from `NewExtendedMiningJob`, in case the Template's Coinbase is a SegWit transaction, BIP141 fields MUST NOT be stripped from `SubmitSolution`'s `coinbase_tx`. | ||
|
|
||
| That's because the Template Distribution Server would not be able to propagate a block without that data. | ||
|
|
||
| ### 3.7.4. BIP141 on `NewTemplate` | ||
|
|
||
| On the Template Distribution Protocol's `NewTemplate` there is one field affected by BIP141: | ||
| - `coinbase_tx_outputs` | ||
|
|
||
| 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). | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So maybe add:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two sentences are conflicting.
TP provides an
OP_RETURNwith a commitment hash that's calculated with a Coinbase witness as part of its pre-image. So whoever is downstream to TP is pretty much forced to abide by the Coinbase witness that TP chose, despite the fact that this is not really communicated anywhere on Sv2 TDP.Unfortunately, there is a
NewTemplate.coinbase_tx_outputs(whereOP_RETURNwith witness commitments is sent), but there's noNewTemplate.coinbase_witnessor alike.As expected, our current TP implementation does it with a
0x00..00witness.I validated this empirically, by trying to propagate a block with a witness commitment sent by our current TP and a Coinbase witness different than
0x00..00. The block was rejected by its peers.So either:
OP_RETURNwith the witness commitment, leaving it for the receiving end to decide which Coinbase witness they want to use and calculate a witness commitment accordinglyOR
0x00..00until some soft-fork changes it into consensus-criticalOption A would pretty much render
NewTemplate.coinbase_tx_outputsuseless. Or at least, AFAIK, sending Witness commitment is the only meaningful purpose for this field.Option B would force a spec upgrade if-and-when a soft-fork changes things with regards to Coinbase's witness reserved value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be a disaster, because it's hard enough to get miners to upgrade their node software, let alone their entire sv2 stack.
It sounds like closing #15 was a mistake?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused about what you mean here. You're referring to the TP I implemented (
sv2-tpand itsbitcoind -sv2predecessor), right? This lets Bitcoin Core generate a block template. The coinbase it uses for that is never communicated, but it indeed uses0x00..00and theOP_RETURNvalue reflects that.For
SubmitSolutionthe coinbase has to be reconstructed by other SRI roles. The Template Provider just passes it along. Are you saying that if these other roles pick a different value than0x00..00then the result is broken? That would make sense.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to my understanding, yes. Or at least as far as we expect:
When it comes to who's responsible for calculating the Witness Commitment, an alternative option would be to shift this responsibility to the Sv2 role who receives
NewTemplate. The tradeoffs would be:RequestTransactionData/.Successround-trip would be necessarywitness reserved valueon this hypothetical soft fork... does the Sv2 role have all the necessary data to make that decision, or is that something that only a Bitcoin Full Node could do?So having TP be responsible for calculating the Witness Commitment feels like a sensible design decision.
But as long as
NewTemplatecarries no information about Coinbase witness (which was proposed and rejected on #15), the Sv2 role that receives this message and wants to propagate a block later has no option but to ASSUME a value for Coinbase witness. And today we live with this "implied convention" of using0x000...000, which is an intuitively sensible choice (but really could have been anything, given that this field is not consensus-critical).It's kinda sad to realize that the protocol is not future-proof on this regard, especially given that this could have been solved with just one extra field on
NewTemplate, as #15 proposed.However, I must also point out that the purpose of this current PR is not to advocate for spec changes, but rather to simply establish conventions that avoids confusion and enables smooth interoperability with what the Bitcoin protocol consensus looks like today. So I'm pretty ok with saying "just use
0x000...000forwitness reserved value" as an official convention and leaving future/hypothetical problems for the future.In case protocol stakeholders (@jakubtrnka @Fi3 @TheBlueMatt) feel this deserves a discussion for spec change, we can do it on a separate issue/PR.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a few words suggested by @Sjors #153 (comment)
but not all
that's because I feel this topic about future implications of Coinbase witness is derailing into a discussion on its own, and as stated above I'd like to keep the scope of this PR constrained to conventions around serialization without diving into potential spec changes
so I crated #156 as a place where we can discuss about it more freely
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do think it's worth mentioning that the witness commitment is (currently) assumed to be zero.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you mean the Coinbase witness (currently aka witness reserved value) is assumed to be zero, right?
the witness commitment is
0x6a24aa21a9edfollowed by the hash of the concatenation of witness root and witness reserved value, so we don't really expect it to be zero.anyways, just added a commit for this:
7251e64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes