LCOV - code coverage report
Current view: top level - src - addrman_impl.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 19 20 95.0 %
Date: 2026-06-25 07:23:43 Functions: 27 32 84.4 %

          Line data    Source code
       1             : // Copyright (c) 2021 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             : #ifndef BITCOIN_ADDRMAN_IMPL_H
       6             : #define BITCOIN_ADDRMAN_IMPL_H
       7             : 
       8             : #include <logging.h>
       9             : #include <logging/timer.h>
      10             : #include <netaddress.h>
      11             : #include <protocol.h>
      12             : #include <serialize.h>
      13             : #include <sync.h>
      14             : #include <timedata.h>
      15             : #include <uint256.h>
      16             : #include <util/time.h>
      17             : 
      18             : #include <cstdint>
      19             : #include <optional>
      20             : #include <set>
      21             : #include <unordered_map>
      22             : #include <unordered_set>
      23             : #include <utility>
      24             : #include <vector>
      25             : 
      26             : /** Total number of buckets for tried addresses */
      27             : static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2{8};
      28             : static constexpr int ADDRMAN_TRIED_BUCKET_COUNT{1 << ADDRMAN_TRIED_BUCKET_COUNT_LOG2};
      29             : /** Total number of buckets for new addresses */
      30             : static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2{10};
      31             : static constexpr int ADDRMAN_NEW_BUCKET_COUNT{1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2};
      32             : /** Maximum allowed number of entries in buckets for new and tried addresses */
      33             : static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2{6};
      34             : static constexpr int ADDRMAN_BUCKET_SIZE{1 << ADDRMAN_BUCKET_SIZE_LOG2};
      35             : 
      36             : /**
      37             :  * Extended statistics about a CAddress
      38             :  */
      39           0 : class AddrInfo : public CAddress
      40             : {
      41             : public:
      42             :     //! last try whatsoever by us (memory only)
      43      175830 :     NodeSeconds m_last_try{0s};
      44             : 
      45             :     //! last counted attempt (memory only)
      46      175830 :     NodeSeconds m_last_count_attempt{0s};
      47             : 
      48             :     //! where knowledge about this address first came from
      49             :     CNetAddr source;
      50             : 
      51             :     //! last successful connection by us
      52      175830 :     NodeSeconds m_last_success{0s};
      53             : 
      54             :     //! connection attempts since last successful attempt
      55      175830 :     int nAttempts{0};
      56             : 
      57             :     //! reference count in new sets (memory only)
      58      175830 :     int nRefCount{0};
      59             : 
      60             :     //! in tried set? (memory only)
      61      175830 :     bool fInTried{false};
      62             : 
      63             :     //! position in vRandom
      64      175830 :     mutable int nRandomPos{-1};
      65             : 
      66      456552 :     SERIALIZE_METHODS(AddrInfo, obj)
      67             :     {
      68      152184 :         READWRITEAS(CAddress, obj);
      69      152184 :         READWRITE(obj.source, Using<ChronoFormatter<int64_t>>(obj.m_last_success), obj.nAttempts);
      70      152184 :     }
      71             : 
      72      209232 :     AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
      73       69744 :     {
      74      139488 :     }
      75             : 
      76      318258 :     AddrInfo() : CAddress(), source()
      77      106086 :     {
      78      212172 :     }
      79             : 
      80             :     //! Calculate in which "tried" bucket this entry belongs
      81             :     int GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const;
      82             : 
      83             :     //! Calculate in which "new" bucket this entry belongs, given a certain source
      84             :     int GetNewBucket(const uint256& nKey, const CNetAddr& src, const NetGroupManager& netgroupman) const;
      85             : 
      86             :     //! Calculate in which "new" bucket this entry belongs, using its default source
      87        3755 :     int GetNewBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
      88             :     {
      89        3755 :         return GetNewBucket(nKey, source, netgroupman);
      90             :     }
      91             : 
      92             :     //! Calculate in which position of a bucket to store this entry.
      93             :     int GetBucketPosition(const uint256 &nKey, bool fNew, int bucket) const;
      94             : 
      95             :     //! Determine whether the statistics about this entry are bad enough so that it can just be deleted
      96             :     bool IsTerrible(NodeSeconds now = Now<NodeSeconds>()) const;
      97             : 
      98             :     //! Calculate the relative chance this entry should be given when selecting nodes to connect to
      99             :     double GetChance(NodeSeconds now = Now<NodeSeconds>()) const;
     100             : };
     101             : 
     102             : class AddrManImpl
     103             : {
     104             : public:
     105             :     AddrManImpl(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio);
     106             : 
     107             :     ~AddrManImpl();
     108             : 
     109             :     template <typename Stream>
     110             :     void Serialize(Stream& s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
     111             : 
     112             :     template <typename Stream>
     113             :     void Unserialize(Stream& s_) EXCLUSIVE_LOCKS_REQUIRED(!cs);
     114             : 
     115             :     size_t Size(std::optional<Network> net, std::optional<bool> in_new) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
     116             : 
     117             :     bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
     118             :         EXCLUSIVE_LOCKS_REQUIRED(!cs);
     119             : 
     120             :     bool Good(const CService& addr, NodeSeconds time)
     121             :         EXCLUSIVE_LOCKS_REQUIRED(!cs);
     122             : 
     123             :     void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time)
     124             :         EXCLUSIVE_LOCKS_REQUIRED(!cs);
     125             : 
     126             :     void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs);
     127             : 
     128             :     std::pair<CAddress, NodeSeconds> SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs);
     129             : 
     130             :     std::pair<CAddress, NodeSeconds> Select(bool new_only, std::optional<Network> network) const
     131             :         EXCLUSIVE_LOCKS_REQUIRED(!cs);
     132             : 
     133             :     std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const
     134             :         EXCLUSIVE_LOCKS_REQUIRED(!cs);
     135             : 
     136             :     void Connected(const CService& addr, NodeSeconds time)
     137             :         EXCLUSIVE_LOCKS_REQUIRED(!cs);
     138             : 
     139             :     void SetServices(const CService& addr, ServiceFlags nServices)
     140             :         EXCLUSIVE_LOCKS_REQUIRED(!cs);
     141             : 
     142             :     std::optional<AddressPosition> FindAddressEntry(const CAddress& addr)
     143             :         EXCLUSIVE_LOCKS_REQUIRED(!cs);
     144             : 
     145             :     AddrInfo GetAddressInfo(const CService& addr)
     146             :         EXCLUSIVE_LOCKS_REQUIRED(!cs);
     147             : 
     148             :     friend class AddrManDeterministic;
     149             : 
     150             : private:
     151             :     //! A mutex to protect the inner data structures.
     152             :     mutable Mutex cs;
     153             : 
     154             :     //! Source of random numbers for randomization in inner loops
     155             :     mutable FastRandomContext insecure_rand GUARDED_BY(cs);
     156             : 
     157             :     //! secret key to randomize bucket select with
     158             :     uint256 nKey;
     159             : 
     160             :     //! Serialization versions.
     161             :     enum Format : uint8_t {
     162             :         V0_HISTORICAL = 0,    //!< historic format, before commit e6b343d88
     163             :         V1_DETERMINISTIC = 1, //!< for pre-asmap files
     164             :         V2_ASMAP = 2,         //!< for files including asmap version
     165             :         V3_BIP155 = 3,        //!< same as V2_ASMAP plus addresses are in BIP155 format
     166             :         V4_MULTIPORT = 4,     //!< adds support for multiple ports per IP
     167             :     };
     168             : 
     169             :     //! The maximum format this software knows it can unserialize. Also, we always serialize
     170             :     //! in this format.
     171             :     //! The format (first byte in the serialized stream) can be higher than this and
     172             :     //! still this software may be able to unserialize the file - if the second byte
     173             :     //! (see `lowest_compatible` in `Unserialize()`) is less or equal to this.
     174             :     static constexpr Format FILE_FORMAT = Format::V4_MULTIPORT;
     175             : 
     176             :     //! The initial value of a field that is incremented every time an incompatible format
     177             :     //! change is made (such that old software versions would not be able to parse and
     178             :     //! understand the new file format). This is 32 because we overtook the "key size"
     179             :     //! field which was 32 historically.
     180             :     //! @note Don't increment this. Increment `lowest_compatible` in `Serialize()` instead.
     181             :     static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
     182             : 
     183             :     //! last used nId
     184             :     int nIdCount GUARDED_BY(cs){0};
     185             : 
     186             :     //! table with information about all nIds
     187             :     std::unordered_map<int, AddrInfo> mapInfo GUARDED_BY(cs);
     188             : 
     189             :     //! find an nId based on its network address and port.
     190             :     std::unordered_map<CService, int, CServiceHash> mapAddr GUARDED_BY(cs);
     191             : 
     192             :     //! randomly-ordered vector of all nIds
     193             :     //! This is mutable because it is unobservable outside the class, so any
     194             :     //! changes to it (even in const methods) are also unobservable.
     195             :     mutable std::vector<int> vRandom GUARDED_BY(cs);
     196             : 
     197             :     // number of "tried" entries
     198             :     int nTried GUARDED_BY(cs){0};
     199             : 
     200             :     //! list of "tried" buckets
     201             :     int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
     202             : 
     203             :     //! number of (unique) "new" entries
     204             :     int nNew GUARDED_BY(cs){0};
     205             : 
     206             :     //! list of "new" buckets
     207             :     int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
     208             : 
     209             :     //! last time Good was called (memory only). Initially set to 1 so that "never" is strictly worse.
     210             :     NodeSeconds m_last_good GUARDED_BY(cs){1s};
     211             : 
     212             :     //! Holds addrs inserted into tried table that collide with existing entries. Test-before-evict discipline used to resolve these collisions.
     213             :     std::set<int> m_tried_collisions;
     214             : 
     215             :     /** Perform consistency checks every m_consistency_check_ratio operations (if non-zero). */
     216             :     const int32_t m_consistency_check_ratio;
     217             : 
     218             :     /** Reference to the netgroup manager. netgroupman must be constructed before addrman and destructed after. */
     219             :     const NetGroupManager& m_netgroupman;
     220             : 
     221             :     struct NewTriedCount {
     222             :         size_t n_new;
     223             :         size_t n_tried;
     224             :     };
     225             : 
     226             :     /** Number of entries in addrman per network and new/tried table. */
     227             :     std::unordered_map<Network, NewTriedCount> m_network_counts GUARDED_BY(cs);
     228             : 
     229             :     //! Find an entry.
     230             :     AddrInfo* Find(const CService& addr, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
     231             : 
     232             :     //! Create a new entry and add it to the internal data structures mapInfo, mapAddr and vRandom.
     233             :     AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
     234             : 
     235             :     //! Swap two elements in vRandom.
     236             :     void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs);
     237             : 
     238             :     //! Delete an entry. It must not be in tried, and have refcount 0.
     239             :     void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
     240             : 
     241             :     //! Clear a position in a "new" table. This is the only place where entries are actually deleted.
     242             :     void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
     243             : 
     244             :     //! Move an entry from the "new" table(s) to the "tried" table
     245             :     void MakeTried(AddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
     246             : 
     247             :     /** Attempt to add a single address to addrman's new table.
     248             :      *  @see AddrMan::Add() for parameters. */
     249             :     bool AddSingle(const CAddress& addr, const CNetAddr& source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
     250             : 
     251             :     bool Good_(const CService& addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
     252             : 
     253             :     bool Add_(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
     254             : 
     255             :     void Attempt_(const CService& addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
     256             : 
     257             :     std::pair<CAddress, NodeSeconds> Select_(bool new_only, std::optional<Network> network) const EXCLUSIVE_LOCKS_REQUIRED(cs);
     258             : 
     259             :     /** Helper to generalize looking up an addrman entry from either table.
     260             :      *
     261             :      *  @return  int The nid of the entry. If the addrman position is empty or not found, returns -1.
     262             :      * */
     263             :     int GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
     264             : 
     265             :     std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
     266             : 
     267             :     void Connected_(const CService& addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
     268             : 
     269             :     void SetServices_(const CService& addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
     270             : 
     271             :     AddrInfo GetAddressInfo_(const CService& addr) EXCLUSIVE_LOCKS_REQUIRED(cs);
     272             : 
     273             :     void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs);
     274             : 
     275             :     std::pair<CAddress, NodeSeconds> SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs);
     276             : 
     277             :     std::optional<AddressPosition> FindAddressEntry_(const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(cs);
     278             : 
     279             :     size_t Size_(std::optional<Network> net, std::optional<bool> in_new) const EXCLUSIVE_LOCKS_REQUIRED(cs);
     280             : 
     281             :     //! Consistency check, taking into account m_consistency_check_ratio.
     282             :     //! Will std::abort if an inconsistency is detected.
     283             :     void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
     284             : 
     285             :     //! Perform consistency check, regardless of m_consistency_check_ratio.
     286             :     //! @returns an error code or zero.
     287             :     int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs);
     288             : };
     289             : 
     290             : #endif // BITCOIN_ADDRMAN_IMPL_H

Generated by: LCOV version 1.16