Magic Bytes & Block Size
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.
-
This value tells the node exactly how many bytes of raw block data follow.
-
The size includes the 80-byte header and all transaction data.
-
It does not include the 8 bytes used for the Magic Value and the Size prefix itself.
3. The Block Data
Following the size prefix is the actual block.
-
Block Header: 80 bytes.
-
Transaction Count: A Variable Length Integer (VarInt).
-
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.
-
If the block size is
1,000,000bytes (0x0F4240 in hex). -
It will be stored in the file as:
40 42 0F 00.
In the next section, we will discuss why blocks are not stored in height order in these files.
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: