Bitcoin Core 28.99.0
P2P Digital Currency
txdb.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2022 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#include <txdb.h>
7
8#include <coins.h>
9#include <dbwrapper.h>
10#include <logging.h>
12#include <random.h>
13#include <serialize.h>
14#include <uint256.h>
15#include <util/vector.h>
16
17#include <cassert>
18#include <cstdlib>
19#include <iterator>
20#include <utility>
21
22static constexpr uint8_t DB_COIN{'C'};
23static constexpr uint8_t DB_BEST_BLOCK{'B'};
24static constexpr uint8_t DB_HEAD_BLOCKS{'H'};
25// Keys used in previous version that might still be found in the DB:
26static constexpr uint8_t DB_COINS{'c'};
27
29{
30 std::unique_ptr<CDBIterator> cursor{m_db->NewIterator()};
31 // DB_COINS was deprecated in v0.15.0, commit
32 // 1088b02f0ccd7358d2b7076bb9e122d59d502d02
33 cursor->Seek(std::make_pair(DB_COINS, uint256{}));
34 return cursor->Valid();
35}
36
37namespace {
38
39struct CoinEntry {
40 COutPoint* outpoint;
41 uint8_t key;
42 explicit CoinEntry(const COutPoint* ptr) : outpoint(const_cast<COutPoint*>(ptr)), key(DB_COIN) {}
43
44 SERIALIZE_METHODS(CoinEntry, obj) { READWRITE(obj.key, obj.outpoint->hash, VARINT(obj.outpoint->n)); }
45};
46
47} // namespace
48
50 m_db_params{std::move(db_params)},
51 m_options{std::move(options)},
52 m_db{std::make_unique<CDBWrapper>(m_db_params)} { }
53
54void CCoinsViewDB::ResizeCache(size_t new_cache_size)
55{
56 // We can't do this operation with an in-memory DB since we'll lose all the coins upon
57 // reset.
59 // Have to do a reset first to get the original `m_db` state to release its
60 // filesystem lock.
61 m_db.reset();
62 m_db_params.cache_bytes = new_cache_size;
63 m_db_params.wipe_data = false;
64 m_db = std::make_unique<CDBWrapper>(m_db_params);
65 }
66}
67
68std::optional<Coin> CCoinsViewDB::GetCoin(const COutPoint& outpoint) const
69{
70 if (Coin coin; m_db->Read(CoinEntry(&outpoint), coin)) return coin;
71 return std::nullopt;
72}
73
74bool CCoinsViewDB::HaveCoin(const COutPoint &outpoint) const {
75 return m_db->Exists(CoinEntry(&outpoint));
76}
77
79 uint256 hashBestChain;
80 if (!m_db->Read(DB_BEST_BLOCK, hashBestChain))
81 return uint256();
82 return hashBestChain;
83}
84
85std::vector<uint256> CCoinsViewDB::GetHeadBlocks() const {
86 std::vector<uint256> vhashHeadBlocks;
87 if (!m_db->Read(DB_HEAD_BLOCKS, vhashHeadBlocks)) {
88 return std::vector<uint256>();
89 }
90 return vhashHeadBlocks;
91}
92
93bool CCoinsViewDB::BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashBlock) {
94 CDBBatch batch(*m_db);
95 size_t count = 0;
96 size_t changed = 0;
97 assert(!hashBlock.IsNull());
98
99 uint256 old_tip = GetBestBlock();
100 if (old_tip.IsNull()) {
101 // We may be in the middle of replaying.
102 std::vector<uint256> old_heads = GetHeadBlocks();
103 if (old_heads.size() == 2) {
104 if (old_heads[0] != hashBlock) {
105 LogPrintLevel(BCLog::COINDB, BCLog::Level::Error, "The coins database detected an inconsistent state, likely due to a previous crash or shutdown. You will need to restart bitcoind with the -reindex-chainstate or -reindex configuration option.\n");
106 }
107 assert(old_heads[0] == hashBlock);
108 old_tip = old_heads[1];
109 }
110 }
111
112 // In the first batch, mark the database as being in the middle of a
113 // transition from old_tip to hashBlock.
114 // A vector is used for future extensibility, as we may want to support
115 // interrupting after partial writes from multiple independent reorgs.
116 batch.Erase(DB_BEST_BLOCK);
117 batch.Write(DB_HEAD_BLOCKS, Vector(hashBlock, old_tip));
118
119 for (auto it{cursor.Begin()}; it != cursor.End();) {
120 if (it->second.IsDirty()) {
121 CoinEntry entry(&it->first);
122 if (it->second.coin.IsSpent())
123 batch.Erase(entry);
124 else
125 batch.Write(entry, it->second.coin);
126 changed++;
127 }
128 count++;
129 it = cursor.NextAndMaybeErase(*it);
131 LogDebug(BCLog::COINDB, "Writing partial batch of %.2f MiB\n", batch.SizeEstimate() * (1.0 / 1048576.0));
132 m_db->WriteBatch(batch);
133 batch.Clear();
135 static FastRandomContext rng;
137 LogPrintf("Simulating a crash. Goodbye.\n");
138 _Exit(0);
139 }
140 }
141 }
142 }
143
144 // In the last batch, mark the database as consistent with hashBlock again.
145 batch.Erase(DB_HEAD_BLOCKS);
146 batch.Write(DB_BEST_BLOCK, hashBlock);
147
148 LogDebug(BCLog::COINDB, "Writing final batch of %.2f MiB\n", batch.SizeEstimate() * (1.0 / 1048576.0));
149 bool ret = m_db->WriteBatch(batch);
150 LogDebug(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
151 return ret;
152}
153
155{
156 return m_db->EstimateSize(DB_COIN, uint8_t(DB_COIN + 1));
157}
158
161{
162public:
163 // Prefer using CCoinsViewDB::Cursor() since we want to perform some
164 // cache warmup on instantiation.
165 CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256&hashBlockIn):
166 CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
168
169 bool GetKey(COutPoint &key) const override;
170 bool GetValue(Coin &coin) const override;
171
172 bool Valid() const override;
173 void Next() override;
174
175private:
176 std::unique_ptr<CDBIterator> pcursor;
177 std::pair<char, COutPoint> keyTmp;
178
179 friend class CCoinsViewDB;
180};
181
182std::unique_ptr<CCoinsViewCursor> CCoinsViewDB::Cursor() const
183{
184 auto i = std::make_unique<CCoinsViewDBCursor>(
185 const_cast<CDBWrapper&>(*m_db).NewIterator(), GetBestBlock());
186 /* It seems that there are no "const iterators" for LevelDB. Since we
187 only need read operations on it, use a const-cast to get around
188 that restriction. */
189 i->pcursor->Seek(DB_COIN);
190 // Cache key of first record
191 if (i->pcursor->Valid()) {
192 CoinEntry entry(&i->keyTmp.second);
193 i->pcursor->GetKey(entry);
194 i->keyTmp.first = entry.key;
195 } else {
196 i->keyTmp.first = 0; // Make sure Valid() and GetKey() return false
197 }
198 return i;
199}
200
202{
203 // Return cached key
204 if (keyTmp.first == DB_COIN) {
205 key = keyTmp.second;
206 return true;
207 }
208 return false;
209}
210
212{
213 return pcursor->GetValue(coin);
214}
215
217{
218 return keyTmp.first == DB_COIN;
219}
220
222{
223 pcursor->Next();
224 CoinEntry entry(&keyTmp.second);
225 if (!pcursor->Valid() || !pcursor->GetKey(entry)) {
226 keyTmp.first = 0; // Invalidate cached key after last record so that Valid() and GetKey() return false
227 } else {
228 keyTmp.first = entry.key;
229 }
230}
int ret
Cursor for iterating over CoinsView state.
Definition: coins.h:235
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.cpp:161
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.cpp:176
bool GetKey(COutPoint &key) const override
Definition: txdb.cpp:201
~CCoinsViewDBCursor()=default
bool GetValue(Coin &coin) const override
Definition: txdb.cpp:211
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Definition: txdb.cpp:165
bool Valid() const override
Definition: txdb.cpp:216
void Next() override
Definition: txdb.cpp:221
std::pair< char, COutPoint > keyTmp
Definition: txdb.cpp:177
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:52
bool BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:93
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:74
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:56
CCoinsViewDB(DBParams db_params, CoinsViewOptions options)
Definition: txdb.cpp:49
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:78
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:68
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:182
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:54
CoinsViewOptions m_options
Definition: txdb.h:55
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:85
bool NeedsUpgrade()
Whether an unsupported database format is used.
Definition: txdb.cpp:28
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:154
DBParams m_db_params
Definition: txdb.h:54
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:74
void Erase(const K &key)
Definition: dbwrapper.h:112
size_t SizeEstimate() const
Definition: dbwrapper.h:120
void Write(const K &key, const V &value)
Definition: dbwrapper.h:100
void Clear()
Definition: dbwrapper.cpp:165
CDBIterator * NewIterator()
Definition: dbwrapper.cpp:394
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
A UTXO entry.
Definition: coins.h:33
Fast randomness source.
Definition: random.h:377
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
Definition: random.h:254
constexpr bool IsNull() const
Definition: uint256.h:48
256-bit opaque blob.
Definition: uint256.h:190
#define LogPrintLevel(category, level,...)
Definition: logging.h:272
#define LogDebug(category,...)
Definition: logging.h:280
#define LogPrintf(...)
Definition: logging.h:266
@ COINDB
Definition: logging.h:61
#define VARINT(obj)
Definition: serialize.h:498
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
Definition: serialize.h:240
#define READWRITE(...)
Definition: serialize.h:156
constexpr CoinEntry(const CAmount v, const State s)
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
Definition: coins.h:266
CoinsCachePair * NextAndMaybeErase(CoinsCachePair &current) noexcept
Return the next entry after current, possibly erasing current.
Definition: coins.h:284
CoinsCachePair * Begin() const noexcept
Definition: coins.h:280
CoinsCachePair * End() const noexcept
Definition: coins.h:281
User-controlled performance and debug options.
Definition: txdb.h:42
int simulate_crash_ratio
If non-zero, randomly exit when the database is flushed with (1/ratio) probability.
Definition: txdb.h:47
size_t batch_write_bytes
Maximum database write batch size in bytes.
Definition: txdb.h:44
Application-specific storage settings.
Definition: dbwrapper.h:34
bool wipe_data
If true, remove all existing data.
Definition: dbwrapper.h:42
size_t cache_bytes
Configures various leveldb cache settings.
Definition: dbwrapper.h:38
bool memory_only
If true, use leveldb's memory environment.
Definition: dbwrapper.h:40
static int count
static constexpr uint8_t DB_HEAD_BLOCKS
Definition: txdb.cpp:24
static constexpr uint8_t DB_BEST_BLOCK
Definition: txdb.cpp:23
static constexpr uint8_t DB_COIN
Definition: txdb.cpp:22
static constexpr uint8_t DB_COINS
Definition: txdb.cpp:26
assert(!tx.IsCoinBase())
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
Definition: vector.h:23