Skip to content

Block Header Binary Encoding

Lars Kuhtz edited this page Oct 11, 2024 · 30 revisions

C code for decoding chainweb headers is available in this repository

BlockHeader Binary Format For Chain Graphs of Degree Three without Hash

defined in Chainweb.BlockHeader

Size Bytes Value
8 0-7 flags
8 8-15 time
32 16-47 parent
110 48-157 adjacents
32 158-189 target
32 190-221 payload
4 222-225 chain
32 226-257 weight
8 258-265 height
4 266-269 version
8 270-277 epoch start
8 278-285 nonce
32 286-317 hash

total: 318 bytes

Adjacent Parents Record (length 3):

Size Bytes Value
2 0-1 length
108 2-109 adjacents

total: 110 bytes

Adjacent Parent:

Size Bytes Value
4 0-3 chain
32 4-35 hash

total: 36 bytes

Generally, the size in bytes for a chain graph of degree $d$ is: $210 + d 36$.

Fields

POW related values:

Arithmetic operations and comparisons on parent, target, weight, and hash interpret the value as unsigned 256 bit integral numbers in little endian encoding. All operations are performed using rational arithmetic of unlimited precision and the final result is rounded. Please consult the code for details of how the result is rounded.

Time Stamps:

time and epoch start are a little endian twoth complement encoded integral numbers that count SI microseconds since POSIX epoch (leap seconds are ignored). These numbers are always positive (highest bit is 0).

Numbers:

  • height is a little endian encoded unsigned integral 64 bit number.
  • length is a little endian encoded unsigned integral 16 bit number.

Version:

version identifies the chainweb version. It is a 32 bit value in little endian encoding. Values up to 0x0000FFFF are reserved for production versions (which includes Development and testnets).

value version
0x00000005 Mainnet01
0x00000001 Development
0x00000007 Testnet04
0x00000009 Testnet05

Other:

  • nonce is any sequence of 8 bytes that is only compared for equality.
  • chain is any sequence of 4 bytes that identifies a chain and can be compared for equality.
  • payload is any sequence of 32 bytes that is a cryptographic hash of the payload associated with the block and can be compared for equality.
  • flags are eight bytes of value 0x0 that are reserved for future use.

Work Header Binary Format

The work bytes received from the /miner/work endpoint is slightly different than the above header format. These headers do not include the block hash, instead prefixing the header above (without hash) with chain id and hash target bytes.

The first 36 bytes are informational. Only the bytes from position 36 to the end are subject of the POW hash computation.

The PoW hash is computed as the blake2s hash of the 286 work bytes (the work header without the information bytes), which equal the binary block header bytes without the final block hash.

The final 8 bytes are the nonce. The creation time is encoded in bytes 44-52 (see above for details of the encoding). Miners are allowed, but not required, to update the time to reflect the solve time for the block more closely. A larger value for the creation time increases the accuracy of difficulty adjustment which is in the interest of miners -- the high difficulty guarantees that the outcome of the race of winning blocks is determined by actual hash power. However, blocks that are predated (i.e. have a a creation time that is in the future) are rejected during block header validation. Leaving the time unchanged is a valid choice.

Miners must not change or make any assumptions about the content of the "reserved" bytes.

Defined in Chainweb.Miner.Core

Size Bytes Work Bytes Value
4 0-3 NA chain
32 4-35 NA hash-target
8 36-43 0-7 reserved
8 44-51 8-15 time
298 52-313 16-277 reserved
8 314-321 278-285 nonce

total: 322 bytes

total work: 286 bytes

For arithmetic comparisons the hash-target is interpreted as unsigned 256 bit integral number in little endian encoding.

time is a little endian twoth complement encoded integral number that counts SI microseconds since POSIX epoch (leap seconds are ignored). The value is always positive (highest bit is 0).

Kadena miners (ASICs) use the Kadena Stratum protocol dialect to communicate with mining pools.