Branch data Line data Source code
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 : 202286 : SaltedCoinsCacheHasher::SaltedCoinsCacheHasher(bool deterministic)
23 [ + - ]: 202286 : : m_hasher{
24 [ - + ]: 606858 : deterministic ? 0x8e819f2607a18de6 : FastRandomContext().rand64(),
25 [ + - ]: 404572 : deterministic ? 0xf4020d2e3983b0eb : FastRandomContext().rand64()}
26 : : {
27 : 202286 : }
28 : :
29 : 591 : CoinsViewEmpty& CoinsViewEmpty::Get()
30 : : {
31 [ + + + - ]: 591 : static CoinsViewEmpty instance;
32 : 591 : return instance;
33 : : }
34 : :
35 : 22080 : std::optional<Coin> CCoinsViewCache::PeekCoin(const COutPoint& outpoint) const
36 : : {
37 [ + + ]: 22080 : if (auto it{cacheCoins.find(outpoint)}; it != cacheCoins.end()) {
38 [ + + ]: 1180 : return it->second.coin.IsSpent() ? std::nullopt : std::optional{it->second.coin};
39 : : }
40 : 20900 : return base->PeekCoin(outpoint);
41 : : }
42 : :
43 : 200972 : CCoinsViewCache::CCoinsViewCache(CCoinsView* in_base, bool deterministic) :
44 : 200972 : CCoinsViewBacked(in_base), m_deterministic(deterministic),
45 [ + - + - ]: 200972 : cacheCoins(0, SaltedCoinsCacheHasher{/*deterministic=*/deterministic}, CCoinsMap::key_equal{}, &m_cache_coins_memory_resource)
46 : : {
47 : 200972 : m_sentinel.second.SelfRef(m_sentinel);
48 : 200972 : }
49 : :
50 : 185499 : size_t CCoinsViewCache::DynamicMemoryUsage() const {
51 : 185499 : return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
52 : : }
53 : :
54 : 23603898 : std::optional<Coin> CCoinsViewCache::FetchCoinFromBase(const COutPoint& outpoint) const
55 : : {
56 : 23603898 : return base->GetCoin(outpoint);
57 : : }
58 : :
59 : 24213255 : CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
60 [ + + ]: 24213255 : const auto [ret, inserted] = cacheCoins.try_emplace(outpoint);
61 [ + + ]: 24213255 : if (inserted) {
62 [ + + ]: 23625880 : if (auto coin{FetchCoinFromBase(outpoint)}) {
63 : 528331 : ret->second.coin = std::move(*coin);
64 [ + + ]: 528331 : cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
65 [ - + ]: 528331 : Assert(!ret->second.coin.IsSpent());
66 : : } else {
67 : 23097549 : cacheCoins.erase(ret);
68 : 23097549 : return cacheCoins.end();
69 : 23625880 : }
70 : : }
71 : 1115706 : return ret;
72 : : }
73 : :
74 : 14391537 : std::optional<Coin> CCoinsViewCache::GetCoin(const COutPoint& outpoint) const
75 : : {
76 [ + + + + ]: 14391537 : if (auto it{FetchCoin(outpoint)}; it != cacheCoins.end() && !it->second.coin.IsSpent()) return it->second.coin;
77 : 14037437 : return std::nullopt;
78 : : }
79 : :
80 : 169342 : void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) {
81 [ - + ]: 169342 : assert(!coin.IsSpent());
82 [ + + ]: 169342 : if (coin.out.scriptPubKey.IsUnspendable()) return;
83 : 147707 : CCoinsMap::iterator it;
84 : 147707 : bool inserted;
85 [ + + ]: 147707 : std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
86 : 147707 : bool fresh = false;
87 [ + + ]: 147707 : if (!possible_overwrite) {
88 [ + + ]: 96021 : if (!it->second.coin.IsSpent()) {
89 [ + - ]: 17 : throw std::logic_error("Attempted to overwrite an unspent coin (when possible_overwrite is false)");
90 : : }
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 : 96004 : fresh = !it->second.IsDirty();
105 : : }
106 [ + + ]: 147690 : if (!inserted) {
107 [ + - + + ]: 21004 : Assume(TrySub(m_dirty_count, it->second.IsDirty()));
108 [ + + + - ]: 24413 : Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage()));
109 : : }
110 : 147690 : it->second.coin = std::move(coin);
111 : 147690 : CCoinsCacheEntry::SetDirty(*it, m_sentinel);
112 : 147690 : ++m_dirty_count;
113 [ + + ]: 147690 : if (fresh) CCoinsCacheEntry::SetFresh(*it, m_sentinel);
114 [ + + ]: 234443 : cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
115 : : TRACEPOINT(utxocache, add,
116 : : outpoint.hash.data(),
117 : : (uint32_t)outpoint.n,
118 : : (uint32_t)it->second.coin.nHeight,
119 : : (int64_t)it->second.coin.out.nValue,
120 : 169325 : (bool)it->second.coin.IsCoinBase());
121 : : }
122 : :
123 : 20653 : void CCoinsViewCache::EmplaceCoinInternalDANGER(const COutPoint& outpoint, Coin&& coin) {
124 [ + + ]: 20653 : const auto mem_usage{coin.DynamicMemoryUsage()};
125 [ + + ]: 20653 : auto [it, inserted] = cacheCoins.try_emplace(outpoint, std::move(coin));
126 [ + + ]: 20653 : if (inserted) {
127 : 20652 : CCoinsCacheEntry::SetDirty(*it, m_sentinel);
128 : 20652 : ++m_dirty_count;
129 : 20652 : cachedCoinsUsage += mem_usage;
130 : : }
131 : 20653 : }
132 : :
133 : 58113 : void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) {
134 : 58113 : bool fCoinbase = tx.IsCoinBase();
135 : 58113 : const Txid& txid = tx.GetHash();
136 [ - + + + ]: 137673 : for (size_t i = 0; i < tx.vout.size(); ++i) {
137 [ - + ]: 79560 : 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 [ + - ]: 159120 : cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase), overwrite);
141 : : }
142 : 58113 : }
143 : :
144 : 69498 : bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
145 : 69498 : CCoinsMap::iterator it = FetchCoin(outpoint);
146 [ + + ]: 69498 : if (it == cacheCoins.end()) return false;
147 [ + - + + ]: 138984 : Assume(TrySub(m_dirty_count, it->second.IsDirty()));
148 [ + + + - : 167789 : Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage()));
+ + ]
149 : : TRACEPOINT(utxocache, spent,
150 : : outpoint.hash.data(),
151 : : (uint32_t)outpoint.n,
152 : : (uint32_t)it->second.coin.nHeight,
153 : : (int64_t)it->second.coin.out.nValue,
154 : 69492 : (bool)it->second.coin.IsCoinBase());
155 [ + + ]: 69492 : if (moveout) {
156 : 34632 : *moveout = std::move(it->second.coin);
157 : : }
158 [ + + ]: 69492 : if (it->second.IsFresh()) {
159 : 8036 : cacheCoins.erase(it);
160 : : } else {
161 : 61456 : CCoinsCacheEntry::SetDirty(*it, m_sentinel);
162 : 61456 : ++m_dirty_count;
163 : 61456 : it->second.coin.Clear();
164 : : }
165 : : return true;
166 : : }
167 : :
168 : : static const Coin coinEmpty;
169 : :
170 : 8709155 : const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const {
171 [ + + ]: 8709155 : CCoinsMap::const_iterator it = FetchCoin(outpoint);
172 [ + + ]: 8709155 : if (it == cacheCoins.end()) {
173 : : return coinEmpty;
174 : : } else {
175 : 328857 : return it->second.coin;
176 : : }
177 : : }
178 : :
179 : 1043065 : bool CCoinsViewCache::HaveCoin(const COutPoint& outpoint) const
180 : : {
181 [ + + ]: 1043065 : CCoinsMap::const_iterator it = FetchCoin(outpoint);
182 [ + + + + ]: 1043065 : return (it != cacheCoins.end() && !it->second.coin.IsSpent());
183 : : }
184 : :
185 : 207945 : bool CCoinsViewCache::HaveCoinInCache(const COutPoint &outpoint) const {
186 [ + + ]: 207945 : CCoinsMap::const_iterator it = cacheCoins.find(outpoint);
187 [ + + + + ]: 207945 : return (it != cacheCoins.end() && !it->second.coin.IsSpent());
188 : : }
189 : :
190 : 40585 : uint256 CCoinsViewCache::GetBestBlock() const {
191 [ + + ]: 81170 : if (m_block_hash.IsNull())
192 : 20467 : m_block_hash = base->GetBestBlock();
193 : 40585 : return m_block_hash;
194 : : }
195 : :
196 : 451789 : void CCoinsViewCache::SetBestBlock(const uint256& in_block_hash)
197 : : {
198 : 451789 : m_block_hash = in_block_hash;
199 : 451789 : }
200 : :
201 : 11262 : void CCoinsViewCache::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& in_block_hash)
202 : : {
203 [ + + ]: 204546 : for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) {
204 [ + + ]: 193292 : if (!it->second.IsDirty()) { // TODO a cursor can only contain dirty entries
205 : 18 : continue;
206 : : }
207 [ + + ]: 193274 : auto [itUs, inserted]{cacheCoins.try_emplace(it->first)};
208 [ + + ]: 193274 : if (inserted) {
209 [ + + + + ]: 142950 : if (it->second.IsFresh() && it->second.coin.IsSpent()) {
210 : 1 : cacheCoins.erase(itUs); // TODO fresh coins should have been removed at spend
211 : : } 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 [ - + ]: 142949 : CCoinsCacheEntry& entry{itUs->second};
215 [ - + - - ]: 142949 : assert(entry.coin.DynamicMemoryUsage() == 0);
216 [ + + ]: 142949 : if (cursor.WillErase(*it)) {
217 : : // Since this entry will be erased,
218 : : // we can move the coin into us instead of copying it
219 : 130893 : entry.coin = std::move(it->second.coin);
220 : : } else {
221 : 12056 : entry.coin = it->second.coin;
222 : : }
223 : 142949 : CCoinsCacheEntry::SetDirty(*itUs, m_sentinel);
224 : 142949 : ++m_dirty_count;
225 [ + + ]: 142949 : 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 [ + + ]: 142949 : if (it->second.IsFresh()) CCoinsCacheEntry::SetFresh(*itUs, m_sentinel);
230 : : }
231 : : } else {
232 : : // Found the entry in the parent cache
233 [ + + + + ]: 50324 : 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 : : }
240 : :
241 [ + + + + ]: 50316 : 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 [ + - + + ]: 7154 : Assume(TrySub(m_dirty_count, itUs->second.IsDirty()));
245 [ + + + - ]: 8676 : Assume(TrySub(cachedCoinsUsage, itUs->second.coin.DynamicMemoryUsage()));
246 : 3577 : cacheCoins.erase(itUs);
247 : : } else {
248 : : // A normal modification.
249 [ + + + - : 111995 : Assume(TrySub(cachedCoinsUsage, itUs->second.coin.DynamicMemoryUsage()));
+ + ]
250 [ + + ]: 46739 : if (cursor.WillErase(*it)) {
251 : : // Since this entry will be erased,
252 : : // we can move the coin into us instead of copying it
253 : 44184 : itUs->second.coin = std::move(it->second.coin);
254 : : } else {
255 : 2555 : itUs->second.coin = it->second.coin;
256 : : }
257 [ + + ]: 46739 : cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage();
258 [ + + ]: 46739 : if (!itUs->second.IsDirty()) {
259 : 40700 : CCoinsCacheEntry::SetDirty(*itUs, m_sentinel);
260 : 40700 : ++m_dirty_count;
261 : : }
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 : : }
267 : : }
268 : : }
269 : 11254 : SetBestBlock(in_block_hash);
270 : 11254 : }
271 : :
272 : 11292 : void CCoinsViewCache::Flush(bool reallocate_cache)
273 : : {
274 : 11292 : auto cursor{CoinsViewCacheCursor(m_dirty_count, m_sentinel, cacheCoins, /*will_erase=*/true)};
275 : 11292 : base->BatchWrite(cursor, m_block_hash);
276 : 11292 : Assume(m_dirty_count == 0);
277 : 11292 : cacheCoins.clear();
278 [ + + ]: 11292 : if (reallocate_cache) {
279 : 926 : ReallocateCache();
280 : : }
281 : 11292 : cachedCoinsUsage = 0;
282 : 11292 : }
283 : :
284 : 222 : void CCoinsViewCache::Sync()
285 : : {
286 : 222 : auto cursor{CoinsViewCacheCursor(m_dirty_count, m_sentinel, cacheCoins, /*will_erase=*/false)};
287 : 222 : base->BatchWrite(cursor, m_block_hash);
288 [ - + ]: 222 : Assume(m_dirty_count == 0);
289 [ - + ]: 222 : 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 : : }
293 : 222 : }
294 : :
295 : 9959 : void CCoinsViewCache::Reset() noexcept
296 : : {
297 : 9959 : cacheCoins.clear();
298 : 9959 : cachedCoinsUsage = 0;
299 : 9959 : m_dirty_count = 0;
300 : 9959 : SetBestBlock(uint256::ZERO);
301 : 9959 : }
302 : :
303 : 11811 : void CCoinsViewCache::Uncache(const COutPoint& hash)
304 : : {
305 : 11811 : CCoinsMap::iterator it = cacheCoins.find(hash);
306 [ + + + + ]: 11811 : if (it != cacheCoins.end() && !it->second.IsDirty()) {
307 [ + + + - ]: 3095 : Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage()));
308 : : TRACEPOINT(utxocache, uncache,
309 : : hash.hash.data(),
310 : : (uint32_t)hash.n,
311 : : (uint32_t)it->second.coin.nHeight,
312 : : (int64_t)it->second.coin.out.nValue,
313 : 1285 : (bool)it->second.coin.IsCoinBase());
314 : 1285 : cacheCoins.erase(it);
315 : : }
316 : 11811 : }
317 : :
318 : 40615 : unsigned int CCoinsViewCache::GetCacheSize() const {
319 : 40615 : return cacheCoins.size();
320 : : }
321 : :
322 : 1235 : bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
323 : : {
324 [ + - ]: 1235 : if (!tx.IsCoinBase()) {
325 [ - + + + ]: 2482 : for (unsigned int i = 0; i < tx.vin.size(); i++) {
326 [ + + ]: 1251 : if (!HaveCoin(tx.vin[i].prevout)) {
327 : : return false;
328 : : }
329 : : }
330 : : }
331 : : return true;
332 : : }
333 : :
334 : 926 : void CCoinsViewCache::ReallocateCache()
335 : : {
336 : : // Cache should be empty when we're calling this.
337 [ - + ]: 926 : assert(cacheCoins.size() == 0);
338 : 926 : cacheCoins.~CCoinsMap();
339 : 926 : m_cache_coins_memory_resource.~CCoinsMapMemoryResource();
340 : 926 : ::new (&m_cache_coins_memory_resource) CCoinsMapMemoryResource{};
341 : 926 : ::new (&cacheCoins) CCoinsMap{0, SaltedCoinsCacheHasher{/*deterministic=*/m_deterministic}, CCoinsMap::key_equal{}, &m_cache_coins_memory_resource};
342 : 926 : }
343 : :
344 : 308 : void CCoinsViewCache::SanityCheck() const
345 : : {
346 : 308 : size_t recomputed_usage = 0;
347 : 308 : size_t count_dirty = 0;
348 [ + + + + ]: 484461 : for (const auto& [_, entry] : cacheCoins) {
349 [ + + ]: 484153 : if (entry.coin.IsSpent()) {
350 [ + - - + ]: 22186 : assert(entry.IsDirty() && !entry.IsFresh()); // A spent coin must be dirty and cannot be fresh
351 : : } else {
352 [ + + - + ]: 461967 : assert(entry.IsDirty() || !entry.IsFresh()); // An unspent coin must not be fresh if not dirty
353 : : }
354 : :
355 : : // Recompute cachedCoinsUsage.
356 [ + + ]: 484153 : recomputed_usage += entry.coin.DynamicMemoryUsage();
357 : :
358 : : // Count the number of entries we expect in the linked list.
359 [ + + ]: 484153 : if (entry.IsDirty()) ++count_dirty;
360 : : }
361 : : // Iterate over the linked list of flagged entries.
362 : 308 : size_t count_linked = 0;
363 [ + + ]: 53576 : for (auto it = m_sentinel.second.Next(); it != &m_sentinel; it = it->second.Next()) {
364 : : // Verify linked list integrity.
365 [ - + ]: 53268 : assert(it->second.Next()->second.Prev() == it);
366 [ - + ]: 53268 : assert(it->second.Prev()->second.Next() == it);
367 : : // Verify they are actually flagged.
368 [ - + ]: 53268 : assert(it->second.IsDirty());
369 : : // Count the number of entries actually in the list.
370 : 53268 : ++count_linked;
371 : : }
372 [ + - - + ]: 308 : assert(count_dirty == count_linked && count_dirty == m_dirty_count);
373 [ - + ]: 308 : assert(recomputed_usage == cachedCoinsUsage);
374 : 308 : }
375 : :
376 : 9957 : CCoinsViewCache::ResetGuard CoinsViewOverlay::StartFetching(const CBlock& block LIFETIMEBOUND) noexcept
377 : : {
378 [ - + ]: 9957 : Assert(m_futures.empty());
379 [ - + ]: 9957 : Assert(m_inputs.empty());
380 [ - + ]: 9957 : Assert(m_input_head.load(std::memory_order_relaxed) == 0);
381 [ - + ]: 9957 : Assert(m_input_tail == 0);
382 [ + + - + : 9957 : if (const auto workers_count{m_thread_pool->WorkersCount()}; workers_count > 0 && block.vtx.size() > 1) {
+ + ]
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 : 82 : std::unordered_set<Txid, SaltedCoinsCacheHasher> earlier_txids;
387 [ - + ]: 82 : earlier_txids.reserve(block.vtx.size());
388 : 82 : earlier_txids.emplace(block.vtx[0]->GetHash());
389 [ + + ]: 1106 : for (const auto& tx : block.vtx | std::views::drop(1)) {
390 [ + + ]: 2060 : for (const auto& input : tx->vin) {
391 [ + + ]: 1036 : if (!earlier_txids.contains(input.prevout.hash)) m_inputs.emplace_back(input.prevout);
392 : : }
393 : 1024 : earlier_txids.emplace(tx->GetHash());
394 : : }
395 : : // Only submit tasks if we have something to fetch.
396 [ - + + - ]: 82 : if (m_inputs.size()) {
397 : 164 : std::vector<std::function<void()>> tasks(workers_count, [this] {
398 [ + + ]: 1431 : while (ProcessInput()) {}
399 : 82 : });
400 [ + + ]: 82 : if (auto futures{m_thread_pool->Submit(std::move(tasks))}) {
401 : 81 : m_futures = std::move(*futures);
402 : : } else {
403 : : // Submit can fail if a shared owner of the thread pool outside of this class calls Stop() or
404 : : // Interrupt() on a different thread after we call WorkersCount() above. In that case parallel
405 : : // fetching will not make progress, so we clear the inputs to fall back to single threaded fetching.
406 : 1 : LogWarning("Failed to submit prevout fetch tasks (%s); falling back to single-threaded fetching for this block.", SubmitErrorString(futures.error()));
407 : 1 : m_inputs.clear();
408 : 1 : StopFetching(); // Assert nothing changed if we failed to start tasks.
409 : 82 : }
410 : 82 : }
411 : 82 : }
412 : 9957 : return CreateResetGuard();
413 : : }
414 : :
415 : : static const uint64_t MIN_TRANSACTION_OUTPUT_WEIGHT{WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut())};
416 : : static const uint64_t MAX_OUTPUTS_PER_BLOCK{MAX_BLOCK_WEIGHT / MIN_TRANSACTION_OUTPUT_WEIGHT};
417 : :
418 : 136 : const Coin& AccessByTxid(const CCoinsViewCache& view, const Txid& txid)
419 : : {
420 : 136 : COutPoint iter(txid, 0);
421 [ + + ]: 7666795 : while (iter.n < MAX_OUTPUTS_PER_BLOCK) {
422 : 7666726 : const Coin& alternate = view.AccessCoin(iter);
423 [ + + ]: 7666726 : if (!alternate.IsSpent()) return alternate;
424 : 7666659 : ++iter.n;
425 : : }
426 : : return coinEmpty;
427 : : }
428 : :
429 : : template <typename ReturnType, typename Func>
430 : 41731 : static ReturnType ExecuteBackedWrapper(Func func, const std::vector<std::function<void()>>& err_callbacks)
431 : : {
432 : : try {
433 [ + - ]: 41731 : return func();
434 [ - - ]: 0 : } catch(const std::runtime_error& e) {
435 [ - - ]: 0 : for (const auto& f : err_callbacks) {
436 [ - - ]: 0 : f();
437 : : }
438 [ - - ]: 0 : LogError("Error reading from database: %s\n", e.what());
439 : : // Starting the shutdown sequence and returning false to the caller would be
440 : : // interpreted as 'entry not found' (as opposed to unable to read data), and
441 : : // could lead to invalid interpretation. Just exit immediately, as we can't
442 : : // continue anyway, and all writes should be atomic.
443 : 0 : std::abort();
444 : : }
445 : : }
446 : :
447 : 21307 : std::optional<Coin> CCoinsViewErrorCatcher::GetCoin(const COutPoint& outpoint) const
448 : : {
449 : 42614 : return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::GetCoin(outpoint); }, m_err_callbacks);
450 : : }
451 : :
452 : 0 : bool CCoinsViewErrorCatcher::HaveCoin(const COutPoint& outpoint) const
453 : : {
454 : 0 : return ExecuteBackedWrapper<bool>([&]() { return CCoinsViewBacked::HaveCoin(outpoint); }, m_err_callbacks);
455 : : }
456 : :
457 : 20424 : std::optional<Coin> CCoinsViewErrorCatcher::PeekCoin(const COutPoint& outpoint) const
458 : : {
459 : 40848 : return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::PeekCoin(outpoint); }, m_err_callbacks);
460 : : }
|