Bitcoin Core 30.99.0
P2P Digital Currency
process_messages.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 <banman.h>
7#include <net.h>
8#include <net_processing.h>
9#include <node/warnings.h>
10#include <protocol.h>
11#include <script/script.h>
12#include <sync.h>
14#include <test/fuzz/fuzz.h>
15#include <test/fuzz/util.h>
16#include <test/fuzz/util/net.h>
17#include <test/util/mining.h>
18#include <test/util/net.h>
21#include <util/time.h>
22#include <validationinterface.h>
23
24#include <ios>
25#include <string>
26#include <utility>
27#include <vector>
28
29namespace {
31
32void ResetChainman(TestingSetup& setup)
33{
34 SetMockTime(setup.m_node.chainman->GetParams().GenesisBlock().Time());
35 setup.m_node.chainman.reset();
36 setup.m_make_chainman();
37 setup.LoadVerifyActivateChainstate();
38 for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
39 MineBlock(setup.m_node, {});
40 }
41 setup.m_node.validation_signals->SyncWithValidationInterfaceQueue();
42}
43} // namespace
44
46{
47 static const auto testing_setup{
48 MakeNoLogFileContext<TestingSetup>(
49 /*chain_type=*/ChainType::REGTEST,
50 {}),
51 };
52 g_setup = testing_setup.get();
53 ResetChainman(*g_setup);
54}
55
57{
59 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
60
61 auto& node{g_setup->m_node};
62 auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
63 connman.ResetAddrCache();
64 connman.ResetMaxOutboundCycle();
65 auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
66 const auto block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};
67 SetMockTime(1610000000); // any time to successfully reset ibd
68 chainman.ResetIbd();
69 chainman.DisableNextWrite();
70
71 // Reset, so that dangling pointers can be detected by sanitizers.
72 node.banman.reset();
73 node.addrman.reset();
74 node.peerman.reset();
75 node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
76 node.peerman = PeerManager::make(connman, *node.addrman,
77 /*banman=*/nullptr, chainman,
78 *node.mempool, *node.warnings,
80 .reconcile_txs = true,
81 .deterministic_rng = true,
82 });
83 connman.SetMsgProc(node.peerman.get());
84 connman.SetAddrman(*node.addrman);
85
87
88 std::vector<CNode*> peers;
89 const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
90 for (int i = 0; i < num_peers_to_add; ++i) {
91 peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
92 CNode& p2p_node = *peers.back();
93
94 FillNode(fuzzed_data_provider, connman, p2p_node);
95
96 connman.AddTestNode(p2p_node);
97 }
98
100 {
101 const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};
102
103 const auto mock_time = ConsumeTime(fuzzed_data_provider);
104 SetMockTime(mock_time);
105
106 CSerializedNetMsg net_msg;
107 net_msg.m_type = random_message_type;
109
110 CNode& random_node = *PickValue(fuzzed_data_provider, peers);
111
112 connman.FlushSendBuffer(random_node);
113 (void)connman.ReceiveMsgFrom(random_node, std::move(net_msg));
114
115 bool more_work{true};
116 while (more_work) { // Ensure that every message is eventually processed in some way or another
117 random_node.fPauseSend = false;
118
119 try {
120 more_work = connman.ProcessMessagesOnce(random_node);
121 } catch (const std::ios_base::failure&) {
122 }
123 node.peerman->SendMessages(&random_node);
124 }
125 }
126 node.validation_signals->SyncWithValidationInterfaceQueue();
127 node.connman->StopNodes();
128 if (block_index_size != WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())) {
129 // Reuse the global chainman, but reset it when it is dirty
130 ResetChainman(*g_setup);
131 }
132}
const TestingSetup * g_setup
static constexpr size_t MESSAGE_TYPE_SIZE
Definition: protocol.h:31
Information about a peer.
Definition: net.h:680
std::atomic_bool fPauseSend
Definition: net.h:744
std::string ConsumeBytesAsString(size_t num_bytes)
T ConsumeIntegralInRange(T min, T max)
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
Definition: net.h:1031
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition: consensus.h:19
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
Definition: messages.h:21
static const unsigned int MAX_PROTOCOL_MESSAGE_LENGTH
Maximum length of incoming protocol messages (no message over 4 MB is currently acceptable).
Definition: net.h:65
FUZZ_TARGET(process_messages,.init=initialize_process_messages)
void initialize_process_messages()
node::NodeContext m_node
Definition: setup_common.h:66
std::string m_type
Definition: net.h:137
std::vector< unsigned char > data
Definition: net.h:136
void ResetAddrCache()
Definition: net.cpp:74
Testing setup that configures a complete environment.
Definition: setup_common.h:121
#define LOCK(cs)
Definition: sync.h:259
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:290
void FillNode(FuzzedDataProvider &fuzzed_data_provider, ConnmanTestMsg &connman, CNode &node) noexcept
Definition: net.cpp:457
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, const std::optional< NodeId > &node_id_in=std::nullopt)
Definition: net.h:310
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
Definition: util.h:47
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
COutPoint MineBlock(const NodeContext &node, const node::BlockAssembler::Options &assembler_options)
Returns the generated coin.
Definition: mining.cpp:70
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
static int setup(void)
Definition: tests.c:7808
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:44
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:38