Bitcoin ABC 0.32.4
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/caches.h>
14#include <kernel/cs_main.h>
15#include <util/fs.h>
16#include <util/result.h>
17
18#include <cstddef>
19#include <cstdint>
20#include <functional>
21#include <memory>
22#include <optional>
23#include <string>
24#include <utility>
25#include <vector>
26
27struct BlockHash;
28class CBlockFileInfo;
29class CBlockIndex;
30class COutPoint;
31
32namespace Consensus {
33struct Params;
34};
35namespace util {
36class SignalInterrupt;
37} // namespace util
38
46};
47
49class CCoinsViewDB final : public CCoinsView {
50protected:
53 std::unique_ptr<CDBWrapper> m_db;
54
55public:
56 explicit CCoinsViewDB(DBParams db_params, CoinsViewOptions options);
57
58 bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
59 bool HaveCoin(const COutPoint &outpoint) const override;
60 BlockHash GetBestBlock() const override;
61 std::vector<BlockHash> GetHeadBlocks() const override;
63 const BlockHash &hashBlock) override;
64 CCoinsViewCursor *Cursor() const override;
65
67 bool NeedsUpgrade();
68 size_t EstimateSize() const override;
69
71 void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
72
75 std::optional<fs::path> StoragePath() { return m_db->StoragePath(); }
76};
77
80public:
82
83 bool GetKey(COutPoint &key) const override;
84 bool GetValue(Coin &coin) const override;
85 unsigned int GetValueSize() const override;
86
87 bool Valid() const override;
88 void Next() override;
89
90private:
91 CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
92 : CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
93 std::unique_ptr<CDBIterator> pcursor;
94 std::pair<char, COutPoint> keyTmp;
95
96 friend class CCoinsViewDB;
97};
98
100class CBlockTreeDB : public CDBWrapper {
101public:
103 bool WriteBatchSync(
104 const std::vector<std::pair<int, const CBlockFileInfo *>> &fileInfo,
105 int nLastFile, const std::vector<const CBlockIndex *> &blockinfo);
106 bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
107 bool ReadLastBlockFile(int &nFile);
108 bool WriteReindexing(bool fReindexing);
109 bool IsReindexing() const;
110 bool WriteFlag(const std::string &name, bool fValue);
111 bool ReadFlag(const std::string &name, bool &fValue);
113 const Consensus::Params &params,
114 std::function<CBlockIndex *(const BlockHash &)> insertBlockIndex,
115 const util::SignalInterrupt &interrupt)
117 ;
118
121 bool Upgrade();
122};
123
124[[nodiscard]] util::Result<void>
125CheckLegacyTxindex(CBlockTreeDB &block_tree_db);
126
127#endif // BITCOIN_TXDB_H
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:21
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:100
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:194
bool Upgrade()
Attempt to update from an older database format.
Definition: txdb.cpp:367
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:198
bool IsReindexing() const
Definition: txdb.cpp:206
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * > > &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
Definition: txdb.cpp:268
bool LoadBlockIndexGuts(const Consensus::Params &params, std::function< CBlockIndex *(const BlockHash &)> insertBlockIndex, const util::SignalInterrupt &interrupt) EXCLUSIVE_LOCKS_REQUIRED(
Definition: txdb.h:112
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:292
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:210
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:287
Cursor for iterating over CoinsView state.
Definition: coins.h:222
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:79
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:93
bool GetKey(COutPoint &key) const override
Definition: txdb.cpp:235
~CCoinsViewDBCursor()=default
bool GetValue(Coin &coin) const override
Definition: txdb.cpp:244
bool Valid() const override
Definition: txdb.cpp:252
CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
Definition: txdb.h:91
unsigned int GetValueSize() const override
Definition: txdb.cpp:248
void Next() override
Definition: txdb.cpp:256
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:94
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:49
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:109
std::vector< BlockHash > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:117
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:101
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:105
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:53
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:214
CCoinsViewDB(DBParams db_params, CoinsViewOptions options)
Definition: txdb.cpp:84
bool BatchWrite(CoinsViewCacheCursor &cursor, const BlockHash &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:125
std::optional< fs::path > StoragePath()
Definition: txdb.h:75
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:88
CoinsViewOptions m_options
Definition: txdb.h:52
bool NeedsUpgrade()
Whether an unsupported database format is used.
Definition: txdb.cpp:60
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:190
DBParams m_db_params
Definition: txdb.h:51
Abstract view on the open txout dataset.
Definition: coins.h:305
CDBWrapper(const DBParams &params)
Definition: dbwrapper.cpp:121
A UTXO entry.
Definition: coins.h:29
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
static constexpr size_t DEFAULT_DB_CACHE_BATCH
Default LevelDB write batch size.
Definition: caches.h:16
const char * name
Definition: rest.cpp:47
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
Definition: coins.h:255
User-controlled performance and debug options.
Definition: txdb.h:40
int simulate_crash_ratio
If non-zero, randomly exit when the database is flushed with (1/ratio) probability.
Definition: txdb.h:45
size_t batch_write_bytes
Maximum database write batch size in bytes.
Definition: txdb.h:42
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
util::Result< void > CheckLegacyTxindex(CBlockTreeDB &block_tree_db)
Definition: txdb.cpp:36