mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-13 17:18:00 +02:00
coins: introduce CoinsViewOverlay
Introduce `CoinsViewOverlay`, a `CCoinsViewCache` subclass that reads coins without mutating the underlying cache via `FetchCoin()`. Use `PeekCoin()` to look up a Coin through a stack of `CCoinsViewCache` layers without populating parent caches. This prevents the main cache from caching inputs pulled from disk for a block that has not yet been fully validated. Once `Flush()` is called on the view, these inputs will be added as spent to `coinsCache` in the main cache via `BatchWrite()`. This is the foundation for async input fetching, where worker threads must not mutate shared state. Co-authored-by: l0rinc <pap.lorinc@gmail.com>
This commit is contained in:
@@ -60,10 +60,15 @@ size_t CCoinsViewCache::DynamicMemoryUsage() const {
|
||||
return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
|
||||
}
|
||||
|
||||
std::optional<Coin> CCoinsViewCache::FetchCoinFromBase(const COutPoint& outpoint) const
|
||||
{
|
||||
return base->GetCoin(outpoint);
|
||||
}
|
||||
|
||||
CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
|
||||
const auto [ret, inserted] = cacheCoins.try_emplace(outpoint);
|
||||
if (inserted) {
|
||||
if (auto coin{base->GetCoin(outpoint)}) {
|
||||
if (auto coin{FetchCoinFromBase(outpoint)}) {
|
||||
ret->second.coin = std::move(*coin);
|
||||
cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
|
||||
Assert(!ret->second.coin.IsSpent());
|
||||
|
||||
Reference in New Issue
Block a user