Coverage Report

Created: 2024-08-21 05:08

/workdir/bitcoin/src/addrman.h
Line
Count
Source (jump to first uncovered line)
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
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
0
    bool operator==(AddressPosition other) {
51
0
        return std::tie(tried, multiplicity, bucket, position) ==
52
0
               std::tie(other.tried, other.multiplicity, other.bucket, other.position);
53
0
    }
54
    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
     * If an address already exists in addrman, the existing entry may be updated
115
     * (e.g. adding additional service flags). If the existing entry is in the new table,
116
     * it may be added to more buckets, improving the probability of selection.
117
     *
118
     * @param[in] vAddr           Address records to attempt to add.
119
     * @param[in] source          The address of the node that sent us these addr records.
120
     * @param[in] time_penalty    A "time penalty" to apply to the address record's nTime. If a peer
121
     *                            sends us an address record with nTime=n, then we'll add it to our
122
     *                            addrman with nTime=(n - time_penalty).
123
     * @return    true if at least one address is successfully added, or added to an additional bucket. Unaffected by updates. */
124
    bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty = 0s);
125
126
    /**
127
     * Mark an address record as accessible and attempt to move it to addrman's tried table.
128
     *
129
     * @param[in] addr            Address record to attempt to move to tried table.
130
     * @param[in] time            The time that we were last connected to this peer.
131
     * @return    true if the address is successfully moved from the new table to the tried table.
132
     */
133
    bool Good(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
134
135
    //! Mark an entry as connection attempted to.
136
    void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = Now<NodeSeconds>());
137
138
    //! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
139
    void ResolveCollisions();
140
141
    /**
142
     * Randomly select an address in the tried table that another address is
143
     * attempting to evict.
144
     *
145
     * @return CAddress The record for the selected tried peer.
146
     *         seconds  The last time we attempted to connect to that peer.
147
     */
148
    std::pair<CAddress, NodeSeconds> SelectTriedCollision();
149
150
    /**
151
     * Choose an address to connect to.
152
     *
153
     * @param[in] new_only Whether to only select addresses from the new table. Passing `true` returns
154
     *                     an address from the new table or an empty pair. Passing `false` will return an
155
     *                     empty pair or an address from either the new or tried table (it does not
156
     *                     guarantee a tried entry).
157
     * @param[in] network  Select only addresses of this network (nullopt = all). Passing a network may
158
     *                     slow down the search.
159
     * @return    CAddress The record for the selected peer.
160
     *            seconds  The last time we attempted to connect to that peer.
161
     */
162
    std::pair<CAddress, NodeSeconds> Select(bool new_only = false, std::optional<Network> network = std::nullopt) const;
163
164
    /**
165
     * Return all or many randomly selected addresses, optionally by network.
166
     *
167
     * @param[in] max_addresses  Maximum number of addresses to return (0 = all).
168
     * @param[in] max_pct        Maximum percentage of addresses to return (0 = all).
169
     * @param[in] network        Select only addresses of this network (nullopt = all).
170
     * @param[in] filtered       Select only addresses that are considered good quality (false = all).
171
     *
172
     * @return                   A vector of randomly selected addresses from vRandom.
173
     */
174
    std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const;
175
176
    /**
177
     * Returns an information-location pair for all addresses in the selected addrman table.
178
     * If an address appears multiple times in the new table, an information-location pair
179
     * is returned for each occurrence. Addresses only ever appear once in the tried table.
180
     *
181
     * @param[in] from_tried     Selects which table to return entries from.
182
     *
183
     * @return                   A vector consisting of pairs of AddrInfo and AddressPosition.
184
     */
185
    std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries(bool from_tried) const;
186
187
    /** We have successfully connected to this peer. Calling this function
188
     *  updates the CAddress's nTime, which is used in our IsTerrible()
189
     *  decisions and gossiped to peers. Callers should be careful that updating
190
     *  this information doesn't leak topology information to network spies.
191
     *
192
     *  net_processing calls this function when it *disconnects* from a peer to
193
     *  not leak information about currently connected peers.
194
     *
195
     * @param[in]   addr     The address of the peer we were connected to
196
     * @param[in]   time     The time that we were last connected to this peer
197
     */
198
    void Connected(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
199
200
    //! Update an entry's service bits.
201
    void SetServices(const CService& addr, ServiceFlags nServices);
202
203
    /** Test-only function
204
     * Find the address record in AddrMan and return information about its
205
     * position.
206
     * @param[in] addr       The address record to look up.
207
     * @return               Information about the address record in AddrMan
208
     *                       or nullopt if address is not found.
209
     */
210
    std::optional<AddressPosition> FindAddressEntry(const CAddress& addr);
211
};
212
213
#endif // BITCOIN_ADDRMAN_H