Changeset View
Changeset View
Standalone View
Standalone View
src/addrman.h
Show First 20 Lines • Show All 190 Lines • ▼ Show 20 Lines | |||||
//! the maximum number of tried addr collisions to store | //! the maximum number of tried addr collisions to store | ||||
#define ADDRMAN_SET_TRIED_COLLISION_SIZE 10 | #define ADDRMAN_SET_TRIED_COLLISION_SIZE 10 | ||||
/** | /** | ||||
* Stochastical (IP) address manager | * Stochastical (IP) address manager | ||||
*/ | */ | ||||
class CAddrMan { | class CAddrMan { | ||||
private: | protected: | ||||
//! critical section to protect the inner data structures | //! critical section to protect the inner data structures | ||||
mutable CCriticalSection cs; | mutable CCriticalSection cs; | ||||
private: | |||||
//! last used nId | //! last used nId | ||||
int nIdCount; | int nIdCount GUARDED_BY(cs); | ||||
//! table with information about all nIds | //! table with information about all nIds | ||||
std::map<int, CAddrInfo> mapInfo; | std::map<int, CAddrInfo> mapInfo GUARDED_BY(cs); | ||||
//! find an nId based on its network address | //! find an nId based on its network address | ||||
std::map<CNetAddr, int> mapAddr; | std::map<CNetAddr, int> mapAddr GUARDED_BY(cs); | ||||
//! randomly-ordered vector of all nIds | //! randomly-ordered vector of all nIds | ||||
std::vector<int> vRandom; | std::vector<int> vRandom GUARDED_BY(cs); | ||||
// number of "tried" entries | // number of "tried" entries | ||||
int nTried; | int nTried GUARDED_BY(cs); | ||||
//! list of "tried" buckets | //! list of "tried" buckets | ||||
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE]; | int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs); | ||||
//! number of (unique) "new" entries | //! number of (unique) "new" entries | ||||
int nNew; | int nNew GUARDED_BY(cs); | ||||
//! 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] GUARDED_BY(cs); | ||||
//! last time Good was called (memory only) | //! last time Good was called (memory only) | ||||
int64_t nLastGood; | int64_t nLastGood GUARDED_BY(cs); | ||||
//! Holds addrs inserted into tried table that collide with existing | //! Holds addrs inserted into tried table that collide with existing | ||||
//! entries. Test-before-evict discpline used to resolve these collisions. | //! entries. Test-before-evict discpline used to resolve these collisions. | ||||
std::set<int> m_tried_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. | ||||
CAddrInfo *Find(const CNetAddr &addr, int *pnId = nullptr); | CAddrInfo *Find(const CNetAddr &addr, int *pnId = nullptr) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs); | |||||
//! find an entry, creating it if necessary. | //! find an entry, creating it if necessary. | ||||
//! nTime and nServices of the found node are updated, if necessary. | //! nTime and nServices of the found node are updated, if necessary. | ||||
CAddrInfo *Create(const CAddress &addr, const CNetAddr &addrSource, | CAddrInfo *Create(const CAddress &addr, const CNetAddr &addrSource, | ||||
int *pnId = nullptr); | int *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs); | ||||
//! Swap two elements in vRandom. | //! Swap two elements in vRandom. | ||||
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2); | void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs); | |||||
//! Move an entry from the "new" table(s) to the "tried" table | //! Move an entry from the "new" table(s) to the "tried" table | ||||
void MakeTried(CAddrInfo &info, int nId); | void MakeTried(CAddrInfo &info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs); | ||||
//! 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) EXCLUSIVE_LOCKS_REQUIRED(cs); | ||||
//! 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) EXCLUSIVE_LOCKS_REQUIRED(cs); | ||||
//! 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, bool test_before_evict, int64_t time); | void Good_(const CService &addr, bool test_before_evict, int64_t time) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs); | |||||
//! 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) EXCLUSIVE_LOCKS_REQUIRED(cs); | ||||
//! 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) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs); | |||||
//! 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) EXCLUSIVE_LOCKS_REQUIRED(cs); | ||||
//! See if any to-be-evicted tried table entries have been tested and if so | //! See if any to-be-evicted tried table entries have been tested and if so | ||||
//! resolve the collisions. | //! resolve the collisions. | ||||
void ResolveCollisions_(); | void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs); | ||||
//! Return a random to-be-evicted tried table address. | //! Return a random to-be-evicted tried table address. | ||||
CAddrInfo SelectTriedCollision_(); | CAddrInfo SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs); | ||||
//! 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_() EXCLUSIVE_LOCKS_REQUIRED(cs); | ||||
#endif | #endif | ||||
//! Select several addresses at once. | //! Select several addresses at once. | ||||
void GetAddr_(std::vector<CAddress> &vAddr); | void GetAddr_(std::vector<CAddress> &vAddr) EXCLUSIVE_LOCKS_REQUIRED(cs); | ||||
//! Mark an entry as currently-connected-to. | //! Mark an entry as currently-connected-to. | ||||
void Connected_(const CService &addr, int64_t nTime); | void Connected_(const CService &addr, int64_t nTime) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs); | |||||
//! Update an entry's service bits. | //! Update an entry's service bits. | ||||
void SetServices_(const CService &addr, ServiceFlags nServices); | void SetServices_(const CService &addr, ServiceFlags nServices) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs); | |||||
public: | public: | ||||
/** | /** | ||||
* serialized format: | * serialized format: | ||||
* * version byte (currently 1) | * * version byte (currently 1) | ||||
* * 0x20 + nKey (serialized as if it were a vector, for backward | * * 0x20 + nKey (serialized as if it were a vector, for backward | ||||
* compatibility) | * compatibility) | ||||
* * nNew | * * nNew | ||||
▲ Show 20 Lines • Show All 357 Lines • Show Last 20 Lines |