Branch data Line data Source code
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>
6 : : #include <index/coinstatsindex.h>
7 : : #include <interfaces/chain.h>
8 : : #include <kernel/coinstats.h>
9 : : #include <key.h>
10 : : #include <primitives/block.h>
11 : : #include <primitives/transaction.h>
12 : : #include <script/script.h>
13 : : #include <sync.h>
14 : : #include <test/util/setup_common.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 : :
25 : : BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)
26 : :
27 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
28 : : {
29 [ + - ]: 1 : CoinStatsIndex coin_stats_index{interfaces::MakeChain(m_node), 1_MiB, true};
30 [ + - + - : 2 : BOOST_REQUIRE(coin_stats_index.Init());
+ - + - ]
31 : :
32 : 1 : const CBlockIndex* block_index;
33 : 1 : {
34 [ + - ]: 1 : LOCK(cs_main);
35 [ + - - + : 2 : block_index = m_node.chainman->ActiveChain().Tip();
+ - ]
36 : 0 : }
37 : :
38 : : // CoinStatsIndex should not be found before it is started.
39 [ + - + - : 1 : BOOST_CHECK(!coin_stats_index.LookUpStats(*block_index));
+ - ]
40 : :
41 : : // BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
42 : : // is started.
43 [ + - + - : 2 : BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
+ - + - ]
44 : :
45 [ + - ]: 1 : coin_stats_index.Sync();
46 : :
47 : : // Check that CoinStatsIndex works for genesis block.
48 : 1 : const CBlockIndex* genesis_block_index;
49 : 1 : {
50 [ + - ]: 1 : LOCK(cs_main);
51 [ + - - + : 2 : genesis_block_index = m_node.chainman->ActiveChain().Genesis();
+ - ]
52 : 0 : }
53 [ + - + - : 2 : BOOST_CHECK(coin_stats_index.LookUpStats(*genesis_block_index));
+ - + - ]
54 : :
55 : : // Check that CoinStatsIndex updates with new blocks.
56 [ + - + - : 2 : BOOST_CHECK(coin_stats_index.LookUpStats(*block_index));
+ - + - ]
57 : :
58 [ + - + - : 2 : const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
+ - ]
59 : 1 : std::vector<CMutableTransaction> noTxns;
60 [ + - ]: 1 : CreateAndProcessBlock(noTxns, script_pub_key);
61 : :
62 : : // Let the CoinStatsIndex to catch up again.
63 [ + - + - : 2 : BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain());
+ - + - ]
64 : :
65 : 1 : const CBlockIndex* new_block_index;
66 : 1 : {
67 [ + - ]: 1 : LOCK(cs_main);
68 [ + - - + : 2 : new_block_index = m_node.chainman->ActiveChain().Tip();
+ - ]
69 : 0 : }
70 [ + - + - : 2 : BOOST_CHECK(coin_stats_index.LookUpStats(*new_block_index));
+ - + - ]
71 : :
72 [ + - + - : 2 : BOOST_CHECK(block_index != new_block_index);
+ - ]
73 : :
74 : : // Shutdown sequence (c.f. Shutdown() in init.cpp)
75 [ + - ]: 1 : coin_stats_index.Stop();
76 : 1 : }
77 : :
78 : : BOOST_AUTO_TEST_SUITE_END()
|