Bitcoin ABC  0.24.10
P2P Digital Currency
txdb.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <blockdb.h>
9 #include <chain.h>
10 #include <node/ui_interface.h>
11 #include <pow/pow.h>
12 #include <random.h>
13 #include <shutdown.h>
14 #include <util/system.h>
15 #include <util/translation.h>
16 #include <util/vector.h>
17 #include <version.h>
18 
19 #include <cstdint>
20 #include <memory>
21 
22 static const char DB_COIN = 'C';
23 static const char DB_COINS = 'c';
24 static const char DB_BLOCK_FILES = 'f';
25 static const char DB_BLOCK_INDEX = 'b';
26 
27 static const char DB_BEST_BLOCK = 'B';
28 static const char DB_HEAD_BLOCKS = 'H';
29 static const char DB_FLAG = 'F';
30 static const char DB_REINDEX_FLAG = 'R';
31 static const char DB_LAST_BLOCK = 'l';
32 
33 namespace {
34 
35 struct CoinEntry {
36  COutPoint *outpoint;
37  char key;
38  explicit CoinEntry(const COutPoint *ptr)
39  : outpoint(const_cast<COutPoint *>(ptr)), key(DB_COIN) {}
40 
41  SERIALIZE_METHODS(CoinEntry, obj) {
42  TxId id = obj.outpoint->GetTxId();
43  uint32_t n = obj.outpoint->GetN();
44  READWRITE(obj.key, id, VARINT(n));
45  SER_READ(obj, *obj.outpoint = COutPoint(id, n));
46  }
47 };
48 } // namespace
49 
50 CCoinsViewDB::CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory,
51  bool fWipe)
52  : m_db(std::make_unique<CDBWrapper>(ldb_path, nCacheSize, fMemory, fWipe,
53  true)),
54  m_ldb_path(ldb_path), m_is_memory(fMemory) {}
55 
56 void CCoinsViewDB::ResizeCache(size_t new_cache_size) {
57  // Have to do a reset first to get the original `m_db` state to release its
58  // filesystem lock.
59  m_db.reset();
60  m_db = std::make_unique<CDBWrapper>(m_ldb_path, new_cache_size, m_is_memory,
61  /*fWipe*/ false, /*obfuscate*/ true);
62 }
63 
64 bool CCoinsViewDB::GetCoin(const COutPoint &outpoint, Coin &coin) const {
65  return m_db->Read(CoinEntry(&outpoint), coin);
66 }
67 
68 bool CCoinsViewDB::HaveCoin(const COutPoint &outpoint) const {
69  return m_db->Exists(CoinEntry(&outpoint));
70 }
71 
73  BlockHash hashBestChain;
74  if (!m_db->Read(DB_BEST_BLOCK, hashBestChain)) {
75  return BlockHash();
76  }
77  return hashBestChain;
78 }
79 
80 std::vector<BlockHash> CCoinsViewDB::GetHeadBlocks() const {
81  std::vector<BlockHash> vhashHeadBlocks;
82  if (!m_db->Read(DB_HEAD_BLOCKS, vhashHeadBlocks)) {
83  return std::vector<BlockHash>();
84  }
85  return vhashHeadBlocks;
86 }
87 
88 bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) {
89  CDBBatch batch(*m_db);
90  size_t count = 0;
91  size_t changed = 0;
92  size_t batch_size =
93  (size_t)gArgs.GetArg("-dbbatchsize", DEFAULT_DB_BATCH_SIZE);
94  int crash_simulate = gArgs.GetArg("-dbcrashratio", 0);
95  assert(!hashBlock.IsNull());
96 
97  BlockHash old_tip = GetBestBlock();
98  if (old_tip.IsNull()) {
99  // We may be in the middle of replaying.
100  std::vector<BlockHash> old_heads = GetHeadBlocks();
101  if (old_heads.size() == 2) {
102  assert(old_heads[0] == hashBlock);
103  old_tip = old_heads[1];
104  }
105  }
106 
107  // In the first batch, mark the database as being in the middle of a
108  // transition from old_tip to hashBlock.
109  // A vector is used for future extensibility, as we may want to support
110  // interrupting after partial writes from multiple independent reorgs.
111  batch.Erase(DB_BEST_BLOCK);
112  batch.Write(DB_HEAD_BLOCKS, Vector(hashBlock, old_tip));
113 
114  for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
115  if (it->second.flags & CCoinsCacheEntry::DIRTY) {
116  CoinEntry entry(&it->first);
117  if (it->second.coin.IsSpent()) {
118  batch.Erase(entry);
119  } else {
120  batch.Write(entry, it->second.coin);
121  }
122  changed++;
123  }
124  count++;
125  CCoinsMap::iterator itOld = it++;
126  mapCoins.erase(itOld);
127  if (batch.SizeEstimate() > batch_size) {
128  LogPrint(BCLog::COINDB, "Writing partial batch of %.2f MiB\n",
129  batch.SizeEstimate() * (1.0 / 1048576.0));
130  m_db->WriteBatch(batch);
131  batch.Clear();
132  if (crash_simulate) {
133  static FastRandomContext rng;
134  if (rng.randrange(crash_simulate) == 0) {
135  LogPrintf("Simulating a crash. Goodbye.\n");
136  _Exit(0);
137  }
138  }
139  }
140  }
141 
142  // In the last batch, mark the database as consistent with hashBlock again.
143  batch.Erase(DB_HEAD_BLOCKS);
144  batch.Write(DB_BEST_BLOCK, hashBlock);
145 
146  LogPrint(BCLog::COINDB, "Writing final batch of %.2f MiB\n",
147  batch.SizeEstimate() * (1.0 / 1048576.0));
148  bool ret = m_db->WriteBatch(batch);
150  "Committed %u changed transaction outputs (out of "
151  "%u) to coin database...\n",
152  (unsigned int)changed, (unsigned int)count);
153  return ret;
154 }
155 
157  return m_db->EstimateSize(DB_COIN, char(DB_COIN + 1));
158 }
159 
160 CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe)
161  : CDBWrapper(GetDataDir() / "blocks" / "index", nCacheSize, fMemory,
162  fWipe) {}
163 
165  return Read(std::make_pair(DB_BLOCK_FILES, nFile), info);
166 }
167 
168 bool CBlockTreeDB::WriteReindexing(bool fReindexing) {
169  if (fReindexing) {
170  return Write(DB_REINDEX_FLAG, '1');
171  } else {
172  return Erase(DB_REINDEX_FLAG);
173  }
174 }
175 
177  return Exists(DB_REINDEX_FLAG);
178 }
179 
181  return Read(DB_LAST_BLOCK, nFile);
182 }
183 
186  const_cast<CDBWrapper &>(*m_db).NewIterator(), GetBestBlock());
192  i->pcursor->Seek(DB_COIN);
193  // Cache key of first record
194  if (i->pcursor->Valid()) {
195  CoinEntry entry(&i->keyTmp.second);
196  i->pcursor->GetKey(entry);
197  i->keyTmp.first = entry.key;
198  } else {
199  // Make sure Valid() and GetKey() return false
200  i->keyTmp.first = 0;
201  }
202  return i;
203 }
204 
206  // Return cached key
207  if (keyTmp.first == DB_COIN) {
208  key = keyTmp.second;
209  return true;
210  }
211  return false;
212 }
213 
215  return pcursor->GetValue(coin);
216 }
217 
218 unsigned int CCoinsViewDBCursor::GetValueSize() const {
219  return pcursor->GetValueSize();
220 }
221 
223  return keyTmp.first == DB_COIN;
224 }
225 
227  pcursor->Next();
228  CoinEntry entry(&keyTmp.second);
229  if (!pcursor->Valid() || !pcursor->GetKey(entry)) {
230  // Invalidate cached key after last record so that Valid() and GetKey()
231  // return false
232  keyTmp.first = 0;
233  } else {
234  keyTmp.first = entry.key;
235  }
236 }
237 
239  const std::vector<std::pair<int, const CBlockFileInfo *>> &fileInfo,
240  int nLastFile, const std::vector<const CBlockIndex *> &blockinfo) {
241  CDBBatch batch(*this);
242  for (std::vector<std::pair<int, const CBlockFileInfo *>>::const_iterator
243  it = fileInfo.begin();
244  it != fileInfo.end(); it++) {
245  batch.Write(std::make_pair(DB_BLOCK_FILES, it->first), *it->second);
246  }
247  batch.Write(DB_LAST_BLOCK, nLastFile);
248  for (std::vector<const CBlockIndex *>::const_iterator it =
249  blockinfo.begin();
250  it != blockinfo.end(); it++) {
251  batch.Write(std::make_pair(DB_BLOCK_INDEX, (*it)->GetBlockHash()),
252  CDiskBlockIndex(*it));
253  }
254  return WriteBatch(batch, true);
255 }
256 
257 bool CBlockTreeDB::WriteFlag(const std::string &name, bool fValue) {
258  return Write(std::make_pair(DB_FLAG, name), fValue ? '1' : '0');
259 }
260 
261 bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) {
262  char ch;
263  if (!Read(std::make_pair(DB_FLAG, name), ch)) {
264  return false;
265  }
266  fValue = ch == '1';
267  return true;
268 }
269 
271  const Consensus::Params &params,
272  std::function<CBlockIndex *(const BlockHash &)> insertBlockIndex) {
273  std::unique_ptr<CDBIterator> pcursor(NewIterator());
274 
275  uint64_t version = 0;
276  pcursor->Seek("version");
277  if (pcursor->Valid()) {
278  pcursor->GetValue(version);
279  }
280 
281  if (version != CLIENT_VERSION) {
282  return error("%s: Invalid block index database version: %s", __func__,
283  version);
284  }
285 
286  pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
287 
288  // Load m_block_index
289  while (pcursor->Valid()) {
290  if (ShutdownRequested()) {
291  return false;
292  }
293  std::pair<char, uint256> key;
294  if (!pcursor->GetKey(key) || key.first != DB_BLOCK_INDEX) {
295  break;
296  }
297 
298  CDiskBlockIndex diskindex;
299  if (!pcursor->GetValue(diskindex)) {
300  return error("%s : failed to read value", __func__);
301  }
302 
303  // Construct block index object
304  CBlockIndex *pindexNew = insertBlockIndex(diskindex.GetBlockHash());
305  pindexNew->pprev = insertBlockIndex(diskindex.hashPrev);
306  pindexNew->nHeight = diskindex.nHeight;
307  pindexNew->nFile = diskindex.nFile;
308  pindexNew->nDataPos = diskindex.nDataPos;
309  pindexNew->nUndoPos = diskindex.nUndoPos;
310  pindexNew->nVersion = diskindex.nVersion;
311  pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
312  pindexNew->nTime = diskindex.nTime;
313  pindexNew->nBits = diskindex.nBits;
314  pindexNew->nNonce = diskindex.nNonce;
315  pindexNew->nStatus = diskindex.nStatus;
316  pindexNew->nTx = diskindex.nTx;
317 
318  if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits,
319  params)) {
320  return error("%s: CheckProofOfWork failed: %s", __func__,
321  pindexNew->ToString());
322  }
323 
324  pcursor->Next();
325  }
326 
327  return true;
328 }
329 
330 namespace {
332 class CCoins {
333 public:
335  bool fCoinBase;
336 
339  std::vector<CTxOut> vout;
340 
342  int nHeight;
343 
345  CCoins() : fCoinBase(false), vout(0), nHeight(0) {}
346 
347  template <typename Stream> void Unserialize(Stream &s) {
348  uint32_t nCode = 0;
349  // version
350  unsigned int nVersionDummy = 0;
351  ::Unserialize(s, VARINT(nVersionDummy));
352  // header code
353  ::Unserialize(s, VARINT(nCode));
354  fCoinBase = nCode & 1;
355  std::vector<bool> vAvail(2, false);
356  vAvail[0] = (nCode & 2) != 0;
357  vAvail[1] = (nCode & 4) != 0;
358  uint32_t nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1);
359  // spentness bitmask
360  while (nMaskCode > 0) {
361  uint8_t chAvail = 0;
362  ::Unserialize(s, chAvail);
363  for (unsigned int p = 0; p < 8; p++) {
364  bool f = (chAvail & (1 << p)) != 0;
365  vAvail.push_back(f);
366  }
367  if (chAvail != 0) {
368  nMaskCode--;
369  }
370  }
371  // txouts themself
372  vout.assign(vAvail.size(), CTxOut());
373  for (size_t i = 0; i < vAvail.size(); i++) {
374  if (vAvail[i]) {
375  ::Unserialize(s, Using<TxOutCompression>(vout[i]));
376  }
377  }
378  // coinbase height
380  }
381 };
382 } // namespace
383 
390  std::unique_ptr<CDBIterator> pcursor(m_db->NewIterator());
391  pcursor->Seek(std::make_pair(DB_COINS, uint256()));
392  if (!pcursor->Valid()) {
393  return true;
394  }
395 
396  int64_t count = 0;
397  LogPrintf("Upgrading utxo-set database...\n");
398  size_t batch_size = 1 << 24;
399  CDBBatch batch(*m_db);
400  int reportDone = -1;
401  std::pair<uint8_t, uint256> key;
402  std::pair<uint8_t, uint256> prev_key = {DB_COINS, uint256()};
403  while (pcursor->Valid()) {
404  if (ShutdownRequested()) {
405  break;
406  }
407 
408  if (!pcursor->GetKey(key) || key.first != DB_COINS) {
409  break;
410  }
411 
412  if (count++ % 256 == 0) {
413  uint32_t high =
414  0x100 * *key.second.begin() + *(key.second.begin() + 1);
415  int percentageDone = (int)(high * 100.0 / 65536.0 + 0.5);
416  uiInterface.ShowProgress(_("Upgrading UTXO database").translated,
417  percentageDone, true);
418  if (reportDone < percentageDone / 10) {
419  // report max. every 10% step
420  LogPrintfToBeContinued("[%d%%]...", percentageDone);
421  reportDone = percentageDone / 10;
422  }
423  }
424 
425  CCoins old_coins;
426  if (!pcursor->GetValue(old_coins)) {
427  return error("%s: cannot parse CCoins record", __func__);
428  }
429 
430  const TxId id(key.second);
431  for (size_t i = 0; i < old_coins.vout.size(); ++i) {
432  if (!old_coins.vout[i].IsNull() &&
433  !old_coins.vout[i].scriptPubKey.IsUnspendable()) {
434  Coin newcoin(std::move(old_coins.vout[i]), old_coins.nHeight,
435  old_coins.fCoinBase);
436  COutPoint outpoint(id, i);
437  CoinEntry entry(&outpoint);
438  batch.Write(entry, newcoin);
439  }
440  }
441 
442  batch.Erase(key);
443  if (batch.SizeEstimate() > batch_size) {
444  m_db->WriteBatch(batch);
445  batch.Clear();
446  m_db->CompactRange(prev_key, key);
447  prev_key = key;
448  }
449 
450  pcursor->Next();
451  }
452 
453  m_db->WriteBatch(batch);
454  m_db->CompactRange({DB_COINS, uint256()}, key);
455  uiInterface.ShowProgress("", 100, false);
456  LogPrintf("[%s].\n", ShutdownRequested() ? "CANCELLED" : "DONE");
457  return !ShutdownRequested();
458 }
459 
461  std::unique_ptr<CDBIterator> pcursor(NewIterator());
462 
463  uint64_t version = 0;
464  pcursor->Seek("version");
465  if (pcursor->Valid()) {
466  pcursor->GetValue(version);
467  }
468 
469  if (version >= CLIENT_VERSION) {
470  // The DB is already up to date.
471  return true;
472  }
473 
474  CDBBatch batch(*this);
475 
476  pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256()));
477  if (!pcursor->Valid()) {
478  // The DB is empty, so just write the version number and consider the
479  // upgrade done.
480  batch.Write("version", uint64_t(CLIENT_VERSION));
481  WriteBatch(batch);
482  return true;
483  }
484 
485  int64_t count = 0;
486  LogPrintf("Upgrading block index database...\n");
487  int reportDone = -1;
488  std::pair<uint8_t, uint256> key = {DB_BLOCK_INDEX, uint256()};
489  while (pcursor->Valid()) {
490  if (ShutdownRequested()) {
491  break;
492  }
493 
494  if (!pcursor->GetKey(key) || key.first != DB_BLOCK_INDEX) {
495  break;
496  }
497 
498  if (count++ % 256 == 0) {
499  uint32_t high =
500  0x100 * *key.second.begin() + *(key.second.begin() + 1);
501  int percentageDone = (int)(high * 100.0 / 65536.0 + 0.5);
502  uiInterface.ShowProgress(
503  _("Upgrading block index database").translated, percentageDone,
504  true);
505  if (reportDone < percentageDone / 10) {
506  // report max. every 10% step
507  LogPrintfToBeContinued("[%d%%]...", percentageDone);
508  reportDone = percentageDone / 10;
509  }
510  }
511 
512  // Read the block index entry and update it.
513  CDiskBlockIndex diskindex;
514  if (!pcursor->GetValue(diskindex)) {
515  return error("%s: cannot parse CDiskBlockIndex record", __func__);
516  }
517 
518  // The block hash needs to be usable.
519  BlockHash blockhash = diskindex.GetBlockHash();
520  diskindex.phashBlock = &blockhash;
521 
522  bool mustUpdate = false;
523 
524  // We must update the block index to add the size.
527  diskindex.nTx > 0 && diskindex.nSize == 0) {
528  if (!diskindex.nStatus.hasData()) {
529  // The block was pruned, we need a full reindex.
530  LogPrintf("\nThe block %s is pruned. The block index cannot be "
531  "upgraded and reindexing is required.\n",
532  blockhash.GetHex());
533  return false;
534  }
535 
536  CBlock block;
537  if (!ReadBlockFromDisk(block, &diskindex, params)) {
538  // Failed to read the block from disk, even though it is marked
539  // that we have data for this block.
540  return false;
541  }
542 
543  mustUpdate = true;
544  diskindex.nSize = ::GetSerializeSize(block, PROTOCOL_VERSION);
545  }
546 
547  if (mustUpdate) {
548  batch.Write(std::make_pair(DB_BLOCK_INDEX, blockhash), diskindex);
549  }
550 
551  pcursor->Next();
552  }
553 
554  // Upgrade is done, now let's update the version number.
555  batch.Write("version", uint64_t(CLIENT_VERSION));
556 
557  WriteBatch(batch);
559  uiInterface.ShowProgress("", 100, false);
560  LogPrintf("[%s].\n", ShutdownRequested() ? "CANCELLED" : "DONE");
561  return !ShutdownRequested();
562 }
CBlockTreeDB::IsReindexing
bool IsReindexing() const
Definition: txdb.cpp:176
CCoinsViewDB::Cursor
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:184
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1182
CCoinsViewDB::GetHeadBlocks
std::vector< BlockHash > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:80
ShutdownRequested
bool ShutdownRequested()
Definition: shutdown.cpp:18
CCoinsViewDB::Upgrade
bool Upgrade()
Attempt to update from an older database format.
Definition: txdb.cpp:389
DB_COIN
static const char DB_COIN
Definition: txdb.cpp:22
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:55
GetDataDir
const fs::path & GetDataDir(bool fNetSpecific)
Definition: system.cpp:831
CBlockIndex::nFile
int nFile
Which # file this block is stored in (blk?????.dat)
Definition: blockindex.h:39
count
static int count
Definition: tests.c:41
pow.h
DB_COINS
static const char DB_COINS
Definition: txdb.cpp:23
CBlockIndex::nTime
uint32_t nTime
Definition: blockindex.h:81
CDiskBlockIndex::GetBlockHash
BlockHash GetBlockHash() const
Definition: chain.h:129
nHeight
unsigned int nHeight
Definition: mempool_eviction.cpp:13
CDBBatch
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:48
CBlockIndex::nTx
unsigned int nTx
Number of transactions in this block.
Definition: blockindex.h:54
uiInterface
CClientUIInterface uiInterface
Definition: ui_interface.cpp:12
VARINT_MODE
#define VARINT_MODE(obj, mode)
Definition: serialize.h:595
CBlockIndex::nBits
uint32_t nBits
Definition: blockindex.h:82
LogPrintfToBeContinued
#define LogPrintfToBeContinued
These are aliases used to explicitly state that the message should not end with a newline character.
Definition: logging.h:205
CBlockIndex::pprev
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: blockindex.h:30
CBlockIndex::nHeight
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:36
DB_BEST_BLOCK
static const char DB_BEST_BLOCK
Definition: txdb.cpp:27
CBlockTreeDB::Upgrade
bool Upgrade(const Consensus::Params &params)
Attempt to update from an older database format.
Definition: txdb.cpp:460
CBlockIndex::ToString
std::string ToString() const
Definition: blockindex.h:187
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
VarIntMode::NONNEGATIVE_SIGNED
@ NONNEGATIVE_SIGNED
DB_BLOCK_FILES
static const char DB_BLOCK_FILES
Definition: txdb.cpp:24
CCoinsViewDBCursor
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:84
CDBWrapper::Read
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:230
CBlockIndex::nStatus
BlockStatus nStatus
Verification status of this block. See enum BlockStatus.
Definition: blockindex.h:76
CBlockIndex::nDataPos
unsigned int nDataPos
Byte offset within blk?????.dat where this block's data is stored.
Definition: blockindex.h:42
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
CDiskBlockIndex
Used to marshal pointers into hashes for db storage.
Definition: chain.h:83
version.h
CBlockIndex::GetBlockHash
BlockHash GetBlockHash() const
Definition: blockindex.h:133
shutdown.h
CDBBatch::Write
void Write(const K &key, const V &value)
Definition: dbwrapper.h:73
CCoinsViewCursor
Cursor for iterating over CoinsView state.
Definition: coins.h:155
Consensus::Params
Parameters that influence chain consensus.
Definition: params.h:59
blockdb.h
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
CheckProofOfWork
bool CheckProofOfWork(const BlockHash &hash, uint32_t nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:45
random.h
CDBWrapper::NewIterator
CDBIterator * NewIterator()
Definition: dbwrapper.h:289
Unserialize
void Unserialize(Stream &s, char &a)
Definition: serialize.h:293
BlockStatus::hasData
bool hasData() const
Definition: blockstatus.h:54
CTxOut
An output of a transaction.
Definition: transaction.h:130
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
fs::path
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:33
VARINT
#define VARINT(obj)
Definition: serialize.h:596
CCoinsViewDB::HaveCoin
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:68
CBlockIndex::nVersion
int32_t nVersion
block header
Definition: blockindex.h:79
CDBBatch::Clear
void Clear()
Definition: dbwrapper.h:68
CBlockIndex::hashMerkleRoot
uint256 hashMerkleRoot
Definition: blockindex.h:80
CService::SERIALIZE_METHODS
SERIALIZE_METHODS(CService, obj)
Definition: netaddress.h:540
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
ArgsManager::GetArg
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:582
base_blob::GetHex
std::string GetHex() const
Definition: uint256.cpp:16
DB_FLAG
static const char DB_FLAG
Definition: txdb.cpp:29
ReadBlockFromDisk
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &params)
Functions for disk access for blocks.
Definition: blockdb.cpp:33
CBlockIndex::nSize
unsigned int nSize
Size of this block.
Definition: blockindex.h:59
bilingual_str::translated
std::string translated
Definition: translation.h:19
CCoinsViewDB::GetBestBlock
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:72
uint256
256-bit opaque blob.
Definition: uint256.h:127
CCoinsViewDBCursor::GetValue
bool GetValue(Coin &coin) const override
Definition: txdb.cpp:214
READWRITE
#define READWRITE(...)
Definition: serialize.h:179
CBlockTreeDB::LoadBlockIndexGuts
bool LoadBlockIndexGuts(const Consensus::Params &params, std::function< CBlockIndex *(const BlockHash &)> insertBlockIndex)
Definition: txdb.cpp:270
TxId
A TxId is the identifier of a transaction.
Definition: txid.h:14
CBlockTreeDB::ReadFlag
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:261
CDBWrapper::Erase
bool Erase(const K &key, bool fSync=false)
Definition: dbwrapper.h:278
LogPrint
#define LogPrint(category,...)
Definition: logging.h:193
CDiskBlockIndex::hashPrev
BlockHash hashPrev
Definition: chain.h:87
chain.h
base_blob::begin
uint8_t * begin()
Definition: uint256.h:83
BlockHash
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
name
const char * name
Definition: rest.cpp:43
ui_interface.h
CDBWrapper
Definition: dbwrapper.h:171
system.h
CBlock
Definition: block.h:55
CLIENT_VERSION
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
Definition: clientversion.h:44
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
BCLog::COINDB
@ COINDB
Definition: logging.h:56
CCoinsViewDBCursor::keyTmp
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:99
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:30
translation.h
DB_REINDEX_FLAG
static const char DB_REINDEX_FLAG
Definition: txdb.cpp:30
Vector
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
Definition: vector.h:21
vector.h
CBlockIndex::nUndoPos
unsigned int nUndoPos
Byte offset within rev?????.dat where this block's undo data is stored.
Definition: blockindex.h:45
gArgs
ArgsManager gArgs
Definition: system.cpp:76
CBlockFileInfo
Definition: blockfileinfo.h:13
CCoinsViewDBCursor::Next
void Next() override
Definition: txdb.cpp:226
CDBBatch::Erase
void Erase(const K &key)
Definition: dbwrapper.h:97
CCoinsViewDB::m_is_memory
bool m_is_memory
Definition: txdb.h:57
CBlockIndex::nNonce
uint32_t nNonce
Definition: blockindex.h:83
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
FastRandomContext::randrange
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:204
DB_LAST_BLOCK
static const char DB_LAST_BLOCK
Definition: txdb.cpp:31
DEFAULT_DB_BATCH_SIZE
static constexpr int64_t DEFAULT_DB_BATCH_SIZE
-dbbatchsize default (bytes)
Definition: txdb.h:35
CDBWrapper::Write
bool Write(const K &key, const V &value, bool fSync=false)
Definition: dbwrapper.h:256
CDBWrapper::Exists
bool Exists(const K &key) const
Definition: dbwrapper.h:262
DB_BLOCK_INDEX
static const char DB_BLOCK_INDEX
Definition: txdb.cpp:25
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
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
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
SER_READ
#define SER_READ(obj, code)
Definition: serialize.h:182
CCoinsCacheEntry::DIRTY
@ DIRTY
DIRTY means the CCoinsCacheEntry is potentially different from the version in the parent cache.
Definition: coins.h:133
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23
CBlockIndex::phashBlock
const BlockHash * phashBlock
pointer to the hash of the block, if any.
Definition: blockindex.h:27
CDiskBlockIndex::TRACK_SIZE_VERSION
static constexpr int TRACK_SIZE_VERSION
Definition: chain.h:85
CDBWrapper::CompactRange
void CompactRange(const K &key_begin, const K &key_end) const
Compact a certain range of keys in the database.
Definition: dbwrapper.h:318
CCoinsViewDBCursor::pcursor
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:98
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
CDBWrapper::WriteBatch
bool WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:190
LogPrintf
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:175
txdb.h
FastRandomContext
Fast randomness source.
Definition: random.h:129
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
DB_HEAD_BLOCKS
static const char DB_HEAD_BLOCKS
Definition: txdb.cpp:28
CDBBatch::SizeEstimate
size_t SizeEstimate() const
Definition: dbwrapper.h:112
CBlockTreeDB::ReadLastBlockFile
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:180