Hi how is blockchain actually stored on disk?

I noticed bitcoin uses key store database level DB but what is the structure?


Here I don't get it... Does anyone has simple overview how it is stored?

I have also read this... but don't get it. What are the keys used in the blockchain levelDB (ie what are the key:value pairs)?


You may mix up block data with the UTXO set (unspent transaction outputs). Bitcoin Core uses LevelDB for the later (the UTXO set).

Quick lookups for unspent transaction is important for performance.

The block data is a different things. If you have a non-pruned peer (normal full node mode), Core stores all block files so it can serve those blocks to other peers bootstrapping (and for chain reorganisations).

Those block files are not an API, use it with care. If you interested in parsing blocks, consider using the RPC or REST API.

The .blk files contain a bunch of blocks (raw network serialisation format). There is also a block index (a levelDB database) that contains the file offsets and the headers.

Learn more about the blocks format here (or google).

  • So If I wanted to try implement storing blockchain on filesystem, how would you suggest doing it? having file with indexes and each index would represent block location inside another file? – dtechlearn Dec 1 '18 at 23:10

Libbitcoin db uses memory mapped files(mmap). No data is deleted, only appended, as all public data is validated first before transferred to the store. All valid tx are permanently stored (no tx are removed from unconfirmed set). Each db (tx, block, history etc) includes a hashtable which maps the key to the respective file offset in the db file.

As a memory mapped file is just a pointer from the application point of view, the actual IO is hidden and optimized by the OS. This is one reason for the high query performance of Libbitcoin Server. The specific application of storing/indexing Bitcoin public data allows for a high performance specialized store vs a generalized one optimized for journaling/recoverability and other guarantees not needed for public Bitcoin data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.