Changeset View
Changeset View
Standalone View
Standalone View
src/addrman.h
Show First 20 Lines • Show All 168 Lines • ▼ Show 20 Lines | |||||
#define ADDRMAN_RETRIES 3 | #define ADDRMAN_RETRIES 3 | ||||
//! how many successive failures are allowed ... | //! how many successive failures are allowed ... | ||||
#define ADDRMAN_MAX_FAILURES 10 | #define ADDRMAN_MAX_FAILURES 10 | ||||
//! ... in at least this many days | //! ... in at least this many days | ||||
#define ADDRMAN_MIN_FAIL_DAYS 7 | #define ADDRMAN_MIN_FAIL_DAYS 7 | ||||
//! how recent a successful connection should be before we allow an address to | |||||
//! be evicted from tried | |||||
#define ADDRMAN_REPLACEMENT_SECONDS (4 * 60 * 60) | |||||
//! the maximum percentage of nodes to return in a getaddr call | //! the maximum percentage of nodes to return in a getaddr call | ||||
#define ADDRMAN_GETADDR_MAX_PCT 23 | #define ADDRMAN_GETADDR_MAX_PCT 23 | ||||
//! the maximum number of nodes to return in a getaddr call | //! the maximum number of nodes to return in a getaddr call | ||||
#define ADDRMAN_GETADDR_MAX 2500 | #define ADDRMAN_GETADDR_MAX 2500 | ||||
//! Convenience | //! Convenience | ||||
#define ADDRMAN_TRIED_BUCKET_COUNT (1 << ADDRMAN_TRIED_BUCKET_COUNT_LOG2) | #define ADDRMAN_TRIED_BUCKET_COUNT (1 << ADDRMAN_TRIED_BUCKET_COUNT_LOG2) | ||||
#define ADDRMAN_NEW_BUCKET_COUNT (1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2) | #define ADDRMAN_NEW_BUCKET_COUNT (1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2) | ||||
#define ADDRMAN_BUCKET_SIZE (1 << ADDRMAN_BUCKET_SIZE_LOG2) | #define ADDRMAN_BUCKET_SIZE (1 << ADDRMAN_BUCKET_SIZE_LOG2) | ||||
//! the maximum number of tried addr collisions to store | |||||
#define ADDRMAN_SET_TRIED_COLLISION_SIZE 10 | |||||
/** | /** | ||||
* Stochastical (IP) address manager | * Stochastical (IP) address manager | ||||
*/ | */ | ||||
class CAddrMan { | class CAddrMan { | ||||
private: | private: | ||||
//! critical section to protect the inner data structures | //! critical section to protect the inner data structures | ||||
mutable CCriticalSection cs; | mutable CCriticalSection cs; | ||||
Show All 19 Lines | private: | ||||
int nNew; | int nNew; | ||||
//! list of "new" buckets | //! list of "new" buckets | ||||
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE]; | int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE]; | ||||
//! last time Good was called (memory only) | //! last time Good was called (memory only) | ||||
int64_t nLastGood; | int64_t nLastGood; | ||||
//! Holds addrs inserted into tried table that collide with existing | |||||
//! entries. Test-before-evict discpline used to resolve these collisions. | |||||
std::set<int> m_tried_collisions; | |||||
protected: | protected: | ||||
//! secret key to randomize bucket select with | //! secret key to randomize bucket select with | ||||
uint256 nKey; | uint256 nKey; | ||||
//! Source of random numbers for randomization in inner loops | //! Source of random numbers for randomization in inner loops | ||||
FastRandomContext insecure_rand; | FastRandomContext insecure_rand; | ||||
//! Find an entry. | //! Find an entry. | ||||
Show All 13 Lines | protected: | ||||
//! Delete an entry. It must not be in tried, and have refcount 0. | //! Delete an entry. It must not be in tried, and have refcount 0. | ||||
void Delete(int nId); | void Delete(int nId); | ||||
//! Clear a position in a "new" table. This is the only place where entries | //! Clear a position in a "new" table. This is the only place where entries | ||||
//! are actually deleted. | //! are actually deleted. | ||||
void ClearNew(int nUBucket, int nUBucketPos); | void ClearNew(int nUBucket, int nUBucketPos); | ||||
//! Mark an entry "good", possibly moving it from "new" to "tried". | //! Mark an entry "good", possibly moving it from "new" to "tried". | ||||
void Good_(const CService &addr, int64_t nTime); | void Good_(const CService &addr, bool test_before_evict, int64_t time); | ||||
//! Add an entry to the "new" table. | //! Add an entry to the "new" table. | ||||
bool Add_(const CAddress &addr, const CNetAddr &source, | bool Add_(const CAddress &addr, const CNetAddr &source, | ||||
int64_t nTimePenalty); | int64_t nTimePenalty); | ||||
//! Mark an entry as attempted to connect. | //! Mark an entry as attempted to connect. | ||||
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime); | void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime); | ||||
//! Select an address to connect to, if newOnly is set to true, only the new | //! Select an address to connect to, if newOnly is set to true, only the new | ||||
//! table is selected from. | //! table is selected from. | ||||
CAddrInfo Select_(bool newOnly); | CAddrInfo Select_(bool newOnly); | ||||
//! 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_(); | |||||
//! Wraps GetRandInt to allow tests to override RandomInt and make it | //! Wraps GetRandInt to allow tests to override RandomInt and make it | ||||
//! determinismistic. | //! determinismistic. | ||||
virtual int RandomInt(int nMax); | virtual int RandomInt(int nMax); | ||||
#ifdef DEBUG_ADDRMAN | #ifdef DEBUG_ADDRMAN | ||||
//! Perform consistency check. Returns an error code or zero. | //! Perform consistency check. Returns an error code or zero. | ||||
int Check_(); | int Check_(); | ||||
#endif | #endif | ||||
▲ Show 20 Lines • Show All 280 Lines • ▼ Show 20 Lines | bool Add(const std::vector<CAddress> &vAddr, const CNetAddr &source, | ||||
LogPrint(BCLog::ADDRMAN, | LogPrint(BCLog::ADDRMAN, | ||||
"Added %i addresses from %s: %i tried, %i new\n", nAdd, | "Added %i addresses from %s: %i tried, %i new\n", nAdd, | ||||
source.ToString(), nTried, nNew); | source.ToString(), nTried, nNew); | ||||
} | } | ||||
return nAdd > 0; | return nAdd > 0; | ||||
} | } | ||||
//! Mark an entry as accessible. | //! Mark an entry as accessible. | ||||
void Good(const CService &addr, int64_t nTime = GetAdjustedTime()) { | void Good(const CService &addr, bool test_before_evict = true, | ||||
int64_t nTime = GetAdjustedTime()) { | |||||
LOCK(cs); | LOCK(cs); | ||||
Check(); | Check(); | ||||
Good_(addr, nTime); | Good_(addr, test_before_evict, nTime); | ||||
Check(); | Check(); | ||||
} | } | ||||
//! Mark an entry as connection attempted to. | //! Mark an entry as connection attempted to. | ||||
void Attempt(const CService &addr, bool fCountFailure, | void Attempt(const CService &addr, bool fCountFailure, | ||||
int64_t nTime = GetAdjustedTime()) { | int64_t nTime = GetAdjustedTime()) { | ||||
LOCK(cs); | LOCK(cs); | ||||
Check(); | Check(); | ||||
Attempt_(addr, fCountFailure, nTime); | Attempt_(addr, fCountFailure, nTime); | ||||
Check(); | Check(); | ||||
} | } | ||||
//! 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; | |||||
} | |||||
/** | /** | ||||
* Choose an address to connect to. | * Choose an address to connect to. | ||||
*/ | */ | ||||
CAddrInfo Select(bool newOnly = false) { | CAddrInfo Select(bool newOnly = false) { | ||||
CAddrInfo addrRet; | CAddrInfo addrRet; | ||||
{ | { | ||||
LOCK(cs); | LOCK(cs); | ||||
Check(); | Check(); | ||||
Show All 35 Lines |