2012-01-04 14:39:45 -08:00
|
|
|
// Copyright (c) 2012 Pieter Wuille
|
2014-10-23 21:04:27 -07:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 07:02:28 -07:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-12 22:13:08 -07:00
|
|
|
|
2014-11-03 07:16:40 -08:00
|
|
|
#ifndef BITCOIN_ADDRMAN_H
|
|
|
|
#define BITCOIN_ADDRMAN_H
|
2012-01-04 14:39:45 -08:00
|
|
|
|
|
|
|
#include "netbase.h"
|
|
|
|
#include "protocol.h"
|
2014-06-26 05:41:53 -07:00
|
|
|
#include "random.h"
|
2012-05-11 08:00:03 -07:00
|
|
|
#include "sync.h"
|
2014-06-19 06:08:37 -07:00
|
|
|
#include "timedata.h"
|
2013-04-12 22:13:08 -07:00
|
|
|
#include "util.h"
|
2012-01-04 14:39:45 -08:00
|
|
|
|
|
|
|
#include <map>
|
2013-04-12 22:13:08 -07:00
|
|
|
#include <set>
|
|
|
|
#include <stdint.h>
|
2012-01-04 14:39:45 -08:00
|
|
|
#include <vector>
|
|
|
|
|
2015-04-28 07:48:28 -07:00
|
|
|
/**
|
|
|
|
* Extended statistics about a CAddress
|
2014-10-23 21:04:27 -07:00
|
|
|
*/
|
2012-01-04 14:39:45 -08:00
|
|
|
class CAddrInfo : public CAddress
|
|
|
|
{
|
2015-04-19 11:10:13 -07:00
|
|
|
public:
|
|
|
|
//! last try whatsoever by us (memory only)
|
|
|
|
int64_t nLastTry;
|
|
|
|
|
2012-01-04 14:39:45 -08:00
|
|
|
private:
|
2014-10-23 21:04:27 -07:00
|
|
|
//! where knowledge about this address first came from
|
2012-01-04 14:39:45 -08:00
|
|
|
CNetAddr source;
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! last successful connection by us
|
2013-04-12 22:13:08 -07:00
|
|
|
int64_t nLastSuccess;
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! connection attempts since last successful attempt
|
2012-01-04 14:39:45 -08:00
|
|
|
int nAttempts;
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! reference count in new sets (memory only)
|
2012-01-04 14:39:45 -08:00
|
|
|
int nRefCount;
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! in tried set? (memory only)
|
2012-01-04 14:39:45 -08:00
|
|
|
bool fInTried;
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! position in vRandom
|
2012-01-04 14:39:45 -08:00
|
|
|
int nRandomPos;
|
|
|
|
|
|
|
|
friend class CAddrMan;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2014-09-02 00:58:09 -07: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-19 23:42:31 -07:00
|
|
|
|
2014-08-20 13:44:38 -07:00
|
|
|
template <typename Stream, typename Operation>
|
2014-08-20 15:49:32 -07:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
|
|
|
READWRITE(*(CAddress*)this);
|
2012-01-04 14:39:45 -08: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-19 23:42:31 -07:00
|
|
|
}
|
2012-01-04 14:39:45 -08:00
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
nLastSuccess = 0;
|
|
|
|
nLastTry = 0;
|
|
|
|
nAttempts = 0;
|
|
|
|
nRefCount = 0;
|
|
|
|
fInTried = false;
|
|
|
|
nRandomPos = -1;
|
|
|
|
}
|
|
|
|
|
2012-05-05 12:27:52 -07:00
|
|
|
CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
|
2012-01-04 14:39:45 -08:00
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
CAddrInfo() : CAddress(), source()
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Calculate in which "tried" bucket this entry belongs
|
2015-03-08 06:30:05 -07:00
|
|
|
int GetTriedBucket(const uint256 &nKey) const;
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Calculate in which "new" bucket this entry belongs, given a certain source
|
2015-03-08 06:30:05 -07:00
|
|
|
int GetNewBucket(const uint256 &nKey, const CNetAddr& src) const;
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Calculate in which "new" bucket this entry belongs, using its default source
|
2015-03-08 06:30:05 -07:00
|
|
|
int GetNewBucket(const uint256 &nKey) const
|
2012-01-04 14:39:45 -08:00
|
|
|
{
|
|
|
|
return GetNewBucket(nKey, source);
|
|
|
|
}
|
|
|
|
|
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-23 21:04:27 -07:00
|
|
|
//! Determine whether the statistics about this entry are bad enough so that it can just be deleted
|
2013-04-12 22:13:08 -07:00
|
|
|
bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Calculate the relative chance this entry should be given when selecting nodes to connect to
|
2013-04-12 22:13:08 -07:00
|
|
|
double GetChance(int64_t nNow = GetAdjustedTime()) const;
|
2012-01-04 14:39:45 -08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
/** Stochastic address manager
|
|
|
|
*
|
|
|
|
* Design goals:
|
2015-04-28 07:47:17 -07:00
|
|
|
* * Keep the address tables in-memory, and asynchronously dump the entire table to peers.dat.
|
2014-10-23 21:04:27 -07:00
|
|
|
* * Make sure no (localized) attacker can fill the entire table with his nodes/addresses.
|
|
|
|
*
|
|
|
|
* To that end:
|
|
|
|
* * Addresses are organized into buckets.
|
2015-03-19 10:01:57 -07:00
|
|
|
* * Address that have not yet been tried go into 1024 "new" buckets.
|
|
|
|
* * Based on the address range (/16 for IPv4) of source of the information, 64 buckets are selected at random
|
2015-04-28 07:48:28 -07:00
|
|
|
* * The actual bucket is chosen from one of these, based on the range in which the address itself is located.
|
2015-03-19 10:01:57 -07:00
|
|
|
* * One single address can occur in up to 8 different buckets, to increase selection chances for addresses that
|
2014-10-23 21:04:27 -07: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 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-23 21:04:27 -07: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
|
2015-03-19 10:01:57 -07:00
|
|
|
#define ADDRMAN_TRIED_BUCKET_COUNT 256
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! total number of buckets for new addresses
|
2015-03-19 10:01:57 -07:00
|
|
|
#define ADDRMAN_NEW_BUCKET_COUNT 1024
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2015-03-18 09:31:49 -07:00
|
|
|
//! maximum allowed number of entries in buckets for new and tried addresses
|
|
|
|
#define ADDRMAN_BUCKET_SIZE 64
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread
|
2015-03-19 10:01:57 -07:00
|
|
|
#define ADDRMAN_TRIED_BUCKETS_PER_GROUP 8
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! over how many buckets entries with new addresses originating from a single group are spread
|
2015-03-19 10:01:57 -07:00
|
|
|
#define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 64
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! in how many buckets for entries with new addresses a single address may occur
|
2015-03-19 10:01:57 -07:00
|
|
|
#define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 8
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! how old addresses can maximally be
|
2012-01-04 14:39:45 -08:00
|
|
|
#define ADDRMAN_HORIZON_DAYS 30
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! after how many failed attempts we give up on a new node
|
2012-01-04 14:39:45 -08:00
|
|
|
#define ADDRMAN_RETRIES 3
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! how many successive failures are allowed ...
|
2012-01-04 14:39:45 -08:00
|
|
|
#define ADDRMAN_MAX_FAILURES 10
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! ... in at least this many days
|
2012-01-04 14:39:45 -08:00
|
|
|
#define ADDRMAN_MIN_FAIL_DAYS 7
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! the maximum percentage of nodes to return in a getaddr call
|
2012-01-04 14:39:45 -08:00
|
|
|
#define ADDRMAN_GETADDR_MAX_PCT 23
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! the maximum number of nodes to return in a getaddr call
|
2012-01-04 14:39:45 -08:00
|
|
|
#define ADDRMAN_GETADDR_MAX 2500
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
/**
|
|
|
|
* Stochastical (IP) address manager
|
|
|
|
*/
|
2012-01-04 14:39:45 -08:00
|
|
|
class CAddrMan
|
|
|
|
{
|
|
|
|
private:
|
2014-10-23 21:04:27 -07:00
|
|
|
//! critical section to protect the inner data structures
|
2012-01-04 14:39:45 -08:00
|
|
|
mutable CCriticalSection cs;
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! secret key to randomize bucket select with
|
2015-03-08 06:30:05 -07:00
|
|
|
uint256 nKey;
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! last used nId
|
2012-01-04 14:39:45 -08:00
|
|
|
int nIdCount;
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! table with information about all nIds
|
2012-01-04 14:39:45 -08:00
|
|
|
std::map<int, CAddrInfo> mapInfo;
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! find an nId based on its network address
|
2012-01-04 14:39:45 -08:00
|
|
|
std::map<CNetAddr, int> mapAddr;
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! randomly-ordered vector of all nIds
|
2012-01-04 14:39:45 -08:00
|
|
|
std::vector<int> vRandom;
|
|
|
|
|
|
|
|
// number of "tried" entries
|
|
|
|
int nTried;
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! list of "tried" buckets
|
2015-03-18 09:31:49 -07:00
|
|
|
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! number of (unique) "new" entries
|
2012-01-04 14:39:45 -08:00
|
|
|
int nNew;
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! list of "new" buckets
|
2015-03-18 09:31:49 -07:00
|
|
|
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
|
2012-01-04 14:39:45 -08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Find an entry.
|
2012-01-04 14:39:45 -08:00
|
|
|
CAddrInfo* Find(const CNetAddr& addr, int *pnId = NULL);
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! find an entry, creating it if necessary.
|
|
|
|
//! nTime and nServices of the found node are updated, if necessary.
|
2012-01-04 14:39:45 -08:00
|
|
|
CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = NULL);
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Swap two elements in vRandom.
|
2012-05-08 18:48:14 -07:00
|
|
|
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2);
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2015-03-18 09:31:49 -07:00
|
|
|
//! Move an entry from the "new" table(s) to the "tried" table
|
|
|
|
void MakeTried(CAddrInfo& info, int nId);
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2015-03-18 09:31:49 -07:00
|
|
|
//! Delete an entry. It must not be in tried, and have refcount 0.
|
|
|
|
void Delete(int nId);
|
2012-09-18 12:07:58 -07:00
|
|
|
|
2015-03-18 09:31:49 -07: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 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Mark an entry "good", possibly moving it from "new" to "tried".
|
2013-04-12 22:13:08 -07:00
|
|
|
void Good_(const CService &addr, int64_t nTime);
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Add an entry to the "new" table.
|
2013-04-12 22:13:08 -07:00
|
|
|
bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty);
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Mark an entry as attempted to connect.
|
2013-04-12 22:13:08 -07:00
|
|
|
void Attempt_(const CService &addr, int64_t nTime);
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Select an address to connect to.
|
|
|
|
//! nUnkBias determines how much to favor new addresses over tried ones (min=0, max=100)
|
2015-04-19 11:10:13 -07:00
|
|
|
CAddrInfo Select_();
|
2012-01-04 14:39:45 -08:00
|
|
|
|
|
|
|
#ifdef DEBUG_ADDRMAN
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Perform consistency check. Returns an error code or zero.
|
2012-01-04 14:39:45 -08:00
|
|
|
int Check_();
|
|
|
|
#endif
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Select several addresses at once.
|
2012-01-04 14:39:45 -08:00
|
|
|
void GetAddr_(std::vector<CAddress> &vAddr);
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Mark an entry as currently-connected-to.
|
2013-04-12 22:13:08 -07:00
|
|
|
void Connected_(const CService &addr, int64_t nTime);
|
2012-01-04 14:39:45 -08:00
|
|
|
|
|
|
|
public:
|
2014-10-23 21:04:27 -07:00
|
|
|
/**
|
|
|
|
* serialized format:
|
2015-03-18 09:31:49 -07:00
|
|
|
* * version byte (currently 1)
|
|
|
|
* * 0x20 + nKey (serialized as if it were a vector, for backward compatibility)
|
2014-10-23 21:04:27 -07:00
|
|
|
* * nNew
|
|
|
|
* * nTried
|
2015-03-18 09:31:49 -07:00
|
|
|
* * number of "new" buckets XOR 2**30
|
2014-10-23 21:04:27 -07:00
|
|
|
* * all nNew addrinfos in vvNew
|
|
|
|
* * all nTried addrinfos in vvTried
|
|
|
|
* * for each bucket:
|
|
|
|
* * number of elements
|
|
|
|
* * for each element: index
|
|
|
|
*
|
2015-03-18 09:31:49 -07: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-23 21:04:27 -07:00
|
|
|
* Notice that vvTried, mapAddr and vVector are never encoded explicitly;
|
|
|
|
* they are instead reconstructed from the other information.
|
|
|
|
*
|
|
|
|
* vvNew is serialized, but only used if ADDRMAN_UNKOWN_BUCKET_COUNT didn't change,
|
|
|
|
* 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 11:16:58 -07:00
|
|
|
template<typename Stream>
|
|
|
|
void Serialize(Stream &s, int nType, int nVersionDummy) const
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
2015-03-18 09:31:49 -07:00
|
|
|
unsigned char nVersion = 1;
|
2014-07-10 11:16:58 -07:00
|
|
|
s << nVersion;
|
2015-03-08 06:30:05 -07:00
|
|
|
s << ((unsigned char)32);
|
2014-07-10 11:16:58 -07:00
|
|
|
s << nKey;
|
|
|
|
s << nNew;
|
|
|
|
s << nTried;
|
|
|
|
|
2015-03-18 09:31:49 -07:00
|
|
|
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
|
2014-07-10 11:16:58 -07:00
|
|
|
s << nUBuckets;
|
|
|
|
std::map<int, int> mapUnkIds;
|
|
|
|
int nIds = 0;
|
|
|
|
for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
|
|
|
|
mapUnkIds[(*it).first] = nIds;
|
|
|
|
const CAddrInfo &info = (*it).second;
|
|
|
|
if (info.nRefCount) {
|
2015-03-18 09:31:49 -07:00
|
|
|
assert(nIds != nNew); // this means nNew was wrong, oh ow
|
2014-07-10 11:16:58 -07:00
|
|
|
s << info;
|
|
|
|
nIds++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nIds = 0;
|
|
|
|
for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
|
|
|
|
const CAddrInfo &info = (*it).second;
|
|
|
|
if (info.fInTried) {
|
2015-03-18 09:31:49 -07:00
|
|
|
assert(nIds != nTried); // this means nTried was wrong, oh ow
|
2014-07-10 11:16:58 -07:00
|
|
|
s << info;
|
|
|
|
nIds++;
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 09:31:49 -07: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 11:16:58 -07:00
|
|
|
s << nSize;
|
2015-03-18 09:31:49 -07: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 11:16:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2014-07-10 11:16:58 -07:00
|
|
|
template<typename Stream>
|
|
|
|
void Unserialize(Stream& s, int nType, int nVersionDummy)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
2015-03-18 09:31:49 -07:00
|
|
|
Clear();
|
|
|
|
|
2014-07-10 11:16:58 -07:00
|
|
|
unsigned char nVersion;
|
|
|
|
s >> nVersion;
|
2015-03-08 06:30:05 -07:00
|
|
|
unsigned char nKeySize;
|
|
|
|
s >> nKeySize;
|
2015-03-18 09:31:49 -07:00
|
|
|
if (nKeySize != 32) throw std::ios_base::failure("Incorrect keysize in addrman deserialization");
|
2014-07-10 11:16:58 -07:00
|
|
|
s >> nKey;
|
|
|
|
s >> nNew;
|
|
|
|
s >> nTried;
|
|
|
|
int nUBuckets = 0;
|
|
|
|
s >> nUBuckets;
|
2015-03-18 09:31:49 -07:00
|
|
|
if (nVersion != 0) {
|
|
|
|
nUBuckets ^= (1 << 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize entries from the new table.
|
2014-07-10 11:16:58 -07: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 09:31:49 -07: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 11:16:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
nIdCount = nNew;
|
2015-03-18 09:31:49 -07:00
|
|
|
|
|
|
|
// Deserialize entries from the tried table.
|
2014-07-10 11:16:58 -07:00
|
|
|
int nLost = 0;
|
|
|
|
for (int n = 0; n < nTried; n++) {
|
|
|
|
CAddrInfo info;
|
|
|
|
s >> info;
|
2015-03-18 09:31:49 -07:00
|
|
|
int nKBucket = info.GetTriedBucket(nKey);
|
|
|
|
int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
|
|
|
|
if (vvTried[nKBucket][nKBucketPos] == -1) {
|
2014-07-10 11:16:58 -07:00
|
|
|
info.nRandomPos = vRandom.size();
|
|
|
|
info.fInTried = true;
|
|
|
|
vRandom.push_back(nIdCount);
|
|
|
|
mapInfo[nIdCount] = info;
|
|
|
|
mapAddr[info] = nIdCount;
|
2015-03-18 09:31:49 -07:00
|
|
|
vvTried[nKBucket][nKBucketPos] = nIdCount;
|
2014-07-10 11:16:58 -07:00
|
|
|
nIdCount++;
|
2012-01-04 14:39:45 -08:00
|
|
|
} else {
|
2014-07-10 11:16:58 -07:00
|
|
|
nLost++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nTried -= nLost;
|
2015-03-18 09:31:49 -07:00
|
|
|
|
|
|
|
// Deserialize positions in the new table (if possible).
|
|
|
|
for (int bucket = 0; bucket < nUBuckets; bucket++) {
|
2014-07-10 11:16:58 -07:00
|
|
|
int nSize = 0;
|
|
|
|
s >> nSize;
|
|
|
|
for (int n = 0; n < nSize; n++) {
|
|
|
|
int nIndex = 0;
|
|
|
|
s >> nIndex;
|
2015-03-18 09:31:49 -07: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 14:39:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 09:31:49 -07: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) {
|
|
|
|
LogPrint("addrman", "addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk, nLost);
|
|
|
|
}
|
|
|
|
|
|
|
|
Check();
|
2014-07-10 11:16:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int GetSerializeSize(int nType, int nVersion) const
|
|
|
|
{
|
|
|
|
return (CSizeComputer(nType, nVersion) << *this).size();
|
|
|
|
}
|
2012-01-04 14:39:45 -08:00
|
|
|
|
2015-03-18 09:31:49 -07:00
|
|
|
void Clear()
|
2012-01-04 14:39:45 -08:00
|
|
|
{
|
2015-03-18 09:31:49 -07: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 14:39:45 -08:00
|
|
|
|
2015-03-18 09:31:49 -07:00
|
|
|
nIdCount = 0;
|
|
|
|
nTried = 0;
|
|
|
|
nNew = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAddrMan()
|
|
|
|
{
|
|
|
|
Clear();
|
2012-01-04 14:39:45 -08:00
|
|
|
}
|
|
|
|
|
2015-03-08 06:30:05 -07:00
|
|
|
~CAddrMan()
|
|
|
|
{
|
2015-03-18 09:31:49 -07:00
|
|
|
nKey.SetNull();
|
2015-03-08 06:30:05 -07:00
|
|
|
}
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Return the number of (unique) addresses in all tables.
|
2012-01-04 14:39:45 -08:00
|
|
|
int size()
|
|
|
|
{
|
|
|
|
return vRandom.size();
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Consistency check
|
2012-01-04 14:39:45 -08:00
|
|
|
void Check()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_ADDRMAN
|
|
|
|
{
|
2012-04-06 09:39:12 -07:00
|
|
|
LOCK(cs);
|
2012-01-04 14:39:45 -08:00
|
|
|
int err;
|
|
|
|
if ((err=Check_()))
|
2013-09-18 03:38:08 -07:00
|
|
|
LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
|
2012-01-04 14:39:45 -08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Add a single address.
|
2013-04-12 22:13:08 -07:00
|
|
|
bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0)
|
2012-01-04 14:39:45 -08:00
|
|
|
{
|
|
|
|
bool fRet = false;
|
|
|
|
{
|
2012-04-06 09:39:12 -07:00
|
|
|
LOCK(cs);
|
2012-01-04 14:39:45 -08:00
|
|
|
Check();
|
|
|
|
fRet |= Add_(addr, source, nTimePenalty);
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
if (fRet)
|
2014-09-08 03:25:52 -07:00
|
|
|
LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
|
2012-01-04 14:39:45 -08:00
|
|
|
return fRet;
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Add multiple addresses.
|
2013-04-12 22:13:08 -07:00
|
|
|
bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
|
2012-01-04 14:39:45 -08:00
|
|
|
{
|
|
|
|
int nAdd = 0;
|
|
|
|
{
|
2012-04-06 09:39:12 -07:00
|
|
|
LOCK(cs);
|
2012-01-04 14:39:45 -08:00
|
|
|
Check();
|
|
|
|
for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
|
|
|
|
nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
if (nAdd)
|
2014-05-21 03:50:46 -07:00
|
|
|
LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
|
2012-01-04 14:39:45 -08:00
|
|
|
return nAdd > 0;
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Mark an entry as accessible.
|
2013-04-12 22:13:08 -07:00
|
|
|
void Good(const CService &addr, int64_t nTime = GetAdjustedTime())
|
2012-01-04 14:39:45 -08:00
|
|
|
{
|
|
|
|
{
|
2012-04-06 09:39:12 -07:00
|
|
|
LOCK(cs);
|
2012-01-04 14:39:45 -08:00
|
|
|
Check();
|
|
|
|
Good_(addr, nTime);
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Mark an entry as connection attempted to.
|
2013-04-12 22:13:08 -07:00
|
|
|
void Attempt(const CService &addr, int64_t nTime = GetAdjustedTime())
|
2012-01-04 14:39:45 -08:00
|
|
|
{
|
|
|
|
{
|
2012-04-06 09:39:12 -07:00
|
|
|
LOCK(cs);
|
2012-01-04 14:39:45 -08:00
|
|
|
Check();
|
|
|
|
Attempt_(addr, nTime);
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
/**
|
|
|
|
* Choose an address to connect to.
|
|
|
|
* nUnkBias determines how much "new" entries are favored over "tried" ones (0-100).
|
|
|
|
*/
|
2015-04-19 11:10:13 -07:00
|
|
|
CAddrInfo Select()
|
2012-01-04 14:39:45 -08:00
|
|
|
{
|
2015-04-19 11:10:13 -07:00
|
|
|
CAddrInfo addrRet;
|
2012-01-04 14:39:45 -08:00
|
|
|
{
|
2012-04-06 09:39:12 -07:00
|
|
|
LOCK(cs);
|
2012-01-04 14:39:45 -08:00
|
|
|
Check();
|
2015-03-19 09:51:59 -07:00
|
|
|
addrRet = Select_();
|
2012-01-04 14:39:45 -08:00
|
|
|
Check();
|
|
|
|
}
|
|
|
|
return addrRet;
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Return a bunch of addresses, selected at random.
|
2012-01-04 14:39:45 -08:00
|
|
|
std::vector<CAddress> GetAddr()
|
|
|
|
{
|
|
|
|
Check();
|
|
|
|
std::vector<CAddress> vAddr;
|
2012-04-06 09:39:12 -07:00
|
|
|
{
|
|
|
|
LOCK(cs);
|
2012-01-04 14:39:45 -08:00
|
|
|
GetAddr_(vAddr);
|
2012-04-06 09:39:12 -07:00
|
|
|
}
|
2012-01-04 14:39:45 -08:00
|
|
|
Check();
|
|
|
|
return vAddr;
|
|
|
|
}
|
|
|
|
|
2014-10-23 21:04:27 -07:00
|
|
|
//! Mark an entry as currently-connected-to.
|
2013-04-12 22:13:08 -07:00
|
|
|
void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
|
2012-01-04 14:39:45 -08:00
|
|
|
{
|
|
|
|
{
|
2012-04-06 09:39:12 -07:00
|
|
|
LOCK(cs);
|
2012-01-04 14:39:45 -08:00
|
|
|
Check();
|
|
|
|
Connected_(addr, nTime);
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-03 07:16:40 -08:00
|
|
|
#endif // BITCOIN_ADDRMAN_H
|