Bitcoin Core  26.99.0
P2P Digital Currency
process_message.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 <consensus/consensus.h>
6 #include <net.h>
7 #include <net_processing.h>
9 #include <protocol.h>
10 #include <script/script.h>
11 #include <sync.h>
13 #include <test/fuzz/fuzz.h>
14 #include <test/fuzz/util.h>
15 #include <test/fuzz/util/net.h>
16 #include <test/util/mining.h>
17 #include <test/util/net.h>
18 #include <test/util/setup_common.h>
19 #include <test/util/validation.h>
20 #include <util/check.h>
21 #include <util/time.h>
22 #include <validationinterface.h>
23 
24 #include <cstdlib>
25 #include <iostream>
26 #include <memory>
27 #include <string>
28 #include <string_view>
29 #include <vector>
30 
31 namespace {
32 const TestingSetup* g_setup;
33 std::string_view LIMIT_TO_MESSAGE_TYPE{};
34 } // namespace
35 
37 {
38  if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) {
39  LIMIT_TO_MESSAGE_TYPE = val;
40  Assert(std::count(getAllNetMessageTypes().begin(), getAllNetMessageTypes().end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed
41  }
42 
43  static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(
44  /*chain_type=*/ChainType::REGTEST,
45  /*extra_args=*/{"-txreconciliation"});
46  g_setup = testing_setup.get();
47  for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
48  MineBlock(g_setup->m_node, CScript() << OP_TRUE);
49  }
51 }
52 
54 {
55  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
56 
57  ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get());
58  auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman);
59  SetMockTime(1610000000); // any time to successfully reset ibd
60  chainman.ResetIbd();
61 
63 
64  const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
65  if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {
66  return;
67  }
68  CNode& p2p_node = *ConsumeNodeAsUniquePtr(fuzzed_data_provider).release();
69 
70  connman.AddTestNode(p2p_node);
71  FillNode(fuzzed_data_provider, connman, p2p_node);
72 
73  const auto mock_time = ConsumeTime(fuzzed_data_provider);
74  SetMockTime(mock_time);
75 
76  CSerializedNetMsg net_msg;
77  net_msg.m_type = random_message_type;
78  net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
79 
80  connman.FlushSendBuffer(p2p_node);
81  (void)connman.ReceiveMsgFrom(p2p_node, std::move(net_msg));
82 
83  bool more_work{true};
84  while (more_work) {
85  p2p_node.fPauseSend = false;
86  try {
87  more_work = connman.ProcessMessagesOnce(p2p_node);
88  } catch (const std::ios_base::failure&) {
89  }
90  g_setup->m_node.peerman->SendMessages(&p2p_node);
91  }
93  g_setup->m_node.connman->StopNodes();
94 }
#define Assert(val)
Identity function.
Definition: check.h:77
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:31
Information about a peer.
Definition: net.h:677
std::atomic_bool fPauseSend
Definition: net.h:742
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:414
std::string ConsumeBytesAsString(size_t num_bytes)
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
Definition: net.h:1000
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition: consensus.h:19
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:68
FUZZ_TARGET(process_message,.init=initialize_process_message)
void initialize_process_message()
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:167
@ OP_TRUE
Definition: script.h:83
std::string m_type
Definition: net.h:136
std::vector< unsigned char > data
Definition: net.h:135
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &&ser_msg) const
Definition: net.cpp:90
void AddTestNode(CNode &node)
Definition: net.h:48
void Handshake(CNode &node, bool successfully_connected, ServiceFlags remote_services, ServiceFlags local_services, int32_t version, bool relay_txs) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface bool ProcessMessagesOnce(CNode &node) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface
Definition: net.h:73
void FlushSendBuffer(CNode &node) const
Definition: net.cpp:78
Testing setup that configures a complete environment.
Definition: setup_common.h:78
#define LOCK(cs)
Definition: sync.h:257
void FillNode(FuzzedDataProvider &fuzzed_data_provider, ConnmanTestMsg &connman, CNode &node) noexcept
Definition: net.cpp:406
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, const std::optional< NodeId > &node_id_in=std::nullopt)
Definition: net.h:145
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
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 CScript &coinbase_scriptPubKey)
Returns the generated coin.
Definition: mining.cpp:63
static int count
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:81
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...