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