/tmp/bitcoin/src/coins.cpp
Line | Count | Source |
1 | | // Copyright (c) 2012-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <coins.h> |
6 | | |
7 | | #include <consensus/consensus.h> |
8 | | #include <primitives/block.h> |
9 | | #include <random.h> |
10 | | #include <uint256.h> |
11 | | #include <util/log.h> |
12 | | #include <util/threadpool.h> |
13 | | #include <util/trace.h> |
14 | | |
15 | | #include <ranges> |
16 | | #include <unordered_set> |
17 | | |
18 | | TRACEPOINT_SEMAPHORE(utxocache, add); |
19 | | TRACEPOINT_SEMAPHORE(utxocache, spent); |
20 | | TRACEPOINT_SEMAPHORE(utxocache, uncache); |
21 | | |
22 | | SaltedCoinsCacheHasher::SaltedCoinsCacheHasher(bool deterministic) |
23 | 517k | : m_hasher{ |
24 | 517k | deterministic ? 0x8e819f2607a18de6 : FastRandomContext().rand64(), |
25 | 517k | deterministic ? 0xf4020d2e3983b0eb : FastRandomContext().rand64()} |
26 | 517k | { |
27 | 517k | } |
28 | | |
29 | | CoinsViewEmpty& CoinsViewEmpty::Get() |
30 | 92.8k | { |
31 | 92.8k | static CoinsViewEmpty instance; |
32 | 92.8k | return instance; |
33 | 92.8k | } |
34 | | |
35 | | std::optional<Coin> CCoinsViewCache::PeekCoin(const COutPoint& outpoint) const |
36 | 454k | { |
37 | 454k | if (auto it{cacheCoins.find(outpoint)}; it != cacheCoins.end()) { |
38 | 52.0k | return it->second.coin.IsSpent() ? std::nullopt : std::optional{it->second.coin}; |
39 | 52.0k | } |
40 | 402k | return base->PeekCoin(outpoint); |
41 | 454k | } |
42 | | |
43 | | CCoinsViewCache::CCoinsViewCache(CCoinsView* in_base, bool deterministic) : |
44 | 400k | CCoinsViewBacked(in_base), m_deterministic(deterministic), |
45 | 400k | cacheCoins(0, SaltedCoinsCacheHasher{/*deterministic=*/deterministic}, CCoinsMap::key_equal{}, &m_cache_coins_memory_resource) |
46 | 400k | { |
47 | 400k | m_sentinel.second.SelfRef(m_sentinel); |
48 | 400k | } |
49 | | |
50 | 1.03M | size_t CCoinsViewCache::DynamicMemoryUsage() const { |
51 | 1.03M | return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage; |
52 | 1.03M | } |
53 | | |
54 | | std::optional<Coin> CCoinsViewCache::FetchCoinFromBase(const COutPoint& outpoint) const |
55 | 38.0M | { |
56 | 38.0M | return base->GetCoin(outpoint); |
57 | 38.0M | } |
58 | | |
59 | 84.6M | CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const { |
60 | 84.6M | const auto [ret, inserted] = cacheCoins.try_emplace(outpoint); |
61 | 84.6M | if (inserted) { |
62 | 38.4M | if (auto coin{FetchCoinFromBase(outpoint)}) { |
63 | 11.5M | ret->second.coin = std::move(*coin); |
64 | 11.5M | cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage(); |
65 | 11.5M | Assert(!ret->second.coin.IsSpent()); |
66 | 26.8M | } else { |
67 | 26.8M | cacheCoins.erase(ret); |
68 | 26.8M | return cacheCoins.end(); |
69 | 26.8M | } |
70 | 38.4M | } |
71 | 57.7M | return ret; |
72 | 84.6M | } |
73 | | |
74 | | std::optional<Coin> CCoinsViewCache::GetCoin(const COutPoint& outpoint) const |
75 | 26.9M | { |
76 | 26.9M | if (auto it{FetchCoin(outpoint)}; it != cacheCoins.end() && !it->second.coin.IsSpent()) return it->second.coin; |
77 | 15.6M | return std::nullopt; |
78 | 26.9M | } |
79 | | |
80 | 17.5M | void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) { |
81 | 17.5M | assert(!coin.IsSpent()); |
82 | 17.5M | if (coin.out.scriptPubKey.IsUnspendable()) return; |
83 | 17.2M | CCoinsMap::iterator it; |
84 | 17.2M | bool inserted; |
85 | 17.2M | std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>()); |
86 | 17.2M | bool fresh = false; |
87 | 17.2M | if (!possible_overwrite) { |
88 | 17.0M | if (!it->second.coin.IsSpent()) { |
89 | 17 | throw std::logic_error("Attempted to overwrite an unspent coin (when possible_overwrite is false)"); |
90 | 17 | } |
91 | | // If the coin exists in this cache as a spent coin and is DIRTY, then |
92 | | // its spentness hasn't been flushed to the parent cache. We're |
93 | | // re-adding the coin to this cache now but we can't mark it as FRESH. |
94 | | // If we mark it FRESH and then spend it before the cache is flushed |
95 | | // we would remove it from this cache and would never flush spentness |
96 | | // to the parent cache. |
97 | | // |
98 | | // Re-adding a spent coin can happen in the case of a re-org (the coin |
99 | | // is 'spent' when the block adding it is disconnected and then |
100 | | // re-added when it is also added in a newly connected block). |
101 | | // |
102 | | // If the coin doesn't exist in the current cache, or is spent but not |
103 | | // DIRTY, then it can be marked FRESH. |
104 | 17.0M | fresh = !it->second.IsDirty(); |
105 | 17.0M | } |
106 | 17.2M | if (!inserted) { |
107 | 11.1k | Assume(TrySub(m_dirty_count, it->second.IsDirty())); |
108 | 11.1k | Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage())); |
109 | 11.1k | } |
110 | 17.2M | it->second.coin = std::move(coin); |
111 | 17.2M | CCoinsCacheEntry::SetDirty(*it, m_sentinel); |
112 | 17.2M | ++m_dirty_count; |
113 | 17.2M | if (fresh) CCoinsCacheEntry::SetFresh(*it, m_sentinel); |
114 | 17.2M | cachedCoinsUsage += it->second.coin.DynamicMemoryUsage(); |
115 | 17.2M | TRACEPOINT(utxocache, add, |
116 | 17.2M | outpoint.hash.data(), |
117 | 17.2M | (uint32_t)outpoint.n, |
118 | 17.2M | (uint32_t)it->second.coin.nHeight, |
119 | 17.2M | (int64_t)it->second.coin.out.nValue, |
120 | 17.2M | (bool)it->second.coin.IsCoinBase()); |
121 | 17.2M | } |
122 | | |
123 | 24.9k | void CCoinsViewCache::EmplaceCoinInternalDANGER(const COutPoint& outpoint, Coin&& coin) { |
124 | 24.9k | const auto mem_usage{coin.DynamicMemoryUsage()}; |
125 | 24.9k | auto [it, inserted] = cacheCoins.try_emplace(outpoint, std::move(coin)); |
126 | 24.9k | if (inserted) { |
127 | 24.9k | CCoinsCacheEntry::SetDirty(*it, m_sentinel); |
128 | 24.9k | ++m_dirty_count; |
129 | 24.9k | cachedCoinsUsage += mem_usage; |
130 | 24.9k | } |
131 | 24.9k | } |
132 | | |
133 | 8.80M | void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) { |
134 | 8.80M | bool fCoinbase = tx.IsCoinBase(); |
135 | 8.80M | const Txid& txid = tx.GetHash(); |
136 | 26.2M | for (size_t i = 0; i < tx.vout.size(); ++i) { |
137 | 17.4M | bool overwrite = check_for_overwrite ? cache.HaveCoin(COutPoint(txid, i)) : fCoinbase; |
138 | | // Coinbase transactions can always be overwritten, in order to correctly |
139 | | // deal with the pre-BIP30 occurrences of duplicate coinbase transactions. |
140 | 17.4M | cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase), overwrite); |
141 | 17.4M | } |
142 | 8.80M | } |
143 | | |
144 | 11.2M | bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) { |
145 | 11.2M | CCoinsMap::iterator it = FetchCoin(outpoint); |
146 | 11.2M | if (it == cacheCoins.end()) return false; |
147 | 11.2M | Assume(TrySub(m_dirty_count, it->second.IsDirty())); |
148 | 11.2M | Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage())); |
149 | 11.2M | TRACEPOINT(utxocache, spent, |
150 | 11.2M | outpoint.hash.data(), |
151 | 11.2M | (uint32_t)outpoint.n, |
152 | 11.2M | (uint32_t)it->second.coin.nHeight, |
153 | 11.2M | (int64_t)it->second.coin.out.nValue, |
154 | 11.2M | (bool)it->second.coin.IsCoinBase()); |
155 | 11.2M | if (moveout) { |
156 | 169k | *moveout = std::move(it->second.coin); |
157 | 169k | } |
158 | 11.2M | if (it->second.IsFresh()) { |
159 | 239k | cacheCoins.erase(it); |
160 | 11.0M | } else { |
161 | 11.0M | CCoinsCacheEntry::SetDirty(*it, m_sentinel); |
162 | 11.0M | ++m_dirty_count; |
163 | 11.0M | it->second.coin.Clear(); |
164 | 11.0M | } |
165 | 11.2M | return true; |
166 | 11.2M | } |
167 | | |
168 | | static const Coin coinEmpty; |
169 | | |
170 | 22.4M | const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const { |
171 | 22.4M | CCoinsMap::const_iterator it = FetchCoin(outpoint); |
172 | 22.4M | if (it == cacheCoins.end()) { |
173 | 10.0M | return coinEmpty; |
174 | 12.4M | } else { |
175 | 12.4M | return it->second.coin; |
176 | 12.4M | } |
177 | 22.4M | } |
178 | | |
179 | | bool CCoinsViewCache::HaveCoin(const COutPoint& outpoint) const |
180 | 23.9M | { |
181 | 23.9M | CCoinsMap::const_iterator it = FetchCoin(outpoint); |
182 | 23.9M | return (it != cacheCoins.end() && !it->second.coin.IsSpent()); |
183 | 23.9M | } |
184 | | |
185 | 304k | bool CCoinsViewCache::HaveCoinInCache(const COutPoint &outpoint) const { |
186 | 304k | CCoinsMap::const_iterator it = cacheCoins.find(outpoint); |
187 | 304k | return (it != cacheCoins.end() && !it->second.coin.IsSpent()); |
188 | 304k | } |
189 | | |
190 | 497k | uint256 CCoinsViewCache::GetBestBlock() const { |
191 | 497k | if (m_block_hash.IsNull()) |
192 | 221k | m_block_hash = base->GetBestBlock(); |
193 | 497k | return m_block_hash; |
194 | 497k | } |
195 | | |
196 | | void CCoinsViewCache::SetBestBlock(const uint256& in_block_hash) |
197 | 790k | { |
198 | 790k | m_block_hash = in_block_hash; |
199 | 790k | } |
200 | | |
201 | | void CCoinsViewCache::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& in_block_hash) |
202 | 125k | { |
203 | 623k | for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) { |
204 | 497k | if (!it->second.IsDirty()) { // TODO a cursor can only contain dirty entries |
205 | 18 | continue; |
206 | 18 | } |
207 | 497k | auto [itUs, inserted]{cacheCoins.try_emplace(it->first)}; |
208 | 497k | if (inserted) { |
209 | 390k | if (it->second.IsFresh() && it->second.coin.IsSpent()) { |
210 | 1 | cacheCoins.erase(itUs); // TODO fresh coins should have been removed at spend |
211 | 390k | } else { |
212 | | // The parent cache does not have an entry, while the child cache does. |
213 | | // Move the data up and mark it as dirty. |
214 | 390k | CCoinsCacheEntry& entry{itUs->second}; |
215 | 390k | assert(entry.coin.DynamicMemoryUsage() == 0); |
216 | 390k | if (cursor.WillErase(*it)) { |
217 | | // Since this entry will be erased, |
218 | | // we can move the coin into us instead of copying it |
219 | 380k | entry.coin = std::move(it->second.coin); |
220 | 380k | } else { |
221 | 10.0k | entry.coin = it->second.coin; |
222 | 10.0k | } |
223 | 390k | CCoinsCacheEntry::SetDirty(*itUs, m_sentinel); |
224 | 390k | ++m_dirty_count; |
225 | 390k | cachedCoinsUsage += entry.coin.DynamicMemoryUsage(); |
226 | | // We can mark it FRESH in the parent if it was FRESH in the child |
227 | | // Otherwise it might have just been flushed from the parent's cache |
228 | | // and already exist in the grandparent |
229 | 390k | if (it->second.IsFresh()) CCoinsCacheEntry::SetFresh(*itUs, m_sentinel); |
230 | 390k | } |
231 | 390k | } else { |
232 | | // Found the entry in the parent cache |
233 | 106k | if (it->second.IsFresh() && !itUs->second.coin.IsSpent()) { |
234 | | // The coin was marked FRESH in the child cache, but the coin |
235 | | // exists in the parent cache. If this ever happens, it means |
236 | | // the FRESH flag was misapplied and there is a logic error in |
237 | | // the calling code. |
238 | 8 | throw std::logic_error("FRESH flag misapplied to coin that exists in parent cache"); |
239 | 8 | } |
240 | | |
241 | 106k | if (itUs->second.IsFresh() && it->second.coin.IsSpent()) { |
242 | | // The grandparent cache does not have an entry, and the coin |
243 | | // has been spent. We can just delete it from the parent cache. |
244 | 35.6k | Assume(TrySub(m_dirty_count, itUs->second.IsDirty())); |
245 | 35.6k | Assume(TrySub(cachedCoinsUsage, itUs->second.coin.DynamicMemoryUsage())); |
246 | 35.6k | cacheCoins.erase(itUs); |
247 | 71.2k | } else { |
248 | | // A normal modification. |
249 | 71.2k | Assume(TrySub(cachedCoinsUsage, itUs->second.coin.DynamicMemoryUsage())); |
250 | 71.2k | if (cursor.WillErase(*it)) { |
251 | | // Since this entry will be erased, |
252 | | // we can move the coin into us instead of copying it |
253 | 69.4k | itUs->second.coin = std::move(it->second.coin); |
254 | 69.4k | } else { |
255 | 1.75k | itUs->second.coin = it->second.coin; |
256 | 1.75k | } |
257 | 71.2k | cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage(); |
258 | 71.2k | if (!itUs->second.IsDirty()) { |
259 | 46.2k | CCoinsCacheEntry::SetDirty(*itUs, m_sentinel); |
260 | 46.2k | ++m_dirty_count; |
261 | 46.2k | } |
262 | | // NOTE: It isn't safe to mark the coin as FRESH in the parent |
263 | | // cache. If it already existed and was spent in the parent |
264 | | // cache then marking it FRESH would prevent that spentness |
265 | | // from being flushed to the grandparent. |
266 | 71.2k | } |
267 | 106k | } |
268 | 497k | } |
269 | 125k | SetBestBlock(in_block_hash); |
270 | 125k | } |
271 | | |
272 | | void CCoinsViewCache::Flush(bool reallocate_cache) |
273 | 127k | { |
274 | 127k | auto cursor{CoinsViewCacheCursor(m_dirty_count, m_sentinel, cacheCoins, /*will_erase=*/true)}; |
275 | 127k | base->BatchWrite(cursor, m_block_hash); |
276 | 127k | Assume(m_dirty_count == 0); |
277 | 127k | cacheCoins.clear(); |
278 | 127k | if (reallocate_cache) { |
279 | 3.24k | ReallocateCache(); |
280 | 3.24k | } |
281 | 127k | cachedCoinsUsage = 0; |
282 | 127k | } |
283 | | |
284 | | void CCoinsViewCache::Sync() |
285 | 1.44k | { |
286 | 1.44k | auto cursor{CoinsViewCacheCursor(m_dirty_count, m_sentinel, cacheCoins, /*will_erase=*/false)}; |
287 | 1.44k | base->BatchWrite(cursor, m_block_hash); |
288 | 1.44k | Assume(m_dirty_count == 0); |
289 | 1.44k | if (m_sentinel.second.Next() != &m_sentinel) { |
290 | | /* BatchWrite must clear flags of all entries */ |
291 | 0 | throw std::logic_error("Not all unspent flagged entries were cleared"); |
292 | 0 | } |
293 | 1.44k | } |
294 | | |
295 | | void CCoinsViewCache::Reset() noexcept |
296 | 113k | { |
297 | 113k | cacheCoins.clear(); |
298 | 113k | cachedCoinsUsage = 0; |
299 | 113k | m_dirty_count = 0; |
300 | 113k | SetBestBlock(uint256::ZERO); |
301 | 113k | } |
302 | | |
303 | | void CCoinsViewCache::Uncache(const COutPoint& hash) |
304 | 22.1k | { |
305 | 22.1k | CCoinsMap::iterator it = cacheCoins.find(hash); |
306 | 22.1k | if (it != cacheCoins.end() && !it->second.IsDirty()) { |
307 | 9.27k | Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage())); |
308 | 9.27k | TRACEPOINT(utxocache, uncache, |
309 | 9.27k | hash.hash.data(), |
310 | 9.27k | (uint32_t)hash.n, |
311 | 9.27k | (uint32_t)it->second.coin.nHeight, |
312 | 9.27k | (int64_t)it->second.coin.out.nValue, |
313 | 9.27k | (bool)it->second.coin.IsCoinBase()); |
314 | 9.27k | cacheCoins.erase(it); |
315 | 9.27k | } |
316 | 22.1k | } |
317 | | |
318 | 514k | unsigned int CCoinsViewCache::GetCacheSize() const { |
319 | 514k | return cacheCoins.size(); |
320 | 514k | } |
321 | | |
322 | | bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const |
323 | 8.65M | { |
324 | 8.65M | if (!tx.IsCoinBase()) { |
325 | 19.8M | for (unsigned int i = 0; i < tx.vin.size(); i++) { |
326 | 11.2M | if (!HaveCoin(tx.vin[i].prevout)) { |
327 | 324 | return false; |
328 | 324 | } |
329 | 11.2M | } |
330 | 8.65M | } |
331 | 8.65M | return true; |
332 | 8.65M | } |
333 | | |
334 | | void CCoinsViewCache::ReallocateCache() |
335 | 3.24k | { |
336 | | // Cache should be empty when we're calling this. |
337 | 3.24k | assert(cacheCoins.size() == 0); |
338 | 3.24k | cacheCoins.~CCoinsMap(); |
339 | 3.24k | m_cache_coins_memory_resource.~CCoinsMapMemoryResource(); |
340 | 3.24k | ::new (&m_cache_coins_memory_resource) CCoinsMapMemoryResource{}; |
341 | 3.24k | ::new (&cacheCoins) CCoinsMap{0, SaltedCoinsCacheHasher{/*deterministic=*/m_deterministic}, CCoinsMap::key_equal{}, &m_cache_coins_memory_resource}; |
342 | 3.24k | } |
343 | | |
344 | | void CCoinsViewCache::SanityCheck() const |
345 | 341 | { |
346 | 341 | size_t recomputed_usage = 0; |
347 | 341 | size_t count_dirty = 0; |
348 | 517k | for (const auto& [_, entry] : cacheCoins) { |
349 | 517k | if (entry.coin.IsSpent()) { |
350 | 18.5k | assert(entry.IsDirty() && !entry.IsFresh()); // A spent coin must be dirty and cannot be fresh |
351 | 498k | } else { |
352 | 498k | assert(entry.IsDirty() || !entry.IsFresh()); // An unspent coin must not be fresh if not dirty |
353 | 498k | } |
354 | | |
355 | | // Recompute cachedCoinsUsage. |
356 | 517k | recomputed_usage += entry.coin.DynamicMemoryUsage(); |
357 | | |
358 | | // Count the number of entries we expect in the linked list. |
359 | 517k | if (entry.IsDirty()) ++count_dirty; |
360 | 517k | } |
361 | | // Iterate over the linked list of flagged entries. |
362 | 341 | size_t count_linked = 0; |
363 | 48.6k | for (auto it = m_sentinel.second.Next(); it != &m_sentinel; it = it->second.Next()) { |
364 | | // Verify linked list integrity. |
365 | 48.2k | assert(it->second.Next()->second.Prev() == it); |
366 | 48.2k | assert(it->second.Prev()->second.Next() == it); |
367 | | // Verify they are actually flagged. |
368 | 48.2k | assert(it->second.IsDirty()); |
369 | | // Count the number of entries actually in the list. |
370 | 48.2k | ++count_linked; |
371 | 48.2k | } |
372 | 341 | assert(count_dirty == count_linked && count_dirty == m_dirty_count); |
373 | 341 | assert(recomputed_usage == cachedCoinsUsage); |
374 | 341 | } |
375 | | |
376 | | CCoinsViewCache::ResetGuard CoinsViewOverlay::StartFetching(const CBlock& block LIFETIMEBOUND) noexcept |
377 | 113k | { |
378 | 113k | Assert(m_futures.empty()); |
379 | 113k | Assert(m_inputs.empty()); |
380 | 113k | Assert(m_input_head.load(std::memory_order_relaxed) == 0); |
381 | 113k | Assert(m_input_tail == 0); |
382 | 113k | if (const auto workers_count{m_thread_pool->WorkersCount()}; workers_count > 0) { |
383 | | // Loop through the block inputs and set their prevouts in the queue. |
384 | | // Filter inputs that spend outputs created earlier in the same block. These outputs will be created |
385 | | // directly in the cache from the tx that creates them, so they will not be requested from a base view. |
386 | 113k | std::unordered_set<Txid, SaltedCoinsCacheHasher> earlier_txids; |
387 | 113k | earlier_txids.reserve(block.vtx.size()); |
388 | 113k | for (const auto& tx : block.vtx | std::views::drop(1)) { |
389 | 82.5k | for (const auto& input : tx->vin) { |
390 | 82.5k | if (!earlier_txids.contains(input.prevout.hash)) m_inputs.emplace_back(input.prevout); |
391 | 82.5k | } |
392 | 51.2k | earlier_txids.emplace(tx->GetHash()); |
393 | 51.2k | } |
394 | | // Only submit tasks if we have something to fetch. |
395 | 113k | if (m_inputs.size()) { |
396 | 25.1k | std::vector<std::function<void()>> tasks(workers_count, [this] { |
397 | 77.1k | while (ProcessInput()) {} |
398 | 25.1k | }); |
399 | 9.21k | if (auto futures{m_thread_pool->Submit(std::move(tasks))}) { |
400 | 9.21k | m_futures = std::move(*futures); |
401 | 9.21k | } else { |
402 | | // Submit can fail if a shared owner of the thread pool outside of this class calls Stop() or |
403 | | // Interrupt() on a different thread after we call WorkersCount() above. In that case parallel |
404 | | // fetching will not make progress, so we clear the inputs to fall back to single threaded fetching. |
405 | 1 | LogWarning("Failed to submit prevout fetch tasks; falling back to single-threaded fetching for this block."); |
406 | 1 | m_inputs.clear(); |
407 | 1 | StopFetching(); // Assert nothing changed if we failed to start tasks. |
408 | 1 | } |
409 | 9.21k | } |
410 | 113k | } |
411 | 113k | return CreateResetGuard(); |
412 | 113k | } |
413 | | |
414 | | static const uint64_t MIN_TRANSACTION_OUTPUT_WEIGHT{WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut())}; |
415 | | static const uint64_t MAX_OUTPUTS_PER_BLOCK{MAX_BLOCK_WEIGHT / MIN_TRANSACTION_OUTPUT_WEIGHT}; |
416 | | |
417 | | const Coin& AccessByTxid(const CCoinsViewCache& view, const Txid& txid) |
418 | 170 | { |
419 | 170 | COutPoint iter(txid, 0); |
420 | 9.22M | while (iter.n < MAX_OUTPUTS_PER_BLOCK) { |
421 | 9.22M | const Coin& alternate = view.AccessCoin(iter); |
422 | 9.22M | if (!alternate.IsSpent()) return alternate; |
423 | 9.22M | ++iter.n; |
424 | 9.22M | } |
425 | 83 | return coinEmpty; |
426 | 170 | } |
427 | | |
428 | | template <typename ReturnType, typename Func> |
429 | | static ReturnType ExecuteBackedWrapper(Func func, const std::vector<std::function<void()>>& err_callbacks) |
430 | 1.31M | { |
431 | 1.31M | try { |
432 | 1.31M | return func(); |
433 | 1.31M | } catch(const std::runtime_error& e) { |
434 | 0 | for (const auto& f : err_callbacks) { |
435 | 0 | f(); |
436 | 0 | } |
437 | 0 | LogError("Error reading from database: %s\n", e.what()); |
438 | | // Starting the shutdown sequence and returning false to the caller would be |
439 | | // interpreted as 'entry not found' (as opposed to unable to read data), and |
440 | | // could lead to invalid interpretation. Just exit immediately, as we can't |
441 | | // continue anyway, and all writes should be atomic. |
442 | 0 | std::abort(); |
443 | 0 | } |
444 | 1.31M | } coins.cpp:std::optional<Coin> ExecuteBackedWrapper<std::optional<Coin>, CCoinsViewErrorCatcher::GetCoin(COutPoint const&) const::$_0>(CCoinsViewErrorCatcher::GetCoin(COutPoint const&) const::$_0, std::vector<std::function<void ()>, std::allocator<std::function<void ()>>> const&) Line | Count | Source | 430 | 914k | { | 431 | 914k | try { | 432 | 914k | return func(); | 433 | 914k | } catch(const std::runtime_error& e) { | 434 | 0 | for (const auto& f : err_callbacks) { | 435 | 0 | f(); | 436 | 0 | } | 437 | 0 | LogError("Error reading from database: %s\n", e.what()); | 438 | | // Starting the shutdown sequence and returning false to the caller would be | 439 | | // interpreted as 'entry not found' (as opposed to unable to read data), and | 440 | | // could lead to invalid interpretation. Just exit immediately, as we can't | 441 | | // continue anyway, and all writes should be atomic. | 442 | 0 | std::abort(); | 443 | 0 | } | 444 | 914k | } |
Unexecuted instantiation: coins.cpp:bool ExecuteBackedWrapper<bool, CCoinsViewErrorCatcher::HaveCoin(COutPoint const&) const::$_0>(CCoinsViewErrorCatcher::HaveCoin(COutPoint const&) const::$_0, std::vector<std::function<void ()>, std::allocator<std::function<void ()>>> const&) coins.cpp:std::optional<Coin> ExecuteBackedWrapper<std::optional<Coin>, CCoinsViewErrorCatcher::PeekCoin(COutPoint const&) const::$_0>(CCoinsViewErrorCatcher::PeekCoin(COutPoint const&) const::$_0, std::vector<std::function<void ()>, std::allocator<std::function<void ()>>> const&) Line | Count | Source | 430 | 399k | { | 431 | 399k | try { | 432 | 399k | return func(); | 433 | 399k | } catch(const std::runtime_error& e) { | 434 | 0 | for (const auto& f : err_callbacks) { | 435 | 0 | f(); | 436 | 0 | } | 437 | 0 | LogError("Error reading from database: %s\n", e.what()); | 438 | | // Starting the shutdown sequence and returning false to the caller would be | 439 | | // interpreted as 'entry not found' (as opposed to unable to read data), and | 440 | | // could lead to invalid interpretation. Just exit immediately, as we can't | 441 | | // continue anyway, and all writes should be atomic. | 442 | 0 | std::abort(); | 443 | 0 | } | 444 | 399k | } |
|
445 | | |
446 | | std::optional<Coin> CCoinsViewErrorCatcher::GetCoin(const COutPoint& outpoint) const |
447 | 914k | { |
448 | 914k | return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::GetCoin(outpoint); }, m_err_callbacks); |
449 | 914k | } |
450 | | |
451 | | bool CCoinsViewErrorCatcher::HaveCoin(const COutPoint& outpoint) const |
452 | 0 | { |
453 | 0 | return ExecuteBackedWrapper<bool>([&]() { return CCoinsViewBacked::HaveCoin(outpoint); }, m_err_callbacks); |
454 | 0 | } |
455 | | |
456 | | std::optional<Coin> CCoinsViewErrorCatcher::PeekCoin(const COutPoint& outpoint) const |
457 | 399k | { |
458 | 399k | return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::PeekCoin(outpoint); }, m_err_callbacks); |
459 | 399k | } |