39 return _(
"The -txindex upgrade started by a previous version can not "
40 "be completed. Restart with the previous version or run a "
43 bool txindex_legacy_flag{
false};
44 block_tree_db.
ReadFlag(
"txindex", txindex_legacy_flag);
45 if (txindex_legacy_flag) {
47 if (!block_tree_db.
WriteFlag(
"txindex",
false)) {
49 "Failed to write block index db flag 'txindex'='0'");
51 return _(
"The block index db contains a legacy 'txindex'. To clear the "
52 "occupied disk space, run a full -reindex, otherwise ignore "
53 "this error. This error message will not be displayed again.");
67 TxId id = obj.outpoint->GetTxId();
68 uint32_t n = obj.outpoint->GetN();
77 : m_db(std::make_unique<
CDBWrapper>(ldb_path, nCacheSize, fMemory, fWipe,
79 m_ldb_path(ldb_path), m_is_memory(fMemory) {}
95 return m_db->Read(CoinEntry(&outpoint), coin);
99 return m_db->Exists(CoinEntry(&outpoint));
107 return hashBestChain;
111 std::vector<BlockHash> vhashHeadBlocks;
113 return std::vector<BlockHash>();
115 return vhashHeadBlocks;
131 if (old_heads.size() == 2) {
132 assert(old_heads[0] == hashBlock);
133 old_tip = old_heads[1];
144 for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
146 CoinEntry entry(&it->first);
147 if (it->second.coin.IsSpent()) {
150 batch.
Write(entry, it->second.coin);
155 CCoinsMap::iterator itOld = it++;
156 mapCoins.erase(itOld);
160 m_db->WriteBatch(batch);
162 if (crash_simulate) {
164 if (rng.
randrange(crash_simulate) == 0) {
165 LogPrintf(
"Simulating a crash. Goodbye.\n");
178 bool ret =
m_db->WriteBatch(batch);
180 "Committed %u changed transaction outputs (out of "
181 "%u) to coin database...\n",
182 (
unsigned int)changed, (
unsigned int)
count);
225 CoinEntry entry(&i->
keyTmp.second);
227 i->
keyTmp.first = entry.key;
245 return pcursor->GetValue(coin);
249 return pcursor->GetValueSize();
258 CoinEntry entry(&
keyTmp.second);
269 const std::vector<std::pair<int, const CBlockFileInfo *>> &fileInfo,
270 int nLastFile,
const std::vector<const CBlockIndex *> &blockinfo) {
272 for (std::vector<std::pair<int, const CBlockFileInfo *>>::const_iterator
273 it = fileInfo.begin();
274 it != fileInfo.end(); it++) {
278 for (std::vector<const CBlockIndex *>::const_iterator it =
280 it != blockinfo.end(); it++) {
304 std::unique_ptr<CDBIterator> pcursor(
NewIterator());
306 uint64_t version = 0;
307 pcursor->Seek(
"version");
308 if (pcursor->Valid()) {
309 pcursor->GetValue(version);
313 return error(
"%s: Invalid block index database version: %s", __func__,
320 while (pcursor->Valid()) {
324 std::pair<char, uint256> key;
330 if (!pcursor->GetValue(diskindex)) {
331 return error(
"%s : failed to read value", __func__);
338 pindexNew->nFile = diskindex.nFile;
339 pindexNew->nDataPos = diskindex.nDataPos;
340 pindexNew->nUndoPos = diskindex.nUndoPos;
346 pindexNew->nStatus = diskindex.nStatus;
347 pindexNew->
nTx = diskindex.
nTx;
351 return error(
"%s: CheckProofOfWork failed: %s", __func__,
370 std::vector<CTxOut> vout;
376 CCoins() : fCoinBase(false), vout(0),
nHeight(0) {}
378 template <
typename Stream>
void Unserialize(Stream &s) {
381 unsigned int nVersionDummy = 0;
385 fCoinBase = nCode & 1;
386 std::vector<bool> vAvail(2,
false);
387 vAvail[0] = (nCode & 2) != 0;
388 vAvail[1] = (nCode & 4) != 0;
389 uint32_t nMaskCode = (nCode / 8) + ((nCode & 6) != 0 ? 0 : 1);
391 while (nMaskCode > 0) {
394 for (
unsigned int p = 0; p < 8; p++) {
395 bool f = (chAvail & (1 << p)) != 0;
403 vout.assign(vAvail.size(),
CTxOut());
404 for (
size_t i = 0; i < vAvail.size(); i++) {
421 std::unique_ptr<CDBIterator> pcursor(
m_db->NewIterator());
423 if (!pcursor->Valid()) {
428 LogPrintf(
"Upgrading utxo-set database...\n");
429 size_t batch_size = 1 << 24;
432 std::pair<uint8_t, uint256> key;
434 while (pcursor->Valid()) {
439 if (!pcursor->GetKey(key) || key.first !=
DB_COINS) {
443 if (
count++ % 256 == 0) {
445 0x100 * *key.second.
begin() + *(key.second.begin() + 1);
446 int percentageDone = (int)(high * 100.0 / 65536.0 + 0.5);
448 percentageDone,
true);
449 if (reportDone < percentageDone / 10) {
452 reportDone = percentageDone / 10;
457 if (!pcursor->GetValue(old_coins)) {
458 return error(
"%s: cannot parse CCoins record", __func__);
461 const TxId id(key.second);
462 for (
size_t i = 0; i < old_coins.vout.size(); ++i) {
463 if (!old_coins.vout[i].IsNull() &&
464 !old_coins.vout[i].scriptPubKey.IsUnspendable()) {
465 Coin newcoin(std::move(old_coins.vout[i]), old_coins.nHeight,
466 old_coins.fCoinBase);
468 CoinEntry entry(&outpoint);
469 batch.
Write(entry, newcoin);
475 m_db->WriteBatch(batch);
477 m_db->CompactRange(prev_key, key);
484 m_db->WriteBatch(batch);
495 std::unique_ptr<CDBIterator> pcursor(
NewIterator());
497 uint64_t version = 0;
498 pcursor->Seek(
"version");
499 if (pcursor->Valid()) {
500 pcursor->GetValue(version);
514 "\nThe database is too old. The block index cannot be upgraded "
515 "and reindexing is required.\n");
522 LogPrintf(
"Updating the block index database version to %d\n",
RecursiveMutex cs_main
Global state.
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
The block chain is a tree shaped structure starting with the genesis block at the root,...
std::string ToString() const
CBlockIndex * pprev
pointer to the index of the predecessor of this block
unsigned int nTx
Number of transactions in this block.
int32_t nVersion
block header
BlockHash GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
Access to the block database (blocks/index/)
bool Upgrade(const Consensus::Params ¶ms)
Attempt to update from an older database format.
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * >> &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
bool WriteReindexing(bool fReindexing)
bool IsReindexing() const
bool ReadFlag(const std::string &name, bool &fValue)
bool ReadLastBlockFile(int &nFile)
bool LoadBlockIndexGuts(const Consensus::Params ¶ms, std::function< CBlockIndex *(const BlockHash &)> insertBlockIndex) EXCLUSIVE_LOCKS_REQUIRED(
bool WriteFlag(const std::string &name, bool fValue)
Cursor for iterating over CoinsView state.
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
std::unique_ptr< CDBIterator > pcursor
bool GetKey(COutPoint &key) const override
bool GetValue(Coin &coin) const override
bool Valid() const override
unsigned int GetValueSize() const override
std::pair< char, COutPoint > keyTmp
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
std::vector< BlockHash > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
std::unique_ptr< CDBWrapper > m_db
bool Upgrade()
Attempt to update from an older database format.
bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe)
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Batch of changes queued to be written to a CDBWrapper.
size_t SizeEstimate() const
void Write(const K &key, const V &value)
CDBIterator * NewIterator()
bool WriteBatch(CDBBatch &batch, bool fSync=false)
bool Read(const K &key, V &value) const
bool Erase(const K &key, bool fSync=false)
bool Write(const K &key, const V &value, bool fSync=false)
bool Exists(const K &key) const
Used to marshal pointers into hashes for db storage.
BlockHash GetBlockHash() const
static constexpr int TRACK_SIZE_VERSION
An outpoint - a combination of a transaction hash and an index n into its vout.
An output of a transaction.
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
#define LogPrint(category,...)
#define LogPrintfToBeContinued
These are aliases used to explicitly state that the message should not end with a newline character.
bool CheckProofOfWork(const BlockHash &hash, uint32_t nBits, const Consensus::Params ¶ms)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
#define VARINT_MODE(obj, mode)
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
void Unserialize(Stream &s, char &a)
#define SER_READ(obj, code)
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
A BlockHash is a unqiue identifier for a block.
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
@ DIRTY
DIRTY means the CCoinsCacheEntry is potentially different from the version in the parent cache.
Parameters that influence chain consensus.
A TxId is the identifier of a transaction.
bool error(const char *fmt, const Args &...args)
bilingual_str _(const char *psz)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
static constexpr uint8_t DB_TXINDEX_BLOCK
static const char DB_BLOCK_FILES
static const char DB_REINDEX_FLAG
static const char DB_COINS
std::optional< bilingual_str > CheckLegacyTxindex(CBlockTreeDB &block_tree_db)
static const char DB_HEAD_BLOCKS
static const char DB_LAST_BLOCK
static const char DB_BLOCK_INDEX
static const char DB_COIN
static const char DB_FLAG
static const char DB_BEST_BLOCK
static constexpr int64_t DEFAULT_DB_BATCH_SIZE
-dbbatchsize default (bytes)
CClientUIInterface uiInterface
std::vector< typename std::common_type< Args... >::type > Vector(Args &&...args)
Construct a vector with the specified elements.