You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

addrman.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // Copyright (c) 2012 Pieter Wuille
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file license.txt or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef _BITCOIN_ADDRMAN
  5. #define _BITCOIN_ADDRMAN 1
  6. #include "netbase.h"
  7. #include "protocol.h"
  8. #include "util.h"
  9. #include <map>
  10. #include <vector>
  11. #include <openssl/rand.h>
  12. /** Extended statistics about a CAddress */
  13. class CAddrInfo : public CAddress
  14. {
  15. private:
  16. // where knowledge about this address first came from
  17. CNetAddr source;
  18. // last succesfull connection by us
  19. int64 nLastSuccess;
  20. // last try whatsoever by us:
  21. // int64 CAddress::nLastTry
  22. // connection attempts since last succesful attempt
  23. int nAttempts;
  24. // reference count in new sets (memory only)
  25. int nRefCount;
  26. // in tried set? (memory only)
  27. bool fInTried;
  28. // position in vRandom
  29. int nRandomPos;
  30. friend class CAddrMan;
  31. public:
  32. IMPLEMENT_SERIALIZE(
  33. CAddress* pthis = (CAddress*)(this);
  34. READWRITE(*pthis);
  35. READWRITE(source);
  36. READWRITE(nLastSuccess);
  37. READWRITE(nAttempts);
  38. )
  39. void Init()
  40. {
  41. nLastSuccess = 0;
  42. nLastTry = 0;
  43. nAttempts = 0;
  44. nRefCount = 0;
  45. fInTried = false;
  46. nRandomPos = -1;
  47. }
  48. CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
  49. {
  50. Init();
  51. }
  52. CAddrInfo() : CAddress(), source()
  53. {
  54. Init();
  55. }
  56. // Calculate in which "tried" bucket this entry belongs
  57. int GetTriedBucket(const std::vector<unsigned char> &nKey) const;
  58. // Calculate in which "new" bucket this entry belongs, given a certain source
  59. int GetNewBucket(const std::vector<unsigned char> &nKey, const CNetAddr& src) const;
  60. // Calculate in which "new" bucket this entry belongs, using its default source
  61. int GetNewBucket(const std::vector<unsigned char> &nKey) const
  62. {
  63. return GetNewBucket(nKey, source);
  64. }
  65. // Determine whether the statistics about this entry are bad enough so that it can just be deleted
  66. bool IsTerrible(int64 nNow = GetAdjustedTime()) const;
  67. // Calculate the relative chance this entry should be given when selecting nodes to connect to
  68. double GetChance(int64 nNow = GetAdjustedTime()) const;
  69. };
  70. // Stochastic address manager
  71. //
  72. // Design goals:
  73. // * Only keep a limited number of addresses around, so that addr.dat and memory requirements do not grow without bound.
  74. // * Keep the address tables in-memory, and asynchronously dump the entire to able in addr.dat.
  75. // * Make sure no (localized) attacker can fill the entire table with his nodes/addresses.
  76. //
  77. // To that end:
  78. // * Addresses are organized into buckets.
  79. // * Address that have not yet been tried go into 256 "new" buckets.
  80. // * Based on the address range (/16 for IPv4) of source of the information, 32 buckets are selected at random
  81. // * The actual bucket is chosen from one of these, based on the range the address itself is located.
  82. // * One single address can occur in up to 4 different buckets, to increase selection chances for addresses that
  83. // are seen frequently. The chance for increasing this multiplicity decreases exponentially.
  84. // * When adding a new address to a full bucket, a randomly chosen entry (with a bias favoring less recently seen
  85. // ones) is removed from it first.
  86. // * Addresses of nodes that are known to be accessible go into 64 "tried" buckets.
  87. // * Each address range selects at random 4 of these buckets.
  88. // * The actual bucket is chosen from one of these, based on the full address.
  89. // * When adding a new good address to a full bucket, a randomly chosen entry (with a bias favoring less recently
  90. // tried ones) is evicted from it, back to the "new" buckets.
  91. // * Bucket selection is based on cryptographic hashing, using a randomly-generated 256-bit key, which should not
  92. // be observable by adversaries.
  93. // * Several indexes are kept for high performance. Defining DEBUG_ADDRMAN will introduce frequent (and expensive)
  94. // consistency checks for the entire datastructure.
  95. // total number of buckets for tried addresses
  96. #define ADDRMAN_TRIED_BUCKET_COUNT 64
  97. // maximum allowed number of entries in buckets for tried addresses
  98. #define ADDRMAN_TRIED_BUCKET_SIZE 64
  99. // total number of buckets for new addresses
  100. #define ADDRMAN_NEW_BUCKET_COUNT 256
  101. // maximum allowed number of entries in buckets for new addresses
  102. #define ADDRMAN_NEW_BUCKET_SIZE 64
  103. // over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread
  104. #define ADDRMAN_TRIED_BUCKETS_PER_GROUP 4
  105. // over how many buckets entries with new addresses originating from a single group are spread
  106. #define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 32
  107. // in how many buckets for entries with new addresses a single address may occur
  108. #define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 4
  109. // how many entries in a bucket with tried addresses are inspected, when selecting one to replace
  110. #define ADDRMAN_TRIED_ENTRIES_INSPECT_ON_EVICT 4
  111. // how old addresses can maximally be
  112. #define ADDRMAN_HORIZON_DAYS 30
  113. // after how many failed attempts we give up on a new node
  114. #define ADDRMAN_RETRIES 3
  115. // how many successive failures are allowed ...
  116. #define ADDRMAN_MAX_FAILURES 10
  117. // ... in at least this many days
  118. #define ADDRMAN_MIN_FAIL_DAYS 7
  119. // the maximum percentage of nodes to return in a getaddr call
  120. #define ADDRMAN_GETADDR_MAX_PCT 23
  121. // the maximum number of nodes to return in a getaddr call
  122. #define ADDRMAN_GETADDR_MAX 2500
  123. /** Stochastical (IP) address manager */
  124. class CAddrMan
  125. {
  126. private:
  127. // critical section to protect the inner data structures
  128. mutable CCriticalSection cs;
  129. // secret key to randomize bucket select with
  130. std::vector<unsigned char> nKey;
  131. // last used nId
  132. int nIdCount;
  133. // table with information about all nId's
  134. std::map<int, CAddrInfo> mapInfo;
  135. // find an nId based on its network address
  136. std::map<CNetAddr, int> mapAddr;
  137. // randomly-ordered vector of all nId's
  138. std::vector<int> vRandom;
  139. // number of "tried" entries
  140. int nTried;
  141. // list of "tried" buckets
  142. std::vector<std::vector<int> > vvTried;
  143. // number of (unique) "new" entries
  144. int nNew;
  145. // list of "new" buckets
  146. std::vector<std::set<int> > vvNew;
  147. protected:
  148. // Find an entry.
  149. CAddrInfo* Find(const CNetAddr& addr, int *pnId = NULL);
  150. // find an entry, creating it if necessary.
  151. // nTime and nServices of found node is updated, if necessary.
  152. CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = NULL);
  153. // Swap two elements in vRandom.
  154. void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2);
  155. // Return position in given bucket to replace.
  156. int SelectTried(int nKBucket);
  157. // Remove an element from a "new" bucket.
  158. // This is the only place where actual deletes occur.
  159. // They are never deleted while in the "tried" table, only possibly evicted back to the "new" table.
  160. int ShrinkNew(int nUBucket);
  161. // Move an entry from the "new" table(s) to the "tried" table
  162. // @pre vvUnkown[nOrigin].count(nId) != 0
  163. void MakeTried(CAddrInfo& info, int nId, int nOrigin);
  164. // Mark an entry "good", possibly moving it from "new" to "tried".
  165. void Good_(const CService &addr, int64 nTime);
  166. // Add an entry to the "new" table.
  167. bool Add_(const CAddress &addr, const CNetAddr& source, int64 nTimePenalty);
  168. // Mark an entry as attempted to connect.
  169. void Attempt_(const CService &addr, int64 nTime);
  170. // Select an address to connect to.
  171. // nUnkBias determines how much to favor new addresses over tried ones (min=0, max=100)
  172. CAddress Select_(int nUnkBias);
  173. #ifdef DEBUG_ADDRMAN
  174. // Perform consistency check. Returns an error code or zero.
  175. int Check_();
  176. #endif
  177. // Select several addresses at once.
  178. void GetAddr_(std::vector<CAddress> &vAddr);
  179. // Mark an entry as currently-connected-to.
  180. void Connected_(const CService &addr, int64 nTime);
  181. public:
  182. IMPLEMENT_SERIALIZE
  183. (({
  184. // serialized format:
  185. // * version byte (currently 0)
  186. // * nKey
  187. // * nNew
  188. // * nTried
  189. // * number of "new" buckets
  190. // * all nNew addrinfo's in vvNew
  191. // * all nTried addrinfo's in vvTried
  192. // * for each bucket:
  193. // * number of elements
  194. // * for each element: index
  195. //
  196. // Notice that vvTried, mapAddr and vVector are never encoded explicitly;
  197. // they are instead reconstructed from the other information.
  198. //
  199. // vvNew is serialized, but only used if ADDRMAN_UNKOWN_BUCKET_COUNT didn't change,
  200. // otherwise it is reconstructed as well.
  201. //
  202. // This format is more complex, but significantly smaller (at most 1.5 MiB), and supports
  203. // changes to the ADDRMAN_ parameters without breaking the on-disk structure.
  204. {
  205. LOCK(cs);
  206. unsigned char nVersion = 0;
  207. READWRITE(nVersion);
  208. READWRITE(nKey);
  209. READWRITE(nNew);
  210. READWRITE(nTried);
  211. CAddrMan *am = const_cast<CAddrMan*>(this);
  212. if (fWrite)
  213. {
  214. int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT;
  215. READWRITE(nUBuckets);
  216. std::map<int, int> mapUnkIds;
  217. int nIds = 0;
  218. for (std::map<int, CAddrInfo>::iterator it = am->mapInfo.begin(); it != am->mapInfo.end(); it++)
  219. {
  220. if (nIds == nNew) break; // this means nNew was wrong, oh ow
  221. mapUnkIds[(*it).first] = nIds;
  222. CAddrInfo &info = (*it).second;
  223. if (info.nRefCount)
  224. {
  225. READWRITE(info);
  226. nIds++;
  227. }
  228. }
  229. nIds = 0;
  230. for (std::map<int, CAddrInfo>::iterator it = am->mapInfo.begin(); it != am->mapInfo.end(); it++)
  231. {
  232. if (nIds == nTried) break; // this means nTried was wrong, oh ow
  233. CAddrInfo &info = (*it).second;
  234. if (info.fInTried)
  235. {
  236. READWRITE(info);
  237. nIds++;
  238. }
  239. }
  240. for (std::vector<std::set<int> >::iterator it = am->vvNew.begin(); it != am->vvNew.end(); it++)
  241. {
  242. const std::set<int> &vNew = (*it);
  243. int nSize = vNew.size();
  244. READWRITE(nSize);
  245. for (std::set<int>::iterator it2 = vNew.begin(); it2 != vNew.end(); it2++)
  246. {
  247. int nIndex = mapUnkIds[*it2];
  248. READWRITE(nIndex);
  249. }
  250. }
  251. } else {
  252. int nUBuckets = 0;
  253. READWRITE(nUBuckets);
  254. am->nIdCount = 0;
  255. am->mapInfo.clear();
  256. am->mapAddr.clear();
  257. am->vRandom.clear();
  258. am->vvTried = std::vector<std::vector<int> >(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0));
  259. am->vvNew = std::vector<std::set<int> >(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>());
  260. for (int n = 0; n < am->nNew; n++)
  261. {
  262. CAddrInfo &info = am->mapInfo[n];
  263. READWRITE(info);
  264. am->mapAddr[info] = n;
  265. info.nRandomPos = vRandom.size();
  266. am->vRandom.push_back(n);
  267. if (nUBuckets != ADDRMAN_NEW_BUCKET_COUNT)
  268. {
  269. am->vvNew[info.GetNewBucket(am->nKey)].insert(n);
  270. info.nRefCount++;
  271. }
  272. }
  273. am->nIdCount = am->nNew;
  274. int nLost = 0;
  275. for (int n = 0; n < am->nTried; n++)
  276. {
  277. CAddrInfo info;
  278. READWRITE(info);
  279. std::vector<int> &vTried = am->vvTried[info.GetTriedBucket(am->nKey)];
  280. if (vTried.size() < ADDRMAN_TRIED_BUCKET_SIZE)
  281. {
  282. info.nRandomPos = vRandom.size();
  283. info.fInTried = true;
  284. am->vRandom.push_back(am->nIdCount);
  285. am->mapInfo[am->nIdCount] = info;
  286. am->mapAddr[info] = am->nIdCount;
  287. vTried.push_back(am->nIdCount);
  288. am->nIdCount++;
  289. } else {
  290. nLost++;
  291. }
  292. }
  293. am->nTried -= nLost;
  294. for (int b = 0; b < nUBuckets; b++)
  295. {
  296. std::set<int> &vNew = am->vvNew[b];
  297. int nSize = 0;
  298. READWRITE(nSize);
  299. for (int n = 0; n < nSize; n++)
  300. {
  301. int nIndex = 0;
  302. READWRITE(nIndex);
  303. CAddrInfo &info = am->mapInfo[nIndex];
  304. if (nUBuckets == ADDRMAN_NEW_BUCKET_COUNT && info.nRefCount < ADDRMAN_NEW_BUCKETS_PER_ADDRESS)
  305. {
  306. info.nRefCount++;
  307. vNew.insert(nIndex);
  308. }
  309. }
  310. }
  311. }
  312. }
  313. });)
  314. CAddrMan() : vRandom(0), vvTried(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0)), vvNew(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>())
  315. {
  316. nKey.resize(32);
  317. RAND_bytes(&nKey[0], 32);
  318. nIdCount = 0;
  319. nTried = 0;
  320. nNew = 0;
  321. }
  322. // Return the number of (unique) addresses in all tables.
  323. int size()
  324. {
  325. return vRandom.size();
  326. }
  327. // Consistency check
  328. void Check()
  329. {
  330. #ifdef DEBUG_ADDRMAN
  331. {
  332. LOCK(cs);
  333. int err;
  334. if ((err=Check_()))
  335. printf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
  336. }
  337. #endif
  338. }
  339. // Add a single address.
  340. bool Add(const CAddress &addr, const CNetAddr& source, int64 nTimePenalty = 0)
  341. {
  342. bool fRet = false;
  343. {
  344. LOCK(cs);
  345. Check();
  346. fRet |= Add_(addr, source, nTimePenalty);
  347. Check();
  348. }
  349. if (fRet)
  350. printf("Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort().c_str(), source.ToString().c_str(), nTried, nNew);
  351. return fRet;
  352. }
  353. // Add multiple addresses.
  354. bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64 nTimePenalty = 0)
  355. {
  356. int nAdd = 0;
  357. {
  358. LOCK(cs);
  359. Check();
  360. for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
  361. nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
  362. Check();
  363. }
  364. if (nAdd)
  365. printf("Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString().c_str(), nTried, nNew);
  366. return nAdd > 0;
  367. }
  368. // Mark an entry as accessible.
  369. void Good(const CService &addr, int64 nTime = GetAdjustedTime())
  370. {
  371. {
  372. LOCK(cs);
  373. Check();
  374. Good_(addr, nTime);
  375. Check();
  376. }
  377. }
  378. // Mark an entry as connection attempted to.
  379. void Attempt(const CService &addr, int64 nTime = GetAdjustedTime())
  380. {
  381. {
  382. LOCK(cs);
  383. Check();
  384. Attempt_(addr, nTime);
  385. Check();
  386. }
  387. }
  388. // Choose an address to connect to.
  389. // nUnkBias determines how much "new" entries are favored over "tried" ones (0-100).
  390. CAddress Select(int nUnkBias = 50)
  391. {
  392. CAddress addrRet;
  393. {
  394. LOCK(cs);
  395. Check();
  396. addrRet = Select_(nUnkBias);
  397. Check();
  398. }
  399. return addrRet;
  400. }
  401. // Return a bunch of addresses, selected at random.
  402. std::vector<CAddress> GetAddr()
  403. {
  404. Check();
  405. std::vector<CAddress> vAddr;
  406. {
  407. LOCK(cs);
  408. GetAddr_(vAddr);
  409. }
  410. Check();
  411. return vAddr;
  412. }
  413. // Mark an entry as currently-connected-to.
  414. void Connected(const CService &addr, int64 nTime = GetAdjustedTime())
  415. {
  416. {
  417. LOCK(cs);
  418. Check();
  419. Connected_(addr, nTime);
  420. Check();
  421. }
  422. }
  423. };
  424. #endif