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