/root/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 <random.h> |
9 | | #include <uint256.h> |
10 | | #include <util/log.h> |
11 | | #include <util/trace.h> |
12 | | |
13 | | TRACEPOINT_SEMAPHORE(utxocache, add); |
14 | | TRACEPOINT_SEMAPHORE(utxocache, spent); |
15 | | TRACEPOINT_SEMAPHORE(utxocache, uncache); |
16 | | |
17 | 0 | std::optional<Coin> CCoinsView::GetCoin(const COutPoint& outpoint) const { return std::nullopt; } |
18 | 0 | std::optional<Coin> CCoinsView::PeekCoin(const COutPoint& outpoint) const { return GetCoin(outpoint); } |
19 | 0 | uint256 CCoinsView::GetBestBlock() const { return uint256(); } |
20 | 0 | std::vector<uint256> CCoinsView::GetHeadBlocks() const { return std::vector<uint256>(); } |
21 | | void CCoinsView::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& hashBlock) |
22 | 0 | { |
23 | 0 | for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) { } |
24 | 0 | } |
25 | | |
26 | 0 | std::unique_ptr<CCoinsViewCursor> CCoinsView::Cursor() const { return nullptr; } |
27 | | |
28 | | bool CCoinsView::HaveCoin(const COutPoint &outpoint) const |
29 | 0 | { |
30 | 0 | return GetCoin(outpoint).has_value(); |
31 | 0 | } |
32 | | |
33 | 0 | CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { } |
34 | 0 | std::optional<Coin> CCoinsViewBacked::GetCoin(const COutPoint& outpoint) const { return base->GetCoin(outpoint); } |
35 | 0 | std::optional<Coin> CCoinsViewBacked::PeekCoin(const COutPoint& outpoint) const { return base->PeekCoin(outpoint); } |
36 | 0 | bool CCoinsViewBacked::HaveCoin(const COutPoint &outpoint) const { return base->HaveCoin(outpoint); } |
37 | 0 | uint256 CCoinsViewBacked::GetBestBlock() const { return base->GetBestBlock(); } |
38 | 0 | std::vector<uint256> CCoinsViewBacked::GetHeadBlocks() const { return base->GetHeadBlocks(); } |
39 | 0 | void CCoinsViewBacked::SetBackend(CCoinsView &viewIn) { base = &viewIn; } |
40 | 0 | void CCoinsViewBacked::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& hashBlock) { base->BatchWrite(cursor, hashBlock); } |
41 | 0 | std::unique_ptr<CCoinsViewCursor> CCoinsViewBacked::Cursor() const { return base->Cursor(); } |
42 | 0 | size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); } |
43 | | |
44 | | std::optional<Coin> CCoinsViewCache::PeekCoin(const COutPoint& outpoint) const |
45 | 0 | { |
46 | 0 | if (auto it{cacheCoins.find(outpoint)}; it != cacheCoins.end()) { |
47 | 0 | return it->second.coin.IsSpent() ? std::nullopt : std::optional{it->second.coin}; |
48 | 0 | } |
49 | 0 | return base->PeekCoin(outpoint); |
50 | 0 | } |
51 | | |
52 | | CCoinsViewCache::CCoinsViewCache(CCoinsView* baseIn, bool deterministic) : |
53 | 0 | CCoinsViewBacked(baseIn), m_deterministic(deterministic), |
54 | 0 | cacheCoins(0, SaltedOutpointHasher(/*deterministic=*/deterministic), CCoinsMap::key_equal{}, &m_cache_coins_memory_resource) |
55 | 0 | { |
56 | 0 | m_sentinel.second.SelfRef(m_sentinel); |
57 | 0 | } |
58 | | |
59 | 0 | size_t CCoinsViewCache::DynamicMemoryUsage() const { |
60 | 0 | return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage; |
61 | 0 | } |
62 | | |
63 | | std::optional<Coin> CCoinsViewCache::FetchCoinFromBase(const COutPoint& outpoint) const |
64 | 0 | { |
65 | 0 | return base->GetCoin(outpoint); |
66 | 0 | } |
67 | | |
68 | 0 | CCoinsMap::iterator CCoinsViewCache::FetchCoin(const COutPoint &outpoint) const { |
69 | 0 | const auto [ret, inserted] = cacheCoins.try_emplace(outpoint); |
70 | 0 | if (inserted) { |
71 | 0 | if (auto coin{FetchCoinFromBase(outpoint)}) { |
72 | 0 | ret->second.coin = std::move(*coin); |
73 | 0 | cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage(); |
74 | 0 | Assert(!ret->second.coin.IsSpent()); Line | Count | Source | 113 | 0 | #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val) |
|
75 | 0 | } else { |
76 | 0 | cacheCoins.erase(ret); |
77 | 0 | return cacheCoins.end(); |
78 | 0 | } |
79 | 0 | } |
80 | 0 | return ret; |
81 | 0 | } |
82 | | |
83 | | std::optional<Coin> CCoinsViewCache::GetCoin(const COutPoint& outpoint) const |
84 | 0 | { |
85 | 0 | if (auto it{FetchCoin(outpoint)}; it != cacheCoins.end() && !it->second.coin.IsSpent()) return it->second.coin; |
86 | 0 | return std::nullopt; |
87 | 0 | } |
88 | | |
89 | 0 | void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possible_overwrite) { |
90 | 0 | assert(!coin.IsSpent()); |
91 | 0 | if (coin.out.scriptPubKey.IsUnspendable()) return; |
92 | 0 | CCoinsMap::iterator it; |
93 | 0 | bool inserted; |
94 | 0 | std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>()); |
95 | 0 | bool fresh = false; |
96 | 0 | if (!possible_overwrite) { |
97 | 0 | if (!it->second.coin.IsSpent()) { |
98 | 0 | throw std::logic_error("Attempted to overwrite an unspent coin (when possible_overwrite is false)"); |
99 | 0 | } |
100 | | // If the coin exists in this cache as a spent coin and is DIRTY, then |
101 | | // its spentness hasn't been flushed to the parent cache. We're |
102 | | // re-adding the coin to this cache now but we can't mark it as FRESH. |
103 | | // If we mark it FRESH and then spend it before the cache is flushed |
104 | | // we would remove it from this cache and would never flush spentness |
105 | | // to the parent cache. |
106 | | // |
107 | | // Re-adding a spent coin can happen in the case of a re-org (the coin |
108 | | // is 'spent' when the block adding it is disconnected and then |
109 | | // re-added when it is also added in a newly connected block). |
110 | | // |
111 | | // If the coin doesn't exist in the current cache, or is spent but not |
112 | | // DIRTY, then it can be marked FRESH. |
113 | 0 | fresh = !it->second.IsDirty(); |
114 | 0 | } |
115 | 0 | if (!inserted) { |
116 | 0 | Assume(TrySub(m_dirty_count, it->second.IsDirty())); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
117 | 0 | Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage())); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
118 | 0 | } |
119 | 0 | it->second.coin = std::move(coin); |
120 | 0 | CCoinsCacheEntry::SetDirty(*it, m_sentinel); |
121 | 0 | ++m_dirty_count; |
122 | 0 | if (fresh) CCoinsCacheEntry::SetFresh(*it, m_sentinel); |
123 | 0 | cachedCoinsUsage += it->second.coin.DynamicMemoryUsage(); |
124 | 0 | TRACEPOINT(utxocache, add, |
125 | 0 | outpoint.hash.data(), |
126 | 0 | (uint32_t)outpoint.n, |
127 | 0 | (uint32_t)it->second.coin.nHeight, |
128 | 0 | (int64_t)it->second.coin.out.nValue, |
129 | 0 | (bool)it->second.coin.IsCoinBase()); |
130 | 0 | } |
131 | | |
132 | 0 | void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin) { |
133 | 0 | const auto mem_usage{coin.DynamicMemoryUsage()}; |
134 | 0 | auto [it, inserted] = cacheCoins.try_emplace(std::move(outpoint), std::move(coin)); |
135 | 0 | if (inserted) { |
136 | 0 | CCoinsCacheEntry::SetDirty(*it, m_sentinel); |
137 | 0 | ++m_dirty_count; |
138 | 0 | cachedCoinsUsage += mem_usage; |
139 | 0 | } |
140 | 0 | } |
141 | | |
142 | 0 | void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) { |
143 | 0 | bool fCoinbase = tx.IsCoinBase(); |
144 | 0 | const Txid& txid = tx.GetHash(); |
145 | 0 | for (size_t i = 0; i < tx.vout.size(); ++i) { |
146 | 0 | bool overwrite = check_for_overwrite ? cache.HaveCoin(COutPoint(txid, i)) : fCoinbase; |
147 | | // Coinbase transactions can always be overwritten, in order to correctly |
148 | | // deal with the pre-BIP30 occurrences of duplicate coinbase transactions. |
149 | 0 | cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase), overwrite); |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | 0 | bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) { |
154 | 0 | CCoinsMap::iterator it = FetchCoin(outpoint); |
155 | 0 | if (it == cacheCoins.end()) return false; |
156 | 0 | Assume(TrySub(m_dirty_count, it->second.IsDirty())); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
157 | 0 | Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage())); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
158 | 0 | TRACEPOINT(utxocache, spent, |
159 | 0 | outpoint.hash.data(), |
160 | 0 | (uint32_t)outpoint.n, |
161 | 0 | (uint32_t)it->second.coin.nHeight, |
162 | 0 | (int64_t)it->second.coin.out.nValue, |
163 | 0 | (bool)it->second.coin.IsCoinBase()); |
164 | 0 | if (moveout) { |
165 | 0 | *moveout = std::move(it->second.coin); |
166 | 0 | } |
167 | 0 | if (it->second.IsFresh()) { |
168 | 0 | cacheCoins.erase(it); |
169 | 0 | } else { |
170 | 0 | CCoinsCacheEntry::SetDirty(*it, m_sentinel); |
171 | 0 | ++m_dirty_count; |
172 | 0 | it->second.coin.Clear(); |
173 | 0 | } |
174 | 0 | return true; |
175 | 0 | } |
176 | | |
177 | | static const Coin coinEmpty; |
178 | | |
179 | 0 | const Coin& CCoinsViewCache::AccessCoin(const COutPoint &outpoint) const { |
180 | 0 | CCoinsMap::const_iterator it = FetchCoin(outpoint); |
181 | 0 | if (it == cacheCoins.end()) { |
182 | 0 | return coinEmpty; |
183 | 0 | } else { |
184 | 0 | return it->second.coin; |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | 0 | bool CCoinsViewCache::HaveCoin(const COutPoint &outpoint) const { |
189 | 0 | CCoinsMap::const_iterator it = FetchCoin(outpoint); |
190 | 0 | return (it != cacheCoins.end() && !it->second.coin.IsSpent()); |
191 | 0 | } |
192 | | |
193 | 0 | bool CCoinsViewCache::HaveCoinInCache(const COutPoint &outpoint) const { |
194 | 0 | CCoinsMap::const_iterator it = cacheCoins.find(outpoint); |
195 | 0 | return (it != cacheCoins.end() && !it->second.coin.IsSpent()); |
196 | 0 | } |
197 | | |
198 | 0 | uint256 CCoinsViewCache::GetBestBlock() const { |
199 | 0 | if (hashBlock.IsNull()) |
200 | 0 | hashBlock = base->GetBestBlock(); |
201 | 0 | return hashBlock; |
202 | 0 | } |
203 | | |
204 | 0 | void CCoinsViewCache::SetBestBlock(const uint256 &hashBlockIn) { |
205 | 0 | hashBlock = hashBlockIn; |
206 | 0 | } |
207 | | |
208 | | void CCoinsViewCache::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& hashBlockIn) |
209 | 0 | { |
210 | 0 | for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) { |
211 | 0 | if (!it->second.IsDirty()) { // TODO a cursor can only contain dirty entries |
212 | 0 | continue; |
213 | 0 | } |
214 | 0 | auto [itUs, inserted]{cacheCoins.try_emplace(it->first)}; |
215 | 0 | if (inserted) { |
216 | 0 | if (it->second.IsFresh() && it->second.coin.IsSpent()) { |
217 | 0 | cacheCoins.erase(itUs); // TODO fresh coins should have been removed at spend |
218 | 0 | } else { |
219 | | // The parent cache does not have an entry, while the child cache does. |
220 | | // Move the data up and mark it as dirty. |
221 | 0 | CCoinsCacheEntry& entry{itUs->second}; |
222 | 0 | assert(entry.coin.DynamicMemoryUsage() == 0); |
223 | 0 | if (cursor.WillErase(*it)) { |
224 | | // Since this entry will be erased, |
225 | | // we can move the coin into us instead of copying it |
226 | 0 | entry.coin = std::move(it->second.coin); |
227 | 0 | } else { |
228 | 0 | entry.coin = it->second.coin; |
229 | 0 | } |
230 | 0 | CCoinsCacheEntry::SetDirty(*itUs, m_sentinel); |
231 | 0 | ++m_dirty_count; |
232 | 0 | cachedCoinsUsage += entry.coin.DynamicMemoryUsage(); |
233 | | // We can mark it FRESH in the parent if it was FRESH in the child |
234 | | // Otherwise it might have just been flushed from the parent's cache |
235 | | // and already exist in the grandparent |
236 | 0 | if (it->second.IsFresh()) CCoinsCacheEntry::SetFresh(*itUs, m_sentinel); |
237 | 0 | } |
238 | 0 | } else { |
239 | | // Found the entry in the parent cache |
240 | 0 | if (it->second.IsFresh() && !itUs->second.coin.IsSpent()) { |
241 | | // The coin was marked FRESH in the child cache, but the coin |
242 | | // exists in the parent cache. If this ever happens, it means |
243 | | // the FRESH flag was misapplied and there is a logic error in |
244 | | // the calling code. |
245 | 0 | throw std::logic_error("FRESH flag misapplied to coin that exists in parent cache"); |
246 | 0 | } |
247 | | |
248 | 0 | if (itUs->second.IsFresh() && it->second.coin.IsSpent()) { |
249 | | // The grandparent cache does not have an entry, and the coin |
250 | | // has been spent. We can just delete it from the parent cache. |
251 | 0 | Assume(TrySub(m_dirty_count, itUs->second.IsDirty())); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
252 | 0 | Assume(TrySub(cachedCoinsUsage, itUs->second.coin.DynamicMemoryUsage())); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
253 | 0 | cacheCoins.erase(itUs); |
254 | 0 | } else { |
255 | | // A normal modification. |
256 | 0 | Assume(TrySub(cachedCoinsUsage, itUs->second.coin.DynamicMemoryUsage())); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
257 | 0 | if (cursor.WillErase(*it)) { |
258 | | // Since this entry will be erased, |
259 | | // we can move the coin into us instead of copying it |
260 | 0 | itUs->second.coin = std::move(it->second.coin); |
261 | 0 | } else { |
262 | 0 | itUs->second.coin = it->second.coin; |
263 | 0 | } |
264 | 0 | cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage(); |
265 | 0 | if (!itUs->second.IsDirty()) { |
266 | 0 | CCoinsCacheEntry::SetDirty(*itUs, m_sentinel); |
267 | 0 | ++m_dirty_count; |
268 | 0 | } |
269 | | // NOTE: It isn't safe to mark the coin as FRESH in the parent |
270 | | // cache. If it already existed and was spent in the parent |
271 | | // cache then marking it FRESH would prevent that spentness |
272 | | // from being flushed to the grandparent. |
273 | 0 | } |
274 | 0 | } |
275 | 0 | } |
276 | 0 | SetBestBlock(hashBlockIn); |
277 | 0 | } |
278 | | |
279 | | void CCoinsViewCache::Flush(bool reallocate_cache) |
280 | 0 | { |
281 | 0 | auto cursor{CoinsViewCacheCursor(m_dirty_count, m_sentinel, cacheCoins, /*will_erase=*/true)}; |
282 | 0 | base->BatchWrite(cursor, hashBlock); |
283 | 0 | Assume(m_dirty_count == 0); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
284 | 0 | cacheCoins.clear(); |
285 | 0 | if (reallocate_cache) { |
286 | 0 | ReallocateCache(); |
287 | 0 | } |
288 | 0 | cachedCoinsUsage = 0; |
289 | 0 | } |
290 | | |
291 | | void CCoinsViewCache::Sync() |
292 | 0 | { |
293 | 0 | auto cursor{CoinsViewCacheCursor(m_dirty_count, m_sentinel, cacheCoins, /*will_erase=*/false)}; |
294 | 0 | base->BatchWrite(cursor, hashBlock); |
295 | 0 | Assume(m_dirty_count == 0); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
296 | 0 | if (m_sentinel.second.Next() != &m_sentinel) { |
297 | | /* BatchWrite must clear flags of all entries */ |
298 | 0 | throw std::logic_error("Not all unspent flagged entries were cleared"); |
299 | 0 | } |
300 | 0 | } |
301 | | |
302 | | void CCoinsViewCache::Reset() noexcept |
303 | 0 | { |
304 | 0 | cacheCoins.clear(); |
305 | 0 | cachedCoinsUsage = 0; |
306 | 0 | m_dirty_count = 0; |
307 | 0 | SetBestBlock(uint256::ZERO); |
308 | 0 | } |
309 | | |
310 | | void CCoinsViewCache::Uncache(const COutPoint& hash) |
311 | 0 | { |
312 | 0 | CCoinsMap::iterator it = cacheCoins.find(hash); |
313 | 0 | if (it != cacheCoins.end() && !it->second.IsDirty()) { |
314 | 0 | Assume(TrySub(cachedCoinsUsage, it->second.coin.DynamicMemoryUsage())); Line | Count | Source | 125 | 0 | #define Assume(val) inline_assertion_check<false>(val, std::source_location::current(), #val) |
|
315 | 0 | TRACEPOINT(utxocache, uncache, |
316 | 0 | hash.hash.data(), |
317 | 0 | (uint32_t)hash.n, |
318 | 0 | (uint32_t)it->second.coin.nHeight, |
319 | 0 | (int64_t)it->second.coin.out.nValue, |
320 | 0 | (bool)it->second.coin.IsCoinBase()); |
321 | 0 | cacheCoins.erase(it); |
322 | 0 | } |
323 | 0 | } |
324 | | |
325 | 0 | unsigned int CCoinsViewCache::GetCacheSize() const { |
326 | 0 | return cacheCoins.size(); |
327 | 0 | } |
328 | | |
329 | | bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const |
330 | 0 | { |
331 | 0 | if (!tx.IsCoinBase()) { |
332 | 0 | for (unsigned int i = 0; i < tx.vin.size(); i++) { |
333 | 0 | if (!HaveCoin(tx.vin[i].prevout)) { |
334 | 0 | return false; |
335 | 0 | } |
336 | 0 | } |
337 | 0 | } |
338 | 0 | return true; |
339 | 0 | } |
340 | | |
341 | | void CCoinsViewCache::ReallocateCache() |
342 | 0 | { |
343 | | // Cache should be empty when we're calling this. |
344 | 0 | assert(cacheCoins.size() == 0); |
345 | 0 | cacheCoins.~CCoinsMap(); |
346 | 0 | m_cache_coins_memory_resource.~CCoinsMapMemoryResource(); |
347 | 0 | ::new (&m_cache_coins_memory_resource) CCoinsMapMemoryResource{}; |
348 | 0 | ::new (&cacheCoins) CCoinsMap{0, SaltedOutpointHasher{/*deterministic=*/m_deterministic}, CCoinsMap::key_equal{}, &m_cache_coins_memory_resource}; |
349 | 0 | } |
350 | | |
351 | | void CCoinsViewCache::SanityCheck() const |
352 | 0 | { |
353 | 0 | size_t recomputed_usage = 0; |
354 | 0 | size_t count_dirty = 0; |
355 | 0 | for (const auto& [_, entry] : cacheCoins) { |
356 | 0 | if (entry.coin.IsSpent()) { |
357 | 0 | assert(entry.IsDirty() && !entry.IsFresh()); // A spent coin must be dirty and cannot be fresh |
358 | 0 | } else { |
359 | 0 | assert(entry.IsDirty() || !entry.IsFresh()); // An unspent coin must not be fresh if not dirty |
360 | 0 | } |
361 | | |
362 | | // Recompute cachedCoinsUsage. |
363 | 0 | recomputed_usage += entry.coin.DynamicMemoryUsage(); |
364 | | |
365 | | // Count the number of entries we expect in the linked list. |
366 | 0 | if (entry.IsDirty()) ++count_dirty; |
367 | 0 | } |
368 | | // Iterate over the linked list of flagged entries. |
369 | 0 | size_t count_linked = 0; |
370 | 0 | for (auto it = m_sentinel.second.Next(); it != &m_sentinel; it = it->second.Next()) { |
371 | | // Verify linked list integrity. |
372 | 0 | assert(it->second.Next()->second.Prev() == it); |
373 | 0 | assert(it->second.Prev()->second.Next() == it); |
374 | | // Verify they are actually flagged. |
375 | 0 | assert(it->second.IsDirty()); |
376 | | // Count the number of entries actually in the list. |
377 | 0 | ++count_linked; |
378 | 0 | } |
379 | 0 | assert(count_dirty == count_linked && count_dirty == m_dirty_count); |
380 | 0 | assert(recomputed_usage == cachedCoinsUsage); |
381 | 0 | } |
382 | | |
383 | | static const uint64_t MIN_TRANSACTION_OUTPUT_WEIGHT{WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut())}; |
384 | | static const uint64_t MAX_OUTPUTS_PER_BLOCK{MAX_BLOCK_WEIGHT / MIN_TRANSACTION_OUTPUT_WEIGHT}; |
385 | | |
386 | | const Coin& AccessByTxid(const CCoinsViewCache& view, const Txid& txid) |
387 | 0 | { |
388 | 0 | COutPoint iter(txid, 0); |
389 | 0 | while (iter.n < MAX_OUTPUTS_PER_BLOCK) { |
390 | 0 | const Coin& alternate = view.AccessCoin(iter); |
391 | 0 | if (!alternate.IsSpent()) return alternate; |
392 | 0 | ++iter.n; |
393 | 0 | } |
394 | 0 | return coinEmpty; |
395 | 0 | } |
396 | | |
397 | | template <typename ReturnType, typename Func> |
398 | | static ReturnType ExecuteBackedWrapper(Func func, const std::vector<std::function<void()>>& err_callbacks) |
399 | 0 | { |
400 | 0 | try { |
401 | 0 | return func(); |
402 | 0 | } catch(const std::runtime_error& e) { |
403 | 0 | for (const auto& f : err_callbacks) { |
404 | 0 | f(); |
405 | 0 | } |
406 | 0 | LogError("Error reading from database: %s\n", e.what());Line | Count | Source | 97 | 0 | #define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 89 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
| LogError("Error reading from database: %s\n", e.what());Line | Count | Source | 97 | 0 | #define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 89 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
| LogError("Error reading from database: %s\n", e.what());Line | Count | Source | 97 | 0 | #define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__) Line | Count | Source | 89 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__) |
|
|
407 | | // Starting the shutdown sequence and returning false to the caller would be |
408 | | // interpreted as 'entry not found' (as opposed to unable to read data), and |
409 | | // could lead to invalid interpretation. Just exit immediately, as we can't |
410 | | // continue anyway, and all writes should be atomic. |
411 | 0 | std::abort(); |
412 | 0 | } |
413 | 0 | } Unexecuted instantiation: 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&) 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&) Unexecuted instantiation: 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&) |
414 | | |
415 | | std::optional<Coin> CCoinsViewErrorCatcher::GetCoin(const COutPoint& outpoint) const |
416 | 0 | { |
417 | 0 | return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::GetCoin(outpoint); }, m_err_callbacks); |
418 | 0 | } |
419 | | |
420 | | bool CCoinsViewErrorCatcher::HaveCoin(const COutPoint& outpoint) const |
421 | 0 | { |
422 | 0 | return ExecuteBackedWrapper<bool>([&]() { return CCoinsViewBacked::HaveCoin(outpoint); }, m_err_callbacks); |
423 | 0 | } |
424 | | |
425 | | std::optional<Coin> CCoinsViewErrorCatcher::PeekCoin(const COutPoint& outpoint) const |
426 | 0 | { |
427 | 0 | return ExecuteBackedWrapper<std::optional<Coin>>([&]() { return CCoinsViewBacked::PeekCoin(outpoint); }, m_err_callbacks); |
428 | 0 | } |