Bitcoin Core
31.99.0
P2P Digital Currency
src
test
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
>
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
BOOST_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
{
34
LOCK
(
cs_main
);
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
{
50
LOCK
(
cs_main
);
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
{
67
LOCK
(
cs_main
);
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
78
BOOST_AUTO_TEST_SUITE_END
()
m_node
node::NodeContext m_node
Definition:
bitcoin-gui.cpp:47
block.h
check.h
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition:
chain.h:94
CScript
Serialized script, used inside transaction inputs and outputs.
Definition:
script.h:406
CoinStatsIndex
CoinStatsIndex maintains statistics on the UTXO set.
Definition:
coinstatsindex.h:31
coinstats.h
coinstatsindex.h
BOOST_FIXTURE_TEST_CASE
BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
Definition:
coinstatsindex_tests.cpp:27
cs_main
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
BOOST_AUTO_TEST_SUITE_END()
chain.h
chain.h
key.h
interfaces::MakeChain
std::unique_ptr< Chain > MakeChain(node::NodeContext &node)
Return implementation of Chain interface.
Definition:
interfaces.cpp:1088
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition:
object.cpp:16
transaction.h
script.h
OP_CHECKSIG
@ OP_CHECKSIG
Definition:
script.h:191
ToByteVector
std::vector< unsigned char > ToByteVector(const T &in)
Definition:
script.h:68
setup_common.h
TestChain100Setup
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition:
setup_common.h:139
node::NodeContext::chainman
std::unique_ptr< ChainstateManager > chainman
Definition:
context.h:76
sync.h
LOCK
#define LOCK(cs)
Definition:
sync.h:268
Generated on Mon Aug 24 2026 20:00:44 for Bitcoin Core by
1.9.4