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_params{
std::move(db_params)},
75 m_options{
std::move(options)},
93 return m_db->Read(CoinEntry(&outpoint), coin);
97 return m_db->Exists(CoinEntry(&outpoint));
104 return hashBestChain;
108 std::vector<uint256> vhashHeadBlocks;
110 return std::vector<uint256>();
112 return vhashHeadBlocks;
125 if (old_heads.size() == 2) {
126 assert(old_heads[0] == hashBlock);
127 old_tip = old_heads[1];
138 for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
140 CoinEntry entry(&it->first);
141 if (it->second.coin.IsSpent())
144 batch.
Write(entry, it->second.coin);
148 it = erase ? mapCoins.erase(it) : std::next(it);
151 m_db->WriteBatch(batch);
156 LogPrintf(
"Simulating a crash. Goodbye.\n");
168 bool ret =
m_db->WriteBatch(batch);
169 LogPrint(
BCLog::COINDB,
"Committed %u changed transaction outputs (out of %u) to coin database...\n", (
unsigned int)changed, (
unsigned int)
count);
210 bool Valid()
const override;
211 void Next()
override;
222 auto i = std::make_unique<CCoinsViewDBCursor>(
229 if (i->pcursor->Valid()) {
230 CoinEntry entry(&i->keyTmp.second);
231 i->pcursor->GetKey(entry);
232 i->keyTmp.first = entry.key;
251 return pcursor->GetValue(coin);
262 CoinEntry entry(&
keyTmp.second);
270 bool CBlockTreeDB::WriteBatchSync(
const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo,
int nLastFile,
const std::vector<const CBlockIndex*>& blockinfo) {
272 for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
276 for (std::vector<const CBlockIndex*>::const_iterator it=blockinfo.begin(); it != blockinfo.end(); it++) {
283 return Write(std::make_pair(
DB_FLAG,
name), fValue ? uint8_t{
'1'} : uint8_t{
'0'});
290 fValue = ch == uint8_t{
'1'};
297 std::unique_ptr<CDBIterator> pcursor(
NewIterator());
301 while (pcursor->Valid()) {
303 std::pair<uint8_t, uint256> key;
306 if (pcursor->GetValue(diskindex)) {
311 pindexNew->nFile = diskindex.nFile;
312 pindexNew->nDataPos = diskindex.nDataPos;
313 pindexNew->nUndoPos = diskindex.nUndoPos;
319 pindexNew->nStatus = diskindex.nStatus;
320 pindexNew->
nTx = diskindex.
nTx;
323 return error(
"%s: CheckProofOfWork failed: %s", __func__, pindexNew->
ToString());
328 return error(
"%s: failed to read value", __func__);
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
uint256 GetBlockHash() const
unsigned int nTx
Number of transactions in this block.
int32_t nVersion
block header
int nHeight
height of the entry in the chain. The genesis block has height 0
Access to the block database (blocks/index/)
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
bool WriteReindexing(bool fReindexing)
void ReadReindexing(bool &fReindexing)
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * > > &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
bool ReadFlag(const std::string &name, bool &fValue)
bool ReadLastBlockFile(int &nFile)
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
~CCoinsViewDBCursor()=default
bool GetValue(Coin &coin) const override
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
bool Valid() const override
std::pair< char, COutPoint > keyTmp
CCoinsView backed by the coin database (chainstate/)
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 BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, bool erase=true) override
Do a bulk modification (multiple Coin changes + BestBlock change).
CCoinsViewDB(DBParams db_params, CoinsViewOptions options)
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
CoinsViewOptions m_options
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
bool NeedsUpgrade()
Whether an unsupported database format is used.
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.
uint256 ConstructBlockHash() const
An outpoint - a combination of a transaction hash and an index n into its vout.
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
constexpr bool IsNull() const
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
#define LogPrint(category,...)
bool error(const char *fmt, const Args &... args)
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.
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
bool ShutdownRequested()
Returns true if a shutdown is requested, false otherwise.
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.
User-controlled performance and debug options.
int simulate_crash_ratio
If non-zero, randomly exit when the database is flushed with (1/ratio) probability.
size_t batch_write_bytes
Maximum database write batch size in bytes.
Parameters that influence chain consensus.
Application-specific storage settings.
bool wipe_data
If true, remove all existing data.
size_t cache_bytes
Configures various leveldb cache settings.
bool memory_only
If true, use leveldb's memory environment.
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 constexpr uint8_t DB_LAST_BLOCK
static constexpr uint8_t DB_HEAD_BLOCKS
static constexpr uint8_t DB_REINDEX_FLAG
std::optional< bilingual_str > CheckLegacyTxindex(CBlockTreeDB &block_tree_db)
static constexpr uint8_t DB_BEST_BLOCK
static constexpr uint8_t DB_COIN
static constexpr uint8_t DB_FLAG
static constexpr uint8_t DB_COINS
static constexpr uint8_t DB_BLOCK_FILES
static constexpr uint8_t DB_BLOCK_INDEX
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.