38 return _(
"The -txindex upgrade started by a previous version cannot be completed. Restart with the previous version or run a full -reindex.");
40 bool txindex_legacy_flag{
false};
41 block_tree_db.
ReadFlag(
"txindex", txindex_legacy_flag);
42 if (txindex_legacy_flag) {
44 if (!block_tree_db.
WriteFlag(
"txindex",
false)) {
45 return Untranslated(
"Failed to write block index db flag 'txindex'='0'");
47 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.");
54 std::unique_ptr<CDBIterator> cursor{
m_db->NewIterator()};
58 return cursor->Valid();
74 m_db(
std::make_unique<
CDBWrapper>(ldb_path, nCacheSize, fMemory, fWipe, true)),
76 m_is_memory(fMemory) { }
86 m_db = std::make_unique<CDBWrapper>(
92 return m_db->Read(CoinEntry(&outpoint), coin);
96 return m_db->Exists(CoinEntry(&outpoint));
103 return hashBestChain;
107 std::vector<uint256> vhashHeadBlocks;
109 return std::vector<uint256>();
111 return vhashHeadBlocks;
123 if (old_tip.IsNull()) {
126 if (old_heads.size() == 2) {
127 assert(old_heads[0] == hashBlock);
128 old_tip = old_heads[1];
139 for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
141 CoinEntry entry(&it->first);
142 if (it->second.coin.IsSpent())
145 batch.
Write(entry, it->second.coin);
149 CCoinsMap::iterator itOld = it++;
150 mapCoins.erase(itOld);
153 m_db->WriteBatch(batch);
155 if (crash_simulate) {
157 if (rng.
randrange(crash_simulate) == 0) {
158 LogPrintf(
"Simulating a crash. Goodbye.\n");
170 bool ret =
m_db->WriteBatch(batch);
171 LogPrint(
BCLog::COINDB,
"Committed %u changed transaction outputs (out of %u) to coin database...\n", (
unsigned int)changed, (
unsigned int)
count);
215 bool Valid()
const override;
216 void Next()
override;
227 auto i = std::make_unique<CCoinsViewDBCursor>(
234 if (i->pcursor->Valid()) {
235 CoinEntry entry(&i->keyTmp.second);
236 i->pcursor->GetKey(entry);
237 i->keyTmp.first = entry.key;
256 return pcursor->GetValue(coin);
267 CoinEntry entry(&
keyTmp.second);
275 bool CBlockTreeDB::WriteBatchSync(
const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo,
int nLastFile,
const std::vector<const CBlockIndex*>& blockinfo) {
277 for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
281 for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
288 return Write(std::make_pair(
DB_FLAG,
name), fValue ? uint8_t{
'1'} : uint8_t{
'0'});
295 fValue = ch == uint8_t{
'1'};
302 std::unique_ptr<CDBIterator> pcursor(
NewIterator());
306 while (pcursor->Valid()) {
308 std::pair<uint8_t, uint256> key;
311 if (pcursor->GetValue(diskindex)) {
316 pindexNew->nFile = diskindex.nFile;
317 pindexNew->nDataPos = diskindex.nDataPos;
318 pindexNew->nUndoPos = diskindex.nUndoPos;
324 pindexNew->nStatus = diskindex.nStatus;
325 pindexNew->
nTx = diskindex.
nTx;
328 return error(
"%s: CheckProofOfWork failed: %s", __func__, pindexNew->
ToString());
333 return error(
"%s: failed to read value", __func__);
bool Exists(const K &key) const
bool GetValue(Coin &coin) const override
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
std::string ToString() const
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
#define LogPrint(category,...)
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Batch of changes queued to be written to a CDBWrapper.
static constexpr uint8_t DB_BLOCK_INDEX
bool ReadLastBlockFile(int &nFile)
CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe)
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
std::unique_ptr< CDBIterator > pcursor
void ReadReindexing(bool &fReindexing)
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
static constexpr uint8_t DB_BEST_BLOCK
static constexpr uint8_t DB_HEAD_BLOCKS
bool GetKey(COutPoint &key) const override
static constexpr uint8_t DB_LAST_BLOCK
bool WriteReindexing(bool fReindexing)
bool NeedsUpgrade()
Whether an unsupported database format is used.
CDBIterator * NewIterator()
DIRTY means the CCoinsCacheEntry is potentially different from the version in the parent cache...
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
uint256 GetBlockHash() const
static constexpr uint8_t DB_FLAG
Access to the block database (blocks/index/)
bool Erase(const K &key, bool fSync=false)
std::optional< bilingual_str > CheckLegacyTxindex(CBlockTreeDB &block_tree_db)
static constexpr uint8_t DB_TXINDEX_BLOCK
static constexpr uint8_t DB_REINDEX_FLAG
bilingual_str _(const char *psz)
Translation function.
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
static constexpr uint8_t DB_COINS
void Write(const K &key, const V &value)
size_t SizeEstimate() const
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Used to marshal pointers into hashes for db storage.
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Parameters that influence chain consensus.
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.
An outpoint - a combination of a transaction hash and an index n into its vout.
std::pair< char, COutPoint > keyTmp
bool Read(const K &key, V &value) const
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
bool ReadFlag(const std::string &name, bool &fValue)
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo *> > &fileInfo, int nLastFile, const std::vector< const CBlockIndex *> &blockinfo)
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
~CCoinsViewDBCursor()=default
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
int32_t nVersion
block header
uint256 ConstructBlockHash() const
bool Write(const K &key, const V &value, bool fSync=false)
static constexpr uint8_t DB_COIN
The block chain is a tree shaped structure starting with the genesis block at the root...
CCoinsView backed by the coin database (chainstate/)
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
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).
bool WriteBatch(CDBBatch &batch, bool fSync=false)
static constexpr uint8_t DB_BLOCK_FILES
bool Valid() const override
int nHeight
height of the entry in the chain. The genesis block has height 0
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool error(const char *fmt, const Args &... args)
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
unsigned int nTx
Number of transactions in this block.
std::unique_ptr< CDBWrapper > m_db
Cursor for iterating over CoinsView state.