6 #ifndef BITCOIN_ADDRMAN_H 7 #define BITCOIN_ADDRMAN_H 60 template <
typename Stream,
typename Operation>
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_REPLACEMENT_HOURS 4 172 #define ADDRMAN_GETADDR_MAX_PCT 23 175 #define ADDRMAN_GETADDR_MAX 2500 178 #define ADDRMAN_TRIED_BUCKET_COUNT (1 << ADDRMAN_TRIED_BUCKET_COUNT_LOG2) 179 #define ADDRMAN_NEW_BUCKET_COUNT (1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2) 180 #define ADDRMAN_BUCKET_SIZE (1 << ADDRMAN_BUCKET_SIZE_LOG2) 183 #define ADDRMAN_SET_TRIED_COLLISION_SIZE 10 316 template<
typename Stream>
321 unsigned char nVersion = 1;
323 s << ((
unsigned char)32);
330 std::map<int, int> mapUnkIds;
332 for (
const auto& entry : mapInfo) {
333 mapUnkIds[entry.first] = nIds;
336 assert(nIds != nNew);
342 for (
const auto& entry : mapInfo) {
345 assert(nIds != nTried);
353 if (vvNew[bucket][i] != -1)
358 if (vvNew[bucket][i] != -1) {
359 int nIndex = mapUnkIds[vvNew[bucket][i]];
366 template<
typename Stream>
373 unsigned char nVersion;
375 unsigned char nKeySize;
377 if (nKeySize != 32)
throw std::ios_base::failure(
"Incorrect keysize in addrman deserialization");
384 nUBuckets ^= (1 << 30);
388 throw std::ios_base::failure(
"Corrupt CAddrMan serialization, nNew exceeds limit.");
392 throw std::ios_base::failure(
"Corrupt CAddrMan serialization, nTried exceeds limit.");
396 for (
int n = 0; n < nNew; n++) {
401 vRandom.push_back(n);
405 int nUBucket = info.GetNewBucket(
nKey);
406 int nUBucketPos = info.GetBucketPosition(
nKey,
true, nUBucket);
407 if (vvNew[nUBucket][nUBucketPos] == -1) {
408 vvNew[nUBucket][nUBucketPos] = n;
417 for (
int n = 0; n < nTried; n++) {
421 int nKBucketPos = info.GetBucketPosition(
nKey,
false, nKBucket);
422 if (vvTried[nKBucket][nKBucketPos] == -1) {
423 info.nRandomPos = vRandom.size();
424 info.fInTried =
true;
425 vRandom.push_back(nIdCount);
426 mapInfo[nIdCount] = info;
427 mapAddr[info] = nIdCount;
428 vvTried[nKBucket][nKBucketPos] = nIdCount;
437 for (
int bucket = 0; bucket < nUBuckets; bucket++) {
440 for (
int n = 0; n < nSize; n++) {
443 if (nIndex >= 0 && nIndex < nNew) {
448 vvNew[bucket][nUBucketPos] = nIndex;
456 for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); ) {
457 if (it->second.fInTried ==
false && it->second.nRefCount == 0) {
458 std::map<int, CAddrInfo>::const_iterator itCopy = it++;
465 if (nLost + nLostUnk > 0) {
466 LogPrint(
BCLog::ADDRMAN,
"addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk, nLost);
475 std::vector<int>().swap(vRandom);
479 vvNew[bucket][entry] = -1;
484 vvTried[bucket][entry] = -1;
510 return vRandom.size();
521 LogPrintf(
"ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
546 for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
547 nAdd +=
Add_(*it,
source, nTimePenalty) ? 1 : 0;
550 LogPrint(
BCLog::ADDRMAN,
"Added %i addresses from %s: %i tried, %i new\n", nAdd,
source.ToString(), nTried, nNew);
560 Good_(addr, test_before_evict, nTime);
569 Attempt_(addr, fCountFailure, nTime);
614 std::vector<CAddress> vAddr;
642 #endif // BITCOIN_ADDRMAN_H int nRefCount
reference count in new sets (memory only)
ServiceFlags
nServices flags
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions...
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.
CAddrInfo * Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
find an entry, creating it if necessary.
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Update an entry's service bits.
UniValue ret(UniValue::VARR)
void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Delete an entry. It must not be in tried, and have refcount 0.
void Good_(const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Mark an entry "good", possibly moving it from "new" to "tried".
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Mark an entry as attempted to connect.
int nAttempts
connection attempts since last successful attempt
bool Add_(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Add an entry to the "new" table.
std::set< int > m_tried_collisions
Holds addrs inserted into tried table that collide with existing entries. Test-before-evict disciplin...
int nIdCount GUARDED_BY(cs)
last used nId
virtual int RandomInt(int nMax)
Wraps GetRandInt to allow tests to override RandomInt and make it determinismistic.
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
int nRandomPos
position in vRandom
CAddrInfo SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Return a random to-be-evicted tried table address.
bool Add(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty=0)
Add a single address.
#define ADDRMAN_TRIED_BUCKET_COUNT
Convenience.
#define READWRITEAS(type, obj)
bool fInTried
in tried set? (memory only)
Stochastical (IP) address manager.
std::vector< CAddress > GetAddr()
Return a bunch of addresses, selected at random.
Extended statistics about a CAddress.
int GetNewBucket(const uint256 &nKey, const CNetAddr &src) const
Calculate in which "new" bucket this entry belongs, given a certain source.
int GetTriedBucket(const uint256 &nKey) const
Calculate in which "tried" bucket this entry belongs.
FastRandomContext insecure_rand
Source of random numbers for randomization in inner loops.
void MakeTried(CAddrInfo &info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Move an entry from the "new" table(s) to the "tried" table.
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
bool IsTerrible(int64_t nNow=GetAdjustedTime()) const
Determine whether the statistics about this entry are bad enough so that it can just be deleted...
void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs)
Clear a position in a "new" table. This is the only place where entries are actually deleted...
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
size_t size() const
Return the number of (unique) addresses in all tables.
#define EXCLUSIVE_LOCKS_REQUIRED(...)
CAddrInfo Select_(bool newOnly) EXCLUSIVE_LOCKS_REQUIRED(cs)
Select an address to connect to, if newOnly is set to true, only the new table is selected from...
int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
Calculate in which position of a bucket to store this entry.
void Connected_(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Mark an entry as currently-connected-to.
CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
#define ADDRMAN_NEW_BUCKET_COUNT
int64_t GetAdjustedTime()
CAddrInfo SelectTriedCollision()
Randomly select an address in tried that another address is attempting to evict.
std::string ToStringIPPort() const
int GetNewBucket(const uint256 &nKey) const
Calculate in which "new" bucket this entry belongs, using its default source.
void SerializationOp(Stream &s, Operation ser_action)
double GetChance(int64_t nNow=GetAdjustedTime()) const
Calculate the relative chance this entry should be given when selecting nodes to connect to...
int64_t nLastCountAttempt
last counted attempt (memory only)
void GetAddr_(std::vector< CAddress > &vAddr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Select several addresses at once.
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions...
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty=0)
Add multiple addresses.
void Good(const CService &addr, bool test_before_evict=true, int64_t nTime=GetAdjustedTime())
Mark an entry as accessible.
void SetServices(const CService &addr, ServiceFlags nServices)
uint256 nKey
secret key to randomize bucket select with
void Serialize(Stream &s) const
serialized format:
void Connected(const CService &addr, int64_t nTime=GetAdjustedTime())
Mark an entry as currently-connected-to.
int64_t nLastSuccess
last successful connection by us
CCriticalSection cs
critical section to protect the inner data structures
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
CAddrInfo * Find(const CNetAddr &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.