|
| 1 | +// Copyright (c) 2020 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 <random.h> |
| 6 | +#include <uint256.h> |
| 7 | +#include <consensus/validation.h> |
| 8 | +#include <sync.h> |
| 9 | +#include <test/util/setup_common.h> |
| 10 | +#include <validation.h> |
| 11 | + |
| 12 | +#include <vector> |
| 13 | + |
| 14 | +#include <boost/test/unit_test.hpp> |
| 15 | + |
| 16 | +BOOST_FIXTURE_TEST_SUITE(validation_chainstate_tests, TestingSetup) |
| 17 | + |
| 18 | +//! Test resizing coins-related CChainState caches during runtime. |
| 19 | +//! |
| 20 | +BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches) |
| 21 | +{ |
| 22 | + ChainstateManager manager; |
| 23 | + |
| 24 | + //! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view. |
| 25 | + auto add_coin = [](CCoinsViewCache& coins_view) -> COutPoint { |
| 26 | + Coin newcoin; |
| 27 | + uint256 txid = InsecureRand256(); |
| 28 | + COutPoint outp{txid, 0}; |
| 29 | + newcoin.nHeight = 1; |
| 30 | + newcoin.out.nValue = InsecureRand32(); |
| 31 | + newcoin.out.scriptPubKey.assign((uint32_t)56, 1); |
| 32 | + coins_view.AddCoin(outp, std::move(newcoin), false); |
| 33 | + |
| 34 | + return outp; |
| 35 | + }; |
| 36 | + |
| 37 | + ENTER_CRITICAL_SECTION(cs_main); |
| 38 | + CChainState& c1 = manager.InitializeChainstate(); |
| 39 | + LEAVE_CRITICAL_SECTION(cs_main); |
| 40 | + c1.InitCoinsDB( |
| 41 | + /* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false); |
| 42 | + WITH_LOCK(::cs_main, c1.InitCoinsCache(1 << 23)); |
| 43 | + |
| 44 | + // Add a coin to the in-memory cache, upsize once, then downsize. |
| 45 | + { |
| 46 | + LOCK(::cs_main); |
| 47 | + auto outpoint = add_coin(c1.CoinsTip()); |
| 48 | + |
| 49 | + // Set a meaningless bestblock value in the coinsview cache - otherwise we won't |
| 50 | + // flush during ResizecoinsCaches() and will subsequently hit an assertion. |
| 51 | + c1.CoinsTip().SetBestBlock(InsecureRand256()); |
| 52 | + |
| 53 | + BOOST_CHECK(c1.CoinsTip().HaveCoinInCache(outpoint)); |
| 54 | + |
| 55 | + c1.ResizeCoinsCaches( |
| 56 | + 1 << 24, // upsizing the coinsview cache |
| 57 | + 1 << 22 // downsizing the coinsdb cache |
| 58 | + ); |
| 59 | + |
| 60 | + // View should still have the coin cached, since we haven't destructed the cache on upsize. |
| 61 | + BOOST_CHECK(c1.CoinsTip().HaveCoinInCache(outpoint)); |
| 62 | + |
| 63 | + c1.ResizeCoinsCaches( |
| 64 | + 1 << 22, // downsizing the coinsview cache |
| 65 | + 1 << 23 // upsizing the coinsdb cache |
| 66 | + ); |
| 67 | + |
| 68 | + // The view cache should be empty since we had to destruct to downsize. |
| 69 | + BOOST_CHECK(!c1.CoinsTip().HaveCoinInCache(outpoint)); |
| 70 | + } |
| 71 | + |
| 72 | + // Avoid triggering the address sanitizer. |
| 73 | + WITH_LOCK(::cs_main, manager.Unload()); |
| 74 | +} |
| 75 | + |
| 76 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments