6 #ifndef FABCOIN_ADDRMAN_H 7 #define FABCOIN_ADDRMAN_H 60 template <
typename Stream,
typename Operation>
72 nLastCountAttempt = 0;
138 #define ADDRMAN_TRIED_BUCKET_COUNT_LOG2 8 142 #define ADDRMAN_NEW_BUCKET_COUNT_LOG2 10 145 #define ADDRMAN_BUCKET_SIZE_LOG2 6 148 #define ADDRMAN_TRIED_BUCKETS_PER_GROUP 8 151 #define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 64 154 #define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 8 157 #define ADDRMAN_HORIZON_DAYS 30 160 #define ADDRMAN_RETRIES 3 163 #define ADDRMAN_MAX_FAILURES 10 166 #define ADDRMAN_MIN_FAIL_DAYS 7 169 #define ADDRMAN_GETADDR_MAX_PCT 23 172 #define ADDRMAN_GETADDR_MAX 2500 175 #define ADDRMAN_TRIED_BUCKET_COUNT (1 << ADDRMAN_TRIED_BUCKET_COUNT_LOG2) 176 #define ADDRMAN_NEW_BUCKET_COUNT (1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2) 177 #define ADDRMAN_BUCKET_SIZE (1 << ADDRMAN_BUCKET_SIZE_LOG2) 230 void SwapRandom(
unsigned int nRandomPos1,
unsigned int nRandomPos2);
233 void MakeTried(
CAddrInfo& info,
int nId);
236 void Delete(
int nId);
239 void ClearNew(
int nUBucket,
int nUBucketPos);
248 void Attempt_(
const CService &addr,
bool fCountFailure, int64_t
nTime);
254 virtual int RandomInt(
int nMax);
262 void GetAddr_(std::vector<CAddress> &vAddr);
300 template<
typename Stream>
305 unsigned char nVersion = 1;
307 s << ((
unsigned char)32);
314 std::map<int, int> mapUnkIds;
316 for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
317 mapUnkIds[(*it).first] = nIds;
326 for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
337 if (vvNew[
bucket][i] != -1)
342 if (vvNew[
bucket][i] != -1) {
343 int nIndex = mapUnkIds[vvNew[
bucket][i]];
350 template<
typename Stream>
357 unsigned char nVersion;
359 unsigned char nKeySize;
361 if (nKeySize != 32)
throw std::ios_base::failure(
"Incorrect keysize in addrman deserialization");
368 nUBuckets ^= (1 << 30);
372 throw std::ios_base::failure(
"Corrupt CAddrMan serialization, nNew exceeds limit.");
376 throw std::ios_base::failure(
"Corrupt CAddrMan serialization, nTried exceeds limit.");
380 for (
int n = 0; n < nNew; n++) {
385 vRandom.push_back(n);
389 int nUBucket = info.GetNewBucket(nKey);
390 int nUBucketPos = info.GetBucketPosition(nKey,
true, nUBucket);
391 if (vvNew[nUBucket][nUBucketPos] == -1) {
392 vvNew[nUBucket][nUBucketPos] = n;
401 for (
int n = 0; n < nTried; n++) {
405 int nKBucketPos = info.GetBucketPosition(nKey,
false, nKBucket);
406 if (vvTried[nKBucket][nKBucketPos] == -1) {
407 info.nRandomPos = vRandom.size();
408 info.fInTried =
true;
409 vRandom.push_back(nIdCount);
410 mapInfo[nIdCount] = info;
411 mapAddr[info] = nIdCount;
412 vvTried[nKBucket][nKBucketPos] = nIdCount;
424 for (
int n = 0; n < nSize; n++) {
427 if (nIndex >= 0 && nIndex < nNew) {
432 vvNew[
bucket][nUBucketPos] = nIndex;
440 for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); ) {
441 if (it->second.fInTried ==
false && it->second.nRefCount == 0) {
442 std::map<int, CAddrInfo>::const_iterator itCopy = it++;
443 Delete(itCopy->first);
449 if (nLost + nLostUnk > 0) {
450 LogPrint(
BCLog::ADDRMAN,
"addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk, nLost);
458 std::vector<int>().
swap(vRandom);
462 vvNew[
bucket][entry] = -1;
467 vvTried[
bucket][entry] = -1;
493 return vRandom.size();
504 LogPrintf(
"ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
515 fRet |= Add_(addr, source, nTimePenalty);
529 for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
530 nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
552 Attempt_(addr, fCountFailure,
nTime);
565 addrRet = Select_(newOnly);
575 std::vector<CAddress> vAddr;
589 Connected_(addr,
nTime);
597 SetServices_(addr, nServices);
603 #endif // FABCOIN_ADDRMAN_H void Serialize(Stream &s) const
serialized format:
int nRefCount
reference count in new sets (memory only)
int GetTriedBucket(const uint256 &nKey) const
Calculate in which "tried" bucket this entry belongs.
ServiceFlags
nServices flags
CAddrInfo Select(bool newOnly=false)
Choose an address to connect to.
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime=GetAdjustedTime())
Mark an entry as connection attempted to.
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
Calculate in which position of a bucket to store this entry.
std::string ToStringIPPort() const
int64_t nLastGood
last time Good was called (memory only)
int nAttempts
connection attempts since last successful attempt
assert(len-trim+(2 *lenIndices)<=WIDTH)
int nRandomPos
position in vRandom
bool Add(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty=0)
Add a single address.
#define ADDRMAN_TRIED_BUCKET_COUNT
Convenience.
double GetChance(int64_t nNow=GetAdjustedTime()) const
Calculate the relative chance this entry should be given when selecting nodes to connect to...
bool fInTried
in tried set? (memory only)
Stochastical (IP) address manager.
std::vector< CAddress > GetAddr()
Return a bunch of addresses, selected at random.
size_t size() const
Return the number of (unique) addresses in all tables.
Extended statistics about a CAddress.
std::vector< int > vRandom
randomly-ordered vector of all nIds
FastRandomContext insecure_rand
Source of random numbers for randomization in inner loops.
void Unserialize(Stream &s)
A combination of a network address (CNetAddr) and a (TCP) port.
void Check()
Consistency check.
A CService with information about it as peer.
#define ADDRMAN_BUCKET_SIZE
int nIdCount
last used nId
std::map< CNetAddr, int > mapAddr
find an nId based on its network address
#define LogPrint(category,...)
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
#define ADDRMAN_NEW_BUCKET_COUNT
int64_t GetAdjustedTime()
void SerializationOp(Stream &s, Operation ser_action)
int64_t nLastCountAttempt
last counted attempt (memory only)
std::map< int, CAddrInfo > mapInfo
table with information about all nIds
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty=0)
Add multiple addresses.
std::string ToString() const
void SetServices(const CService &addr, ServiceFlags nServices)
uint256 nKey
secret key to randomize bucket select with
int nNew
number of (unique) "new" entries
void Connected(const CService &addr, int64_t nTime=GetAdjustedTime())
Mark an entry as currently-connected-to.
int GetNewBucket(const uint256 &nKey, const CNetAddr &src) const
Calculate in which "new" bucket this entry belongs, given a certain source.
int64_t nLastSuccess
last successful connection by us
bool IsTerrible(int64_t nNow=GetAdjustedTime()) const
Determine whether the statistics about this entry are bad enough so that it can just be deleted...
CCriticalSection cs
critical section to protect the inner data structures
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
int64_t nLastTry
last try whatsoever by us (memory only)
#define ADDRMAN_NEW_BUCKETS_PER_ADDRESS
in how many buckets for entries with new addresses a single address may occur
CNetAddr source
where knowledge about this address first came from
int GetNewBucket(const uint256 &nKey) const
Calculate in which "new" bucket this entry belongs, using its default source.
void Good(const CService &addr, int64_t nTime=GetAdjustedTime())
Mark an entry as accessible.