| 21 | //! Test resizing coins-related CChainState caches during runtime. |
| 22 | //! |
| 23 | BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches) |
| 24 | { |
| 25 | ChainstateManager manager; |
| 26 | WITH_LOCK(::cs_main, manager.m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(1 << 20, true)); |
| 27 | CTxMemPool mempool; |
| 28 | |
| 29 | //! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view. |
| 30 | auto add_coin = [](CCoinsViewCache& coins_view) -> COutPoint { |
| 31 | Coin newcoin; |
| 32 | uint256 txid = InsecureRand256(); |
| 33 | COutPoint outp{txid, 0}; |
| 34 | newcoin.nHeight = 1; |
| 35 | newcoin.out.nValue = InsecureRand32(); |
| 36 | newcoin.out.scriptPubKey.assign((uint32_t)56, 1); |
| 37 | coins_view.AddCoin(outp, std::move(newcoin), false); |
| 38 | |
| 39 | return outp; |
| 40 | }; |
| 41 | |
| 42 | CChainState& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool)); |
| 43 | c1.InitCoinsDB( |
| 44 | /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false); |
| 45 | WITH_LOCK(::cs_main, c1.InitCoinsCache(1 << 23)); |
| 46 | BOOST_REQUIRE(c1.LoadGenesisBlock()); // Need at least one block loaded to be able to flush caches |
| 47 | |
| 48 | // Add a coin to the in-memory cache, upsize once, then downsize. |
| 49 | { |
| 50 | LOCK(::cs_main); |
| 51 | auto outpoint = add_coin(c1.CoinsTip()); |
| 52 | |
| 53 | // Set a meaningless bestblock value in the coinsview cache - otherwise we won't |
| 54 | // flush during ResizecoinsCaches() and will subsequently hit an assertion. |
| 55 | c1.CoinsTip().SetBestBlock(InsecureRand256()); |
| 56 | |
| 57 | BOOST_CHECK(c1.CoinsTip().HaveCoinInCache(outpoint)); |
| 58 | |
| 59 | c1.ResizeCoinsCaches( |
| 60 | 1 << 24, // upsizing the coinsview cache |
| 61 | 1 << 22 // downsizing the coinsdb cache |
| 62 | ); |
| 63 | |
| 64 | // View should still have the coin cached, since we haven't destructed the cache on upsize. |
| 65 | BOOST_CHECK(c1.CoinsTip().HaveCoinInCache(outpoint)); |
| 66 | |
| 67 | c1.ResizeCoinsCaches( |
| 68 | 1 << 22, // downsizing the coinsview cache |
| 69 | 1 << 23 // upsizing the coinsdb cache |
| 70 | ); |
| 71 | |
| 72 | // The view cache should be empty since we had to destruct to downsize. |
| 73 | BOOST_CHECK(!c1.CoinsTip().HaveCoinInCache(outpoint)); |
| 74 | } |
| 75 | |
| 76 | // Avoid triggering the address sanitizer. |
| 77 | WITH_LOCK(::cs_main, manager.Unload()); |
| 78 | } |
| 79 | |
| 80 | //! Test UpdateTip behavior for both active and background chainstates. |
nothing calls this directly
no test coverage detected