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
6 #define _BITCOIN_ADDRMAN
21 * Extended statistics about a CAddress
23 class CAddrInfo : public CAddress
26 //! where knowledge about this address first came from
29 //! last successful connection by us
32 //! last try whatsoever by us:
33 // int64_t CAddress::nLastTry
35 //! connection attempts since last successful attempt
38 //! reference count in new sets (memory only)
41 //! in tried set? (memory only)
44 //! position in vRandom
47 friend class CAddrMan;
51 ADD_SERIALIZE_METHODS;
53 template <typename Stream, typename Operation>
54 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
55 READWRITE(*(CAddress*)this);
57 READWRITE(nLastSuccess);
71 CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
76 CAddrInfo() : CAddress(), source()
81 //! Calculate in which "tried" bucket this entry belongs
82 int GetTriedBucket(const std::vector<unsigned char> &nKey) const;
84 //! Calculate in which "new" bucket this entry belongs, given a certain source
85 int GetNewBucket(const std::vector<unsigned char> &nKey, const CNetAddr& src) const;
87 //! Calculate in which "new" bucket this entry belongs, using its default source
88 int GetNewBucket(const std::vector<unsigned char> &nKey) const
90 return GetNewBucket(nKey, source);
93 //! Determine whether the statistics about this entry are bad enough so that it can just be deleted
94 bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;
96 //! Calculate the relative chance this entry should be given when selecting nodes to connect to
97 double GetChance(int64_t nNow = GetAdjustedTime()) const;
101 /** Stochastic address manager
104 * * Keep the address tables in-memory, and asynchronously dump the entire to able in peers.dat.
105 * * Make sure no (localized) attacker can fill the entire table with his nodes/addresses.
108 * * Addresses are organized into buckets.
109 * * Address that have not yet been tried go into 256 "new" buckets.
110 * * Based on the address range (/16 for IPv4) of source of the information, 32 buckets are selected at random
111 * * The actual bucket is chosen from one of these, based on the range the address itself is located.
112 * * One single address can occur in up to 4 different buckets, to increase selection chances for addresses that
113 * are seen frequently. The chance for increasing this multiplicity decreases exponentially.
114 * * When adding a new address to a full bucket, a randomly chosen entry (with a bias favoring less recently seen
115 * ones) is removed from it first.
116 * * Addresses of nodes that are known to be accessible go into 64 "tried" buckets.
117 * * Each address range selects at random 4 of these buckets.
118 * * The actual bucket is chosen from one of these, based on the full address.
119 * * When adding a new good address to a full bucket, a randomly chosen entry (with a bias favoring less recently
120 * tried ones) is evicted from it, back to the "new" buckets.
121 * * Bucket selection is based on cryptographic hashing, using a randomly-generated 256-bit key, which should not
122 * be observable by adversaries.
123 * * Several indexes are kept for high performance. Defining DEBUG_ADDRMAN will introduce frequent (and expensive)
124 * consistency checks for the entire data structure.
127 //! total number of buckets for tried addresses
128 #define ADDRMAN_TRIED_BUCKET_COUNT 64
130 //! maximum allowed number of entries in buckets for tried addresses
131 #define ADDRMAN_TRIED_BUCKET_SIZE 64
133 //! total number of buckets for new addresses
134 #define ADDRMAN_NEW_BUCKET_COUNT 256
136 //! maximum allowed number of entries in buckets for new addresses
137 #define ADDRMAN_NEW_BUCKET_SIZE 64
139 //! over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread
140 #define ADDRMAN_TRIED_BUCKETS_PER_GROUP 4
142 //! over how many buckets entries with new addresses originating from a single group are spread
143 #define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 32
145 //! in how many buckets for entries with new addresses a single address may occur
146 #define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 4
148 //! how many entries in a bucket with tried addresses are inspected, when selecting one to replace
149 #define ADDRMAN_TRIED_ENTRIES_INSPECT_ON_EVICT 4
151 //! how old addresses can maximally be
152 #define ADDRMAN_HORIZON_DAYS 30
154 //! after how many failed attempts we give up on a new node
155 #define ADDRMAN_RETRIES 3
157 //! how many successive failures are allowed ...
158 #define ADDRMAN_MAX_FAILURES 10
160 //! ... in at least this many days
161 #define ADDRMAN_MIN_FAIL_DAYS 7
163 //! the maximum percentage of nodes to return in a getaddr call
164 #define ADDRMAN_GETADDR_MAX_PCT 23
166 //! the maximum number of nodes to return in a getaddr call
167 #define ADDRMAN_GETADDR_MAX 2500
170 * Stochastical (IP) address manager
175 //! critical section to protect the inner data structures
176 mutable CCriticalSection cs;
178 //! secret key to randomize bucket select with
179 std::vector<unsigned char> nKey;
184 //! table with information about all nIds
185 std::map<int, CAddrInfo> mapInfo;
187 //! find an nId based on its network address
188 std::map<CNetAddr, int> mapAddr;
190 //! randomly-ordered vector of all nIds
191 std::vector<int> vRandom;
193 // number of "tried" entries
196 //! list of "tried" buckets
197 std::vector<std::vector<int> > vvTried;
199 //! number of (unique) "new" entries
202 //! list of "new" buckets
203 std::vector<std::set<int> > vvNew;
208 CAddrInfo* Find(const CNetAddr& addr, int *pnId = NULL);
210 //! find an entry, creating it if necessary.
211 //! nTime and nServices of the found node are updated, if necessary.
212 CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = NULL);
214 //! Swap two elements in vRandom.
215 void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2);
217 //! Return position in given bucket to replace.
218 int SelectTried(int nKBucket);
220 //! Remove an element from a "new" bucket.
221 //! This is the only place where actual deletions occur.
222 //! Elements are never deleted while in the "tried" table, only possibly evicted back to the "new" table.
223 int ShrinkNew(int nUBucket);
225 //! Move an entry from the "new" table(s) to the "tried" table
226 //! @pre vvUnkown[nOrigin].count(nId) != 0
227 void MakeTried(CAddrInfo& info, int nId, int nOrigin);
229 //! Mark an entry "good", possibly moving it from "new" to "tried".
230 void Good_(const CService &addr, int64_t nTime);
232 //! Add an entry to the "new" table.
233 bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty);
235 //! Mark an entry as attempted to connect.
236 void Attempt_(const CService &addr, int64_t nTime);
238 //! Select an address to connect to.
239 //! nUnkBias determines how much to favor new addresses over tried ones (min=0, max=100)
240 CAddress Select_(int nUnkBias);
243 //! Perform consistency check. Returns an error code or zero.
247 //! Select several addresses at once.
248 void GetAddr_(std::vector<CAddress> &vAddr);
250 //! Mark an entry as currently-connected-to.
251 void Connected_(const CService &addr, int64_t nTime);
256 * * version byte (currently 0)
260 * * number of "new" buckets
261 * * all nNew addrinfos in vvNew
262 * * all nTried addrinfos in vvTried
264 * * number of elements
265 * * for each element: index
267 * Notice that vvTried, mapAddr and vVector are never encoded explicitly;
268 * they are instead reconstructed from the other information.
270 * vvNew is serialized, but only used if ADDRMAN_UNKOWN_BUCKET_COUNT didn't change,
271 * otherwise it is reconstructed as well.
273 * This format is more complex, but significantly smaller (at most 1.5 MiB), and supports
274 * changes to the ADDRMAN_ parameters without breaking the on-disk structure.
276 * We don't use ADD_SERIALIZE_METHODS since the serialization and deserialization code has
277 * very little in common.
280 template<typename Stream>
281 void Serialize(Stream &s, int nType, int nVersionDummy) const
285 unsigned char nVersion = 0;
291 int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT;
293 std::map<int, int> mapUnkIds;
295 for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
296 if (nIds == nNew) break; // this means nNew was wrong, oh ow
297 mapUnkIds[(*it).first] = nIds;
298 const CAddrInfo &info = (*it).second;
299 if (info.nRefCount) {
305 for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
306 if (nIds == nTried) break; // this means nTried was wrong, oh ow
307 const CAddrInfo &info = (*it).second;
313 for (std::vector<std::set<int> >::const_iterator it = vvNew.begin(); it != vvNew.end(); it++) {
314 const std::set<int> &vNew = (*it);
315 int nSize = vNew.size();
317 for (std::set<int>::const_iterator it2 = vNew.begin(); it2 != vNew.end(); it2++) {
318 int nIndex = mapUnkIds[*it2];
324 template<typename Stream>
325 void Unserialize(Stream& s, int nType, int nVersionDummy)
329 unsigned char nVersion;
341 vvTried = std::vector<std::vector<int> >(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0));
342 vvNew = std::vector<std::set<int> >(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>());
343 for (int n = 0; n < nNew; n++) {
344 CAddrInfo &info = mapInfo[n];
347 info.nRandomPos = vRandom.size();
348 vRandom.push_back(n);
349 if (nUBuckets != ADDRMAN_NEW_BUCKET_COUNT) {
350 vvNew[info.GetNewBucket(nKey)].insert(n);
356 for (int n = 0; n < nTried; n++) {
359 std::vector<int> &vTried = vvTried[info.GetTriedBucket(nKey)];
360 if (vTried.size() < ADDRMAN_TRIED_BUCKET_SIZE) {
361 info.nRandomPos = vRandom.size();
362 info.fInTried = true;
363 vRandom.push_back(nIdCount);
364 mapInfo[nIdCount] = info;
365 mapAddr[info] = nIdCount;
366 vTried.push_back(nIdCount);
373 for (int b = 0; b < nUBuckets; b++) {
374 std::set<int> &vNew = vvNew[b];
377 for (int n = 0; n < nSize; n++) {
380 CAddrInfo &info = mapInfo[nIndex];
381 if (nUBuckets == ADDRMAN_NEW_BUCKET_COUNT && info.nRefCount < ADDRMAN_NEW_BUCKETS_PER_ADDRESS) {
389 unsigned int GetSerializeSize(int nType, int nVersion) const
391 return (CSizeComputer(nType, nVersion) << *this).size();
394 CAddrMan() : vRandom(0), vvTried(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0)), vvNew(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>())
397 GetRandBytes(&nKey[0], 32);
404 //! Return the number of (unique) addresses in all tables.
407 return vRandom.size();
410 //! Consistency check
418 LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
423 //! Add a single address.
424 bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0)
430 fRet |= Add_(addr, source, nTimePenalty);
434 LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
438 //! Add multiple addresses.
439 bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
445 for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
446 nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
450 LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
454 //! Mark an entry as accessible.
455 void Good(const CService &addr, int64_t nTime = GetAdjustedTime())
465 //! Mark an entry as connection attempted to.
466 void Attempt(const CService &addr, int64_t nTime = GetAdjustedTime())
471 Attempt_(addr, nTime);
477 * Choose an address to connect to.
478 * nUnkBias determines how much "new" entries are favored over "tried" ones (0-100).
480 CAddress Select(int nUnkBias = 50)
486 addrRet = Select_(nUnkBias);
492 //! Return a bunch of addresses, selected at random.
493 std::vector<CAddress> GetAddr()
496 std::vector<CAddress> vAddr;
505 //! Mark an entry as currently-connected-to.
506 void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
511 Connected_(addr, nTime);
517 #endif // _BITCOIN_ADDRMAN