MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / AddCoin

Method AddCoin

src/coins.cpp:68–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

66}
67
68void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) {
69 assert(!coin.IsSpent());
70 if (coin.out.scriptPubKey.IsUnspendable()) return;
71 CCoinsMap::iterator it;
72 bool inserted;
73 std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
74 bool fresh = false;
75 if (!possible_overwrite) {
76 if (!it->second.coin.IsSpent()) {
77 throw std::logic_error("Attempted to overwrite an unspent coin (when possible_overwrite is false)");
78 }
79 // If the coin exists in this cache as a spent coin and is DIRTY, then
80 // its spentness hasn't been flushed to the parent cache. We're
81 // re-adding the coin to this cache now but we can't mark it as FRESH.
82 // If we mark it FRESH and then spend it before the cache is flushed
83 // we would remove it from this cache and would never flush spentness
84 // to the parent cache.
85 //
86 // Re-adding a spent coin can happen in the case of a re-org (the coin
87 // is 'spent' when the block adding it is disconnected and then
88 // re-added when it is also added in a newly connected block).
89 //
90 // If the coin doesn't exist in the current cache, or is spent but not
91 // DIRTY, then it can be marked FRESH.
92 fresh = !it->second.IsDirty();
93 }
94 if (!inserted) {
95 Assume(TrySub(m_dirty_count, it->second.IsDirty()));
96 Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage()));
97 }
98 it->second.coin = std::move(coin);
99 CCoinsCacheEntry::SetDirty(*it, m_sentinel);
100 ++m_dirty_count;
101 if (fresh) CCoinsCacheEntry::SetFresh(*it, m_sentinel);
102 cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
103 TRACEPOINT(utxocache, add,
104 outpoint.hash.data(),
105 (uint32_t)outpoint.n,
106 (uint32_t)it->second.coin.nHeight,
107 (int64_t)it->second.coin.out.nValue,
108 (bool)it->second.coin.IsCoinBase());
109}
110
111void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin) {
112 const auto mem_usage{coin.DynamicMemoryUsage()};

Callers 14

MutateTxSignFunction · 0.80
AddCoinsFunction · 0.80
ApplyTxInUndoFunction · 0.80
BOOST_AUTO_TEST_CASEFunction · 0.80
SimulationTestMethod · 0.80
CheckAddCoinFunction · 0.80
TestFlushBehaviorMethod · 0.80
BOOST_AUTO_TEST_CASEFunction · 0.80
BOOST_FIXTURE_TEST_CASEFunction · 0.80
TestCoinsViewFunction · 0.80
FUZZ_TARGETFunction · 0.80
AddTestCoinFunction · 0.80

Calls 8

TrySubFunction · 0.85
IsUnspendableMethod · 0.80
emplaceMethod · 0.80
IsSpentMethod · 0.45
IsDirtyMethod · 0.45
DynamicMemoryUsageMethod · 0.45
dataMethod · 0.45
IsCoinBaseMethod · 0.45

Tested by 9

BOOST_AUTO_TEST_CASEFunction · 0.64
SimulationTestMethod · 0.64
CheckAddCoinFunction · 0.64
TestFlushBehaviorMethod · 0.64
BOOST_AUTO_TEST_CASEFunction · 0.64
BOOST_FIXTURE_TEST_CASEFunction · 0.64
TestCoinsViewFunction · 0.64
FUZZ_TARGETFunction · 0.64
AddTestCoinFunction · 0.64