Inside Bitcoin
Opening the selected file…
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.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_TXDB_H7#define BITCOIN_TXDB_H9#include <coins.h>10#include <dbwrapper.h>11#include <kernel/caches.h>12#include <kernel/cs_main.h>13#include <sync.h>14#include <util/fs.h>16#include <cstddef>17#include <cstdint>18#include <future>19#include <memory>20#include <optional>21#include <string>22#include <vector>24class COutPoint;25class uint256;27//! User-controlled performance and debug options.28struct CoinsViewOptions {29//! Maximum database write batch size in bytes.30uint64_t batch_write_bytes{DEFAULT_DB_CACHE_BATCH};31//! If non-zero, randomly exit when the database is flushed with (1/ratio) probability.32int simulate_crash_ratio{0};33};35/** CCoinsView backed by the coin database (chainstate/) */36class CCoinsViewDB final : public CCoinsView37{38protected:39DBParams m_db_params;40CoinsViewOptions m_options;41//! Prevents CompactFull() from using m_db while ResizeCache() replaces it.42Mutex m_db_mutex;43std::unique_ptr<CDBWrapper> m_db;44std::shared_future<void> m_compaction;45public:46explicit CCoinsViewDB(DBParams db_params, CoinsViewOptions options);47~CCoinsViewDB() override;49std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;50std::optional<Coin> PeekCoin(const COutPoint& outpoint) const override;51bool HaveCoin(const COutPoint& outpoint) const override;52uint256 GetBestBlock() const override;53std::vector<uint256> GetHeadBlocks() const override;54void BatchWrite(CoinsViewCacheCursor& cursor, const uint256& block_hash) override;55//! Get a cursor to iterate over the whole state.56std::unique_ptr<CCoinsViewCursor> Cursor() const;58//! Whether an unsupported database format is used.59bool NeedsUpgrade();60size_t EstimateSize() const override;62//! Dynamically alter the underlying leveldb cache size.63void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_db_mutex);65//! Perform a full compaction of the underlying LevelDB on a one-shot background thread.66std::shared_future<void> CompactFullAsync() EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_db_mutex);68//! Return an underlying LevelDB property value, if available.69std::optional<std::string> GetDBProperty(const std::string& property);70};72#endif // BITCOIN_TXDB_H