Inside Bitcoin
Opening the selected file…
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) 2021-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#ifndef BITCOIN_NETGROUP_H6#define BITCOIN_NETGROUP_H8#include <netaddress.h>9#include <uint256.h>11#include <cstddef>12#include <vector>14/**15* Netgroup manager16*/17class NetGroupManager {18public:19NetGroupManager(const NetGroupManager&) = delete;20NetGroupManager(NetGroupManager&&) = default;21NetGroupManager& operator=(const NetGroupManager&) = delete;22NetGroupManager& operator=(NetGroupManager&&) = delete;24static NetGroupManager WithEmbeddedAsmap(std::span<const std::byte> asmap) {25return NetGroupManager(asmap, {});26}28static NetGroupManager WithLoadedAsmap(std::vector<std::byte>&& asmap) {29return NetGroupManager(std::span{asmap}, std::move(asmap));30}32static NetGroupManager NoAsmap() {33return NetGroupManager({}, {});34}36/** Get the asmap version, a checksum identifying the asmap being used. */37uint256 GetAsmapVersion() const;39/**40* Get the canonical identifier of the network group for address.41*42* The groups are assigned in a way where it should be costly for an attacker to43* obtain addresses with many different group identifiers, even if it is cheap44* to obtain addresses with the same identifier.45*46* @note No two connections will be attempted to addresses with the same network47* group.48*/49std::vector<unsigned char> GetGroup(const CNetAddr& address) const;51/**52* Get the autonomous system on the BGP path to address.53*54* The ip->AS mapping depends on how asmap is constructed.55*/56uint32_t GetMappedAS(const CNetAddr& address) const;58/**59* Analyze and log current health of ASMap based buckets.60*/61void ASMapHealthCheck(const std::vector<CNetAddr>& clearnet_addrs) const;63/**64* Indicates whether ASMap is being used for clearnet bucketing.65*/66bool UsingASMap() const;68private:69/** Compressed IP->ASN mapping.70*71* Data may be loaded from a file when a node starts or embedded in the72* binary.73*74* This mapping is then used for bucketing nodes in Addrman and for75* ensuring we connect to a diverse set of peers in Connman. The map is76* empty if no file was provided.77*78* If asmap is provided, nodes will be bucketed by AS they belong to, in79* order to make impossible for a node to connect to several nodes hosted80* in a single AS. This is done in response to Erebus attack, but also to81* generally diversify the connections every node creates, especially82* useful when a large fraction of nodes operate under a couple of cloud83* providers.84*85* If a new asmap is provided, the existing addrman records are86* re-bucketed.87*88* This is initialized in the constructor, const, and therefore is89* thread-safe. m_asmap can either point to m_loaded_asmap which holds90* data loaded from an external file at runtime or it can point to embedded91* asmap data.92*/93const std::span<const std::byte> m_asmap;94std::vector<std::byte> m_loaded_asmap;95const uint256 m_asmap_version;97explicit NetGroupManager(std::span<const std::byte> embedded_asmap, std::vector<std::byte>&& loaded_asmap);98};100#endif // BITCOIN_NETGROUP_H
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.