validation.cpp raw
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
13 using kernel::ChainstateRole;
14
15 void TestBlockManager::CleanupForFuzzing()
16 {
17 m_dirty_blockindex.clear();
18 m_dirty_fileinfo.clear();
19 m_blockfile_info.resize(1);
20 }
21
22 void TestChainstateManager::DisableNextWrite()
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
33 void TestChainstateManager::ResetIbd()
34 {
35 m_cached_is_ibd = true;
36 assert(IsInitialBlockDownload());
37 }
38
39 void TestChainstateManager::JumpOutOfIbd()
40 {
41 Assert(IsInitialBlockDownload());
42 m_cached_is_ibd = false;
43 Assert(!IsInitialBlockDownload());
44 }
45
46 void ValidationInterfaceTest::BlockConnected(
47 const ChainstateRole& role,
48 CValidationInterface& obj,
49 const std::shared_ptr<const CBlock>& block,
50 const CBlockIndex* pindex)
51 {
52 obj.BlockConnected(role, block, pindex);
53 }
54 void TestChainstateManager::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state)
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
66 void TestChainstateManager::InvalidChainFound(CBlockIndex* pindexNew)
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
78 CBlockIndex* TestChainstateManager::FindMostWorkChain()
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
90 void TestChainstateManager::ResetBestInvalid()
91 {
92 m_best_invalid = nullptr;
93 }
94