Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/db.h
| #include <math.h> | #ifndef BITCOIN_SEEDER_DB_H | ||||
| #include <stdint.h> | #define BITCOIN_SEEDER_DB_H | ||||
| #include "netbase.h" | |||||
| #include "protocol.h" | |||||
| #include "util.h" | |||||
| #include <cmath> | |||||
| #include <cstdint> | |||||
| #include <deque> | #include <deque> | ||||
| #include <map> | #include <map> | ||||
| #include <set> | #include <set> | ||||
| #include <vector> | #include <vector> | ||||
| #include "netbase.h" | |||||
| #include "protocol.h" | |||||
| #include "util.h" | |||||
| #define MIN_RETRY 1000 | #define MIN_RETRY 1000 | ||||
| #define REQUIRE_VERSION 70001 | #define REQUIRE_VERSION 70001 | ||||
| static inline int GetRequireHeight(const bool testnet = fTestNet) { | static inline int GetRequireHeight(const bool testnet = fTestNet) { | ||||
| return testnet ? 500000 : 350000; | return testnet ? 500000 : 350000; | ||||
| } | } | ||||
| std::string static inline ToString(const CService &ip) { | static inline std::string ToString(const CService &ip) { | ||||
| std::string str = ip.ToString(); | std::string str = ip.ToString(); | ||||
| while (str.size() < 22) | while (str.size() < 22) | ||||
| str += ' '; | str += ' '; | ||||
| return str; | return str; | ||||
| } | } | ||||
| class CAddrStat { | class CAddrStat { | ||||
| private: | private: | ||||
| ▲ Show 20 Lines • Show All 127 Lines • ▼ Show 20 Lines | int GetIgnoreTime() const { | ||||
| } | } | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| void Update(bool good); | void Update(bool good); | ||||
| friend class CAddrDb; | friend class CAddrDb; | ||||
| IMPLEMENT_SERIALIZE(unsigned char version = 4; READWRITE(version); | IMPLEMENT_SERIALIZE(uint8_t version = 4; READWRITE(version); READWRITE(ip); | ||||
| READWRITE(ip); READWRITE(services); READWRITE(lastTry); | READWRITE(services); READWRITE(lastTry); | ||||
| unsigned char tried = ourLastTry != 0; READWRITE(tried); | uint8_t tried = ourLastTry != 0; READWRITE(tried); | ||||
| if (tried) { | if (tried) { | ||||
| READWRITE(ourLastTry); | READWRITE(ourLastTry); | ||||
| READWRITE(ignoreTill); | READWRITE(ignoreTill); | ||||
| READWRITE(stat2H); | READWRITE(stat2H); | ||||
| READWRITE(stat8H); | READWRITE(stat8H); | ||||
| READWRITE(stat1D); | READWRITE(stat1D); | ||||
| READWRITE(stat1W); | READWRITE(stat1W); | ||||
| if (version >= 1) | if (version >= 1) | ||||
| Show All 35 Lines | |||||
| // / | \ | // / | \ | ||||
| // tracked nodes (b) unknown nodes (e) active nodes | // tracked nodes (b) unknown nodes (e) active nodes | ||||
| // / \ | // / \ | ||||
| // (d) good nodes (c) non-good nodes | // (d) good nodes (c) non-good nodes | ||||
| class CAddrDb { | class CAddrDb { | ||||
| private: | private: | ||||
| mutable CCriticalSection cs; | mutable CCriticalSection cs; | ||||
| int nId; // number of address id's | // number of address id's | ||||
| std::map<int, CAddrInfo> | int nId; | ||||
| idToInfo; // map address id to address info (b,c,d,e) | // map address id to address info (b,c,d,e) | ||||
| std::map<CService, int> ipToId; // map ip to id (b,c,d,e) | std::map<int, CAddrInfo> idToInfo; | ||||
| std::deque<int> ourId; // sequence of tried nodes, in order we have tried | // map ip to id (b,c,d,e) | ||||
| // connecting to them (c,d) | std::map<CService, int> ipToId; | ||||
| std::set<int> unkId; // set of nodes not yet tried (b) | // sequence of tried nodes, in order we have tried connecting to them (c,d) | ||||
| std::set<int> goodId; // set of good nodes (d, good e) | std::deque<int> ourId; | ||||
| // set of nodes not yet tried (b) | |||||
| std::set<int> unkId; | |||||
| // set of good nodes (d, good e) | |||||
| std::set<int> goodId; | |||||
| int nDirty; | int nDirty; | ||||
| protected: | protected: | ||||
| // internal routines that assume proper locks are acquired | // internal routines that assume proper locks are acquired | ||||
| void Add_(const CAddress &addr, bool force); // add an address | // add an address | ||||
| bool Get_(CServiceResult &ip, int &wait); // get an IP to test (must call | void Add_(const CAddress &addr, bool force); | ||||
| // Good_, Bad_, or Skipped_ on | // get an IP to test (must call Good_, Bad_, or Skipped_ on result | ||||
| // result afterwards) | // afterwards) | ||||
| bool Get_(CServiceResult &ip, int &wait); | |||||
| bool GetMany_(std::vector<CServiceResult> &ips, int max, int &wait); | bool GetMany_(std::vector<CServiceResult> &ips, int max, int &wait); | ||||
| void | // mark an IP as good (must have been returned by Get_) | ||||
| Good_(const CService &ip, int clientV, std::string clientSV, | void Good_(const CService &ip, int clientV, std::string clientSV, | ||||
| int blocks); // mark an IP as good (must have been returned by Get_) | int blocks); | ||||
| void Bad_(const CService &ip, int ban); // mark an IP as bad (and optionally | // mark an IP as bad (and optionally ban it) (must have been returned by | ||||
| // ban it) (must have been returned | // Get_) | ||||
| // by Get_) | void Bad_(const CService &ip, int ban); | ||||
| void Skipped_(const CService &ip); // mark an IP as skipped (must have been | // mark an IP as skipped (must have been returned by Get_) | ||||
| // returned by Get_) | void Skipped_(const CService &ip); | ||||
| int Lookup_(const CService &ip); // look up id of an IP | // look up id of an IP | ||||
| void | int Lookup_(const CService &ip); | ||||
| GetIPs_(std::set<CNetAddr> &ips, uint64_t requestedFlags, int max, | // get a random set of IPs (shared lock only) | ||||
| const bool *nets); // get a random set of IPs (shared lock only) | void GetIPs_(std::set<CNetAddr> &ips, uint64_t requestedFlags, int max, | ||||
| const bool *nets); | |||||
| public: | public: | ||||
| std::map<CService, time_t> | // nodes that are banned, with their unban time (a) | ||||
| banned; // nodes that are banned, with their unban time (a) | std::map<CService, time_t> banned; | ||||
| void GetStats(CAddrDbStats &stats) { | void GetStats(CAddrDbStats &stats) { | ||||
| SHARED_CRITICAL_BLOCK(cs) { | SHARED_CRITICAL_BLOCK(cs) { | ||||
| stats.nBanned = banned.size(); | stats.nBanned = banned.size(); | ||||
| stats.nAvail = idToInfo.size(); | stats.nAvail = idToInfo.size(); | ||||
| stats.nTracked = ourId.size(); | stats.nTracked = ourId.size(); | ||||
| stats.nGood = goodId.size(); | stats.nGood = goodId.size(); | ||||
| stats.nNew = unkId.size(); | stats.nNew = unkId.size(); | ||||
| stats.nAge = time(NULL) - idToInfo[ourId[0]].ourLastTry; | stats.nAge = time(nullptr) - idToInfo[ourId[0]].ourLastTry; | ||||
| } | } | ||||
| } | } | ||||
| void ResetIgnores() { | void ResetIgnores() { | ||||
| for (std::map<int, CAddrInfo>::iterator it = idToInfo.begin(); | for (std::map<int, CAddrInfo>::iterator it = idToInfo.begin(); | ||||
| it != idToInfo.end(); it++) { | it != idToInfo.end(); it++) { | ||||
| (*it).second.ignoreTill = 0; | (*it).second.ignoreTill = 0; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 121 Lines • ▼ Show 20 Lines | void ResultMany(const std::vector<CServiceResult> &ips) { | ||||
| } | } | ||||
| } | } | ||||
| void GetIPs(std::set<CNetAddr> &ips, uint64_t requestedFlags, int max, | void GetIPs(std::set<CNetAddr> &ips, uint64_t requestedFlags, int max, | ||||
| const bool *nets) { | const bool *nets) { | ||||
| SHARED_CRITICAL_BLOCK(cs) | SHARED_CRITICAL_BLOCK(cs) | ||||
| GetIPs_(ips, requestedFlags, max, nets); | GetIPs_(ips, requestedFlags, max, nets); | ||||
| } | } | ||||
| }; | }; | ||||
| #endif | |||||