mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 23:03:45 +01:00
coins: only adjust cachedCoinsUsage on EmplaceCoinInternalDANGER insert
`EmplaceCoinInternalDANGER()` incremented `cachedCoinsUsage` even when `try_emplace` did not insert (duplicate key), inflating the counter. This is mostly reachable in tests today since `AssumeUTXO` does not overwrite. Increment only on successful insert, and capture `coin.DynamicMemoryUsage()` before the move so accounting uses the correct value. Fuzz: add an `EmplaceCoinInternalDANGER` path to exercise insert-only accounting. Unit test: emplace two different coins at the same outpoint (with different `DynamicMemoryUsage()`), verify `SelfTest()` passes and `AccessCoin(outpoint)` returns the first coin. Co-authored-by: Andrew Toth <andrewstoth@gmail.com> Co-authored-by: w0xlt <woltx@protonmail.com>
This commit is contained in:
@@ -111,9 +111,12 @@ void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possi
|
||||
}
|
||||
|
||||
void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin) {
|
||||
cachedCoinsUsage += coin.DynamicMemoryUsage();
|
||||
const auto mem_usage{coin.DynamicMemoryUsage()};
|
||||
auto [it, inserted] = cacheCoins.try_emplace(std::move(outpoint), std::move(coin));
|
||||
if (inserted) CCoinsCacheEntry::SetDirty(*it, m_sentinel);
|
||||
if (inserted) {
|
||||
CCoinsCacheEntry::SetDirty(*it, m_sentinel);
|
||||
cachedCoinsUsage += mem_usage;
|
||||
}
|
||||
}
|
||||
|
||||
void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) {
|
||||
|
||||
Reference in New Issue
Block a user