Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1leveldb File format2===================4<beginning_of_file>5[data block 1]6[data block 2]7...8[data block N]9[meta block 1]10...11[meta block K]12[metaindex block]13[index block]14[Footer] (fixed size; starts at file_size - sizeof(Footer))15<end_of_file>17The file contains internal pointers. Each such pointer is called18a BlockHandle and contains the following information:20offset: varint6421size: varint6423See [varints](https://developers.google.com/protocol-buffers/docs/encoding#varints)24for an explanation of varint64 format.261. The sequence of key/value pairs in the file are stored in sorted27order and partitioned into a sequence of data blocks. These blocks28come one after another at the beginning of the file. Each data block29is formatted according to the code in `block_builder.cc`, and then30optionally compressed.322. After the data blocks we store a bunch of meta blocks. The33supported meta block types are described below. More meta block types34may be added in the future. Each meta block is again formatted using35`block_builder.cc` and then optionally compressed.373. A "metaindex" block. It contains one entry for every other meta38block where the key is the name of the meta block and the value is a39BlockHandle pointing to that meta block.414. An "index" block. This block contains one entry per data block,42where the key is a string >= last key in that data block and before43the first key in the successive data block. The value is the44BlockHandle for the data block.465. At the very end of the file is a fixed length footer that contains47the BlockHandle of the metaindex and index blocks as well as a magic number.49metaindex_handle: char[p]; // Block handle for metaindex50index_handle: char[q]; // Block handle for index51padding: char[40-p-q];// zeroed bytes to make fixed length52// (40==2*BlockHandle::kMaxEncodedLength)53magic: fixed64; // == 0xdb4775248b80fb57 (little-endian)55## "filter" Meta Block57If a `FilterPolicy` was specified when the database was opened, a58filter block is stored in each table. The "metaindex" block contains59an entry that maps from `filter.<N>` to the BlockHandle for the filter60block where `<N>` is the string returned by the filter policy's61`Name()` method.63The filter block stores a sequence of filters, where filter i contains64the output of `FilterPolicy::CreateFilter()` on all keys that are stored65in a block whose file offset falls within the range67[ i*base ... (i+1)*base-1 ]69Currently, "base" is 2KB. So for example, if blocks X and Y start in70the range `[ 0KB .. 2KB-1 ]`, all of the keys in X and Y will be71converted to a filter by calling `FilterPolicy::CreateFilter()`, and the72resulting filter will be stored as the first filter in the filter73block.75The filter block is formatted as follows:77[filter 0]78[filter 1]79[filter 2]80...81[filter N-1]83[offset of filter 0] : 4 bytes84[offset of filter 1] : 4 bytes85[offset of filter 2] : 4 bytes86...87[offset of filter N-1] : 4 bytes89[offset of beginning of offset array] : 4 bytes90lg(base) : 1 byte92The offset array at the end of the filter block allows efficient93mapping from a data block offset to the corresponding filter.95## "stats" Meta Block97This meta block contains a bunch of stats. The key is the name98of the statistic. The value contains the statistic.100TODO(postrelease): record following stats.102data size103index size104key size (uncompressed)105value size (uncompressed)106number of entries107number of data blocks
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.