30 std::unique_ptr<CDBIterator> cursor{
m_db->NewIterator()};
34 return cursor->Valid();
50 m_db_params{
std::move(db_params)},
51 m_options{
std::move(options)},
70 if (
Coin coin;
m_db->Read(CoinEntry(&outpoint), coin))
return coin;
75 return m_db->Exists(CoinEntry(&outpoint));
86 std::vector<uint256> vhashHeadBlocks;
88 return std::vector<uint256>();
90 return vhashHeadBlocks;
103 if (old_heads.size() == 2) {
104 if (old_heads[0] != hashBlock) {
105 LogPrintLevel(
BCLog::COINDB,
BCLog::Level::Error,
"The coins database detected an inconsistent state, likely due to a previous crash or shutdown. You will need to restart bitcoind with the -reindex-chainstate or -reindex configuration option.\n");
107 assert(old_heads[0] == hashBlock);
108 old_tip = old_heads[1];
119 for (
auto it{cursor.
Begin()}; it != cursor.
End();) {
120 if (it->second.IsDirty()) {
121 CoinEntry entry(&it->first);
122 if (it->second.coin.IsSpent())
125 batch.
Write(entry, it->second.coin);
132 m_db->WriteBatch(batch);
137 LogPrintf(
"Simulating a crash. Goodbye.\n");
149 bool ret =
m_db->WriteBatch(batch);
150 LogDebug(
BCLog::COINDB,
"Committed %u changed transaction outputs (out of %u) to coin database...\n", (
unsigned int)changed, (
unsigned int)
count);
172 bool Valid()
const override;
173 void Next()
override;
184 auto i = std::make_unique<CCoinsViewDBCursor>(
191 if (i->pcursor->Valid()) {
192 CoinEntry entry(&i->keyTmp.second);
193 i->pcursor->GetKey(entry);
194 i->keyTmp.first = entry.key;
213 return pcursor->GetValue(coin);
224 CoinEntry entry(&
keyTmp.second);
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 BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
std::unique_ptr< CDBWrapper > m_db
CCoinsViewDB(DBParams db_params, CoinsViewOptions options)
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
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()
An outpoint - a combination of a transaction hash and an index n into its vout.
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
constexpr bool IsNull() const
#define LogPrintLevel(category, level,...)
#define LogDebug(category,...)
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
constexpr CoinEntry(const CAmount v, const State s)
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
CoinsCachePair * NextAndMaybeErase(CoinsCachePair ¤t) noexcept
Return the next entry after current, possibly erasing current.
CoinsCachePair * Begin() const noexcept
CoinsCachePair * End() const noexcept
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.
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.
static constexpr uint8_t DB_HEAD_BLOCKS
static constexpr uint8_t DB_BEST_BLOCK
static constexpr uint8_t DB_COIN
static constexpr uint8_t DB_COINS
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.