Merge #9226: Remove fNetworkNode and pnodeLocalHost.
[bitcoinplatinum.git] / src / addrman.h
blob76e7b210f7c3404f37568c6df8bf300958957d9e
1 // Copyright (c) 2012 Pieter Wuille
2 // Copyright (c) 2012-2015 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"
10 #include "protocol.h"
11 #include "random.h"
12 #include "sync.h"
13 #include "timedata.h"
14 #include "util.h"
16 #include <map>
17 #include <set>
18 #include <stdint.h>
19 #include <vector>
21 /**
22 * Extended statistics about a CAddress
24 class CAddrInfo : public CAddress
28 public:
29 //! last try whatsoever by us (memory only)
30 int64_t nLastTry;
32 //! last counted attempt (memory only)
33 int64_t nLastCountAttempt;
35 private:
36 //! where knowledge about this address first came from
37 CNetAddr source;
39 //! last successful connection by us
40 int64_t nLastSuccess;
42 //! connection attempts since last successful attempt
43 int nAttempts;
45 //! reference count in new sets (memory only)
46 int nRefCount;
48 //! in tried set? (memory only)
49 bool fInTried;
51 //! position in vRandom
52 int nRandomPos;
54 friend class CAddrMan;
56 public:
58 ADD_SERIALIZE_METHODS;
60 template <typename Stream, typename Operation>
61 inline void SerializationOp(Stream& s, Operation ser_action) {
62 READWRITE(*(CAddress*)this);
63 READWRITE(source);
64 READWRITE(nLastSuccess);
65 READWRITE(nAttempts);
68 void Init()
70 nLastSuccess = 0;
71 nLastTry = 0;
72 nLastCountAttempt = 0;
73 nAttempts = 0;
74 nRefCount = 0;
75 fInTried = false;
76 nRandomPos = -1;
79 CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
81 Init();
84 CAddrInfo() : CAddress(), source()
86 Init();
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
114 * Design goals:
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.
118 * To that end:
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
174 /**
175 * Stochastical (IP) address manager
177 class CAddrMan
179 private:
180 //! critical section to protect the inner data structures
181 mutable CCriticalSection cs;
183 //! last used nId
184 int nIdCount;
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
196 int nTried;
198 //! list of "tried" buckets
199 int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
201 //! number of (unique) "new" entries
202 int nNew;
204 //! list of "new" buckets
205 int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
207 //! last time Good was called (memory only)
208 int64_t nLastGood;
210 protected:
211 //! secret key to randomize bucket select with
212 uint256 nKey;
214 //! Source of random numbers for randomization in inner loops
215 FastRandomContext insecure_rand;
217 //! Find an entry.
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);
251 #ifdef DEBUG_ADDRMAN
252 //! Perform consistency check. Returns an error code or zero.
253 int Check_();
254 #endif
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);
265 public:
267 * serialized format:
268 * * version byte (currently 1)
269 * * 0x20 + nKey (serialized as if it were a vector, for backward compatibility)
270 * * nNew
271 * * nTried
272 * * number of "new" buckets XOR 2**30
273 * * all nNew addrinfos in vvNew
274 * * all nTried addrinfos in vvTried
275 * * for each bucket:
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
281 * deserialization.
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
298 LOCK(cs);
300 unsigned char nVersion = 1;
301 s << nVersion;
302 s << ((unsigned char)32);
303 s << nKey;
304 s << nNew;
305 s << nTried;
307 int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
308 s << nUBuckets;
309 std::map<int, int> mapUnkIds;
310 int nIds = 0;
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
316 s << info;
317 nIds++;
320 nIds = 0;
321 for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
322 const CAddrInfo &info = (*it).second;
323 if (info.fInTried) {
324 assert(nIds != nTried); // this means nTried was wrong, oh ow
325 s << info;
326 nIds++;
329 for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
330 int nSize = 0;
331 for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
332 if (vvNew[bucket][i] != -1)
333 nSize++;
335 s << nSize;
336 for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
337 if (vvNew[bucket][i] != -1) {
338 int nIndex = mapUnkIds[vvNew[bucket][i]];
339 s << nIndex;
345 template<typename Stream>
346 void Unserialize(Stream& s)
348 LOCK(cs);
350 Clear();
352 unsigned char nVersion;
353 s >> nVersion;
354 unsigned char nKeySize;
355 s >> nKeySize;
356 if (nKeySize != 32) throw std::ios_base::failure("Incorrect keysize in addrman deserialization");
357 s >> nKey;
358 s >> nNew;
359 s >> nTried;
360 int nUBuckets = 0;
361 s >> nUBuckets;
362 if (nVersion != 0) {
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];
377 s >> info;
378 mapAddr[info] = 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;
388 info.nRefCount++;
392 nIdCount = nNew;
394 // Deserialize entries from the tried table.
395 int nLost = 0;
396 for (int n = 0; n < nTried; n++) {
397 CAddrInfo info;
398 s >> info;
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;
408 nIdCount++;
409 } else {
410 nLost++;
413 nTried -= nLost;
415 // Deserialize positions in the new table (if possible).
416 for (int bucket = 0; bucket < nUBuckets; bucket++) {
417 int nSize = 0;
418 s >> nSize;
419 for (int n = 0; n < nSize; n++) {
420 int nIndex = 0;
421 s >> nIndex;
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) {
426 info.nRefCount++;
427 vvNew[bucket][nUBucketPos] = nIndex;
433 // Prune new entries with refcount 0 (as a result of collisions).
434 int nLostUnk = 0;
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);
439 nLostUnk++;
440 } else {
441 it++;
444 if (nLost + nLostUnk > 0) {
445 LogPrint("addrman", "addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk, nLost);
448 Check();
451 void Clear()
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;
466 nIdCount = 0;
467 nTried = 0;
468 nNew = 0;
469 nLastGood = 1; //Initially at 1 so that "never" is strictly worse.
472 CAddrMan()
474 Clear();
477 ~CAddrMan()
479 nKey.SetNull();
482 //! Return the number of (unique) addresses in all tables.
483 size_t size() const
485 LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead
486 return vRandom.size();
489 //! Consistency check
490 void Check()
492 #ifdef DEBUG_ADDRMAN
494 LOCK(cs);
495 int err;
496 if ((err=Check_()))
497 LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
499 #endif
502 //! Add a single address.
503 bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0)
505 LOCK(cs);
506 bool fRet = false;
507 Check();
508 fRet |= Add_(addr, source, nTimePenalty);
509 Check();
510 if (fRet)
511 LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
512 return fRet;
515 //! Add multiple addresses.
516 bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
518 LOCK(cs);
519 int nAdd = 0;
520 Check();
521 for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
522 nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
523 Check();
524 if (nAdd)
525 LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
526 return nAdd > 0;
529 //! Mark an entry as accessible.
530 void Good(const CService &addr, int64_t nTime = GetAdjustedTime())
532 LOCK(cs);
533 Check();
534 Good_(addr, nTime);
535 Check();
538 //! Mark an entry as connection attempted to.
539 void Attempt(const CService &addr, bool fCountFailure, int64_t nTime = GetAdjustedTime())
541 LOCK(cs);
542 Check();
543 Attempt_(addr, fCountFailure, nTime);
544 Check();
548 * Choose an address to connect to.
550 CAddrInfo Select(bool newOnly = false)
552 CAddrInfo addrRet;
554 LOCK(cs);
555 Check();
556 addrRet = Select_(newOnly);
557 Check();
559 return addrRet;
562 //! Return a bunch of addresses, selected at random.
563 std::vector<CAddress> GetAddr()
565 Check();
566 std::vector<CAddress> vAddr;
568 LOCK(cs);
569 GetAddr_(vAddr);
571 Check();
572 return vAddr;
575 //! Mark an entry as currently-connected-to.
576 void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
578 LOCK(cs);
579 Check();
580 Connected_(addr, nTime);
581 Check();
584 void SetServices(const CService &addr, ServiceFlags nServices)
586 LOCK(cs);
587 Check();
588 SetServices_(addr, nServices);
589 Check();
594 #endif // BITCOIN_ADDRMAN_H