Bitcoin Core  24.99.0
P2P Digital Currency
process_messages.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2022 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 <consensus/consensus.h>
6 #include <net.h>
7 #include <net_processing.h>
8 #include <protocol.h>
10 #include <test/fuzz/fuzz.h>
11 #include <test/fuzz/util.h>
12 #include <test/fuzz/util/net.h>
13 #include <test/util/mining.h>
14 #include <test/util/net.h>
15 #include <test/util/setup_common.h>
16 #include <test/util/validation.h>
17 #include <validation.h>
18 #include <validationinterface.h>
19 
20 namespace {
21 const TestingSetup* g_setup;
22 } // namespace
23 
25 {
26  static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(
27  /*chain_name=*/CBaseChainParams::REGTEST,
28  /*extra_args=*/{"-txreconciliation"});
29  g_setup = testing_setup.get();
30  for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
31  MineBlock(g_setup->m_node, CScript() << OP_TRUE);
32  }
34 }
35 
37 {
38  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
39 
40  ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get());
41  TestChainState& chainstate = *static_cast<TestChainState*>(&g_setup->m_node.chainman->ActiveChainstate());
42  SetMockTime(1610000000); // any time to successfully reset ibd
43  chainstate.ResetIbd();
44 
46 
47  std::vector<CNode*> peers;
48  const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
49  for (int i = 0; i < num_peers_to_add; ++i) {
50  peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
51  CNode& p2p_node = *peers.back();
52 
53  FillNode(fuzzed_data_provider, connman, p2p_node);
54 
55  connman.AddTestNode(p2p_node);
56  }
57 
58  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
59  const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
60 
61  const auto mock_time = ConsumeTime(fuzzed_data_provider);
62  SetMockTime(mock_time);
63 
64  CSerializedNetMsg net_msg;
65  net_msg.m_type = random_message_type;
66  net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
67 
68  CNode& random_node = *PickValue(fuzzed_data_provider, peers);
69 
70  (void)connman.ReceiveMsgFrom(random_node, net_msg);
71  random_node.fPauseSend = false;
72 
73  try {
74  connman.ProcessMessagesOnce(random_node);
75  } catch (const std::ios_base::failure&) {
76  }
77  g_setup->m_node.peerman->SendMessages(&random_node);
78  }
80  g_setup->m_node.connman->StopNodes();
81 }
static const std::string REGTEST
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:30
Information about a peer.
Definition: net.h:347
std::atomic_bool fPauseSend
Definition: net.h:416
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
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:630
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:18
FUZZ_TARGET_INIT(process_messages, initialize_process_messages)
void initialize_process_messages()
@ OP_TRUE
Definition: script.h:80
std::string m_type
Definition: net.h:122
std::vector< unsigned char > data
Definition: net.h:121
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &ser_msg) const
Definition: net.cpp:85
void Handshake(CNode &node, bool successfully_connected, ServiceFlags remote_services, ServiceFlags local_services, int32_t version, bool relay_txs) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface void ProcessMessagesOnce(CNode &node) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface
Definition: net.h:50
void AddTestNode(CNode &node)
Definition: net.h:28
void ResetIbd()
Reset the ibd cache to its initial state.
Definition: validation.cpp:12
Testing setup that configures a complete environment.
Definition: setup_common.h:108
#define LOCK(cs)
Definition: sync.h:258
void FillNode(FuzzedDataProvider &fuzzed_data_provider, ConnmanTestMsg &connman, CNode &node) noexcept
Definition: net.cpp:350
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, const std::optional< NodeId > &node_id_in=std::nullopt)
Definition: net.h:137
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:22
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
Definition: util.h:48
CTxIn MineBlock(const NodeContext &node, const CScript &coinbase_scriptPubKey)
Returns the generated coin.
Definition: mining.cpp:61
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:89
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...