Bitcoin ABC 0.30.7
P2P Digital Currency
txdb.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BITCOIN_TXDB_H
7#define BITCOIN_TXDB_H
8
9#include <blockfileinfo.h>
10#include <coins.h>
11#include <dbwrapper.h>
12#include <flatfile.h>
13#include <kernel/cs_main.h>
14#include <util/fs.h>
15#include <util/result.h>
16
17#include <cstddef>
18#include <cstdint>
19#include <functional>
20#include <memory>
21#include <optional>
22#include <string>
23#include <utility>
24#include <vector>
25
26struct BlockHash;
27class CBlockFileInfo;
28class CBlockIndex;
29class COutPoint;
30
31namespace Consensus {
32struct Params;
33};
34
36static constexpr int64_t MIN_DB_CACHE_MB = 4;
38static constexpr int64_t MAX_DB_CACHE_MB = sizeof(void *) > 4 ? 16384 : 1024;
40static constexpr int64_t DEFAULT_DB_CACHE_MB = 1024;
42static constexpr int64_t DEFAULT_DB_BATCH_SIZE = 16 << 20;
44static constexpr int64_t MAX_BLOCK_DB_CACHE_MB = 2;
46// Unlike for the UTXO database, for the txindex scenario the leveldb cache make
47// a meaningful difference:
48// https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
49static constexpr int64_t MAX_TX_INDEX_CACHE_MB = 1024;
51static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB = 1024;
53static constexpr int64_t MAX_COINS_DB_CACHE_MB = 8;
54
62};
63
65class CCoinsViewDB final : public CCoinsView {
66protected:
69 std::unique_ptr<CDBWrapper> m_db;
70
71public:
72 explicit CCoinsViewDB(DBParams db_params, CoinsViewOptions options);
73
74 bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
75 bool HaveCoin(const COutPoint &outpoint) const override;
76 BlockHash GetBestBlock() const override;
77 std::vector<BlockHash> GetHeadBlocks() const override;
78 bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock,
79 bool erase = true) override;
80 CCoinsViewCursor *Cursor() const override;
81
84 bool Upgrade();
85 size_t EstimateSize() const override;
86
88 void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
89
92 std::optional<fs::path> StoragePath() { return m_db->StoragePath(); }
93};
94
97public:
99
100 bool GetKey(COutPoint &key) const override;
101 bool GetValue(Coin &coin) const override;
102 unsigned int GetValueSize() const override;
103
104 bool Valid() const override;
105 void Next() override;
106
107private:
108 CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
109 : CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
110 std::unique_ptr<CDBIterator> pcursor;
111 std::pair<char, COutPoint> keyTmp;
112
113 friend class CCoinsViewDB;
114};
115
117class CBlockTreeDB : public CDBWrapper {
118public:
120 bool WriteBatchSync(
121 const std::vector<std::pair<int, const CBlockFileInfo *>> &fileInfo,
122 int nLastFile, const std::vector<const CBlockIndex *> &blockinfo);
123 bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
124 bool ReadLastBlockFile(int &nFile);
125 bool WriteReindexing(bool fReindexing);
126 bool IsReindexing() const;
127 bool WriteFlag(const std::string &name, bool fValue);
128 bool ReadFlag(const std::string &name, bool &fValue);
130 const Consensus::Params &params,
131 std::function<CBlockIndex *(const BlockHash &)> insertBlockIndex)
133 ;
134
137 bool Upgrade();
138};
139
140[[nodiscard]] util::Result<void>
141CheckLegacyTxindex(CBlockTreeDB &block_tree_db);
142
143#endif // BITCOIN_TXDB_H
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:19
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
Access to the block database (blocks/index/)
Definition: txdb.h:117
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:188
bool Upgrade()
Attempt to update from an older database format.
Definition: txdb.cpp:487
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:192
bool IsReindexing() const
Definition: txdb.cpp:200
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * > > &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
Definition: txdb.cpp:262
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:286
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:204
bool LoadBlockIndexGuts(const Consensus::Params &params, std::function< CBlockIndex *(const BlockHash &)> insertBlockIndex) EXCLUSIVE_LOCKS_REQUIRED(
Definition: txdb.h:129
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:281
Cursor for iterating over CoinsView state.
Definition: coins.h:143
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:96
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:110
bool GetKey(COutPoint &key) const override
Definition: txdb.cpp:229
bool GetValue(Coin &coin) const override
Definition: txdb.cpp:238
~CCoinsViewDBCursor()
Definition: txdb.h:98
bool Valid() const override
Definition: txdb.cpp:246
CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
Definition: txdb.h:108
unsigned int GetValueSize() const override
Definition: txdb.cpp:242
void Next() override
Definition: txdb.cpp:250
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:111
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:65
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:103
std::vector< BlockHash > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:111
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:95
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:99
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:69
bool Upgrade()
Attempt to update from an older database format.
Definition: txdb.cpp:416
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:208
CCoinsViewDB(DBParams db_params, CoinsViewOptions options)
Definition: txdb.cpp:78
bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock, bool erase=true) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:119
std::optional< fs::path > StoragePath()
Definition: txdb.h:92
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:82
CoinsViewOptions m_options
Definition: txdb.h:68
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:184
DBParams m_db_params
Definition: txdb.h:67
Abstract view on the open txout dataset.
Definition: coins.h:163
CDBWrapper(const DBParams &params)
Definition: dbwrapper.cpp:121
A UTXO entry.
Definition: coins.h:28
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher, std::equal_to< COutPoint >, PoolAllocator< std::pair< const COutPoint, CCoinsCacheEntry >, sizeof(std::pair< const COutPoint, CCoinsCacheEntry >)+sizeof(void *) *4 > > CCoinsMap
PoolAllocator's MAX_BLOCK_SIZE_BYTES parameter here uses sizeof the data, and adds the size of 4 poin...
Definition: coins.h:138
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
const char * name
Definition: rest.cpp:47
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
User-controlled performance and debug options.
Definition: txdb.h:56
int simulate_crash_ratio
If non-zero, randomly exit when the database is flushed with (1/ratio) probability.
Definition: txdb.h:61
size_t batch_write_bytes
Maximum database write batch size in bytes.
Definition: txdb.h:58
Parameters that influence chain consensus.
Definition: params.h:34
Application-specific storage settings.
Definition: dbwrapper.h:32
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
static constexpr int64_t MAX_TX_INDEX_CACHE_MB
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:49
static constexpr int64_t MAX_DB_CACHE_MB
max. -dbcache (MiB)
Definition: txdb.h:38
static constexpr int64_t MAX_BLOCK_DB_CACHE_MB
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:44
static constexpr int64_t MIN_DB_CACHE_MB
min. -dbcache (MiB)
Definition: txdb.h:36
util::Result< void > CheckLegacyTxindex(CBlockTreeDB &block_tree_db)
Definition: txdb.cpp:37
static constexpr int64_t MAX_COINS_DB_CACHE_MB
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:53
static constexpr int64_t DEFAULT_DB_BATCH_SIZE
-dbbatchsize default (bytes)
Definition: txdb.h:42
static constexpr int64_t DEFAULT_DB_CACHE_MB
-dbcache default (MiB)
Definition: txdb.h:40
static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:51