Branch data Line data Source code
1 : : // Copyright (c) 2020-2022 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 <chainparams.h>
6 : : #include <index/coinstatsindex.h>
7 : : #include <interfaces/chain.h>
8 : : #include <kernel/coinstats.h>
9 : : #include <test/util/setup_common.h>
10 : : #include <test/util/validation.h>
11 : : #include <validation.h>
12 : :
13 : : #include <boost/test/unit_test.hpp>
14 : :
15 : : BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)
16 : :
17 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
18 : : {
19 [ + - ]: 1 : CoinStatsIndex coin_stats_index{interfaces::MakeChain(m_node), 1 << 20, true};
20 [ + - + - : 2 : BOOST_REQUIRE(coin_stats_index.Init());
+ - + - ]
21 : :
22 : 1 : const CBlockIndex* block_index;
23 : 1 : {
24 [ + - ]: 1 : LOCK(cs_main);
25 [ + - + - : 2 : block_index = m_node.chainman->ActiveChain().Tip();
+ - ]
26 : 0 : }
27 : :
28 : : // CoinStatsIndex should not be found before it is started.
29 [ + - + - : 2 : BOOST_CHECK(!coin_stats_index.LookUpStats(*block_index));
+ - + - ]
30 : :
31 : : // BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
32 : : // is started.
33 [ + - + - : 2 : BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
+ - + - ]
34 : :
35 [ + - ]: 1 : coin_stats_index.Sync();
36 : :
37 : : // Check that CoinStatsIndex works for genesis block.
38 : 1 : const CBlockIndex* genesis_block_index;
39 : 1 : {
40 [ + - ]: 1 : LOCK(cs_main);
41 [ + - + - : 2 : genesis_block_index = m_node.chainman->ActiveChain().Genesis();
+ - ]
42 : 0 : }
43 [ + - + - : 2 : BOOST_CHECK(coin_stats_index.LookUpStats(*genesis_block_index));
+ - + - ]
44 : :
45 : : // Check that CoinStatsIndex updates with new blocks.
46 [ + - + - : 2 : BOOST_CHECK(coin_stats_index.LookUpStats(*block_index));
+ - + - ]
47 : :
48 [ + - + - : 1 : const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
+ - ]
49 : 1 : std::vector<CMutableTransaction> noTxns;
50 [ + - ]: 1 : CreateAndProcessBlock(noTxns, script_pub_key);
51 : :
52 : : // Let the CoinStatsIndex to catch up again.
53 [ + - + - : 2 : BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain());
+ - + - ]
54 : :
55 : 1 : const CBlockIndex* new_block_index;
56 : 1 : {
57 [ + - ]: 1 : LOCK(cs_main);
58 [ + - + - : 2 : new_block_index = m_node.chainman->ActiveChain().Tip();
+ - ]
59 : 0 : }
60 [ + - + - : 2 : BOOST_CHECK(coin_stats_index.LookUpStats(*new_block_index));
+ - + - ]
61 : :
62 [ + - + - : 2 : BOOST_CHECK(block_index != new_block_index);
+ - ]
63 : :
64 : : // It is not safe to stop and destroy the index until it finishes handling
65 : : // the last BlockConnected notification. The BlockUntilSyncedToCurrentChain()
66 : : // call above is sufficient to ensure this, but the
67 : : // SyncWithValidationInterfaceQueue() call below is also needed to ensure
68 : : // TSAN always sees the test thread waiting for the notification thread, and
69 : : // avoid potential false positive reports.
70 [ + - ]: 1 : m_node.validation_signals->SyncWithValidationInterfaceQueue();
71 : :
72 : : // Shutdown sequence (c.f. Shutdown() in init.cpp)
73 [ + - ]: 1 : coin_stats_index.Stop();
74 : 1 : }
75 : :
76 : : // Test shutdown between BlockConnected and ChainStateFlushed notifications,
77 : : // make sure index is not corrupted and is able to reload.
78 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(coinstatsindex_unclean_shutdown, TestChain100Setup)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
79 : : {
80 : 1 : Chainstate& chainstate = Assert(m_node.chainman)->ActiveChainstate();
81 : 1 : const CChainParams& params = Params();
82 : 1 : {
83 [ + - ]: 1 : CoinStatsIndex index{interfaces::MakeChain(m_node), 1 << 20};
84 [ + - + - : 2 : BOOST_REQUIRE(index.Init());
+ - + - ]
85 [ + - ]: 1 : index.Sync();
86 : 1 : std::shared_ptr<const CBlock> new_block;
87 : 1 : CBlockIndex* new_block_index = nullptr;
88 : 1 : {
89 [ + - + - : 2 : const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
+ - ]
90 [ + - ]: 1 : const CBlock block = this->CreateBlock({}, script_pub_key, chainstate);
91 : :
92 [ + - - + ]: 1 : new_block = std::make_shared<CBlock>(block);
93 : :
94 [ + - ]: 1 : LOCK(cs_main);
95 [ + - ]: 1 : BlockValidationState state;
96 [ + - + - : 2 : BOOST_CHECK(CheckBlock(block, state, params.GetConsensus()));
+ - + - ]
97 [ + - + - : 2 : BOOST_CHECK(m_node.chainman->AcceptBlock(new_block, state, &new_block_index, true, nullptr, nullptr, true));
+ - + - ]
98 [ + - + - ]: 1 : CCoinsViewCache view(&chainstate.CoinsTip());
99 [ + - + - : 2 : BOOST_CHECK(chainstate.ConnectBlock(block, state, new_block_index, view));
+ - ]
100 [ + - ]: 3 : }
101 : : // Send block connected notification, then stop the index without
102 : : // sending a chainstate flushed notification. Prior to #24138, this
103 : : // would cause the index to be corrupted and fail to reload.
104 [ + - ]: 1 : ValidationInterfaceTest::BlockConnected(ChainstateRole::NORMAL, index, new_block, new_block_index);
105 [ + - ]: 1 : index.Stop();
106 : 1 : }
107 : :
108 : 1 : {
109 [ + - ]: 1 : CoinStatsIndex index{interfaces::MakeChain(m_node), 1 << 20};
110 [ + - + - : 2 : BOOST_REQUIRE(index.Init());
+ - + - ]
111 : : // Make sure the index can be loaded.
112 [ + - + - : 2 : BOOST_REQUIRE(index.StartBackgroundSync());
+ - + - ]
113 [ + - ]: 1 : index.Stop();
114 : 1 : }
115 : 1 : }
116 : :
117 : : BOOST_AUTO_TEST_SUITE_END()
|