Bitcoin LevelDB
Reading Bitcoin Core’s block index and chainstate directly from disk
Updated November 2026. The first version of this page reproduced, without attribution, an answer by Pieter Wuille on Bitcoin Stack Exchange and a parser posted under the same question. Both are published there under CC BY-SA 4.0. This version is rewritten in our own words from the Bitcoin Core source (release v28.1) and links to those answers instead. It also corrects the record layout and the varint encoder, and adds the block-file XOR key of Bitcoin Core 28.0.
Further reading: Pieter Wuille’s answer to “What are the keys used in the blockchain levelDB?”, and the other answers to that question (Bitcoin Stack Exchange, CC BY-SA 4.0).
Two databases and a pile of files
Bitcoin Core keeps two LevelDB key-value databases next to its raw block files:
blocks/index/, the block index: one record per known block header, saying where the block and its undo data are on disk;chainstate/, the UTXO set of the current best chain;blocks/blkNNNNN.datandblocks/revNNNNN.dat: the blocks themselves, in network format, and their undo data, appended one after another. These are plain files, not a database.
The optional transaction index has lived in its own database, indexes/txindex/, since Bitcoin Core 0.17. LevelDB does not allow two programs to open a database at once, so copy it, or stop the node, before reading. For most questions the RPC calls getblock, gettxout and gettxoutsetinfo are simpler.
Keys in the block index
| Key | Value |
|---|---|
'b' + 32-byte block hash | the block index record (below) |
'f' + 4-byte file number | statistics of one block file: block count, file sizes, lowest and highest height and time |
'l' | the number of the last block file used |
'R' | set while a reindex is in progress |
'F' + flag name | a named boolean flag |
Keys in the chainstate
| Key | Value |
|---|---|
'C' + txid + output index (varint) | one unspent output: height, whether it is a coinbase output, amount and script (from Bitcoin Core 0.15) |
'c' + txid | the older per-transaction format (before 0.15); Bitcoin Core v28.1 refuses a chainstate that still holds it and asks for -reindex-chainstate |
'B' | the hash of the block up to which the UTXO set is complete |
'H' | the head blocks while a flush is interrupted |
The chainstate values are XOR-obfuscated with a key stored in the same database (key 0x0e 0x00 "obfuscate_key"). XOR each value with the key, repeated, before decoding it.
The block index record
A 'b' record is a run of varints followed by the 80-byte block header: client version, height, status flags, transaction count; then, only if the status says the data is there, the file number and the byte offsets of the block and its undo data; then the header (version, previous hash, merkle root, time, bits, nonce). Bitcoin Core’s varint is not the common LEB128: it is big-endian base 128, and each continuation byte also adds one, so every number has exactly one encoding.
import hashlib
def read_varint(buf, i):
"""Bitcoin Core VARINT (serialize.h ReadVarInt): MSB base-128 with an offset."""
n = 0
while True:
b = buf[i]; i += 1
n = (n << 7) | (b & 0x7F)
if b & 0x80:
n += 1
else:
return n, i
def write_varint(n):
"""Inverse of read_varint (serialize.h WriteVarInt)."""
out = [n & 0x7F]
while n > 0x7F:
n = (n >> 7) - 1
out.append((n & 0x7F) | 0x80)
return bytes(reversed(out))
BLOCK_HAVE_DATA, BLOCK_HAVE_UNDO = 8, 16
def parse_block_index_value(value):
"""Value of a 'b' + block-hash key in blocks/index (chain.h CDiskBlockIndex)."""
i, rec = 0, {}
for field in ('client_version', 'height', 'status', 'tx_count'):
rec[field], i = read_varint(value, i)
if rec['status'] & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO):
rec['file'], i = read_varint(value, i)
if rec['status'] & BLOCK_HAVE_DATA:
rec['data_pos'], i = read_varint(value, i)
if rec['status'] & BLOCK_HAVE_UNDO:
rec['undo_pos'], i = read_varint(value, i)
header = value[i:i + 80]
rec['version'] = int.from_bytes(header[0:4], 'little')
rec['prev_hash'] = header[4:36][::-1].hex()
rec['merkle_root'] = header[36:68][::-1].hex()
rec['time'] = int.from_bytes(header[68:72], 'little')
rec['bits'] = hex(int.from_bytes(header[72:76], 'little'))
rec['nonce'] = int.from_bytes(header[76:80], 'little')
rec['block_hash'] = hashlib.sha256(hashlib.sha256(header).digest()).digest()[::-1].hex()
return rec
Fed the value of the record for block 80,000, parse_block_index_value returns height 80000, two transactions, file 0, bits = 0x1b5bede6, and the block hash 000000000043a8c0fd1d6f726790caa2a406010d19efd2780db27bdbbd93baf6, which it recomputes from the header. Hashes are stored in little-endian byte order, so they are reversed for display.
Reading the block files directly
Each block in a blk*.dat file is stored as the network magic (4 bytes), the block size (4 bytes, little-endian), then the block. Since Bitcoin Core 28.0, a node whose blocks directory is created fresh XORs these files with an 8-byte random key kept in blocks/xor.dat (option -blocksxor). Older software cannot read such files until it applies the key. A blocks directory created by an earlier version keeps an all-zero key.
The block index holds headers, file positions and a little status data only; the blocks themselves are plain append-only files. That is what makes direct, daemon-free reads possible. The Real difficulty charts on this site were made this way: we read blocks/blk00000.dat from a full node’s disk and parsed the 80-byte headers in order (magic, size, header, then nBits and the double SHA-256 block hash) for the first 4,032 blocks, with no RPC. On a node set up with Bitcoin Core 28.0 or later, apply the key from blocks/xor.dat first. The same property underlies the V37 design work: a node can rebuild committed state from compact on-disk records and prove it with Merkle proofs of logarithmic size; see the Purple Paper summary, §13 (Verifying Payment).