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
7#include <util/time.h>
8#include <validation.h>
9
10#include <boost/test/unit_test.hpp>
11
12#include <chrono>
13
14
15BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)
16
17BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
18{
19 CoinStatsIndex coin_stats_index{1 << 20, true};
20
22 const CBlockIndex* block_index;
23 {
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>());
43 }
44
45 // Check that CoinStatsIndex works for genesis block.
46 const CBlockIndex* genesis_block_index;
47 {
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
64 const CBlockIndex* new_block_index;
65 {
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
NodeContext m_node
Definition: bitcoin-gui.cpp:36
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:118
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:146
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
CoinStatsIndex maintains statistics on the UTXO set.
BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK(expr)
Definition: object.cpp:17
@ OP_CHECKSIG
Definition: script.h:183
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:60
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:47
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:116
#define LOCK(cs)
Definition: sync.h:226
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:22