2012-01-04 23:39:45 +01:00
|
|
|
// Copyright (c) 2012 Pieter Wuille
|
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
|
|
|
|
|
|
|
#include "netbase.h"
|
|
|
|
#include "protocol.h"
|
2014-06-26 14:41:53 +02:00
|
|
|
#include "random.h"
|
2012-05-11 17:00:03 +02:00
|
|
|
#include "sync.h"
|
2014-06-19 15:08:37 +02:00
|
|
|
#include "timedata.h"
|
2015-03-08 14:30:05 +01:00
|
|
|
#include "uint256.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#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>
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
/**
|
|
|
|
* Extended statistics about a CAddress
|
|
|
|
*/
|
2012-01-04 23:39:45 +01:00
|
|
|
class CAddrInfo : public CAddress
|
|
|
|
{
|
|
|
|
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
|
|
|
//! last try whatsoever by us:
|
2013-04-13 07:13:08 +02:00
|
|
|
// int64_t CAddress::nLastTry
|
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>
|
2014-08-21 00:49:32 +02:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
|
|
|
READWRITE(*(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;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
* * Keep the address tables in-memory, and asynchronously dump the entire to able in peers.dat.
|
|
|
|
* * Make sure no (localized) attacker can fill the entire table with his nodes/addresses.
|
|
|
|
*
|
|
|
|
* To that end:
|
|
|
|
* * Addresses are organized into buckets.
|
|
|
|
* * Address that have not yet been tried go into 256 "new" buckets.
|
|
|
|
* * Based on the address range (/16 for IPv4) of source of the information, 32 buckets are selected at random
|
|
|
|
* * The actual bucket is chosen from one of these, based on the range the address itself is located.
|
|
|
|
* * One single address can occur in up to 4 different buckets, to increase selection chances for addresses that
|
|
|
|
* 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.
|
|
|
|
* * Addresses of nodes that are known to be accessible go into 64 "tried" buckets.
|
|
|
|
* * Each address range selects at random 4 of these buckets.
|
|
|
|
* * 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
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_TRIED_BUCKET_COUNT 64
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! maximum allowed number of entries in buckets for tried addresses
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_TRIED_BUCKET_SIZE 64
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! total number of buckets for new addresses
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_NEW_BUCKET_COUNT 256
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! maximum allowed number of entries in buckets for new addresses
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_NEW_BUCKET_SIZE 64
|
|
|
|
|
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
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_TRIED_BUCKETS_PER_GROUP 4
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! over how many buckets entries with new addresses originating from a single group are spread
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 32
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! in how many buckets for entries with new addresses a single address may occur
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 4
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! how many entries in a bucket with tried addresses are inspected, when selecting one to replace
|
2012-01-04 23:39:45 +01:00
|
|
|
#define ADDRMAN_TRIED_ENTRIES_INSPECT_ON_EVICT 4
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
/**
|
|
|
|
* Stochastical (IP) address manager
|
|
|
|
*/
|
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
|
|
|
//! secret key to randomize bucket select with
|
2015-03-08 14:30:05 +01:00
|
|
|
uint256 nKey;
|
2012-01-04 23:39:45 +01:00
|
|
|
|
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
|
2012-01-04 23:39:45 +01:00
|
|
|
std::vector<std::vector<int> > vvTried;
|
|
|
|
|
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
|
2012-01-04 23:39:45 +01:00
|
|
|
std::vector<std::set<int> > vvNew;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Find an entry.
|
2012-01-04 23:39:45 +01:00
|
|
|
CAddrInfo* Find(const CNetAddr& addr, int *pnId = NULL);
|
|
|
|
|
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.
|
2012-01-04 23:39:45 +01:00
|
|
|
CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = NULL);
|
|
|
|
|
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
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Return position in given bucket to replace.
|
2012-01-04 23:39:45 +01:00
|
|
|
int SelectTried(int nKBucket);
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Remove an element from a "new" bucket.
|
|
|
|
//! This is the only place where actual deletions occur.
|
|
|
|
//! Elements are never deleted while in the "tried" table, only possibly evicted back to the "new" table.
|
2012-01-04 23:39:45 +01:00
|
|
|
int ShrinkNew(int nUBucket);
|
2012-09-18 21:07:58 +02:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Move an entry from the "new" table(s) to the "tried" table
|
|
|
|
//! @pre vvUnkown[nOrigin].count(nId) != 0
|
2012-01-04 23:39:45 +01:00
|
|
|
void MakeTried(CAddrInfo& info, int nId, int nOrigin);
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Mark an entry "good", possibly moving it from "new" to "tried".
|
2013-04-13 07:13:08 +02:00
|
|
|
void Good_(const CService &addr, int64_t nTime);
|
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.
|
2013-04-13 07:13:08 +02:00
|
|
|
void Attempt_(const CService &addr, int64_t nTime);
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Select an address to connect to.
|
|
|
|
//! nUnkBias determines how much to favor new addresses over tried ones (min=0, max=100)
|
2012-01-04 23:39:45 +01:00
|
|
|
CAddress Select_(int nUnkBias);
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
public:
|
2014-10-24 06:04:27 +02:00
|
|
|
/**
|
|
|
|
* serialized format:
|
|
|
|
* * version byte (currently 0)
|
|
|
|
* * nKey
|
|
|
|
* * nNew
|
|
|
|
* * nTried
|
|
|
|
* * number of "new" buckets
|
|
|
|
* * all nNew addrinfos in vvNew
|
|
|
|
* * all nTried addrinfos in vvTried
|
|
|
|
* * for each bucket:
|
|
|
|
* * number of elements
|
|
|
|
* * for each element: index
|
|
|
|
*
|
|
|
|
* 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 20:16:58 +02:00
|
|
|
template<typename Stream>
|
|
|
|
void Serialize(Stream &s, int nType, int nVersionDummy) const
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
|
|
|
unsigned char nVersion = 0;
|
|
|
|
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;
|
|
|
|
|
|
|
|
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT;
|
|
|
|
s << nUBuckets;
|
|
|
|
std::map<int, int> mapUnkIds;
|
|
|
|
int nIds = 0;
|
|
|
|
for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
|
|
|
|
if (nIds == nNew) break; // this means nNew was wrong, oh ow
|
|
|
|
mapUnkIds[(*it).first] = nIds;
|
|
|
|
const CAddrInfo &info = (*it).second;
|
|
|
|
if (info.nRefCount) {
|
|
|
|
s << info;
|
|
|
|
nIds++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nIds = 0;
|
|
|
|
for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
|
|
|
|
if (nIds == nTried) break; // this means nTried was wrong, oh ow
|
|
|
|
const CAddrInfo &info = (*it).second;
|
|
|
|
if (info.fInTried) {
|
|
|
|
s << info;
|
|
|
|
nIds++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (std::vector<std::set<int> >::const_iterator it = vvNew.begin(); it != vvNew.end(); it++) {
|
|
|
|
const std::set<int> &vNew = (*it);
|
|
|
|
int nSize = vNew.size();
|
|
|
|
s << nSize;
|
|
|
|
for (std::set<int>::const_iterator it2 = vNew.begin(); it2 != vNew.end(); it2++) {
|
|
|
|
int nIndex = mapUnkIds[*it2];
|
|
|
|
s << nIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-04 23:39:45 +01:00
|
|
|
|
2014-07-10 20:16:58 +02:00
|
|
|
template<typename Stream>
|
|
|
|
void Unserialize(Stream& s, int nType, int nVersionDummy)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
|
|
|
|
unsigned char nVersion;
|
|
|
|
s >> nVersion;
|
2015-03-08 14:30:05 +01:00
|
|
|
unsigned char nKeySize;
|
|
|
|
s >> nKeySize;
|
|
|
|
if (nKeySize != 32) throw std::ios_base::failure("Incorrect keysize in addrman");
|
2014-07-10 20:16:58 +02:00
|
|
|
s >> nKey;
|
|
|
|
s >> nNew;
|
|
|
|
s >> nTried;
|
|
|
|
|
|
|
|
int nUBuckets = 0;
|
|
|
|
s >> nUBuckets;
|
|
|
|
nIdCount = 0;
|
|
|
|
mapInfo.clear();
|
|
|
|
mapAddr.clear();
|
|
|
|
vRandom.clear();
|
|
|
|
vvTried = std::vector<std::vector<int> >(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0));
|
|
|
|
vvNew = std::vector<std::set<int> >(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>());
|
|
|
|
for (int n = 0; n < nNew; n++) {
|
|
|
|
CAddrInfo &info = mapInfo[n];
|
|
|
|
s >> info;
|
|
|
|
mapAddr[info] = n;
|
|
|
|
info.nRandomPos = vRandom.size();
|
|
|
|
vRandom.push_back(n);
|
|
|
|
if (nUBuckets != ADDRMAN_NEW_BUCKET_COUNT) {
|
|
|
|
vvNew[info.GetNewBucket(nKey)].insert(n);
|
|
|
|
info.nRefCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nIdCount = nNew;
|
|
|
|
int nLost = 0;
|
|
|
|
for (int n = 0; n < nTried; n++) {
|
|
|
|
CAddrInfo info;
|
|
|
|
s >> info;
|
|
|
|
std::vector<int> &vTried = vvTried[info.GetTriedBucket(nKey)];
|
|
|
|
if (vTried.size() < ADDRMAN_TRIED_BUCKET_SIZE) {
|
|
|
|
info.nRandomPos = vRandom.size();
|
|
|
|
info.fInTried = true;
|
|
|
|
vRandom.push_back(nIdCount);
|
|
|
|
mapInfo[nIdCount] = info;
|
|
|
|
mapAddr[info] = nIdCount;
|
|
|
|
vTried.push_back(nIdCount);
|
|
|
|
nIdCount++;
|
2012-01-04 23:39:45 +01:00
|
|
|
} else {
|
2014-07-10 20:16:58 +02:00
|
|
|
nLost++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nTried -= nLost;
|
|
|
|
for (int b = 0; b < nUBuckets; b++) {
|
|
|
|
std::set<int> &vNew = vvNew[b];
|
|
|
|
int nSize = 0;
|
|
|
|
s >> nSize;
|
|
|
|
for (int n = 0; n < nSize; n++) {
|
|
|
|
int nIndex = 0;
|
|
|
|
s >> nIndex;
|
|
|
|
CAddrInfo &info = mapInfo[nIndex];
|
|
|
|
if (nUBuckets == ADDRMAN_NEW_BUCKET_COUNT && info.nRefCount < ADDRMAN_NEW_BUCKETS_PER_ADDRESS) {
|
|
|
|
info.nRefCount++;
|
|
|
|
vNew.insert(nIndex);
|
2012-01-04 23:39:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-10 20:16:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int GetSerializeSize(int nType, int nVersion) const
|
|
|
|
{
|
|
|
|
return (CSizeComputer(nType, nVersion) << *this).size();
|
|
|
|
}
|
2012-01-04 23:39:45 +01:00
|
|
|
|
|
|
|
CAddrMan() : vRandom(0), vvTried(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0)), vvNew(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>())
|
|
|
|
{
|
2015-03-08 14:30:05 +01:00
|
|
|
nKey = GetRandHash();
|
2012-01-04 23:39:45 +01:00
|
|
|
|
|
|
|
nIdCount = 0;
|
|
|
|
nTried = 0;
|
|
|
|
nNew = 0;
|
|
|
|
}
|
|
|
|
|
2015-03-08 14:30:05 +01:00
|
|
|
~CAddrMan()
|
|
|
|
{
|
|
|
|
nKey.SetNull();
|
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Return the number of (unique) addresses in all tables.
|
2012-01-04 23:39:45 +01:00
|
|
|
int size()
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
bool fRet = false;
|
|
|
|
{
|
2012-04-06 18:39:12 +02:00
|
|
|
LOCK(cs);
|
2012-01-04 23:39:45 +01:00
|
|
|
Check();
|
|
|
|
fRet |= Add_(addr, source, nTimePenalty);
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
if (fRet)
|
2014-09-08 12:25:52 +02:00
|
|
|
LogPrint("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
|
|
|
{
|
|
|
|
int nAdd = 0;
|
|
|
|
{
|
2012-04-06 18:39:12 +02:00
|
|
|
LOCK(cs);
|
2012-01-04 23:39:45 +01: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 12:50:46 +02:00
|
|
|
LogPrint("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.
|
2013-04-13 07:13:08 +02:00
|
|
|
void Good(const CService &addr, int64_t nTime = GetAdjustedTime())
|
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();
|
|
|
|
Good_(addr, nTime);
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
//! Mark an entry as connection attempted to.
|
2013-04-13 07:13:08 +02:00
|
|
|
void Attempt(const CService &addr, int64_t nTime = GetAdjustedTime())
|
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();
|
|
|
|
Attempt_(addr, nTime);
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-24 06:04:27 +02:00
|
|
|
/**
|
|
|
|
* Choose an address to connect to.
|
|
|
|
* nUnkBias determines how much "new" entries are favored over "tried" ones (0-100).
|
|
|
|
*/
|
2012-01-04 23:39:45 +01:00
|
|
|
CAddress Select(int nUnkBias = 50)
|
|
|
|
{
|
|
|
|
CAddress addrRet;
|
|
|
|
{
|
2012-04-06 18:39:12 +02:00
|
|
|
LOCK(cs);
|
2012-01-04 23:39:45 +01:00
|
|
|
Check();
|
|
|
|
addrRet = Select_(nUnkBias);
|
|
|
|
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
|
|
|
{
|
|
|
|
{
|
2012-04-06 18:39:12 +02:00
|
|
|
LOCK(cs);
|
2012-01-04 23:39:45 +01:00
|
|
|
Check();
|
|
|
|
Connected_(addr, nTime);
|
|
|
|
Check();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#endif // BITCOIN_ADDRMAN_H
|