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 <addrman.h>6#include <banman.h>7#include <kernel/chainparams.h>8#include <net.h>9#include <net_processing.h>10#include <primitives/block.h>11#include <primitives/transaction.h>12#include <protocol.h>13#include <sync.h>14#include <test/fuzz/FuzzedDataProvider.h>15#include <test/fuzz/fuzz.h>16#include <test/fuzz/util.h>17#include <test/fuzz/util/net.h>18#include <test/util/net.h>19#include <test/util/random.h>20#include <test/util/setup_common.h>21#include <test/util/time.h>22#include <test/util/validation.h>23#include <uint256.h>24#include <util/check.h>25#include <util/time.h>26#include <validation.h>27#include <validationinterface.h>29#include <algorithm>30#include <array>31#include <cstdlib>32#include <functional>33#include <iostream>34#include <memory>35#include <optional>36#include <string>37#include <string_view>38#include <utility>39#include <vector>41namespace {42TestingSetup* g_setup;43std::string_view LIMIT_TO_MESSAGE_TYPE{};45} // namespace47extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;49void initialize_process_message()50{51FakeNodeClock init_clock{}; // Uses the existing mock time52if (const auto val{std::getenv("LIMIT_TO_MESSAGE_TYPE")}) {53LIMIT_TO_MESSAGE_TYPE = val;54Assert(std::count(ALL_NET_MESSAGE_TYPES.begin(), ALL_NET_MESSAGE_TYPES.end(), LIMIT_TO_MESSAGE_TYPE)); // Unknown message type passed55}57static const auto testing_setup{58MakeNoLogFileContext<TestingSetup>(59/*chain_type=*/ChainType::REGTEST,60{}),61};62g_setup = testing_setup.get();63ResetChainmanAndMempool(*g_setup, init_clock);64}66FUZZ_TARGET(process_message, .init = initialize_process_message)67{68SeedRandomStateForTest(SeedRand::ZEROS);69FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());71auto& node{g_setup->m_node};72auto& connman{static_cast<ConnmanTestMsg&>(*node.connman)};73connman.Reset();74auto& chainman{static_cast<TestChainstateManager&>(*node.chainman)};75const auto block_index_size{WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size())};76const auto initial_sequence{WITH_LOCK(node.mempool->cs, return node.mempool->GetSequence())};77FakeNodeClock node_clock{1610000000s}; // 2021-01-07, arbitrary78FakeSteadyClock steady_clock;79chainman.ResetIbd();80chainman.DisableNextWrite();82// Reset, so that dangling pointers can be detected by sanitizers.83node.banman.reset();84node.addrman.reset();85node.peerman.reset();86node.addrman = std::make_unique<AddrMan>(*node.netgroupman, /*deterministic=*/true, /*consistency_check_ratio=*/0);87node.peerman = PeerManager::make(connman, *node.addrman,88/*banman=*/nullptr, chainman,89*node.mempool, *node.warnings,90PeerManager::Options{91.reconcile_txs = true,92.deterministic_rng = true,93});95connman.SetMsgProc(node.peerman.get());96connman.SetAddrman(*node.addrman);97LOCK(NetEventsInterface::g_msgproc_mutex);99const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::MESSAGE_TYPE_SIZE).c_str()};100if (!LIMIT_TO_MESSAGE_TYPE.empty() && random_message_type != LIMIT_TO_MESSAGE_TYPE) {101return;102}104node.validation_signals->RegisterValidationInterface(node.peerman.get());106CNode& p2p_node = *ConsumeNodeAsUniquePtr(fuzzed_data_provider, steady_clock).release();108connman.AddTestNode(p2p_node);109FillNode(fuzzed_data_provider, connman, p2p_node);111node_clock.set(ConsumeTime(fuzzed_data_provider));113CSerializedNetMsg net_msg;114net_msg.m_type = random_message_type;115net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider, MAX_PROTOCOL_MESSAGE_LENGTH);117connman.FlushSendBuffer(p2p_node);118(void)connman.ReceiveMsgFrom(p2p_node, std::move(net_msg));120if (fuzzed_data_provider.ConsumeBool()) {121chainman.JumpOutOfIbd();122}124bool more_work{true};125while (more_work) {126p2p_node.fPauseSend = false;127try {128more_work = connman.ProcessMessagesOnce(p2p_node);129} catch (const std::ios_base::failure&) {130}131node.peerman->SendMessages(p2p_node);132}133node.validation_signals->SyncWithValidationInterfaceQueue();134node.validation_signals->UnregisterValidationInterface(node.peerman.get());135node.connman->StopNodes();136const auto end_sequence{WITH_LOCK(node.mempool->cs, return node.mempool->GetSequence())};137if (block_index_size != WITH_LOCK(chainman.GetMutex(), return chainman.BlockIndex().size()) || initial_sequence != end_sequence) {138// Reuse the global chainman and mempool, but reset them when dirty.139MakeRandDeterministicDANGEROUS(uint256::ZERO);140ResetChainmanAndMempool(*g_setup, node_clock);141}142}
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.