Bitcoin Core 32.1.0
P2P Digital Currency
Loading...
Searching...
No Matches
validation.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
6
7#include <coins.h>
9#include <node/blockstorage.h>
10#include <node/mining_types.h>
11#include <test/util/mining.h>
12#include <test/util/script.h>
14#include <test/util/time.h>
15#include <test/util/txmempool.h>
16#include <txmempool.h>
17#include <util/check.h>
18#include <util/time.h>
19#include <validation.h>
20#include <validationinterface.h>
21
22#include <memory>
23#include <utility>
24#include <vector>
25
27
34
36{
37 struct TestChainstate : public Chainstate {
38 void ResetNextWrite() { m_next_write = NodeClock::time_point::max() - 1s; }
39 };
41 for (const auto& cs : m_chainstates) {
42 static_cast<TestChainstate&>(*cs).ResetNextWrite();
43 }
44}
45
51
58
60 const ChainstateRole& role,
62 const std::shared_ptr<const CBlock>& block,
63 const CBlockIndex* pindex)
64{
65 obj.BlockConnected(role, block, pindex);
66}
68{
69 struct TestChainstate : public Chainstate {
70 void CallInvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
71 {
72 InvalidBlockFound(pindex, state);
73 }
74 };
75
76 static_cast<TestChainstate*>(&ActiveChainstate())->CallInvalidBlockFound(pindex, state);
77}
78
80{
81 struct TestChainstate : public Chainstate {
82 void CallInvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
83 {
84 InvalidChainFound(pindexNew);
85 }
86 };
87
88 static_cast<TestChainstate*>(&ActiveChainstate())->CallInvalidChainFound(pindexNew);
89}
90
92{
93 struct TestChainstate : public Chainstate {
94 CBlockIndex* CallFindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
95 {
96 return FindMostWorkChain();
97 }
98 };
99
100 return static_cast<TestChainstate*>(&ActiveChainstate())->CallFindMostWorkChain();
101}
102
104{
105 m_best_invalid = nullptr;
106}
107
108std::vector<std::pair<COutPoint, CAmount>> ResetChainmanAndMempool(TestingSetup& setup, FakeNodeClock& node_clock)
109{
110 node_clock.set(setup.m_node.chainman->GetParams().GenesisBlock().Time());
111
112 bilingual_str error{};
113 setup.m_node.mempool.reset();
114 setup.m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(setup.m_node), error);
115 Assert(error.empty());
116
117 setup.m_node.chainman.reset();
118 setup.m_make_chainman();
120
122 options.coinbase_output_script = P2WSH_OP_TRUE;
123
124 std::vector<std::pair<COutPoint, CAmount>> mature_coinbase;
125 for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
126 COutPoint prevout{MineBlock(setup.m_node, options)};
127 if (i < COINBASE_MATURITY) {
128 LOCK(cs_main);
129 CAmount subsidy{setup.m_node.chainman->ActiveChainstate().CoinsTip().GetCoin(prevout)->out.nValue};
130 mature_coinbase.emplace_back(prevout, subsidy);
131 }
132 }
133 return mature_coinbase;
134}
int64_t CAmount
Amount in satoshis (Can be negative).
Definition amount.h:12
static void pool cs
#define Assert(val)
Identity function.
Definition check.h:116
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition chain.h:94
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition transaction.h:30
Implement this to subscribe to events generated in validation and mempool.
virtual void BlockConnected(const kernel::ChainstateRole &role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
bool IsInitialBlockDownload() const noexcept
Check whether we are doing an initial block download (synchronizing from disk or network).
Chainstate & ActiveChainstate() const
std::atomic_bool m_cached_is_ibd
Whether initial block download (IBD) is ongoing.
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition time.h:54
void set(NodeSeconds t)
Set mocktime.
Definition time.h:71
static void BlockConnected(const kernel::ChainstateRole &role, CValidationInterface &obj, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
std::set< int > m_dirty_fileinfo
Dirty block file entries.
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
std::vector< CBlockFileInfo > m_blockfile_info
constexpr int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule).
Definition consensus.h:19
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition cs_main.cpp:8
node::NodeContext m_node
std::function< void()> m_make_chainman
void LoadVerifyActivateChainstate()
void CleanupForFuzzing()
Test-only method to clear internal state for fuzzing.
void ResetBestInvalid() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void InvalidChainFound(CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
CBlockIndex * FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void DisableNextWrite()
Disable the next write of all chainstates.
void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Wrappers that avoid making chainstatemanager internals public for tests.
void JumpOutOfIbd()
Toggle IsInitialBlockDownload from true to false.
void ResetIbd()
Reset the ibd cache to its initial state.
Testing setup that configures a complete environment.
Bilingual messages:
Definition translation.h:24
bool empty() const
Definition translation.h:35
Information about chainstate that notifications are sent from.
Definition types.h:18
Block template creation options.
CScript coinbase_output_script
Script to put in the coinbase transaction.
std::unique_ptr< CTxMemPool > mempool
Definition context.h:71
std::unique_ptr< ChainstateManager > chainman
Definition context.h:76
#define LOCK(cs)
Definition sync.h:268
COutPoint MineBlock(const NodeContext &node, const node::BlockCreateOptions &assembler_options)
Returns the generated coin.
Definition mining.cpp:119
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)
Definition txmempool.cpp:21
std::vector< std::pair< COutPoint, CAmount > > ResetChainmanAndMempool(TestingSetup &setup, FakeNodeClock &node_clock)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
assert(!tx.IsCoinBase())