Branch data Line data Source code
1 : : // Copyright (c) 2012 Pieter Wuille 2 : : // Copyright (c) 2012-2022 The Bitcoin Core developers 3 : : // Distributed under the MIT software license, see the accompanying 4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : : 6 : : #ifndef BITCOIN_ADDRMAN_H 7 : : #define BITCOIN_ADDRMAN_H 8 : : 9 : : #include <netaddress.h> 10 : : #include <netgroup.h> 11 : : #include <protocol.h> 12 : : #include <streams.h> 13 : : #include <util/time.h> 14 : : 15 : : #include <cstdint> 16 : : #include <memory> 17 : : #include <optional> 18 : : #include <utility> 19 : : #include <vector> 20 : : 21 : 0 : class InvalidAddrManVersionError : public std::ios_base::failure 22 : : { 23 : : public: 24 : 0 : InvalidAddrManVersionError(std::string msg) : std::ios_base::failure(msg) { } 25 : : }; 26 : : 27 : : class AddrManImpl; 28 : : class AddrInfo; 29 : : 30 : : /** Default for -checkaddrman */ 31 : : static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0}; 32 : : 33 : : /** Location information for an address in AddrMan */ 34 : : struct AddressPosition { 35 : : // Whether the address is in the new or tried table 36 : : const bool tried; 37 : : 38 : : // Addresses in the tried table should always have a multiplicity of 1. 39 : : // Addresses in the new table can have multiplicity between 1 and 40 : : // ADDRMAN_NEW_BUCKETS_PER_ADDRESS 41 : : const int multiplicity; 42 : : 43 : : // If the address is in the new table, the bucket and position are 44 : : // populated based on the first source who sent the address. 45 : : // In certain edge cases, this may not be where the address is currently 46 : : // located. 47 : : const int bucket; 48 : : const int position; 49 : : 50 : : bool operator==(AddressPosition other) { 51 : : return std::tie(tried, multiplicity, bucket, position) == 52 : : std::tie(other.tried, other.multiplicity, other.bucket, other.position); 53 : : } 54 : 0 : explicit AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in) 55 : 0 : : tried{tried_in}, multiplicity{multiplicity_in}, bucket{bucket_in}, position{position_in} {} 56 : : }; 57 : : 58 : : /** Stochastic address manager 59 : : * 60 : : * Design goals: 61 : : * * Keep the address tables in-memory, and asynchronously dump the entire table to peers.dat. 62 : : * * Make sure no (localized) attacker can fill the entire table with his nodes/addresses. 63 : : * 64 : : * To that end: 65 : : * * Addresses are organized into buckets that can each store up to 64 entries. 66 : : * * Addresses to which our node has not successfully connected go into 1024 "new" buckets. 67 : : * * Based on the address range (/16 for IPv4) of the source of information, or if an asmap is provided, 68 : : * the AS it belongs to (for IPv4/IPv6), 64 buckets are selected at random. 69 : : * * The actual bucket is chosen from one of these, based on the range in which the address itself is located. 70 : : * * The position in the bucket is chosen based on the full address. 71 : : * * One single address can occur in up to 8 different buckets to increase selection chances for addresses that 72 : : * are seen frequently. The chance for increasing this multiplicity decreases exponentially. 73 : : * * When adding a new address to an occupied position of a bucket, it will not replace the existing entry 74 : : * unless that address is also stored in another bucket or it doesn't meet one of several quality criteria 75 : : * (see IsTerrible for exact criteria). 76 : : * * Addresses of nodes that are known to be accessible go into 256 "tried" buckets. 77 : : * * Each address range selects at random 8 of these buckets. 78 : : * * The actual bucket is chosen from one of these, based on the full address. 79 : : * * When adding a new good address to an occupied position of a bucket, a FEELER connection to the 80 : : * old address is attempted. The old entry is only replaced and moved back to the "new" buckets if this 81 : : * attempt was unsuccessful. 82 : : * * Bucket selection is based on cryptographic hashing, using a randomly-generated 256-bit key, which should not 83 : : * be observable by adversaries. 84 : : * * Several indexes are kept for high performance. Setting m_consistency_check_ratio with the -checkaddrman 85 : : * configuration option will introduce (expensive) consistency checks for the entire data structure. 86 : : */ 87 : : class AddrMan 88 : : { 89 : : protected: 90 : : const std::unique_ptr<AddrManImpl> m_impl; 91 : : 92 : : public: 93 : : explicit AddrMan(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio); 94 : : 95 : : ~AddrMan(); 96 : : 97 : : template <typename Stream> 98 : : void Serialize(Stream& s_) const; 99 : : 100 : : template <typename Stream> 101 : : void Unserialize(Stream& s_); 102 : : 103 : : /** 104 : : * Return size information about addrman. 105 : : * 106 : : * @param[in] net Select addresses only from specified network (nullopt = all) 107 : : * @param[in] in_new Select addresses only from one table (true = new, false = tried, nullopt = both) 108 : : * @return Number of unique addresses that match specified options. 109 : : */ 110 : : size_t Size(std::optional<Network> net = std::nullopt, std::optional<bool> in_new = std::nullopt) const; 111 : : 112 : : /** 113 : : * Attempt to add one or more addresses to addrman's new table. 114 : : * 115 : : * @param[in] vAddr Address records to attempt to add. 116 : : * @param[in] source The address of the node that sent us these addr records. 117 : : * @param[in] time_penalty A "time penalty" to apply to the address record's nTime. If a peer 118 : : * sends us an address record with nTime=n, then we'll add it to our 119 : : * addrman with nTime=(n - time_penalty). 120 : : * @return true if at least one address is successfully added. */ 121 : : bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty = 0s); 122 : : 123 : : /** 124 : : * Mark an address record as accessible and attempt to move it to addrman's tried table. 125 : : * 126 : : * @param[in] addr Address record to attempt to move to tried table. 127 : : * @param[in] time The time that we were last connected to this peer. 128 : : * @return true if the address is successfully moved from the new table to the tried table. 129 : : */ 130 : : bool Good(const CService& addr, NodeSeconds time = Now<NodeSeconds>()); 131 : : 132 : : //! Mark an entry as connection attempted to. 133 : : void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = Now<NodeSeconds>()); 134 : : 135 : : //! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions. 136 : : void ResolveCollisions(); 137 : : 138 : : /** 139 : : * Randomly select an address in the tried table that another address is 140 : : * attempting to evict. 141 : : * 142 : : * @return CAddress The record for the selected tried peer. 143 : : * seconds The last time we attempted to connect to that peer. 144 : : */ 145 : : std::pair<CAddress, NodeSeconds> SelectTriedCollision(); 146 : : 147 : : /** 148 : : * Choose an address to connect to. 149 : : * 150 : : * @param[in] new_only Whether to only select addresses from the new table. Passing `true` returns 151 : : * an address from the new table or an empty pair. Passing `false` will return an 152 : : * empty pair or an address from either the new or tried table (it does not 153 : : * guarantee a tried entry). 154 : : * @param[in] network Select only addresses of this network (nullopt = all). Passing a network may 155 : : * slow down the search. 156 : : * @return CAddress The record for the selected peer. 157 : : * seconds The last time we attempted to connect to that peer. 158 : : */ 159 : : std::pair<CAddress, NodeSeconds> Select(bool new_only = false, std::optional<Network> network = std::nullopt) const; 160 : : 161 : : /** 162 : : * Return all or many randomly selected addresses, optionally by network. 163 : : * 164 : : * @param[in] max_addresses Maximum number of addresses to return (0 = all). 165 : : * @param[in] max_pct Maximum percentage of addresses to return (0 = all). 166 : : * @param[in] network Select only addresses of this network (nullopt = all). 167 : : * 168 : : * @return A vector of randomly selected addresses from vRandom. 169 : : */ 170 : : std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const; 171 : : 172 : : /** 173 : : * Returns an information-location pair for all addresses in the selected addrman table. 174 : : * If an address appears multiple times in the new table, an information-location pair 175 : : * is returned for each occurence. Addresses only ever appear once in the tried table. 176 : : * 177 : : * @param[in] from_tried Selects which table to return entries from. 178 : : * 179 : : * @return A vector consisting of pairs of AddrInfo and AddressPosition. 180 : : */ 181 : : std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries(bool from_tried) const; 182 : : 183 : : /** We have successfully connected to this peer. Calling this function 184 : : * updates the CAddress's nTime, which is used in our IsTerrible() 185 : : * decisions and gossiped to peers. Callers should be careful that updating 186 : : * this information doesn't leak topology information to network spies. 187 : : * 188 : : * net_processing calls this function when it *disconnects* from a peer to 189 : : * not leak information about currently connected peers. 190 : : * 191 : : * @param[in] addr The address of the peer we were connected to 192 : : * @param[in] time The time that we were last connected to this peer 193 : : */ 194 : : void Connected(const CService& addr, NodeSeconds time = Now<NodeSeconds>()); 195 : : 196 : : //! Update an entry's service bits. 197 : : void SetServices(const CService& addr, ServiceFlags nServices); 198 : : 199 : : /** Test-only function 200 : : * Find the address record in AddrMan and return information about its 201 : : * position. 202 : : * @param[in] addr The address record to look up. 203 : : * @return Information about the address record in AddrMan 204 : : * or nullopt if address is not found. 205 : : */ 206 : : std::optional<AddressPosition> FindAddressEntry(const CAddress& addr); 207 : : }; 208 : : 209 : : #endif // BITCOIN_ADDRMAN_H