Bitcoin Core 31.99.0
P2P Digital Currency
p2p_handshake.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>
6#include <net.h>
7#include <net_processing.h>
8#include <protocol.h>
9#include <sync.h>
11#include <test/fuzz/fuzz.h>
12#include <test/fuzz/util.h>
13#include <test/fuzz/util/net.h>
14#include <test/util/net.h>
16#include <test/util/time.h>
18#include <util/time.h>
19#include <validationinterface.h>
20
21#include <ios>
22#include <utility>
23#include <vector>
24
25namespace {
27
28void initialize()
29{
30 static const auto testing_setup = MakeNoLogFileContext<TestingSetup>(
31 /*chain_type=*/ChainType::REGTEST);
32 g_setup = testing_setup.get();
33}
34} // namespace
35
36FUZZ_TARGET(p2p_handshake, .init = ::initialize)
37{
39 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
40
41 auto& node{g_setup->m_node};
42 auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
43 auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
44 NodeClockContext clock_ctx{1610000000s}; // any time to successfully reset ibd
45 chainman.ResetIbd();
46
47 node.banman.reset();
48 node.addrman.reset();
49 node.peerman.reset();
50 node.addrman = std::make_unique<AddrMan>(
51 *node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
52 node.peerman = PeerManager::make(connman, *node.addrman,
53 /*banman=*/nullptr, chainman,
54 *node.mempool, *node.warnings,
56 .reconcile_txs = true,
57 .deterministic_rng = true,
58 });
59 connman.SetMsgProc(node.peerman.get());
60 connman.SetAddrman(*node.addrman);
61
63
64 std::vector<CNode*> peers;
65 const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
66 for (int i = 0; i < num_peers_to_add; ++i) {
67 peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
68 connman.AddTestNode(*peers.back());
69 node.peerman->InitializeNode(
70 *peers.back(),
71 static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>()));
72 }
73
75 {
76 CNode& connection = *PickValue(fuzzed_data_provider, peers);
77 if (connection.fDisconnect || connection.fSuccessfullyConnected) {
78 // Skip if the connection was disconnected or if the version
79 // handshake was already completed.
80 continue;
81 }
82
83 clock_ctx += std::chrono::seconds{
85 -std::chrono::seconds{10min}.count(), // Allow mocktime to go backwards slightly
86 std::chrono::seconds{TIMEOUT_INTERVAL}.count()),
87 };
88
89 CSerializedNetMsg net_msg;
92
93 connman.FlushSendBuffer(connection);
94 (void)connman.ReceiveMsgFrom(connection, std::move(net_msg));
95
96 bool more_work{true};
97 while (more_work) {
98 connection.fPauseSend = false;
99
100 try {
101 more_work = connman.ProcessMessagesOnce(connection);
102 } catch (const std::ios_base::failure&) {
103 }
104 node.peerman->SendMessages(connection);
105 }
106 }
107
108 node.connman->StopNodes();
109}
const TestingSetup * g_setup
Information about a peer.
Definition: net.h:680
std::atomic_bool fSuccessfullyConnected
fSuccessfullyConnected is set to true on receiving VERACK from the peer.
Definition: net.h:735
std::atomic_bool fPauseSend
Definition: net.h:744
std::atomic_bool fDisconnect
Definition: net.h:738
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:1030
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:40
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
static void initialize()
Definition: fuzz.cpp:93
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
Definition: basic.cpp:8
Definition: messages.h:21
static constexpr std::chrono::minutes TIMEOUT_INTERVAL
Time after which to disconnect, after waiting for a ping response (or inactivity).
Definition: net.h:59
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(p2p_handshake,.init=::initialize)
const std::array ALL_NET_MESSAGE_TYPES
All known message types (see above).
Definition: protocol.h:270
ServiceFlags
nServices flags
Definition: protocol.h:309
node::NodeContext m_node
Definition: setup_common.h:63
std::string m_type
Definition: net.h:137
std::vector< unsigned char > data
Definition: net.h:136
Testing setup that configures a complete environment.
Definition: setup_common.h:118
#define LOCK(cs)
Definition: sync.h:268
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, const std::optional< NodeId > &node_id_in=std::nullopt)
Definition: net.h:310
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
Definition: util.h:49
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:59
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.
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:39