38 std::unique_ptr<CDBIterator>
cursor{
m_db->NewIterator()};
59 m_options{
std::move(options)},
65 if (
m_compaction.wait_for(std::chrono::seconds{0}) != std::future_status::ready) {
89 if (
Coin coin;
m_db->Read(CoinEntry(&outpoint), coin)) {
97 return m_db->Exists(CoinEntry(&outpoint));
110 return std::vector<uint256>();
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");
147 if (it->second.IsDirty()) {
148 CoinEntry entry(&it->first);
149 if (it->second.coin.IsSpent()) {
152 batch.
Write(entry, it->second.coin);
156 it =
cursor.NextAndMaybeErase(*it);
160 m_db->WriteBatch(batch);
165 LogError(
"Simulating a crash. Goodbye.");
177 m_db->WriteBatch(batch);
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.
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/)
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
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.
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.
void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
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
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.
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.
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 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_BEST_BLOCK
static constexpr uint8_t DB_COIN
static constexpr uint8_t DB_COINS
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
std::vector< std::common_type_t< Args... > > Vector(Args &&... args)
Construct a vector with the specified elements.