LCOV - code coverage report
Current view: top level - src - addrman_impl.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 10 10
Test Date: 2024-08-28 04:44:32 Functions: 62.5 % 8 5
Branches: 43.2 % 44 19

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

Generated by: LCOV version 2.0-1