25constexpr uint32_t NUM_OUTPOINTS = 256;
27constexpr uint32_t NUM_COINS = 256;
29constexpr uint32_t MAX_CACHES = 4;
31using coinidx_type = uint8_t;
39 Coin coins[NUM_COINS];
46 static const uint8_t PREFIX_O[1] = {
'o'};
47 static const uint8_t PREFIX_S[1] = {
's'};
48 static const uint8_t PREFIX_M[1] = {
'm'};
51 coinbase.
vin.emplace_back();
55 for (uint32_t i = 0; i < NUM_OUTPOINTS; ++i) {
56 uint32_t idx = (i * 1200U) >> 12;
57 const uint8_t ser[4] = {uint8_t(idx), uint8_t(idx >> 8), uint8_t(idx >> 16), uint8_t(idx >> 24)};
62 tx.
vin.emplace_back(outpoints[i]);
66 for (uint32_t i = 0; i < NUM_COINS; ++i) {
67 const uint8_t ser[4] = {uint8_t(i), uint8_t(i >> 8), uint8_t(i >> 16), uint8_t(i >> 24)};
78 std::copy(hash.
begin(), hash.
begin() + 20, coins[i].out.scriptPubKey.begin() + 3);
86 std::copy(hash.
begin(), hash.
begin() + 20, coins[i].out.scriptPubKey.begin() + 2);
93 std::copy(hash.
begin(), hash.
begin() + 20, coins[i].out.scriptPubKey.begin() + 2);
99 std::copy(hash.
begin(), hash.
begin() + 32, coins[i].out.scriptPubKey.begin() + 2);
105 std::copy(hash.
begin(), hash.
begin() + 32, coins[i].out.scriptPubKey.begin() + 2);
117enum class EntryType : uint8_t
135 coinidx_type coinidx;
143 CacheEntry entry[NUM_OUTPOINTS];
146 for (uint32_t i = 0; i < NUM_OUTPOINTS; ++i) {
158 std::map<COutPoint, Coin> m_data;
163 if (
auto it{m_data.find(outpoint)}; it != m_data.end()) {
164 assert(!it->second.IsSpent());
172 for (
auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) {
173 if (it->second.IsDirty()) {
174 if (it->second.coin.IsSpent()) {
175 m_data.erase(it->first);
177 if (cursor.WillErase(*it)) {
178 m_data[it->first] = std::move(it->second.coin);
180 m_data[it->first] = it->second.coin;
185 auto it2 = m_data.find(it->first);
186 if (it->second.coin.IsSpent()) {
187 assert(it2 == m_data.end());
189 assert(it2 != m_data.end());
190 assert(it->second.coin.out == it2->second.out);
191 assert(it->second.coin.fCoinBase == it2->second.fCoinBase);
192 assert(it->second.coin.nHeight == it2->second.nHeight);
201struct OverlayFetchScope
209std::shared_ptr<ThreadPool> g_thread_pool{std::make_shared<ThreadPool>(
"cache_fuzz")};
223 static const PrecomputedData
data;
228 std::vector<std::unique_ptr<CCoinsViewCache>>
caches;
240 auto lookup = [&](uint32_t outpointidx,
int sim_idx = -1) -> std::optional<std::pair<coinidx_type, uint32_t>> {
241 uint32_t cache_idx = sim_idx == -1 ?
caches.size() : sim_idx;
243 const auto& entry =
sim_caches[cache_idx].entry[outpointidx];
244 if (entry.entrytype == EntryType::UNSPENT) {
245 return {{entry.coinidx, entry.height}};
249 if (cache_idx == 0)
break;
260 for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
262 prev_cache.entry[outpointidx] = cache.entry[outpointidx];
288 auto sim =
lookup(outpointidx);
291 caches.back()->PeekCoin(
data.outpoints[outpointidx]) :
292 caches.back()->GetCoin(
data.outpoints[outpointidx]);
294 if (!sim.has_value()) {
297 assert(realcoin && !realcoin->IsSpent());
298 const auto& simcoin =
data.coins[sim->first];
299 assert(realcoin->out == simcoin.out);
300 assert(realcoin->fCoinBase == simcoin.fCoinBase);
301 assert(realcoin->nHeight == sim->second);
308 auto sim =
lookup(outpointidx);
310 auto real =
caches.back()->HaveCoin(
data.outpoints[outpointidx]);
312 assert(sim.has_value() == real);
318 (void)
caches.back()->HaveCoinInCache(
data.outpoints[outpointidx]);
324 auto sim =
lookup(outpointidx);
326 const auto& realcoin =
caches.back()->AccessCoin(
data.outpoints[outpointidx]);
328 if (!sim.has_value()) {
329 assert(realcoin.IsSpent());
331 assert(!realcoin.IsSpent());
332 const auto& simcoin =
data.coins[sim->first];
333 assert(simcoin.out == realcoin.out);
334 assert(simcoin.fCoinBase == realcoin.fCoinBase);
335 assert(realcoin.nHeight == sim->second);
343 auto sim =
lookup(outpointidx);
347 caches.back()->AddCoin(
data.outpoints[outpointidx], std::move(coin), sim.has_value());
350 entry.entrytype = EntryType::UNSPENT;
351 entry.coinidx = coinidx;
361 caches.back()->AddCoin(
data.outpoints[outpointidx], std::move(coin),
true);
364 entry.entrytype = EntryType::UNSPENT;
365 entry.coinidx = coinidx;
372 caches.back()->SpendCoin(
data.outpoints[outpointidx],
nullptr);
380 auto sim =
lookup(outpointidx);
383 caches.back()->SpendCoin(
data.outpoints[outpointidx], &realcoin);
387 if (!sim.has_value()) {
391 const auto& simcoin =
data.coins[sim->first];
401 caches.back()->Uncache(
data.outpoints[outpointidx]);
405 if (
caches.size() != MAX_CACHES) {
425 caches.back()->SanityCheck();
458 (void)
caches.back()->CreateResetGuard();
463 (void)
caches.back()->GetCacheSize();
467 (void)
caches.back()->DynamicMemoryUsage();
478 cache->SanityCheck();
482 for (
unsigned sim_idx = 1; sim_idx <=
caches.size(); ++sim_idx) {
483 auto& cache = *
caches[sim_idx - 1];
484 size_t cache_size = 0;
486 for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
487 cache_size += cache.HaveCoinInCache(
data.outpoints[outpointidx]);
488 const auto real{cache.PeekCoin(
data.outpoints[outpointidx])};
489 auto sim =
lookup(outpointidx, sim_idx);
490 if (!sim.has_value()) {
494 assert(real->out ==
data.coins[sim->first].out);
495 assert(real->fCoinBase ==
data.coins[sim->first].fCoinBase);
496 assert(real->nHeight == sim->second);
501 assert(cache.GetCacheSize() >= cache_size);
505 for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
506 auto realcoin =
bottom.GetCoin(
data.outpoints[outpointidx]);
507 auto sim =
lookup(outpointidx, 0);
508 if (!sim.has_value()) {
511 assert(realcoin && !realcoin->IsSpent());
512 assert(realcoin->out ==
data.coins[sim->first].out);
513 assert(realcoin->fCoinBase ==
data.coins[sim->first].fCoinBase);
514 assert(realcoin->nHeight == sim->second);
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr int32_t DEFAULT_PREVOUTFETCH_THREADS
std::vector< CTransactionRef > vtx
CCoinsView that adds a memory cache for transactions to another CCoinsView.
An outpoint - a combination of a transaction hash and an index n into its vout.
A hasher class for SHA-256.
void Finalize(unsigned char hash[OUTPUT_SIZE])
CSHA256 & Write(const unsigned char *data, size_t len)
CTxOut out
unspent transaction output
bool IsSpent() const
Either this coin never existed (see e.g.
bool fCoinBase
whether containing transaction was a coinbase
uint32_t nHeight
at which height this containing transaction was included in the active block chain
void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &) override
Do a bulk modification (multiple Coin changes + BestBlock change).
std::optional< Coin > GetCoin(const COutPoint &) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
CCoinsViewCache subclass that asynchronously fetches most block input prevouts in parallel during Con...
bool AllInputsConsumed() const noexcept
Verify that all parallel fetched input prevouts have been consumed.
T ConsumeIntegralInRange(T min, T max)
constexpr uint64_t GetUint64(int pos) const
constexpr unsigned char * begin()
void resize(size_type new_size)
static transaction_identifier FromUint256(const uint256 &id)
std::vector< std::unique_ptr< CCoinsViewCache > > caches
Real CCoinsViewCache objects.
std::unique_ptr< OverlayFetchScope > overlay_fetch_scope
Long-lived StartFetching guard (nullptr unless corresponding level is a CoinsViewOverlay).
uint32_t current_height
Current height in the simulation.
CacheLevel sim_caches[MAX_CACHES+1]
Simulated cache data (sim_caches[0] matches bottom, sim_caches[i+1] matches caches[i]).
static const PrecomputedData data
Precomputed COutPoint and CCoins values.
auto flush
Flush changes in top cache to the one below.
LIMITED_WHILE(provider.remaining_bytes(), 10000)
CoinsViewBottom bottom
Dummy coinsview instance (base of the hierarchy).
auto lookup
Helper lookup function in the simulated cache stack.
FuzzedDataProvider provider(buffer.data(), buffer.size())
static CTransactionRef MakeTransactionRef(Tx &&txIn)
A mutable version of CTransaction.
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
SeedRandomStateForTest(SeedRand::ZEROS)
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
@ ZEROS
Seed with a compile time constant of zeros.