seeder/db.h

242 lines
7.3 KiB
C
Raw Normal View History

2011-12-16 18:37:05 +01:00
#include <stdint.h>
2011-12-19 23:18:06 +01:00
#include <math.h>
2011-12-16 18:37:05 +01:00
#include <set>
#include <map>
#include <vector>
#include <deque>
#include "netbase.h"
#include "protocol.h"
#include "util.h"
2011-12-20 05:20:50 +01:00
#define MIN_RETRY 1000
2011-12-19 23:18:06 +01:00
2011-12-20 14:29:21 +01:00
std::string static inline ToString(const CIPPort &ip) {
std::string str = ip.ToString();
while (str.size() < 22) str += ' ';
return str;
}
2011-12-23 02:43:32 +01:00
class CAddrStat {
2011-12-22 03:25:35 +01:00
private:
float weight;
2011-12-23 02:43:32 +01:00
float count;
float reliability;
2011-12-22 03:25:35 +01:00
public:
2011-12-23 02:43:32 +01:00
CAddrStat() : weight(0), count(0), reliability(0) {}
void Update(bool good, int64 age, double tau) {
double f = exp(-age/tau);
reliability = reliability * f + (good ? (1.0-f) : 0);
count = count * f + 1;
weight = weight * f + (1.0-f);
2011-12-22 03:25:35 +01:00
}
2011-12-23 02:43:32 +01:00
IMPLEMENT_SERIALIZE (
READWRITE(weight);
READWRITE(count);
READWRITE(reliability);
)
friend class CAddrInfo;
};
2011-12-22 03:25:35 +01:00
2011-12-16 18:37:05 +01:00
class CAddrInfo {
private:
2011-12-19 23:18:06 +01:00
CIPPort ip;
2011-12-16 18:37:05 +01:00
uint64_t services;
2011-12-20 08:35:22 +01:00
int64 lastTry;
int64 ourLastTry;
2011-12-23 02:43:32 +01:00
int64 ignoreTill;
CAddrStat stat2H;
CAddrStat stat8H;
CAddrStat stat1D;
CAddrStat stat1W;
int clientVersion;
2011-12-19 23:18:06 +01:00
int total;
int success;
public:
2011-12-23 02:43:32 +01:00
CAddrInfo() : services(0), lastTry(0), ourLastTry(0), ignoreTill(0), clientVersion(0), total(0), success(0) {}
2011-12-19 23:18:06 +01:00
bool IsGood() {
2011-12-23 02:43:32 +01:00
if (ip.GetPort() != 8333) return false;
if (!(services & NODE_NETWORK)) return false;
if (!ip.IsRoutable()) return false;
if (!ip.IsIPv4()) return false;
if (clientVersion && clientVersion < 32400) return false;
if (total <= 3 && success * 2 >= total) return true;
if (stat2H.reliability > 0.7 && stat2H.count > 3) return true;
if (stat8H.reliability > 0.6 && stat8H.count > 6) return true;
if (stat1D.reliability > 0.5 && stat1D.count > 12) return true;
if (stat1W.reliability > 0.4 && stat1W.count > 24) return true;
return false;
}
int GetBanTime() {
if (IsGood()) return 0;
if (clientVersion && clientVersion < 31900) { return 1000000; }
if (stat1D.reliability < 0.01 && stat1D.count > 5) { return 500000; }
if (stat1W.reliability - stat1W.weight + 1.0 < 0.10 && stat1W.count > 4) { return 240*3600; }
return 0;
2011-12-19 23:18:06 +01:00
}
2011-12-23 02:43:32 +01:00
int GetIgnoreTime() {
if (IsGood()) return 0;
if (stat2H.reliability - stat2H.weight + 1.0 < 0.2 && stat2H.count > 3) { return 3*3600; }
if (stat8H.reliability - stat8H.weight + 1.0 < 0.2 && stat8H.count > 6) { return 12*3600; }
if (stat1D.reliability - stat1D.weight + 1.0 < 0.2 && stat1D.count > 9) { return 36*3600; }
return 0;
2011-12-19 23:18:06 +01:00
}
2011-12-23 02:43:32 +01:00
2011-12-19 23:45:18 +01:00
void Update(bool good);
2011-12-19 23:18:06 +01:00
friend class CAddrDb;
2011-12-20 08:35:22 +01:00
IMPLEMENT_SERIALIZE (
2011-12-23 02:43:32 +01:00
unsigned char version = 0;
2011-12-20 08:35:22 +01:00
READWRITE(version);
READWRITE(ip);
READWRITE(services);
READWRITE(lastTry);
READWRITE(ourLastTry);
2011-12-23 02:43:32 +01:00
READWRITE(ignoreTill);
READWRITE(stat2H);
READWRITE(stat8H);
READWRITE(stat1D);
READWRITE(stat1W);
2011-12-20 08:35:22 +01:00
READWRITE(total);
READWRITE(success);
2011-12-23 02:43:32 +01:00
READWRITE(clientVersion);
2011-12-20 08:35:22 +01:00
)
2011-12-19 23:18:06 +01:00
};
2011-12-16 18:37:05 +01:00
2011-12-16 18:51:45 +01:00
// seen nodes
// / \
2011-12-20 14:29:21 +01:00
// (a) banned nodes available nodes--------------
// / | \
// tracked nodes (b) unknown nodes (e) active nodes
2011-12-16 18:51:45 +01:00
// / \
// (d) good nodes (c) non-good nodes
2011-12-16 18:37:05 +01:00
class CAddrDb {
private:
2011-12-20 08:35:22 +01:00
mutable CCriticalSection cs;
2011-12-16 18:37:05 +01:00
int nId; // number of address id's
2011-12-20 14:29:21 +01:00
std::map<int, CAddrInfo> idToInfo; // map address id to address info (b,c,d,e)
std::map<CIPPort, int> ipToId; // map ip to id (b,c,d,e)
2011-12-19 23:18:06 +01:00
std::deque<int> ourId; // sequence of tried nodes, in order we have tried connecting to them (c,d)
std::set<int> unkId; // set of nodes not yet tried (b)
2011-12-20 14:29:21 +01:00
std::set<int> goodId; // set of good nodes (d, good e)
2011-12-19 23:18:06 +01:00
std::map<CIPPort, time_t> banned; // nodes that are banned, with their unban time (a)
2011-12-23 02:43:32 +01:00
int nDirty;
2011-12-16 18:37:05 +01:00
2011-12-19 23:18:06 +01:00
protected:
2011-12-20 14:29:21 +01:00
// internal routines that assume proper locks are acquired
void Add_(const CAddress &addr); // add an address
bool Get_(CIPPort &ip, int& wait); // get an IP to test (must call Good_, Bad_, or Skipped_ on result afterwards)
2011-12-23 02:43:32 +01:00
void Good_(const CIPPort &ip, int clientV); // mark an IP as good (must have been returned by Get_)
2011-12-20 14:29:21 +01:00
void Bad_(const CIPPort &ip, int ban); // mark an IP as bad (and optionally ban it) (must have been returned by Get_)
void Skipped_(const CIPPort &ip); // mark an IP as skipped (must have been returned by Get_)
int Lookup_(const CIPPort &ip); // look up id of an IP
void GetIPs_(std::set<CIP>& ips, int max, bool fOnlyIPv4); // get a random set of IPs (shared lock only)
2011-12-16 18:37:05 +01:00
2011-12-19 23:18:06 +01:00
public:
2011-12-20 08:35:22 +01:00
2011-12-23 02:43:32 +01:00
// serialization code
2011-12-20 14:29:21 +01:00
// format:
// nVersion (0 for now)
2011-12-23 02:43:32 +01:00
// n (number of ips in (b,c,d))
// CAddrInfo[n]
2011-12-20 14:29:21 +01:00
// banned
// acquires a shared lock (this does not suffice for read mode, but we assume that only happens at startup, single-threaded)
// this way, dumping does not interfere with GetIPs_, which is called from the DNS thread
2011-12-20 08:35:22 +01:00
IMPLEMENT_SERIALIZE (({
int nVersion = 0;
READWRITE(nVersion);
2011-12-20 09:31:28 +01:00
SHARED_CRITICAL_BLOCK(cs) {
2011-12-20 08:35:22 +01:00
if (fWrite) {
CAddrDb *db = const_cast<CAddrDb*>(this);
2011-12-23 02:43:32 +01:00
int n = ourId.size() + unkId.size();
READWRITE(n);
2011-12-20 08:35:22 +01:00
for (std::deque<int>::const_iterator it = ourId.begin(); it != ourId.end(); it++) {
std::map<int, CAddrInfo>::iterator ci = db->idToInfo.find(*it);
READWRITE((*ci).second);
}
for (std::set<int>::const_iterator it = unkId.begin(); it != unkId.end(); it++) {
std::map<int, CAddrInfo>::iterator ci = db->idToInfo.find(*it);
READWRITE((*ci).second);
}
} else {
CAddrDb *db = const_cast<CAddrDb*>(this);
db->nId = 0;
2011-12-23 02:43:32 +01:00
int n;
READWRITE(n);
for (int i=0; i<n; i++) {
2011-12-20 08:35:22 +01:00
CAddrInfo info;
READWRITE(info);
2011-12-23 02:43:32 +01:00
if (!info.GetBanTime()) {
2011-12-20 14:29:21 +01:00
int id = db->nId++;
db->idToInfo[id] = info;
db->ipToId[info.ip] = id;
2011-12-23 02:43:32 +01:00
if (info.ourLastTry) {
db->ourId.push_back(id);
if (info.IsGood()) db->goodId.insert(id);
} else {
db->unkId.insert(id);
}
2011-12-20 14:29:21 +01:00
}
2011-12-20 08:35:22 +01:00
}
2011-12-23 02:43:32 +01:00
db->nDirty++;
2011-12-20 08:35:22 +01:00
}
READWRITE(banned);
}
});)
2011-12-20 14:29:21 +01:00
// print statistics
2011-12-20 05:20:50 +01:00
void Stats() {
2011-12-20 14:29:21 +01:00
SHARED_CRITICAL_BLOCK(cs) {
2011-12-23 02:43:32 +01:00
if (nDirty > 50) {
2011-12-20 14:29:21 +01:00
printf("**** %i available (%i tracked, %i new, %i active), %i banned; %i good\n",
(int)idToInfo.size(),
(int)ourId.size(),
(int)unkId.size(),
(int)idToInfo.size() - (int)ourId.size() - (int)unkId.size(),
(int)banned.size(),
(int)goodId.size());
2011-12-23 02:43:32 +01:00
nDirty = 0; // hopefully atomic
2011-12-20 14:29:21 +01:00
}
}
2011-12-20 05:20:50 +01:00
}
2011-12-19 23:18:06 +01:00
void Add(const CAddress &addr) {
CRITICAL_BLOCK(cs)
Add_(addr);
}
void Add(const std::vector<CAddress> &vAddr) {
CRITICAL_BLOCK(cs)
for (int i=0; i<vAddr.size(); i++)
Add_(vAddr[i]);
}
2011-12-23 02:43:32 +01:00
void Good(const CIPPort &addr, int clientVersion) {
2011-12-19 23:18:06 +01:00
CRITICAL_BLOCK(cs)
2011-12-23 02:43:32 +01:00
Good_(addr, clientVersion);
2011-12-19 23:18:06 +01:00
}
void Skipped(const CIPPort &addr) {
CRITICAL_BLOCK(cs)
Skipped_(addr);
}
void Bad(const CIPPort &addr, int ban = 0) {
CRITICAL_BLOCK(cs)
Bad_(addr, ban);
}
2011-12-20 06:42:48 +01:00
bool Get(CIPPort &ip, int& wait) {
2011-12-19 23:18:06 +01:00
CRITICAL_BLOCK(cs)
2011-12-20 06:42:48 +01:00
return Get_(ip, wait);
2011-12-19 23:18:06 +01:00
}
void GetIPs(std::set<CIP>& ips, int max, bool fOnlyIPv4 = true) {
2011-12-20 09:31:28 +01:00
SHARED_CRITICAL_BLOCK(cs)
2011-12-19 23:18:06 +01:00
GetIPs_(ips, max, fOnlyIPv4);
}
};