<p>Yes, yes, yes(*), and no.</p>
<p>The blockchain-related on-disk data Bitcoin Core keeps generally fall into these three categories:</p>
<ul>
<li>The block data (flat <code>blocks/blk?????.dat</code> files with concatenated blocks).</li>
<li>The block index (LevelDB database <code>blocks/index/*</code>).</li>
<li>The UTXO set (LevelDB database <code>chainstate/*</code>).</li>
</ul>
<p>In this list, the later entries contain references to the previous ones. The chainstate contains the hash of the block that is currently considered &quot;active&quot; (i.e., the UTXO corresponds to it). This hash must exist in the block index at all times. Further, the block index stores in which block file and position within that file each block is stored.</p>
<p>To maintain consistency:</p>
<ul>
<li>Whenever block index writes to disk happen, the currently open block files are flushed. It is possible that bitcoind crashes during the block file write, which may leave a partially written block on disk, but since this block will not be written to the block index, after restart, it will be as if this block wasn't received at all; it will be redownloaded, and written to disk again (possibly in the same file byte range that the earlier attempt was written to as well).</li>
<li>Block index writes are done using a LevelDB batch(*), which either makes it entirely to disk, or not at all.</li>
<li>Whenever chainstate writes to disk happen, the block index is flushed.</li>
<li>When the chainstate is written to disk, and there is a large number of modified entries, the write is split up into multiple LevelDB batches, to reduce peak memory usage during writing. Before this happens, a &quot;flush in progress&quot; entry is written to the database, which is removed after the flush completes. If bitcoind crashes in the middle, the chainstate will be in an inconsistent state, but on restart the &quot;in progress&quot; record will be detected, and the relevant range of blocks will be processed and applied again to the UTXO set.</li>
</ul>
<p>(*) There have been reports of LevelDB itself causing corruption of its on-disk files if interrupted during a write, on some platforms.</p>

CC BY-SA 4.0
2024-01-23T19:51:01.190Z
2024-01-23T19:51:01.190Z
Pieter Wuille
<p>In particular, when a new block arrives:</p>
<ol>
<li><p>Is the block written to disk in an atomic fashion, so a new block either exists, or it doesn't?   (reader can never see a partially written state)</p>
</li>
<li><p>Are the summary databases (eg chainstate, block-index) written to, and flushed to disk in an atomic fashion along with the new block, such that the blockchain filestore will never be out-of-sync with the levelDB filestore(s)?</p>
</li>
<li><p>More generally, can the bitcoin datastores be said to exhibit ACID properties?</p>
</li>
<li><p>Are there any plans to change the datastore behavior in this regard?</p>
</li>
</ol>
<p>(I believe the answers to 1-3 are: no, no, and no.  But I'd like to hear more details, and also rationale for the status quo).</p>

<bitcoin-core><storage><leveldb><blockchain-data-storage>
Does Bitcoin Core write blockchain and chainstate data atomically?
user49396
# Does Bitcoin Core write blockchain and chainstate data atomically?

(Asked by: username_0 on 2024-01-22)

In particular, when a new block arrives:

1. Is the block written to disk in an atomic fashion, so a new block either exists, or it doesn't? (reader can never see a partially written state)
2. Are the summary databases (eg chainstate, block-index) written to, and flushed to disk in an atomic fashion along with the new block, such that the blockchain filestore will never be out-of-sync with the levelDB filestore(s)?
3. More generally, can the bitcoin datastores be said to exhibit ACID properties?
4. Are there any plans to change the datastore behavior in this regard?

(I believe the answers to 1-3 are: no, no, and no. But I'd like to hear more details, and also rationale for the status quo).

**Tags:** bitcoin-core, storage, leveldb, blockchain-data-storage


---

## 1 answers

### Answer by username_1 on 2024-01-23. Score: 2 | Accepted answer

Yes, yes, yes(\*), and no.

The blockchain-related on-disk data Bitcoin Core keeps generally fall into these three categories:

* The block data (flat `blocks/blk?????.dat` files with concatenated blocks).
* The block index (LevelDB database `blocks/index/*`).
* The UTXO set (LevelDB database `chainstate/*`).

In this list, the later entries contain references to the previous ones. The chainstate contains the hash of the block that is currently considered "active" (i.e., the UTXO corresponds to it). This hash must exist in the block index at all times. Further, the block index stores in which block file and position within that file each block is stored.

To maintain consistency:

* Whenever block index writes to disk happen, the currently open block files are flushed. It is possible that bitcoind crashes during the block file write, which may leave a partially written block on disk, but since this block will not be written to the block index, after restart, it will be as if this block wasn't received at all; it will be redownloaded, and written to disk again (possibly in the same file byte range that the earlier attempt was written to as well).
* Block index writes are done using a LevelDB batch(\*), which either makes it entirely to disk, or not at all.
* Whenever chainstate writes to disk happen, the block index is flushed.
* When the chainstate is written to disk, and there is a large number of modified entries, the write is split up into multiple LevelDB batches, to reduce peak memory usage during writing. Before this happens, a "flush in progress" entry is written to the database, which is removed after the flush completes. If bitcoind crashes in the middle, the chainstate will be in an inconsistent state, but on restart the "in progress" record will be detected, and the relevant range of blocks will be processed and applied again to the UTXO set.

(\*) There have been reports of LevelDB itself causing corruption of its on-disk files if interrupted during a write, on some platforms.


---

