2012-01-04 23:39:45 +01:00
|
|
|
// Copyright (c) 2012 Pieter Wuille
|
2020-01-15 02:17:38 +07:00
|
|
|
// Copyright (c) 2012-2020 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
|
|
|
|
2020-04-20 17:11:08 +02:00
|
|
|
#include <fs.h>
|
2021-09-07 10:48:45 +02:00
|
|
|
#include <logging.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <netaddress.h>
|
|
|
|
#include <protocol.h>
|
|
|
|
#include <sync.h>
|
|
|
|
#include <timedata.h>
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2021-09-07 10:48:45 +02:00
|
|
|
#include <cstdint>
|
2021-05-02 18:44:17 +02:00
|
|
|
#include <optional>
|
2013-04-13 00:13:08 -05:00
|
|
|
#include <set>
|
2020-04-20 17:11:08 +02:00
|
|
|
#include <unordered_map>
|
2019-12-29 18:54:33 +01:00
|
|
|
#include <vector>
|
2012-01-04 23:39:45 +01:00
|
|
|
|
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};
|
|
|
|
|
2015-04-28 14:48:28 +00:00
|
|
|
/**
|
|
|
|
* Extended statistics about a CAddress
|
2014-10-24 12:04:27 +08:00
|
|
|
*/
|
2012-01-04 23:39:45 +01:00
|
|
|
class CAddrInfo : public CAddress
|
|
|
|
{
|
2015-04-19 11:10:13 -07:00
|
|
|
public:
|
|
|
|
//! last try whatsoever by us (memory only)
|
2019-01-05 12:02:12 +01:00
|
|
|
int64_t nLastTry{0};
|
2015-04-19 11:10:13 -07:00
|
|
|
|
2015-04-19 13:39:38 -07:00
|
|
|
//! last counted attempt (memory only)
|
2019-01-05 12:02:12 +01:00
|
|
|
int64_t nLastCountAttempt{0};
|
2015-04-19 13:39:38 -07:00
|
|
|
|
2012-01-04 23:39:45 +01:00
|
|
|
private:
|
2014-10-24 12:04:27 +08:00
|
|
|
//! where knowledge about this address first came from
|
2012-01-04 23:39:45 +01:00
|
|
|
CNetAddr source;
|
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! last successful connection by us
|
2019-01-05 12:02:12 +01:00
|
|
|
int64_t nLastSuccess{0};
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! connection attempts since last successful attempt
|
2019-01-05 12:02:12 +01:00
|
|
|
int nAttempts{0};
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! reference count in new sets (memory only)
|
2019-01-05 12:02:12 +01:00
|
|
|
int nRefCount{0};
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! in tried set? (memory only)
|
2019-01-05 12:02:12 +01:00
|
|
|
bool fInTried{false};
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! position in vRandom
|
2021-05-13 13:43:36 +02:00
|
|
|
mutable int nRandomPos{-1};
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2021-09-01 11:21:29 -07:00
|
|
|
friend class AddrManImpl;
|
2021-02-08 14:14:12 +01:00
|
|
|
friend class CAddrManDeterministic;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-07-07 16:08:51 -07:00
|
|
|
SERIALIZE_METHODS(CAddrInfo, obj)
|
|
|
|
{
|
|
|
|
READWRITEAS(CAddress, obj);
|
|
|
|
READWRITE(obj.source, obj.nLastSuccess, obj.nAttempts);
|
overhaul serialization code
The implementation of each class' serialization/deserialization is no longer
passed within a macro. The implementation now lies within a template of form:
template <typename T, typename Stream, typename Operation>
inline static size_t SerializationOp(T thisPtr, Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
/* CODE */
return nSerSize;
}
In cases when codepath should depend on whether or not we are just deserializing
(old fGetSize, fWrite, fRead flags) an additional clause can be used:
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
The IMPLEMENT_SERIALIZE macro will now be a freestanding clause added within
class' body (similiar to Qt's Q_OBJECT) to implement GetSerializeSize,
Serialize and Unserialize. These are now wrappers around
the "SerializationOp" template.
2014-08-20 08:42:31 +02:00
|
|
|
}
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2012-05-05 21:27:52 +02:00
|
|
|
CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CAddrInfo() : CAddress(), source()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! Calculate in which "tried" bucket this entry belongs
|
2019-12-24 13:18:44 -05:00
|
|
|
int GetTriedBucket(const uint256 &nKey, const std::vector<bool> &asmap) const;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! Calculate in which "new" bucket this entry belongs, given a certain source
|
2019-12-24 13:18:44 -05:00
|
|
|
int GetNewBucket(const uint256 &nKey, const CNetAddr& src, const std::vector<bool> &asmap) const;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! Calculate in which "new" bucket this entry belongs, using its default source
|
2019-12-24 13:18:44 -05:00
|
|
|
int GetNewBucket(const uint256 &nKey, const std::vector<bool> &asmap) const
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
2019-12-24 13:18:44 -05:00
|
|
|
return GetNewBucket(nKey, source, asmap);
|
2012-01-04 23:39:45 +01:00
|
|
|
}
|
|
|
|
|
2015-03-18 09:31:49 -07:00
|
|
|
//! Calculate in which position of a bucket to store this entry.
|
|
|
|
int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
|
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! Determine whether the statistics about this entry are bad enough so that it can just be deleted
|
2013-04-13 00:13:08 -05:00
|
|
|
bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! Calculate the relative chance this entry should be given when selecting nodes to connect to
|
2013-04-13 00:13:08 -05:00
|
|
|
double GetChance(int64_t nNow = GetAdjustedTime()) const;
|
2012-01-04 23:39:45 +01:00
|
|
|
};
|
|
|
|
|
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-08-18 09:45:54 +01:00
|
|
|
/** Total number of buckets for tried addresses */
|
2021-08-17 17:31:23 +01:00
|
|
|
static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2{8};
|
2021-08-17 13:55:11 -07:00
|
|
|
static constexpr int ADDRMAN_TRIED_BUCKET_COUNT{1 << ADDRMAN_TRIED_BUCKET_COUNT_LOG2};
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2021-08-18 09:45:54 +01:00
|
|
|
/** Total number of buckets for new addresses */
|
2021-08-17 17:31:23 +01:00
|
|
|
static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2{10};
|
2021-08-17 13:55:11 -07:00
|
|
|
static constexpr int ADDRMAN_NEW_BUCKET_COUNT{1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2};
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2021-08-18 09:45:54 +01:00
|
|
|
/** Maximum allowed number of entries in buckets for new and tried addresses */
|
2021-08-17 17:31:23 +01:00
|
|
|
static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2{6};
|
|
|
|
static constexpr int ADDRMAN_BUCKET_SIZE{1 << ADDRMAN_BUCKET_SIZE_LOG2};
|
2017-02-15 17:45:22 -08:00
|
|
|
|
2018-07-24 16:59:49 +01:00
|
|
|
/**
|
|
|
|
* Stochastical (IP) address manager
|
2014-10-24 12:04:27 +08:00
|
|
|
*/
|
2012-01-04 23:39:45 +01:00
|
|
|
class CAddrMan
|
|
|
|
{
|
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-09-28 15:46:43 -04:00
|
|
|
explicit CAddrMan(std::vector<bool> asmap, bool deterministic, int32_t consistency_check_ratio);
|
|
|
|
|
|
|
|
~CAddrMan();
|
|
|
|
|
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-07-22 17:00:36 -07:00
|
|
|
//! Add addresses to addrman's new table.
|
2021-09-01 11:21:29 -07:00
|
|
|
bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! Mark an entry as accessible.
|
2021-09-01 11:21:29 -07:00
|
|
|
void Good(const CService &addr, int64_t nTime = GetAdjustedTime());
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
//! Mark an entry as connection attempted to.
|
2021-09-01 11:21:29 -07:00
|
|
|
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime = GetAdjustedTime());
|
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
|
|
|
|
|
|
|
//! Randomly select an address in tried that another address is attempting to evict.
|
2021-09-01 11:21:29 -07:00
|
|
|
CAddrInfo SelectTriedCollision();
|
2016-10-27 13:55:39 -04:00
|
|
|
|
2014-10-24 12:04:27 +08:00
|
|
|
/**
|
|
|
|
* Choose an address to connect to.
|
|
|
|
*/
|
2021-09-01 11:21:29 -07:00
|
|
|
CAddrInfo 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-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
|
|
|
|
2020-11-04 11:41:17 +00:00
|
|
|
//! Outer function for Connected_()
|
2021-09-01 11:21:29 -07:00
|
|
|
void Connected(const CService &addr, int64_t nTime = GetAdjustedTime());
|
2015-09-22 15:24:16 -04:00
|
|
|
|
2021-09-01 11:21:29 -07:00
|
|
|
void SetServices(const CService &addr, ServiceFlags nServices);
|
2016-03-26 18:58:00 +01:00
|
|
|
|
2021-09-24 14:14:39 -06:00
|
|
|
const std::vector<bool>& GetAsmap() const;
|
2021-08-24 11:47:17 +01:00
|
|
|
|
2021-05-24 19:26:49 +03:00
|
|
|
friend class CAddrManTest;
|
2021-02-08 14:14:12 +01:00
|
|
|
friend class CAddrManDeterministic;
|
2012-01-04 23:39:45 +01:00
|
|
|
};
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#endif // BITCOIN_ADDRMAN_H
|