Bitcoin Core 29.99.0
P2P Digital Currency
coinstatsindex_tests.cpp
Go to the documentation of this file.
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>
7#include <interfaces/chain.h>
8#include <kernel/coinstats.h>
11#include <validation.h>
12
13#include <boost/test/unit_test.hpp>
14
15BOOST_AUTO_TEST_SUITE(coinstatsindex_tests)
16
17BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
18{
19 CoinStatsIndex coin_stats_index{interfaces::MakeChain(m_node), 1 << 20, true};
20 BOOST_REQUIRE(coin_stats_index.Init());
21
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));
30
31 // BlockUntilSyncedToCurrentChain should return false before CoinStatsIndex
32 // is started.
33 BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
34
35 coin_stats_index.Sync();
36
37 // Check that CoinStatsIndex works for genesis block.
38 const CBlockIndex* genesis_block_index;
39 {
41 genesis_block_index = m_node.chainman->ActiveChain().Genesis();
42 }
43 BOOST_CHECK(coin_stats_index.LookUpStats(*genesis_block_index));
44
45 // Check that CoinStatsIndex updates with new blocks.
46 BOOST_CHECK(coin_stats_index.LookUpStats(*block_index));
47
48 const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
49 std::vector<CMutableTransaction> noTxns;
50 CreateAndProcessBlock(noTxns, script_pub_key);
51
52 // Let the CoinStatsIndex to catch up again.
53 BOOST_CHECK(coin_stats_index.BlockUntilSyncedToCurrentChain());
54
55 const CBlockIndex* new_block_index;
56 {
58 new_block_index = m_node.chainman->ActiveChain().Tip();
59 }
60 BOOST_CHECK(coin_stats_index.LookUpStats(*new_block_index));
61
62 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 m_node.validation_signals->SyncWithValidationInterfaceQueue();
71
72 // Shutdown sequence (c.f. Shutdown() in init.cpp)
73 coin_stats_index.Stop();
74}
75
76// Test shutdown between BlockConnected and ChainStateFlushed notifications,
77// make sure index is not corrupted and is able to reload.
78BOOST_FIXTURE_TEST_CASE(coinstatsindex_unclean_shutdown, TestChain100Setup)
79{
80 Chainstate& chainstate = Assert(m_node.chainman)->ActiveChainstate();
81 const CChainParams& params = Params();
82 {
84 BOOST_REQUIRE(index.Init());
85 index.Sync();
86 std::shared_ptr<const CBlock> new_block;
87 CBlockIndex* new_block_index = nullptr;
88 {
89 const CScript script_pub_key{CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
90 const CBlock block = this->CreateBlock({}, script_pub_key, chainstate);
91
92 new_block = std::make_shared<CBlock>(block);
93
96 BOOST_CHECK(CheckBlock(block, state, params.GetConsensus()));
97 BOOST_CHECK(m_node.chainman->AcceptBlock(new_block, state, &new_block_index, true, nullptr, nullptr, true));
98 CCoinsViewCache view(&chainstate.CoinsTip());
99 BOOST_CHECK(chainstate.ConnectBlock(block, state, new_block_index, view));
100 }
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 ValidationInterfaceTest::BlockConnected(ChainstateRole::NORMAL, index, new_block, new_block_index);
105 index.Stop();
106 }
107
108 {
110 BOOST_REQUIRE(index.Init());
111 // Make sure the index can be loaded.
112 BOOST_REQUIRE(index.StartBackgroundSync());
113 index.Stop();
114 }
115}
116
node::NodeContext m_node
Definition: bitcoin-gui.cpp:42
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
Definition: check.h:106
Definition: block.h:69
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:141
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:69
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:81
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:363
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:521
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:627
bool ActivateBestChain(BlockValidationState &state, std::shared_ptr< const CBlock > pblock=nullptr) LOCKS_EXCLUDED(DisconnectResult DisconnectBlock(const CBlock &block, const CBlockIndex *pindex, CCoinsViewCache &view) EXCLUSIVE_LOCKS_REQUIRED(boo ConnectBlock)(const CBlock &block, BlockValidationState &state, CBlockIndex *pindex, CCoinsViewCache &view, bool fJustCheck=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Find the best known block, and make it the tip of the block chain.
Definition: validation.h:725
CoinStatsIndex maintains statistics on the UTXO set.
static void BlockConnected(ChainstateRole role, CValidationInterface &obj, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Definition: validation.cpp:25
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:17
@ OP_CHECKSIG
Definition: script.h:190
std::vector< unsigned char > ToByteVector(const T &in)
Definition: script.h:67
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:146
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
Definition: context.h:88
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:72
#define LOCK(cs)
Definition: sync.h:265
bool CheckBlock(const CBlock &block, BlockValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.