Bitcoin Core  22.99.0
P2P Digital Currency
coinstatsindex_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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 <index/coinstatsindex.h>
7 #include <util/time.h>
8 #include <validation.h>
9 
10 #include <boost/test/unit_test.hpp>
11 
12 #include <chrono>
13 
14 using node::CCoinsStats;
16 
17 BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)
18 
19 BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
20 {
21  CoinStatsIndex coin_stats_index{1 << 20, true};
22 
23  CCoinsStats coin_stats{CoinStatsHashType::MUHASH};
24  const CBlockIndex* block_index;
25  {
26  LOCK(cs_main);
27  block_index = m_node.chainman->ActiveChain().Tip();
28  }
29 
30  // CoinStatsIndex should not be found before it is started.
31  BOOST_CHECK(!coin_stats_index.LookUpStats(block_index, coin_stats));
32 
33  // BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
34  // is started.
35  BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
36 
37  BOOST_REQUIRE(coin_stats_index.Start(m_node.chainman->ActiveChainstate()));
38 
39  // Allow the CoinStatsIndex to catch up with the block index that is syncing
40  // in a background thread.
41  const auto timeout = GetTime<std::chrono::seconds>() + 120s;
42  while (!coin_stats_index.BlockUntilSyncedToCurrentChain()) {
43  BOOST_REQUIRE(timeout > GetTime<std::chrono::milliseconds>());
44  UninterruptibleSleep(100ms);
45  }
46 
47  // Check that CoinStatsIndex works for genesis block.
48  const CBlockIndex* genesis_block_index;
49  {
50  LOCK(cs_main);
51  genesis_block_index = m_node.chainman->ActiveChain().Genesis();
52  }
53  BOOST_CHECK(coin_stats_index.LookUpStats(genesis_block_index, coin_stats));
54 
55  // Check that CoinStatsIndex updates with new blocks.
56  coin_stats_index.LookUpStats(block_index, coin_stats);
57 
58  const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
59  std::vector<CMutableTransaction> noTxns;
60  CreateAndProcessBlock(noTxns, script_pub_key);
61 
62  // Let the CoinStatsIndex to catch up again.
63  BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain());
64 
65  CCoinsStats new_coin_stats{CoinStatsHashType::MUHASH};
66  const CBlockIndex* new_block_index;
67  {
68  LOCK(cs_main);
69  new_block_index = m_node.chainman->ActiveChain().Tip();
70  }
71  coin_stats_index.LookUpStats(new_block_index, new_coin_stats);
72 
73  BOOST_CHECK(block_index != new_block_index);
74 
75  // Shutdown sequence (c.f. Shutdown() in init.cpp)
76  coin_stats_index.Stop();
77 
78  // Rest of shutdown sequence and destructors happen in ~TestingSetup()
79 }
80 
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
node::CCoinsStats
Definition: coinstats.h:30
setup_common.h
m_node
node::NodeContext m_node
Definition: bitcoin-gui.cpp:36
coinstatsindex.h
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
BOOST_FIXTURE_TEST_CASE
BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
Definition: coinstatsindex_tests.cpp:19
ToByteVector
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:60
time.h
node::CoinStatsHashType
CoinStatsHashType
Definition: coinstats.h:24
TestChain100Setup
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:124
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
UninterruptibleSleep
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:22
LOCK
#define LOCK(cs)
Definition: sync.h:226
node::NodeContext::chainman
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:48
CoinStatsIndex
CoinStatsIndex maintains statistics on the UTXO set.
Definition: coinstatsindex.h:17
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:138
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:151
OP_CHECKSIG
@ OP_CHECKSIG
Definition: script.h:183
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17