Bitcoin Core 31.99.0
P2P Digital Currency
coinstatsindex_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2020-present 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 <chain.h>
7#include <interfaces/chain.h>
8#include <kernel/coinstats.h>
9#include <key.h>
10#include <primitives/block.h>
12#include <script/script.h>
13#include <sync.h>
15#include <util/check.h>
16#include <validation.h>
17
18#include <boost/test/unit_test.hpp>
19
20#include <memory>
21#include <optional>
22#include <span>
23#include <vector>
24
25BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)
26
27BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
28{
29 CoinStatsIndex coin_stats_index{interfaces::MakeChain(m_node), 1_MiB, true};
30 BOOST_REQUIRE(coin_stats_index.Init());
31
32 const CBlockIndex* block_index;
33 {
35 block_index = m_node.chainman->ActiveChain().Tip();
36 }
37
38 // CoinStatsIndex should not be found before it is started.
39 BOOST_CHECK(!coin_stats_index.LookUpStats(*block_index));
40
41 // BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
42 // is started.
43 BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
44
45 coin_stats_index.Sync();
46
47 // Check that CoinStatsIndex works for genesis block.
48 const CBlockIndex* genesis_block_index;
49 {
51 genesis_block_index = m_node.chainman->ActiveChain().Genesis();
52 }
53 BOOST_CHECK(coin_stats_index.LookUpStats(*genesis_block_index));
54
55 // Check that CoinStatsIndex updates with new blocks.
56 BOOST_CHECK(coin_stats_index.LookUpStats(*block_index));
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 const CBlockIndex* new_block_index;
66 {
68 new_block_index = m_node.chainman->ActiveChain().Tip();
69 }
70 BOOST_CHECK(coin_stats_index.LookUpStats(*new_block_index));
71
72 BOOST_CHECK(block_index != new_block_index);
73
74 // Shutdown sequence (c.f. Shutdown() in init.cpp)
75 coin_stats_index.Stop();
76}
77
node::NodeContext m_node
Definition: bitcoin-gui.cpp:47
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:94
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)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
BOOST_AUTO_TEST_SUITE_END()
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
#define BOOST_CHECK(expr)
Definition: object.cpp:16
@ OP_CHECKSIG
Definition: script.h:191
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:68
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:139
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:76
#define LOCK(cs)
Definition: sync.h:268