4 Data Encoding and Structures¶
Every object that the consensus rules talk about, a transaction, a block header, a block, is exchanged and hashed as a byte string. This chapter specifies those byte strings exactly: the primitive encodings they are built from, the layout of transactions in each of their three serialization types, the 180-byte block header, the block, and the identifiers (hashes) derived from each of them. It also specifies what the two Merkle-root fields of the header contain, before and after the change made by DCP-0005.
The generic Merkle tree construction and the hash functions themselves belong to Chapter 3. Scripts carried inside transactions are opaque byte strings here and are interpreted in Chapter 6. The rules that decide whether a decoded transaction or block is valid are in Chapter 7 and Chapter 10. The framing of these structures inside peer-to-peer messages is in Chapter 15.
4.1 Decoding Principles¶
All structures are concatenations of fields with no padding, no alignment and no field tags. Integers are little-endian (Section 2.4) unless stated otherwise. A structure carries no overall length prefix: its extent is determined by decoding it field by field from its first byte.
dcrd computes every identifier and every size from the decoded structure, by serializing it again, never from the bytes it received. The encoding is designed so that this makes no difference: the decoder accepts only canonical encodings, so every byte string that decodes successfully re-encodes to exactly the same byte string, and two different accepted byte strings never decode to the same structure. An implementation may therefore hash either the received bytes or a re-serialization, provided it rejects everything that dcrd’s decoder rejects. The rules in this chapter that use the word “undecodable” list every such rejection.
Rule 4.1 · Decodability
A block MUST be decodable according to Section 4.6, and every transaction it contains MUST be decodable according to Section 4.3.7. A byte string that is undecodable under any rule of this chapter is not a block (or transaction) at all, and no block can be built on it. In particular, a structure that ends before all of its fields have been read is undecodable.dcrd: wire/msgblock.go · MsgBlock.BtcDecode | wire/msgtx.go · MsgTx.BtcDecode
The serialization used on the wire is the same as the one dcrd uses for storage and for hashing. dcrd’s encoder and decoder accept a protocol version argument; the only effect of that argument on any structure in this chapter is the transaction-count bound of Rule 4.10, which never decides the validity of a block (see the note after that rule).
4.2 Primitive Encodings¶
4.2.1 Fixed-Width Integers¶
Fixed-width integers occupy exactly their width in bytes and are serialized least significant byte first. Signed integers are serialized as the two’s complement bit pattern of the same width, so an int64 field is written as le64(uint64(x)) and read back as int64(read_le64()). The decoder places no range restriction on any integer field: every bit pattern decodes, and range checks (for example on amounts) are validation rules stated elsewhere.
| Type | Bytes | Encoding |
|---|---|---|
uint8 | 1 | The byte itself. |
uint16 | 2 | le16(x) |
uint32 | 4 | le32(x) |
int32 | 4 | le32(uint32(x)) |
uint64 | 8 | le64(x) |
int64 | 8 | le64(uint64(x)) |
[n]byte | The bytes in order, unaltered. Used for hashes (), the header’s final state () and extra data (). |
4.2.2 Variable-Length Integers¶
Counts and lengths are encoded as variable-length integers (varints), the same scheme as Bitcoin’s “CompactSize”. The first byte, the discriminant, either is the value itself or announces a 2-, 4- or 8-byte little-endian value that follows. The scheme can represent every uint64.
| Value | Bytes | Discriminant | Encoding |
|---|---|---|---|
| 1 | u8(v) | ||
| 3 | 0xfd | 0xfd || le16(v) | |
| 5 | 0xfe | 0xfe || le32(v) | |
| 9 | 0xff | 0xff || le64(v) |
The same value could be written in more than one of these forms, for example as 05 or as fd0500. Only the shortest form is accepted.
Rule 4.2 · Canonical variable-length integers
A varint is undecodable if its value could have been encoded in fewer bytes. Precisely, after reading the discriminant and the value that follows it:
- if and (that is, ), reject;
- if and , reject;
- if and , reject.
A discriminant below 0xfd is always canonical. A varint whose following 2, 4 or 8 bytes are not all present is undecodable (Rule 4.1).
dcrd: wire/common.go · ReadVarInt
Algorithm 4.1 · Variable-length integer encoding and decoding
def encode_varint(v: uint64) -> bytes:
if v < 0xfd:
return u8(v)
if v <= 0xffff:
return u8(0xfd) + le16(v)
if v <= 0xffffffff:
return u8(0xfe) + le32(v)
return u8(0xff) + le64(v)
def decode_varint(r) -> uint64:
# r.read_*() reject("truncated") when too few bytes remain
d = r.read_u8()
if d == 0xff:
v = r.read_le64()
if v < 0x100000000:
reject("non-canonical varint")
elif d == 0xfe:
v = r.read_le32()
if v < 0x10000:
reject("non-canonical varint")
elif d == 0xfd:
v = r.read_le16()
if v < 0xfd:
reject("non-canonical varint")
else:
v = d
return v
def varint_size(v: uint64) -> int:
return 1 if v < 0xfd else 3 if v <= 0xffff else 5 if v <= 0xffffffff else 9dcrd: wire/common.go · WriteVarInt | wire/common.go · ReadVarInt | wire/common.go · VarIntSerializeSize
Note
DCP-0005 adopts the same encoding, with the same canonical requirement, for the item count of compact block filters (Chapter 16).4.2.3 Variable-Length Byte Strings¶
A variable-length byte string (varbytes) is a varint length followed by bytes. Scripts are the only varbytes in consensus structures. Each use site imposes a maximum on , checked after the varint has been decoded and before the bytes are read; a length above the maximum makes the structure undecodable even if the input would contain that many bytes. The maxima for scripts are given in Rule 4.7.
A variable-length string has the same encoding with the bytes interpreted as text. Strings occur only in peer-to-peer messages (Chapter 15); dcrd bounds their length by MaxMessagePayload and, for some fields, additionally requires printable ASCII (0x20 to 0x7e).
4.2.4 Hashes¶
A hash is serialized as its 32 bytes in the order the hash function produced them, with no length prefix. All hashes in consensus structures are BLAKE-256 digests (Chapter 3), written below, with the single exception of the proof-of-work hash of Section 4.4.2. When a hash is shown to people it is displayed with its bytes reversed, as described in Section 2.4. Every byte string in this chapter is in serialized (internal) order unless it is explicitly labelled as display form.
4.3 Transactions¶
A Decred transaction is split into two parts. The prefix holds everything that determines what the transaction does: the outpoints it spends, the outputs it creates, its lock time and its expiry. The witness holds, for each input, the signature script that authorizes the spend together with three fields of fraud-proof data (the amount, block height and block index of the output being spent). The transaction’s identifier is the hash of the prefix alone, so altering a signature script, which third parties can sometimes do without invalidating it, cannot change the identifier that later transactions use to refer to it.
A transaction can be serialized in three ways: in full (prefix followed by witness), as the prefix alone, or as the witness alone. Which one is used is recorded in the transaction’s first four bytes.
| Field | Type | Meaning |
|---|---|---|
version | uint16 | Transaction version (Table 4.5). |
ser_type | uint16 | Serialization type the transaction was or will be encoded with (Table 4.4). |
inputs | list of TxIn | Each with prefix fields and witness fields (Table 4.8, Table 4.9). |
outputs | list of TxOut | Table 4.11. |
lock_time | uint32 | Earliest height or time at which the transaction may be included (Chapter 7). |
expiry | uint32 | Height from which the transaction may no longer be included; 0 means no expiry (Chapter 7). |
4.3.1 The Version Word and Serialization Types¶
Every serialized transaction begins with a 32-bit little-endian version word. Its low 16 bits are the transaction version; its high 16 bits are the serialization type:
(4.1)
ser_type | dcrd name | Body after the version word | Use |
|---|---|---|---|
0 | TxSerializeFull | prefix body, then witness body | The normal encoding: in blocks, in tx messages, in storage. |
1 | TxSerializeNoWitness | prefix body only | Input to the transaction hash (Section 4.3.8). |
2 | TxSerializeOnlyWitness | witness body only | Input to the witness hash (Section 4.3.8). |
Rule 4.3 · Serialization type
A transaction whose serialization type (the high 16 bits of the version word) is not0, 1 or 2 is undecodable.dcrd: wire/msgtx.go · MsgTx.BtcDecode
The version (low 16 bits) is not restricted by the decoder. The versions with a defined meaning are listed below; which versions are permitted in which context is a validation rule (Chapter 7, Chapter 8, Chapter 12).
4.3.2 Outpoints¶
An outpoint identifies one output of an earlier transaction. Unlike Bitcoin’s, a Decred outpoint also names the transaction tree in which the referenced transaction appears, because a block carries regular and stake transactions in two separate lists (Section 4.6).
| Field | Type | Bytes | Description |
|---|---|---|---|
| Hash | [32]byte | 32 | Transaction hash (Section 4.3.8) of the transaction that created the output. |
| Index | uint32 | 4 | Zero-based index of the output within that transaction’s outputs. |
| Tree | int8 | 1 | Tree of the transaction that created the output: 0 regular, 1 stake (Table 4.7). |
| Constant | Value | Meaning |
|---|---|---|
TxTreeRegular | 0 | The regular transaction tree: ordinary transfers and the coinbase. |
TxTreeStake | 1 | The stake transaction tree: tickets, votes, revocations and treasury transactions. |
TxTreeUnknown | In-memory marker for “not in a block”; never serialized. |
Rule 4.4 · Outpoint tree byte
The tree byte is read as an unsigned byte and reinterpreted asint8. If the result is negative, that is, if the byte is 0x80 or greater, the transaction is undecodable.dcrd: wire/msgtx.go · ReadOutPoint
Tree values 2 to 127 decode successfully. dcrd identifies unspent outputs by the full triple (hash, index, tree), and every output it records has tree 0 or 1, according to the type of the transaction that created it (Chapter 7). An outpoint with any other tree value therefore never refers to an existing output, and an input carrying one is rejected by the ordinary missing-input rule, or, for an input that is required to be null, by the null-outpoint requirement (which includes tree 0), not by the decoder.
A null outpoint has the all-zero hash, index MaxPrevOutIndex and tree 0. It marks inputs that spend nothing, such as those of the coinbase, the stakebase input of a vote, the treasurybase and treasury spends; which of these require a null outpoint, and which components are checked, is specified with each transaction type.
4.3.3 Transaction Inputs¶
Each input has fields in the prefix and fields in the witness. The two halves are paired by position: the -th witness entry belongs to the -th prefix entry.
| Field | Type | Bytes | Description |
|---|---|---|---|
| PreviousOutPoint | Outpoint | 37 | The output being spent (Table 4.6). |
| Sequence | uint32 | 4 | Sequence number. MaxTxInSequenceNum is the default. For transactions of version 2 or higher it can encode a relative lock time (Table 4.10; semantics in Chapter 7). |
| Field | Type | Bytes | Description |
|---|---|---|---|
| ValueIn | int64 | 8 | Amount in atoms of the output being spent, committed by the spender. NullValueIn is the conventional “not given” value. |
| BlockHeight | uint32 | 4 | Height of the block containing the output being spent. NullBlockHeight is the null value. |
| BlockIndex | uint32 | 4 | Zero-based position, within its tree, of the transaction that created the output being spent. NullBlockIndex is the null value. |
| SignatureScript | varbytes | var | Script that satisfies the spent output’s public key script (Chapter 6). |
dcrd calls the three numeric witness fields fraud-proof data: they repeat, inside the spending transaction, the amount and the location of the output being spent. For inputs that spend an existing output, consensus requires them to match the output exactly; for inputs that spend nothing, specific transaction types require the null values. Both requirements are specified in Chapter 7. Note that “null” for BlockHeight is , a valid height, while “null” for BlockIndex is 0xffffffff.
| Constant | Value | Meaning |
|---|---|---|
MaxTxInSequenceNum | 0xffffffff | Maximum and default sequence number. |
SequenceLockTimeDisabled | 1 << 31 | If set, the sequence number is not a relative lock time. |
SequenceLockTimeIsSeconds | 1 << 22 | If set, the relative lock time counts units of 512 seconds; otherwise blocks. |
SequenceLockTimeMask | 0x0000ffff | Mask that extracts the relative lock time value. |
SequenceLockTimeGranularity | 9 | Seconds-based lock times are shifted right by this amount (). |
4.3.4 Transaction Outputs¶
| Field | Type | Bytes | Description |
|---|---|---|---|
| Value | int64 | 8 | Amount in atoms (Section 4.7). |
| Version | uint16 | 2 | Script version of PkScript. DefaultPkScriptVersion (Chapter 6). |
| PkScript | varbytes | var | Public key script: the conditions for spending this output (Chapter 6). |
4.3.5 Lock Time and Expiry¶
The prefix ends with two uint32 fields. LockTime is interpreted as a block height or a Unix time depending on its magnitude, and Expiry is a block height, with NoExpiryValue meaning that the transaction never expires. Their semantics are given in Chapter 7. A witness-only serialization does not carry them.
4.3.6 Serialized Layouts¶
The prefix body and witness body are defined as follows; each serialization type is the version word followed by one or both bodies (Table 4.4).
| Field | Type | Bytes | Description |
|---|---|---|---|
| TxInCount | varint | 1–9 | Number of inputs . |
| TxIn[i] | TxIn prefix | Table 4.8, for . | |
| TxOutCount | varint | 1–9 | Number of outputs . |
| TxOut[j] | TxOut | var | Table 4.11, for . |
| LockTime | uint32 | 4 | |
| Expiry | uint32 | 4 |
| Field | Type | Bytes | Description |
|---|---|---|---|
| WitnessCount | varint | 1–9 | Number of witness entries. In a full serialization it equals the input count (Rule 4.6). |
| TxIn[i] | TxIn witness | var | Table 4.9, in input order. |
Algorithm 4.2 · Transaction serialization
FULL, NO_WITNESS, ONLY_WITNESS = 0, 1, 2
def encode_varbytes(b: bytes) -> bytes:
return encode_varint(len(b)) + b
def prefix_body(tx) -> bytes:
out = encode_varint(len(tx.inputs))
for ti in tx.inputs:
out += ti.prev.hash + le32(ti.prev.index) + u8(ti.prev.tree) + le32(ti.sequence)
out += encode_varint(len(tx.outputs))
for to in tx.outputs:
out += le64(uint64(to.value)) + le16(to.version) + encode_varbytes(to.pk_script)
return out + le32(tx.lock_time) + le32(tx.expiry)
def witness_body(tx) -> bytes:
out = encode_varint(len(tx.inputs))
for ti in tx.inputs:
out += (le64(uint64(ti.value_in)) + le32(ti.block_height)
+ le32(ti.block_index) + encode_varbytes(ti.sig_script))
return out
def serialize_tx(tx, ser_type: int) -> bytes:
out = le32(tx.version | (ser_type << 16))
if ser_type == FULL:
return out + prefix_body(tx) + witness_body(tx)
if ser_type == NO_WITNESS:
return out + prefix_body(tx)
if ser_type == ONLY_WITNESS:
return out + witness_body(tx)dcrd: wire/msgtx.go · MsgTx.BtcEncode | wire/msgtx.go · encodePrefix | wire/msgtx.go · encodeWitness
The serialized size of a transaction in each type follows directly. With inputs, outputs, signature scripts and public key scripts , and the varint size of :
(4.2)
(4.3)
(4.4)
The size that the transaction-size rule of Chapter 7 compares with MaxTxSize (393216 bytes on the main network) is the size in the serialization type the transaction was decoded with.
4.3.7 Decoding¶
Decoding reads the version word, rejects an unknown serialization type, and then reads the bodies that the type calls for. Fields that the chosen type does not carry are set to zero or empty. In particular a prefix-only transaction has every input’s ValueIn, BlockHeight and BlockIndex equal to (not to the null values) and empty signature scripts, and a witness-only transaction has all-zero outpoints, zero sequence numbers, no outputs, and zero lock time and expiry.
The decoder bounds every count and length before allocating storage for it. The bounds are derived from MaxMessagePayload bytes, the largest peer-to-peer message payload: a count is rejected when even the smallest possible elements could not fit into one message.
| Bound | Value | Derivation |
|---|---|---|
| Inputs (prefix or witness count) | 780336 | , where is dcrd’s minTxInPayload (outpoint, a one-byte script-length varint and the sequence, plus one byte). |
| Outputs | 3728271 | , where is minTxOutPayload (value and a one-byte script-length varint). |
| Signature script length | 33554432 | MaxMessagePayload. |
| Public key script length | 33554432 | MaxMessagePayload. |
Rule 4.5 · Transaction element counts
A transaction is undecodable if its input count (in the prefix body, or in the witness body of a witness-only serialization) exceeds 780336, or if its output count exceeds 3728271. Each count is compared after it has been decoded as a varint and before any element is read.dcrd: wire/msgtx.go · MsgTx.decodePrefix | wire/msgtx.go · MsgTx.decodeWitness
Rule 4.6 · Witness count of a full serialization
In a transaction of serialization type0 (full), the witness count MUST equal the number of inputs read from the prefix body; otherwise the transaction is undecodable.dcrd: wire/msgtx.go · MsgTx.decodeWitness
Rule 4.7 · Script lengths
A signature script or public key script whose length varint exceeds 33554432 makes the transaction undecodable. The comparison is made before the script bytes are read.dcrd: wire/msgtx.go · readScript | wire/msgtx.go · readTxInWitness | wire/msgtx.go · readTxOut
Algorithm 4.3 · Transaction decoding: constants and helpers
MAX_MESSAGE_PAYLOAD = 33554432
MAX_TX_IN = MAX_MESSAGE_PAYLOAD // 43 + 1 # 780336
MAX_TX_OUT = MAX_MESSAGE_PAYLOAD // 9 + 1 # 3728271
def decode_varbytes(r, max_len: int) -> bytes:
n = decode_varint(r)
if n > max_len:
reject("script too long")
return r.read(n) # reject("truncated") if short
def read_witness_fields(r, ti):
ti.value_in = int64(r.read_le64())
ti.block_height = r.read_le32()
ti.block_index = r.read_le32()
ti.sig_script = decode_varbytes(r, MAX_MESSAGE_PAYLOAD)dcrd: wire/msgtx.go · readScript | wire/msgtx.go · readTxInWitness
Algorithm 4.4 · Transaction decoding
def decode_tx(r) -> Tx:
word = r.read_le32()
tx = Tx(version=word & 0xffff, ser_type=word >> 16,
inputs=[], outputs=[], lock_time=0, expiry=0)
if tx.ser_type not in (FULL, NO_WITNESS, ONLY_WITNESS):
reject("unknown serialization type")
if tx.ser_type in (FULL, NO_WITNESS):
n = decode_varint(r)
if n > MAX_TX_IN:
reject("too many inputs")
for _ in range(n):
ti = TxIn() # all witness fields zero / empty
ti.prev.hash = r.read(32)
ti.prev.index = r.read_le32()
tree = r.read_u8()
if tree >= 0x80:
reject("negative transaction tree")
ti.prev.tree = tree
ti.sequence = r.read_le32()
tx.inputs.append(ti)
m = decode_varint(r)
if m > MAX_TX_OUT:
reject("too many outputs")
for _ in range(m):
to = TxOut()
to.value = int64(r.read_le64())
to.version = r.read_le16()
to.pk_script = decode_varbytes(r, MAX_MESSAGE_PAYLOAD)
tx.outputs.append(to)
tx.lock_time = r.read_le32()
tx.expiry = r.read_le32()
if tx.ser_type == FULL:
k = decode_varint(r)
if k != len(tx.inputs):
reject("witness count differs from input count")
for ti in tx.inputs:
read_witness_fields(r, ti)
elif tx.ser_type == ONLY_WITNESS:
k = decode_varint(r)
if k > MAX_TX_IN:
reject("too many inputs")
for _ in range(k):
ti = TxIn() # all prefix fields zero
read_witness_fields(r, ti)
tx.inputs.append(ti)
return txdcrd: wire/msgtx.go · MsgTx.BtcDecode | wire/msgtx.go · ReadOutPoint | wire/msgtx.go · readTxOut
Note
None of the bounds in Table 4.14 can be reached by a transaction inside a block that respects the block size limit of Chapter 10 (at mostMaxBlockPayload bytes): an input prefix alone occupies 41 bytes and an output at least 11. They matter for robustness, not for which blocks are valid, but an implementation that enforces tighter bounds at decoding time must make sure they too can never bind on a block within the size limit.4.3.8 Transaction Identifiers¶
Three hashes are defined for every transaction. Each is computed from the decoded transaction, independently of the serialization type it was received in.
Algorithm 4.5 · Transaction hashes
def tx_hash(tx) -> bytes: # "transaction hash", "txid"
return blake256(serialize_tx(tx, NO_WITNESS))
def tx_witness_hash(tx) -> bytes:
return blake256(serialize_tx(tx, ONLY_WITNESS))
def tx_full_hash(tx) -> bytes:
return blake256(tx_hash(tx) + tx_witness_hash(tx))dcrd: wire/msgtx.go · MsgTx.TxHash | wire/msgtx.go · MsgTx.TxHashWitness | wire/msgtx.go · MsgTx.TxHashFull
| Hash | Used for |
|---|---|
Transaction hash (tx_hash) | The transaction’s identity: the Hash field of outpoints that spend its outputs, the identity of tickets in the ticket pool and lottery, duplicate-transaction checks, display to users. It does not commit to any witness field. |
Witness hash (tx_witness_hash) | Only as an input to the full hash. |
Full hash (tx_full_hash) | The leaves of the transaction-tree Merkle trees (Section 4.5), so that the block header commits to the witnesses as well as the prefixes. |
Implementation pitfall
- The transaction hash is the hash of the prefix serialization, which begins with the version word carrying serialization type
1. It is therefore not the hash of any substring of the full serialization, whose version word carries type0. For a version-1 transaction the prefix serialization begins01000100, the full serialization01000000. - Likewise the witness serialization begins with type
2in its version word and repeats the input count. - The full hash is the hash of the 64-byte concatenation of the two other hashes, not the hash of the full serialization.
- Signature hashing (Chapter 6) defines its own serializations. Its prefix component coincides with the transaction hash for the signature hash type
SigHashAllwithoutSigHashAnyOneCanPay(dcrd reuses the cached transaction hash in that case); its witness component is not the witness hash defined here.
Rationale
Including the serialization type in the hashed version word guarantees that the prefix and witness hashes of a transaction differ even when the two bodies would otherwise be identical byte strings (code comment inwire/msgtx.go · MsgTx.TxHashFull). Excluding the witness from the identifier makes the identifier immune to signature-script malleability.4.3.9 Transactions in Blocks¶
Each transaction in a block is decoded with the serialization type given in its own version word. Blocks produced by dcrd contain only full (type 0) transactions, and dcrd’s mempool policy refuses any other type, but the block format and the consensus code do not themselves name the serialization type.
Implementation pitfall
dcrd has no consensus rule that names the serialization type of a transaction in a block. A block containing a type-1 or type-2 transaction is decoded as specified above (Rule 4.1 does not reject it), its size (header Size, Rule 4.11) counts the transaction’s actual, shorter encoding, its Merkle leaf is the full hash of the transaction as decoded (with zeroed witness or zeroed prefix fields), and the transaction is then subject to every ordinary validation rule using those decoded values. A header can therefore match such a block in Size and in its Merkle roots. The block is nevertheless always invalid, for the following reasons:
- A type-
2transaction has no outputs and fails the requirement that every transaction have at least one output (Chapter 7). - A type-
1transaction hasValueIn,BlockHeight,BlockIndexand an empty signature script on every input, and it has at least one input. An input that spends an existing output fails the fraud-proof rule (ValueInmust equal the output’s amount, and outputs of amount cannot be spent at all). The null input of a coinbase, treasurybase or treasury spend fails the null-fraud-proof rule (BlockIndexmust be0xffffffff). A vote’s stakebase input fails the same rule, so the transaction is not recognized as a vote and is then rejected for its null outpoint; in any case its ticket input is an input of the first kind.
Running dcrd’s validation on blocks whose transactions were re-encoded in type 1 produced, respectively, a fraud-proof amount error (regular transaction and ticket), a non-null coinbase fraud proof error (coinbase) and a null-previous-output error (vote); type 2 produced the no-outputs error. Because the Merkle leaf is computed from the decoded fields, a full-type transaction with the same field values has the same leaf and is invalid for the same reason, so the header itself commits to invalid data. An implementation may therefore reject a block containing a non-full transaction once the header’s commitments to it have been checked, but must not validate the transaction as though the missing fields were present.
4.4 The Block Header¶
The block header is a fixed 180-byte structure (MaxBlockHeaderPayload ). Every field has a fixed width, so the header has exactly one serialization. Besides the usual chain link, Merkle root, difficulty, time and nonce, it carries a summary of the block’s stake activity, which allows several stake rules to be checked from headers alone.
| Field | Type | Bytes | Description |
|---|---|---|---|
| Version | int32 | 4 | Offset 0. Block version (Chapter 13); not the protocol version. |
| PrevBlock | [32]byte | 32 | Offset 4. Block hash (Section 4.4.1) of the parent block; all zero in the genesis block. This is always the BLAKE-256 block hash, never the proof-of-work hash. |
| MerkleRoot | [32]byte | 32 | Offset 36. Commitment to the transactions: the regular-tree Merkle root, or the combined root after DCP-0005 (Section 4.5). |
| StakeRoot | [32]byte | 32 | Offset 68. The stake-tree Merkle root, or the header commitment root after DCP-0005 (Section 4.5). |
| VoteBits | uint16 | 2 | Offset 100. Bit 0 (0x0001) set means the regular transaction tree of the parent block is approved by this block’s votes; its consistency with the votes is specified in Chapter 8 and its effect in Chapter 10. The remaining bits are described in Chapter 13. |
| FinalState | [6]byte | 6 | Offset 102. Final state of the ticket-lottery pseudorandom generator (Chapter 8). |
| Voters | uint16 | 2 | Offset 108. Number of votes in the block. |
| FreshStake | uint8 | 1 | Offset 110. Number of ticket purchases in the block. |
| Revocations | uint8 | 1 | Offset 111. Number of revocations in the block. |
| PoolSize | uint32 | 4 | Offset 112. Size of the live ticket pool (Chapter 8). |
| Bits | uint32 | 4 | Offset 116. Proof-of-work target in compact form (Chapter 9). |
| SBits | int64 | 8 | Offset 120. Stake difficulty: the ticket price in atoms (Chapter 9). |
| Height | uint32 | 4 | Offset 128. Height of this block. |
| Size | uint32 | 4 | Offset 132. Length in bytes of the serialized block (Rule 4.11). |
| Timestamp | uint32 | 4 | Offset 136. Block time, Unix seconds. |
| Nonce | uint32 | 4 | Offset 140. Proof-of-work nonce. |
| ExtraData | [32]byte | 32 | Offset 144. Additional nonce space for miners; no consensus meaning beyond being hashed. |
| StakeVersion | uint32 | 4 | Offset 176. Stake version, determined by the versions of recent votes (Chapter 13). |
Algorithm 4.6 · Block header serialization
def serialize_header(h) -> bytes: # always exactly 180 bytes
return (le32(uint32(h.version)) + h.prev_block + h.merkle_root + h.stake_root
+ le16(h.vote_bits) + h.final_state + le16(h.voters)
+ u8(h.fresh_stake) + u8(h.revocations) + le32(h.pool_size)
+ le32(h.bits) + le64(uint64(h.sbits)) + le32(h.height)
+ le32(h.size) + le32(h.timestamp) + le32(h.nonce)
+ h.extra_data + le32(h.stake_version))dcrd: wire/blockheader.go · writeBlockHeader
The decoder reads the 18 fields in this order and imposes no condition on any of them; a header is undecodable only if fewer than 180 bytes are available.
Note
Nonce and ExtraData together form 36 contiguous bytes (offsets 140 to 175) that the consensus rules treat as an uninterpreted blob. DCP-0011 describes them as a single 36-byte extra-data field and recommends a layout for miners and pools; dcrd’s split into a 4-byte nonce and 32 further bytes is a representation convenience only.Note
The timestamp is an unsigned 32-bit count of seconds, so it can represent times up to 2106-02-07. The header has no other time field.4.4.1 The Block Hash¶
The block hash, which identifies a block and is the value stored in the next block’s PrevBlock, is
(4.5)
It commits to every header field, and through MerkleRoot (and, before DCP-0005, StakeRoot) to every transaction. The block hash has always been computed this way and is unaffected by DCP-0011.
4.4.2 The Proof-of-Work Hash¶
The proof-of-work hash is the value compared with the target. Its input is the same 180-byte header serialization; only the hash function differs:
- if the
blake3powagenda (DCP-0011) is not active for the block, the proof-of-work hash is the block hash, ; - if it is active, the proof-of-work hash is with a 32-byte output.
The hash functions are specified in Chapter 3 and the target check in Chapter 9. After DCP-0011 the proof-of-work hash and the block hash of a block are different values, and only the block hash is used to identify blocks.
4.5 Transaction Trees and Merkle Roots¶
A block holds two ordered lists of transactions: the regular transaction tree and the stake transaction tree. (“Tree” is Decred’s name for these lists; each is a flat sequence, and the word refers to the Merkle tree built over it.) Which transactions belong in which list is specified in Chapter 10 and Chapter 8.
The Merkle root of a list of transactions is the Merkle root, as defined in Chapter 3, of the list of their full hashes (Algorithm 4.5) in block order. That construction pairs adjacent hashes with BLAKE-256, duplicating the last hash of any level with an odd number of entries; the root of an empty list is 32 zero bytes, and the root of a one-element list is that element.
Algorithm 4.7 · Transaction tree roots and the combined root
def tree_root(txs) -> bytes:
# merkle_root() is the generic construction of the cryptography chapter;
# merkle_root([]) is 32 zero bytes.
return merkle_root([tx_full_hash(tx) for tx in txs])
def combined_root(regular_txs, stake_txs) -> bytes:
# Equal to merkle_root([tree_root(regular_txs), tree_root(stake_txs)]).
return blake256(tree_root(regular_txs) + tree_root(stake_txs))dcrd: blockchain/standalone/merkle.go · CalcTxTreeMerkleRoot | blockchain/standalone/merkle.go · CalcCombinedTxTreeMerkleRoot
What the header’s two root fields must contain depends on whether the headercommitments agenda (DCP-0005) is active for the block, as determined from its parent by the rules of Chapter 13.
Rule 4.8 · Merkle roots before header commitments
If theheadercommitments agenda is not active for the block, the header’s MerkleRoot MUST equal tree_root of the regular transaction tree and its StakeRoot MUST equal tree_root of the stake transaction tree.dcrd: internal/blockchain/validate.go · checkMerkleRoots | internal/blockchain/validate.go · checkMerkleRootsContext
Rule 4.9 · Merkle roots after header commitments
If theheadercommitments agenda is active for the block, the header’s MerkleRoot MUST equal combined_root of the regular and stake transaction trees, that is, where and are the regular and stake tree roots. The StakeRoot field then holds the header commitment root, which MUST equal the version 1 commitment root of DCP-0005, which is the hash of the block’s version 2 compact filter (the BLAKE-256 hash of the serialized filter data, with an all-zero hash for an empty filter). The filter and its hash are specified in Chapter 16 and the enforcement of this field in Chapter 10.dcrd: internal/blockchain/validate.go · checkMerkleRoots | internal/blockchain/validate.go · checkConnectBlock
History
At launch the header committed to the two trees separately, inMerkleRoot and StakeRoot. DCP-0005 merged both commitments into MerkleRoot and repurposed StakeRoot as the root of a new Merkle tree of header commitments, whose only leaf so far is the hash of the block’s compact filter, so that light clients can verify filters from headers. On the main network the agenda became active at block 431488; an implementation must nevertheless determine activation from the chain (Chapter 13).Note
dcrd checks the Merkle roots twice. Before the full chain context of a block is available it determines the agenda state “positionally”, from the known activation block of each network; where that is not possible (on networks without a recorded activation) it provisionally accepts a header that matches either variant. The contextual check made when the block is connected then requires the variant selected by the agenda state. The net consensus effect is exactly the two rules above.Implementation pitfall
The Merkle leaves are full hashes, which commit to the witnesses. Outpoints refer to transactions by their transaction hash, which does not. Using the transaction hash as the leaf yields a different root (Section 4.9.4 shows both for a real block).4.6 Block Serialization¶
| Field | Type | Bytes | Description |
|---|---|---|---|
| Header | BlockHeader | 180 | Table 4.16. |
| TxCount | varint | 1–9 | Number of regular transactions. |
| Transactions | Tx[] | var | Regular transaction tree, each transaction serialized in its own serialization type (normally full). |
| STxCount | varint | 1–9 | Number of stake transactions. |
| STransactions | Tx[] | var | Stake transaction tree, likewise. |
Algorithm 4.8 · Block serialization
def serialize_block(b) -> bytes:
out = serialize_header(b.header) + encode_varint(len(b.regular))
for tx in b.regular:
out += serialize_tx(tx, tx.ser_type)
out += encode_varint(len(b.stake))
for tx in b.stake:
out += serialize_tx(tx, tx.ser_type)
return outdcrd: wire/msgblock.go · MsgBlock.BtcEncode
Decoding reads the header, then each count followed by that many transactions decoded with Algorithm 4.4. The encoding of a block is exactly the bytes so consumed. The decoders of blocks and transactions do not themselves examine what follows the structure. When a block or a transaction is carried in a peer-to-peer message, dcrd rejects the message unless its payload consists of exactly those bytes (a payload with unconsumed trailing bytes is an error); that requirement, and the per-message size limits, are part of Chapter 15. Outside that framing, trailing bytes are simply not part of the block: dcrd’s submitblock RPC, for example, decodes the block from the front of the submitted data and does not reject anything after it. Such bytes count neither towards the block’s size nor towards its hash, although dcrd keeps the submitted buffer as the block’s serialization: it is written to the block database with the trailing bytes and returned unchanged by getblock in raw form, while peers are sent a re-encoding without them.
Rule 4.10 · Block transaction counts
A block is undecodable if either its regular or its stake transaction count exceeds , where is the size of the smallest possible full transaction (minTxPayload: version word, three one-byte varints, lock time, expiry) and is MaxBlockPayloadV3 when decoding at peer-to-peer protocol version 3 or lower (giving 33334), and MaxBlockPayload at higher protocol versions (giving 43691). dcrd decodes blocks from storage and from the submitblock RPC at protocol version 0, and therefore with the bound 33334.dcrd: wire/msgblock.go · MsgBlock.BtcDecode | wire/msgblock.go · MaxTxPerTxTree
Note
This bound depends on a transport parameter, but it never decides whether a block is valid. Every block must have a serialized size of at most 1310720 bytes (Chapter 10), and every transaction must have at least one input and one output (Chapter 7), which makes it at least 66 bytes long even in prefix-only form (see Table 4.18). Such a block holds at most transactions in total, well below 33334. Put the other way round, a block that exceeded the count within 1310720 bytes would have to contain a transaction shorter than 40 bytes, which cannot have both an input and an output. An implementation may therefore omit the bound, or apply either value, without affecting consensus.4.6.1 Block Size¶
The size of a block is the length in bytes of serialize_block applied to the decoded block: header, both counts and every transaction in the serialization type it was decoded with. Since decoding is canonical this is also the number of bytes the block occupied on the wire.
Rule 4.11 · Header size field
The header’sSize field MUST equal the size of the block as just defined.dcrd: internal/blockchain/validate.go · checkBlockDataSanity
The limits on the size, the context-free bound of MaxBlockPayload bytes and the network-specific maximum MaximumBlockSizes (393216 bytes on the main network), are block validation rules specified in Chapter 10.
| Structure | Bytes | Composition |
|---|---|---|
| Block header | 180 | Fixed. |
| Outpoint | 37 | 32 + 4 + 1. |
| Input, prefix part | 41 | Outpoint + 4. |
| Input, witness part | 8 + 4 + 4 + script-length varint + script; 17 with an empty script. | |
| Output | 8 + 2 + script-length varint + script; 11 with an empty script. | |
| Full transaction, no inputs or outputs | 15 | 4 + 1 + 1 + 4 + 4 + 1. Decodable but never valid. |
| Full transaction, 1 input, 1 output, empty scripts | 84 | Smallest full transaction that has an input and an output. |
| Prefix-only transaction, 1 input, 1 output | 66 | Same transaction in serialization type 1. |
| Witness-only transaction, 1 input | 22 | Same transaction in serialization type 2. |
| Main-network genesis block | 300 | Header, one 118-byte coinbase, empty stake tree. |
4.6.2 The Genesis Block¶
Each network’s genesis block is a fixed constant (Chapter 14). It is valid by definition, and none of its fields are validated. Two of its fields are inconsistent with the rules above, and an implementation must use the constant as given rather than recompute it:
- its header
Sizeis on every network, although the main-network genesis block serializes to 300 bytes; - on the test network (version 3) its
MerkleRootis the transaction hash of the genesis coinbase instead of its full hash (a mistake recorded in a comment inchaincfg/testnetparams.go · TestNet3Params); on the other networks it is the full hash, as Rule 4.8 would require.
Changing either value would change the genesis hash and therefore every block of that network.
4.7 Amounts¶
Amounts are integers counting atoms and are carried in int64 fields: TxOut.Value, TxIn.ValueIn and the header’s SBits. On the wire they are eight little-endian bytes of the two’s complement representation, and every bit pattern decodes. The range checks, which make negative output values, values above MaxAmount and negative input values other than NullValueIn invalid, are transaction rules (Chapter 7).
| Constant | Value | Meaning |
|---|---|---|
AtomsPerCoin | Atoms per DCR. | |
AtomsPerCent | Atoms per hundredth of a DCR. | |
MaxAmount | Largest amount permitted in any single value or sum (Chapter 7). | |
NullValueIn | Conventional “not given” value of ValueIn. |
4.8 Summary of Constants and Decoding Limits¶
| Constant | Value | Meaning |
|---|---|---|
MaxVarIntPayload | 9 | Longest varint encoding. |
HashSize | 32 | Bytes in a hash. |
MaxBlockHeaderPayload | 180 | Bytes in a block header (exact). |
MaxMessagePayload | 33554432 | Largest peer-to-peer payload; basis of the transaction decoding bounds. |
MaxBlockPayloadV3 | 1000000 | Largest block message at protocol version 3 or lower. |
MaxBlockPayload | 1310720 | Largest block message at later protocol versions; also the context-free block size limit (Chapter 10). |
MaxPrevOutIndex | 0xffffffff | Largest outpoint index; the index of a null outpoint. |
MaxTxInSequenceNum | 0xffffffff | Largest and default sequence number. |
NullValueIn | Null ValueIn. | |
NullBlockHeight | 0 | Null BlockHeight. |
NullBlockIndex | 0xffffffff | Null BlockIndex. |
NoExpiryValue | 0 | Expiry value meaning “never expires”. |
DefaultPkScriptVersion | 0 | Default output script version. |
TxTreeRegular | 0 | Regular tree identifier. |
TxTreeStake | 1 | Stake tree identifier. |
| Structure | Condition | Rule |
|---|---|---|
| Any | Input ends before a field is complete. | Rule 4.1 |
| Varint | Non-canonical (value fits a shorter form). | Rule 4.2 |
| Transaction | Serialization type not 0, 1 or 2. | Rule 4.3 |
| Transaction | Outpoint tree byte 0x80. | Rule 4.4 |
| Transaction | Input count or output count . | Rule 4.5 |
| Transaction | Full serialization whose witness count differs from its input count. | Rule 4.6 |
| Transaction | Script length . | Rule 4.7 |
| Block | Regular or stake transaction count above 33334 (protocol version , and storage) or 43691 (later versions). | Rule 4.10 |
4.9 Test Vectors¶
All vectors in this section were produced by running dcrd’s wire, chaincfg and blockchain/standalone packages at the reference commit. Byte strings are in serialized order; hashes are given in both serialized (“internal”) and display form.
4.9.1 Variable-Length Integers¶
Test vector: Varint encodings at every boundary
value encoding
0 00
252 (0xfc) fc
253 (0xfd) fdfd00
255 (0xff) fdff00
256 (0x100) fd0001
65535 (0xffff) fdffff
65536 (0x10000) fe00000100
4294967295 (0xffffffff) feffffffff
4294967296 (0x100000000) ff0000000001000000
2^64 - 1 ffffffffffffffffffTest vector: Varint decoding: rejected and accepted inputs
input result
fd0000 reject: non-canonical (0 < 0xfd)
fdfc00 reject: non-canonical (252 < 0xfd)
fdfd00 253
fe00000000 reject: non-canonical (0 < 0x10000)
feffff0000 reject: non-canonical (65535 < 0x10000)
fe00000100 65536
ff0000000000000000 reject: non-canonical (0 < 0x100000000)
ffffffffff00000000 reject: non-canonical (4294967295 < 0x100000000)
ff0000000001000000 4294967296
fd00 reject: truncated
fe000001 reject: truncated
(empty) reject: truncated4.9.2 The Main-Network Genesis Block¶
The main-network genesis header serializes to the following 180 bytes:
Test vector: Main-network genesis block header
01000000
0000000000000000000000000000000000000000000000000000000000000000
0dc101dfc3c6a2eb10ca0c5374e10d28feb53f7eabcc850511ceadb99174aa66
0000000000000000000000000000000000000000000000000000000000000000
0000 000000000000 0000 00 00 00000000 ffff011b 00c2eb0b00000000
00000000 00000000 a0d7b856 00000000
0000000000000000000000000000000000000000000000000000000000000000
00000000
block hash (internal):
80d9212bf4ceb066ded2866b39d4ed89e0ab60f335c11df8e7bf85d9c35c8e29
block hash (display):
298e5cc3d985bfe7f81dc135f360abe089edd4396b86d2de66b0cef42b21d980
BLAKE3 of the header (internal):
1baa4b79809fc85fac9333371d37e203f34c345595f5ed5ed435fbc10b09d3d7The lines hold, in order: Version; PrevBlock; MerkleRoot; StakeRoot; VoteBits to SBits; Height, Size, Timestamp and Nonce; ExtraData; StakeVersion (see Table 4.22). The BLAKE3 value only illustrates Section 4.4.2; the genesis block is not subject to a proof-of-work check.
| Offset | Bytes | Field | Serialized bytes | Value |
|---|---|---|---|---|
| 0 | 4 | Version | 01000000 | 1 |
| 4 | 32 | PrevBlock | 0000000000000000000000000000000000000000000000000000000000000000 | none |
| 36 | 32 | MerkleRoot | 0dc101dfc3c6a2eb10ca0c5374e10d28feb53f7eabcc850511ceadb99174aa66 | Full hash of the coinbase |
| 68 | 32 | StakeRoot | 0000000000000000000000000000000000000000000000000000000000000000 | Empty stake tree |
| 100 | 2 | VoteBits | 0000 | 0 |
| 102 | 6 | FinalState | 000000000000 | zero |
| 108 | 2 | Voters | 0000 | 0 |
| 110 | 1 | FreshStake | 00 | 0 |
| 111 | 1 | Revocations | 00 | 0 |
| 112 | 4 | PoolSize | 00000000 | 0 |
| 116 | 4 | Bits | ffff011b | 0x1b01ffff |
| 120 | 8 | SBits | 00c2eb0b00000000 | 200000000 (2 DCR) |
| 128 | 4 | Height | 00000000 | 0 |
| 132 | 4 | Size | 00000000 | 0 (see Section 4.6.2) |
| 136 | 4 | Timestamp | a0d7b856 | 1454954400 (2016-02-08 18:00:00 UTC) |
| 140 | 4 | Nonce | 00000000 | 0 |
| 144 | 32 | ExtraData | 0000000000000000000000000000000000000000000000000000000000000000 | zero |
| 176 | 4 | StakeVersion | 00000000 | 0 |
The genesis block has one regular transaction, the coinbase, and an empty stake tree. Its MerkleRoot is tree_root of a one-element list, which is the coinbase’s full hash, and its StakeRoot is tree_root of the empty list, which is zero: the genesis header conforms to Rule 4.8.
Test vector: Main-network genesis coinbase in all three serialization types
full serialization (118 bytes):
01000000 version 1, type 0
01 1 input
0000000000000000000000000000000000000000000000000000000000000000
ffffffff 00 index 0xffffffff, tree 0
ffffffff sequence
01 1 output
0000000000000000 0000 value 0, script version 0
20 script length 32
801679e98561ada96caec2949a5d41c4cab3851eb740d951c10ecbcf265c1fd9
00000000 00000000 lock time, expiry
01 1 witness
ffffffffffffffff ValueIn = -1 (null)
00000000 ffffffff BlockHeight, BlockIndex (null)
02 0000 signature script 0000
prefix serialization (98 bytes): the first 98 bytes above,
with version word 01000100 in place of 01000000
witness serialization (24 bytes):
01000200 01 ffffffffffffffff 00000000 ffffffff 02 0000
transaction hash, internal and display:
2c0ad603d44a16698ac951fa22aab5e7b30293fa1d0ac72560cdfcc9eabcdfe7
e7dfbceac9fccd6025c70a1dfa9302b3e7b5aa22fa51c98a69164ad403d60a2c
witness hash, internal and display:
6c6a06b87b148ada9f3950d0cc275a4bad21622116bd1de16909f5eff1874eeb
eb4e87f1eff50969e11dbd16216221ad4b5a27ccd050399fda8a147bb8066a6c
full hash, internal and display:
0dc101dfc3c6a2eb10ca0c5374e10d28feb53f7eabcc850511ceadb99174aa66
66aa7491b9adce110585ccab7e3fb5fe280de174530cca10eba2c6c3df01c10d
BLAKE-256 of the full serialization (not an identifier), internal:
33c951d47eef1f372f3ac45f8da9eb3f7982121b8db4a30ab12c28ccb3a51c00Test vector: Main-network genesis block (300 bytes)
The 180-byte header above, then01, the 118-byte full coinbase, and 00 (no stake transactions). Its size is bytes; the header’s Size field nevertheless holds 0.4.9.3 A Regular Transaction¶
The following one-input, two-output pay-to-public-key-hash transaction is taken from dcrd’s test data (dcrutil/txsort/testdata/tx150043-14.hex).
Test vector: Transaction aa4c208c…ff6684, full serialization (252 bytes)
01000000 version 1, type 0
01 1 input; outpoint:
64084d5f9b7fcedd3d816b7e1ba154123310dd892c09b53c8bf811804efab903
00000000 00 index 0, tree 0
ffffffff sequence
02 2 outputs
52deda0a00000000 0000 182115922, ver. 0
19 76a9146ab21ce59fedbfe724cd979d1409397c68105c0d88ac 25-byte script
52deda0a00000000 0000 182115922, ver. 0
19 76a9145529ab44931e1ad083df53fa97f4922a80f234b688ac 25-byte script
00000000 lock time 0
00000000 expiry 0 (none)
01 1 witness
781fb61500000000 ValueIn 364257144
0d4a0200 BlockHeight 150029
04000000 BlockIndex 4
6b 107-byte script:
483045022100c6ae9a0587e64b6761a55e81321eee6dd3550e9c9a69c058d773
4f0ade1ce00702200650ecc1eacc938bedae55a7f2d0e73bee548dcce77352ef
f77b090b8087b6de012102d30f38730d4d4feb7f7fec0d2c81b550ff702d03c8
191cd6b98e28f9da9e2c9cThe fee, input amount minus output amounts, is atoms.
Test vector: The same transaction, prefix and witness serializations
prefix serialization (127 bytes):
01000100 01
64084d5f9b7fcedd3d816b7e1ba154123310dd892c09b53c8bf811804efab903
00000000 00 ffffffff
02
52deda0a00000000 0000
19 76a9146ab21ce59fedbfe724cd979d1409397c68105c0d88ac
52deda0a00000000 0000
19 76a9145529ab44931e1ad083df53fa97f4922a80f234b688ac
00000000 00000000
witness serialization (129 bytes):
01000200 01 781fb61500000000 0d4a0200 04000000 6b
483045022100c6ae9a0587e64b6761a55e81321eee6dd3550e9c9a69c058d773
4f0ade1ce00702200650ecc1eacc938bedae55a7f2d0e73bee548dcce77352ef
f77b090b8087b6de012102d30f38730d4d4feb7f7fec0d2c81b550ff702d03c8
191cd6b98e28f9da9e2c9c
transaction hash, internal and display:
8466ff8d3e1ca16c4f3bd6dc651c8bd9da9c0066d51dd1546957a9858c204caa
aa4c208c85a9576954d11dd566009cdad98b1c65dcd63b4f6ca11c3e8dff6684
witness hash, internal and display:
3f2d5c43f31cb6d0379c5522bcedb8552d4da1bd1b1b9f4efa7288dc9789f7bf
bff78997dc8872fa4e9f1b1bbda14d2d55b8edbc22559c37d0b61cf3435c2d3f
full hash, internal and display:
9a40e02e84c432d82dbdce8f62ab6bfce9ba3b28cbd4554529a29efc946736d2
d2366794fc9ea2294555d4cb283bbae9fc6bab628fcebd2dd832c4842ee0409aDecoding the 127-byte prefix serialization yields a transaction with serialization type 1, one input and two outputs, whose transaction hash is the same as above. Its full hash, computed from the zeroed witness fields, is (display)d2de7ea99bdc25cb96c3b0bcbc1ea8cc267d8b24e8f1ee673ca637077e890f28, which differs from the full hash of the complete transaction.
4.9.4 Merkle Roots of a Block After DCP-0005¶
Main-network block 432100 (from dcrd’s test data, internal/rpcserver/testdata/block432100.bz2) is later than the activation of headercommitments at block 431488. It has 2 regular and 5 stake transactions.
Test vector: Block 432100: roots and size
block hash (display):
000000000000000023455b4328635d8e014dbeea99c6140aa715836cc7e55981
regular tree root (display):
703983b9e834362a3cd3b4e397b76465b85fde1a87325415ec7527d471f24ed8
stake tree root (display):
8ef384279548ac3965c0f972dcbc39992ffbc8563b4adba80e932887abe38102
combined root = BLAKE-256(regular root || stake root), internal:
0bc8a255edde9901ecc4cdb93e4e573cb38ae91e84495ecddc0c93c019351d5d
header bytes 36..67 (MerkleRoot), equal to the combined root:
0bc8a255edde9901ecc4cdb93e4e573cb38ae91e84495ecddc0c93c019351d5d
header bytes 68..99 (StakeRoot), the commitment root:
7731998be0a78e955f6fb98d2f35479905c3279f6257beab42a51d556cef55b9
regular root computed with transaction-hash leaves (wrong), display:
cccb5c46855861a22b010df67933286e9225f5cb55a5a8833d9a80fae27c1e1d
header bytes 132..135 (Size):
de0a0000 = 2782 = length of the serialized blockThe combined root is the BLAKE-256 hash of the 64 bytes formed by the regular and stake tree roots in internal byte order (the reverse of the display forms shown).
4.9.5 Decoding Failures¶
Each of the following modifications of the 118-byte genesis coinbase was passed to dcrd’s transaction decoder.
Test vector: Transaction decoding failures
modification result
byte 2 (low byte of type) := 03 reject: unsupported serialization type
byte 41 (outpoint tree) := 80 reject: negative transaction tree (-128)
byte 41 (outpoint tree) := 7f decodes (tree 127)
byte 98 (witness count) := 02 reject: witness count 2, prefix count 1
byte 98 (witness count) := 00 reject: witness count 0, prefix count 1
last byte removed reject: truncated
byte 2 := 01 decodes as a 98-byte prefix-only transaction;
the remaining 20 bytes are not part of itThe last case shows why a decoder must stop at the end of the structure: in a block, those 20 bytes would be read as the start of the next transaction or count.