Add UpdatedBlockTip signal to CMainSignals and CValidationInterface
[bitcoinplatinum.git] / src / addrman.h
blob384b6cfdb9371a089b6e2f4c43377e72e4206e57
1 // Copyright (c) 2012 Pieter Wuille
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_ADDRMAN_H
6 #define BITCOIN_ADDRMAN_H
8 #include "netbase.h"
9 #include "protocol.h"
10 #include "random.h"
11 #include "sync.h"
12 #include "timedata.h"
13 #include "util.h"
15 #include <map>
16 #include <set>
17 #include <stdint.h>
18 #include <vector>
20 /**
21 * Extended statistics about a CAddress
23 class CAddrInfo : public CAddress
25 public:
26 //! last try whatsoever by us (memory only)
27 int64_t nLastTry;
29 private:
30 //! where knowledge about this address first came from
31 CNetAddr source;
33 //! last successful connection by us
34 int64_t nLastSuccess;
36 //! connection attempts since last successful attempt
37 int nAttempts;
39 //! reference count in new sets (memory only)
40 int nRefCount;
42 //! in tried set? (memory only)
43 bool fInTried;
45 //! position in vRandom
46 int nRandomPos;
48 friend class CAddrMan;
50 public:
52 ADD_SERIALIZE_METHODS;
54 template <typename Stream, typename Operation>
55 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
56 READWRITE(*(CAddress*)this);
57 READWRITE(source);
58 READWRITE(nLastSuccess);
59 READWRITE(nAttempts);
62 void Init()
64 nLastSuccess = 0;
65 nLastTry = 0;
66 nAttempts = 0;
67 nRefCount = 0;
68 fInTried = false;
69 nRandomPos = -1;
72 CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
74 Init();
77 CAddrInfo() : CAddress(), source()
79 Init();
82 //! Calculate in which "tried" bucket this entry belongs
83 int GetTriedBucket(const uint256 &nKey) const;
85 //! Calculate in which "new" bucket this entry belongs, given a certain source
86 int GetNewBucket(const uint256 &nKey, const CNetAddr& src) const;
88 //! Calculate in which "new" bucket this entry belongs, using its default source
89 int GetNewBucket(const uint256 &nKey) const
91 return GetNewBucket(nKey, source);
94 //! Calculate in which position of a bucket to store this entry.
95 int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
97 //! Determine whether the statistics about this entry are bad enough so that it can just be deleted
98 bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;
100 //! Calculate the relative chance this entry should be given when selecting nodes to connect to
101 double GetChance(int64_t nNow = GetAdjustedTime()) const;
105 /** Stochastic address manager
107 * Design goals:
108 * * Keep the address tables in-memory, and asynchronously dump the entire table to peers.dat.
109 * * Make sure no (localized) attacker can fill the entire table with his nodes/addresses.
111 * To that end:
112 * * Addresses are organized into buckets.
113 * * Addresses that have not yet been tried go into 1024 "new" buckets.
114 * * Based on the address range (/16 for IPv4) of the source of information, 64 buckets are selected at random.
115 * * The actual bucket is chosen from one of these, based on the range in which the address itself is located.
116 * * One single address can occur in up to 8 different buckets to increase selection chances for addresses that
117 * are seen frequently. The chance for increasing this multiplicity decreases exponentially.
118 * * When adding a new address to a full bucket, a randomly chosen entry (with a bias favoring less recently seen
119 * ones) is removed from it first.
120 * * Addresses of nodes that are known to be accessible go into 256 "tried" buckets.
121 * * Each address range selects at random 8 of these buckets.
122 * * The actual bucket is chosen from one of these, based on the full address.
123 * * When adding a new good address to a full bucket, a randomly chosen entry (with a bias favoring less recently
124 * tried ones) is evicted from it, back to the "new" buckets.
125 * * Bucket selection is based on cryptographic hashing, using a randomly-generated 256-bit key, which should not
126 * be observable by adversaries.
127 * * Several indexes are kept for high performance. Defining DEBUG_ADDRMAN will introduce frequent (and expensive)
128 * consistency checks for the entire data structure.
131 //! total number of buckets for tried addresses
132 #define ADDRMAN_TRIED_BUCKET_COUNT 256
134 //! total number of buckets for new addresses
135 #define ADDRMAN_NEW_BUCKET_COUNT 1024
137 //! maximum allowed number of entries in buckets for new and tried addresses
138 #define ADDRMAN_BUCKET_SIZE 64
140 //! over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread
141 #define ADDRMAN_TRIED_BUCKETS_PER_GROUP 8
143 //! over how many buckets entries with new addresses originating from a single group are spread
144 #define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 64
146 //! in how many buckets for entries with new addresses a single address may occur
147 #define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 8
149 //! how old addresses can maximally be
150 #define ADDRMAN_HORIZON_DAYS 30
152 //! after how many failed attempts we give up on a new node
153 #define ADDRMAN_RETRIES 3
155 //! how many successive failures are allowed ...
156 #define ADDRMAN_MAX_FAILURES 10
158 //! ... in at least this many days
159 #define ADDRMAN_MIN_FAIL_DAYS 7
161 //! the maximum percentage of nodes to return in a getaddr call
162 #define ADDRMAN_GETADDR_MAX_PCT 23
164 //! the maximum number of nodes to return in a getaddr call
165 #define ADDRMAN_GETADDR_MAX 2500
167 /**
168 * Stochastical (IP) address manager
170 class CAddrMan
172 private:
173 //! critical section to protect the inner data structures
174 mutable CCriticalSection cs;
176 //! secret key to randomize bucket select with
177 uint256 nKey;
179 //! last used nId
180 int nIdCount;
182 //! table with information about all nIds
183 std::map<int, CAddrInfo> mapInfo;
185 //! find an nId based on its network address
186 std::map<CNetAddr, int> mapAddr;
188 //! randomly-ordered vector of all nIds
189 std::vector<int> vRandom;
191 // number of "tried" entries
192 int nTried;
194 //! list of "tried" buckets
195 int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
197 //! number of (unique) "new" entries
198 int nNew;
200 //! list of "new" buckets
201 int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
203 protected:
205 //! Find an entry.
206 CAddrInfo* Find(const CNetAddr& addr, int *pnId = NULL);
208 //! find an entry, creating it if necessary.
209 //! nTime and nServices of the found node are updated, if necessary.
210 CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = NULL);
212 //! Swap two elements in vRandom.
213 void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2);
215 //! Move an entry from the "new" table(s) to the "tried" table
216 void MakeTried(CAddrInfo& info, int nId);
218 //! Delete an entry. It must not be in tried, and have refcount 0.
219 void Delete(int nId);
221 //! Clear a position in a "new" table. This is the only place where entries are actually deleted.
222 void ClearNew(int nUBucket, int nUBucketPos);
224 //! Mark an entry "good", possibly moving it from "new" to "tried".
225 void Good_(const CService &addr, int64_t nTime);
227 //! Add an entry to the "new" table.
228 bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty);
230 //! Mark an entry as attempted to connect.
231 void Attempt_(const CService &addr, int64_t nTime);
233 //! Select an address to connect to.
234 CAddrInfo Select_();
236 #ifdef DEBUG_ADDRMAN
237 //! Perform consistency check. Returns an error code or zero.
238 int Check_();
239 #endif
241 //! Select several addresses at once.
242 void GetAddr_(std::vector<CAddress> &vAddr);
244 //! Mark an entry as currently-connected-to.
245 void Connected_(const CService &addr, int64_t nTime);
247 public:
249 * serialized format:
250 * * version byte (currently 1)
251 * * 0x20 + nKey (serialized as if it were a vector, for backward compatibility)
252 * * nNew
253 * * nTried
254 * * number of "new" buckets XOR 2**30
255 * * all nNew addrinfos in vvNew
256 * * all nTried addrinfos in vvTried
257 * * for each bucket:
258 * * number of elements
259 * * for each element: index
261 * 2**30 is xorred with the number of buckets to make addrman deserializer v0 detect it
262 * as incompatible. This is necessary because it did not check the version number on
263 * deserialization.
265 * Notice that vvTried, mapAddr and vVector are never encoded explicitly;
266 * they are instead reconstructed from the other information.
268 * vvNew is serialized, but only used if ADDRMAN_UNKNOWN_BUCKET_COUNT didn't change,
269 * otherwise it is reconstructed as well.
271 * This format is more complex, but significantly smaller (at most 1.5 MiB), and supports
272 * changes to the ADDRMAN_ parameters without breaking the on-disk structure.
274 * We don't use ADD_SERIALIZE_METHODS since the serialization and deserialization code has
275 * very little in common.
277 template<typename Stream>
278 void Serialize(Stream &s, int nType, int nVersionDummy) const
280 LOCK(cs);
282 unsigned char nVersion = 1;
283 s << nVersion;
284 s << ((unsigned char)32);
285 s << nKey;
286 s << nNew;
287 s << nTried;
289 int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
290 s << nUBuckets;
291 std::map<int, int> mapUnkIds;
292 int nIds = 0;
293 for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
294 mapUnkIds[(*it).first] = nIds;
295 const CAddrInfo &info = (*it).second;
296 if (info.nRefCount) {
297 assert(nIds != nNew); // this means nNew was wrong, oh ow
298 s << info;
299 nIds++;
302 nIds = 0;
303 for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
304 const CAddrInfo &info = (*it).second;
305 if (info.fInTried) {
306 assert(nIds != nTried); // this means nTried was wrong, oh ow
307 s << info;
308 nIds++;
311 for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
312 int nSize = 0;
313 for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
314 if (vvNew[bucket][i] != -1)
315 nSize++;
317 s << nSize;
318 for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
319 if (vvNew[bucket][i] != -1) {
320 int nIndex = mapUnkIds[vvNew[bucket][i]];
321 s << nIndex;
327 template<typename Stream>
328 void Unserialize(Stream& s, int nType, int nVersionDummy)
330 LOCK(cs);
332 Clear();
334 unsigned char nVersion;
335 s >> nVersion;
336 unsigned char nKeySize;
337 s >> nKeySize;
338 if (nKeySize != 32) throw std::ios_base::failure("Incorrect keysize in addrman deserialization");
339 s >> nKey;
340 s >> nNew;
341 s >> nTried;
342 int nUBuckets = 0;
343 s >> nUBuckets;
344 if (nVersion != 0) {
345 nUBuckets ^= (1 << 30);
348 // Deserialize entries from the new table.
349 for (int n = 0; n < nNew; n++) {
350 CAddrInfo &info = mapInfo[n];
351 s >> info;
352 mapAddr[info] = n;
353 info.nRandomPos = vRandom.size();
354 vRandom.push_back(n);
355 if (nVersion != 1 || nUBuckets != ADDRMAN_NEW_BUCKET_COUNT) {
356 // In case the new table data cannot be used (nVersion unknown, or bucket count wrong),
357 // immediately try to give them a reference based on their primary source address.
358 int nUBucket = info.GetNewBucket(nKey);
359 int nUBucketPos = info.GetBucketPosition(nKey, true, nUBucket);
360 if (vvNew[nUBucket][nUBucketPos] == -1) {
361 vvNew[nUBucket][nUBucketPos] = n;
362 info.nRefCount++;
366 nIdCount = nNew;
368 // Deserialize entries from the tried table.
369 int nLost = 0;
370 for (int n = 0; n < nTried; n++) {
371 CAddrInfo info;
372 s >> info;
373 int nKBucket = info.GetTriedBucket(nKey);
374 int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
375 if (vvTried[nKBucket][nKBucketPos] == -1) {
376 info.nRandomPos = vRandom.size();
377 info.fInTried = true;
378 vRandom.push_back(nIdCount);
379 mapInfo[nIdCount] = info;
380 mapAddr[info] = nIdCount;
381 vvTried[nKBucket][nKBucketPos] = nIdCount;
382 nIdCount++;
383 } else {
384 nLost++;
387 nTried -= nLost;
389 // Deserialize positions in the new table (if possible).
390 for (int bucket = 0; bucket < nUBuckets; bucket++) {
391 int nSize = 0;
392 s >> nSize;
393 for (int n = 0; n < nSize; n++) {
394 int nIndex = 0;
395 s >> nIndex;
396 if (nIndex >= 0 && nIndex < nNew) {
397 CAddrInfo &info = mapInfo[nIndex];
398 int nUBucketPos = info.GetBucketPosition(nKey, true, bucket);
399 if (nVersion == 1 && nUBuckets == ADDRMAN_NEW_BUCKET_COUNT && vvNew[bucket][nUBucketPos] == -1 && info.nRefCount < ADDRMAN_NEW_BUCKETS_PER_ADDRESS) {
400 info.nRefCount++;
401 vvNew[bucket][nUBucketPos] = nIndex;
407 // Prune new entries with refcount 0 (as a result of collisions).
408 int nLostUnk = 0;
409 for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); ) {
410 if (it->second.fInTried == false && it->second.nRefCount == 0) {
411 std::map<int, CAddrInfo>::const_iterator itCopy = it++;
412 Delete(itCopy->first);
413 nLostUnk++;
414 } else {
415 it++;
418 if (nLost + nLostUnk > 0) {
419 LogPrint("addrman", "addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk, nLost);
422 Check();
425 unsigned int GetSerializeSize(int nType, int nVersion) const
427 return (CSizeComputer(nType, nVersion) << *this).size();
430 void Clear()
432 std::vector<int>().swap(vRandom);
433 nKey = GetRandHash();
434 for (size_t bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
435 for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
436 vvNew[bucket][entry] = -1;
439 for (size_t bucket = 0; bucket < ADDRMAN_TRIED_BUCKET_COUNT; bucket++) {
440 for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
441 vvTried[bucket][entry] = -1;
445 nIdCount = 0;
446 nTried = 0;
447 nNew = 0;
450 CAddrMan()
452 Clear();
455 ~CAddrMan()
457 nKey.SetNull();
460 //! Return the number of (unique) addresses in all tables.
461 size_t size() const
463 return vRandom.size();
466 //! Consistency check
467 void Check()
469 #ifdef DEBUG_ADDRMAN
471 LOCK(cs);
472 int err;
473 if ((err=Check_()))
474 LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
476 #endif
479 //! Add a single address.
480 bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0)
482 bool fRet = false;
484 LOCK(cs);
485 Check();
486 fRet |= Add_(addr, source, nTimePenalty);
487 Check();
489 if (fRet)
490 LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
491 return fRet;
494 //! Add multiple addresses.
495 bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
497 int nAdd = 0;
499 LOCK(cs);
500 Check();
501 for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
502 nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
503 Check();
505 if (nAdd)
506 LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
507 return nAdd > 0;
510 //! Mark an entry as accessible.
511 void Good(const CService &addr, int64_t nTime = GetAdjustedTime())
514 LOCK(cs);
515 Check();
516 Good_(addr, nTime);
517 Check();
521 //! Mark an entry as connection attempted to.
522 void Attempt(const CService &addr, int64_t nTime = GetAdjustedTime())
525 LOCK(cs);
526 Check();
527 Attempt_(addr, nTime);
528 Check();
533 * Choose an address to connect to.
535 CAddrInfo Select()
537 CAddrInfo addrRet;
539 LOCK(cs);
540 Check();
541 addrRet = Select_();
542 Check();
544 return addrRet;
547 //! Return a bunch of addresses, selected at random.
548 std::vector<CAddress> GetAddr()
550 Check();
551 std::vector<CAddress> vAddr;
553 LOCK(cs);
554 GetAddr_(vAddr);
556 Check();
557 return vAddr;
560 //! Mark an entry as currently-connected-to.
561 void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
564 LOCK(cs);
565 Check();
566 Connected_(addr, nTime);
567 Check();
572 #endif // BITCOIN_ADDRMAN_H