Bitcoin Learning Series

What is blk.dat file in Bitcoin?

blk.dat file store the raw data of blocks.

Arnish Gupta
3 min readAug 29, 2022

--

Greetings, I am coming to you with the new interesting topic. You are reading Bitcoin Learning Series. When you run the bitcoin node then you see the blocks folder in bitcoin node path (Default Path: ~/.bitcoin/blocks/). We will understand today about this blocks folder.

This folder will automatically created when you run the bitcoin node.

Photo by David Bruno Silva on Unsplash

** Indexes **

  1. What is the blocks folder stored?
  2. What is block hash?
  3. How to calculate block size for block hash?
  4. What happened If we remove the blocks folder?

What is the blocks folder stored?

  • The data received from a node is pushed on this file. The first file created by the node is blk00000.dat
  • The name of these files will look like this
~/.bitcoin/blocks/
blk00000.dat
blk00001.dat
blk00002.dat
blk00003.dat
blk00004.dat
...
  • The complete blockchain is not stored on a single file So the bitcoin node split it into multiple .dat (Data) files.
  • .dat file has the information of a block in a binary format. When you open the blk00000.dat file then you can see the hash of the genesis block.

What is block hash?

  • We will understand the block hash with the genesis block that we found in the blk00000.dat file.
f9beb4d91d0100000100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000

We will divide this hash into five parts:

  1. Magic Byte: It tells you the starting and ending position of each block. f9beb4d9
  2. Size of upcoming blocks: It tells about the size of a block that helps to identify the position. 1d010000
  3. Block Header: It has a summary of the data in the block. It is the combination of six things.

0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c

  • Version (4 bytes): Version of this block.
  • Previous Block (32 bytes): It is the hash of the Last block of the blockchain. It is the result of Twice SHA256.
  • Merkle Root (32 bytes): hash of all the transaction summaries.
  • Time (4 bytes): Unix time when mining the block.
  • Bits (4 bytes): A shortened version of the target
  • Nonce (4 bytes): Number Only Used Once. When the miner achieves the target using change the order (nonce).

4. Transaction Count: This means how many a number of transaction exist in this block.

// transaction count 
01
in this genesis block only one tranaction happened.

5. Transaction Data: Store the information of transactions. the rest hash will be the transaction data.

01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000

Block cannot be downloaded by order like (1, 2, 3, 4, 5..) but it is a download by a parallel like (1, 2, 5, 3, 4, 9 …) and the reason is that it is download in an async way and it cannot be stuck in a particular order.

How to calculate block size for block hash?

  • We use genesis block hash to understand the concept. We have seen the hash of size of the block in the above topic that is 1d010000

So there are two steps to identify the size.

  1. Convert this hash to (Swap Endian) Format
endian(1d010000) ==== 0000011d

2. Convert the result into decimal format

HexToDec(0000011d) === 285So 285 bytes data in the block and we have to add magic-bytes (8 bytes) 285+8 = 293 bytes

The maximum block size should be 128 MB. you can verify from here

static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB

What happened If we remove the blocks folder?

  • In that case, you have to run the bitcoin node again because the bitcoin node firstly syncs with the near node and downloads the data into the blocks folder in the blk.dat file.
  • So don’t try to remove this directory. It will cost you to take the time.

Bitcoin is a Big Ocean, It Can Be Learned Slowly.

So stay tuned with me and join me to learn the blockchain development series every Monday at 9 AM. You can share your thought as well. I am happy to read your words respectfully.

Arnish Gupta

Signing off.

--

--

Arnish Gupta

Learn Blockchain with me every Monday at 9AM (GMT+5:30).