addrman.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // ECOin - Copyright (c) - 2014/2021 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef _ECOIN_ADDRMAN
  3. #define _ECOIN_ADDRMAN 1
  4. #include "netbase.h"
  5. #include "protocol.h"
  6. #include "util.h"
  7. #include "sync.h"
  8. #include <map>
  9. #include <vector>
  10. #include <openssl/rand.h>
  11. /** Extended statistics about a CAddress */
  12. class CAddrInfo : public CAddress
  13. {
  14. private:
  15. // where knowledge about this address first came from
  16. CNetAddr source;
  17. // last successful connection by us
  18. int64 nLastSuccess;
  19. // last try whatsoever by us:
  20. // int64 CAddress::nLastTry
  21. // connection attempts since last successful attempt
  22. int nAttempts;
  23. // reference count in new sets (memory only)
  24. int nRefCount;
  25. // in tried set? (memory only)
  26. bool fInTried;
  27. // position in vRandom
  28. int nRandomPos;
  29. friend class CAddrMan;
  30. public:
  31. IMPLEMENT_SERIALIZE(
  32. CAddress* pthis = (CAddress*)(this);
  33. READWRITE(*pthis);
  34. READWRITE(source);
  35. READWRITE(nLastSuccess);
  36. READWRITE(nAttempts);
  37. )
  38. void Init()
  39. {
  40. nLastSuccess = 0;
  41. nLastTry = 0;
  42. nAttempts = 0;
  43. nRefCount = 0;
  44. fInTried = false;
  45. nRandomPos = -1;
  46. }
  47. CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
  48. {
  49. Init();
  50. }
  51. CAddrInfo() : CAddress(), source()
  52. {
  53. Init();
  54. }
  55. // Calculate in which "tried" bucket this entry belongs
  56. int GetTriedBucket(const std::vector<unsigned char> &nKey) const;
  57. // Calculate in which "new" bucket this entry belongs, given a certain source
  58. int GetNewBucket(const std::vector<unsigned char> &nKey, const CNetAddr& src) const;
  59. // Calculate in which "new" bucket this entry belongs, using its default source
  60. int GetNewBucket(const std::vector<unsigned char> &nKey) const
  61. {
  62. return GetNewBucket(nKey, source);
  63. }
  64. // Determine whether the statistics about this entry are bad enough so that it can just be deleted
  65. bool IsTerrible(int64 nNow = GetAdjustedTime()) const;
  66. // Calculate the relative chance this entry should be given when selecting nodes to connect to
  67. double GetChance(int64 nNow = GetAdjustedTime()) const;
  68. };
  69. // total number of buckets for tried addresses
  70. #define ADDRMAN_TRIED_BUCKET_COUNT 64
  71. // maximum allowed number of entries in buckets for tried addresses
  72. #define ADDRMAN_TRIED_BUCKET_SIZE 64
  73. // total number of buckets for new addresses
  74. #define ADDRMAN_NEW_BUCKET_COUNT 256
  75. // maximum allowed number of entries in buckets for new addresses
  76. #define ADDRMAN_NEW_BUCKET_SIZE 64
  77. // over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread
  78. #define ADDRMAN_TRIED_BUCKETS_PER_GROUP 4
  79. // over how many buckets entries with new addresses originating from a single group are spread
  80. #define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 32
  81. // in how many buckets for entries with new addresses a single address may occur
  82. #define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 4
  83. // how many entries in a bucket with tried addresses are inspected, when selecting one to replace
  84. #define ADDRMAN_TRIED_ENTRIES_INSPECT_ON_EVICT 4
  85. // how old addresses can maximally be
  86. #define ADDRMAN_HORIZON_DAYS 30
  87. // after how many failed attempts we give up on a new node
  88. #define ADDRMAN_RETRIES 3
  89. // how many successive failures are allowed ...
  90. #define ADDRMAN_MAX_FAILURES 10
  91. // ... in at least this many days
  92. #define ADDRMAN_MIN_FAIL_DAYS 7
  93. // the maximum percentage of nodes to return in a getaddr call
  94. #define ADDRMAN_GETADDR_MAX_PCT 23
  95. // the maximum number of nodes to return in a getaddr call
  96. #define ADDRMAN_GETADDR_MAX 2500
  97. /** Stochastical (IP) address manager */
  98. class CAddrMan
  99. {
  100. private:
  101. // critical section to protect the inner data structures
  102. mutable CCriticalSection cs;
  103. // secret key to randomize bucket select with
  104. std::vector<unsigned char> nKey;
  105. // last used nId
  106. int nIdCount;
  107. // table with information about all nIds
  108. std::map<int, CAddrInfo> mapInfo;
  109. // find an nId based on its network address
  110. std::map<CNetAddr, int> mapAddr;
  111. // randomly-ordered vector of all nIds
  112. std::vector<int> vRandom;
  113. // number of "tried" entries
  114. int nTried;
  115. // list of "tried" buckets
  116. std::vector<std::vector<int> > vvTried;
  117. // number of (unique) "new" entries
  118. int nNew;
  119. // list of "new" buckets
  120. std::vector<std::set<int> > vvNew;
  121. protected:
  122. // Find an entry.
  123. CAddrInfo* Find(const CNetAddr& addr, int *pnId = NULL);
  124. // find an entry, creating it if necessary.
  125. // nTime and nServices of found node is updated, if necessary.
  126. CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = NULL);
  127. // Swap two elements in vRandom.
  128. void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2);
  129. // Return position in given bucket to replace.
  130. int SelectTried(int nKBucket);
  131. // Remove an element from a "new" bucket.
  132. // This is the only place where actual deletes occur.
  133. // They are never deleted while in the "tried" table, only possibly evicted back to the "new" table.
  134. int ShrinkNew(int nUBucket);
  135. // Move an entry from the "new" table(s) to the "tried" table
  136. // @pre vvUnkown[nOrigin].count(nId) != 0
  137. void MakeTried(CAddrInfo& info, int nId, int nOrigin);
  138. // Mark an entry "good", possibly moving it from "new" to "tried".
  139. void Good_(const CService &addr, int64 nTime);
  140. // Add an entry to the "new" table.
  141. bool Add_(const CAddress &addr, const CNetAddr& source, int64 nTimePenalty);
  142. // Mark an entry as attempted to connect.
  143. void Attempt_(const CService &addr, int64 nTime);
  144. // Select an address to connect to.
  145. // nUnkBias determines how much to favor new addresses over tried ones (min=0, max=100)
  146. CAddress Select_(int nUnkBias);
  147. #ifdef DEBUG_ADDRMAN
  148. // Perform consistency check. Returns an error code or zero.
  149. int Check_();
  150. #endif
  151. // Select several addresses at once.
  152. void GetAddr_(std::vector<CAddress> &vAddr);
  153. // Mark an entry as currently-connected-to.
  154. void Connected_(const CService &addr, int64 nTime);
  155. public:
  156. IMPLEMENT_SERIALIZE
  157. (({
  158. {
  159. LOCK(cs);
  160. unsigned char nVersion = 0;
  161. READWRITE(nVersion);
  162. READWRITE(nKey);
  163. READWRITE(nNew);
  164. READWRITE(nTried);
  165. CAddrMan *am = const_cast<CAddrMan*>(this);
  166. if (fWrite)
  167. {
  168. int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT;
  169. READWRITE(nUBuckets);
  170. std::map<int, int> mapUnkIds;
  171. int nIds = 0;
  172. for (std::map<int, CAddrInfo>::iterator it = am->mapInfo.begin(); it != am->mapInfo.end(); it++)
  173. {
  174. if (nIds == nNew) break; // this means nNew was wrong, oh ow
  175. mapUnkIds[(*it).first] = nIds;
  176. CAddrInfo &info = (*it).second;
  177. if (info.nRefCount)
  178. {
  179. READWRITE(info);
  180. nIds++;
  181. }
  182. }
  183. nIds = 0;
  184. for (std::map<int, CAddrInfo>::iterator it = am->mapInfo.begin(); it != am->mapInfo.end(); it++)
  185. {
  186. if (nIds == nTried) break; // this means nTried was wrong, oh ow
  187. CAddrInfo &info = (*it).second;
  188. if (info.fInTried)
  189. {
  190. READWRITE(info);
  191. nIds++;
  192. }
  193. }
  194. for (std::vector<std::set<int> >::iterator it = am->vvNew.begin(); it != am->vvNew.end(); it++)
  195. {
  196. const std::set<int> &vNew = (*it);
  197. int nSize = vNew.size();
  198. READWRITE(nSize);
  199. for (std::set<int>::iterator it2 = vNew.begin(); it2 != vNew.end(); it2++)
  200. {
  201. int nIndex = mapUnkIds[*it2];
  202. READWRITE(nIndex);
  203. }
  204. }
  205. } else {
  206. int nUBuckets = 0;
  207. READWRITE(nUBuckets);
  208. am->nIdCount = 0;
  209. am->mapInfo.clear();
  210. am->mapAddr.clear();
  211. am->vRandom.clear();
  212. am->vvTried = std::vector<std::vector<int> >(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0));
  213. am->vvNew = std::vector<std::set<int> >(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>());
  214. for (int n = 0; n < am->nNew; n++)
  215. {
  216. CAddrInfo &info = am->mapInfo[n];
  217. READWRITE(info);
  218. am->mapAddr[info] = n;
  219. info.nRandomPos = vRandom.size();
  220. am->vRandom.push_back(n);
  221. if (nUBuckets != ADDRMAN_NEW_BUCKET_COUNT)
  222. {
  223. am->vvNew[info.GetNewBucket(am->nKey)].insert(n);
  224. info.nRefCount++;
  225. }
  226. }
  227. am->nIdCount = am->nNew;
  228. int nLost = 0;
  229. for (int n = 0; n < am->nTried; n++)
  230. {
  231. CAddrInfo info;
  232. READWRITE(info);
  233. std::vector<int> &vTried = am->vvTried[info.GetTriedBucket(am->nKey)];
  234. if (vTried.size() < ADDRMAN_TRIED_BUCKET_SIZE)
  235. {
  236. info.nRandomPos = vRandom.size();
  237. info.fInTried = true;
  238. am->vRandom.push_back(am->nIdCount);
  239. am->mapInfo[am->nIdCount] = info;
  240. am->mapAddr[info] = am->nIdCount;
  241. vTried.push_back(am->nIdCount);
  242. am->nIdCount++;
  243. } else {
  244. nLost++;
  245. }
  246. }
  247. am->nTried -= nLost;
  248. for (int b = 0; b < nUBuckets; b++)
  249. {
  250. std::set<int> &vNew = am->vvNew[b];
  251. int nSize = 0;
  252. READWRITE(nSize);
  253. for (int n = 0; n < nSize; n++)
  254. {
  255. int nIndex = 0;
  256. READWRITE(nIndex);
  257. CAddrInfo &info = am->mapInfo[nIndex];
  258. if (nUBuckets == ADDRMAN_NEW_BUCKET_COUNT && info.nRefCount < ADDRMAN_NEW_BUCKETS_PER_ADDRESS)
  259. {
  260. info.nRefCount++;
  261. vNew.insert(nIndex);
  262. }
  263. }
  264. }
  265. }
  266. }
  267. });)
  268. CAddrMan() : vRandom(0), vvTried(ADDRMAN_TRIED_BUCKET_COUNT, std::vector<int>(0)), vvNew(ADDRMAN_NEW_BUCKET_COUNT, std::set<int>())
  269. {
  270. nKey.resize(32);
  271. RAND_bytes(&nKey[0], 32);
  272. nIdCount = 0;
  273. nTried = 0;
  274. nNew = 0;
  275. }
  276. // Return the number of (unique) addresses in all tables.
  277. int size()
  278. {
  279. return vRandom.size();
  280. }
  281. // Consistency check
  282. void Check()
  283. {
  284. #ifdef DEBUG_ADDRMAN
  285. {
  286. LOCK(cs);
  287. int err;
  288. if ((err=Check_()))
  289. printf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
  290. }
  291. #endif
  292. }
  293. // Add a single address.
  294. bool Add(const CAddress &addr, const CNetAddr& source, int64 nTimePenalty = 0)
  295. {
  296. bool fRet = false;
  297. {
  298. LOCK(cs);
  299. Check();
  300. fRet |= Add_(addr, source, nTimePenalty);
  301. Check();
  302. }
  303. if (fRet)
  304. printf("Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort().c_str(), source.ToString().c_str(), nTried, nNew);
  305. return fRet;
  306. }
  307. // Add multiple addresses.
  308. bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64 nTimePenalty = 0)
  309. {
  310. int nAdd = 0;
  311. {
  312. LOCK(cs);
  313. Check();
  314. for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
  315. nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
  316. Check();
  317. }
  318. if (nAdd)
  319. printf("Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString().c_str(), nTried, nNew);
  320. return nAdd > 0;
  321. }
  322. // Mark an entry as accessible.
  323. void Good(const CService &addr, int64 nTime = GetAdjustedTime())
  324. {
  325. {
  326. LOCK(cs);
  327. Check();
  328. Good_(addr, nTime);
  329. Check();
  330. }
  331. }
  332. // Mark an entry as connection attempted to.
  333. void Attempt(const CService &addr, int64 nTime = GetAdjustedTime())
  334. {
  335. {
  336. LOCK(cs);
  337. Check();
  338. Attempt_(addr, nTime);
  339. Check();
  340. }
  341. }
  342. // Choose an address to connect to.
  343. // nUnkBias determines how much "new" entries are favored over "tried" ones (0-100).
  344. CAddress Select(int nUnkBias = 50)
  345. {
  346. CAddress addrRet;
  347. {
  348. LOCK(cs);
  349. Check();
  350. addrRet = Select_(nUnkBias);
  351. Check();
  352. }
  353. return addrRet;
  354. }
  355. // Return a bunch of addresses, selected at random.
  356. std::vector<CAddress> GetAddr()
  357. {
  358. Check();
  359. std::vector<CAddress> vAddr;
  360. {
  361. LOCK(cs);
  362. GetAddr_(vAddr);
  363. }
  364. Check();
  365. return vAddr;
  366. }
  367. // Mark an entry as currently-connected-to.
  368. void Connected(const CService &addr, int64 nTime = GetAdjustedTime())
  369. {
  370. {
  371. LOCK(cs);
  372. Check();
  373. Connected_(addr, nTime);
  374. Check();
  375. }
  376. }
  377. };
  378. #endif