Branch data Line data Source code
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 : : #ifndef BITCOIN_TEST_UTIL_VALIDATION_H
6 : : #define BITCOIN_TEST_UTIL_VALIDATION_H
7 : :
8 : : #include <consensus/amount.h>
9 : : #include <primitives/transaction.h>
10 : : #include <util/task_runner.h>
11 : : #include <validation.h>
12 : :
13 : : #include <cstddef>
14 : : #include <functional>
15 : : #include <thread>
16 : : #include <utility>
17 : : #include <vector>
18 : :
19 : : namespace node {
20 : : class BlockManager;
21 : : struct NodeContext;
22 : : }
23 : : class BaseIndex;
24 : : class CValidationInterface;
25 : : class FakeNodeClock;
26 : : struct TestingSetup;
27 : :
28 : : /// Runs callbacks synchronously and deterministically, while avoiding DEBUG_LOCKORDER false positives.
29 : 2 : class ImmediateBackgroundTaskRunner : public util::TaskRunnerInterface
30 : : {
31 : : public:
32 [ + - ]: 1174654 : void insert(std::function<void()> func) override { std::thread(std::move(func)).join(); }
33 : 2 : void flush() override {}
34 : 291077 : size_t size() override { return 0; }
35 : : };
36 : :
37 : : struct TestBlockManager : public node::BlockManager {
38 : : /** Test-only method to clear internal state for fuzzing */
39 : : void CleanupForFuzzing();
40 : : };
41 : :
42 : : struct TestChainstateManager : public ChainstateManager {
43 : : /** Disable the next write of all chainstates */
44 : : void DisableNextWrite();
45 : : /** Reset the ibd cache to its initial state */
46 : : void ResetIbd();
47 : : /** Toggle IsInitialBlockDownload from true to false */
48 : : void JumpOutOfIbd();
49 : : /** Wrappers that avoid making chainstatemanager internals public for tests */
50 : : void InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
51 : : void InvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
52 : : CBlockIndex* FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
53 : : void ResetBestInvalid() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
54 : : };
55 : :
56 : : class ValidationInterfaceTest
57 : : {
58 : : public:
59 : : static void BlockConnected(
60 : : const kernel::ChainstateRole& role,
61 : : CValidationInterface& obj,
62 : : const std::shared_ptr<const CBlock>& block,
63 : : const CBlockIndex* pindex);
64 : : };
65 : :
66 : : struct ResetOptions {
67 : : bool init_block_filter_basic{false};
68 : : };
69 : :
70 : : std::vector<std::pair<COutPoint, CAmount>> ResetChainmanAndMempool(TestingSetup& setup, FakeNodeClock& node_clock, const ResetOptions& opts = {});
71 : :
72 : : /// Drain validation callbacks before stopping an index, matching Shutdown() in init.cpp.
73 : : void StopIndex(BaseIndex& index, node::NodeContext& node);
74 : :
75 : : #endif // BITCOIN_TEST_UTIL_VALIDATION_H
|