Bitcoin ABC  0.24.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 <primitives/block.h>
14 
15 #include <memory>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 struct BlockHash;
21 class CBlockIndex;
22 class CCoinsViewDBCursor;
23 
24 namespace Consensus {
25 struct Params;
26 }
27 
29 static constexpr int64_t MIN_DB_CACHE_MB = 4;
31 static constexpr int64_t MAX_DB_CACHE_MB = sizeof(void *) > 4 ? 16384 : 1024;
33 static constexpr int64_t DEFAULT_DB_CACHE_MB = 1024;
35 static constexpr int64_t DEFAULT_DB_BATCH_SIZE = 16 << 20;
37 static constexpr int64_t MAX_BLOCK_DB_CACHE_MB = 2;
39 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
40 // a meaningful difference:
41 // https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
42 static constexpr int64_t MAX_TX_INDEX_CACHE_MB = 1024;
44 static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB = 1024;
46 static constexpr int64_t MAX_COINS_DB_CACHE_MB = 8;
47 
48 // Actually declared in validation.cpp; can't include because of circular
49 // dependency.
50 extern RecursiveMutex cs_main;
51 
53 class CCoinsViewDB final : public CCoinsView {
54 protected:
55  std::unique_ptr<CDBWrapper> m_db;
56  fs::path m_ldb_path;
58 
59 public:
64  explicit CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory,
65  bool fWipe);
66 
67  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
68  bool HaveCoin(const COutPoint &outpoint) const override;
69  BlockHash GetBestBlock() const override;
70  std::vector<BlockHash> GetHeadBlocks() const override;
71  bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override;
72  CCoinsViewCursor *Cursor() const override;
73 
76  bool Upgrade();
77  size_t EstimateSize() const override;
78 
80  void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
81 };
82 
85 public:
87 
88  bool GetKey(COutPoint &key) const override;
89  bool GetValue(Coin &coin) const override;
90  unsigned int GetValueSize() const override;
91 
92  bool Valid() const override;
93  void Next() override;
94 
95 private:
96  CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
97  : CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
98  std::unique_ptr<CDBIterator> pcursor;
99  std::pair<char, COutPoint> keyTmp;
100 
101  friend class CCoinsViewDB;
102 };
103 
105 class CBlockTreeDB : public CDBWrapper {
106 public:
107  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false,
108  bool fWipe = false);
109 
110  bool WriteBatchSync(
111  const std::vector<std::pair<int, const CBlockFileInfo *>> &fileInfo,
112  int nLastFile, const std::vector<const CBlockIndex *> &blockinfo);
113  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
114  bool ReadLastBlockFile(int &nFile);
115  bool WriteReindexing(bool fReindexing);
116  bool IsReindexing() const;
117  bool WriteFlag(const std::string &name, bool fValue);
118  bool ReadFlag(const std::string &name, bool &fValue);
119  bool LoadBlockIndexGuts(
120  const Consensus::Params &params,
121  std::function<CBlockIndex *(const BlockHash &)> insertBlockIndex);
122 
125  bool Upgrade(const Consensus::Params &params);
126 };
127 
128 #endif // BITCOIN_TXDB_H
CBlockTreeDB::IsReindexing
bool IsReindexing() const
Definition: txdb.cpp:176
MIN_DB_CACHE_MB
static constexpr int64_t MIN_DB_CACHE_MB
min. -dbcache (MiB)
Definition: txdb.h:29
CCoinsViewDB::Cursor
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:184
block.h
CCoinsViewDB::GetHeadBlocks
std::vector< BlockHash > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:80
CCoinsViewDB::Upgrade
bool Upgrade()
Attempt to update from an older database format.
Definition: txdb.cpp:389
CCoinsViewDB
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:53
dbwrapper.h
MAX_COINS_DB_CACHE_MB
static constexpr int64_t MAX_COINS_DB_CACHE_MB
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:46
MAX_TX_INDEX_CACHE_MB
static constexpr int64_t MAX_TX_INDEX_CACHE_MB
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:42
flatfile.h
CBlockTreeDB::Upgrade
bool Upgrade(const Consensus::Params &params)
Attempt to update from an older database format.
Definition: txdb.cpp:460
CCoinsViewDBCursor::GetKey
bool GetKey(COutPoint &key) const override
Definition: txdb.cpp:205
CCoinsViewDB::CCoinsViewDB
CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe)
Definition: txdb.cpp:50
DEFAULT_DB_CACHE_MB
static constexpr int64_t DEFAULT_DB_CACHE_MB
-dbcache default (MiB)
Definition: txdb.h:33
AnnotatedMixin< std::recursive_mutex >
CCoinsViewDBCursor
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:84
CBlockTreeDB::WriteBatchSync
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * >> &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
Definition: txdb.cpp:238
MAX_DB_CACHE_MB
static constexpr int64_t MAX_DB_CACHE_MB
max. -dbcache (MiB)
Definition: txdb.h:31
CCoinsView
Abstract view on the open txout dataset.
Definition: coins.h:175
CCoinsViewCursor
Cursor for iterating over CoinsView state.
Definition: coins.h:155
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:59
CCoinsViewDB::ResizeCache
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:56
MAX_FILTER_INDEX_CACHE_MB
static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:44
Consensus
Definition: blockdb.h:10
CCoinsViewDBCursor::GetValueSize
unsigned int GetValueSize() const override
Definition: txdb.cpp:218
Coin
A UTXO entry.
Definition: coins.h:27
CCoinsViewDB::EstimateSize
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:156
CCoinsViewDB::HaveCoin
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:68
CBlockTreeDB::ReadBlockFileInfo
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:164
CBlockTreeDB::WriteReindexing
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:168
CCoinsViewDBCursor::Valid
bool Valid() const override
Definition: txdb.cpp:222
CBlockTreeDB
Access to the block database (blocks/index/)
Definition: txdb.h:105
CCoinsViewDB::GetBestBlock
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:72
CCoinsViewDBCursor::GetValue
bool GetValue(Coin &coin) const override
Definition: txdb.cpp:214
CBlockTreeDB::LoadBlockIndexGuts
bool LoadBlockIndexGuts(const Consensus::Params &params, std::function< CBlockIndex *(const BlockHash &)> insertBlockIndex)
Definition: txdb.cpp:270
CBlockTreeDB::ReadFlag
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:261
coins.h
BlockHash
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
name
const char * name
Definition: rest.cpp:43
CDBWrapper
Definition: dbwrapper.h:171
CBlockTreeDB::WriteFlag
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:257
CCoinsViewDB::BatchWrite
bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:88
CCoinsViewDBCursor::keyTmp
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:99
CCoinsViewDBCursor::~CCoinsViewDBCursor
~CCoinsViewDBCursor()
Definition: txdb.h:86
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
CBlockFileInfo
Definition: blockfileinfo.h:13
CCoinsViewDBCursor::Next
void Next() override
Definition: txdb.cpp:226
CCoinsViewDB::m_is_memory
bool m_is_memory
Definition: txdb.h:57
CCoinsMap
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:152
CBlockTreeDB::CBlockTreeDB
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:160
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:508
DEFAULT_DB_BATCH_SIZE
static constexpr int64_t DEFAULT_DB_BATCH_SIZE
-dbbatchsize default (bytes)
Definition: txdb.h:35
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
MAX_BLOCK_DB_CACHE_MB
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:37
CCoinsViewDB::GetCoin
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:64
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23
CCoinsViewDBCursor::CCoinsViewDBCursor
CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
Definition: txdb.h:96
CCoinsViewDBCursor::pcursor
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:98
cs_main
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:103
CCoinsViewDB::m_ldb_path
fs::path m_ldb_path
Definition: txdb.h:56
CCoinsViewDB::m_db
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:55
blockfileinfo.h
CDBIterator
Definition: dbwrapper.h:115
CBlockTreeDB::ReadLastBlockFile
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:180