 |
PRCYCoin
2.0.0.7rc1
P2P Digital Currency
|
Go to the documentation of this file.
7 #ifndef BITCOIN_ADDRMAN_H
8 #define BITCOIN_ADDRMAN_H
60 template <
typename Stream,
typename Operation>
61 inline void SerializationOp(Stream& s, Operation ser_action,
int nType,
int nVersion)
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);
239 void ClearNew(
int nUBucket,
int nUBucketPos);
262 void GetAddr_(std::vector<CAddress>& vAddr);
300 template <
typename Stream>
301 void Serialize(Stream& s,
int nType,
int nVersionDummy)
const
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;
320 assert(nIds !=
nNew);
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 for (
int n = 0; n <
nNew; n++) {
376 info.nRandomPos =
vRandom.size();
381 int nUBucket = info.GetNewBucket(
nKey);
382 int nUBucketPos = info.GetBucketPosition(
nKey,
true, nUBucket);
383 if (
vvNew[nUBucket][nUBucketPos] == -1) {
384 vvNew[nUBucket][nUBucketPos] = n;
393 for (
int n = 0; n <
nTried; n++) {
397 int nKBucketPos = info.GetBucketPosition(
nKey,
false, nKBucket);
398 if (
vvTried[nKBucket][nKBucketPos] == -1) {
399 info.nRandomPos =
vRandom.size();
400 info.fInTried =
true;
413 for (
int bucket = 0; bucket < nUBuckets; bucket++) {
416 for (
int n = 0; n < nSize; n++) {
419 if (nIndex >= 0 && nIndex <
nNew) {
424 vvNew[bucket][nUBucketPos] = nIndex;
432 for (std::map<int, CAddrInfo>::const_iterator it =
mapInfo.begin(); it !=
mapInfo.end();) {
433 if (it->second.fInTried ==
false && it->second.nRefCount == 0) {
434 std::map<int, CAddrInfo>::const_iterator itCopy = it++;
441 if (nLost + nLostUnk > 0) {
442 LogPrint(
BCLog::ADDRMAN,
"addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk, nLost);
455 std::vector<int>().swap(
vRandom);
459 vvNew[bucket][entry] = -1;
498 if ((err = Check_()))
499 LogPrintf(
"ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
523 for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
524 nAdd +=
Add_(*it,
source, nTimePenalty) ? 1 : 0;
545 Attempt_(addr, fCountFailure, nTime);
569 std::vector<CAddress> vAddr;
596 #endif // BITCOIN_ADDRMAN_H
A combination of a network address (CNetAddr) and a (TCP) port.
uint256 nKey
secret key to randomize bucket select with
const uint256 UINT256_ZERO
constant uint256 instances
void SetServices_(const CService &addr, ServiceFlags nServices)
Update an entry's service bits.
CNetAddr source
where knowledge about this address first came from
CAddrInfo * Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId=NULL)
find an entry, creating it if necessary.
CAddrInfo Select_(bool newOnly)
Select an address to connect to, if newOnly is set to true, only the new table is selected from.
virtual int RandomInt(int nMax)
Wraps GetRandInt to allow tests to override RandomInt and make it determinismistic.
void ClearNew(int nUBucket, int nUBucketPos)
Clear a position in a "new" table. This is the only place where entries are actually deleted.
#define ADDRMAN_NEW_BUCKETS_PER_ADDRESS
in how many buckets for entries with new addresses a single address may occur
bool Add(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty=0)
Add a single address.
void Unserialize(Stream &s, int nType, int nVersionDummy)
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
int GetTriedBucket(const uint256 &nKey) const
Calculate in which "tried" bucket this entry belongs.
int64_t nLastSuccess
last successful connection by us
double GetChance(int64_t nNow=GetAdjustedTime()) const
Calculate the relative chance this entry should be given when selecting nodes to connect to.
CAddrInfo Select(bool newOnly=false)
Choose an address to connect to.
ServiceFlags
nServices flags
bool IsTerrible(int64_t nNow=GetAdjustedTime()) const
Determine whether the statistics about this entry are bad enough so that it can just be deleted.
std::string ToStringIPPort() const
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE]
list of "new" buckets
RecursiveMutex cs
critical section to protect the inner data structures
int GetNewBucket(const uint256 &nKey) const
Calculate in which "new" bucket this entry belongs, using its default source.
std::vector< CAddress > GetAddr()
Return a bunch of addresses, selected at random.
unsigned int GetSerializeSize(int nType, int nVersion) const
int64_t nLastGood
last time Good was called (memory only)
Extended statistics about a CAddress.
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
int64_t nLastCountAttempt
last counted attempt (memory only)
CAddrInfo * Find(const CNetAddr &addr, int *pnId=NULL)
Find an entry.
#define ADDRMAN_BUCKET_SIZE
#define ADDRMAN_TRIED_BUCKET_COUNT
Convenience.
std::map< CNetAddr, int > mapAddr
find an nId based on its network address
CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2)
Swap two elements in vRandom.
int nRefCount
reference count in new sets (memory only)
FastRandomContext insecure_rand
Source of random numbers for randomization in inner loops.
int nRandomPos
position in vRandom
void MakeTried(CAddrInfo &info, int nId)
Move an entry from the "new" table(s) to the "tried" table.
int64_t nLastTry
last try whatsoever by us (memory only)
int nIdCount
last used nId
int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
Calculate in which position of a bucket to store this entry.
256-bit unsigned big integer.
std::vector< int > vRandom
randomly-ordered vector of all nIds
#define LogPrint(category,...)
#define ADDRMAN_NEW_BUCKET_COUNT
void Check()
Consistency check.
Stochastical (IP) address manager.
int GetNewBucket(const uint256 &nKey, const CNetAddr &src) const
Calculate in which "new" bucket this entry belongs, given a certain source.
int64_t GetAdjustedTime()
A CService with information about it as peer.
int nAttempts
connection attempts since last successful attempt
void Delete(int nId)
Delete an entry. It must not be in tried, and have refcount 0.
void Serialize(Stream &s, int nType, int nVersionDummy) const
serialized format:
void Connected_(const CService &addr, int64_t nTime)
Mark an entry as currently-connected-to.
void Connected(const CService &addr, int64_t nTime=GetAdjustedTime())
Mark an entry as currently-connected-to.
void GetAddr_(std::vector< CAddress > &vAddr)
Select several addresses at once.
void SetServices(const CService &addr, ServiceFlags nServices)
int size()
Return the number of (unique) addresses in all tables.
bool fInTried
in tried set? (memory only)
void Good_(const CService &addr, int64_t nTime)
Mark an entry "good", possibly moving it from "new" to "tried".
void Good(const CService &addr, int64_t nTime=GetAdjustedTime())
Mark an entry as accessible.
int nNew
number of (unique) "new" entries
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.
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE]
list of "tried" buckets
bool Add_(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty)
Add an entry to the "new" table.
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime=GetAdjustedTime())
Mark an entry as connection attempted to.
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime)
Mark an entry as attempted to connect.