2012-01-04 23:39:45 +01:00
|
|
|
// Copyright (c) 2012 Pieter Wuille
|
2021-12-30 19:36:57 +02:00
|
|
|
// Copyright (c) 2012-2021 The Bitcoin Core developers
|
2014-10-24 12:04:27 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 22:02:28 +08:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#ifndef BITCOIN_ADDRMAN_H
|
|
|
|
#define BITCOIN_ADDRMAN_H
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <netaddress.h>
|
2021-08-31 18:40:18 +01:00
|
|
|
#include <netgroup.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <protocol.h>
|
2021-09-10 16:37:41 -06:00
|
|
|
#include <streams.h>
|
2022-03-28 14:20:04 +02:00
|
|
|
#include <util/time.h>
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2021-09-07 10:48:45 +02:00
|
|
|
#include <cstdint>
|
2021-09-10 16:37:41 -06:00
|
|
|
#include <memory>
|
2021-05-02 18:44:17 +02:00
|
|
|
#include <optional>
|
2021-09-10 16:37:41 -06:00
|
|
|
#include <utility>
|
2019-12-29 18:54:33 +01:00
|
|
|
#include <vector>
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2022-01-29 21:58:59 +09:00
|
|
|
class InvalidAddrManVersionError : public std::ios_base::failure
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InvalidAddrManVersionError(std::string msg) : std::ios_base::failure(msg) { }
|
|
|
|
};
|
|
|
|
|
2021-09-01 11:21:29 -07:00
|
|
|
class AddrManImpl;
|
|
|
|
|
2020-10-23 22:03:24 +01:00
|
|
|
/** Default for -checkaddrman */
|
|
|
|
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
|
|
|
|
|
2021-12-09 18:25:22 +00:00
|
|
|
/** Test-only struct, capturing info about an address in AddrMan */
|
|
|
|
struct AddressPosition {
|
|
|
|
// Whether the address is in the new or tried table
|
|
|
|
const bool tried;
|
|
|
|
|
|
|
|
// Addresses in the tried table should always have a multiplicity of 1.
|
|
|
|
// Addresses in the new table can have multiplicity between 1 and
|
|
|
|
// ADDRMAN_NEW_BUCKETS_PER_ADDRESS
|
|
|
|
const int multiplicity;
|
|
|
|
|
|
|
|
// If the address is in the new table, the bucket and position are
|
|
|
|
// populated based on the first source who sent the address.
|
|
|
|
// In certain edge cases, this may not be where the address is currently
|
|
|
|
// located.
|
|
|
|
const int bucket;
|
|
|
|
const int position;
|
|
|
|
|
|
|
|
bool operator==(AddressPosition other) {
|
|
|
|
return std::tie(tried, multiplicity, bucket, position) ==
|
|
|
|
std::tie(other.tried, other.multiplicity, other.bucket, other.position);
|
|
|
|
}
|
|
|
|
explicit AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
|
|
|
|
: tried{tried_in}, multiplicity{multiplicity_in}, bucket{bucket_in}, position{position_in} {}
|
|
|
|
};
|
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
/** Stochastic address manager
|
|
|
|
*
|
|
|
|
* Design goals:
|
2015-04-28 14:47:17 +00:00
|
|
|
* * Keep the address tables in-memory, and asynchronously dump the entire table to peers.dat.
|
2014-10-24 12:04:27 +08:00
|
|
|
* * Make sure no (localized) attacker can fill the entire table with his nodes/addresses.
|
|
|
|
*
|
|
|
|
* To that end:
|
2021-07-28 23:48:34 +02:00
|
|
|
* * Addresses are organized into buckets that can each store up to 64 entries.
|
|
|
|
* * Addresses to which our node has not successfully connected go into 1024 "new" buckets.
|
|
|
|
* * Based on the address range (/16 for IPv4) of the source of information, or if an asmap is provided,
|
|
|
|
* the AS it belongs to (for IPv4/IPv6), 64 buckets are selected at random.
|
2015-04-28 14:48:28 +00:00
|
|
|
* * The actual bucket is chosen from one of these, based on the range in which the address itself is located.
|
2021-07-28 23:48:34 +02:00
|
|
|
* * The position in the bucket is chosen based on the full address.
|
2015-04-22 09:02:01 -04:00
|
|
|
* * One single address can occur in up to 8 different buckets to increase selection chances for addresses that
|
2014-10-24 12:04:27 +08:00
|
|
|
* are seen frequently. The chance for increasing this multiplicity decreases exponentially.
|
2021-07-28 23:48:34 +02:00
|
|
|
* * When adding a new address to an occupied position of a bucket, it will not replace the existing entry
|
|
|
|
* unless that address is also stored in another bucket or it doesn't meet one of several quality criteria
|
|
|
|
* (see IsTerrible for exact criteria).
|
2015-03-19 10:01:57 -07:00
|
|
|
* * Addresses of nodes that are known to be accessible go into 256 "tried" buckets.
|
|
|
|
* * Each address range selects at random 8 of these buckets.
|
2014-10-24 12:04:27 +08:00
|
|
|
* * The actual bucket is chosen from one of these, based on the full address.
|
2021-07-28 23:48:34 +02:00
|
|
|
* * When adding a new good address to an occupied position of a bucket, a FEELER connection to the
|
|
|
|
* old address is attempted. The old entry is only replaced and moved back to the "new" buckets if this
|
|
|
|
* attempt was unsuccessful.
|
2014-10-24 12:04:27 +08:00
|
|
|
* * Bucket selection is based on cryptographic hashing, using a randomly-generated 256-bit key, which should not
|
|
|
|
* be observable by adversaries.
|
2020-10-23 22:03:24 +01:00
|
|
|
* * Several indexes are kept for high performance. Setting m_consistency_check_ratio with the -checkaddrman
|
|
|
|
* configuration option will introduce (expensive) consistency checks for the entire data structure.
|
2014-10-24 12:04:27 +08:00
|
|
|
*/
|
2021-09-10 18:16:37 -06:00
|
|
|
class AddrMan
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
2021-11-01 15:32:19 +00:00
|
|
|
protected:
|
2021-09-01 11:21:29 -07:00
|
|
|
const std::unique_ptr<AddrManImpl> m_impl;
|
|
|
|
|
2012-01-04 23:39:45 +01:00
|
|
|
public:
|
2021-08-31 18:40:18 +01:00
|
|
|
explicit AddrMan(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio);
|
2021-09-28 15:46:43 -04:00
|
|
|
|
2021-09-10 18:16:37 -06:00
|
|
|
~AddrMan();
|
2021-09-28 15:46:43 -04:00
|
|
|
|
2020-05-19 17:39:05 +02:00
|
|
|
template <typename Stream>
|
2021-09-01 11:21:29 -07:00
|
|
|
void Serialize(Stream& s_) const;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2020-05-19 17:39:05 +02:00
|
|
|
template <typename Stream>
|
2021-09-01 11:21:29 -07:00
|
|
|
void Unserialize(Stream& s_);
|
2014-07-10 20:16:58 +02:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! Return the number of (unique) addresses in all tables.
|
2021-09-01 11:21:29 -07:00
|
|
|
size_t size() const;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2021-10-28 12:46:26 +01:00
|
|
|
/**
|
|
|
|
* Attempt to add one or more addresses to addrman's new table.
|
|
|
|
*
|
|
|
|
* @param[in] vAddr Address records to attempt to add.
|
|
|
|
* @param[in] source The address of the node that sent us these addr records.
|
2022-03-28 11:41:20 +02:00
|
|
|
* @param[in] time_penalty A "time penalty" to apply to the address record's nTime. If a peer
|
2021-10-28 12:46:26 +01:00
|
|
|
* sends us an address record with nTime=n, then we'll add it to our
|
2022-03-28 11:41:20 +02:00
|
|
|
* addrman with nTime=(n - time_penalty).
|
2021-10-01 17:26:08 +01:00
|
|
|
* @return true if at least one address is successfully added. */
|
2022-03-28 14:20:04 +02:00
|
|
|
bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty = 0s);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2021-12-10 15:37:04 +01:00
|
|
|
/**
|
|
|
|
* Mark an address record as accessible and attempt to move it to addrman's tried table.
|
|
|
|
*
|
|
|
|
* @param[in] addr Address record to attempt to move to tried table.
|
2022-03-28 14:20:04 +02:00
|
|
|
* @param[in] time The time that we were last connected to this peer.
|
2021-12-10 15:37:04 +01:00
|
|
|
* @return true if the address is successfully moved from the new table to the tried table.
|
|
|
|
*/
|
2022-03-24 19:56:00 +01:00
|
|
|
bool Good(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! Mark an entry as connection attempted to.
|
2022-03-24 19:56:00 +01:00
|
|
|
void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = Now<NodeSeconds>());
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2016-10-27 13:55:39 -04:00
|
|
|
//! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
|
2021-09-01 11:21:29 -07:00
|
|
|
void ResolveCollisions();
|
2016-10-27 13:55:39 -04:00
|
|
|
|
2021-08-25 15:40:59 -07:00
|
|
|
/**
|
|
|
|
* Randomly select an address in the tried table that another address is
|
|
|
|
* attempting to evict.
|
|
|
|
*
|
|
|
|
* @return CAddress The record for the selected tried peer.
|
2022-03-28 14:20:04 +02:00
|
|
|
* seconds The last time we attempted to connect to that peer.
|
2021-08-25 15:40:59 -07:00
|
|
|
*/
|
2022-03-28 14:20:04 +02:00
|
|
|
std::pair<CAddress, NodeSeconds> SelectTriedCollision();
|
2016-10-27 13:55:39 -04:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
/**
|
|
|
|
* Choose an address to connect to.
|
2021-08-25 15:40:59 -07:00
|
|
|
*
|
|
|
|
* @param[in] newOnly Whether to only select addresses from the new table.
|
|
|
|
* @return CAddress The record for the selected peer.
|
2022-03-28 14:20:04 +02:00
|
|
|
* seconds The last time we attempted to connect to that peer.
|
2014-10-24 12:04:27 +08:00
|
|
|
*/
|
2022-03-28 14:20:04 +02:00
|
|
|
std::pair<CAddress, NodeSeconds> Select(bool newOnly = false) const;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2021-05-02 19:03:49 +02:00
|
|
|
/**
|
|
|
|
* Return all or many randomly selected addresses, optionally by network.
|
|
|
|
*
|
|
|
|
* @param[in] max_addresses Maximum number of addresses to return (0 = all).
|
|
|
|
* @param[in] max_pct Maximum percentage of addresses to return (0 = all).
|
|
|
|
* @param[in] network Select only addresses of this network (nullopt = all).
|
2021-09-03 17:27:11 -07:00
|
|
|
*
|
|
|
|
* @return A vector of randomly selected addresses from vRandom.
|
2021-05-02 19:03:49 +02:00
|
|
|
*/
|
2021-09-01 11:21:29 -07:00
|
|
|
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2021-09-03 17:27:11 -07:00
|
|
|
/** We have successfully connected to this peer. Calling this function
|
|
|
|
* updates the CAddress's nTime, which is used in our IsTerrible()
|
|
|
|
* decisions and gossiped to peers. Callers should be careful that updating
|
|
|
|
* this information doesn't leak topology information to network spies.
|
|
|
|
*
|
|
|
|
* net_processing calls this function when it *disconnects* from a peer to
|
|
|
|
* not leak information about currently connected peers.
|
|
|
|
*
|
|
|
|
* @param[in] addr The address of the peer we were connected to
|
2022-03-28 14:20:04 +02:00
|
|
|
* @param[in] time The time that we were last connected to this peer
|
2021-09-03 17:27:11 -07:00
|
|
|
*/
|
2022-03-24 19:56:00 +01:00
|
|
|
void Connected(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
|
2015-09-22 15:24:16 -04:00
|
|
|
|
2021-09-03 17:27:11 -07:00
|
|
|
//! Update an entry's service bits.
|
2021-09-10 18:53:57 -06:00
|
|
|
void SetServices(const CService& addr, ServiceFlags nServices);
|
2016-03-26 18:58:00 +01:00
|
|
|
|
2021-12-09 18:25:22 +00:00
|
|
|
/** Test-only function
|
|
|
|
* Find the address record in AddrMan and return information about its
|
|
|
|
* position.
|
|
|
|
* @param[in] addr The address record to look up.
|
|
|
|
* @return Information about the address record in AddrMan
|
|
|
|
* or nullopt if address is not found.
|
|
|
|
*/
|
|
|
|
std::optional<AddressPosition> FindAddressEntry(const CAddress& addr);
|
2012-01-04 23:39:45 +01:00
|
|
|
};
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#endif // BITCOIN_ADDRMAN_H
|