Bitcoin ABC  0.29.6
P2P Digital Currency
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
6 #ifndef BITCOIN_TXDB_H
7 #define BITCOIN_TXDB_H
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>
17 #include <cstddef>
18 #include <cstdint>
19 #include <functional>
20 #include <memory>
21 #include <optional>
22 #include <string>
23 #include <utility>
24 #include <vector>
26 struct BlockHash;
27 class CBlockFileInfo;
28 class CBlockIndex;
29 class COutPoint;
31 namespace Consensus {
32 struct Params;
33 };
36 static constexpr int64_t MIN_DB_CACHE_MB = 4;
38 static constexpr int64_t MAX_DB_CACHE_MB = sizeof(void *) > 4 ? 16384 : 1024;
40 static constexpr int64_t DEFAULT_DB_CACHE_MB = 1024;
42 static constexpr int64_t DEFAULT_DB_BATCH_SIZE = 16 << 20;
44 static 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 //
49 static constexpr int64_t MAX_TX_INDEX_CACHE_MB = 1024;
51 static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB = 1024;
53 static constexpr int64_t MAX_COINS_DB_CACHE_MB = 8;
62 };
65 class CCoinsViewDB final : public CCoinsView {
66 protected:
69  std::unique_ptr<CDBWrapper> m_db;
71 public:
72  explicit CCoinsViewDB(DBParams db_params, CoinsViewOptions options);
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;
84  bool Upgrade();
85  size_t EstimateSize() const override;
88  void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
92  std::optional<fs::path> StoragePath() { return m_db->StoragePath(); }
93 };
97 public:
100  bool GetKey(COutPoint &key) const override;
101  bool GetValue(Coin &coin) const override;
102  unsigned int GetValueSize() const override;
104  bool Valid() const override;
105  void Next() override;
107 private:
108  CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
109  : CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
110  std::unique_ptr<CDBIterator> pcursor;
111  std::pair<char, COutPoint> keyTmp;
113  friend class CCoinsViewDB;
114 };
117 class CBlockTreeDB : public CDBWrapper {
118 public:
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  ;
137  bool Upgrade();
138 };
140 [[nodiscard]] util::Result<void>
141 CheckLegacyTxindex(CBlockTreeDB &block_tree_db);
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 WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * >> &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
Definition: txdb.cpp:262
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 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
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
std::optional< fs::path > StoragePath()
Definition: txdb.h:92
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
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:120
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
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
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