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 connman.Reset();
44 auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
45 FakeNodeClock clock{1610000000s}; // 2021-01-07, arbitrary
46 FakeSteadyClock steady_clock;
47 chainman.ResetIbd();
48
49 node.banman.reset();
50 node.addrman.reset();
51 node.peerman.reset();
52 node.addrman = std::make_unique<AddrMan>(
53 *node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
54 node.peerman = PeerManager::make(connman, *node.addrman,
55 /*banman=*/nullptr, chainman,
56 *node.mempool, *node.warnings,
58 .reconcile_txs = true,
59 .deterministic_rng = true,
60 });
61 connman.SetMsgProc(node.peerman.get());
62 connman.SetAddrman(*node.addrman);
63
65
66 std::vector<CNode*> peers;
67 const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
68 for (int i = 0; i < num_peers_to_add; ++i) {
69 peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, steady_clock, i).release());
70 connman.AddTestNode(*peers.back());
71 node.peerman->InitializeNode(
72 *peers.back(),
73 static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>()));
74 }
75
76 // Toggle IBD from within the loop, so that some messages may be processed
77 // under IBD and the rest after leaving it. JumpOutOfIbd() latches, so guard
78 // it to call at most once.
79 bool jump_out_of_ibd{false};
81 CNode& connection = *PickValue(fuzzed_data_provider, peers);
82 if (connection.fDisconnect || connection.fSuccessfullyConnected) {
83 // Skip if the connection was disconnected or if the version
84 // handshake was already completed.
85 continue;
86 }
87
88 if (!jump_out_of_ibd) jump_out_of_ibd = fuzzed_data_provider.ConsumeBool();
89 if (jump_out_of_ibd && chainman.IsInitialBlockDownload()) chainman.JumpOutOfIbd();
90
91 clock += std::chrono::seconds{
93 -std::chrono::seconds{10min}.count(), // Allow mocktime to go backwards slightly
94 std::chrono::seconds{TIMEOUT_INTERVAL}.count()),
95 };
96
97 CSerializedNetMsg net_msg;
100
101 connman.FlushSendBuffer(connection);
102 (void)connman.ReceiveMsgFrom(connection, std::move(net_msg));
103
104 bool more_work{true};
105 while (more_work) {
106 connection.fPauseSend = false;
107
108 try {
109 more_work = connman.ProcessMessagesOnce(connection);
110 } catch (const std::ios_base::failure&) {
111 }
112 node.peerman->SendMessages(connection);
113 }
114 }
115
116 node.connman->StopNodes();
117}
const TestingSetup * g_setup
Information about a peer.
Definition: net.h:683
std::atomic_bool fSuccessfullyConnected
fSuccessfullyConnected is set to true on receiving VERACK from the peer.
Definition: net.h:742
std::atomic_bool fPauseSend
Definition: net.h:751
std::atomic_bool fDisconnect
Definition: net.h:745
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:54
Helper to initialize the global MockableSteadyClock, let a duration elapse, and reset it after use in...
Definition: time.h:29
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:1044
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
LIMITED_WHILE(provider.remaining_bytes(), 10000)
static void initialize()
Definition: fuzz.cpp:93
Definition: basic.cpp:11
Definition: messages.h:21
constexpr std::chrono::minutes TIMEOUT_INTERVAL
Time after which to disconnect, after waiting for a ping response (or inactivity).
Definition: net.h:59
constexpr 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:274
ServiceFlags
nServices flags
Definition: protocol.h:321
node::NodeContext m_node
Definition: setup_common.h:60
std::string m_type
Definition: net.h:139
std::vector< unsigned char > data
Definition: net.h:138
void Reset()
Reset the internal state.
Definition: net.cpp:83
Testing setup that configures a complete environment.
Definition: setup_common.h:115
#define LOCK(cs)
Definition: sync.h:268
SeedRandomStateForTest(SeedRand::ZEROS)
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, FakeSteadyClock &clock, const std::optional< NodeId > &node_id_in=std::nullopt)
Definition: net.h:310
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
Definition: util.h:57
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:63
@ ZEROS
Seed with a compile time constant of zeros.
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:45