2012-01-04 23:39:45 +01:00
|
|
|
// Copyright (c) 2012 Pieter Wuille
|
2018-07-27 00:36:45 +02:00
|
|
|
// Copyright (c) 2012-2018 The Bitcoin Core developers
|
2014-10-24 06:04:27 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02: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 01:57:53 +01:00
|
|
|
#include <netaddress.h>
|
|
|
|
#include <protocol.h>
|
|
|
|
#include <random.h>
|
|
|
|
#include <sync.h>
|
|
|
|
#include <timedata.h>
|
|
|
|
#include <util.h>
|
2012-01-04 23:39:45 +01:00
|
|
|
|
|
|
|
#include <map>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <set>
|
|
|
|
#include <stdint.h>
|
2012-01-04 23:39:45 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2015-04-28 16:48:28 +02:00
|
|
|
/**
|
|
|
|
* Extended statistics about a CAddress
|
2014-10-24 06:04:27 +02:00
|
|
|
*/
|
2012-01-04 23:39:45 +01:00
|
|
|
class CAddrInfo : public CAddress
|
|
|
|
{
|
2015-09-22 21:24:16 +02:00
|
|
|
|
|
|
|
|
2015-04-19 20:10:13 +02:00
|
|
|
public:
|
|
|
|
//! last try whatsoever by us (memory only)
|
|
|
|
int64_t nLastTry;
|
|
|
|
|
2015-04-19 22:39:38 +02:00
|
|
|
//! last counted attempt (memory only)
|
|
|
|
int64_t nLastCountAttempt;
|
|
|
|
|
2012-01-04 23:39:45 +01:00
|
|
|
private:
|
2014-10-24 06:04:27 +02:00
|
|
|
//! where knowledge about this address first came from
|
2012-01-04 23:39:45 +01:00
|
|
|
CNetAddr source;
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! last successful connection by us
|
2013-04-13 07:13:08 +02:00
|
|
|
int64_t nLastSuccess;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! connection attempts since last successful attempt
|
2012-01-04 23:39:45 +01:00
|
|
|
int nAttempts;
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! reference count in new sets (memory only)
|
2012-01-04 23:39:45 +01:00
|
|
|
int nRefCount;
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! in tried set? (memory only)
|
2012-01-04 23:39:45 +01:00
|
|
|
bool fInTried;
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! position in vRandom
|
2012-01-04 23:39:45 +01:00
|
|
|
int nRandomPos;
|
|
|
|
|
|
|
|
friend class CAddrMan;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2014-09-02 09:58:09 +02:00
|
|
|
ADD_SERIALIZE_METHODS;
|
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
|
|
|
|
2014-08-20 22:44:38 +02:00
|
|
|
template <typename Stream, typename Operation>
|
2016-10-29 01:29:17 +02:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action) {
|
2017-07-08 01:06:56 +02:00
|
|
|
READWRITEAS(CAddress, *this);
|
2012-01-04 23:39:45 +01:00
|
|
|
READWRITE(source);
|
|
|
|
READWRITE(nLastSuccess);
|
|
|
|
READWRITE(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
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
nLastSuccess = 0;
|
|
|
|
nLastTry = 0;
|
2015-04-19 22:39:38 +02:00
|
|
|
nLastCountAttempt = 0;
|
2012-01-04 23:39:45 +01:00
|
|
|
nAttempts = 0;
|
|
|
|
nRefCount = 0;
|
|
|
|
fInTried = false;
|
|
|
|
nRandomPos = -1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
CAddrInfo() : CAddress(), source()
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Calculate in which "tried" bucket this entry belongs
|
2015-03-08 14:30:05 +01:00
|
|
|
int GetTriedBucket(const uint256 &nKey) const;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Calculate in which "new" bucket this entry belongs, given a certain source
|
2015-03-08 14:30:05 +01:00
|
|
|
int GetNewBucket(const uint256 &nKey, const CNetAddr& src) const;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Calculate in which "new" bucket this entry belongs, using its default source
|
2015-03-08 14:30:05 +01:00
|
|
|
int GetNewBucket(const uint256 &nKey) const
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
|
|
|
return GetNewBucket(nKey, source);
|
|
|
|
}
|
|
|
|
|
2015-03-18 17:31:49 +01: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 06:04:27 +02:00
|
|
|
//! Determine whether the statistics about this entry are bad enough so that it can just be deleted
|
2013-04-13 07:13:08 +02:00
|
|
|
bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Calculate the relative chance this entry should be given when selecting nodes to connect to
|
2013-04-13 07:13:08 +02:00
|
|
|
double GetChance(int64_t nNow = GetAdjustedTime()) const;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
/** Stochastic address manager
|
|
|
|
*
|
|
|
|
* Design goals:
|
2015-04-28 16:47:17 +02:00
|
|
|
* * Keep the address tables in-memory, and asynchronously dump the entire table to peers.dat.
|
2014-10-24 06:04:27 +02:00
|
|
|
* * Make sure no (localized) attacker can fill the entire table with his nodes/addresses.
|
|
|
|
*
|
|
|
|
* To that end:
|
|
|
|
* * Addresses are organized into buckets.
|
2015-04-22 15:02:01 +02:00
|
|
|
* * Addresses that have not yet been tried go into 1024 "new" buckets.
|
|
|
|
* * Based on the address range (/16 for IPv4) of the source of information, 64 buckets are selected at random.
|
2015-04-28 16:48:28 +02:00
|
|
|
* * The actual bucket is chosen from one of these, based on the range in which the address itself is located.
|
2015-04-22 15:02:01 +02:00
|
|
|
* * One single address can occur in up to 8 different buckets to increase selection chances for addresses that
|
2014-10-24 06:04:27 +02:00
|
|
|
* are seen frequently. The chance for increasing this multiplicity decreases exponentially.
|
|
|
|
* * When adding a new address to a full bucket, a randomly chosen entry (with a bias favoring less recently seen
|
|
|
|
* ones) is removed from it first.
|
2015-03-19 18:01:57 +01: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 06:04:27 +02:00
|
|
|
* * The actual bucket is chosen from one of these, based on the full address.
|
|
|
|
* * When adding a new good address to a full bucket, a randomly chosen entry (with a bias favoring less recently
|
|
|
|
* tried ones) is evicted from it, back to the "new" buckets.
|
|
|
|
* * Bucket selection is based on cryptographic hashing, using a randomly-generated 256-bit key, which should not
|
|
|
|
* be observable by adversaries.
|
|
|
|
* * Several indexes are kept for high performance. Defining DEBUG_ADDRMAN will introduce frequent (and expensive)
|
|
|
|
* consistency checks for the entire data structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//! total number of buckets for tried addresses
|
2017-02-16 02:45:22 +01:00
|
|
|
#define ADDRMAN_TRIED_BUCKET_COUNT_LOG2 8
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! total number of buckets for new addresses
|
2017-02-16 02:45:22 +01:00
|
|
|
#define ADDRMAN_NEW_BUCKET_COUNT_LOG2 10
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2015-03-18 17:31:49 +01:00
|
|
|
//! maximum allowed number of entries in buckets for new and tried addresses
|
2017-02-16 02:45:22 +01:00
|
|
|
#define ADDRMAN_BUCKET_SIZE_LOG2 6
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread
|
2015-03-19 18:01:57 +01:00
|
|
|
#define ADDRMAN_TRIED_BUCKETS_PER_GROUP 8
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! over how many buckets entries with new addresses originating from a single group are spread
|
2015-03-19 18:01:57 +01:00
|
|
|
#define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 64
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! in how many buckets for entries with new addresses a single address may occur
|
2015-03-19 18:01:57 +01:00
|
|
|
#define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 8
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! how old addresses can maximally be
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_HORIZON_DAYS 30
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! after how many failed attempts we give up on a new node
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_RETRIES 3
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! how many successive failures are allowed ...
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_MAX_FAILURES 10
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! ... in at least this many days
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_MIN_FAIL_DAYS 7
|
|
|
|
|
2016-10-27 19:55:39 +02:00
|
|
|
//! how recent a successful connection should be before we allow an address to be evicted from tried
|
|
|
|
#define ADDRMAN_REPLACEMENT_HOURS 4
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! the maximum percentage of nodes to return in a getaddr call
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_GETADDR_MAX_PCT 23
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! the maximum number of nodes to return in a getaddr call
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_GETADDR_MAX 2500
|
|
|
|
|
2017-02-16 02:45:22 +01:00
|
|
|
//! Convenience
|
|
|
|
#define ADDRMAN_TRIED_BUCKET_COUNT (1 << ADDRMAN_TRIED_BUCKET_COUNT_LOG2)
|
|
|
|
#define ADDRMAN_NEW_BUCKET_COUNT (1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2)
|
|
|
|
#define ADDRMAN_BUCKET_SIZE (1 << ADDRMAN_BUCKET_SIZE_LOG2)
|
|
|
|
|
2016-10-27 19:55:39 +02:00
|
|
|
//! the maximum number of tried addr collisions to store
|
|
|
|
#define ADDRMAN_SET_TRIED_COLLISION_SIZE 10
|
|
|
|
|
2018-07-24 17:59:49 +02:00
|
|
|
/**
|
|
|
|
* Stochastical (IP) address manager
|
2014-10-24 06:04:27 +02:00
|
|
|
*/
|
2012-01-04 23:39:45 +01:00
|
|
|
class CAddrMan
|
|
|
|
{
|
|
|
|
private:
|
2014-10-24 06:04:27 +02:00
|
|
|
//! critical section to protect the inner data structures
|
2012-01-04 23:39:45 +01:00
|
|
|
mutable CCriticalSection cs;
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! last used nId
|
2012-01-04 23:39:45 +01:00
|
|
|
int nIdCount;
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! table with information about all nIds
|
2012-01-04 23:39:45 +01:00
|
|
|
std::map<int, CAddrInfo> mapInfo;
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! find an nId based on its network address
|
2012-01-04 23:39:45 +01:00
|
|
|
std::map<CNetAddr, int> mapAddr;
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! randomly-ordered vector of all nIds
|
2012-01-04 23:39:45 +01:00
|
|
|
std::vector<int> vRandom;
|
|
|
|
|
|
|
|
// number of "tried" entries
|
|
|
|
int nTried;
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! list of "tried" buckets
|
2015-03-18 17:31:49 +01:00
|
|
|
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! number of (unique) "new" entries
|
2012-01-04 23:39:45 +01:00
|
|
|
int nNew;
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! list of "new" buckets
|
2015-03-18 17:31:49 +01:00
|
|
|
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2015-04-19 22:39:38 +02:00
|
|
|
//! last time Good was called (memory only)
|
|
|
|
int64_t nLastGood;
|
|
|
|
|
2018-03-21 10:54:17 +01:00
|
|
|
//! Holds addrs inserted into tried table that collide with existing entries. Test-before-evict discipline used to resolve these collisions.
|
2016-10-27 19:55:39 +02:00
|
|
|
std::set<int> m_tried_collisions;
|
|
|
|
|
2012-01-04 23:39:45 +01:00
|
|
|
protected:
|
2015-12-13 04:34:08 +01:00
|
|
|
//! secret key to randomize bucket select with
|
|
|
|
uint256 nKey;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2016-10-13 16:19:20 +02:00
|
|
|
//! Source of random numbers for randomization in inner loops
|
|
|
|
FastRandomContext insecure_rand;
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Find an entry.
|
2017-08-07 07:36:37 +02:00
|
|
|
CAddrInfo* Find(const CNetAddr& addr, int *pnId = nullptr);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! find an entry, creating it if necessary.
|
|
|
|
//! nTime and nServices of the found node are updated, if necessary.
|
2017-08-07 07:36:37 +02:00
|
|
|
CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = nullptr);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Swap two elements in vRandom.
|
2012-05-09 03:48:14 +02:00
|
|
|
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2015-03-18 17:31:49 +01:00
|
|
|
//! Move an entry from the "new" table(s) to the "tried" table
|
|
|
|
void MakeTried(CAddrInfo& info, int nId);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2015-03-18 17:31:49 +01:00
|
|
|
//! Delete an entry. It must not be in tried, and have refcount 0.
|
|
|
|
void Delete(int nId);
|
2012-09-18 21:07:58 +02:00
|
|
|
|
2015-03-18 17:31:49 +01:00
|
|
|
//! Clear a position in a "new" table. This is the only place where entries are actually deleted.
|
|
|
|
void ClearNew(int nUBucket, int nUBucketPos);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Mark an entry "good", possibly moving it from "new" to "tried".
|
2016-10-27 19:55:39 +02:00
|
|
|
void Good_(const CService &addr, bool test_before_evict, int64_t time);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Add an entry to the "new" table.
|
2013-04-13 07:13:08 +02:00
|
|
|
bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Mark an entry as attempted to connect.
|
2015-04-19 21:34:43 +02:00
|
|
|
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2015-09-22 21:24:16 +02:00
|
|
|
//! Select an address to connect to, if newOnly is set to true, only the new table is selected from.
|
|
|
|
CAddrInfo Select_(bool newOnly);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2016-10-27 19:55:39 +02:00
|
|
|
//! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
|
|
|
|
void ResolveCollisions_();
|
|
|
|
|
|
|
|
//! Return a random to-be-evicted tried table address.
|
|
|
|
CAddrInfo SelectTriedCollision_();
|
|
|
|
|
2015-12-13 04:34:08 +01:00
|
|
|
//! Wraps GetRandInt to allow tests to override RandomInt and make it determinismistic.
|
|
|
|
virtual int RandomInt(int nMax);
|
|
|
|
|
2012-01-04 23:39:45 +01:00
|
|
|
#ifdef DEBUG_ADDRMAN
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Perform consistency check. Returns an error code or zero.
|
2012-01-04 23:39:45 +01:00
|
|
|
int Check_();
|
|
|
|
#endif
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Select several addresses at once.
|
2012-01-04 23:39:45 +01:00
|
|
|
void GetAddr_(std::vector<CAddress> &vAddr);
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Mark an entry as currently-connected-to.
|
2013-04-13 07:13:08 +02:00
|
|
|
void Connected_(const CService &addr, int64_t nTime);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2016-03-26 18:58:00 +01:00
|
|
|
//! Update an entry's service bits.
|
2016-06-08 19:12:22 +02:00
|
|
|
void SetServices_(const CService &addr, ServiceFlags nServices);
|
2016-03-26 18:58:00 +01:00
|
|
|
|
2012-01-04 23:39:45 +01:00
|
|
|
public:
|
2014-10-24 06:04:27 +02:00
|
|
|
/**
|
|
|
|
* serialized format:
|
2015-03-18 17:31:49 +01:00
|
|
|
* * version byte (currently 1)
|
|
|
|
* * 0x20 + nKey (serialized as if it were a vector, for backward compatibility)
|
2014-10-24 06:04:27 +02:00
|
|
|
* * nNew
|
|
|
|
* * nTried
|
2015-03-18 17:31:49 +01:00
|
|
|
* * number of "new" buckets XOR 2**30
|
2014-10-24 06:04:27 +02:00
|
|
|
* * all nNew addrinfos in vvNew
|
|
|
|
* * all nTried addrinfos in vvTried
|
|
|
|
* * for each bucket:
|
|
|
|
* * number of elements
|
|
|
|
* * for each element: index
|
|
|
|
*
|
2015-03-18 17:31:49 +01:00
|
|
|
* 2**30 is xorred with the number of buckets to make addrman deserializer v0 detect it
|
|
|
|
* as incompatible. This is necessary because it did not check the version number on
|
|
|
|
* deserialization.
|
|
|
|
*
|
2014-10-24 06:04:27 +02:00
|
|
|
* Notice that vvTried, mapAddr and vVector are never encoded explicitly;
|
|
|
|
* they are instead reconstructed from the other information.
|
|
|
|
*
|
2015-08-09 01:17:27 +02:00
|
|
|
* vvNew is serialized, but only used if ADDRMAN_UNKNOWN_BUCKET_COUNT didn't change,
|
2014-10-24 06:04:27 +02:00
|
|
|
* otherwise it is reconstructed as well.
|
|
|
|
*
|
|
|
|
* This format is more complex, but significantly smaller (at most 1.5 MiB), and supports
|
|
|
|
* changes to the ADDRMAN_ parameters without breaking the on-disk structure.
|
|
|
|
*
|
|
|
|
* We don't use ADD_SERIALIZE_METHODS since the serialization and deserialization code has
|
|
|
|
* very little in common.
|
|
|
|
*/
|
2014-07-10 20:16:58 +02:00
|
|
|
template<typename Stream>
|
2016-10-29 01:29:17 +02:00
|
|
|
void Serialize(Stream &s) const
|
2014-07-10 20:16:58 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
2015-03-18 17:31:49 +01:00
|
|
|
unsigned char nVersion = 1;
|
2014-07-10 20:16:58 +02:00
|
|
|
s << nVersion;
|
2015-03-08 14:30:05 +01:00
|
|
|
s << ((unsigned char)32);
|
2014-07-10 20:16:58 +02:00
|
|
|
s << nKey;
|
|
|
|
s << nNew;
|
|
|
|
s << nTried;
|
|
|
|
|
2015-03-18 17:31:49 +01:00
|
|
|
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
|
2014-07-10 20:16:58 +02:00
|
|
|
s << nUBuckets;
|
|
|
|
std::map<int, int> mapUnkIds;
|
|
|
|
int nIds = 0;
|
2017-06-04 22:02:43 +02:00
|
|
|
for (const auto& entry : mapInfo) {
|
|
|
|
mapUnkIds[entry.first] = nIds;
|
|
|
|
const CAddrInfo &info = entry.second;
|
2014-07-10 20:16:58 +02:00
|
|
|
if (info.nRefCount) {
|
2015-03-18 17:31:49 +01:00
|
|
|
assert(nIds != nNew); // this means nNew was wrong, oh ow
|
2014-07-10 20:16:58 +02:00
|
|
|
s << info;
|
|
|
|
nIds++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nIds = 0;
|
2017-06-04 22:02:43 +02:00
|
|
|
for (const auto& entry : mapInfo) {
|
|
|
|
const CAddrInfo &info = entry.second;
|
2014-07-10 20:16:58 +02:00
|
|
|
if (info.fInTried) {
|
2015-03-18 17:31:49 +01:00
|
|
|
assert(nIds != nTried); // this means nTried was wrong, oh ow
|
2014-07-10 20:16:58 +02:00
|
|
|
s << info;
|
|
|
|
nIds++;
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 17:31:49 +01:00
|
|
|
for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
|
|
|
|
int nSize = 0;
|
|
|
|
for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
|
|
|
|
if (vvNew[bucket][i] != -1)
|
|
|
|
nSize++;
|
|
|
|
}
|
2014-07-10 20:16:58 +02:00
|
|
|
s << nSize;
|
2015-03-18 17:31:49 +01:00
|
|
|
for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
|
|
|
|
if (vvNew[bucket][i] != -1) {
|
|
|
|
int nIndex = mapUnkIds[vvNew[bucket][i]];
|
|
|
|
s << nIndex;
|
|
|
|
}
|
2014-07-10 20:16:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-07-10 20:16:58 +02:00
|
|
|
template<typename Stream>
|
2016-10-29 01:29:17 +02:00
|
|
|
void Unserialize(Stream& s)
|
2014-07-10 20:16:58 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
2015-03-18 17:31:49 +01:00
|
|
|
Clear();
|
|
|
|
|
2014-07-10 20:16:58 +02:00
|
|
|
unsigned char nVersion;
|
|
|
|
s >> nVersion;
|
2015-03-08 14:30:05 +01:00
|
|
|
unsigned char nKeySize;
|
|
|
|
s >> nKeySize;
|
2015-03-18 17:31:49 +01:00
|
|
|
if (nKeySize != 32) throw std::ios_base::failure("Incorrect keysize in addrman deserialization");
|
2014-07-10 20:16:58 +02:00
|
|
|
s >> nKey;
|
|
|
|
s >> nNew;
|
|
|
|
s >> nTried;
|
|
|
|
int nUBuckets = 0;
|
|
|
|
s >> nUBuckets;
|
2015-03-18 17:31:49 +01:00
|
|
|
if (nVersion != 0) {
|
|
|
|
nUBuckets ^= (1 << 30);
|
|
|
|
}
|
|
|
|
|
2016-04-24 07:21:52 +02:00
|
|
|
if (nNew > ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE) {
|
|
|
|
throw std::ios_base::failure("Corrupt CAddrMan serialization, nNew exceeds limit.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nTried > ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE) {
|
|
|
|
throw std::ios_base::failure("Corrupt CAddrMan serialization, nTried exceeds limit.");
|
|
|
|
}
|
|
|
|
|
2015-03-18 17:31:49 +01:00
|
|
|
// Deserialize entries from the new table.
|
2014-07-10 20:16:58 +02:00
|
|
|
for (int n = 0; n < nNew; n++) {
|
|
|
|
CAddrInfo &info = mapInfo[n];
|
|
|
|
s >> info;
|
|
|
|
mapAddr[info] = n;
|
|
|
|
info.nRandomPos = vRandom.size();
|
|
|
|
vRandom.push_back(n);
|
2015-03-18 17:31:49 +01:00
|
|
|
if (nVersion != 1 || nUBuckets != ADDRMAN_NEW_BUCKET_COUNT) {
|
|
|
|
// In case the new table data cannot be used (nVersion unknown, or bucket count wrong),
|
|
|
|
// immediately try to give them a reference based on their primary source address.
|
|
|
|
int nUBucket = info.GetNewBucket(nKey);
|
|
|
|
int nUBucketPos = info.GetBucketPosition(nKey, true, nUBucket);
|
|
|
|
if (vvNew[nUBucket][nUBucketPos] == -1) {
|
|
|
|
vvNew[nUBucket][nUBucketPos] = n;
|
|
|
|
info.nRefCount++;
|
|
|
|
}
|
2014-07-10 20:16:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
nIdCount = nNew;
|
2015-03-18 17:31:49 +01:00
|
|
|
|
|
|
|
// Deserialize entries from the tried table.
|
2014-07-10 20:16:58 +02:00
|
|
|
int nLost = 0;
|
|
|
|
for (int n = 0; n < nTried; n++) {
|
|
|
|
CAddrInfo info;
|
|
|
|
s >> info;
|
2015-03-18 17:31:49 +01:00
|
|
|
int nKBucket = info.GetTriedBucket(nKey);
|
|
|
|
int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
|
|
|
|
if (vvTried[nKBucket][nKBucketPos] == -1) {
|
2014-07-10 20:16:58 +02:00
|
|
|
info.nRandomPos = vRandom.size();
|
|
|
|
info.fInTried = true;
|
|
|
|
vRandom.push_back(nIdCount);
|
|
|
|
mapInfo[nIdCount] = info;
|
|
|
|
mapAddr[info] = nIdCount;
|
2015-03-18 17:31:49 +01:00
|
|
|
vvTried[nKBucket][nKBucketPos] = nIdCount;
|
2014-07-10 20:16:58 +02:00
|
|
|
nIdCount++;
|
2012-01-04 23:39:45 +01:00
|
|
|
} else {
|
2014-07-10 20:16:58 +02:00
|
|
|
nLost++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nTried -= nLost;
|
2015-03-18 17:31:49 +01:00
|
|
|
|
|
|
|
// Deserialize positions in the new table (if possible).
|
|
|
|
for (int bucket = 0; bucket < nUBuckets; bucket++) {
|
2014-07-10 20:16:58 +02:00
|
|
|
int nSize = 0;
|
|
|
|
s >> nSize;
|
|
|
|
for (int n = 0; n < nSize; n++) {
|
|
|
|
int nIndex = 0;
|
|
|
|
s >> nIndex;
|
2015-03-18 17:31:49 +01:00
|
|
|
if (nIndex >= 0 && nIndex < nNew) {
|
|
|
|
CAddrInfo &info = mapInfo[nIndex];
|
|
|
|
int nUBucketPos = info.GetBucketPosition(nKey, true, bucket);
|
|
|
|
if (nVersion == 1 && nUBuckets == ADDRMAN_NEW_BUCKET_COUNT && vvNew[bucket][nUBucketPos] == -1 && info.nRefCount < ADDRMAN_NEW_BUCKETS_PER_ADDRESS) {
|
|
|
|
info.nRefCount++;
|
|
|
|
vvNew[bucket][nUBucketPos] = nIndex;
|
|
|
|
}
|
2012-01-04 23:39:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 17:31:49 +01:00
|
|
|
|
|
|
|
// Prune new entries with refcount 0 (as a result of collisions).
|
|
|
|
int nLostUnk = 0;
|
|
|
|
for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); ) {
|
|
|
|
if (it->second.fInTried == false && it->second.nRefCount == 0) {
|
|
|
|
std::map<int, CAddrInfo>::const_iterator itCopy = it++;
|
|
|
|
Delete(itCopy->first);
|
|
|
|
nLostUnk++;
|
|
|
|
} else {
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nLost + nLostUnk > 0) {
|
2016-12-25 21:19:40 +01:00
|
|
|
LogPrint(BCLog::ADDRMAN, "addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk, nLost);
|
2015-03-18 17:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Check();
|
2014-07-10 20:16:58 +02:00
|
|
|
}
|
|
|
|
|
2015-03-18 17:31:49 +01:00
|
|
|
void Clear()
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
2017-10-31 10:25:09 +01:00
|
|
|
LOCK(cs);
|
2015-03-18 17:31:49 +01:00
|
|
|
std::vector<int>().swap(vRandom);
|
|
|
|
nKey = GetRandHash();
|
|
|
|
for (size_t bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
|
|
|
|
for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
|
|
|
|
vvNew[bucket][entry] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (size_t bucket = 0; bucket < ADDRMAN_TRIED_BUCKET_COUNT; bucket++) {
|
|
|
|
for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
|
|
|
|
vvTried[bucket][entry] = -1;
|
|
|
|
}
|
|
|
|
}
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2015-03-18 17:31:49 +01:00
|
|
|
nIdCount = 0;
|
|
|
|
nTried = 0;
|
|
|
|
nNew = 0;
|
2015-04-19 22:39:38 +02:00
|
|
|
nLastGood = 1; //Initially at 1 so that "never" is strictly worse.
|
2017-09-06 02:32:06 +02:00
|
|
|
mapInfo.clear();
|
|
|
|
mapAddr.clear();
|
2015-03-18 17:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CAddrMan()
|
|
|
|
{
|
|
|
|
Clear();
|
2012-01-04 23:39:45 +01:00
|
|
|
}
|
|
|
|
|
2015-03-08 14:30:05 +01:00
|
|
|
~CAddrMan()
|
|
|
|
{
|
2015-03-18 17:31:49 +01:00
|
|
|
nKey.SetNull();
|
2015-03-08 14:30:05 +01:00
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Return the number of (unique) addresses in all tables.
|
2015-06-15 14:43:31 +02:00
|
|
|
size_t size() const
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
2016-11-26 03:11:25 +01:00
|
|
|
LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead
|
2012-01-04 23:39:45 +01:00
|
|
|
return vRandom.size();
|
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Consistency check
|
2012-01-04 23:39:45 +01:00
|
|
|
void Check()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_ADDRMAN
|
|
|
|
{
|
2012-04-06 18:39:12 +02:00
|
|
|
LOCK(cs);
|
2012-01-04 23:39:45 +01:00
|
|
|
int err;
|
|
|
|
if ((err=Check_()))
|
2013-09-18 12:38:08 +02:00
|
|
|
LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
|
2012-01-04 23:39:45 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Add a single address.
|
2013-04-13 07:13:08 +02:00
|
|
|
bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0)
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
2016-11-26 03:11:25 +01:00
|
|
|
LOCK(cs);
|
2012-01-04 23:39:45 +01:00
|
|
|
bool fRet = false;
|
2016-11-26 03:11:25 +01:00
|
|
|
Check();
|
|
|
|
fRet |= Add_(addr, source, nTimePenalty);
|
|
|
|
Check();
|
2016-12-25 21:19:40 +01:00
|
|
|
if (fRet) {
|
|
|
|
LogPrint(BCLog::ADDRMAN, "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
|
|
|
|
}
|
2012-01-04 23:39:45 +01:00
|
|
|
return fRet;
|
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Add multiple addresses.
|
2013-04-13 07:13:08 +02:00
|
|
|
bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
2016-11-26 03:11:25 +01:00
|
|
|
LOCK(cs);
|
2012-01-04 23:39:45 +01:00
|
|
|
int nAdd = 0;
|
2016-11-26 03:11:25 +01:00
|
|
|
Check();
|
|
|
|
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
|
|
|
|
nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
|
|
|
|
Check();
|
2016-12-25 21:19:40 +01:00
|
|
|
if (nAdd) {
|
|
|
|
LogPrint(BCLog::ADDRMAN, "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
|
|
|
|
}
|
2012-01-04 23:39:45 +01:00
|
|
|
return nAdd > 0;
|
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Mark an entry as accessible.
|
2016-10-27 19:55:39 +02:00
|
|
|
void Good(const CService &addr, bool test_before_evict = true, int64_t nTime = GetAdjustedTime())
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
2016-11-26 03:13:48 +01:00
|
|
|
LOCK(cs);
|
|
|
|
Check();
|
2016-10-27 19:55:39 +02:00
|
|
|
Good_(addr, test_before_evict, nTime);
|
2016-11-26 03:13:48 +01:00
|
|
|
Check();
|
2012-01-04 23:39:45 +01:00
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Mark an entry as connection attempted to.
|
2015-04-19 21:34:43 +02:00
|
|
|
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime = GetAdjustedTime())
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
2016-11-26 03:13:48 +01:00
|
|
|
LOCK(cs);
|
|
|
|
Check();
|
|
|
|
Attempt_(addr, fCountFailure, nTime);
|
|
|
|
Check();
|
2012-01-04 23:39:45 +01:00
|
|
|
}
|
|
|
|
|
2016-10-27 19:55:39 +02:00
|
|
|
//! See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
|
|
|
|
void ResolveCollisions()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
Check();
|
|
|
|
ResolveCollisions_();
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Randomly select an address in tried that another address is attempting to evict.
|
|
|
|
CAddrInfo SelectTriedCollision()
|
|
|
|
{
|
|
|
|
CAddrInfo ret;
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
Check();
|
|
|
|
ret = SelectTriedCollision_();
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
/**
|
|
|
|
* Choose an address to connect to.
|
|
|
|
*/
|
2015-09-22 21:24:16 +02:00
|
|
|
CAddrInfo Select(bool newOnly = false)
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
2015-04-19 20:10:13 +02:00
|
|
|
CAddrInfo addrRet;
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
2012-04-06 18:39:12 +02:00
|
|
|
LOCK(cs);
|
2012-01-04 23:39:45 +01:00
|
|
|
Check();
|
2015-09-22 21:24:16 +02:00
|
|
|
addrRet = Select_(newOnly);
|
2012-01-04 23:39:45 +01:00
|
|
|
Check();
|
|
|
|
}
|
|
|
|
return addrRet;
|
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Return a bunch of addresses, selected at random.
|
2012-01-04 23:39:45 +01:00
|
|
|
std::vector<CAddress> GetAddr()
|
|
|
|
{
|
|
|
|
Check();
|
|
|
|
std::vector<CAddress> vAddr;
|
2012-04-06 18:39:12 +02:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
2012-01-04 23:39:45 +01:00
|
|
|
GetAddr_(vAddr);
|
2012-04-06 18:39:12 +02:00
|
|
|
}
|
2012-01-04 23:39:45 +01:00
|
|
|
Check();
|
|
|
|
return vAddr;
|
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Mark an entry as currently-connected-to.
|
2013-04-13 07:13:08 +02:00
|
|
|
void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
|
2012-01-04 23:39:45 +01:00
|
|
|
{
|
2016-11-26 03:13:48 +01:00
|
|
|
LOCK(cs);
|
|
|
|
Check();
|
|
|
|
Connected_(addr, nTime);
|
|
|
|
Check();
|
2012-01-04 23:39:45 +01:00
|
|
|
}
|
2015-09-22 21:24:16 +02:00
|
|
|
|
2016-06-08 19:12:22 +02:00
|
|
|
void SetServices(const CService &addr, ServiceFlags nServices)
|
2016-03-26 18:58:00 +01:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
Check();
|
|
|
|
SetServices_(addr, nServices);
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
|
2012-01-04 23:39:45 +01:00
|
|
|
};
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#endif // BITCOIN_ADDRMAN_H
|