From 763be0ac98338d023d12749387b4ab1cf792069a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Fri, 18 Apr 2025 11:24:30 +0200 Subject: [PATCH] coins: only adjust `cachedCoinsUsage` on `EmplaceCoinInternalDANGER` inserts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Guarantees counter stays balanced both on insert and on in‑place replacement. Note that this is currently only called from AssumeUTXO code where it should only insert. Co-authored-by: Andrew Toth --- src/coins.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/coins.cpp b/src/coins.cpp index 42e83dab8c3..bf858791fdc 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -111,9 +111,11 @@ void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possi } void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin) { - cachedCoinsUsage += 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 += it->second.coin.DynamicMemoryUsage(); + } } void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) {