Bitcoin Core  22.99.0
P2P Digital Currency
txdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <coins.h>
10 #include <dbwrapper.h>
11 
12 #include <memory>
13 #include <optional>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 class CBlockFileInfo;
19 class CBlockIndex;
20 class uint256;
21 namespace Consensus {
22 struct Params;
23 };
24 struct bilingual_str;
25 
27 static const int64_t nDefaultDbCache = 450;
29 static const int64_t nDefaultDbBatchSize = 16 << 20;
31 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
33 static const int64_t nMinDbCache = 4;
35 static const int64_t nMaxBlockDBCache = 2;
37 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
38 // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
39 static const int64_t nMaxTxIndexCache = 1024;
41 static const int64_t max_filter_index_cache = 1024;
43 static const int64_t nMaxCoinsDBCache = 8;
44 
45 // Actually declared in validation.cpp; can't include because of circular dependency.
46 extern RecursiveMutex cs_main;
47 
49 class CCoinsViewDB final : public CCoinsView
50 {
51 protected:
52  std::unique_ptr<CDBWrapper> m_db;
55 public:
59  explicit CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe);
60 
61  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
62  bool HaveCoin(const COutPoint &outpoint) const override;
63  uint256 GetBestBlock() const override;
64  std::vector<uint256> GetHeadBlocks() const override;
65  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
66  std::unique_ptr<CCoinsViewCursor> Cursor() const override;
67 
69  bool Upgrade();
70  size_t EstimateSize() const override;
71 
73  void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
74 };
75 
77 class CBlockTreeDB : public CDBWrapper
78 {
79 public:
80  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
81 
82  bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
83  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
84  bool ReadLastBlockFile(int &nFile);
85  bool WriteReindexing(bool fReindexing);
86  void ReadReindexing(bool &fReindexing);
87  bool WriteFlag(const std::string &name, bool fValue);
88  bool ReadFlag(const std::string &name, bool &fValue);
89  bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
90 };
91 
92 std::optional<bilingual_str> CheckLegacyTxindex(CBlockTreeDB& block_tree_db);
93 
94 #endif // BITCOIN_TXDB_H
CCoinsViewDB::Upgrade
bool Upgrade()
Attempt to update from an older database format. Returns whether an error occurred.
Definition: txdb.cpp:399
CCoinsViewDB
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:49
dbwrapper.h
bilingual_str
Bilingual messages:
Definition: translation.h:16
nMaxDbCache
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:31
CCoinsViewDB::CCoinsViewDB
CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe)
Definition: txdb.cpp:65
CCoinsViewDB::Cursor
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:218
AnnotatedMixin< std::recursive_mutex >
CBlockTreeDB::ReadReindexing
void ReadReindexing(bool &fReindexing)
Definition: txdb.cpp:186
nMaxTxIndexCache
static const int64_t nMaxTxIndexCache
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:39
nDefaultDbCache
static const int64_t nDefaultDbCache
-dbcache default (MiB)
Definition: txdb.h:27
nDefaultDbBatchSize
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
Definition: txdb.h:29
CCoinsView
Abstract view on the open txout dataset.
Definition: coins.h:157
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:70
CCoinsViewDB::ResizeCache
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:70
nMinDbCache
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:33
Consensus
Transaction validation functions.
Definition: params.h:12
Coin
A UTXO entry.
Definition: coins.h:30
CBlockTreeDB::WriteBatchSync
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * > > &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
Definition: txdb.cpp:273
CCoinsViewDB::EstimateSize
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:167
fs::path
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:33
max_filter_index_cache
static const int64_t max_filter_index_cache
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:41
CheckLegacyTxindex
std::optional< bilingual_str > CheckLegacyTxindex(CBlockTreeDB &block_tree_db)
Definition: txdb.cpp:35
CCoinsViewDB::HaveCoin
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:87
CBlockTreeDB::ReadBlockFileInfo
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:175
CBlockTreeDB::WriteReindexing
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:179
nMaxCoinsDBCache
static const int64_t nMaxCoinsDBCache
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:43
CBlockTreeDB
Access to the block database (blocks/index/)
Definition: txdb.h:77
uint256
256-bit opaque blob.
Definition: uint256.h:124
CBlockTreeDB::ReadFlag
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:289
coins.h
name
const char * name
Definition: rest.cpp:43
CDBWrapper
Definition: dbwrapper.h:176
CBlockTreeDB::WriteFlag
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:285
CBlockTreeDB::LoadBlockIndexGuts
bool LoadBlockIndexGuts(const Consensus::Params &consensusParams, std::function< CBlockIndex *(const uint256 &)> insertBlockIndex)
Definition: txdb.cpp:297
CCoinsViewDB::BatchWrite
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:106
CCoinsViewDB::GetBestBlock
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:91
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CBlockFileInfo
Definition: chain.h:40
CCoinsViewDB::m_is_memory
bool m_is_memory
Definition: txdb.h:54
CCoinsMap
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:134
CBlockTreeDB::CBlockTreeDB
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:172
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:561
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
CCoinsViewDB::GetCoin
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:83
nMaxBlockDBCache
static const int64_t nMaxBlockDBCache
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:35
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:145
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:118
CCoinsViewDB::m_ldb_path
fs::path m_ldb_path
Definition: txdb.h:53
CCoinsViewDB::m_db
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:52
CCoinsViewDB::GetHeadBlocks
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:98
CBlockTreeDB::ReadLastBlockFile
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:190