39 std::unique_ptr<CDBIterator> cursor{
m_db->NewIterator()};
43 return cursor->Valid();
59 m_db_params{
std::move(db_params)},
60 m_options{
std::move(options)},
66 if (
m_compaction.wait_for(std::chrono::seconds{0}) != std::future_status::ready) {
90 if (
Coin coin;
m_db->Read(CoinEntry(&outpoint), coin)) {
104 return m_db->Exists(CoinEntry(&outpoint));
111 return hashBestChain;
115 std::vector<uint256> vhashHeadBlocks;
117 return std::vector<uint256>();
119 return vhashHeadBlocks;
133 if (old_heads.size() == 2) {
134 if (old_heads[0] != block_hash) {
135 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");
137 assert(old_heads[0] == block_hash);
138 old_tip = old_heads[1];
153 for (
auto it{cursor.
Begin()}; it != cursor.
End();) {
154 if (it->second.IsDirty()) {
155 CoinEntry entry(&it->first);
156 if (it->second.coin.IsSpent()) {
159 batch.
Write(entry, it->second.coin);
167 m_db->WriteBatch(batch);
172 LogError(
"Simulating a crash. Goodbye.");
184 m_db->WriteBatch(batch);
185 LogDebug(
BCLog::COINDB,
"Committed %u changed transaction outputs (out of %u) to coin database...", (
unsigned int)dirty_count, (
unsigned int)
count);
195 return m_db->GetProperty(property);
210 }
catch (
const std::exception& e) {
211 LogWarning(
"Failed chainstate compaction (%s)", e.what());
230 bool Valid()
const override;
231 void Next()
override;
242 auto i = std::make_unique<CCoinsViewDBCursor>(
249 if (i->pcursor->Valid()) {
250 CoinEntry entry(&i->keyTmp.second);
251 i->pcursor->GetKey(entry);
252 i->keyTmp.first = entry.key;
271 return pcursor->GetValue(coin);
282 CoinEntry entry(&
keyTmp.second);
#define Assert(val)
Identity function.
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 &in_block_hash)
bool Valid() const override
std::pair< char, COutPoint > keyTmp
CCoinsView backed by the coin database (chainstate/)
Mutex m_db_mutex
Prevents CompactFull() from using m_db while ResizeCache() replaces it.
std::shared_future< void > CompactFullAsync() EXCLUSIVE_LOCKS_REQUIRED(cs_main
Perform a full compaction of the underlying LevelDB on a one-shot background thread.
std::shared_future< void > m_compaction
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)
std::optional< std::string > GetDBProperty(const std::string &property)
Return an underlying LevelDB property value, if available.
std::optional< Coin > PeekCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint, without caching results.
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.
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Dynamically alter the underlying leveldb cache size.
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state. Implementations may return nullptr.
CoinsViewOptions m_options
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &block_hash) override
Do a bulk modification (multiple Coin changes + BestBlock change).
bool NeedsUpgrade()
Whether an unsupported database format is used.
size_t EstimateSize() const override
Estimate database size.
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.
static std::string PathToString(const path &path)
Convert path object to a byte string.
#define LogDebug(category,...)
void ThreadRename(const std::string &)
Rename a thread both in terms of an internal (in-memory) name as well as its system thread name.
#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.
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.
fs::path path
Location in the filesystem where leveldb data will be stored.
bool memory_only
If true, use leveldb's memory environment.
#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_BEST_BLOCK
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.