Bitcoin Core Chainstate Database: LevelDB, Coin Cache, and Persistence
Bitcoin Core uses a chainstate database to persist the current validated set of unspent transaction outputs. Unlike historical block storage, chainstate is optimized for answering whether a particular outpoint exists and what conditions govern its spending. It is a derived state that can be reconstructed from validated blockchain history.
Coins and outpoints
An outpoint consists of a transaction identifier and an output index. Its associated coin contains the transaction output and metadata such as creation height and whether it originated in a coinbase transaction. This information supports input validation, value accounting and coinbase maturity checks. The database does not need to retain every previously spent output.
Bitcoin Core uses LevelDB for persistent chainstate storage. Internal key encoding, value compression and database layout are implementation details rather than a public wallet API. Software that reads raw database records risks incompatibility with newer versions and may observe an inconsistent view if the node is writing concurrently.
In-memory caching and persistence
The coins cache reduces repeated disk lookups and accumulates changes while blocks are validated. Connecting a block consumes existing coins and creates new ones. Disconnecting a block reverses those changes using block and undo data. The node coordinates persistence with chainstate metadata so that the database can be associated with a known block.
The dbcache setting influences memory allocation for database caching. Increasing it can improve synchronization performance when adequate RAM is available, but it does not eliminate the need for disk storage or guarantee a particular synchronization time. Container memory limits and other processes must be considered when selecting a value.
Reading the state through RPC
The gettxout RPC queries a specific unspent output. Its include_mempool argument determines whether the mempool view is considered. A null result does not alone prove that an output never existed: it may already be spent or otherwise absent from the selected view.
The gettxoutsetinfo RPC provides statistics about the UTXO set. Depending on the release, options and enabled indexes, this operation can involve substantial work. Its results describe network-wide state, not the balance of a particular wallet. A wallet balance requires matching owned scripts and applying its own confirmation and spending rules.
Consistency and recovery
Record the active block hash when taking application checkpoints. Separate RPC calls can observe different tips if a block arrives between them. Applications should detect tip changes and retry or reconcile when consistency matters. Do not copy a live LevelDB directory as though it were an atomic backup. Use supported snapshot mechanisms or a clean shutdown for consistent filesystem backups.
Primary references
Bitcoin Core coin cache: https://github.com/bitcoin/bitcoin/blob/master/src/coins.h
Bitcoin Core database implementation: https://github.com/bitcoin/bitcoin/blob/master/src/txdb.cpp
