2012-01-04 19:39:45 -03:00
|
|
|
// Copyright (c) 2012 Pieter Wuille
|
2020-01-14 16:17:38 -03:00
|
|
|
// Copyright (c) 2012-2020 The Bitcoin Core developers
|
2014-10-24 01:04:27 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 10:02:28 -04:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 02:13:08 -03:00
|
|
|
|
2014-11-03 12:16:40 -03:00
|
|
|
#ifndef BITCOIN_ADDRMAN_H
|
|
|
|
#define BITCOIN_ADDRMAN_H
|
2012-01-04 19:39:45 -03:00
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <netaddress.h>
|
|
|
|
#include <protocol.h>
|
2021-09-10 19:37:41 -03:00
|
|
|
#include <streams.h>
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <timedata.h>
|
2012-01-04 19:39:45 -03:00
|
|
|
|
2021-09-07 05:48:45 -03:00
|
|
|
#include <cstdint>
|
2021-09-10 19:37:41 -03:00
|
|
|
#include <memory>
|
2021-05-02 12:44:17 -04:00
|
|
|
#include <optional>
|
2021-09-10 19:37:41 -03:00
|
|
|
#include <utility>
|
2019-12-29 14:54:33 -03:00
|
|
|
#include <vector>
|
2012-01-04 19:39:45 -03:00
|
|
|
|
2021-09-01 14:21:29 -04:00
|
|
|
class AddrManImpl;
|
|
|
|
|
2020-10-23 18:03:24 -03:00
|
|
|
/** Default for -checkaddrman */
|
|
|
|
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
|
|
|
|
|
2014-10-24 01:04:27 -03:00
|
|
|
/** Stochastic address manager
|
|
|
|
*
|
|
|
|
* Design goals:
|
2015-04-28 11:47:17 -03:00
|
|
|
* * Keep the address tables in-memory, and asynchronously dump the entire table to peers.dat.
|
2014-10-24 01:04:27 -03:00
|
|
|
* * Make sure no (localized) attacker can fill the entire table with his nodes/addresses.
|
|
|
|
*
|
|
|
|
* To that end:
|
2021-07-28 17:48:34 -04: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 11:48:28 -03:00
|
|
|
* * The actual bucket is chosen from one of these, based on the range in which the address itself is located.
|
2021-07-28 17:48:34 -04:00
|
|
|
* * The position in the bucket is chosen based on the full address.
|
2015-04-22 10:02:01 -03:00
|
|
|
* * One single address can occur in up to 8 different buckets to increase selection chances for addresses that
|
2014-10-24 01:04:27 -03:00
|
|
|
* are seen frequently. The chance for increasing this multiplicity decreases exponentially.
|
2021-07-28 17:48:34 -04: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 14:01:57 -03: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 01:04:27 -03:00
|
|
|
* * The actual bucket is chosen from one of these, based on the full address.
|
2021-07-28 17:48:34 -04: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 01:04:27 -03: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 18:03:24 -03: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 01:04:27 -03:00
|
|
|
*/
|
2021-09-10 21:16:37 -03:00
|
|
|
class AddrMan
|
2012-01-04 19:39:45 -03:00
|
|
|
{
|
2021-11-01 12:32:19 -03:00
|
|
|
protected:
|
2021-09-01 14:21:29 -04:00
|
|
|
const std::unique_ptr<AddrManImpl> m_impl;
|
|
|
|
|
2012-01-04 19:39:45 -03:00
|
|
|
public:
|
2021-09-10 21:16:37 -03:00
|
|
|
explicit AddrMan(std::vector<bool> asmap, bool deterministic, int32_t consistency_check_ratio);
|
2021-09-28 16:46:43 -03:00
|
|
|
|
2021-09-10 21:16:37 -03:00
|
|
|
~AddrMan();
|
2021-09-28 16:46:43 -03:00
|
|
|
|
2020-05-19 11:39:05 -04:00
|
|
|
template <typename Stream>
|
2021-09-01 14:21:29 -04:00
|
|
|
void Serialize(Stream& s_) const;
|
2012-01-04 19:39:45 -03:00
|
|
|
|
2020-05-19 11:39:05 -04:00
|
|
|
template <typename Stream>
|
2021-09-01 14:21:29 -04:00
|
|
|
void Unserialize(Stream& s_);
|
2014-07-10 14:16:58 -04:00
|
|
|
|
2014-10-24 01:04:27 -03:00
|
|
|
//! Return the number of (unique) addresses in all tables.
|
2021-09-01 14:21:29 -04:00
|
|
|
size_t size() const;
|
2012-01-04 19:39:45 -03:00
|
|
|
|
2021-10-28 08:46:26 -03: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.
|
|
|
|
* @param[in] nTimePenalty A "time penalty" to apply to the address record's nTime. If a peer
|
|
|
|
* sends us an address record with nTime=n, then we'll add it to our
|
2021-10-01 13:26:08 -03:00
|
|
|
* addrman with nTime=(n - nTimePenalty).
|
|
|
|
* @return true if at least one address is successfully added. */
|
2021-09-10 21:53:57 -03:00
|
|
|
bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty = 0);
|
2012-01-04 19:39:45 -03:00
|
|
|
|
2021-12-10 11:37:04 -03: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.
|
|
|
|
* @param[in] nTime The time that we were last connected to this peer.
|
|
|
|
* @return true if the address is successfully moved from the new table to the tried table.
|
|
|
|
*/
|
|
|
|
bool Good(const CService& addr, int64_t nTime = GetAdjustedTime());
|
2012-01-04 19:39:45 -03:00
|
|
|
|
2014-10-24 01:04:27 -03:00
|
|
|
//! Mark an entry as connection attempted to.
|
2021-09-10 21:53:57 -03:00
|
|
|
void Attempt(const CService& addr, bool fCountFailure, int64_t nTime = GetAdjustedTime());
|
2012-01-04 19:39:45 -03:00
|
|
|
|
2016-10-27 14:55:39 -03:00
|
|
|
//! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
|
2021-09-01 14:21:29 -04:00
|
|
|
void ResolveCollisions();
|
2016-10-27 14:55:39 -03:00
|
|
|
|
2021-08-25 18:40:59 -04: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.
|
|
|
|
* int64_t The last time we attempted to connect to that peer.
|
|
|
|
*/
|
|
|
|
std::pair<CAddress, int64_t> SelectTriedCollision();
|
2016-10-27 14:55:39 -03:00
|
|
|
|
2014-10-24 01:04:27 -03:00
|
|
|
/**
|
|
|
|
* Choose an address to connect to.
|
2021-08-25 18:40:59 -04:00
|
|
|
*
|
|
|
|
* @param[in] newOnly Whether to only select addresses from the new table.
|
|
|
|
* @return CAddress The record for the selected peer.
|
|
|
|
* int64_t The last time we attempted to connect to that peer.
|
2014-10-24 01:04:27 -03:00
|
|
|
*/
|
2021-08-25 18:40:59 -04:00
|
|
|
std::pair<CAddress, int64_t> Select(bool newOnly = false) const;
|
2012-01-04 19:39:45 -03:00
|
|
|
|
2021-05-02 13:03:49 -04: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 20:27:11 -04:00
|
|
|
*
|
|
|
|
* @return A vector of randomly selected addresses from vRandom.
|
2021-05-02 13:03:49 -04:00
|
|
|
*/
|
2021-09-01 14:21:29 -04:00
|
|
|
std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const;
|
2012-01-04 19:39:45 -03:00
|
|
|
|
2021-09-03 20:27:11 -04: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
|
|
|
|
* @param[in] nTime The time that we were last connected to this peer
|
|
|
|
*/
|
2021-09-10 21:53:57 -03:00
|
|
|
void Connected(const CService& addr, int64_t nTime = GetAdjustedTime());
|
2015-09-22 16:24:16 -03:00
|
|
|
|
2021-09-03 20:27:11 -04:00
|
|
|
//! Update an entry's service bits.
|
2021-09-10 21:53:57 -03:00
|
|
|
void SetServices(const CService& addr, ServiceFlags nServices);
|
2016-03-26 14:58:00 -03:00
|
|
|
|
2021-09-24 17:14:39 -03:00
|
|
|
const std::vector<bool>& GetAsmap() const;
|
2012-01-04 19:39:45 -03:00
|
|
|
};
|
|
|
|
|
2014-11-03 12:16:40 -03:00
|
|
|
#endif // BITCOIN_ADDRMAN_H
|