Bitcoin Core 31.0.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 <addrman.h>
7#include <net.h>
8#include <net_processing.h>
9#include <node/warnings.h>
10#include <protocol.h>
11#include <script/script.h>
12#include <sync.h>
14#include <test/fuzz/fuzz.h>
15#include <test/fuzz/util.h>
16#include <test/fuzz/util/net.h>
17#include <test/util/mining.h>
18#include <test/util/net.h>
21#include <util/time.h>
22#include <validationinterface.h>
23
24#include <ios>
25#include <string>
26#include <utility>
27#include <vector>
28
29namespace {
31
32void initialize()
33{
34 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(
35 /*chain_type=*/ChainType::REGTEST);
36 g_setup = testing_setup.get();
37}
38} // namespace
39
41{
43 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
44
45 auto& connman = static_cast<ConnmanTestMsg&>(*g_setup->m_node.connman);
46 auto& chainman = static_cast<TestChainstateManager&>(*g_setup->m_node.chainman);
47 SetMockTime(1610000000); // any time to successfully reset ibd
48 chainman.ResetIbd();
49
50 node::Warnings warnings{};
51 auto netgroupman{NetGroupManager::NoAsmap()};
52 AddrMan addrman{netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0};
53 auto peerman = PeerManager::make(connman, addrman,
54 /*banman=*/nullptr, chainman,
55 *g_setup->m_node.mempool, warnings,
57 .reconcile_txs = true,
58 .deterministic_rng = true,
59 });
60 connman.SetMsgProc(peerman.get());
61
63
64 std::vector<CNode*> peers;
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 peerman->InitializeNode(
70 *peers.back(),
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
85 -std::chrono::seconds{10min}.count(), // Allow mocktime to go backwards slightly
86 std::chrono::seconds{TIMEOUT_INTERVAL}.count()));
87
91
92 connman.FlushSendBuffer(connection);
93 (void)connman.ReceiveMsgFrom(connection, std::move(net_msg));
94
95 bool more_work{true};
96 while (more_work) {
97 connection.fPauseSend = false;
98
99 try {
100 more_work = connman.ProcessMessagesOnce(connection);
101 } catch (const std::ios_base::failure&) {
102 }
103 peerman->SendMessages(connection);
104 }
105 }
106
107 g_setup->m_node.connman->StopNodes();
108}
const TestingSetup * g_setup
Stochastic address manager.
Definition addrman.h:89
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:1031
static NetGroupManager NoAsmap()
Definition netgroup.h:32
static std::unique_ptr< PeerManager > make(CConnman &connman, AddrMan &addrman, BanMan *banman, ChainstateManager &chainman, CTxMemPool &pool, node::Warnings &warnings, Options opts)
Manages warning messages within a node.
Definition warnings.h:40
static void initialize()
Definition fuzz.cpp:95
#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:65
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
std::string m_type
Definition net.h:137
Testing setup that configures a complete environment.
std::unique_ptr< CConnman > connman
Definition context.h:67
std::unique_ptr< CTxMemPool > mempool
Definition context.h:68
std::unique_ptr< ChainstateManager > chainman
Definition context.h:72
#define LOCK(cs)
Definition sync.h:258
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:47
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition util.h:57
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.
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition time.cpp:81
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition time.cpp:44
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:38