38 std::unique_ptr<CDBIterator> cursor{
m_db->NewIterator()};
42 return cursor->Valid();
50 explicit CoinEntry(
const COutPoint* ptr) : outpoint(const_cast<COutPoint*>(ptr)) {}
65 if (
m_compaction.wait_for(std::chrono::seconds{0}) != std::future_status::ready) {
66 LogInfo(
"Waiting for background chainstate compaction of %s", fs::PathToString(
m_db_params.path));
89 if (
Coin coin;
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;
126 if (old_heads.size() == 2) {
127 if (old_heads[0] != hashBlock) {
128 LogError(
"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");
130 assert(old_heads[0] == hashBlock);
131 old_tip = old_heads[1];
146 for (
auto it{cursor.
Begin()}; it != cursor.
End();) {
147 if (it->second.IsDirty()) {
148 CoinEntry entry(&it->first);
149 if (it->second.coin.IsSpent()) {
152 batch.
Write(entry, it->second.coin);
160 m_db->WriteBatch(batch);
165 LogError(
"Simulating a crash. Goodbye.");
177 m_db->WriteBatch(batch);
178 LogDebug(
BCLog::COINDB,
"Committed %u changed transaction outputs (out of %u) to coin database...", (
unsigned int)dirty_count, (
unsigned int)
count);
188 return m_db->GetProperty(property);
203 }
catch (
const std::exception& e) {
204 LogWarning(
"Failed chainstate compaction (%s)", e.what());
223 bool Valid()
const override;
224 void Next()
override;
235 auto i = std::make_unique<CCoinsViewDBCursor>(
242 if (i->pcursor->Valid()) {
243 CoinEntry entry(&i->keyTmp.second);
244 i->pcursor->GetKey(entry);
245 i->keyTmp.first = entry.key;
264 return pcursor->GetValue(coin);
275 CoinEntry entry(&
keyTmp.second);
constexpr uint8_t DB_BEST_BLOCK
#define Assert(val)
Identity function.
CCoinsViewCursor(const uint256 &hashBlockIn)
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
friend class CCoinsViewDB
std::pair< char, COutPoint > keyTmp
Mutex m_db_mutex
Prevents CompactFull() from using m_db while ResizeCache() replaces it.
std::shared_future< void > m_compaction
bool HaveCoin(const COutPoint &outpoint) const override
std::unique_ptr< CDBWrapper > m_db
CCoinsViewDB(DBParams db_params, CoinsViewOptions options)
std::optional< std::string > GetDBProperty(const std::string &property)
Return an underlying LevelDB property value, if available.
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Dynamically alter the underlying leveldb cache size.
void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock) override
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
std::shared_future< void > CompactFull() EXCLUSIVE_LOCKS_REQUIRED(cs_main
Perform a full compaction of the underlying LevelDB on a one-shot background thread.
CoinsViewOptions m_options
std::vector< uint256 > GetHeadBlocks() const override
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.
void Write(const K &key, const V &value)
size_t ApproximateSize() const
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
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
#define LogDebug(category,...)
void ThreadRename(const std::string &)
#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.
size_t GetTotalCount() const noexcept
size_t GetDirtyCount() const noexcept
CoinsCachePair * Begin() const noexcept
CoinsCachePair * End() const noexcept
User-controlled performance and debug options.
Application-specific storage settings.
#define AssertLockHeld(cs)
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category)
static constexpr size_t WARN_FLUSH_COINS_COUNT
static constexpr uint8_t DB_HEAD_BLOCKS
static constexpr uint8_t DB_COIN
static constexpr uint8_t DB_COINS
std::vector< std::common_type_t< Args... > > Vector(Args &&... args)
Construct a vector with the specified elements.