22#include <boost/test/unit_test.hpp>
44 std::map<COutPoint, Coin> map_;
51 if (
auto it{map_.find(outpoint)}; it != map_.end() && !it->second.IsSpent())
return it->second;
60 if (it->second.IsDirty()) {
62 map_[it->first] = it->second.coin;
63 if (it->second.coin.IsSpent() && m_rng.
randrange(3) == 0) {
65 map_.erase(it->first);
70 hashBestBlock_ = hashBlock;
84 for (
const auto& entry : cacheCoins) {
85 ret += entry.second.coin.DynamicMemoryUsage();
122 bool removed_all_caches =
false;
123 bool reached_4_caches =
false;
124 bool added_an_entry =
false;
125 bool added_an_unspendable_entry =
false;
126 bool removed_an_entry =
false;
127 bool updated_an_entry =
false;
128 bool found_an_entry =
false;
129 bool missed_an_entry =
false;
130 bool uncached_an_entry =
false;
131 bool flushed_without_erase =
false;
134 std::map<COutPoint, Coin> result;
137 std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack;
138 stack.push_back(std::make_unique<CCoinsViewCacheTest>(base));
141 std::vector<Txid> txids;
143 for (
unsigned int i = 0; i < txids.size(); i++) {
159 bool result_havecoin = test_havecoin_before ? stack.back()->HaveCoin(
COutPoint(txid, 0)) :
false;
167 if (test_havecoin_before) {
171 if (test_havecoin_after) {
185 added_an_unspendable_entry =
true;
189 (coin.
IsSpent() ? added_an_entry : updated_an_entry) =
true;
193 stack.back()->AddCoin(
COutPoint(txid, 0), std::move(newcoin), is_overwrite);
196 removed_an_entry =
true;
206 stack[cacheid]->Uncache(
out);
207 uncached_an_entry |= !stack[cacheid]->HaveCoinInCache(
out);
212 for (
const auto& entry : result) {
213 bool have = stack.back()->HaveCoin(entry.first);
214 const Coin& coin = stack.back()->AccessCoin(entry.first);
218 missed_an_entry =
true;
220 BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first));
221 found_an_entry =
true;
224 for (
const auto& test : stack) {
233 if (fake_best_block) stack[flushIndex]->SetBestBlock(
m_rng.
rand256());
235 should_erase ? stack[flushIndex]->Flush() : stack[flushIndex]->Sync();
236 flushed_without_erase |= !should_erase;
243 if (fake_best_block) stack.back()->SetBestBlock(
m_rng.
rand256());
245 should_erase ? stack.back()->Flush() : stack.back()->Sync();
246 flushed_without_erase |= !should_erase;
249 if (stack.size() == 0 || (stack.size() < 4 &&
m_rng.
randbool())) {
252 if (stack.size() > 0) {
253 tip = stack.back().get();
255 removed_all_caches =
true;
257 stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
258 if (stack.size() == 4) {
259 reached_4_caches =
true;
284 CCoinsViewTest base{m_rng};
285 SimulationTest(&base,
false);
294 CCoinsViewDB db_base{{.path =
"test", .cache_bytes = 1 << 23, .memory_only =
true}, {}};
295 SimulationTest(&db_base,
true);
304typedef std::map<COutPoint, std::tuple<CTransaction,CTxUndo,Coin>>
UtxoData;
310 if (utxoSetIt == utxoSet.end()) {
311 utxoSetIt = utxoSet.begin();
313 auto utxoDataIt = utxoData.find(*utxoSetIt);
314 assert(utxoDataIt != utxoData.end());
329 bool spent_a_duplicate_coinbase =
false;
331 std::map<COutPoint, Coin> result;
334 CCoinsViewTest base{m_rng};
335 std::vector<std::unique_ptr<CCoinsViewCacheTest>> stack;
336 stack.push_back(std::make_unique<CCoinsViewCacheTest>(&base));
339 std::set<COutPoint> coinbase_coins;
340 std::set<COutPoint> disconnected_coins;
341 std::set<COutPoint> duplicate_coins;
342 std::set<COutPoint> utxoset;
345 uint32_t randiter = m_rng.
rand32();
348 if (randiter % 20 < 19) {
352 tx.
vout[0].nValue = i;
353 tx.
vout[0].scriptPubKey.assign(m_rng.
rand32() & 0x3F, 0);
354 const int height{int(m_rng.
rand32() >> 1)};
358 if (randiter % 20 < 2 || coinbase_coins.size() < 10) {
360 if (m_rng.
randrange(10) == 0 && coinbase_coins.size()) {
361 auto utxod = FindRandomFrom(coinbase_coins);
365 disconnected_coins.erase(utxod->first);
367 duplicate_coins.insert(utxod->first);
380 if (randiter % 20 == 2 && disconnected_coins.size()) {
381 auto utxod = FindRandomFrom(disconnected_coins);
383 prevout = tx.
vin[0].prevout;
384 if (!
CTransaction(tx).IsCoinBase() && !utxoset.contains(prevout)) {
385 disconnected_coins.erase(utxod->first);
390 if (utxoset.contains(utxod->first)) {
392 assert(duplicate_coins.contains(utxod->first));
394 disconnected_coins.erase(utxod->first);
399 auto utxod = FindRandomFrom(utxoset);
400 prevout = utxod->first;
403 tx.
vin[0].prevout = prevout;
407 old_coin = result[prevout];
409 result[prevout].
Clear();
411 utxoset.erase(prevout);
415 if (duplicate_coins.contains(prevout)) {
416 spent_a_duplicate_coinbase =
true;
430 utxoset.insert(outpoint);
433 utxoData.emplace(outpoint, std::make_tuple(tx,undo,old_coin));
434 }
else if (utxoset.size()) {
436 auto utxod = FindRandomFrom(utxoset);
439 CTxUndo &undo = std::get<1>(utxod->second);
440 Coin &orig_coin = std::get<2>(utxod->second);
444 result[utxod->first].Clear();
447 result[tx.
vin[0].prevout] = orig_coin;
453 BOOST_CHECK(stack.back()->SpendCoin(utxod->first));
461 disconnected_coins.insert(utxod->first);
464 utxoset.erase(utxod->first);
466 utxoset.insert(tx.
vin[0].prevout);
471 for (
const auto& entry : result) {
472 bool have = stack.back()->HaveCoin(entry.first);
473 const Coin& coin = stack.back()->AccessCoin(entry.first);
480 if (utxoset.size() > 1 && m_rng.
randrange(30) == 0) {
481 stack[m_rng.
rand32() % stack.size()]->Uncache(FindRandomFrom(utxoset)->first);
483 if (disconnected_coins.size() > 1 && m_rng.
randrange(30) == 0) {
484 stack[m_rng.
rand32() % stack.size()]->Uncache(FindRandomFrom(disconnected_coins)->first);
486 if (duplicate_coins.size() > 1 && m_rng.
randrange(30) == 0) {
487 stack[m_rng.
rand32() % stack.size()]->Uncache(FindRandomFrom(duplicate_coins)->first);
492 if (stack.size() > 1 && m_rng.
randbool() == 0) {
493 unsigned int flushIndex = m_rng.
randrange(stack.size() - 1);
494 stack[flushIndex]->Flush();
499 if (stack.size() > 0 && m_rng.
randbool() == 0) {
500 stack.back()->Flush();
503 if (stack.size() == 0 || (stack.size() < 4 && m_rng.
randbool())) {
505 if (stack.size() > 0) {
506 tip = stack.back().get();
508 stack.push_back(std::make_unique<CCoinsViewCacheTest>(tip));
520 DataStream ss1{
"97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"_hex};
529 DataStream ss2{
"8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"_hex};
551 BOOST_CHECK_MESSAGE(
false,
"We should have thrown");
552 }
catch (
const std::ios_base::failure&) {
557 uint64_t x = 3000000000ULL;
564 BOOST_CHECK_MESSAGE(
false,
"We should have thrown");
565 }
catch (
const std::ios_base::failure&) {
577 enum class State { CLEAN, DIRTY, FRESH, DIRTY_FRESH };
585 friend std::ostream&
operator<<(std::ostream& os,
const CoinEntry& e) {
return os << e.value <<
", " << e.state; }
591 static constexpr State ToState(
const bool is_dirty,
const bool is_fresh) {
618constexpr auto EX_OVERWRITE_UNSPENT{
"Attempted to overwrite an unspent coin (when possible_overwrite is false)"};
626 if (value !=
SPENT) {
637 auto [iter, inserted] = map.emplace(
OUTPOINT, std::move(entry));
641 return iter->second.coin.DynamicMemoryUsage();
646 if (
auto it{map.find(outp)}; it != map.end()) {
648 it->second.coin.IsSpent() ?
SPENT : it->second.coin.out.nValue,
657 sentinel.second.SelfRef(sentinel);
659 CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
685 test.cache.SelfTest(
false);
715 test.cache.SelfTest();
744 bool possible_overwrite{coinbase};
746 if (
auto* expected_coin{std::get_if<MaybeCoin>(&expected)}) {
748 test.cache.SelfTest();
751 BOOST_CHECK_EXCEPTION(
add_coin(), std::logic_error,
HasReason(std::get<std::string>(expected)));
791 if (
auto* expected_coin{std::get_if<MaybeCoin>(&expected)}) {
793 test.cache.SelfTest(
false);
796 BOOST_CHECK_EXCEPTION(write_coins(), std::logic_error,
HasReason(std::get<std::string>(expected)));
895 CCoinsViewCacheTest* view,
897 std::vector<std::unique_ptr<CCoinsViewCacheTest>>& all_caches,
898 bool do_erasing_flush)
903 auto flush_all = [
this, &all_caches](
bool erase) {
905 for (
auto i = all_caches.rbegin(); i != all_caches.rend(); ++i) {
907 cache->SanityCheck();
911 erase ? cache->Flush() : cache->Sync();
924 view->AddCoin(outp,
Coin(coin),
false);
926 cache_usage = view->DynamicMemoryUsage();
927 cache_size = view->map().size();
951 if (do_erasing_flush) {
957 BOOST_TEST(view->DynamicMemoryUsage() <= cache_usage);
959 BOOST_TEST(view->map().size() < cache_size);
964 view->AccessCoin(outp);
971 view->AddCoin(outp,
Coin(coin),
false),
1002 all_caches[0]->AddCoin(outp, std::move(coin),
false);
1003 all_caches[0]->Sync();
1006 BOOST_CHECK(!all_caches[1]->HaveCoinInCache(outp));
1027 all_caches[0]->AddCoin(outp, std::move(coin),
false);
1035 all_caches[0]->Sync();
1039 BOOST_CHECK(!all_caches[0]->HaveCoinInCache(outp));
1047 CCoinsViewDB base{{.path =
"test", .cache_bytes = 1 << 23, .memory_only =
true}, {}};
1048 std::vector<std::unique_ptr<CCoinsViewCacheTest>> caches;
1049 caches.push_back(std::make_unique<CCoinsViewCacheTest>(&base));
1050 caches.push_back(std::make_unique<CCoinsViewCacheTest>(caches.back().get()));
1052 for (
const auto& view : caches) {
1053 TestFlushBehavior(view.get(), base, caches,
false);
1054 TestFlushBehavior(view.get(), base, caches,
true);
1064 CCoinsMap map{0, CCoinsMap::hasher{}, CCoinsMap::key_equal{}, &resource};
1073 for (
size_t i = 0; i < 1000; ++i) {
1086 CCoinsViewCacheTest cache{&root};
1091 cache.AddCoin(outpoint,
Coin{coin1},
false);
1104 CCoinsViewCacheTest cache{&root};
1109 cache.EmplaceCoinInternalDANGER(
COutPoint{outpoint},
Coin{coin1});
1113 cache.EmplaceCoinInternalDANGER(
COutPoint{outpoint},
Coin{coin2});
1121 CCoinsViewTest root{m_rng};
1124 root_cache.SetBestBlock(base_best_block);
1132 cache.EmplaceCoinInternalDANGER(
COutPoint{outpoint},
Coin{coin});
1135 cache.SetBestBlock(cache_best_block);
1138 const auto reset_guard{cache.CreateResetGuard()};
1140 BOOST_CHECK(!cache.AccessCoin(outpoint).IsSpent());
1143 BOOST_CHECK(!root_cache.HaveCoinInCache(outpoint));
1146 BOOST_CHECK(cache.AccessCoin(outpoint).IsSpent());
1149 BOOST_CHECK(!root_cache.HaveCoinInCache(outpoint));
1153 const auto reset_guard{cache.CreateResetGuard()};
1156 BOOST_CHECK(cache.AccessCoin(outpoint).IsSpent());
1159 BOOST_CHECK(!root_cache.HaveCoinInCache(outpoint));
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
int64_t CAmount
Amount in satoshis (Can be negative)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
CoinsCachePair m_sentinel
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
void SanityCheck() const
Run an internal sanity check on the cache data structure. */.
CCoinsView backed by the coin database (chainstate/)
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Abstract view on the open txout dataset.
virtual std::optional< Coin > GetCoin(const COutPoint &outpoint) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
virtual void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock)
Do a bulk modification (multiple Coin changes + BestBlock change).
virtual uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
An outpoint - a combination of a transaction hash and an index n into its vout.
Serialized script, used inside transaction inputs and outputs.
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxIn > vin
An output of a transaction.
Undo information for a CTransaction.
std::vector< Coin > vprevout
CTxOut out
unspent transaction output
bool IsSpent() const
Either this coin never existed (see e.g.
uint32_t nHeight
at which height this containing transaction was included in the active block chain
unsigned int fCoinBase
whether containing transaction was a coinbase
Double ended buffer combining vector and stream-like interfaces.
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
static void CheckAllDataAccountedFor(const PoolResource< MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES > &resource)
Once all blocks are given back to the resource, tests that the freelists are consistent:
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
uint256 rand256() noexcept
generate a random uint256.
bool randbool() noexcept
Generate a random boolean.
std::vector< B > randbytes(size_t len) noexcept
Generate random bytes.
uint32_t rand32() noexcept
Generate a random 32-bit integer.
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
CCoinsViewCacheTest cache
SingleEntryCacheTest(const CAmount base_value, const MaybeCoin &cache_coin)
constexpr bool IsNull() const
static constexpr unsigned int STATIC_SIZE
void assign(size_type n, const T &val)
static transaction_identifier FromUint256(const uint256 &id)
static void add_coin(const CAmount &nValue, int nInput, std::vector< OutputGroup > &set)
const Coin & AccessByTxid(const CCoinsViewCache &view, const Txid &txid)
Utility function to find any unspent output with a given txid.
std::pair< const COutPoint, CCoinsCacheEntry > CoinsCachePair
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher, std::equal_to< COutPoint >, PoolAllocator< CoinsCachePair, sizeof(CoinsCachePair)+sizeof(void *) *4 > > CCoinsMap
PoolAllocator's MAX_BLOCK_SIZE_BYTES parameter here uses sizeof the data, and adds the size of 4 poin...
CCoinsMap::allocator_type::ResourceType CCoinsMapMemoryResource
BOOST_AUTO_TEST_CASE(ccoins_serialization)
constexpr MaybeCoin VALUE2_DIRTY
std::optional< CoinEntry > MaybeCoin
constexpr MaybeCoin VALUE2_CLEAN
constexpr MaybeCoin MISSING
static MaybeCoin GetCoinsMapEntry(const CCoinsMap &map, const COutPoint &outp=OUTPOINT)
static const COutPoint OUTPOINT
constexpr MaybeCoin VALUE2_DIRTY_FRESH
static void WriteCoinsViewEntry(CCoinsView &view, const MaybeCoin &cache_coin)
static void CheckWriteCoins(const MaybeCoin &parent, const MaybeCoin &child, const CoinOrError &expected)
int ApplyTxInUndo(Coin &&undo, CCoinsViewCache &view, const COutPoint &out)
Restore the UTXO in a Coin at a given COutPoint.
static const unsigned int NUM_SIMULATION_ITERATIONS
constexpr MaybeCoin VALUE1_CLEAN
constexpr MaybeCoin SPENT_DIRTY_FRESH
constexpr MaybeCoin SPENT_CLEAN
constexpr auto EX_OVERWRITE_UNSPENT
constexpr MaybeCoin VALUE1_DIRTY
constexpr MaybeCoin VALUE1_FRESH
static size_t InsertCoinsMapEntry(CCoinsMap &map, CoinsCachePair &sentinel, const CoinEntry &cache_coin)
static void CheckSpendCoins(const CAmount base_value, const MaybeCoin &cache_coin, const MaybeCoin &expected)
void UpdateCoins(const CTransaction &tx, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
constexpr MaybeCoin VALUE2_FRESH
constexpr MaybeCoin VALUE3_DIRTY_FRESH
static void CheckAccessCoin(const CAmount base_value, const MaybeCoin &cache_coin, const MaybeCoin &expected)
constexpr MaybeCoin VALUE3_DIRTY
constexpr MaybeCoin VALUE1_DIRTY_FRESH
constexpr MaybeCoin SPENT_DIRTY
constexpr auto EX_FRESH_MISAPPLIED
std::variant< MaybeCoin, std::string > CoinOrError
static void SetCoinsValue(const CAmount value, Coin &coin)
BOOST_FIXTURE_TEST_CASE(coins_cache_base_simulation_test, CacheTest)
constexpr MaybeCoin SPENT_FRESH
static void CheckAddCoin(const CAmount base_value, const MaybeCoin &cache_coin, const CAmount modify_value, const CoinOrError &expected, const bool coinbase)
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
static bool sanity_check(const std::vector< CTransactionRef > &transactions, const std::map< COutPoint, CAmount > &bumpfees)
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
bool operator==(const CNetAddr &a, const CNetAddr &b)
#define BOOST_CHECK_THROW(stmt, excMatch)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
A Coin in one level of the coins database caching hierarchy.
static void SetFresh(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
static void SetDirty(CoinsCachePair &pair, CoinsCachePair &sentinel) noexcept
A mutable version of CTransaction.
std::vector< CTxOut > vout
Txid GetHash() const
Compute the hash of this CMutableTransaction.
void SimulationTest(CCoinsView *base, bool fake_best_block)
constexpr bool IsDirty() const
friend std::ostream & operator<<(std::ostream &os, const CoinEntry &e)
bool operator==(const CoinEntry &o) const =default
constexpr bool IsDirtyFresh() const
static constexpr State ToState(const bool is_dirty, const bool is_fresh)
constexpr bool IsFresh() const
constexpr CoinEntry(const CAmount v, const State s)
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
CoinsCachePair * NextAndMaybeErase(CoinsCachePair ¤t) noexcept
Return the next entry after current, possibly erasing current.
CoinsCachePair * Begin() const noexcept
CoinsCachePair * End() const noexcept
void TestFlushBehavior(CCoinsViewCacheTest *view, CCoinsViewDB &base, std::vector< std::unique_ptr< CCoinsViewCacheTest > > &all_caches, bool do_erasing_flush)
For CCoinsViewCache instances backed by either another cache instance or leveldb, test cache behavior...
std::map< COutPoint, std::tuple< CTransaction, CTxUndo, Coin > > UtxoData
UtxoData::iterator FindRandomFrom(const std::set< COutPoint > &utxoSet)
@ ZEROS
Seed with a compile time constant of zeros.
CAmount RandMoney(Rng &&rng)