1 // Copyright (c) 2012 Pieter Wuille
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 #ifndef _BITCOIN_ADDRMAN
5 #define _BITCOIN_ADDRMAN 1
15 #include <openssl/rand.h>
17 // Extended statistics about a CAddress
18 class CAddrInfo : public CAddress
21 // where knowledge about this address first came from
24 // last successful connection by us
27 // last try whatsoever by us:
28 // int64_t CAddress::nLastTry
30 // connection attempts since last successful attempt
33 // reference count in new sets (memory only)
36 // in tried set? (memory only)
39 // position in vRandom
42 friend class CAddrMan;
47 CAddress* pthis = (CAddress*)(this);
50 READWRITE(nLastSuccess);
56 CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource);
59 // Calculate in which "tried" bucket this entry belongs
60 int GetTriedBucket(const std::vector<unsigned char> &nKey) const;
62 // Calculate in which "new" bucket this entry belongs, given a certain source
63 int GetNewBucket(const std::vector<unsigned char> &nKey, const CNetAddr& src) const;
65 // Calculate in which "new" bucket this entry belongs, using its default source
66 int GetNewBucket(const std::vector<unsigned char> &nKey) const;
68 // Determine whether the statistics about this entry are bad enough so that it can just be deleted
69 bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;
71 // Calculate the relative chance this entry should be given when selecting nodes to connect to
72 double GetChance(int64_t nNow = GetAdjustedTime()) const;
76 // Stochastic address manager
79 // * Only keep a limited number of addresses around, so that addr.dat and memory requirements do not grow without bound.
80 // * Keep the address tables in-memory, and asynchronously dump the entire to able in addr.dat.
81 // * Make sure no (localized) attacker can fill the entire table with his nodes/addresses.
84 // * Addresses are organized into buckets.
85 // * Address that have not yet been tried go into 256 "new" buckets.
86 // * Based on the address range (/16 for IPv4) of source of the information, 32 buckets are selected at random
87 // * The actual bucket is chosen from one of these, based on the range the address itself is located.
88 // * One single address can occur in up to 4 different buckets, to increase selection chances for addresses that
89 // are seen frequently. The chance for increasing this multiplicity decreases exponentially.
90 // * When adding a new address to a full bucket, a randomly chosen entry (with a bias favoring less recently seen
91 // ones) is removed from it first.
92 // * Addresses of nodes that are known to be accessible go into 64 "tried" buckets.
93 // * Each address range selects at random 4 of these buckets.
94 // * The actual bucket is chosen from one of these, based on the full address.
95 // * When adding a new good address to a full bucket, a randomly chosen entry (with a bias favoring less recently
96 // tried ones) is evicted from it, back to the "new" buckets.
97 // * Bucket selection is based on cryptographic hashing, using a randomly-generated 256-bit key, which should not
98 // be observable by adversaries.
99 // * Several indexes are kept for high performance.
101 // total number of buckets for tried addresses
102 #define ADDRMAN_TRIED_BUCKET_COUNT 64
104 // maximum allowed number of entries in buckets for tried addresses
105 #define ADDRMAN_TRIED_BUCKET_SIZE 64
107 // total number of buckets for new addresses
108 #define ADDRMAN_NEW_BUCKET_COUNT 256
110 // maximum allowed number of entries in buckets for new addresses
111 #define ADDRMAN_NEW_BUCKET_SIZE 64
113 // over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread
114 #define ADDRMAN_TRIED_BUCKETS_PER_GROUP 4
116 // over how many buckets entries with new addresses originating from a single group are spread
117 #define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 32
119 // in how many buckets for entries with new addresses a single address may occur
120 #define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 4
122 // how many entries in a bucket with tried addresses are inspected, when selecting one to replace
123 #define ADDRMAN_TRIED_ENTRIES_INSPECT_ON_EVICT 4
125 // how old addresses can maximally be
126 #define ADDRMAN_HORIZON_DAYS 30
128 // after how many failed attempts we give up on a new node
129 #define ADDRMAN_RETRIES 3
131 // how many successive failures are allowed ...
132 #define ADDRMAN_MAX_FAILURES 10
134 // ... in at least this many days
135 #define ADDRMAN_MIN_FAIL_DAYS 7
137 // the maximum percentage of nodes to return in a getaddr call
138 #define ADDRMAN_GETADDR_MAX_PCT 23
140 // the maximum number of nodes to return in a getaddr call
141 #define ADDRMAN_GETADDR_MAX 2500
143 // Stochastical (IP) address manager
147 // critical section to protect the inner data structures
148 mutable CCriticalSection cs;
150 // secret key to randomize bucket select with
151 std::vector<unsigned char> nKey;
156 // table with information about all nIds
157 std::map<int, CAddrInfo> mapInfo;
159 // find an nId based on its network address
160 std::map<CNetAddr, int> mapAddr;
162 // randomly-ordered vector of all nIds
163 std::vector<int> vRandom;
165 // number of "tried" entries
168 // list of "tried" buckets
169 std::vector<std::vector<int> > vvTried;
171 // number of (unique) "new" entries
174 // list of "new" buckets
175 std::vector<std::set<int> > vvNew;
180 CAddrInfo* Find(const CNetAddr& addr, int *pnId = NULL);
182 // find an entry, creating it if necessary.
183 // nTime and nServices of found node is updated, if necessary.
184 CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = NULL);
186 // Swap two elements in vRandom.
187 void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2);
189 // Return position in given bucket to replace.
190 int SelectTried(int nKBucket);
192 // Remove an element from a "new" bucket.
193 // This is the only place where actual deletes occur.
194 // They are never deleted while in the "tried" table, only possibly evicted back to the "new" table.
195 int ShrinkNew(int nUBucket);
197 // Move an entry from the "new" table(s) to the "tried" table
198 // @pre vvUnkown[nOrigin].count(nId) != 0
199 void MakeTried(CAddrInfo& info, int nId, int nOrigin);
201 // Mark an entry "good", possibly moving it from "new" to "tried".
202 void Good_(const CService &addr, int64_t nTime);
204 // Add an entry to the "new" table.
205 bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty);
207 // Mark an entry as attempted to connect.
208 void Attempt_(const CService &addr, int64_t nTime);
210 // Select an address to connect to.
211 // nUnkBias determines how much to favor new addresses over tried ones (min=0, max=100)
212 CAddress Select_(int nUnkBias);
214 // Select several addresses at once.
215 void GetAddr_(std::vector<CAddress> &vAddr);
216 void GetOnlineAddr_(std::vector<CAddrInfo> &vAddr);
218 // Mark an entry as currently-connected-to.
219 void Connected_(const CService &addr, int64_t nTime);
223 typedef std::map<int, int> MapUnkIds; // For MSVC macro
236 *am = const_cast<CAddrMan*>(this);
241 nUBuckets = ADDRMAN_NEW_BUCKET_COUNT;
243 READWRITE(nUBuckets);
247 for (auto it = am->mapInfo.begin(); it != am->mapInfo.end(); it++)
250 break; // this means nNew was wrong, oh ow
251 mapUnkIds[(*it).first] = nIds;
254 &info = (*it).second;
263 for (auto it = am->mapInfo.begin(); it != am->mapInfo.end(); it++)
266 break; /* this means nTried was wrong, oh ow */
269 &info = (*it).second;
278 auto it = am->vvNew.begin();
279 it != am->vvNew.end();
287 nSize = int( vNew.size() );
290 for (auto it2 = vNew.begin(); it2 != vNew.end(); it2++)
293 nIndex = mapUnkIds[*it2];
303 READWRITE(nUBuckets);
309 std::vector<std::vector<int> >(
310 ADDRMAN_TRIED_BUCKET_COUNT,
314 std::vector<std::set<int> >(
315 ADDRMAN_NEW_BUCKET_COUNT,
318 for (int n = 0; n < am->nNew; n++)
321 &info = am->mapInfo[n];
324 am->mapAddr[info] = n;
325 info.nRandomPos = int( vRandom.size() );
326 am->vRandom.push_back(n);
327 if (nUBuckets != ADDRMAN_NEW_BUCKET_COUNT)
329 am->vvNew[info.GetNewBucket(am->nKey)].insert(n);
333 am->nIdCount = am->nNew;
338 for (int n = 0; n < am->nTried; n++)
346 &vTried = am->vvTried[info.GetTriedBucket(am->nKey)];
348 if (vTried.size() < ADDRMAN_TRIED_BUCKET_SIZE)
350 info.nRandomPos = int( vRandom.size() );
351 info.fInTried = true;
352 am->vRandom.push_back(am->nIdCount);
353 am->mapInfo[am->nIdCount] = info;
354 am->mapAddr[info] = am->nIdCount;
355 vTried.push_back(am->nIdCount);
364 for (int b = 0; b < nUBuckets; b++)
367 &vNew = am->vvNew[b];
373 for (int n = 0; n < nSize; n++)
381 &info = am->mapInfo[nIndex];
384 (nUBuckets == ADDRMAN_NEW_BUCKET_COUNT) &&
385 (info.nRefCount < ADDRMAN_NEW_BUCKETS_PER_ADDRESS)
398 // Return the number of (unique) addresses in all tables.
401 // Add a single address.
402 bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0);
404 // Add multiple addresses.
405 bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0);
407 // Mark an entry as accessible.
408 void Good(const CService &addr, int64_t nTime = GetAdjustedTime());
410 // Mark an entry as connection attempted to.
411 void Attempt(const CService &addr, int64_t nTime = GetAdjustedTime());
413 // Choose an address to connect to.
414 // nUnkBias determines how much "new" entries are favored over "tried" ones (0-100).
415 CAddress Select(int nUnkBias = 50);
417 // Return a bunch of addresses, selected at random.
418 std::vector<CAddress> GetAddr();
420 std::vector<CAddrInfo> GetOnlineAddr();
422 // Mark an entry as currently-connected-to.
423 void Connected(const CService &addr, int64_t nTime = GetAdjustedTime());