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 <chainparams.h>6#include <net.h>7#include <net_permissions.h>8#include <netaddress.h>9#include <protocol.h>10#include <random.h>11#include <test/fuzz/FuzzedDataProvider.h>12#include <test/fuzz/fuzz.h>13#include <test/fuzz/util.h>14#include <test/fuzz/util/net.h>15#include <test/util/net.h>16#include <test/util/setup_common.h>17#include <test/util/time.h>18#include <util/asmap.h>19#include <util/chaintype.h>20#include <util/time.h>22#include <cstdint>23#include <optional>24#include <string>25#include <vector>27void initialize_net()28{29static const auto testing_setup = MakeNoLogFileContext<>(ChainType::MAIN);30}32FUZZ_TARGET(net, .init = initialize_net)33{34FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());35FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)};36FakeSteadyClock steady_clock;37CNode node{ConsumeNode(fuzzed_data_provider, steady_clock)};38node.SetCommonVersion(fuzzed_data_provider.ConsumeIntegral<int>());39if (const auto service_opt =40ConsumeDeserializable<CService>(fuzzed_data_provider, ConsumeDeserializationParams<CNetAddr::SerParams>(fuzzed_data_provider)))41{42node.SetAddrLocal(*service_opt);43}44LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {45CallOneOf(46fuzzed_data_provider,47[&] {48node.CloseSocketDisconnect();49},50[&] {51CNodeStats stats;52node.CopyStats(stats);53},54[&] {55const CNode* add_ref_node = node.AddRef();56assert(add_ref_node == &node);57},58[&] {59if (node.GetRefCount() > 0) {60node.Release();61}62},63[&] {64const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider);65bool complete;66node.ReceiveMsgBytes(b, complete);67});68}70(void)node.GetAddrLocal();71(void)node.GetId();72(void)node.GetLocalNonce();73const int ref_count = node.GetRefCount();74assert(ref_count >= 0);75(void)node.GetCommonVersion();77const NetPermissionFlags net_permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);78(void)node.HasPermission(net_permission_flags);79(void)node.ConnectedThroughNetwork();80}82FUZZ_TARGET(local_address, .init = initialize_net)83{84FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());85FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)};86FakeSteadyClock steady_clock;87CService service{ConsumeService(fuzzed_data_provider)};88CNode node{ConsumeNode(fuzzed_data_provider, steady_clock)};89{90LOCK(g_maplocalhost_mutex);91mapLocalHost.clear();92}93LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {94CallOneOf(95fuzzed_data_provider,96[&] {97service = ConsumeService(fuzzed_data_provider);98},99[&] {100const bool added{AddLocal(service, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, LOCAL_MAX - 1))};101if (!added) return;102assert(service.IsRoutable());103assert(IsLocal(service));104assert(SeenLocal(service));105},106[&] {107(void)RemoveLocal(service);108},109[&] {110(void)SeenLocal(service);111},112[&] {113(void)IsLocal(service);114},115[&] {116(void)GetLocalAddress(node);117});118}119}
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.