Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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
6#include <net.h>
7#include <net_processing.h>
8#include <protocol.h>
9#include <script/script.h>
10#include <sync.h>
12#include <test/fuzz/fuzz.h>
13#include <test/fuzz/util.h>
14#include <test/fuzz/util/net.h>
15#include <test/util/mining.h>
16#include <test/util/net.h>
19#include <util/time.h>
20#include <validationinterface.h>
21
22#include <ios>
23#include <string>
24#include <utility>
25#include <vector>
26
27namespace {
28const TestingSetup* g_setup;
29} // namespace
30
32{
33 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(
34 /*chain_type=*/ChainType::REGTEST,
35 {.extra_args = {"-txreconciliation"}});
36 g_setup = testing_setup.get();
37 for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
38 MineBlock(g_setup->m_node, CScript() << OP_TRUE);
39 }
40 g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
41}
42
44{
45 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
46
47 ConnmanTestMsg& connman = *static_cast<ConnmanTestMsg*>(g_setup->m_node.connman.get());
48 auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman);
49 SetMockTime(1610000000); // any time to successfully reset ibd
50 chainman.ResetIbd();
51
53
54 std::vector<CNode*> peers;
55 const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
56 for (int i = 0; i < num_peers_to_add; ++i) {
57 peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
58 CNode& p2p_node = *peers.back();
59
60 FillNode(fuzzed_data_provider, connman, p2p_node);
61
62 connman.AddTestNode(p2p_node);
63 }
64
65 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 30)
66 {
67 const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
68
69 const auto mock_time = ConsumeTime(fuzzed_data_provider);
70 SetMockTime(mock_time);
71
72 CSerializedNetMsg net_msg;
73 net_msg.m_type = random_message_type;
74 net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);
75
76 CNode& random_node = *PickValue(fuzzed_data_provider, peers);
77
78 connman.FlushSendBuffer(random_node);
79 (void)connman.ReceiveMsgFrom(random_node, std::move(net_msg));
80
81 bool more_work{true};
82 while (more_work) { // Ensure that every message is eventually processed in some way or another
83 random_node.fPauseSend = false;
84
85 try {
86 more_work = connman.ProcessMessagesOnce(random_node);
87 } catch (const std::ios_base::failure&) {
88 }
89 g_setup->m_node.peerman->SendMessages(&random_node);
90 }
91 }
92 g_setup->m_node.validation_signals->SyncWithValidationInterfaceQueue();
93 g_setup->m_node.connman->StopNodes();
94}
static constexpr size_t COMMAND_SIZE
Definition protocol.h:31
Information about a peer.
Definition net.h:670
std::atomic_bool fPauseSend
Definition net.h:734
Serialized script, used inside transaction inputs and outputs.
Definition script.h:414
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:992
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 FUZZ_TARGET(...)
Definition fuzz.h:35
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition fuzz.h:22
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:63
void initialize_process_messages()
@ OP_TRUE
Definition script.h:83
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
node::NodeContext m_node
std::string m_type
Definition net.h:131
std::vector< unsigned char > data
Definition net.h:130
bool ReceiveMsgFrom(CNode &node, CSerializedNetMsg &&ser_msg) const
Definition net.cpp:91
void Handshake(CNode &node, bool successfully_connected, ServiceFlags remote_services, ServiceFlags local_services, int32_t version, bool relay_txs) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface boo ProcessMessagesOnce)(CNode &node) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface
Definition net.h:78
void AddTestNode(CNode &node)
Definition net.h:53
void FlushSendBuffer(CNode &node) const
Definition net.cpp:79
Testing setup that configures a complete environment.
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
Definition context.h:85
#define LOCK(cs)
Definition sync.h:257
void FillNode(FuzzedDataProvider &fuzzed_data_provider, ConnmanTestMsg &connman, CNode &node) noexcept
Definition net.cpp:415
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
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 CScript &coinbase_scriptPubKey)
Returns the generated coin.
Definition mining.cpp:63
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition time.cpp:32