Bitcoin Core  22.99.0
P2P Digital Currency
process_messages.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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/util/mining.h>
13 #include <test/util/net.h>
14 #include <test/util/setup_common.h>
15 #include <test/util/validation.h>
16 #include <txorphanage.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  g_setup = testing_setup.get();
28  for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
29  MineBlock(g_setup->m_node, CScript() << OP_TRUE);
30  }
32 }
33 
35 {
36  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
37 
38  ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get());
39  TestChainState& chainstate = *static_cast<TestChainState*>(&g_setup->m_node.chainman->ActiveChainstate());
40  SetMockTime(1610000000); // any time to successfully reset ibd
41  chainstate.ResetIbd();
42 
43  std::vector<CNode*> peers;
44  const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
45  for (int i = 0; i < num_peers_to_add; ++i) {
46  peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
47  CNode& p2p_node = *peers.back();
48 
49  const bool successfully_connected{fuzzed_data_provider.ConsumeBool()};
50  p2p_node.fSuccessfullyConnected = successfully_connected;
51  p2p_node.fPauseSend = false;
52  g_setup->m_node.peerman->InitializeNode(&p2p_node);
53  FillNode(fuzzed_data_provider, p2p_node, /* init_version */ successfully_connected);
54 
55  connman.AddTestNode(p2p_node);
56  }
57 
58  while (fuzzed_data_provider.ConsumeBool()) {
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  {
78  LOCK(random_node.cs_sendProcessing);
79  g_setup->m_node.peerman->SendMessages(&random_node);
80  }
81  }
83  g_setup->m_node.connman->StopNodes();
84 }
COINBASE_MATURITY
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition: consensus.h:19
CSerializedNetMsg::data
std::vector< unsigned char > data
Definition: net.h:109
setup_common.h
CNode
Information about a peer.
Definition: net.h:393
validationinterface.h
TestChainState::ResetIbd
void ResetIbd()
Reset the ibd cache to its initial state.
Definition: validation.cpp:11
util.h
CSerializedNetMsg::m_type
std::string m_type
Definition: net.h:110
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(process_messages, initialize_process_messages)
Definition: process_messages.cpp:34
ConnmanTestMsg::ReceiveMsgFrom
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &ser_msg) const
Definition: net.cpp:33
MineBlock
CTxIn MineBlock(const NodeContext &node, const CScript &coinbase_scriptPubKey)
Returns the generated coin.
Definition: mining.cpp:59
ConnmanTestMsg
Definition: net.h:18
validation.h
ConsumeTime
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:226
initialize_process_messages
void initialize_process_messages()
Definition: process_messages.cpp:24
ConnmanTestMsg::AddTestNode
void AddTestNode(CNode &node)
Definition: net.h:20
SyncWithValidationInterfaceQueue
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
Definition: validationinterface.cpp:160
net.h
SetMockTime
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:101
consensus.h
CSerializedNetMsg
Definition: net.h:100
CNode::fSuccessfullyConnected
std::atomic_bool fSuccessfullyConnected
fSuccessfullyConnected is set to true on receiving VERACK from the peer.
Definition: net.h:449
FuzzedDataProvider.h
CMessageHeader::COMMAND_SIZE
static constexpr size_t COMMAND_SIZE
Definition: protocol.h:34
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
TestChainState
Definition: validation.h:10
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
net_processing.h
OP_TRUE
@ OP_TRUE
Definition: script.h:77
CNode::cs_sendProcessing
RecursiveMutex cs_sendProcessing
Definition: net.h:419
fuzz.h
LOCK
#define LOCK(cs)
Definition: sync.h:226
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
ConnmanTestMsg::ProcessMessagesOnce
void ProcessMessagesOnce(CNode &node)
Definition: net.h:34
ConsumeNodeAsUniquePtr
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, const std::optional< NodeId > &node_id_in=std::nullopt)
Definition: util.h:276
FuzzedDataProvider::ConsumeBytesAsString
std::string ConsumeBytesAsString(size_t num_bytes)
Definition: FuzzedDataProvider.h:136
TestingSetup
Testing setup that configures a complete environment.
Definition: setup_common.h:99
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
txorphanage.h
CNode::fPauseSend
std::atomic_bool fPauseSend
Definition: net.h:458
FuzzedDataProvider::ConsumeIntegralInRange
T ConsumeIntegralInRange(T min, T max)
Definition: FuzzedDataProvider.h:204
FillNode
void FillNode(FuzzedDataProvider &fuzzed_data_provider, CNode &node, bool init_version) noexcept
Definition: util.cpp:202
mining.h
PickValue
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
Definition: util.h:52