MCPcopy Create free account
hub / github.com/ElementsProject/elements / AddCoin

Method AddCoin

src/coins.cpp:79–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 9

MutateTxSignFunction · 0.80
AddCoinsFunction · 0.80
ApplyTxInUndoFunction · 0.80
BOOST_AUTO_TEST_CASEFunction · 0.80
BOOST_AUTO_TEST_CASEFunction · 0.80
SimulationTestFunction · 0.80
CheckAddCoinBaseFunction · 0.80
FUZZ_TARGET_INITFunction · 0.80
AnalyzePSBTFunction · 0.80

Calls 8

native_keyFunction · 0.85
IsUnspendableMethod · 0.80
IsExplicitMethod · 0.80
GetAmountMethod · 0.80
IsSpentMethod · 0.45
DynamicMemoryUsageMethod · 0.45
dataMethod · 0.45
IsCoinBaseMethod · 0.45

Tested by 5

BOOST_AUTO_TEST_CASEFunction · 0.64
BOOST_AUTO_TEST_CASEFunction · 0.64
SimulationTestFunction · 0.64
CheckAddCoinBaseFunction · 0.64
FUZZ_TARGET_INITFunction · 0.64