 |
Bitcoin Core
21.99.0
P2P Digital Currency
|
Go to the documentation of this file.
43 m_db(std::make_unique<
CDBWrapper>(ldb_path, nCacheSize, fMemory, fWipe, true)),
45 m_is_memory(fMemory) { }
55 m_db = std::make_unique<CDBWrapper>(
61 return m_db->Read(CoinEntry(&outpoint), coin);
65 return m_db->Exists(CoinEntry(&outpoint));
76 std::vector<uint256> vhashHeadBlocks;
78 return std::vector<uint256>();
80 return vhashHeadBlocks;
88 int crash_simulate =
gArgs.
GetArg(
"-dbcrashratio", 0);
95 if (old_heads.size() == 2) {
96 assert(old_heads[0] == hashBlock);
97 old_tip = old_heads[1];
108 for (CCoinsMap::iterator
it = mapCoins.begin();
it != mapCoins.end();) {
110 CoinEntry entry(&
it->first);
111 if (
it->second.coin.IsSpent())
114 batch.
Write(entry,
it->second.coin);
118 CCoinsMap::iterator itOld =
it++;
119 mapCoins.erase(itOld);
122 m_db->WriteBatch(batch);
124 if (crash_simulate) {
126 if (rng.
randrange(crash_simulate) == 0) {
127 LogPrintf(
"Simulating a crash. Goodbye.\n");
139 bool ret =
m_db->WriteBatch(batch);
140 LogPrint(
BCLog::COINDB,
"Committed %u changed transaction outputs (out of %u) to coin database...\n", (
unsigned int)changed, (
unsigned int)
count);
180 CoinEntry entry(&i->
keyTmp.second);
182 i->
keyTmp.first = entry.key;
201 return pcursor->GetValue(coin);
206 return pcursor->GetValueSize();
217 CoinEntry entry(&
keyTmp.second);
225 bool CBlockTreeDB::WriteBatchSync(
const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo,
int nLastFile,
const std::vector<const CBlockIndex*>& blockinfo) {
227 for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator
it=fileInfo.begin();
it != fileInfo.end();
it++) {
231 for (std::vector<const CBlockIndex*>::const_iterator
it=blockinfo.begin();
it != blockinfo.end();
it++) {
251 std::unique_ptr<CDBIterator> pcursor(
NewIterator());
256 while (pcursor->Valid()) {
258 std::pair<char, uint256> key;
261 if (pcursor->GetValue(diskindex)) {
275 pindexNew->
nTx = diskindex.
nTx;
278 return error(
"%s: CheckProofOfWork failed: %s", __func__, pindexNew->
ToString());
282 return error(
"%s: failed to read value", __func__);
302 std::vector<CTxOut> vout;
308 CCoins() : fCoinBase(false), vout(0),
nHeight(0) { }
310 template<
typename Stream>
312 unsigned int nCode = 0;
314 unsigned int nVersionDummy;
318 fCoinBase = nCode & 1;
319 std::vector<bool> vAvail(2,
false);
320 vAvail[0] = (nCode & 2) != 0;
321 vAvail[1] = (nCode & 4) != 0;
322 unsigned int nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1);
324 while (nMaskCode > 0) {
325 unsigned char chAvail = 0;
327 for (
unsigned int p = 0; p < 8; p++) {
328 bool f = (chAvail & (1 << p)) != 0;
335 vout.assign(vAvail.size(),
CTxOut());
336 for (
unsigned int i = 0; i < vAvail.size(); i++) {
352 std::unique_ptr<CDBIterator> pcursor(
m_db->NewIterator());
354 if (!pcursor->Valid()) {
359 LogPrintf(
"Upgrading utxo-set database...\n");
361 uiInterface.ShowProgress(
_(
"Upgrading UTXO database").translated, 0,
true);
362 size_t batch_size = 1 << 24;
365 std::pair<unsigned char, uint256> key;
367 while (pcursor->Valid()) {
371 if (pcursor->GetKey(key) && key.first ==
DB_COINS) {
372 if (
count++ % 256 == 0) {
373 uint32_t high = 0x100 * *key.second.
begin() + *(key.second.begin() + 1);
374 int percentageDone = (int)(high * 100.0 / 65536.0 + 0.5);
376 if (reportDone < percentageDone/10) {
379 reportDone = percentageDone/10;
383 if (!pcursor->GetValue(old_coins)) {
384 return error(
"%s: cannot parse CCoins record", __func__);
387 for (
size_t i = 0; i < old_coins.vout.size(); ++i) {
388 if (!old_coins.vout[i].IsNull() && !old_coins.vout[i].scriptPubKey.IsUnspendable()) {
389 Coin newcoin(std::move(old_coins.vout[i]), old_coins.nHeight, old_coins.fCoinBase);
391 CoinEntry entry(&outpoint);
392 batch.
Write(entry, newcoin);
397 m_db->WriteBatch(batch);
399 m_db->CompactRange(prev_key, key);
407 m_db->WriteBatch(batch);
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
bool Upgrade()
Attempt to update from an older database format. Returns whether an error occurred.
static const char DB_COIN
bilingual_str _(const char *psz)
Translation function.
const fs::path & GetDataDir(bool fNetSpecific)
int nFile
Which # file this block is stored in (blk?????.dat)
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
static const char DB_COINS
Batch of changes queued to be written to a CDBWrapper.
unsigned int nTx
Number of transactions in this block.
CClientUIInterface uiInterface
#define VARINT_MODE(obj, mode)
CBlockIndex * pprev
pointer to the index of the predecessor of this block
int nHeight
height of the entry in the chain. The genesis block has height 0
static const char DB_BEST_BLOCK
std::string ToString() const
bool GetKey(COutPoint &key) const override
CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe)
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
static const char DB_BLOCK_FILES
void ReadReindexing(bool &fReindexing)
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
bool Read(const K &key, V &value) const
unsigned int nDataPos
Byte offset within blk?????.dat where this block's data is stored.
Used to marshal pointers into hashes for db storage.
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
void Write(const K &key, const V &value)
Cursor for iterating over CoinsView state.
Parameters that influence chain consensus.
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
CDBIterator * NewIterator()
void Unserialize(Stream &s, char &a)
uint32_t nStatus
Verification status of this block.
An output of a transaction.
unsigned int GetValueSize() const override
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * > > &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
int32_t nVersion
block header
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
bool WriteReindexing(bool fReindexing)
bool Valid() const override
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
static const char DB_FLAG
bool GetValue(Coin &coin) const override
bool ReadFlag(const std::string &name, bool &fValue)
bool Erase(const K &key, bool fSync=false)
#define LogPrint(category,...)
uint256 GetBlockHash() const
bool WriteFlag(const std::string &name, bool fValue)
bool LoadBlockIndexGuts(const Consensus::Params &consensusParams, std::function< CBlockIndex *(const uint256 &)> insertBlockIndex)
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
std::pair< char, COutPoint > keyTmp
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
static const char DB_REINDEX_FLAG
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
unsigned int nUndoPos
Byte offset within rev?????.dat where this block's undo data is stored.
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
uint256 GetBlockHash() const
static const char DB_LAST_BLOCK
bool Write(const K &key, const V &value, bool fSync=false)
bool Exists(const K &key) const
static const char DB_BLOCK_INDEX
An outpoint - a combination of a transaction hash and an index n into its vout.
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
bool error(const char *fmt, const Args &... args)
@ DIRTY
DIRTY means the CCoinsCacheEntry is potentially different from the version in the parent cache.
The block chain is a tree shaped structure starting with the genesis block at the root,...
std::unique_ptr< CDBIterator > pcursor
std::unique_ptr< CDBWrapper > m_db
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
bool WriteBatch(CDBBatch &batch, bool fSync=false)
static const char DB_HEAD_BLOCKS
size_t SizeEstimate() const
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
bool ReadLastBlockFile(int &nFile)