Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2020-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <banman.h>6#include <net.h>7#include <net_processing.h>8#include <protocol.h>9#include <sync.h>10#include <test/fuzz/FuzzedDataProvider.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>15#include <test/util/setup_common.h>16#include <test/util/time.h>17#include <test/util/validation.h>18#include <util/time.h>19#include <validationinterface.h>21#include <ios>22#include <utility>23#include <vector>25namespace {26TestingSetup* g_setup;28void initialize()29{30static const auto testing_setup = MakeNoLogFileContext<TestingSetup>(31/*chain_type=*/ChainType::REGTEST);32g_setup = testing_setup.get();33}34} // namespace36FUZZ_TARGET(p2p_handshake, .init = ::initialize)37{38SeedRandomStateForTest(SeedRand::ZEROS);39FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());41auto& node{g_setup->m_node};42auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};43connman.Reset();44auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};45FakeNodeClock clock{1610000000s}; // 2021-01-07, arbitrary46FakeSteadyClock steady_clock;47chainman.ResetIbd();49node.banman.reset();50node.addrman.reset();51node.peerman.reset();52node.addrman = std::make_unique<AddrMan>(53*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);54node.peerman = PeerManager::make(connman, *node.addrman,55/*banman=*/nullptr, chainman,56*node.mempool, *node.warnings,57PeerManager::Options{58.reconcile_txs = true,59.deterministic_rng = true,60});61connman.SetMsgProc(node.peerman.get());62connman.SetAddrman(*node.addrman);64LOCK(NetEventsInterface::g_msgproc_mutex);66std::vector<CNode*> peers;67const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);68for (int i = 0; i < num_peers_to_add; ++i) {69peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, steady_clock, i).release());70connman.AddTestNode(*peers.back());71node.peerman->InitializeNode(72*peers.back(),73static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>()));74}76// Toggle IBD from within the loop, so that some messages may be processed77// under IBD and the rest after leaving it. JumpOutOfIbd() latches, so guard78// it to call at most once.79bool jump_out_of_ibd{false};80LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 100) {81CNode& connection = *PickValue(fuzzed_data_provider, peers);82if (connection.fDisconnect || connection.fSuccessfullyConnected) {83// Skip if the connection was disconnected or if the version84// handshake was already completed.85continue;86}88if (!jump_out_of_ibd) jump_out_of_ibd = fuzzed_data_provider.ConsumeBool();89if (jump_out_of_ibd && chainman.IsInitialBlockDownload()) chainman.JumpOutOfIbd();91clock += std::chrono::seconds{92fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(93-std::chrono::seconds{10min}.count(), // Allow mocktime to go backwards slightly94std::chrono::seconds{TIMEOUT_INTERVAL}.count()),95};97CSerializedNetMsg net_msg;98net_msg.m_type = PickValue(fuzzed_data_provider, ALL_NET_MESSAGE_TYPES);99net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);101connman.FlushSendBuffer(connection);102(void)connman.ReceiveMsgFrom(connection, std::move(net_msg));104bool more_work{true};105while (more_work) {106connection.fPauseSend = false;108try {109more_work = connman.ProcessMessagesOnce(connection);110} catch (const std::ios_base::failure&) {111}112node.peerman->SendMessages(connection);113}114}116node.connman->StopNodes();117}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.