db done
This commit is contained in:
parent
7ebdde3715
commit
5e1a514a59
4 changed files with 212 additions and 23 deletions
133
db.cpp
Normal file
133
db.cpp
Normal file
|
@ -0,0 +1,133 @@
|
|||
#include "db.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
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<int>::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<CIP>& 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<int> 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<int>::const_iterator it = ids.begin(); it != ids.end(); it++) {
|
||||
CIPPort &ip = idToInfo[*it].ip;
|
||||
if (ip.IsValid() && (!fOnlyIPv4 || ip.IsIPv4()))
|
||||
ips.insert(ip);
|
||||
}
|
||||
}
|
100
db.h
100
db.h
|
@ -1,6 +1,6 @@
|
|||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <pair>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
@ -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<int, CAddrInfo> idToInfo; // map address id to address info (b,c,d)
|
||||
map<CIP, int> ipToId; // map ip to id (b,c,d)
|
||||
deque<int> ourId; // sequence of tried nodes, in order we have tried connecting to them (c,d)
|
||||
set<int> unkId; // set of nodes not yet tried (b)
|
||||
set<int> goodId; // set of good nodes (d)
|
||||
map<CIP, pair<uint32_t, uint32_t> > banned; // nodes that are banned, with their from/to ban time (a)
|
||||
std::map<int, CAddrInfo> idToInfo; // map address id to address info (b,c,d)
|
||||
std::map<CIPPort, int> ipToId; // map ip to id (b,c,d)
|
||||
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)
|
||||
std::set<int> goodId; // set of good nodes (d)
|
||||
std::map<CIPPort, time_t> banned; // nodes that are banned, with their unban time (a)
|
||||
|
||||
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<CIP>& ips, int max, bool fOnlyIPv4);
|
||||
|
||||
public:
|
||||
void Add(const CAddress &addr);
|
||||
void Add(const vector<CAddress> &vAddr);
|
||||
void Good(const CIP &addr);
|
||||
void Bad(const CIP &addr, int fail);
|
||||
CIP Get();
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
int GetIPv4Address(struct in_addr *addr, int max);
|
||||
}
|
||||
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]);
|
||||
}
|
||||
void Good(const CIPPort &addr) {
|
||||
CRITICAL_BLOCK(cs)
|
||||
Good_(addr);
|
||||
}
|
||||
void Skipped(const CIPPort &addr) {
|
||||
CRITICAL_BLOCK(cs)
|
||||
Skipped_(addr);
|
||||
}
|
||||
void Bad(const CIPPort &addr, int ban = 0) {
|
||||
CRITICAL_BLOCK(cs)
|
||||
Bad_(addr, ban);
|
||||
}
|
||||
bool Get(CIPPort &ip) {
|
||||
CRITICAL_BLOCK(cs)
|
||||
return Get_(ip);
|
||||
}
|
||||
void GetIPs(std::set<CIP>& ips, int max, bool fOnlyIPv4 = true) {
|
||||
CRITICAL_BLOCK(cs)
|
||||
GetIPs_(ips, max, fOnlyIPv4);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -94,7 +94,6 @@ void CAddress::Init()
|
|||
{
|
||||
nServices = NODE_NETWORK;
|
||||
nTime = 100000000;
|
||||
nLastTry = 0;
|
||||
}
|
||||
|
||||
void CAddress::print() const
|
||||
|
|
1
util.h
1
util.h
|
@ -2,6 +2,7 @@
|
|||
#define _UTIL_H_ 1
|
||||
|
||||
#include <pthread.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define loop for (;;)
|
||||
#define BEGIN(a) ((char*)&(a))
|
||||
|
|
Loading…
Reference in a new issue