Bitcoin Core  22.99.0
P2P Digital Currency
validation_chainstate_tests.cpp
Go to the documentation of this file.
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>
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 BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
21 {
22  ChainstateManager manager;
23  WITH_LOCK(::cs_main, manager.m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(1 << 20, true));
24  CTxMemPool mempool;
25 
27  auto add_coin = [](CCoinsViewCache& coins_view) -> COutPoint {
28  Coin newcoin;
29  uint256 txid = InsecureRand256();
30  COutPoint outp{txid, 0};
31  newcoin.nHeight = 1;
32  newcoin.out.nValue = InsecureRand32();
33  newcoin.out.scriptPubKey.assign((uint32_t)56, 1);
34  coins_view.AddCoin(outp, std::move(newcoin), false);
35 
36  return outp;
37  };
38 
39  CChainState& c1 = WITH_LOCK(cs_main, return manager.InitializeChainstate(&mempool));
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.
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 
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
Test resizing coins-related CChainState caches during runtime.
Definition: validation_chainstate_tests.cpp:20
setup_common.h
sync.h
CTxMemPool
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:478
uint256.h
InsecureRand256
static uint256 InsecureRand256()
Definition: setup_common.h:66
validation.h
add_coin
static void add_coin(const CAmount &nValue, int nInput, std::vector< OutputGroup > &set)
Definition: coin_selection.cpp:74
WITH_LOCK
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:270
InsecureRand32
static uint32_t InsecureRand32()
Definition: setup_common.h:65
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
Coin::out
CTxOut out
unspent transaction output
Definition: coins.h:34
CTxOut::nValue
CAmount nValue
Definition: transaction.h:131
random.h
Coin
A UTXO entry.
Definition: coins.h:30
Coin::nHeight
uint32_t nHeight
at which height this containing transaction was included in the active block chain
Definition: coins.h:40
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:132
CCoinsViewCache::HaveCoinInCache
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
Definition: coins.cpp:151
uint256
256-bit opaque blob.
Definition: uint256.h:124
CChainState
CChainState stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:556
ChainstateManager
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:858
prevector::assign
void assign(size_type n, const T &val)
Definition: prevector.h:218
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:116
CCoinsViewCache
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:213
CCoinsViewCache::SetBestBlock
void SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:162
LOCK
#define LOCK(cs)
Definition: sync.h:226
CChainState::InitCoinsDB
void InitCoinsDB(size_t cache_size_bytes, bool in_memory, bool should_wipe, std::string leveldb_name="chainstate")
Initialize the CoinsViews UTXO set database management data structures.
Definition: validation.cpp:1099
TestingSetup
Testing setup that configures a complete environment.
Definition: setup_common.h:99
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
CChainState::CoinsTip
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: validation.h:644
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17