1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
crate::ix!();
//-------------------------------------------[.cpp/bitcoin/src/coins.h]
pub enum CoinsCacheSizeState
{
/**
| The coins cache is in immediate need
| of a flush.
|
*/
CRITICAL = 2,
/**
| The cache is at >= 90% capacity.
|
*/
LARGE = 1,
OK = 0
}
pub type CoinsMap = HashMap<OutPoint,CoinsCacheEntry,SaltedOutpointHasher>;
/**
| CoinsView that adds a memory cache for
| transactions to another CoinsView
|
| By deleting the copy constructor, we
| prevent accidentally using it when
| one intends to create a cache on top of
| a base cache.
|
*/
pub struct CoinsViewCache {
base: CoinsViewBacked,
/**
| Make mutable so that we can "fill the
| cache" even from Get-methods declared
| as "const".
|
*/
hash_block: RefCell<u256>,
cache_coins: RefCell<CoinsMap>,
/**
| Cached dynamic memory usage for the
| inner Coin objects.
|
*/
cached_coins_usage: RefCell<usize>,
}
impl From<*mut dyn CoinsView> for CoinsViewCache {
fn from(base_in: *mut dyn CoinsView) -> Self {
todo!();
/*
: coins_view_backed(baseIn),
: cached_coins_usage(0),
*/
}
}
impl CoinsViewCache {
/* ---------- Standard CoinsView methods ---------- */
pub fn cursor(&self) -> Box<CoinsViewCursor> {
todo!();
/*
throw std::logic_error("CoinsViewCache cursor iteration not supported.");
*/
}
/**
| Calculate the size of the cache (in bytes)
|
*/
pub fn dynamic_memory_usage(&self) -> usize {
todo!();
/*
return memusage::DynamicUsage(cacheCoins) + cachedCoinsUsage;
*/
}
/**
| @note
|
| this is marked const, but may actually
| append to `cacheCoins`, increasing
| memory usage.
|
*/
pub fn fetch_coin<'a, I>(&self, outpoint: &OutPoint) -> I where I: Iterator<Item = <CoinsMap as IntoIterator>::Item> {
todo!();
/*
coins_map::iterator it = cacheCoins.find(outpoint);
if (it != cacheCoins.end())
return it;
Coin tmp;
if (!base->GetCoin(outpoint, tmp))
return cacheCoins.end();
coins_map::iterator ret = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::forward_as_tuple(std::move(tmp))).first;
if (ret->second.coin.IsSpent()) {
// The parent only has an empty entry for this outpoint; we can consider our
// version as fresh.
ret->second.flags = CCoinsCacheEntry::FRESH;
}
cachedCoinsUsage += ret->second.coin.DynamicMemoryUsage();
return ret;
*/
}
pub fn get_coin(&self,
outpoint: &OutPoint,
coin: &mut Coin) -> bool {
todo!();
/*
coins_map::const_iterator it = FetchCoin(outpoint);
if (it != cacheCoins.end()) {
coin = it->second.coin;
return !coin.IsSpent();
}
return false;
*/
}
/**
| Add a coin. Set possible_overwrite
| to true if an unspent version may already
| exist in the cache.
|
*/
pub fn add_coin(&mut self,
outpoint: &OutPoint,
coin: Coin,
possible_overwrite: bool) {
todo!();
/*
assert(!coin.IsSpent());
if (coin.out.scriptPubKey.IsUnspendable()) return;
coins_map::iterator it;
bool inserted;
std::tie(it, inserted) = cacheCoins.emplace(std::piecewise_construct, std::forward_as_tuple(outpoint), std::tuple<>());
bool fresh = false;
if (!inserted) {
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
}
if (!possible_overwrite) {
if (!it->second.coin.IsSpent()) {
throw std::logic_error("Attempted to overwrite an unspent coin (when possible_overwrite is false)");
}
// If the coin exists in this cache as a spent coin and is DIRTY, then
// its spentness hasn't been flushed to the parent cache. We're
// re-adding the coin to this cache now but we can't mark it as FRESH.
// If we mark it FRESH and then spend it before the cache is flushed
// we would remove it from this cache and would never flush spentness
// to the parent cache.
//
// Re-adding a spent coin can happen in the case of a re-org (the coin
// is 'spent' when the block adding it is disconnected and then
// re-added when it is also added in a newly connected block).
//
// If the coin doesn't exist in the current cache, or is spent but not
// DIRTY, then it can be marked FRESH.
fresh = !(it->second.flags & CCoinsCacheEntry::DIRTY);
}
it->second.coin = std::move(coin);
it->second.flags |= CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0);
cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
*/
}
/**
| Emplace a coin into cacheCoins without
| performing any checks, marking the
| emplaced coin as dirty.
|
| NOT FOR GENERAL USE. Used only when loading
| coins from a UTXO snapshot. @sa ChainstateManager::PopulateAndValidateSnapshot()
|
*/
pub fn emplace_coin_internaldanger(&mut self,
outpoint: OutPoint,
coin: Coin) {
*self.cached_coins_usage.borrow_mut() += coin.dynamic_memory_usage();
self.cache_coins.borrow_mut().insert(
outpoint,
CoinsCacheEntry {
coin: coin,
flags: CoinsCacheEntryFlags::DIRTY,
}
);
}
/**
| Spend a coin. Pass moveto in order to
| get the deleted data.
|
| If no unspent output exists for the passed
| outpoint, this call has no effect.
|
*/
pub fn spend_coin(&mut self,
outpoint: &OutPoint,
moveout: *mut Coin) -> bool {
todo!();
/*
coins_map::iterator it = FetchCoin(outpoint);
if (it == cacheCoins.end()) return false;
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
if (moveout) {
*moveout = std::move(it->second.coin);
}
if (it->second.flags & CCoinsCacheEntry::FRESH) {
cacheCoins.erase(it);
} else {
it->second.flags |= CCoinsCacheEntry::DIRTY;
it->second.coin.Clear();
}
return true;
*/
}
/**
| Return a reference to Coin in the cache,
| or coinEmpty if not found. This is more
| efficient than GetCoin.
|
| Generally, do not hold the reference
| returned for more than a short scope.
|
| While the current implementation allows
| for modifications to the contents of
| the cache while holding the reference,
| this behavior should not be relied on!
| To be safe, best to not hold the returned
| reference through any other calls to
| this cache.
|
*/
pub fn access_coin(&self, outpoint: &OutPoint) -> &Coin {
todo!();
/*
coins_map::const_iterator it = FetchCoin(outpoint);
if (it == cacheCoins.end()) {
return coinEmpty;
} else {
return it->second.coin;
}
*/
}
pub fn have_coin(&self, outpoint: &OutPoint) -> bool {
todo!();
/*
coins_map::const_iterator it = FetchCoin(outpoint);
return (it != cacheCoins.end() && !it->second.coin.IsSpent());
*/
}
/**
| Check if we have the given utxo already
| loaded in this cache.
|
| The semantics are the same as HaveCoin(),
| but no calls to the backing CoinsView
| are made.
|
*/
pub fn have_coin_in_cache(&self, outpoint: &OutPoint) -> bool {
todo!();
/*
coins_map::const_iterator it = cacheCoins.find(outpoint);
return (it != cacheCoins.end() && !it->second.coin.IsSpent());
*/
}
pub fn get_best_block(&self) -> u256 {
todo!();
/*
if (hashBlock.IsNull())
hashBlock = base->GetBestBlock();
return hashBlock;
*/
}
pub fn set_best_block(&mut self, hash_block_in: &u256) {
todo!();
/*
hashBlock = hashBlockIn;
*/
}
pub fn batch_write(&mut self,
map_coins: &mut CoinsMap,
hash_block_in: &u256) -> bool {
todo!();
/*
for (coins_map::iterator it = mapCoins.begin(); it != mapCoins.end(); it = mapCoins.erase(it)) {
// Ignore non-dirty entries (optimization).
if (!(it->second.flags & CCoinsCacheEntry::DIRTY)) {
continue;
}
coins_map::iterator itUs = cacheCoins.find(it->first);
if (itUs == cacheCoins.end()) {
// The parent cache does not have an entry, while the child cache does.
// We can ignore it if it's both spent and FRESH in the child
if (!(it->second.flags & CCoinsCacheEntry::FRESH && it->second.coin.IsSpent())) {
// Create the coin in the parent cache, move the data up
// and mark it as dirty.
CCoinsCacheEntry& entry = cacheCoins[it->first];
entry.coin = std::move(it->second.coin);
cachedCoinsUsage += entry.coin.DynamicMemoryUsage();
entry.flags = CCoinsCacheEntry::DIRTY;
// We can mark it FRESH in the parent if it was FRESH in the child
// Otherwise it might have just been flushed from the parent's cache
// and already exist in the grandparent
if (it->second.flags & CCoinsCacheEntry::FRESH) {
entry.flags |= CCoinsCacheEntry::FRESH;
}
}
} else {
// Found the entry in the parent cache
if ((it->second.flags & CCoinsCacheEntry::FRESH) && !itUs->second.coin.IsSpent()) {
// The coin was marked FRESH in the child cache, but the coin
// exists in the parent cache. If this ever happens, it means
// the FRESH flag was misapplied and there is a logic error in
// the calling code.
throw std::logic_error("FRESH flag misapplied to coin that exists in parent cache");
}
if ((itUs->second.flags & CCoinsCacheEntry::FRESH) && it->second.coin.IsSpent()) {
// The grandparent cache does not have an entry, and the coin
// has been spent. We can just delete it from the parent cache.
cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
cacheCoins.erase(itUs);
} else {
// A normal modification.
cachedCoinsUsage -= itUs->second.coin.DynamicMemoryUsage();
itUs->second.coin = std::move(it->second.coin);
cachedCoinsUsage += itUs->second.coin.DynamicMemoryUsage();
itUs->second.flags |= CCoinsCacheEntry::DIRTY;
// NOTE: It isn't safe to mark the coin as FRESH in the parent
// cache. If it already existed and was spent in the parent
// cache then marking it FRESH would prevent that spentness
// from being flushed to the grandparent.
}
}
}
hashBlock = hashBlockIn;
return true;
*/
}
/**
| Push the modifications applied to this
| cache to its base.
|
| Failure to call this method before destruction
| will cause the changes to be forgotten.
|
| If false is returned, the state of this
| cache (and its backing view) will be
| undefined.
|
*/
pub fn flush(&mut self) -> bool {
todo!();
/*
bool fOk = base->BatchWrite(cacheCoins, hashBlock);
cacheCoins.clear();
cachedCoinsUsage = 0;
return fOk;
*/
}
/**
| Removes the UTXO with the given outpoint
| from the cache, if it is not modified.
|
*/
pub fn uncache(&mut self, hash: &OutPoint) {
todo!();
/*
coins_map::iterator it = cacheCoins.find(hash);
if (it != cacheCoins.end() && it->second.flags == 0) {
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
cacheCoins.erase(it);
}
*/
}
/**
| Calculate the size of the cache (in number
| of transaction outputs)
|
*/
pub fn get_cache_size(&self) -> u32 {
todo!();
/*
return cacheCoins.size();
*/
}
/**
| Check whether all prevouts of the transaction
| are present in the UTXO set represented
| by this view
|
*/
pub fn have_inputs(&self, tx: &Transaction) -> bool {
todo!();
/*
if (!tx.IsCoinBase()) {
for (unsigned int i = 0; i < tx.vin.size(); i++) {
if (!HaveCoin(tx.vin[i].prevout)) {
return false;
}
}
}
return true;
*/
}
/**
| Force a reallocation of the cache map. This is
| required when downsizing the cache because the
| map's allocator may be hanging onto a lot of
| memory despite having called .clear().
|
| See:
| https://stackoverflow.com/questions/42114044/how-to-release-unordered-map-memory
*/
pub fn reallocate_cache(&mut self) {
todo!();
/*
// Cache should be empty when we're calling this.
assert(cacheCoins.size() == 0);
cacheCoins.~CCoinsMap();
::new (&cacheCoins) CCoinsMap();
*/
}
}