 |
Bitcoin Core
22.99.0
P2P Digital Currency
|
Go to the documentation of this file.
39 return _(
"The -txindex upgrade started by a previous version can not be completed. Restart with the previous version or run a full -reindex.");
41 bool txindex_legacy_flag{
false};
42 block_tree_db.
ReadFlag(
"txindex", txindex_legacy_flag);
43 if (txindex_legacy_flag) {
45 if (!block_tree_db.
WriteFlag(
"txindex",
false)) {
46 return Untranslated(
"Failed to write block index db flag 'txindex'='0'");
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.");
66 m_db(
std::make_unique<
CDBWrapper>(ldb_path, nCacheSize, fMemory, fWipe, true)),
68 m_is_memory(fMemory) { }
78 m_db = std::make_unique<CDBWrapper>(
84 return m_db->Read(CoinEntry(&outpoint), coin);
88 return m_db->Exists(CoinEntry(&outpoint));
99 std::vector<uint256> vhashHeadBlocks;
101 return std::vector<uint256>();
103 return vhashHeadBlocks;
118 if (old_heads.size() == 2) {
119 assert(old_heads[0] == hashBlock);
120 old_tip = old_heads[1];
131 for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
133 CoinEntry entry(&it->first);
134 if (it->second.coin.IsSpent())
137 batch.
Write(entry, it->second.coin);
141 CCoinsMap::iterator itOld = it++;
142 mapCoins.erase(itOld);
145 m_db->WriteBatch(batch);
147 if (crash_simulate) {
149 if (rng.
randrange(crash_simulate) == 0) {
150 LogPrintf(
"Simulating a crash. Goodbye.\n");
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);
208 bool Valid()
const override;
209 void Next()
override;
220 auto i = std::make_unique<CCoinsViewDBCursor>(
227 if (i->pcursor->Valid()) {
228 CoinEntry entry(&i->keyTmp.second);
229 i->pcursor->GetKey(entry);
230 i->keyTmp.first = entry.key;
249 return pcursor->GetValue(coin);
254 return pcursor->GetValueSize();
265 CoinEntry entry(&
keyTmp.second);
273 bool CBlockTreeDB::WriteBatchSync(
const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo,
int nLastFile,
const std::vector<const CBlockIndex*>& blockinfo) {
275 for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
279 for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
286 return Write(std::make_pair(
DB_FLAG,
name), fValue ? uint8_t{
'1'} : uint8_t{
'0'});
293 fValue = ch == uint8_t{
'1'};
300 std::unique_ptr<CDBIterator> pcursor(
NewIterator());
304 while (pcursor->Valid()) {
306 std::pair<uint8_t, uint256> key;
309 if (pcursor->GetValue(diskindex)) {
314 pindexNew->nFile = diskindex.nFile;
315 pindexNew->nDataPos = diskindex.nDataPos;
316 pindexNew->nUndoPos = diskindex.nUndoPos;
322 pindexNew->nStatus = diskindex.nStatus;
323 pindexNew->
nTx = diskindex.
nTx;
326 return error(
"%s: CheckProofOfWork failed: %s", __func__, pindexNew->
ToString());
331 return error(
"%s: failed to read value", __func__);
351 std::vector<CTxOut> vout;
357 CCoins() : fCoinBase(false), vout(0),
nHeight(0) { }
359 template<
typename Stream>
361 unsigned int nCode = 0;
363 unsigned int nVersionDummy;
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);
373 while (nMaskCode > 0) {
374 unsigned char chAvail = 0;
376 for (
unsigned int p = 0; p < 8; p++) {
377 bool f = (chAvail & (1 << p)) != 0;
384 vout.assign(vAvail.size(),
CTxOut());
385 for (
unsigned int i = 0; i < vAvail.size(); i++) {
401 std::unique_ptr<CDBIterator> pcursor(
m_db->NewIterator());
403 if (!pcursor->Valid()) {
408 LogPrintf(
"Upgrading utxo-set database...\n");
410 uiInterface.ShowProgress(
_(
"Upgrading UTXO database").translated, 0,
true);
411 size_t batch_size = 1 << 24;
414 std::pair<unsigned char, uint256> key;
416 while (pcursor->Valid()) {
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);
425 if (reportDone < percentageDone/10) {
428 reportDone = percentageDone/10;
432 if (!pcursor->GetValue(old_coins)) {
433 return error(
"%s: cannot parse CCoins record", __func__);
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);
440 CoinEntry entry(&outpoint);
441 batch.
Write(entry, newcoin);
446 m_db->WriteBatch(batch);
448 m_db->CompactRange(prev_key, key);
456 m_db->WriteBatch(batch);
static constexpr uint8_t DB_BLOCK_FILES
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.
CCoinsView backed by the coin database (chainstate/)
bilingual_str _(const char *psz)
Translation function.
static constexpr uint8_t DB_BLOCK_INDEX
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
Batch of changes queued to be written to a CDBWrapper.
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
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 constexpr uint8_t 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)
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
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.
void ReadReindexing(bool &fReindexing)
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
bool Read(const K &key, V &value) const
Used to marshal pointers into hashes for db storage.
void Unserialize(Stream &, char)=delete
static constexpr uint8_t DB_FLAG
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
static constexpr uint8_t DB_REINDEX_FLAG
static constexpr uint8_t DB_HEAD_BLOCKS
void Write(const K &key, const V &value)
Cursor for iterating over CoinsView state.
Parameters that influence chain consensus.
static constexpr uint8_t DB_COINS
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
CDBIterator * NewIterator()
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
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)
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
int32_t nVersion
block header
static constexpr uint8_t DB_LAST_BLOCK
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
bool WriteReindexing(bool fReindexing)
bool Valid() const override
Access to the block database (blocks/index/)
std::optional< bilingual_str > CheckLegacyTxindex(CBlockTreeDB &block_tree_db)
static constexpr uint8_t DB_TXINDEX_BLOCK
bool GetValue(Coin &coin) const override
static constexpr uint8_t DB_COIN
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 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.
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
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
bool Write(const K &key, const V &value, bool fSync=false)
bool Exists(const K &key) const
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.
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
The block chain is a tree shaped structure starting with the genesis block at the root,...
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
std::unique_ptr< CDBIterator > pcursor
std::unique_ptr< CDBWrapper > m_db
bool WriteBatch(CDBBatch &batch, bool fSync=false)
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)