TeachMeBitcoin

Magic Bytes & Block Size

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

Magic Bytes and Block Size

The blk.dat files are not just a sequence of blocks; they are a sequence of Block Records. Each record consists of three parts: the Magic Bytes, the Block Size, and the Block Header/Data.

1. The Magic Bytes (Network Delimiter)

To help a node identify where one block ends and another begins, every block in the file is preceded by a 4-byte Magic Value. For Bitcoin Mainnet, this value is: 0xD9 0xB4 0xBE 0xF9

These bytes act as a "start of record" marker. If a file becomes corrupted, a node can scan for these bytes to find the next valid block.

2. The Block Size Prefix

Immediately following the magic bytes is a 4-byte integer (Little-Endian) representing the Block Size.

3. The Block Data

Following the size prefix is the actual block.

  1. Block Header: 80 bytes.

  2. Transaction Count: A Variable Length Integer (VarInt).

  3. Transactions: The raw serialized transactions.

4. Visual Layout of a Record

[ 4 Bytes ] -> Magic Value (0xD9B4BEF9)
[ 4 Bytes ] -> Block Size (N bytes)
[ N Bytes ] -> Raw Block Data

5. Byte Order (Little-Endian)

The Block Size prefix is stored in little-endian.

In the next section, we will discuss why blocks are not stored in height order in these files.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!