Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
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>
15#include <test/util/net.h>
17#include <test/util/time.h>
19#include <util/time.h>
20#include <validationinterface.h>
21
22#include <ios>
23#include <utility>
24#include <vector>
25
26namespace {
28constexpr ReachabilityGoal P2P_HANDSHAKE_COMPLETES{"p2p_handshake completes a handshake"};
29
30void initialize()
31{
33 static const auto testing_setup = MakeNoLogFileContext<TestingSetup>(
34 /*chain_type=*/ChainType::REGTEST);
35 g_setup = testing_setup.get();
36}
37} // namespace
38
40{
42 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
43
44 auto& node{g_setup->m_node};
45 auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};
46 connman.Reset();
47 auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};
48 FakeNodeClock clock{1610000000s}; // 2021-01-07, arbitrary
49 FakeSteadyClock steady_clock;
50 chainman.ResetIbd();
51
52 node.banman.reset();
53 node.addrman.reset();
54 node.peerman.reset();
55 node.addrman = std::make_unique<AddrMan>(
56 *node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);
57 node.peerman = PeerManager::make(connman, *node.addrman,
58 /*banman=*/nullptr, chainman,
59 *node.mempool, *node.warnings,
61 .reconcile_txs = true,
62 .deterministic_rng = true,
63 });
64 connman.SetMsgProc(node.peerman.get());
65 connman.SetAddrman(*node.addrman);
66
68
69 std::vector<CNode*> peers;
71 for (int i = 0; i < num_peers_to_add; ++i) {
72 peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, steady_clock, i).release());
73 connman.AddTestNode(*peers.back());
74 node.peerman->InitializeNode(
75 *peers.back(),
77 }
78
79 // Toggle IBD from within the loop, so that some messages may be processed
80 // under IBD and the rest after leaving it. JumpOutOfIbd() latches, so guard
81 // it to call at most once.
82 bool jump_out_of_ibd{false};
84 CNode& connection = *PickValue(fuzzed_data_provider, peers);
85 if (connection.fDisconnect || connection.fSuccessfullyConnected) {
86 // Skip if the connection was disconnected or if the version
87 // handshake was already completed.
88 continue;
89 }
90
92 if (jump_out_of_ibd && chainman.IsInitialBlockDownload()) chainman.JumpOutOfIbd();
93
94 clock += std::chrono::seconds{
96 -std::chrono::seconds{10min}.count(), // Allow mocktime to go backwards slightly
97 std::chrono::seconds{TIMEOUT_INTERVAL}.count()),
98 };
99
103
104 connman.FlushSendBuffer(connection);
105 (void)connman.ReceiveMsgFrom(connection, std::move(net_msg));
106
107 bool more_work{true};
108 while (more_work) {
109 connection.fPauseSend = false;
110
111 try {
112 more_work = connman.ProcessMessagesOnce(connection);
113 } catch (const std::ios_base::failure&) {
114 }
115 node.peerman->SendMessages(connection);
116 }
117 }
118
119 bool completed_handshake{false};
120 for (const CNode* peer : peers) {
121 completed_handshake |= peer->fSuccessfullyConnected;
122 }
124 node.connman->StopNodes();
125}
const TestingSetup * g_setup
Information about a peer.
Definition net.h:686
std::atomic_bool fSuccessfullyConnected
fSuccessfullyConnected is set to true on receiving VERACK from the peer.
Definition net.h:745
std::atomic_bool fPauseSend
Definition net.h:754
std::atomic_bool fDisconnect
Definition net.h:748
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:1049
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 FUZZ_TARGET(...)
Definition fuzz.h:35
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition fuzz.h:22
Definition basic.cpp:11
constexpr std::chrono::minutes TIMEOUT_INTERVAL
Time after which to disconnect, after waiting for a ping response (or inactivity).
Definition net.h:60
constexpr unsigned int MAX_PROTOCOL_MESSAGE_LENGTH
Maximum length of incoming protocol messages (no message over 4 MB is currently acceptable).
Definition net.h:66
const std::array ALL_NET_MESSAGE_TYPES
All known message types (see above).
Definition protocol.h:274
ServiceFlags
nServices flags
Definition protocol.h:321
void ObserveReachabilityGoal(bool reached, const ReachabilityGoal &goal)
Record whether a registered goal is satisfied by at least one fuzz input.
void RegisterReachabilityGoal(const ReachabilityGoal &goal)
Register a goal before executing any fuzz inputs.
node::NodeContext m_node
std::string m_type
Definition net.h:142
void Reset()
Reset the internal state.
Definition net.cpp:83
A reachability condition explicitly registered by a fuzz target.
Testing setup that configures a complete environment.
#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.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:46
FakeNodeClock clock