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 : 168709 : SaltedCoinsCacheHasher::SaltedCoinsCacheHasher(bool deterministic)
23 [ + - ]: 168709 : : m_hasher{
24 [ - + ]: 506127 : deterministic ? 0x8e819f2607a18de6 : FastRandomContext().rand64(),
25 [ + - ]: 337418 : deterministic ? 0xf4020d2e3983b0eb : FastRandomContext().rand64()}
26 : : {
27 : 168709 : }
28 : :
29 : 588 : CoinsViewEmpty& CoinsViewEmpty::Get()
30 : : {
31 [ + + + - ]: 588 : static CoinsViewEmpty instance;
32 : 588 : return instance;
33 : : }
34 : :
35 : 19888 : std::optional<Coin> CCoinsViewCache::PeekCoin(const COutPoint& outpoint) const
36 : : {
37 [ + + ]: 19888 : if (auto it{cacheCoins.find(outpoint)}; it != cacheCoins.end()) {
38 [ + + ]: 1178 : return it->second.coin.IsSpent() ? std::nullopt : std::optional{it->second.coin};
39 : : }
40 : 18710 : return base->PeekCoin(outpoint);
41 : : }
42 : :
43 : 158624 : CCoinsViewCache::CCoinsViewCache(CCoinsView* in_base, bool deterministic) :
44 : 158624 : CCoinsViewBacked(in_base), m_deterministic(deterministic),
45 [ + - + - ]: 158624 : cacheCoins(0, SaltedCoinsCacheHasher{/*deterministic=*/deterministic}, CCoinsMap::key_equal{}, &m_cache_coins_memory_resource)
46 : : {
47 : 158624 : m_sentinel.second.SelfRef(m_sentinel);
48 : 158624 : }
49 : :
50 : 177657 : size_t CCoinsViewCache::DynamicMemoryUsage() const {
51 : 177657 : return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
52 : : }
53 : :
54 : 24076857 : std::optional<Coin> CCoinsViewCache::FetchCoinFromBase(const COutPoint& outpoint) const
55 : : {
56 : 24076857 : return base->GetCoin(outpoint);
57 : : }
58 : :
59 : 24685047 : CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const {
60 [ + + ]: 24685047 : const auto [ret, inserted] = cacheCoins.try_emplace(outpoint);
61 [ + + ]: 24685047 : if (inserted) {
62 [ + + ]: 24096647 : if (auto coin{FetchCoinFromBase(outpoint)}) {
63 : 554320 : ret->second.coin = std::move(*coin);
64 [ + + ]: 554320 : cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
65 [ - + ]: 554320 : Assert(!ret->second.coin.IsSpent());
66 : : } else {
67 : 23542327 : cacheCoins.erase(ret);
68 : 23542327 : return cacheCoins.end();
69 : 24096647 : }
70 : : }
71 : 1142720 : return ret;
72 : : }
73 : :
74 : 13675791 : std::optional<Coin> CCoinsViewCache::GetCoin(const COutPoint& outpoint) const
75 : : {
76 [ + + + + ]: 13675791 : if (auto it{FetchCoin(outpoint)}; it != cacheCoins.end() && !it->second.coin.IsSpent()) return it->second.coin;
77 : 13330744 : return std::nullopt;
78 : : }
79 : :
80 : 165041 : void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) {
81 [ - + ]: 165041 : assert(!coin.IsSpent());
82 [ + + ]: 165041 : if (coin.out.scriptPubKey.IsUnspendable()) return;
83 : 145647 : CCoinsMap::iterator it;
84 : 145647 : bool inserted;
85 [ + + ]: 145647 : std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
86 : 145647 : bool fresh = false;
87 [ + + ]: 145647 : if (!possible_overwrite) {
88 [ + + ]: 95965 : 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 : 95948 : fresh = !it->second.IsDirty();
105 : : }
106 [ + + ]: 145630 : if (!inserted) {
107 [ + - + + ]: 21200 : Assume(TrySub(m_dirty_count, it->second.IsDirty()));
108 [ + + + - ]: 24708 : Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage()));
109 : : }
110 : 145630 : it->second.coin = std::move(coin);
111 : 145630 : CCoinsCacheEntry::SetDirty(*it, m_sentinel);
112 : 145630 : ++m_dirty_count;
113 [ + + ]: 145630 : if (fresh) CCoinsCacheEntry::SetFresh(*it, m_sentinel);
114 [ + + ]: 232364 : 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 : 165024 : (bool)it->second.coin.IsCoinBase());
121 : : }
122 : :
123 : 20398 : void CCoinsViewCache::EmplaceCoinInternalDANGER(const COutPoint& outpoint, Coin&& coin) {
124 [ + + ]: 20398 : const auto mem_usage{coin.DynamicMemoryUsage()};
125 [ + + ]: 20398 : auto [it, inserted] = cacheCoins.try_emplace(outpoint, std::move(coin));
126 [ + + ]: 20398 : if (inserted) {
127 : 20397 : CCoinsCacheEntry::SetDirty(*it, m_sentinel);
128 : 20397 : ++m_dirty_count;
129 : 20397 : cachedCoinsUsage += mem_usage;
130 : : }
131 : 20398 : }
132 : :
133 : 55799 : void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) {
134 : 55799 : bool fCoinbase = tx.IsCoinBase();
135 : 55799 : const Txid& txid = tx.GetHash();
136 [ - + + + ]: 130700 : for (size_t i = 0; i < tx.vout.size(); ++i) {
137 [ - + ]: 74901 : 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 [ + - ]: 149802 : cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase), overwrite);
141 : : }
142 : 55799 : }
143 : :
144 : 69383 : bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
145 : 69383 : CCoinsMap::iterator it = FetchCoin(outpoint);
146 [ + + ]: 69383 : if (it == cacheCoins.end()) return false;
147 [ + - + + ]: 138754 : Assume(TrySub(m_dirty_count, it->second.IsDirty()));
148 [ + + + - : 167407 : 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 : 69377 : (bool)it->second.coin.IsCoinBase());
155 [ + + ]: 69377 : if (moveout) {
156 : 34622 : *moveout = std::move(it->second.coin);
157 : : }
158 [ + + ]: 69377 : if (it->second.IsFresh()) {
159 : 8036 : cacheCoins.erase(it);
160 : : } else {
161 : 61341 : CCoinsCacheEntry::SetDirty(*it, m_sentinel);
162 : 61341 : ++m_dirty_count;
163 : 61341 : it->second.coin.Clear();
164 : : }
165 : : return true;
166 : : }
167 : :
168 : : static const Coin coinEmpty;
169 : :
170 : 9860822 : const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const {
171 [ + + ]: 9860822 : CCoinsMap::const_iterator it = FetchCoin(outpoint);
172 [ + + ]: 9860822 : if (it == cacheCoins.end()) {
173 : : return coinEmpty;
174 : : } else {
175 : 352401 : return it->second.coin;
176 : : }
177 : : }
178 : :
179 : 1079051 : bool CCoinsViewCache::HaveCoin(const COutPoint& outpoint) const
180 : : {
181 [ + + ]: 1079051 : CCoinsMap::const_iterator it = FetchCoin(outpoint);
182 [ + + + + ]: 1079051 : return (it != cacheCoins.end() && !it->second.coin.IsSpent());
183 : : }
184 : :
185 : 231184 : bool CCoinsViewCache::HaveCoinInCache(const COutPoint &outpoint) const {
186 [ + + ]: 231184 : CCoinsMap::const_iterator it = cacheCoins.find(outpoint);
187 [ + + + + ]: 231184 : return (it != cacheCoins.end() && !it->second.coin.IsSpent());
188 : : }
189 : :
190 : 35873 : uint256 CCoinsViewCache::GetBestBlock() const {
191 [ + + ]: 71746 : if (m_block_hash.IsNull())
192 : 18078 : m_block_hash = base->GetBestBlock();
193 : 35873 : return m_block_hash;
194 : : }
195 : :
196 : 448478 : void CCoinsViewCache::SetBestBlock(const uint256& in_block_hash)
197 : : {
198 : 448478 : m_block_hash = in_block_hash;
199 : 448478 : }
200 : :
201 : 10147 : void CCoinsViewCache::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& in_block_hash)
202 : : {
203 [ + + ]: 193753 : for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) {
204 [ + + ]: 183614 : if (!it->second.IsDirty()) { // TODO a cursor can only contain dirty entries
205 : 18 : continue;
206 : : }
207 [ + + ]: 183596 : auto [itUs, inserted]{cacheCoins.try_emplace(it->first)};
208 [ + + ]: 183596 : if (inserted) {
209 [ + + + + ]: 138241 : 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 [ - + ]: 138240 : CCoinsCacheEntry& entry{itUs->second};
215 [ - + - - ]: 138240 : assert(entry.coin.DynamicMemoryUsage() == 0);
216 [ + + ]: 138240 : if (cursor.WillErase(*it)) {
217 : : // Since this entry will be erased,
218 : : // we can move the coin into us instead of copying it
219 : 126186 : entry.coin = std::move(it->second.coin);
220 : : } else {
221 : 12054 : entry.coin = it->second.coin;
222 : : }
223 : 138240 : CCoinsCacheEntry::SetDirty(*itUs, m_sentinel);
224 : 138240 : ++m_dirty_count;
225 [ + + ]: 138240 : 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 [ + + ]: 138240 : if (it->second.IsFresh()) CCoinsCacheEntry::SetFresh(*itUs, m_sentinel);
230 : : }
231 : : } else {
232 : : // Found the entry in the parent cache
233 [ + + + + ]: 45355 : 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 [ + + + + ]: 45347 : 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 [ + - + + ]: 7150 : Assume(TrySub(m_dirty_count, itUs->second.IsDirty()));
245 [ + + + - ]: 8672 : Assume(TrySub(cachedCoinsUsage, itUs->second.coin.DynamicMemoryUsage()));
246 : 3575 : cacheCoins.erase(itUs);
247 : : } else {
248 : : // A normal modification.
249 [ + + + - : 100309 : Assume(TrySub(cachedCoinsUsage, itUs->second.coin.DynamicMemoryUsage()));
+ + ]
250 [ + + ]: 41772 : if (cursor.WillErase(*it)) {
251 : : // Since this entry will be erased,
252 : : // we can move the coin into us instead of copying it
253 : 39989 : itUs->second.coin = std::move(it->second.coin);
254 : : } else {
255 : 1783 : itUs->second.coin = it->second.coin;
256 : : }
257 [ + + ]: 41772 : cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage();
258 [ + + ]: 41772 : if (!itUs->second.IsDirty()) {
259 : 37072 : CCoinsCacheEntry::SetDirty(*itUs, m_sentinel);
260 : 37072 : ++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 : 10139 : SetBestBlock(in_block_hash);
270 : 10139 : }
271 : :
272 : 10184 : void CCoinsViewCache::Flush(bool reallocate_cache)
273 : : {
274 : 10184 : auto cursor{CoinsViewCacheCursor(m_dirty_count, m_sentinel, cacheCoins, /*will_erase=*/true)};
275 : 10184 : base->BatchWrite(cursor, m_block_hash);
276 : 10184 : Assume(m_dirty_count == 0);
277 : 10184 : cacheCoins.clear();
278 [ + + ]: 10184 : if (reallocate_cache) {
279 : 943 : ReallocateCache();
280 : : }
281 : 10184 : cachedCoinsUsage = 0;
282 : 10184 : }
283 : :
284 : 241 : void CCoinsViewCache::Sync()
285 : : {
286 : 241 : auto cursor{CoinsViewCacheCursor(m_dirty_count, m_sentinel, cacheCoins, /*will_erase=*/false)};
287 : 241 : base->BatchWrite(cursor, m_block_hash);
288 [ - + ]: 241 : Assume(m_dirty_count == 0);
289 [ - + ]: 241 : 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 : 241 : }
294 : :
295 : 8839 : void CCoinsViewCache::Reset() noexcept
296 : : {
297 : 8839 : cacheCoins.clear();
298 : 8839 : cachedCoinsUsage = 0;
299 : 8839 : m_dirty_count = 0;
300 : 8839 : SetBestBlock(uint256::ZERO);
301 : 8839 : }
302 : :
303 : 11904 : void CCoinsViewCache::Uncache(const COutPoint& hash)
304 : : {
305 : 11904 : CCoinsMap::iterator it = cacheCoins.find(hash);
306 [ + + + + ]: 11904 : if (it != cacheCoins.end() && !it->second.IsDirty()) {
307 [ + + + - ]: 3043 : 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 : 1270 : (bool)it->second.coin.IsCoinBase());
314 : 1270 : cacheCoins.erase(it);
315 : : }
316 : 11904 : }
317 : :
318 : 36134 : unsigned int CCoinsViewCache::GetCacheSize() const {
319 : 36134 : return cacheCoins.size();
320 : : }
321 : :
322 : 1232 : bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
323 : : {
324 [ + - ]: 1232 : if (!tx.IsCoinBase()) {
325 [ - + + + ]: 2475 : for (unsigned int i = 0; i < tx.vin.size(); i++) {
326 [ + + ]: 1248 : if (!HaveCoin(tx.vin[i].prevout)) {
327 : : return false;
328 : : }
329 : : }
330 : : }
331 : : return true;
332 : : }
333 : :
334 : 943 : void CCoinsViewCache::ReallocateCache()
335 : : {
336 : : // Cache should be empty when we're calling this.
337 [ - + ]: 943 : assert(cacheCoins.size() == 0);
338 : 943 : cacheCoins.~CCoinsMap();
339 : 943 : m_cache_coins_memory_resource.~CCoinsMapMemoryResource();
340 : 943 : ::new (&m_cache_coins_memory_resource) CCoinsMapMemoryResource{};
341 : 943 : ::new (&cacheCoins) CCoinsMap{0, SaltedCoinsCacheHasher{/*deterministic=*/m_deterministic}, CCoinsMap::key_equal{}, &m_cache_coins_memory_resource};
342 : 943 : }
343 : :
344 : 313 : void CCoinsViewCache::SanityCheck() const
345 : : {
346 : 313 : size_t recomputed_usage = 0;
347 : 313 : size_t count_dirty = 0;
348 [ + + + + ]: 506868 : for (const auto& [_, entry] : cacheCoins) {
349 [ + + ]: 506555 : if (entry.coin.IsSpent()) {
350 [ + - - + ]: 17464 : assert(entry.IsDirty() && !entry.IsFresh()); // A spent coin must be dirty and cannot be fresh
351 : : } else {
352 [ + + - + ]: 489091 : assert(entry.IsDirty() || !entry.IsFresh()); // An unspent coin must not be fresh if not dirty
353 : : }
354 : :
355 : : // Recompute cachedCoinsUsage.
356 [ + + ]: 506555 : recomputed_usage += entry.coin.DynamicMemoryUsage();
357 : :
358 : : // Count the number of entries we expect in the linked list.
359 [ + + ]: 506555 : if (entry.IsDirty()) ++count_dirty;
360 : : }
361 : : // Iterate over the linked list of flagged entries.
362 : 313 : size_t count_linked = 0;
363 [ + + ]: 43082 : for (auto it = m_sentinel.second.Next(); it != &m_sentinel; it = it->second.Next()) {
364 : : // Verify linked list integrity.
365 [ - + ]: 42769 : assert(it->second.Next()->second.Prev() == it);
366 [ - + ]: 42769 : assert(it->second.Prev()->second.Next() == it);
367 : : // Verify they are actually flagged.
368 [ - + ]: 42769 : assert(it->second.IsDirty());
369 : : // Count the number of entries actually in the list.
370 : 42769 : ++count_linked;
371 : : }
372 [ + - - + ]: 313 : assert(count_dirty == count_linked && count_dirty == m_dirty_count);
373 [ - + ]: 313 : assert(recomputed_usage == cachedCoinsUsage);
374 : 313 : }
375 : :
376 : 8837 : CCoinsViewCache::ResetGuard CoinsViewOverlay::StartFetching(const CBlock& block LIFETIMEBOUND) noexcept
377 : : {
378 [ - + ]: 8837 : Assert(m_futures.empty());
379 [ - + ]: 8837 : Assert(m_inputs.empty());
380 [ - + ]: 8837 : Assert(m_input_head.load(std::memory_order_relaxed) == 0);
381 [ - + ]: 8837 : Assert(m_input_tail == 0);
382 [ + + ]: 8837 : 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 : 8836 : std::unordered_set<Txid, SaltedCoinsCacheHasher> earlier_txids;
387 [ - + ]: 8836 : earlier_txids.reserve(block.vtx.size());
388 [ + + ]: 9858 : for (const auto& tx : block.vtx | std::views::drop(1)) {
389 [ + + ]: 2056 : for (const auto& input : tx->vin) {
390 [ + + ]: 1034 : if (!earlier_txids.contains(input.prevout.hash)) m_inputs.emplace_back(input.prevout);
391 : : }
392 : 1022 : earlier_txids.emplace(tx->GetHash());
393 : : }
394 : : // Only submit tasks if we have something to fetch.
395 [ - + + + ]: 8836 : if (m_inputs.size()) {
396 : 160 : std::vector<std::function<void()>> tasks(workers_count, [this] {
397 [ + + ]: 1425 : while (ProcessInput()) {}
398 : 80 : });
399 [ + + ]: 80 : if (auto futures{m_thread_pool->Submit(std::move(tasks))}) {
400 : 79 : m_futures = std::move(*futures);
401 : : } 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 : 80 : }
409 : 80 : }
410 : 8836 : }
411 : 8837 : return CreateResetGuard();
412 : : }
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 : 155 : const Coin& AccessByTxid(const CCoinsViewCache& view, const Txid& txid)
418 : : {
419 : 155 : COutPoint iter(txid, 0);
420 [ + + ]: 8777924 : while (iter.n < MAX_OUTPUTS_PER_BLOCK) {
421 : 8777845 : const Coin& alternate = view.AccessCoin(iter);
422 [ + + ]: 8777845 : if (!alternate.IsSpent()) return alternate;
423 : 8777769 : ++iter.n;
424 : : }
425 : : return coinEmpty;
426 : : }
427 : :
428 : : template <typename ReturnType, typename Func>
429 : 37075 : static ReturnType ExecuteBackedWrapper(Func func, const std::vector<std::function<void()>>& err_callbacks)
430 : : {
431 : : try {
432 [ + - ]: 37075 : return func();
433 [ - - ]: 0 : } catch(const std::runtime_error& e) {
434 [ - - ]: 0 : for (const auto& f : err_callbacks) {
435 [ - - ]: 0 : f();
436 : : }
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 : : }
444 : : }
445 : :
446 : 18841 : std::optional<Coin> CCoinsViewErrorCatcher::GetCoin(const COutPoint& outpoint) const
447 : : {
448 : 37682 : return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::GetCoin(outpoint); }, m_err_callbacks);
449 : : }
450 : :
451 : 0 : bool CCoinsViewErrorCatcher::HaveCoin(const COutPoint& outpoint) const
452 : : {
453 : 0 : return ExecuteBackedWrapper<bool>([&]() { return CCoinsViewBacked::HaveCoin(outpoint); }, m_err_callbacks);
454 : : }
455 : :
456 : 18234 : std::optional<Coin> CCoinsViewErrorCatcher::PeekCoin(const COutPoint& outpoint) const
457 : : {
458 : 36468 : return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::PeekCoin(outpoint); }, m_err_callbacks);
459 : : }
|