From 5e1a514a593c8e86e8c6f5e730c3613b501a385e Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Mon, 19 Dec 2011 23:18:06 +0100 Subject: [PATCH] db done --- db.cpp | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++ db.h | 100 +++++++++++++++++++++++++++++--------- protocol.cpp | 1 - util.h | 1 + 4 files changed, 212 insertions(+), 23 deletions(-) create mode 100644 db.cpp diff --git a/db.cpp b/db.cpp new file mode 100644 index 0000000..d390633 --- /dev/null +++ b/db.cpp @@ -0,0 +1,133 @@ +#include "db.h" +#include + +using namespace std; + +bool CAddrDb::Get_(CIPPort &ip) { + int tot = unkId.size() + ourId.size(); + if (tot == 0) return false; + int rnd = rand() % tot; + if (tot < unkId.size()) { + set::iterator it = unkId.begin(); + return *it; + } else { + int ret = ourId.front(); + ourId.pop_front(); + return ret; + } +} + +int CAddrDb::Lookup_(const CIPPort &ip) { + if (ipToId.count(ip)) + return ipToId[ip]; + return -1; +} + +void CAddrDb::Good_(const CIPPort &addr) { + int id = Lookup_(addr); + if (id == -1) return; + unkId.erase(id); + banned.erase(addr); + CAddrInfo &info = idToInfo[id]; + info.Update(true); + if (info.IsGood()) + goodId.insert(id); + ourId.push_back(id); +} + +void CAddrDb::Bad_(const CIPPort &addr, int ban) +{ + int id = Lookup_(addr); + if (id == -1) return; + unkId.erase(id); + CAddrInfo &info = idToInfo[id]; + info.Update(false); + if (info.IsTerrible()) + if (ban < 604800) ban = 604800; + if (ban) { + banned[info.ip] = ban; + ipToId.erase(info.ip); + goodId.erase(id); + idToInfo.erase(id); + } else { + if (!info.IsGood()) + goodId.erase(id); + ourId.push_back(id); + } +} + +void CAddrDb::Skipped_(const CIPPort &addr) +{ + int id = Lookup_(addr); + if (id == -1) return; + unkId.erase(id); + ourId.push_back(id); +} + + +void CAddrDb::Add_(const CAddress &addr) { + if (!addr.IsRoutable()) + return; + CIPPort ipp(addr); + if (banned.count(ipp)) { + time_t bantime = banned[ipp]; + if (bantime < time(NULL)) + banned.erase(ipp); + else + return; + } + if (ipToId.count(ipp)) { + CAddrInfo &ai = idToInfo[ipToId[ipp]]; + if (addr.nTime > ai.lastTry) + ai.lastTry = addr.nTime; + ai.services |= addr.nServices; + return; + } + CAddrInfo ai; + ai.ip = ipp; + ai.services = addr.nServices; + ai.lastTry = addr.nTime; + ai.ourLastTry = 0; + ai.reliability = 0; + ai.weight = 0; + ai.total = 0; + ai.success = 0; + int id = nId++; + idToInfo[id] = ai; + ipToId[ipp] = id; + unkId.insert(id); +} + +void CAddrDb::GetIPs_(set& ips, int max, bool fOnlyIPv4) { + if (goodId.size() == 0) { + int id = -1; + if (ourId.size() == 0) { + if (unkId.size() == 0) return; + id = *unkId.begin(); + } else { + id = *ourId.begin(); + } + if (id >= 0) { + ips.insert(idToInfo[id].ip); + } + return; + } + if (max > goodId.size() / 2) + max = goodId.size() / 2; + if (max < 1) + max = 1; + int low = *goodId.begin(); + int high = *goodId.rbegin(); + set ids; + while (ids.size() < max) { + int range = high-low+1; + int pos = low + (rand() % range); + int id = *(goodId.lower_bound(pos)); + ids.insert(id); + } + for (set::const_iterator it = ids.begin(); it != ids.end(); it++) { + CIPPort &ip = idToInfo[*it].ip; + if (ip.IsValid() && (!fOnlyIPv4 || ip.IsIPv4())) + ips.insert(ip); + } +} diff --git a/db.h b/db.h index ea84171..65c0894 100644 --- a/db.h +++ b/db.h @@ -1,6 +1,6 @@ #include +#include -#include #include #include #include @@ -10,15 +10,42 @@ #include "protocol.h" #include "util.h" +#define TAU 86400.0 + class CAddrInfo { private: - CIP ip; + CIPPort ip; uint64_t services; - uint32_t lastTry; - uint32_t ourLastTry; - double reliabity; + time_t lastTry; + time_t ourLastTry; + double reliability; + double timing; double weight; -} + double count; + int total; + int success; +public: + bool IsGood() { + return (weight > 0.5 & reliability/weight > 0.8 && timing/weight < 86400 && count/weight > 1.0) && ip.GetPort() == 8333; + } + bool IsTerrible() { + return (weight > 0.5 & reliability/weight < 0.2 && timing/weight < 86400 && count/weight > 2.0); + } + void Update(bool good) { + uint32_t now = time(NULL); + double f = exp(-(now-ourLastTry)/TAU); + reliability = reliability * f + (good ? (1.0-f) : 0); + timing = (timing + (now-ourLastTry) * weight) * f; + count = count * f + 1; + weight = weight * f + (1.0-f); + lastTry = now; + ourLastTry = now; + total++; + if (good) success++; + } + + friend class CAddrDb; +}; // seen nodes // / \ @@ -32,21 +59,50 @@ class CAddrDb { private: CCriticalSection cs; int nId; // number of address id's - map idToInfo; // map address id to address info (b,c,d) - map ipToId; // map ip to id (b,c,d) - deque ourId; // sequence of tried nodes, in order we have tried connecting to them (c,d) - set unkId; // set of nodes not yet tried (b) - set goodId; // set of good nodes (d) - map > banned; // nodes that are banned, with their from/to ban time (a) + std::map idToInfo; // map address id to address info (b,c,d) + std::map ipToId; // map ip to id (b,c,d) + std::deque ourId; // sequence of tried nodes, in order we have tried connecting to them (c,d) + std::set unkId; // set of nodes not yet tried (b) + std::set goodId; // set of good nodes (d) + std::map banned; // nodes that are banned, with their unban time (a) -public: - void Add(const CAddress &addr); - void Add(const vector &vAddr); - void Good(const CIP &addr); - void Bad(const CIP &addr, int fail); - CIP Get(); -} +protected: + void Add_(const CAddress &addr); + void Good_(const CIPPort &ip); + void Bad_(const CIPPort &ip, int ban); + void Skipped_(const CIPPort &ip); + bool Get_(CIPPort &ip); + int Lookup_(const CIPPort &ip); + void GetIPs_(std::set& ips, int max, bool fOnlyIPv4); -extern "C" { - int GetIPv4Address(struct in_addr *addr, int max); -} +public: + void Add(const CAddress &addr) { + CRITICAL_BLOCK(cs) + Add_(addr); + } + void Add(const std::vector &vAddr) { + CRITICAL_BLOCK(cs) + for (int i=0; i& ips, int max, bool fOnlyIPv4 = true) { + CRITICAL_BLOCK(cs) + GetIPs_(ips, max, fOnlyIPv4); + } +}; diff --git a/protocol.cpp b/protocol.cpp index 735df2a..dc3761e 100644 --- a/protocol.cpp +++ b/protocol.cpp @@ -94,7 +94,6 @@ void CAddress::Init() { nServices = NODE_NETWORK; nTime = 100000000; - nLastTry = 0; } void CAddress::print() const diff --git a/util.h b/util.h index 6da6331..633b85d 100644 --- a/util.h +++ b/util.h @@ -2,6 +2,7 @@ #define _UTIL_H_ 1 #include +#include #define loop for (;;) #define BEGIN(a) ((char*)&(a))