1 // Copyright (c) 2012 Pieter Wuille
2 // Copyright (c) 2012-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_ADDRMAN_H
7 #define BITCOIN_ADDRMAN_H
9 #include "netaddress.h"
22 * Extended statistics about a CAddress
24 class CAddrInfo
: public CAddress
29 //! last try whatsoever by us (memory only)
32 //! last counted attempt (memory only)
33 int64_t nLastCountAttempt
;
36 //! where knowledge about this address first came from
39 //! last successful connection by us
42 //! connection attempts since last successful attempt
45 //! reference count in new sets (memory only)
48 //! in tried set? (memory only)
51 //! position in vRandom
54 friend class CAddrMan
;
58 ADD_SERIALIZE_METHODS
;
60 template <typename Stream
, typename Operation
>
61 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
62 READWRITE(*(CAddress
*)this);
64 READWRITE(nLastSuccess
);
72 nLastCountAttempt
= 0;
79 CAddrInfo(const CAddress
&addrIn
, const CNetAddr
&addrSource
) : CAddress(addrIn
), source(addrSource
)
84 CAddrInfo() : CAddress(), source()
89 //! Calculate in which "tried" bucket this entry belongs
90 int GetTriedBucket(const uint256
&nKey
) const;
92 //! Calculate in which "new" bucket this entry belongs, given a certain source
93 int GetNewBucket(const uint256
&nKey
, const CNetAddr
& src
) const;
95 //! Calculate in which "new" bucket this entry belongs, using its default source
96 int GetNewBucket(const uint256
&nKey
) const
98 return GetNewBucket(nKey
, source
);
101 //! Calculate in which position of a bucket to store this entry.
102 int GetBucketPosition(const uint256
&nKey
, bool fNew
, int nBucket
) const;
104 //! Determine whether the statistics about this entry are bad enough so that it can just be deleted
105 bool IsTerrible(int64_t nNow
= GetAdjustedTime()) const;
107 //! Calculate the relative chance this entry should be given when selecting nodes to connect to
108 double GetChance(int64_t nNow
= GetAdjustedTime()) const;
112 /** Stochastic address manager
115 * * Keep the address tables in-memory, and asynchronously dump the entire table to peers.dat.
116 * * Make sure no (localized) attacker can fill the entire table with his nodes/addresses.
119 * * Addresses are organized into buckets.
120 * * Addresses that have not yet been tried go into 1024 "new" buckets.
121 * * Based on the address range (/16 for IPv4) of the source of information, 64 buckets are selected at random.
122 * * The actual bucket is chosen from one of these, based on the range in which the address itself is located.
123 * * One single address can occur in up to 8 different buckets to increase selection chances for addresses that
124 * are seen frequently. The chance for increasing this multiplicity decreases exponentially.
125 * * When adding a new address to a full bucket, a randomly chosen entry (with a bias favoring less recently seen
126 * ones) is removed from it first.
127 * * Addresses of nodes that are known to be accessible go into 256 "tried" buckets.
128 * * Each address range selects at random 8 of these buckets.
129 * * The actual bucket is chosen from one of these, based on the full address.
130 * * When adding a new good address to a full bucket, a randomly chosen entry (with a bias favoring less recently
131 * tried ones) is evicted from it, back to the "new" buckets.
132 * * Bucket selection is based on cryptographic hashing, using a randomly-generated 256-bit key, which should not
133 * be observable by adversaries.
134 * * Several indexes are kept for high performance. Defining DEBUG_ADDRMAN will introduce frequent (and expensive)
135 * consistency checks for the entire data structure.
138 //! total number of buckets for tried addresses
139 #define ADDRMAN_TRIED_BUCKET_COUNT 256
141 //! total number of buckets for new addresses
142 #define ADDRMAN_NEW_BUCKET_COUNT 1024
144 //! maximum allowed number of entries in buckets for new and tried addresses
145 #define ADDRMAN_BUCKET_SIZE 64
147 //! over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread
148 #define ADDRMAN_TRIED_BUCKETS_PER_GROUP 8
150 //! over how many buckets entries with new addresses originating from a single group are spread
151 #define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 64
153 //! in how many buckets for entries with new addresses a single address may occur
154 #define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 8
156 //! how old addresses can maximally be
157 #define ADDRMAN_HORIZON_DAYS 30
159 //! after how many failed attempts we give up on a new node
160 #define ADDRMAN_RETRIES 3
162 //! how many successive failures are allowed ...
163 #define ADDRMAN_MAX_FAILURES 10
165 //! ... in at least this many days
166 #define ADDRMAN_MIN_FAIL_DAYS 7
168 //! the maximum percentage of nodes to return in a getaddr call
169 #define ADDRMAN_GETADDR_MAX_PCT 23
171 //! the maximum number of nodes to return in a getaddr call
172 #define ADDRMAN_GETADDR_MAX 2500
175 * Stochastical (IP) address manager
180 //! critical section to protect the inner data structures
181 mutable CCriticalSection cs
;
186 //! table with information about all nIds
187 std::map
<int, CAddrInfo
> mapInfo
;
189 //! find an nId based on its network address
190 std::map
<CNetAddr
, int> mapAddr
;
192 //! randomly-ordered vector of all nIds
193 std::vector
<int> vRandom
;
195 // number of "tried" entries
198 //! list of "tried" buckets
199 int vvTried
[ADDRMAN_TRIED_BUCKET_COUNT
][ADDRMAN_BUCKET_SIZE
];
201 //! number of (unique) "new" entries
204 //! list of "new" buckets
205 int vvNew
[ADDRMAN_NEW_BUCKET_COUNT
][ADDRMAN_BUCKET_SIZE
];
207 //! last time Good was called (memory only)
211 //! secret key to randomize bucket select with
214 //! Source of random numbers for randomization in inner loops
215 FastRandomContext insecure_rand
;
218 CAddrInfo
* Find(const CNetAddr
& addr
, int *pnId
= NULL
);
220 //! find an entry, creating it if necessary.
221 //! nTime and nServices of the found node are updated, if necessary.
222 CAddrInfo
* Create(const CAddress
&addr
, const CNetAddr
&addrSource
, int *pnId
= NULL
);
224 //! Swap two elements in vRandom.
225 void SwapRandom(unsigned int nRandomPos1
, unsigned int nRandomPos2
);
227 //! Move an entry from the "new" table(s) to the "tried" table
228 void MakeTried(CAddrInfo
& info
, int nId
);
230 //! Delete an entry. It must not be in tried, and have refcount 0.
231 void Delete(int nId
);
233 //! Clear a position in a "new" table. This is the only place where entries are actually deleted.
234 void ClearNew(int nUBucket
, int nUBucketPos
);
236 //! Mark an entry "good", possibly moving it from "new" to "tried".
237 void Good_(const CService
&addr
, int64_t nTime
);
239 //! Add an entry to the "new" table.
240 bool Add_(const CAddress
&addr
, const CNetAddr
& source
, int64_t nTimePenalty
);
242 //! Mark an entry as attempted to connect.
243 void Attempt_(const CService
&addr
, bool fCountFailure
, int64_t nTime
);
245 //! Select an address to connect to, if newOnly is set to true, only the new table is selected from.
246 CAddrInfo
Select_(bool newOnly
);
248 //! Wraps GetRandInt to allow tests to override RandomInt and make it determinismistic.
249 virtual int RandomInt(int nMax
);
252 //! Perform consistency check. Returns an error code or zero.
256 //! Select several addresses at once.
257 void GetAddr_(std::vector
<CAddress
> &vAddr
);
259 //! Mark an entry as currently-connected-to.
260 void Connected_(const CService
&addr
, int64_t nTime
);
262 //! Update an entry's service bits.
263 void SetServices_(const CService
&addr
, ServiceFlags nServices
);
268 * * version byte (currently 1)
269 * * 0x20 + nKey (serialized as if it were a vector, for backward compatibility)
272 * * number of "new" buckets XOR 2**30
273 * * all nNew addrinfos in vvNew
274 * * all nTried addrinfos in vvTried
276 * * number of elements
277 * * for each element: index
279 * 2**30 is xorred with the number of buckets to make addrman deserializer v0 detect it
280 * as incompatible. This is necessary because it did not check the version number on
283 * Notice that vvTried, mapAddr and vVector are never encoded explicitly;
284 * they are instead reconstructed from the other information.
286 * vvNew is serialized, but only used if ADDRMAN_UNKNOWN_BUCKET_COUNT didn't change,
287 * otherwise it is reconstructed as well.
289 * This format is more complex, but significantly smaller (at most 1.5 MiB), and supports
290 * changes to the ADDRMAN_ parameters without breaking the on-disk structure.
292 * We don't use ADD_SERIALIZE_METHODS since the serialization and deserialization code has
293 * very little in common.
295 template<typename Stream
>
296 void Serialize(Stream
&s
) const
300 unsigned char nVersion
= 1;
302 s
<< ((unsigned char)32);
307 int nUBuckets
= ADDRMAN_NEW_BUCKET_COUNT
^ (1 << 30);
309 std::map
<int, int> mapUnkIds
;
311 for (std::map
<int, CAddrInfo
>::const_iterator it
= mapInfo
.begin(); it
!= mapInfo
.end(); it
++) {
312 mapUnkIds
[(*it
).first
] = nIds
;
313 const CAddrInfo
&info
= (*it
).second
;
314 if (info
.nRefCount
) {
315 assert(nIds
!= nNew
); // this means nNew was wrong, oh ow
321 for (std::map
<int, CAddrInfo
>::const_iterator it
= mapInfo
.begin(); it
!= mapInfo
.end(); it
++) {
322 const CAddrInfo
&info
= (*it
).second
;
324 assert(nIds
!= nTried
); // this means nTried was wrong, oh ow
329 for (int bucket
= 0; bucket
< ADDRMAN_NEW_BUCKET_COUNT
; bucket
++) {
331 for (int i
= 0; i
< ADDRMAN_BUCKET_SIZE
; i
++) {
332 if (vvNew
[bucket
][i
] != -1)
336 for (int i
= 0; i
< ADDRMAN_BUCKET_SIZE
; i
++) {
337 if (vvNew
[bucket
][i
] != -1) {
338 int nIndex
= mapUnkIds
[vvNew
[bucket
][i
]];
345 template<typename Stream
>
346 void Unserialize(Stream
& s
)
352 unsigned char nVersion
;
354 unsigned char nKeySize
;
356 if (nKeySize
!= 32) throw std::ios_base::failure("Incorrect keysize in addrman deserialization");
363 nUBuckets
^= (1 << 30);
366 if (nNew
> ADDRMAN_NEW_BUCKET_COUNT
* ADDRMAN_BUCKET_SIZE
) {
367 throw std::ios_base::failure("Corrupt CAddrMan serialization, nNew exceeds limit.");
370 if (nTried
> ADDRMAN_TRIED_BUCKET_COUNT
* ADDRMAN_BUCKET_SIZE
) {
371 throw std::ios_base::failure("Corrupt CAddrMan serialization, nTried exceeds limit.");
374 // Deserialize entries from the new table.
375 for (int n
= 0; n
< nNew
; n
++) {
376 CAddrInfo
&info
= mapInfo
[n
];
379 info
.nRandomPos
= vRandom
.size();
380 vRandom
.push_back(n
);
381 if (nVersion
!= 1 || nUBuckets
!= ADDRMAN_NEW_BUCKET_COUNT
) {
382 // In case the new table data cannot be used (nVersion unknown, or bucket count wrong),
383 // immediately try to give them a reference based on their primary source address.
384 int nUBucket
= info
.GetNewBucket(nKey
);
385 int nUBucketPos
= info
.GetBucketPosition(nKey
, true, nUBucket
);
386 if (vvNew
[nUBucket
][nUBucketPos
] == -1) {
387 vvNew
[nUBucket
][nUBucketPos
] = n
;
394 // Deserialize entries from the tried table.
396 for (int n
= 0; n
< nTried
; n
++) {
399 int nKBucket
= info
.GetTriedBucket(nKey
);
400 int nKBucketPos
= info
.GetBucketPosition(nKey
, false, nKBucket
);
401 if (vvTried
[nKBucket
][nKBucketPos
] == -1) {
402 info
.nRandomPos
= vRandom
.size();
403 info
.fInTried
= true;
404 vRandom
.push_back(nIdCount
);
405 mapInfo
[nIdCount
] = info
;
406 mapAddr
[info
] = nIdCount
;
407 vvTried
[nKBucket
][nKBucketPos
] = nIdCount
;
415 // Deserialize positions in the new table (if possible).
416 for (int bucket
= 0; bucket
< nUBuckets
; bucket
++) {
419 for (int n
= 0; n
< nSize
; n
++) {
422 if (nIndex
>= 0 && nIndex
< nNew
) {
423 CAddrInfo
&info
= mapInfo
[nIndex
];
424 int nUBucketPos
= info
.GetBucketPosition(nKey
, true, bucket
);
425 if (nVersion
== 1 && nUBuckets
== ADDRMAN_NEW_BUCKET_COUNT
&& vvNew
[bucket
][nUBucketPos
] == -1 && info
.nRefCount
< ADDRMAN_NEW_BUCKETS_PER_ADDRESS
) {
427 vvNew
[bucket
][nUBucketPos
] = nIndex
;
433 // Prune new entries with refcount 0 (as a result of collisions).
435 for (std::map
<int, CAddrInfo
>::const_iterator it
= mapInfo
.begin(); it
!= mapInfo
.end(); ) {
436 if (it
->second
.fInTried
== false && it
->second
.nRefCount
== 0) {
437 std::map
<int, CAddrInfo
>::const_iterator itCopy
= it
++;
438 Delete(itCopy
->first
);
444 if (nLost
+ nLostUnk
> 0) {
445 LogPrint("addrman", "addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk
, nLost
);
453 std::vector
<int>().swap(vRandom
);
454 nKey
= GetRandHash();
455 for (size_t bucket
= 0; bucket
< ADDRMAN_NEW_BUCKET_COUNT
; bucket
++) {
456 for (size_t entry
= 0; entry
< ADDRMAN_BUCKET_SIZE
; entry
++) {
457 vvNew
[bucket
][entry
] = -1;
460 for (size_t bucket
= 0; bucket
< ADDRMAN_TRIED_BUCKET_COUNT
; bucket
++) {
461 for (size_t entry
= 0; entry
< ADDRMAN_BUCKET_SIZE
; entry
++) {
462 vvTried
[bucket
][entry
] = -1;
469 nLastGood
= 1; //Initially at 1 so that "never" is strictly worse.
482 //! Return the number of (unique) addresses in all tables.
485 LOCK(cs
); // TODO: Cache this in an atomic to avoid this overhead
486 return vRandom
.size();
489 //! Consistency check
497 LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err
);
502 //! Add a single address.
503 bool Add(const CAddress
&addr
, const CNetAddr
& source
, int64_t nTimePenalty
= 0)
508 fRet
|= Add_(addr
, source
, nTimePenalty
);
511 LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr
.ToStringIPPort(), source
.ToString(), nTried
, nNew
);
515 //! Add multiple addresses.
516 bool Add(const std::vector
<CAddress
> &vAddr
, const CNetAddr
& source
, int64_t nTimePenalty
= 0)
521 for (std::vector
<CAddress
>::const_iterator it
= vAddr
.begin(); it
!= vAddr
.end(); it
++)
522 nAdd
+= Add_(*it
, source
, nTimePenalty
) ? 1 : 0;
525 LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd
, source
.ToString(), nTried
, nNew
);
529 //! Mark an entry as accessible.
530 void Good(const CService
&addr
, int64_t nTime
= GetAdjustedTime())
538 //! Mark an entry as connection attempted to.
539 void Attempt(const CService
&addr
, bool fCountFailure
, int64_t nTime
= GetAdjustedTime())
543 Attempt_(addr
, fCountFailure
, nTime
);
548 * Choose an address to connect to.
550 CAddrInfo
Select(bool newOnly
= false)
556 addrRet
= Select_(newOnly
);
562 //! Return a bunch of addresses, selected at random.
563 std::vector
<CAddress
> GetAddr()
566 std::vector
<CAddress
> vAddr
;
575 //! Mark an entry as currently-connected-to.
576 void Connected(const CService
&addr
, int64_t nTime
= GetAdjustedTime())
580 Connected_(addr
, nTime
);
584 void SetServices(const CService
&addr
, ServiceFlags nServices
)
588 SetServices_(addr
, nServices
);
594 #endif // BITCOIN_ADDRMAN_H