Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

addrman.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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.
  4. #ifndef BITCOIN_ADDRMAN_H
  5. #define BITCOIN_ADDRMAN_H
  6. #include "netbase.h"
  7. #include "protocol.h"
  8. #include "random.h"
  9. #include "sync.h"
  10. #include "timedata.h"
  11. #include "util.h"
  12. #include <map>
  13. #include <set>
  14. #include <stdint.h>
  15. #include <vector>
  16. /**
  17. * Extended statistics about a CAddress
  18. */
  19. class CAddrInfo : public CAddress
  20. {
  21. public:
  22. //! last try whatsoever by us (memory only)
  23. int64_t nLastTry;
  24. private:
  25. //! where knowledge about this address first came from
  26. CNetAddr source;
  27. //! last successful connection by us
  28. int64_t nLastSuccess;
  29. //! connection attempts since last successful attempt
  30. int nAttempts;
  31. //! reference count in new sets (memory only)
  32. int nRefCount;
  33. //! in tried set? (memory only)
  34. bool fInTried;
  35. //! position in vRandom
  36. int nRandomPos;
  37. friend class CAddrMan;
  38. public:
  39. ADD_SERIALIZE_METHODS;
  40. template <typename Stream, typename Operation>
  41. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  42. READWRITE(*(CAddress*)this);
  43. READWRITE(source);
  44. READWRITE(nLastSuccess);
  45. READWRITE(nAttempts);
  46. }
  47. void Init()
  48. {
  49. nLastSuccess = 0;
  50. nLastTry = 0;
  51. nAttempts = 0;
  52. nRefCount = 0;
  53. fInTried = false;
  54. nRandomPos = -1;
  55. }
  56. CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
  57. {
  58. Init();
  59. }
  60. CAddrInfo() : CAddress(), source()
  61. {
  62. Init();
  63. }
  64. //! Calculate in which "tried" bucket this entry belongs
  65. int GetTriedBucket(const uint256 &nKey) const;
  66. //! Calculate in which "new" bucket this entry belongs, given a certain source
  67. int GetNewBucket(const uint256 &nKey, const CNetAddr& src) const;
  68. //! Calculate in which "new" bucket this entry belongs, using its default source
  69. int GetNewBucket(const uint256 &nKey) const
  70. {
  71. return GetNewBucket(nKey, source);
  72. }
  73. //! Calculate in which position of a bucket to store this entry.
  74. int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
  75. //! Determine whether the statistics about this entry are bad enough so that it can just be deleted
  76. bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;
  77. //! Calculate the relative chance this entry should be given when selecting nodes to connect to
  78. double GetChance(int64_t nNow = GetAdjustedTime()) const;
  79. };
  80. /** Stochastic address manager
  81. *
  82. * Design goals:
  83. * * Keep the address tables in-memory, and asynchronously dump the entire table to peers.dat.
  84. * * Make sure no (localized) attacker can fill the entire table with his nodes/addresses.
  85. *
  86. * To that end:
  87. * * Addresses are organized into buckets.
  88. * * Address that have not yet been tried go into 1024 "new" buckets.
  89. * * Based on the address range (/16 for IPv4) of source of the information, 64 buckets are selected at random
  90. * * The actual bucket is chosen from one of these, based on the range in which the address itself is located.
  91. * * One single address can occur in up to 8 different buckets, to increase selection chances for addresses that
  92. * are seen frequently. The chance for increasing this multiplicity decreases exponentially.
  93. * * When adding a new address to a full bucket, a randomly chosen entry (with a bias favoring less recently seen
  94. * ones) is removed from it first.
  95. * * Addresses of nodes that are known to be accessible go into 256 "tried" buckets.
  96. * * Each address range selects at random 8 of these buckets.
  97. * * The actual bucket is chosen from one of these, based on the full address.
  98. * * When adding a new good address to a full bucket, a randomly chosen entry (with a bias favoring less recently
  99. * tried ones) is evicted from it, back to the "new" buckets.
  100. * * Bucket selection is based on cryptographic hashing, using a randomly-generated 256-bit key, which should not
  101. * be observable by adversaries.
  102. * * Several indexes are kept for high performance. Defining DEBUG_ADDRMAN will introduce frequent (and expensive)
  103. * consistency checks for the entire data structure.
  104. */
  105. //! total number of buckets for tried addresses
  106. #define ADDRMAN_TRIED_BUCKET_COUNT 256
  107. //! total number of buckets for new addresses
  108. #define ADDRMAN_NEW_BUCKET_COUNT 1024
  109. //! maximum allowed number of entries in buckets for new and tried addresses
  110. #define ADDRMAN_BUCKET_SIZE 64
  111. //! over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread
  112. #define ADDRMAN_TRIED_BUCKETS_PER_GROUP 8
  113. //! over how many buckets entries with new addresses originating from a single group are spread
  114. #define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 64
  115. //! in how many buckets for entries with new addresses a single address may occur
  116. #define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 8
  117. //! how old addresses can maximally be
  118. #define ADDRMAN_HORIZON_DAYS 30
  119. //! after how many failed attempts we give up on a new node
  120. #define ADDRMAN_RETRIES 3
  121. //! how many successive failures are allowed ...
  122. #define ADDRMAN_MAX_FAILURES 10
  123. //! ... in at least this many days
  124. #define ADDRMAN_MIN_FAIL_DAYS 7
  125. //! the maximum percentage of nodes to return in a getaddr call
  126. #define ADDRMAN_GETADDR_MAX_PCT 23
  127. //! the maximum number of nodes to return in a getaddr call
  128. #define ADDRMAN_GETADDR_MAX 2500
  129. /**
  130. * Stochastical (IP) address manager
  131. */
  132. class CAddrMan
  133. {
  134. private:
  135. //! critical section to protect the inner data structures
  136. mutable CCriticalSection cs;
  137. //! secret key to randomize bucket select with
  138. uint256 nKey;
  139. //! last used nId
  140. int nIdCount;
  141. //! table with information about all nIds
  142. std::map<int, CAddrInfo> mapInfo;
  143. //! find an nId based on its network address
  144. std::map<CNetAddr, int> mapAddr;
  145. //! randomly-ordered vector of all nIds
  146. std::vector<int> vRandom;
  147. // number of "tried" entries
  148. int nTried;
  149. //! list of "tried" buckets
  150. int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
  151. //! number of (unique) "new" entries
  152. int nNew;
  153. //! list of "new" buckets
  154. int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
  155. protected:
  156. //! Find an entry.
  157. CAddrInfo* Find(const CNetAddr& addr, int *pnId = NULL);
  158. //! find an entry, creating it if necessary.
  159. //! nTime and nServices of the found node are updated, if necessary.
  160. CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = NULL);
  161. //! Swap two elements in vRandom.
  162. void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2);
  163. //! Move an entry from the "new" table(s) to the "tried" table
  164. void MakeTried(CAddrInfo& info, int nId);
  165. //! Delete an entry. It must not be in tried, and have refcount 0.
  166. void Delete(int nId);
  167. //! Clear a position in a "new" table. This is the only place where entries are actually deleted.
  168. void ClearNew(int nUBucket, int nUBucketPos);
  169. //! Mark an entry "good", possibly moving it from "new" to "tried".
  170. void Good_(const CService &addr, int64_t nTime);
  171. //! Add an entry to the "new" table.
  172. bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty);
  173. //! Mark an entry as attempted to connect.
  174. void Attempt_(const CService &addr, int64_t nTime);
  175. //! Select an address to connect to.
  176. //! nUnkBias determines how much to favor new addresses over tried ones (min=0, max=100)
  177. CAddrInfo Select_();
  178. #ifdef DEBUG_ADDRMAN
  179. //! Perform consistency check. Returns an error code or zero.
  180. int Check_();
  181. #endif
  182. //! Select several addresses at once.
  183. void GetAddr_(std::vector<CAddress> &vAddr);
  184. //! Mark an entry as currently-connected-to.
  185. void Connected_(const CService &addr, int64_t nTime);
  186. public:
  187. /**
  188. * serialized format:
  189. * * version byte (currently 1)
  190. * * 0x20 + nKey (serialized as if it were a vector, for backward compatibility)
  191. * * nNew
  192. * * nTried
  193. * * number of "new" buckets XOR 2**30
  194. * * all nNew addrinfos in vvNew
  195. * * all nTried addrinfos in vvTried
  196. * * for each bucket:
  197. * * number of elements
  198. * * for each element: index
  199. *
  200. * 2**30 is xorred with the number of buckets to make addrman deserializer v0 detect it
  201. * as incompatible. This is necessary because it did not check the version number on
  202. * deserialization.
  203. *
  204. * Notice that vvTried, mapAddr and vVector are never encoded explicitly;
  205. * they are instead reconstructed from the other information.
  206. *
  207. * vvNew is serialized, but only used if ADDRMAN_UNKOWN_BUCKET_COUNT didn't change,
  208. * otherwise it is reconstructed as well.
  209. *
  210. * This format is more complex, but significantly smaller (at most 1.5 MiB), and supports
  211. * changes to the ADDRMAN_ parameters without breaking the on-disk structure.
  212. *
  213. * We don't use ADD_SERIALIZE_METHODS since the serialization and deserialization code has
  214. * very little in common.
  215. */
  216. template<typename Stream>
  217. void Serialize(Stream &s, int nType, int nVersionDummy) const
  218. {
  219. LOCK(cs);
  220. unsigned char nVersion = 1;
  221. s << nVersion;
  222. s << ((unsigned char)32);
  223. s << nKey;
  224. s << nNew;
  225. s << nTried;
  226. int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
  227. s << nUBuckets;
  228. std::map<int, int> mapUnkIds;
  229. int nIds = 0;
  230. for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
  231. mapUnkIds[(*it).first] = nIds;
  232. const CAddrInfo &info = (*it).second;
  233. if (info.nRefCount) {
  234. assert(nIds != nNew); // this means nNew was wrong, oh ow
  235. s << info;
  236. nIds++;
  237. }
  238. }
  239. nIds = 0;
  240. for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
  241. const CAddrInfo &info = (*it).second;
  242. if (info.fInTried) {
  243. assert(nIds != nTried); // this means nTried was wrong, oh ow
  244. s << info;
  245. nIds++;
  246. }
  247. }
  248. for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
  249. int nSize = 0;
  250. for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
  251. if (vvNew[bucket][i] != -1)
  252. nSize++;
  253. }
  254. s << nSize;
  255. for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
  256. if (vvNew[bucket][i] != -1) {
  257. int nIndex = mapUnkIds[vvNew[bucket][i]];
  258. s << nIndex;
  259. }
  260. }
  261. }
  262. }
  263. template<typename Stream>
  264. void Unserialize(Stream& s, int nType, int nVersionDummy)
  265. {
  266. LOCK(cs);
  267. Clear();
  268. unsigned char nVersion;
  269. s >> nVersion;
  270. unsigned char nKeySize;
  271. s >> nKeySize;
  272. if (nKeySize != 32) throw std::ios_base::failure("Incorrect keysize in addrman deserialization");
  273. s >> nKey;
  274. s >> nNew;
  275. s >> nTried;
  276. int nUBuckets = 0;
  277. s >> nUBuckets;
  278. if (nVersion != 0) {
  279. nUBuckets ^= (1 << 30);
  280. }
  281. // Deserialize entries from the new table.
  282. for (int n = 0; n < nNew; n++) {
  283. CAddrInfo &info = mapInfo[n];
  284. s >> info;
  285. mapAddr[info] = n;
  286. info.nRandomPos = vRandom.size();
  287. vRandom.push_back(n);
  288. if (nVersion != 1 || nUBuckets != ADDRMAN_NEW_BUCKET_COUNT) {
  289. // In case the new table data cannot be used (nVersion unknown, or bucket count wrong),
  290. // immediately try to give them a reference based on their primary source address.
  291. int nUBucket = info.GetNewBucket(nKey);
  292. int nUBucketPos = info.GetBucketPosition(nKey, true, nUBucket);
  293. if (vvNew[nUBucket][nUBucketPos] == -1) {
  294. vvNew[nUBucket][nUBucketPos] = n;
  295. info.nRefCount++;
  296. }
  297. }
  298. }
  299. nIdCount = nNew;
  300. // Deserialize entries from the tried table.
  301. int nLost = 0;
  302. for (int n = 0; n < nTried; n++) {
  303. CAddrInfo info;
  304. s >> info;
  305. int nKBucket = info.GetTriedBucket(nKey);
  306. int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
  307. if (vvTried[nKBucket][nKBucketPos] == -1) {
  308. info.nRandomPos = vRandom.size();
  309. info.fInTried = true;
  310. vRandom.push_back(nIdCount);
  311. mapInfo[nIdCount] = info;
  312. mapAddr[info] = nIdCount;
  313. vvTried[nKBucket][nKBucketPos] = nIdCount;
  314. nIdCount++;
  315. } else {
  316. nLost++;
  317. }
  318. }
  319. nTried -= nLost;
  320. // Deserialize positions in the new table (if possible).
  321. for (int bucket = 0; bucket < nUBuckets; bucket++) {
  322. int nSize = 0;
  323. s >> nSize;
  324. for (int n = 0; n < nSize; n++) {
  325. int nIndex = 0;
  326. s >> nIndex;
  327. if (nIndex >= 0 && nIndex < nNew) {
  328. CAddrInfo &info = mapInfo[nIndex];
  329. int nUBucketPos = info.GetBucketPosition(nKey, true, bucket);
  330. if (nVersion == 1 && nUBuckets == ADDRMAN_NEW_BUCKET_COUNT && vvNew[bucket][nUBucketPos] == -1 && info.nRefCount < ADDRMAN_NEW_BUCKETS_PER_ADDRESS) {
  331. info.nRefCount++;
  332. vvNew[bucket][nUBucketPos] = nIndex;
  333. }
  334. }
  335. }
  336. }
  337. // Prune new entries with refcount 0 (as a result of collisions).
  338. int nLostUnk = 0;
  339. for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); ) {
  340. if (it->second.fInTried == false && it->second.nRefCount == 0) {
  341. std::map<int, CAddrInfo>::const_iterator itCopy = it++;
  342. Delete(itCopy->first);
  343. nLostUnk++;
  344. } else {
  345. it++;
  346. }
  347. }
  348. if (nLost + nLostUnk > 0) {
  349. LogPrint("addrman", "addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk, nLost);
  350. }
  351. Check();
  352. }
  353. unsigned int GetSerializeSize(int nType, int nVersion) const
  354. {
  355. return (CSizeComputer(nType, nVersion) << *this).size();
  356. }
  357. void Clear()
  358. {
  359. std::vector<int>().swap(vRandom);
  360. nKey = GetRandHash();
  361. for (size_t bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
  362. for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
  363. vvNew[bucket][entry] = -1;
  364. }
  365. }
  366. for (size_t bucket = 0; bucket < ADDRMAN_TRIED_BUCKET_COUNT; bucket++) {
  367. for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
  368. vvTried[bucket][entry] = -1;
  369. }
  370. }
  371. nIdCount = 0;
  372. nTried = 0;
  373. nNew = 0;
  374. }
  375. CAddrMan()
  376. {
  377. Clear();
  378. }
  379. ~CAddrMan()
  380. {
  381. nKey.SetNull();
  382. }
  383. //! Return the number of (unique) addresses in all tables.
  384. int size()
  385. {
  386. return vRandom.size();
  387. }
  388. //! Consistency check
  389. void Check()
  390. {
  391. #ifdef DEBUG_ADDRMAN
  392. {
  393. LOCK(cs);
  394. int err;
  395. if ((err=Check_()))
  396. LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
  397. }
  398. #endif
  399. }
  400. //! Add a single address.
  401. bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0)
  402. {
  403. bool fRet = false;
  404. {
  405. LOCK(cs);
  406. Check();
  407. fRet |= Add_(addr, source, nTimePenalty);
  408. Check();
  409. }
  410. if (fRet)
  411. LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
  412. return fRet;
  413. }
  414. //! Add multiple addresses.
  415. bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
  416. {
  417. int nAdd = 0;
  418. {
  419. LOCK(cs);
  420. Check();
  421. for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
  422. nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
  423. Check();
  424. }
  425. if (nAdd)
  426. LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
  427. return nAdd > 0;
  428. }
  429. //! Mark an entry as accessible.
  430. void Good(const CService &addr, int64_t nTime = GetAdjustedTime())
  431. {
  432. {
  433. LOCK(cs);
  434. Check();
  435. Good_(addr, nTime);
  436. Check();
  437. }
  438. }
  439. //! Mark an entry as connection attempted to.
  440. void Attempt(const CService &addr, int64_t nTime = GetAdjustedTime())
  441. {
  442. {
  443. LOCK(cs);
  444. Check();
  445. Attempt_(addr, nTime);
  446. Check();
  447. }
  448. }
  449. /**
  450. * Choose an address to connect to.
  451. * nUnkBias determines how much "new" entries are favored over "tried" ones (0-100).
  452. */
  453. CAddrInfo Select()
  454. {
  455. CAddrInfo addrRet;
  456. {
  457. LOCK(cs);
  458. Check();
  459. addrRet = Select_();
  460. Check();
  461. }
  462. return addrRet;
  463. }
  464. //! Return a bunch of addresses, selected at random.
  465. std::vector<CAddress> GetAddr()
  466. {
  467. Check();
  468. std::vector<CAddress> vAddr;
  469. {
  470. LOCK(cs);
  471. GetAddr_(vAddr);
  472. }
  473. Check();
  474. return vAddr;
  475. }
  476. //! Mark an entry as currently-connected-to.
  477. void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
  478. {
  479. {
  480. LOCK(cs);
  481. Check();
  482. Connected_(addr, nTime);
  483. Check();
  484. }
  485. }
  486. };
  487. #endif // BITCOIN_ADDRMAN_H