Bitcoin Core  31.0.0
P2P Digital Currency
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 
5 #include <test/util/validation.h>
6 
7 #include <node/blockstorage.h>
8 #include <util/check.h>
9 #include <util/time.h>
10 #include <validation.h>
11 #include <validationinterface.h>
12 
14 
16 {
17  m_dirty_blockindex.clear();
18  m_dirty_fileinfo.clear();
19  m_blockfile_info.resize(1);
20 }
21 
23 {
24  struct TestChainstate : public Chainstate {
25  void ResetNextWrite() { m_next_write = NodeClock::time_point::max() - 1s; }
26  };
27  LOCK(::cs_main);
28  for (const auto& cs : m_chainstates) {
29  static_cast<TestChainstate&>(*cs).ResetNextWrite();
30  }
31 }
32 
34 {
35  m_cached_is_ibd = true;
37 }
38 
40 {
42  m_cached_is_ibd = false;
44 }
45 
47  const ChainstateRole& role,
49  const std::shared_ptr<const CBlock>& block,
50  const CBlockIndex* pindex)
51 {
52  obj.BlockConnected(role, block, pindex);
53 }
55 {
56  struct TestChainstate : public Chainstate {
57  void CallInvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
58  {
59  InvalidBlockFound(pindex, state);
60  }
61  };
62 
63  static_cast<TestChainstate*>(&ActiveChainstate())->CallInvalidBlockFound(pindex, state);
64 }
65 
67 {
68  struct TestChainstate : public Chainstate {
69  void CallInvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
70  {
71  InvalidChainFound(pindexNew);
72  }
73  };
74 
75  static_cast<TestChainstate*>(&ActiveChainstate())->CallInvalidChainFound(pindexNew);
76 }
77 
79 {
80  struct TestChainstate : public Chainstate {
81  CBlockIndex* CallFindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
82  {
83  return FindMostWorkChain();
84  }
85  };
86 
87  return static_cast<TestChainstate*>(&ActiveChainstate())->CallFindMostWorkChain();
88 }
89 
91 {
92  m_best_invalid = nullptr;
93 }
std::set< int > m_dirty_fileinfo
Dirty block file entries.
Definition: blockstorage.h:313
assert(!tx.IsCoinBase())
Information about chainstate that notifications are sent from.
Definition: types.h:18
void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Wrappers that avoid making chainstatemanager internals public for tests.
Definition: validation.cpp:54
static void BlockConnected(const kernel::ChainstateRole &role, CValidationInterface &obj, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Definition: validation.cpp:46
bool IsInitialBlockDownload() const noexcept
Check whether we are doing an initial block download (synchronizing from disk or network) ...
Implement this to subscribe to events generated in validation and mempool.
void JumpOutOfIbd()
Toggle IsInitialBlockDownload from true to false.
Definition: validation.cpp:39
Chainstate stores and provides an API to update our local knowledge of the current best chain...
Definition: validation.h:550
CBlockIndex * FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: validation.cpp:78
#define LOCK(cs)
Definition: sync.h:258
virtual void BlockConnected(const kernel::ChainstateRole &role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
void InvalidChainFound(CBlockIndex *pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: validation.cpp:66
std::set< CBlockIndex * > m_dirty_blockindex
Dirty block index entries.
Definition: blockstorage.h:310
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:93
void ResetIbd()
Reset the ibd cache to its initial state.
Definition: validation.cpp:33
Chainstate & ActiveChainstate() const
Alternatives to CurrentChainstate() used by older code to query latest chainstate information without...
void DisableNextWrite()
Disable the next write of all chainstates.
Definition: validation.cpp:22
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
void ResetBestInvalid() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: validation.cpp:90
void CleanupForFuzzing()
Test-only method to clear internal state for fuzzing.
Definition: validation.cpp:15
static bool pool cs
Definition: validation.cpp:400
#define Assert(val)
Identity function.
Definition: check.h:113
std::atomic_bool m_cached_is_ibd
Whether initial block download (IBD) is ongoing.
Definition: validation.h:1049
std::vector< CBlockFileInfo > m_blockfile_info
Definition: blockstorage.h:307