Is there any difference in the structure of a segwit block in comparison to a traditional non-segwit block? A diagram would be helpful if possible.
2 Answers
A segwit block's coinbase transaction must contain an output with a commitment to the witness root hash, see BIP141.
The commitment is recorded in a scriptPubKey of the coinbase transaction. It must be at least 38 bytes, with the first 6-byte of
0x6a24aa21a9ed
, that is:
1-byte - OP_RETURN (0x6a)
1-byte - Push the following 36 bytes (0x24)
4-byte - Commitment header (0xaa21a9ed)
32-byte - Commitment hash: Double-SHA256(witness root hash|witness reserved value)
39th byte onwards: Optional data with no consensus meaning
and the coinbase's input's witness must consist of a single 32-byte array for the witness reserved value.
If there are more than one scriptPubKey matching the pattern, the one with highest output index is assumed to be the commitment.
At the transaction level, a segwit transaction must set the witness flag to 0001, and provide the witnesses before the locktime. A non-segwit transaction contains neither of these properties.
A segwit block differs from a non-segwit block in that it is required to have the witness commitment in an OP_RETURN output of the coinbase transaction. Additionally, the block will contain witness data (duh!) which due to blocksize limit increase to 4,000,000 weight units allows the block to exceed the former blocksize limit of 1 MB.
Segwit introduced the witness transaction id (wtxid) for each transaction in addition to the regular transaction id (txid). The txid does not cover witness data and thus prevents transactions that only use segwit inputs from being subject to third party transaction malleability (see transaction-malleability). The wtxid does cover the signature data contained in the witness and thus is still susceptible. For non-segwit transactions the wtxid equals the txid and they continue to be susceptible.
In order for the block to commit to the witness data of transactions which is not covered by the regular Merkle root in the block header, a second Merkle tree is calculated from the wtxids and committed to in the aforementioned witness commitment in the Coinbase output.
If a block does not contain any segwit transactions, the witness commitment is optional.
- You can read about the witness commitment structure here: BIP0141
You can see the witness commitment of the latest block (558722) here:
via blockstream.info