Bitcoin Core  22.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-2021 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 <chain.h>
9 #include <node/ui_interface.h>
10 #include <pow.h>
11 #include <random.h>
12 #include <shutdown.h>
13 #include <uint256.h>
14 #include <util/system.h>
15 #include <util/translation.h>
16 #include <util/vector.h>
17 
18 #include <stdint.h>
19 
20 static constexpr uint8_t DB_COIN{'C'};
21 static constexpr uint8_t DB_COINS{'c'};
22 static constexpr uint8_t DB_BLOCK_FILES{'f'};
23 static constexpr uint8_t DB_BLOCK_INDEX{'b'};
24 
25 static constexpr uint8_t DB_BEST_BLOCK{'B'};
26 static constexpr uint8_t DB_HEAD_BLOCKS{'H'};
27 static constexpr uint8_t DB_FLAG{'F'};
28 static constexpr uint8_t DB_REINDEX_FLAG{'R'};
29 static constexpr uint8_t DB_LAST_BLOCK{'l'};
30 
31 // Keys used in previous version that might still be found in the DB:
32 static constexpr uint8_t DB_TXINDEX_BLOCK{'T'};
33 // uint8_t DB_TXINDEX{'t'}
34 
35 std::optional<bilingual_str> CheckLegacyTxindex(CBlockTreeDB& block_tree_db)
36 {
37  CBlockLocator ignored{};
38  if (block_tree_db.Read(DB_TXINDEX_BLOCK, ignored)) {
39  return _("The -txindex upgrade started by a previous version can not be completed. Restart with the previous version or run a full -reindex.");
40  }
41  bool txindex_legacy_flag{false};
42  block_tree_db.ReadFlag("txindex", txindex_legacy_flag);
43  if (txindex_legacy_flag) {
44  // Disable legacy txindex and warn once about occupied disk space
45  if (!block_tree_db.WriteFlag("txindex", false)) {
46  return Untranslated("Failed to write block index db flag 'txindex'='0'");
47  }
48  return _("The block index db contains a legacy 'txindex'. To clear the occupied disk space, run a full -reindex, otherwise ignore this error. This error message will not be displayed again.");
49  }
50  return std::nullopt;
51 }
52 
53 namespace {
54 
55 struct CoinEntry {
56  COutPoint* outpoint;
57  uint8_t key;
58  explicit CoinEntry(const COutPoint* ptr) : outpoint(const_cast<COutPoint*>(ptr)), key(DB_COIN) {}
59 
60  SERIALIZE_METHODS(CoinEntry, obj) { READWRITE(obj.key, obj.outpoint->hash, VARINT(obj.outpoint->n)); }
61 };
62 
63 }
64 
65 CCoinsViewDB::CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe) :
66  m_db(std::make_unique<CDBWrapper>(ldb_path, nCacheSize, fMemory, fWipe, true)),
67  m_ldb_path(ldb_path),
68  m_is_memory(fMemory) { }
69 
70 void CCoinsViewDB::ResizeCache(size_t new_cache_size)
71 {
72  // We can't do this operation with an in-memory DB since we'll lose all the coins upon
73  // reset.
74  if (!m_is_memory) {
75  // Have to do a reset first to get the original `m_db` state to release its
76  // filesystem lock.
77  m_db.reset();
78  m_db = std::make_unique<CDBWrapper>(
79  m_ldb_path, new_cache_size, m_is_memory, /*fWipe*/ false, /*obfuscate*/ true);
80  }
81 }
82 
83 bool CCoinsViewDB::GetCoin(const COutPoint &outpoint, Coin &coin) const {
84  return m_db->Read(CoinEntry(&outpoint), coin);
85 }
86 
87 bool CCoinsViewDB::HaveCoin(const COutPoint &outpoint) const {
88  return m_db->Exists(CoinEntry(&outpoint));
89 }
90 
92  uint256 hashBestChain;
93  if (!m_db->Read(DB_BEST_BLOCK, hashBestChain))
94  return uint256();
95  return hashBestChain;
96 }
97 
98 std::vector<uint256> CCoinsViewDB::GetHeadBlocks() const {
99  std::vector<uint256> vhashHeadBlocks;
100  if (!m_db->Read(DB_HEAD_BLOCKS, vhashHeadBlocks)) {
101  return std::vector<uint256>();
102  }
103  return vhashHeadBlocks;
104 }
105 
106 bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
107  CDBBatch batch(*m_db);
108  size_t count = 0;
109  size_t changed = 0;
110  size_t batch_size = (size_t)gArgs.GetIntArg("-dbbatchsize", nDefaultDbBatchSize);
111  int crash_simulate = gArgs.GetIntArg("-dbcrashratio", 0);
112  assert(!hashBlock.IsNull());
113 
114  uint256 old_tip = GetBestBlock();
115  if (old_tip.IsNull()) {
116  // We may be in the middle of replaying.
117  std::vector<uint256> old_heads = GetHeadBlocks();
118  if (old_heads.size() == 2) {
119  assert(old_heads[0] == hashBlock);
120  old_tip = old_heads[1];
121  }
122  }
123 
124  // In the first batch, mark the database as being in the middle of a
125  // transition from old_tip to hashBlock.
126  // A vector is used for future extensibility, as we may want to support
127  // interrupting after partial writes from multiple independent reorgs.
128  batch.Erase(DB_BEST_BLOCK);
129  batch.Write(DB_HEAD_BLOCKS, Vector(hashBlock, old_tip));
130 
131  for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
132  if (it->second.flags & CCoinsCacheEntry::DIRTY) {
133  CoinEntry entry(&it->first);
134  if (it->second.coin.IsSpent())
135  batch.Erase(entry);
136  else
137  batch.Write(entry, it->second.coin);
138  changed++;
139  }
140  count++;
141  CCoinsMap::iterator itOld = it++;
142  mapCoins.erase(itOld);
143  if (batch.SizeEstimate() > batch_size) {
144  LogPrint(BCLog::COINDB, "Writing partial batch of %.2f MiB\n", batch.SizeEstimate() * (1.0 / 1048576.0));
145  m_db->WriteBatch(batch);
146  batch.Clear();
147  if (crash_simulate) {
148  static FastRandomContext rng;
149  if (rng.randrange(crash_simulate) == 0) {
150  LogPrintf("Simulating a crash. Goodbye.\n");
151  _Exit(0);
152  }
153  }
154  }
155  }
156 
157  // In the last batch, mark the database as consistent with hashBlock again.
158  batch.Erase(DB_HEAD_BLOCKS);
159  batch.Write(DB_BEST_BLOCK, hashBlock);
160 
161  LogPrint(BCLog::COINDB, "Writing final batch of %.2f MiB\n", batch.SizeEstimate() * (1.0 / 1048576.0));
162  bool ret = m_db->WriteBatch(batch);
163  LogPrint(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
164  return ret;
165 }
166 
168 {
169  return m_db->EstimateSize(DB_COIN, uint8_t(DB_COIN + 1));
170 }
171 
172 CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(gArgs.GetDataDirNet() / "blocks" / "index", nCacheSize, fMemory, fWipe) {
173 }
174 
176  return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
177 }
178 
179 bool CBlockTreeDB::WriteReindexing(bool fReindexing) {
180  if (fReindexing)
181  return Write(DB_REINDEX_FLAG, uint8_t{'1'});
182  else
183  return Erase(DB_REINDEX_FLAG);
184 }
185 
186 void CBlockTreeDB::ReadReindexing(bool &fReindexing) {
187  fReindexing = Exists(DB_REINDEX_FLAG);
188 }
189 
191  return Read(DB_LAST_BLOCK, nFile);
192 }
193 
196 {
197 public:
198  // Prefer using CCoinsViewDB::Cursor() since we want to perform some
199  // cache warmup on instantiation.
200  CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256&hashBlockIn):
201  CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
203 
204  bool GetKey(COutPoint &key) const override;
205  bool GetValue(Coin &coin) const override;
206  unsigned int GetValueSize() const override;
207 
208  bool Valid() const override;
209  void Next() override;
210 
211 private:
212  std::unique_ptr<CDBIterator> pcursor;
213  std::pair<char, COutPoint> keyTmp;
214 
215  friend class CCoinsViewDB;
216 };
217 
218 std::unique_ptr<CCoinsViewCursor> CCoinsViewDB::Cursor() const
219 {
220  auto i = std::make_unique<CCoinsViewDBCursor>(
221  const_cast<CDBWrapper&>(*m_db).NewIterator(), GetBestBlock());
222  /* It seems that there are no "const iterators" for LevelDB. Since we
223  only need read operations on it, use a const-cast to get around
224  that restriction. */
225  i->pcursor->Seek(DB_COIN);
226  // Cache key of first record
227  if (i->pcursor->Valid()) {
228  CoinEntry entry(&i->keyTmp.second);
229  i->pcursor->GetKey(entry);
230  i->keyTmp.first = entry.key;
231  } else {
232  i->keyTmp.first = 0; // Make sure Valid() and GetKey() return false
233  }
234  return i;
235 }
236 
238 {
239  // Return cached key
240  if (keyTmp.first == DB_COIN) {
241  key = keyTmp.second;
242  return true;
243  }
244  return false;
245 }
246 
248 {
249  return pcursor->GetValue(coin);
250 }
251 
253 {
254  return pcursor->GetValueSize();
255 }
256 
258 {
259  return keyTmp.first == DB_COIN;
260 }
261 
263 {
264  pcursor->Next();
265  CoinEntry entry(&keyTmp.second);
266  if (!pcursor->Valid() || !pcursor->GetKey(entry)) {
267  keyTmp.first = 0; // Invalidate cached key after last record so that Valid() and GetKey() return false
268  } else {
269  keyTmp.first = entry.key;
270  }
271 }
272 
273 bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
274  CDBBatch batch(*this);
275  for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
276  batch.Write(std::make_pair(DB_BLOCK_FILES, it->first), *it->second);
277  }
278  batch.Write(DB_LAST_BLOCK, nLastFile);
279  for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
280  batch.Write(std::make_pair(DB_BLOCK_INDEX, (*it)->GetBlockHash()), CDiskBlockIndex(*it));
281  }
282  return WriteBatch(batch, true);
283 }
284 
285 bool CBlockTreeDB::WriteFlag(const std::string &name, bool fValue) {
286  return Write(std::make_pair(DB_FLAG, name), fValue ? uint8_t{'1'} : uint8_t{'0'});
287 }
288 
289 bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
290  uint8_t ch;
291  if (!Read(std::make_pair(DB_FLAG, name), ch))
292  return false;
293  fValue = ch == uint8_t{'1'};
294  return true;
295 }
296 
297 bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex)
298 {
300  std::unique_ptr<CDBIterator> pcursor(NewIterator());
301  pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
302 
303  // Load m_block_index
304  while (pcursor->Valid()) {
305  if (ShutdownRequested()) return false;
306  std::pair<uint8_t, uint256> key;
307  if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) {
308  CDiskBlockIndex diskindex;
309  if (pcursor->GetValue(diskindex)) {
310  // Construct block index object
311  CBlockIndex* pindexNew = insertBlockIndex(diskindex.GetBlockHash());
312  pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
313  pindexNew->nHeight = diskindex.nHeight;
314  pindexNew->nFile = diskindex.nFile;
315  pindexNew->nDataPos = diskindex.nDataPos;
316  pindexNew->nUndoPos = diskindex.nUndoPos;
317  pindexNew->nVersion = diskindex.nVersion;
318  pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
319  pindexNew->nTime = diskindex.nTime;
320  pindexNew->nBits = diskindex.nBits;
321  pindexNew->nNonce = diskindex.nNonce;
322  pindexNew->nStatus = diskindex.nStatus;
323  pindexNew->nTx = diskindex.nTx;
324 
325  if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams)) {
326  return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());
327  }
328 
329  pcursor->Next();
330  } else {
331  return error("%s: failed to read value", __func__);
332  }
333  } else {
334  break;
335  }
336  }
337 
338  return true;
339 }
340 
341 namespace {
342 
344 class CCoins
345 {
346 public:
348  bool fCoinBase;
349 
351  std::vector<CTxOut> vout;
352 
354  int nHeight;
355 
357  CCoins() : fCoinBase(false), vout(0), nHeight(0) { }
358 
359  template<typename Stream>
360  void Unserialize(Stream &s) {
361  unsigned int nCode = 0;
362  // version
363  unsigned int nVersionDummy;
364  ::Unserialize(s, VARINT(nVersionDummy));
365  // header code
366  ::Unserialize(s, VARINT(nCode));
367  fCoinBase = nCode & 1;
368  std::vector<bool> vAvail(2, false);
369  vAvail[0] = (nCode & 2) != 0;
370  vAvail[1] = (nCode & 4) != 0;
371  unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1);
372  // spentness bitmask
373  while (nMaskCode > 0) {
374  unsigned char chAvail = 0;
375  ::Unserialize(s, chAvail);
376  for (unsigned int p = 0; p < 8; p++) {
377  bool f = (chAvail & (1 << p)) != 0;
378  vAvail.push_back(f);
379  }
380  if (chAvail != 0)
381  nMaskCode--;
382  }
383  // txouts themself
384  vout.assign(vAvail.size(), CTxOut());
385  for (unsigned int i = 0; i < vAvail.size(); i++) {
386  if (vAvail[i])
387  ::Unserialize(s, Using<TxOutCompression>(vout[i]));
388  }
389  // coinbase height
391  }
392 };
393 
394 }
395 
401  std::unique_ptr<CDBIterator> pcursor(m_db->NewIterator());
402  pcursor->Seek(std::make_pair(DB_COINS, uint256()));
403  if (!pcursor->Valid()) {
404  return true;
405  }
406 
407  int64_t count = 0;
408  LogPrintf("Upgrading utxo-set database...\n");
409  LogPrintf("[0%%]..."); /* Continued */
410  uiInterface.ShowProgress(_("Upgrading UTXO database").translated, 0, true);
411  size_t batch_size = 1 << 24;
412  CDBBatch batch(*m_db);
413  int reportDone = 0;
414  std::pair<unsigned char, uint256> key;
415  std::pair<unsigned char, uint256> prev_key = {DB_COINS, uint256()};
416  while (pcursor->Valid()) {
417  if (ShutdownRequested()) {
418  break;
419  }
420  if (pcursor->GetKey(key) && key.first == DB_COINS) {
421  if (count++ % 256 == 0) {
422  uint32_t high = 0x100 * *key.second.begin() + *(key.second.begin() + 1);
423  int percentageDone = (int)(high * 100.0 / 65536.0 + 0.5);
424  uiInterface.ShowProgress(_("Upgrading UTXO database").translated, percentageDone, true);
425  if (reportDone < percentageDone/10) {
426  // report max. every 10% step
427  LogPrintf("[%d%%]...", percentageDone); /* Continued */
428  reportDone = percentageDone/10;
429  }
430  }
431  CCoins old_coins;
432  if (!pcursor->GetValue(old_coins)) {
433  return error("%s: cannot parse CCoins record", __func__);
434  }
435  COutPoint outpoint(key.second, 0);
436  for (size_t i = 0; i < old_coins.vout.size(); ++i) {
437  if (!old_coins.vout[i].IsNull() && !old_coins.vout[i].scriptPubKey.IsUnspendable()) {
438  Coin newcoin(std::move(old_coins.vout[i]), old_coins.nHeight, old_coins.fCoinBase);
439  outpoint.n = i;
440  CoinEntry entry(&outpoint);
441  batch.Write(entry, newcoin);
442  }
443  }
444  batch.Erase(key);
445  if (batch.SizeEstimate() > batch_size) {
446  m_db->WriteBatch(batch);
447  batch.Clear();
448  m_db->CompactRange(prev_key, key);
449  prev_key = key;
450  }
451  pcursor->Next();
452  } else {
453  break;
454  }
455  }
456  m_db->WriteBatch(batch);
457  m_db->CompactRange({DB_COINS, uint256()}, key);
458  uiInterface.ShowProgress("", 100, false);
459  LogPrintf("[%s].\n", ShutdownRequested() ? "CANCELLED" : "DONE");
460  return !ShutdownRequested();
461 }
DB_BLOCK_FILES
static constexpr uint8_t DB_BLOCK_FILES
Definition: txdb.cpp:22
ShutdownRequested
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
Definition: shutdown.cpp:87
CCoinsViewDB::Upgrade
bool Upgrade()
Attempt to update from an older database format. Returns whether an error occurred.
Definition: txdb.cpp:400
CCoinsViewDB
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:49
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:63
DB_BLOCK_INDEX
static constexpr uint8_t DB_BLOCK_INDEX
Definition: txdb.cpp:23
SERIALIZE_METHODS
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
Definition: serialize.h:176
count
static int count
Definition: tests.c:31
assert
assert(!tx.IsCoinBase())
pow.h
CBlockIndex::nTime
uint32_t nTime
Definition: chain.h:206
nHeight
unsigned int nHeight
Definition: mempool_eviction.cpp:14
CDBBatch
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:47
CCoinsViewDBCursor::CCoinsViewDBCursor
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Definition: txdb.cpp:200
CBlockIndex::nTx
unsigned int nTx
Number of transactions in this block.
Definition: chain.h:183
uiInterface
CClientUIInterface uiInterface
Definition: ui_interface.cpp:12
VARINT_MODE
#define VARINT_MODE(obj, mode)
Definition: serialize.h:435
CBlockIndex::nBits
uint32_t nBits
Definition: chain.h:207
uint256.h
CBlockIndex::pprev
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:158
CBlockIndex::nHeight
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:164
DB_BEST_BLOCK
static constexpr uint8_t DB_BEST_BLOCK
Definition: txdb.cpp:25
CBlockIndex::ToString
std::string ToString() const
Definition: chain.h:304
CCoinsViewDBCursor::GetKey
bool GetKey(COutPoint &key) const override
Definition: txdb.cpp:237
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
CheckProofOfWork
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:74
VarIntMode::NONNEGATIVE_SIGNED
@ NONNEGATIVE_SIGNED
CBlockTreeDB::ReadReindexing
void ReadReindexing(bool &fReindexing)
Definition: txdb.cpp:186
CCoinsViewDBCursor
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.cpp:195
CDBWrapper::Read
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:231
CDiskBlockIndex
Used to marshal pointers into hashes for db storage.
Definition: chain.h:368
Unserialize
void Unserialize(Stream &, char)=delete
DB_FLAG
static constexpr uint8_t DB_FLAG
Definition: txdb.cpp:27
AssertLockHeld
AssertLockHeld(pool.cs)
nDefaultDbBatchSize
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
Definition: txdb.h:29
DB_REINDEX_FLAG
static constexpr uint8_t DB_REINDEX_FLAG
Definition: txdb.cpp:28
shutdown.h
DB_HEAD_BLOCKS
static constexpr uint8_t DB_HEAD_BLOCKS
Definition: txdb.cpp:26
CDBBatch::Write
void Write(const K &key, const V &value)
Definition: dbwrapper.h:73
CCoinsViewCursor
Cursor for iterating over CoinsView state.
Definition: coins.h:137
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:70
DB_COINS
static constexpr uint8_t DB_COINS
Definition: txdb.cpp:21
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
random.h
CDBWrapper::NewIterator
CDBIterator * NewIterator()
Definition: dbwrapper.h:296
Untranslated
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:46
CTxOut
An output of a transaction.
Definition: transaction.h:148
CCoinsViewDBCursor::GetValueSize
unsigned int GetValueSize() const override
Definition: txdb.cpp:252
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 block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
VARINT
#define VARINT(obj)
Definition: serialize.h:436
CCoinsViewDB::HaveCoin
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:87
CBlockIndex::nVersion
int32_t nVersion
block header
Definition: chain.h:204
CDBBatch::Clear
void Clear()
Definition: dbwrapper.h:66
CBlockIndex::hashMerkleRoot
uint256 hashMerkleRoot
Definition: chain.h:205
DB_LAST_BLOCK
static constexpr uint8_t DB_LAST_BLOCK
Definition: txdb.cpp:29
CBlockTreeDB::ReadBlockFileInfo
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:175
CBlockTreeDB::WriteReindexing
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:179
CCoinsViewDBCursor::Valid
bool Valid() const override
Definition: txdb.cpp:257
CBlockTreeDB
Access to the block database (blocks/index/)
Definition: txdb.h:77
LogPrintf
#define LogPrintf(...)
Definition: logging.h:188
CheckLegacyTxindex
std::optional< bilingual_str > CheckLegacyTxindex(CBlockTreeDB &block_tree_db)
Definition: txdb.cpp:35
DB_TXINDEX_BLOCK
static constexpr uint8_t DB_TXINDEX_BLOCK
Definition: txdb.cpp:32
bilingual_str::translated
std::string translated
Definition: translation.h:18
uint256
256-bit opaque blob.
Definition: uint256.h:126
CCoinsViewDBCursor::GetValue
bool GetValue(Coin &coin) const override
Definition: txdb.cpp:247
READWRITE
#define READWRITE(...)
Definition: serialize.h:140
DB_COIN
static constexpr uint8_t DB_COIN
Definition: txdb.cpp:20
CBlockTreeDB::ReadFlag
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:289
CDBWrapper::Erase
bool Erase(const K &key, bool fSync=false)
Definition: dbwrapper.h:284
LogPrint
#define LogPrint(category,...)
Definition: logging.h:192
chain.h
CBlockIndex::GetBlockHash
uint256 GetBlockHash() const
Definition: chain.h:264
gArgs
ArgsManager gArgs
Definition: system.cpp:87
name
const char * name
Definition: rest.cpp:52
ui_interface.h
CDBWrapper
Definition: dbwrapper.h:176
system.h
CBlockTreeDB::WriteFlag
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:285
BCLog::COINDB
@ COINDB
Definition: logging.h:57
CCoinsViewDB::BatchWrite
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:106
CCoinsViewDBCursor::keyTmp
std::pair< char, COutPoint > keyTmp
Definition: txdb.cpp:213
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:33
CCoinsViewDBCursor::~CCoinsViewDBCursor
~CCoinsViewDBCursor()
Definition: txdb.cpp:202
translation.h
CCoinsViewDB::GetBestBlock
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:91
COutPoint::n
uint32_t n
Definition: transaction.h:30
Vector
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
Definition: vector.h:20
vector.h
CBlockFileInfo
Definition: chain.h:43
std
Definition: setup_common.h:33
CCoinsViewDBCursor::Next
void Next() override
Definition: txdb.cpp:262
CDBBatch::Erase
void Erase(const K &key)
Definition: dbwrapper.h:98
CCoinsViewDB::m_is_memory
bool m_is_memory
Definition: txdb.h:54
CBlockIndex::nNonce
uint32_t nNonce
Definition: chain.h:208
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
FastRandomContext::randrange
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:202
CDiskBlockIndex::hashPrev
uint256 hashPrev
Definition: chain.h:371
CDiskBlockIndex::GetBlockHash
uint256 GetBlockHash() const
Definition: chain.h:405
CDBWrapper::Write
bool Write(const K &key, const V &value, bool fSync=false)
Definition: dbwrapper.h:257
CDBWrapper::Exists
bool Exists(const K &key) const
Definition: dbwrapper.h:265
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
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
CCoinsCacheEntry::DIRTY
@ DIRTY
DIRTY means the CCoinsCacheEntry is potentially different from the version in the parent cache.
Definition: coins.h:116
CBlockLocator
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:114
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:138
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:151
ArgsManager::GetIntArg
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: system.cpp:594
CCoinsViewDBCursor::pcursor
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.cpp:212
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
CDBWrapper::WriteBatch
bool WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:187
txdb.h
base_blob::begin
unsigned char * begin()
Definition: uint256.h:60
FastRandomContext
Fast randomness source.
Definition: random.h:131
CDBBatch::SizeEstimate
size_t SizeEstimate() const
Definition: dbwrapper.h:114
CDBIterator
Definition: dbwrapper.h:117
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