2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2012-2022 The Bitcoin Core developers
|
2014-10-31 08:43:19 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-05 02:47:07 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <coins.h>
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <consensus/consensus.h>
|
2019-03-27 14:17:13 -04:00
|
|
|
#include <logging.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <random.h>
|
2021-09-03 22:08:53 +05:30
|
|
|
#include <util/trace.h>
|
2014-07-09 17:25:09 +02:00
|
|
|
|
2023-11-23 16:51:36 +01:00
|
|
|
TRACEPOINT_SEMAPHORE(utxocache, add);
|
|
|
|
TRACEPOINT_SEMAPHORE(utxocache, spent);
|
|
|
|
TRACEPOINT_SEMAPHORE(utxocache, uncache);
|
|
|
|
|
2024-09-07 23:12:35 +02:00
|
|
|
std::optional<Coin> CCoinsView::GetCoin(const COutPoint& outpoint) const { return std::nullopt; }
|
2014-12-15 09:11:16 +01:00
|
|
|
uint256 CCoinsView::GetBestBlock() const { return uint256(); }
|
2017-04-19 09:34:30 -07:00
|
|
|
std::vector<uint256> CCoinsView::GetHeadBlocks() const { return std::vector<uint256>(); }
|
2024-07-17 23:11:48 -04:00
|
|
|
bool CCoinsView::BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashBlock) { return false; }
|
2021-06-16 16:27:20 -04:00
|
|
|
std::unique_ptr<CCoinsViewCursor> CCoinsView::Cursor() const { return nullptr; }
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2017-06-13 12:17:30 -07:00
|
|
|
bool CCoinsView::HaveCoin(const COutPoint &outpoint) const
|
|
|
|
{
|
2024-09-07 23:12:35 +02:00
|
|
|
return GetCoin(outpoint).has_value();
|
2017-06-13 12:17:30 -07:00
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2014-09-24 03:19:04 +02:00
|
|
|
CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }
|
2024-09-07 23:12:35 +02:00
|
|
|
std::optional<Coin> CCoinsViewBacked::GetCoin(const COutPoint& outpoint) const { return base->GetCoin(outpoint); }
|
2017-05-30 17:58:54 -07:00
|
|
|
bool CCoinsViewBacked::HaveCoin(const COutPoint &outpoint) const { return base->HaveCoin(outpoint); }
|
2014-07-19 16:42:48 +02:00
|
|
|
uint256 CCoinsViewBacked::GetBestBlock() const { return base->GetBestBlock(); }
|
2017-04-19 09:34:30 -07:00
|
|
|
std::vector<uint256> CCoinsViewBacked::GetHeadBlocks() const { return base->GetHeadBlocks(); }
|
2013-11-05 02:47:07 +01:00
|
|
|
void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; }
|
2024-07-17 23:11:48 -04:00
|
|
|
bool CCoinsViewBacked::BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashBlock) { return base->BatchWrite(cursor, hashBlock); }
|
2021-06-16 16:27:20 -04:00
|
|
|
std::unique_ptr<CCoinsViewCursor> CCoinsViewBacked::Cursor() const { return base->Cursor(); }
|
2017-05-12 15:19:19 -07:00
|
|
|
size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); }
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2023-02-01 18:52:11 -05:00
|
|
|
CCoinsViewCache::CCoinsViewCache(CCoinsView* baseIn, bool deterministic) :
|
|
|
|
CCoinsViewBacked(baseIn), m_deterministic(deterministic),
|
2022-06-11 11:27:38 +02:00
|
|
|
cacheCoins(0, SaltedOutpointHasher(/*deterministic=*/deterministic), CCoinsMap::key_equal{}, &m_cache_coins_memory_resource)
|
2024-06-28 17:14:33 -04:00
|
|
|
{
|
|
|
|
m_sentinel.second.SelfRef(m_sentinel);
|
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
|
2015-05-04 01:31:11 +02:00
|
|
|
size_t CCoinsViewCache::DynamicMemoryUsage() const {
|
|
|
|
return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
|
|
|
|
}
|
|
|
|
|
2017-05-30 17:58:54 -07:00
|
|
|
CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
|
2024-06-23 23:56:14 +02:00
|
|
|
const auto [ret, inserted] = cacheCoins.try_emplace(outpoint);
|
|
|
|
if (inserted) {
|
2024-09-07 23:12:35 +02:00
|
|
|
if (auto coin{base->GetCoin(outpoint)}) {
|
|
|
|
ret->second.coin = std::move(*coin);
|
|
|
|
cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
|
|
|
|
if (ret->second.coin.IsSpent()) { // TODO GetCoin cannot return spent coins
|
|
|
|
// The parent only has an empty entry for this outpoint; we can consider our version as fresh.
|
2024-09-13 10:58:16 +02:00
|
|
|
CCoinsCacheEntry::SetFresh(*ret, m_sentinel);
|
2024-09-07 23:12:35 +02:00
|
|
|
}
|
|
|
|
} else {
|
2024-06-23 23:56:14 +02:00
|
|
|
cacheCoins.erase(ret);
|
|
|
|
return cacheCoins.end();
|
|
|
|
}
|
2014-09-03 09:37:47 +02:00
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-09-07 23:12:35 +02:00
|
|
|
std::optional<Coin> CCoinsViewCache::GetCoin(const COutPoint& outpoint) const
|
2024-09-08 21:57:18 +02:00
|
|
|
{
|
2024-09-07 23:12:35 +02:00
|
|
|
if (auto it{FetchCoin(outpoint)}; it != cacheCoins.end() && !it->second.coin.IsSpent()) return it->second.coin;
|
2024-09-08 21:57:18 +02:00
|
|
|
return std::nullopt;
|
2014-07-19 16:42:48 +02:00
|
|
|
}
|
|
|
|
|
2017-04-25 11:29:29 -07:00
|
|
|
void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) {
|
2017-05-30 17:58:54 -07:00
|
|
|
assert(!coin.IsSpent());
|
2017-04-25 11:29:29 -07:00
|
|
|
if (coin.out.scriptPubKey.IsUnspendable()) return;
|
|
|
|
CCoinsMap::iterator it;
|
|
|
|
bool inserted;
|
2017-04-25 11:29:39 -07:00
|
|
|
std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
|
2017-04-25 11:29:29 -07:00
|
|
|
bool fresh = false;
|
|
|
|
if (!inserted) {
|
2017-05-04 00:15:36 -07:00
|
|
|
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
|
2017-04-25 11:29:29 -07:00
|
|
|
}
|
|
|
|
if (!possible_overwrite) {
|
2017-05-30 17:58:54 -07:00
|
|
|
if (!it->second.coin.IsSpent()) {
|
2020-01-30 15:52:36 -05:00
|
|
|
throw std::logic_error("Attempted to overwrite an unspent coin (when possible_overwrite is false)");
|
2017-04-25 11:29:29 -07:00
|
|
|
}
|
2020-01-30 15:52:36 -05:00
|
|
|
// If the coin exists in this cache as a spent coin and is DIRTY, then
|
|
|
|
// its spentness hasn't been flushed to the parent cache. We're
|
|
|
|
// re-adding the coin to this cache now but we can't mark it as FRESH.
|
|
|
|
// If we mark it FRESH and then spend it before the cache is flushed
|
|
|
|
// we would remove it from this cache and would never flush spentness
|
|
|
|
// to the parent cache.
|
|
|
|
//
|
|
|
|
// Re-adding a spent coin can happen in the case of a re-org (the coin
|
|
|
|
// is 'spent' when the block adding it is disconnected and then
|
|
|
|
// re-added when it is also added in a newly connected block).
|
|
|
|
//
|
|
|
|
// If the coin doesn't exist in the current cache, or is spent but not
|
|
|
|
// DIRTY, then it can be marked FRESH.
|
2024-06-28 16:11:16 -04:00
|
|
|
fresh = !it->second.IsDirty();
|
2017-04-25 11:29:29 -07:00
|
|
|
}
|
2017-05-04 00:15:36 -07:00
|
|
|
it->second.coin = std::move(coin);
|
2024-09-13 10:58:16 +02:00
|
|
|
CCoinsCacheEntry::SetDirty(*it, m_sentinel);
|
|
|
|
if (fresh) CCoinsCacheEntry::SetFresh(*it, m_sentinel);
|
2017-05-04 00:15:36 -07:00
|
|
|
cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
|
2023-11-23 11:06:17 +01:00
|
|
|
TRACEPOINT(utxocache, add,
|
2021-09-03 22:08:53 +05:30
|
|
|
outpoint.hash.data(),
|
|
|
|
(uint32_t)outpoint.n,
|
2022-07-21 12:55:20 +03:00
|
|
|
(uint32_t)it->second.coin.nHeight,
|
|
|
|
(int64_t)it->second.coin.out.nValue,
|
|
|
|
(bool)it->second.coin.IsCoinBase());
|
2017-04-25 11:29:29 -07:00
|
|
|
}
|
|
|
|
|
2020-08-25 13:50:23 -04:00
|
|
|
void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin) {
|
|
|
|
cachedCoinsUsage += coin.DynamicMemoryUsage();
|
2024-09-13 10:58:16 +02:00
|
|
|
auto [it, inserted] = cacheCoins.try_emplace(std::move(outpoint), std::move(coin));
|
|
|
|
if (inserted) CCoinsCacheEntry::SetDirty(*it, m_sentinel);
|
2020-08-25 13:50:23 -04:00
|
|
|
}
|
|
|
|
|
2020-03-23 10:45:34 -04:00
|
|
|
void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) {
|
2017-04-25 11:29:29 -07:00
|
|
|
bool fCoinbase = tx.IsCoinBase();
|
2023-10-11 14:53:04 +01:00
|
|
|
const Txid& txid = tx.GetHash();
|
2017-04-25 11:29:29 -07:00
|
|
|
for (size_t i = 0; i < tx.vout.size(); ++i) {
|
2020-03-23 10:45:34 -04:00
|
|
|
bool overwrite = check_for_overwrite ? cache.HaveCoin(COutPoint(txid, i)) : fCoinbase;
|
|
|
|
// Coinbase transactions can always be overwritten, in order to correctly
|
2017-06-06 15:03:36 +02:00
|
|
|
// deal with the pre-BIP30 occurrences of duplicate coinbase transactions.
|
2017-04-19 09:34:30 -07:00
|
|
|
cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase), overwrite);
|
2017-04-25 11:29:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 11:50:47 -04:00
|
|
|
bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
|
2017-05-30 17:58:54 -07:00
|
|
|
CCoinsMap::iterator it = FetchCoin(outpoint);
|
2017-06-05 11:50:47 -04:00
|
|
|
if (it == cacheCoins.end()) return false;
|
2017-05-04 00:15:36 -07:00
|
|
|
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
|
2023-11-23 11:06:17 +01:00
|
|
|
TRACEPOINT(utxocache, spent,
|
2021-09-03 22:08:53 +05:30
|
|
|
outpoint.hash.data(),
|
|
|
|
(uint32_t)outpoint.n,
|
|
|
|
(uint32_t)it->second.coin.nHeight,
|
|
|
|
(int64_t)it->second.coin.out.nValue,
|
|
|
|
(bool)it->second.coin.IsCoinBase());
|
2017-04-25 11:29:39 -07:00
|
|
|
if (moveout) {
|
2017-05-04 00:15:36 -07:00
|
|
|
*moveout = std::move(it->second.coin);
|
2017-04-25 11:29:29 -07:00
|
|
|
}
|
2024-06-28 16:11:16 -04:00
|
|
|
if (it->second.IsFresh()) {
|
2017-04-25 11:29:29 -07:00
|
|
|
cacheCoins.erase(it);
|
|
|
|
} else {
|
2024-09-13 10:58:16 +02:00
|
|
|
CCoinsCacheEntry::SetDirty(*it, m_sentinel);
|
2017-05-04 00:15:36 -07:00
|
|
|
it->second.coin.Clear();
|
2014-09-02 21:21:15 +02:00
|
|
|
}
|
2017-06-05 11:50:47 -04:00
|
|
|
return true;
|
2014-07-19 16:42:48 +02:00
|
|
|
}
|
|
|
|
|
2017-04-25 11:29:29 -07:00
|
|
|
static const Coin coinEmpty;
|
|
|
|
|
2017-04-25 11:29:39 -07:00
|
|
|
const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const {
|
2017-05-30 17:58:54 -07:00
|
|
|
CCoinsMap::const_iterator it = FetchCoin(outpoint);
|
2017-04-25 11:29:39 -07:00
|
|
|
if (it == cacheCoins.end()) {
|
2017-04-25 11:29:29 -07:00
|
|
|
return coinEmpty;
|
|
|
|
} else {
|
2017-05-04 00:15:36 -07:00
|
|
|
return it->second.coin;
|
2017-04-25 11:29:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 17:58:54 -07:00
|
|
|
bool CCoinsViewCache::HaveCoin(const COutPoint &outpoint) const {
|
|
|
|
CCoinsMap::const_iterator it = FetchCoin(outpoint);
|
|
|
|
return (it != cacheCoins.end() && !it->second.coin.IsSpent());
|
2017-04-25 11:29:29 -07:00
|
|
|
}
|
|
|
|
|
2017-05-30 17:58:54 -07:00
|
|
|
bool CCoinsViewCache::HaveCoinInCache(const COutPoint &outpoint) const {
|
2017-04-25 11:29:39 -07:00
|
|
|
CCoinsMap::const_iterator it = cacheCoins.find(outpoint);
|
2017-06-08 16:56:50 -04:00
|
|
|
return (it != cacheCoins.end() && !it->second.coin.IsSpent());
|
2015-10-22 15:49:53 -07:00
|
|
|
}
|
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
uint256 CCoinsViewCache::GetBestBlock() const {
|
2014-12-15 09:11:16 +01:00
|
|
|
if (hashBlock.IsNull())
|
2013-11-05 02:47:07 +01:00
|
|
|
hashBlock = base->GetBestBlock();
|
|
|
|
return hashBlock;
|
|
|
|
}
|
|
|
|
|
2014-09-03 09:25:32 +02:00
|
|
|
void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) {
|
2013-11-05 02:47:07 +01:00
|
|
|
hashBlock = hashBlockIn;
|
|
|
|
}
|
|
|
|
|
2024-07-17 23:11:48 -04:00
|
|
|
bool CCoinsViewCache::BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashBlockIn) {
|
|
|
|
for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) {
|
2017-09-16 18:47:19 +03:00
|
|
|
// Ignore non-dirty entries (optimization).
|
2024-06-28 16:11:16 -04:00
|
|
|
if (!it->second.IsDirty()) {
|
2017-09-16 18:47:19 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
CCoinsMap::iterator itUs = cacheCoins.find(it->first);
|
|
|
|
if (itUs == cacheCoins.end()) {
|
2020-01-30 15:52:36 -05:00
|
|
|
// The parent cache does not have an entry, while the child cache does.
|
|
|
|
// We can ignore it if it's both spent and FRESH in the child
|
2024-06-28 16:11:16 -04:00
|
|
|
if (!(it->second.IsFresh() && it->second.coin.IsSpent())) {
|
2020-01-30 15:52:36 -05:00
|
|
|
// Create the coin in the parent cache, move the data up
|
|
|
|
// and mark it as dirty.
|
2024-06-28 17:14:33 -04:00
|
|
|
itUs = cacheCoins.try_emplace(it->first).first;
|
|
|
|
CCoinsCacheEntry& entry{itUs->second};
|
2024-07-17 23:11:48 -04:00
|
|
|
if (cursor.WillErase(*it)) {
|
|
|
|
// Since this entry will be erased,
|
|
|
|
// we can move the coin into us instead of copying it
|
2019-12-03 13:25:35 -05:00
|
|
|
entry.coin = std::move(it->second.coin);
|
|
|
|
} else {
|
|
|
|
entry.coin = it->second.coin;
|
|
|
|
}
|
2017-09-16 18:47:19 +03:00
|
|
|
cachedCoinsUsage += entry.coin.DynamicMemoryUsage();
|
2024-09-13 10:58:16 +02:00
|
|
|
CCoinsCacheEntry::SetDirty(*itUs, m_sentinel);
|
2017-09-16 18:47:19 +03:00
|
|
|
// We can mark it FRESH in the parent if it was FRESH in the child
|
|
|
|
// Otherwise it might have just been flushed from the parent's cache
|
|
|
|
// and already exist in the grandparent
|
2024-09-13 10:58:16 +02:00
|
|
|
if (it->second.IsFresh()) CCoinsCacheEntry::SetFresh(*itUs, m_sentinel);
|
2017-09-16 18:47:19 +03:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-30 15:52:36 -05:00
|
|
|
// Found the entry in the parent cache
|
2024-06-28 16:11:16 -04:00
|
|
|
if (it->second.IsFresh() && !itUs->second.coin.IsSpent()) {
|
2020-01-30 15:52:36 -05:00
|
|
|
// The coin was marked FRESH in the child cache, but the coin
|
|
|
|
// exists in the parent cache. If this ever happens, it means
|
|
|
|
// the FRESH flag was misapplied and there is a logic error in
|
|
|
|
// the calling code.
|
|
|
|
throw std::logic_error("FRESH flag misapplied to coin that exists in parent cache");
|
2017-09-16 18:47:19 +03:00
|
|
|
}
|
|
|
|
|
2024-06-28 16:11:16 -04:00
|
|
|
if (itUs->second.IsFresh() && it->second.coin.IsSpent()) {
|
2020-01-30 15:52:36 -05:00
|
|
|
// The grandparent cache does not have an entry, and the coin
|
|
|
|
// has been spent. We can just delete it from the parent cache.
|
2017-09-16 18:47:19 +03:00
|
|
|
cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
|
|
|
|
cacheCoins.erase(itUs);
|
2014-09-03 09:37:47 +02:00
|
|
|
} else {
|
2017-09-16 18:47:19 +03:00
|
|
|
// A normal modification.
|
|
|
|
cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
|
2024-07-17 23:11:48 -04:00
|
|
|
if (cursor.WillErase(*it)) {
|
|
|
|
// Since this entry will be erased,
|
|
|
|
// we can move the coin into us instead of copying it
|
2019-12-03 13:25:35 -05:00
|
|
|
itUs->second.coin = std::move(it->second.coin);
|
|
|
|
} else {
|
|
|
|
itUs->second.coin = it->second.coin;
|
|
|
|
}
|
2017-09-16 18:47:19 +03:00
|
|
|
cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage();
|
2024-09-13 10:58:16 +02:00
|
|
|
CCoinsCacheEntry::SetDirty(*itUs, m_sentinel);
|
2020-01-30 15:52:36 -05:00
|
|
|
// NOTE: It isn't safe to mark the coin as FRESH in the parent
|
|
|
|
// cache. If it already existed and was spent in the parent
|
|
|
|
// cache then marking it FRESH would prevent that spentness
|
|
|
|
// from being flushed to the grandparent.
|
2014-09-03 09:37:47 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-24 02:08:05 +02:00
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
hashBlock = hashBlockIn;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCoinsViewCache::Flush() {
|
2024-07-17 23:11:48 -04:00
|
|
|
auto cursor{CoinsViewCacheCursor(cachedCoinsUsage, m_sentinel, cacheCoins, /*will_erase=*/true)};
|
|
|
|
bool fOk = base->BatchWrite(cursor, hashBlock);
|
2022-06-11 11:00:53 +02:00
|
|
|
if (fOk) {
|
2024-07-17 23:11:48 -04:00
|
|
|
cacheCoins.clear();
|
2022-06-11 11:00:53 +02:00
|
|
|
ReallocateCache();
|
2023-01-30 11:45:14 -05:00
|
|
|
}
|
2015-05-04 01:31:11 +02:00
|
|
|
cachedCoinsUsage = 0;
|
2013-11-05 02:47:07 +01:00
|
|
|
return fOk;
|
|
|
|
}
|
|
|
|
|
2019-12-03 13:25:35 -05:00
|
|
|
bool CCoinsViewCache::Sync()
|
|
|
|
{
|
2024-07-17 23:11:48 -04:00
|
|
|
auto cursor{CoinsViewCacheCursor(cachedCoinsUsage, m_sentinel, cacheCoins, /*will_erase=*/false)};
|
|
|
|
bool fOk = base->BatchWrite(cursor, hashBlock);
|
2024-07-17 23:13:07 -04:00
|
|
|
if (fOk) {
|
|
|
|
if (m_sentinel.second.Next() != &m_sentinel) {
|
|
|
|
/* BatchWrite must clear flags of all entries */
|
|
|
|
throw std::logic_error("Not all unspent flagged entries were cleared");
|
2019-12-03 13:25:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fOk;
|
|
|
|
}
|
|
|
|
|
2017-04-25 11:29:39 -07:00
|
|
|
void CCoinsViewCache::Uncache(const COutPoint& hash)
|
2015-10-21 17:41:40 -07:00
|
|
|
{
|
|
|
|
CCoinsMap::iterator it = cacheCoins.find(hash);
|
2024-06-28 16:11:16 -04:00
|
|
|
if (it != cacheCoins.end() && !it->second.IsDirty() && !it->second.IsFresh()) {
|
2017-05-04 00:15:36 -07:00
|
|
|
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
|
2023-11-23 11:06:17 +01:00
|
|
|
TRACEPOINT(utxocache, uncache,
|
2021-09-03 22:08:53 +05:30
|
|
|
hash.hash.data(),
|
|
|
|
(uint32_t)hash.n,
|
|
|
|
(uint32_t)it->second.coin.nHeight,
|
|
|
|
(int64_t)it->second.coin.out.nValue,
|
|
|
|
(bool)it->second.coin.IsCoinBase());
|
2015-10-21 17:41:40 -07:00
|
|
|
cacheCoins.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
unsigned int CCoinsViewCache::GetCacheSize() const {
|
2013-11-05 02:47:07 +01:00
|
|
|
return cacheCoins.size();
|
|
|
|
}
|
|
|
|
|
2014-07-19 16:42:48 +02:00
|
|
|
bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
|
2013-11-05 02:47:07 +01:00
|
|
|
{
|
|
|
|
if (!tx.IsCoinBase()) {
|
|
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++) {
|
2017-05-30 17:58:54 -07:00
|
|
|
if (!HaveCoin(tx.vin[i].prevout)) {
|
2013-11-05 02:47:07 +01:00
|
|
|
return false;
|
2014-09-02 21:21:15 +02:00
|
|
|
}
|
2013-11-05 02:47:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-11-11 17:35:14 +10:00
|
|
|
|
2019-09-16 15:01:12 -04:00
|
|
|
void CCoinsViewCache::ReallocateCache()
|
|
|
|
{
|
|
|
|
// Cache should be empty when we're calling this.
|
|
|
|
assert(cacheCoins.size() == 0);
|
|
|
|
cacheCoins.~CCoinsMap();
|
2022-06-11 11:27:38 +02:00
|
|
|
m_cache_coins_memory_resource.~CCoinsMapMemoryResource();
|
|
|
|
::new (&m_cache_coins_memory_resource) CCoinsMapMemoryResource{};
|
|
|
|
::new (&cacheCoins) CCoinsMap{0, SaltedOutpointHasher{/*deterministic=*/m_deterministic}, CCoinsMap::key_equal{}, &m_cache_coins_memory_resource};
|
2019-09-16 15:01:12 -04:00
|
|
|
}
|
|
|
|
|
2023-02-01 18:28:08 -05:00
|
|
|
void CCoinsViewCache::SanityCheck() const
|
|
|
|
{
|
|
|
|
size_t recomputed_usage = 0;
|
2024-07-16 10:37:44 -04:00
|
|
|
size_t count_flagged = 0;
|
2023-02-01 18:28:08 -05:00
|
|
|
for (const auto& [_, entry] : cacheCoins) {
|
|
|
|
unsigned attr = 0;
|
2024-06-28 16:11:16 -04:00
|
|
|
if (entry.IsDirty()) attr |= 1;
|
|
|
|
if (entry.IsFresh()) attr |= 2;
|
2023-02-01 18:28:08 -05:00
|
|
|
if (entry.coin.IsSpent()) attr |= 4;
|
|
|
|
// Only 5 combinations are possible.
|
|
|
|
assert(attr != 2 && attr != 4 && attr != 7);
|
|
|
|
|
|
|
|
// Recompute cachedCoinsUsage.
|
|
|
|
recomputed_usage += entry.coin.DynamicMemoryUsage();
|
2024-07-16 10:37:44 -04:00
|
|
|
|
|
|
|
// Count the number of entries we expect in the linked list.
|
|
|
|
if (entry.IsDirty() || entry.IsFresh()) ++count_flagged;
|
|
|
|
}
|
|
|
|
// Iterate over the linked list of flagged entries.
|
|
|
|
size_t count_linked = 0;
|
|
|
|
for (auto it = m_sentinel.second.Next(); it != &m_sentinel; it = it->second.Next()) {
|
|
|
|
// Verify linked list integrity.
|
|
|
|
assert(it->second.Next()->second.Prev() == it);
|
|
|
|
assert(it->second.Prev()->second.Next() == it);
|
|
|
|
// Verify they are actually flagged.
|
|
|
|
assert(it->second.IsDirty() || it->second.IsFresh());
|
|
|
|
// Count the number of entries actually in the list.
|
|
|
|
++count_linked;
|
2023-02-01 18:28:08 -05:00
|
|
|
}
|
2024-07-16 10:37:44 -04:00
|
|
|
assert(count_linked == count_flagged);
|
2023-02-01 18:28:08 -05:00
|
|
|
assert(recomputed_usage == cachedCoinsUsage);
|
|
|
|
}
|
|
|
|
|
2023-11-01 22:46:17 +10:00
|
|
|
static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT = WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut());
|
2017-06-17 00:18:42 +00:00
|
|
|
static const size_t MAX_OUTPUTS_PER_BLOCK = MAX_BLOCK_WEIGHT / MIN_TRANSACTION_OUTPUT_WEIGHT;
|
2017-04-25 11:29:29 -07:00
|
|
|
|
2023-10-11 14:53:04 +01:00
|
|
|
const Coin& AccessByTxid(const CCoinsViewCache& view, const Txid& txid)
|
2017-04-25 11:29:29 -07:00
|
|
|
{
|
|
|
|
COutPoint iter(txid, 0);
|
|
|
|
while (iter.n < MAX_OUTPUTS_PER_BLOCK) {
|
|
|
|
const Coin& alternate = view.AccessCoin(iter);
|
2017-05-30 17:58:54 -07:00
|
|
|
if (!alternate.IsSpent()) return alternate;
|
2017-04-25 11:29:29 -07:00
|
|
|
++iter.n;
|
|
|
|
}
|
|
|
|
return coinEmpty;
|
|
|
|
}
|
2019-03-27 14:17:13 -04:00
|
|
|
|
2024-09-08 21:57:18 +02:00
|
|
|
template <typename ReturnType, typename Func>
|
|
|
|
static ReturnType ExecuteBackedWrapper(Func func, const std::vector<std::function<void()>>& err_callbacks)
|
2022-10-18 11:20:06 +02:00
|
|
|
{
|
2019-03-27 14:17:13 -04:00
|
|
|
try {
|
2022-10-18 11:20:06 +02:00
|
|
|
return func();
|
2019-03-27 14:17:13 -04:00
|
|
|
} catch(const std::runtime_error& e) {
|
2022-10-18 11:20:06 +02:00
|
|
|
for (const auto& f : err_callbacks) {
|
2019-03-27 14:17:13 -04:00
|
|
|
f();
|
|
|
|
}
|
2024-01-11 20:27:28 +01:00
|
|
|
LogError("Error reading from database: %s\n", e.what());
|
2019-03-27 14:17:13 -04:00
|
|
|
// Starting the shutdown sequence and returning false to the caller would be
|
|
|
|
// interpreted as 'entry not found' (as opposed to unable to read data), and
|
|
|
|
// could lead to invalid interpretation. Just exit immediately, as we can't
|
|
|
|
// continue anyway, and all writes should be atomic.
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 11:20:06 +02:00
|
|
|
|
2024-09-07 23:12:35 +02:00
|
|
|
std::optional<Coin> CCoinsViewErrorCatcher::GetCoin(const COutPoint& outpoint) const
|
2024-09-08 21:57:18 +02:00
|
|
|
{
|
2024-09-07 23:12:35 +02:00
|
|
|
return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::GetCoin(outpoint); }, m_err_callbacks);
|
2022-10-18 11:20:06 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 21:57:18 +02:00
|
|
|
bool CCoinsViewErrorCatcher::HaveCoin(const COutPoint& outpoint) const
|
|
|
|
{
|
|
|
|
return ExecuteBackedWrapper<bool>([&]() { return CCoinsViewBacked::HaveCoin(outpoint); }, m_err_callbacks);
|
2022-10-18 11:20:06 +02:00
|
|
|
}
|