2011-12-19 23:18:06 +01:00
|
|
|
#include "db.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2011-12-19 23:45:18 +01:00
|
|
|
void CAddrInfo::Update(bool good) {
|
2011-12-23 02:43:32 +01:00
|
|
|
uint32_t now = time(NULL);
|
|
|
|
if (ourLastTry == 0)
|
|
|
|
ourLastTry = now - MIN_RETRY;
|
|
|
|
int age = now - ourLastTry;
|
|
|
|
lastTry = now;
|
|
|
|
ourLastTry = now;
|
|
|
|
total++;
|
2013-04-15 05:15:53 +02:00
|
|
|
if (good)
|
|
|
|
{
|
|
|
|
success++;
|
|
|
|
ourLastSuccess = now;
|
|
|
|
}
|
2011-12-23 02:43:32 +01:00
|
|
|
stat2H.Update(good, age, 3600*2);
|
|
|
|
stat8H.Update(good, age, 3600*8);
|
|
|
|
stat1D.Update(good, age, 3600*24);
|
|
|
|
stat1W.Update(good, age, 3600*24*7);
|
2012-02-23 18:01:09 +01:00
|
|
|
stat1M.Update(good, age, 3600*24*30);
|
2011-12-23 02:43:32 +01:00
|
|
|
int ign = GetIgnoreTime();
|
|
|
|
if (ign && (ignoreTill==0 || ignoreTill < ign+now)) ignoreTill = ign+now;
|
2011-12-26 15:53:22 +01:00
|
|
|
// printf("%s: got %s result: success=%i/%i; 2H:%.2f%%-%.2f%%(%.2f) 8H:%.2f%%-%.2f%%(%.2f) 1D:%.2f%%-%.2f%%(%.2f) 1W:%.2f%%-%.2f%%(%.2f) \n", ToString(ip).c_str(), good ? "good" : "bad", success, total,
|
|
|
|
// 100.0 * stat2H.reliability, 100.0 * (stat2H.reliability + 1.0 - stat2H.weight), stat2H.count,
|
|
|
|
// 100.0 * stat8H.reliability, 100.0 * (stat8H.reliability + 1.0 - stat8H.weight), stat8H.count,
|
|
|
|
// 100.0 * stat1D.reliability, 100.0 * (stat1D.reliability + 1.0 - stat1D.weight), stat1D.count,
|
|
|
|
// 100.0 * stat1W.reliability, 100.0 * (stat1W.reliability + 1.0 - stat1W.weight), stat1W.count);
|
2011-12-19 23:45:18 +01:00
|
|
|
}
|
|
|
|
|
2013-04-25 01:55:26 +02:00
|
|
|
bool CAddrDb::Get_(CServiceResult &ip, int &wait) {
|
2011-12-20 06:42:48 +01:00
|
|
|
int64 now = time(NULL);
|
2011-12-23 02:43:32 +01:00
|
|
|
int cont = 0;
|
2013-04-13 22:46:23 +02:00
|
|
|
int tot = unkId.size() + ourId.size();
|
|
|
|
if (tot == 0) {
|
|
|
|
wait = 5;
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-23 02:43:32 +01:00
|
|
|
do {
|
|
|
|
int rnd = rand() % tot;
|
|
|
|
int ret;
|
|
|
|
if (rnd < unkId.size()) {
|
2013-04-13 21:45:36 +02:00
|
|
|
set<int>::iterator it = unkId.end(); it--;
|
|
|
|
ret = *it;
|
|
|
|
unkId.erase(it);
|
2011-12-20 05:20:50 +01:00
|
|
|
} else {
|
2011-12-23 13:43:28 +01:00
|
|
|
ret = ourId.front();
|
2011-12-23 02:43:32 +01:00
|
|
|
if (time(NULL) - idToInfo[ret].ourLastTry < MIN_RETRY) return false;
|
|
|
|
ourId.pop_front();
|
2011-12-20 14:29:21 +01:00
|
|
|
}
|
2011-12-23 02:43:32 +01:00
|
|
|
if (idToInfo[ret].ignoreTill && idToInfo[ret].ignoreTill < now) {
|
|
|
|
ourId.push_back(ret);
|
2011-12-26 18:27:02 +01:00
|
|
|
idToInfo[ret].ourLastTry = now;
|
2011-12-23 02:43:32 +01:00
|
|
|
} else {
|
2013-04-25 01:55:26 +02:00
|
|
|
ip.service = idToInfo[ret].ip;
|
|
|
|
ip.ourLastSuccess = idToInfo[ret].ourLastSuccess;
|
2011-12-23 02:43:32 +01:00
|
|
|
break;
|
2011-12-20 05:20:50 +01:00
|
|
|
}
|
2011-12-23 02:43:32 +01:00
|
|
|
} while(1);
|
|
|
|
nDirty++;
|
2011-12-20 05:20:50 +01:00
|
|
|
return true;
|
2011-12-19 23:18:06 +01:00
|
|
|
}
|
|
|
|
|
2012-05-25 15:41:27 +02:00
|
|
|
int CAddrDb::Lookup_(const CService &ip) {
|
2011-12-19 23:18:06 +01:00
|
|
|
if (ipToId.count(ip))
|
|
|
|
return ipToId[ip];
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-05-25 16:05:14 +02:00
|
|
|
void CAddrDb::Good_(const CService &addr, int clientV, std::string clientSV, int blocks) {
|
2011-12-19 23:18:06 +01:00
|
|
|
int id = Lookup_(addr);
|
|
|
|
if (id == -1) return;
|
|
|
|
unkId.erase(id);
|
|
|
|
banned.erase(addr);
|
|
|
|
CAddrInfo &info = idToInfo[id];
|
2011-12-23 02:43:32 +01:00
|
|
|
info.clientVersion = clientV;
|
2012-02-23 18:01:09 +01:00
|
|
|
info.clientSubVersion = clientSV;
|
2012-05-25 16:05:14 +02:00
|
|
|
info.blocks = blocks;
|
2011-12-19 23:18:06 +01:00
|
|
|
info.Update(true);
|
2011-12-20 14:29:21 +01:00
|
|
|
if (info.IsGood() && goodId.count(id)==0) {
|
2011-12-19 23:45:18 +01:00
|
|
|
goodId.insert(id);
|
2011-12-26 15:53:22 +01:00
|
|
|
// printf("%s: good; %i good nodes now\n", ToString(addr).c_str(), (int)goodId.size());
|
2011-12-19 23:45:18 +01:00
|
|
|
}
|
2011-12-23 02:43:32 +01:00
|
|
|
nDirty++;
|
2011-12-19 23:18:06 +01:00
|
|
|
ourId.push_back(id);
|
|
|
|
}
|
|
|
|
|
2012-05-25 15:41:27 +02:00
|
|
|
void CAddrDb::Bad_(const CService &addr, int ban)
|
2011-12-19 23:18:06 +01:00
|
|
|
{
|
|
|
|
int id = Lookup_(addr);
|
|
|
|
if (id == -1) return;
|
|
|
|
unkId.erase(id);
|
|
|
|
CAddrInfo &info = idToInfo[id];
|
|
|
|
info.Update(false);
|
2011-12-19 23:45:18 +01:00
|
|
|
uint32_t now = time(NULL);
|
2011-12-23 02:43:32 +01:00
|
|
|
int ter = info.GetBanTime();
|
|
|
|
if (ter) {
|
2011-12-26 15:53:22 +01:00
|
|
|
// printf("%s: terrible\n", ToString(addr).c_str());
|
2011-12-23 02:43:32 +01:00
|
|
|
if (ban < ter) ban = ter;
|
2011-12-19 23:45:18 +01:00
|
|
|
}
|
2011-12-20 06:42:48 +01:00
|
|
|
if (ban > 0) {
|
2011-12-26 15:53:22 +01:00
|
|
|
// printf("%s: ban for %i seconds\n", ToString(addr).c_str(), ban);
|
2011-12-20 06:42:48 +01:00
|
|
|
banned[info.ip] = ban + now;
|
2011-12-19 23:18:06 +01:00
|
|
|
ipToId.erase(info.ip);
|
|
|
|
goodId.erase(id);
|
|
|
|
idToInfo.erase(id);
|
|
|
|
} else {
|
2012-01-01 19:18:56 +01:00
|
|
|
if (/*!info.IsGood() && */ goodId.count(id)==1) {
|
2011-12-19 23:18:06 +01:00
|
|
|
goodId.erase(id);
|
2011-12-26 15:53:22 +01:00
|
|
|
// printf("%s: not good; %i good nodes left\n", ToString(addr).c_str(), (int)goodId.size());
|
2011-12-19 23:45:18 +01:00
|
|
|
}
|
2011-12-19 23:18:06 +01:00
|
|
|
ourId.push_back(id);
|
|
|
|
}
|
2011-12-23 02:43:32 +01:00
|
|
|
nDirty++;
|
2011-12-19 23:18:06 +01:00
|
|
|
}
|
|
|
|
|
2012-05-25 15:41:27 +02:00
|
|
|
void CAddrDb::Skipped_(const CService &addr)
|
2011-12-19 23:18:06 +01:00
|
|
|
{
|
|
|
|
int id = Lookup_(addr);
|
|
|
|
if (id == -1) return;
|
|
|
|
unkId.erase(id);
|
|
|
|
ourId.push_back(id);
|
2011-12-26 15:53:22 +01:00
|
|
|
// printf("%s: skipped\n", ToString(addr).c_str());
|
2011-12-23 02:43:32 +01:00
|
|
|
nDirty++;
|
2011-12-19 23:18:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-26 01:04:24 +01:00
|
|
|
void CAddrDb::Add_(const CAddress &addr, bool force) {
|
|
|
|
if (!force && !addr.IsRoutable())
|
2011-12-19 23:18:06 +01:00
|
|
|
return;
|
2012-05-25 15:41:27 +02:00
|
|
|
CService ipp(addr);
|
2011-12-19 23:18:06 +01:00
|
|
|
if (banned.count(ipp)) {
|
|
|
|
time_t bantime = banned[ipp];
|
2011-12-26 01:04:24 +01:00
|
|
|
if (force || (bantime < time(NULL) && addr.nTime > bantime))
|
2011-12-19 23:18:06 +01:00
|
|
|
banned.erase(ipp);
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ipToId.count(ipp)) {
|
|
|
|
CAddrInfo &ai = idToInfo[ipToId[ipp]];
|
2011-12-20 14:29:21 +01:00
|
|
|
if (addr.nTime > ai.lastTry || ai.services != addr.nServices)
|
|
|
|
{
|
2011-12-19 23:18:06 +01:00
|
|
|
ai.lastTry = addr.nTime;
|
2011-12-20 14:29:21 +01:00
|
|
|
ai.services |= addr.nServices;
|
|
|
|
// printf("%s: updated\n", ToString(addr).c_str());
|
|
|
|
}
|
2011-12-26 01:04:24 +01:00
|
|
|
if (force) {
|
|
|
|
ai.ignoreTill = 0;
|
|
|
|
}
|
2011-12-19 23:18:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
CAddrInfo ai;
|
|
|
|
ai.ip = ipp;
|
|
|
|
ai.services = addr.nServices;
|
|
|
|
ai.lastTry = addr.nTime;
|
|
|
|
ai.ourLastTry = 0;
|
|
|
|
ai.total = 0;
|
|
|
|
ai.success = 0;
|
|
|
|
int id = nId++;
|
|
|
|
idToInfo[id] = ai;
|
|
|
|
ipToId[ipp] = id;
|
2011-12-23 13:43:28 +01:00
|
|
|
// printf("%s: added\n", ToString(ipp).c_str(), ipToId[ipp]);
|
2011-12-19 23:18:06 +01:00
|
|
|
unkId.insert(id);
|
2011-12-23 02:43:32 +01:00
|
|
|
nDirty++;
|
2011-12-19 23:18:06 +01:00
|
|
|
}
|
|
|
|
|
2016-05-21 12:19:25 +02:00
|
|
|
void CAddrDb::GetIPs_(set<CNetAddr>& ips, uint64_t requestedFlags, int max, const bool* nets) {
|
2011-12-19 23:18:06 +01:00
|
|
|
if (goodId.size() == 0) {
|
|
|
|
int id = -1;
|
|
|
|
if (ourId.size() == 0) {
|
|
|
|
if (unkId.size() == 0) return;
|
|
|
|
id = *unkId.begin();
|
|
|
|
} else {
|
|
|
|
id = *ourId.begin();
|
|
|
|
}
|
2016-06-07 18:54:04 +02:00
|
|
|
if (id >= 0 && (idToInfo[id].services & requestedFlags) == requestedFlags) {
|
2011-12-19 23:18:06 +01:00
|
|
|
ips.insert(idToInfo[id].ip);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2016-05-31 10:40:44 +02:00
|
|
|
std::vector<int> goodIdFiltered;
|
2016-05-21 12:19:25 +02:00
|
|
|
for (std::set<int>::const_iterator it = goodId.begin(); it != goodId.end(); it++) {
|
|
|
|
if ((idToInfo[*it].services & requestedFlags) == requestedFlags)
|
2016-05-31 10:40:44 +02:00
|
|
|
goodIdFiltered.push_back(*it);
|
2016-05-21 12:19:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!goodIdFiltered.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (max > goodIdFiltered.size() / 2)
|
|
|
|
max = goodIdFiltered.size() / 2;
|
2011-12-19 23:18:06 +01:00
|
|
|
if (max < 1)
|
|
|
|
max = 1;
|
2016-05-31 10:40:44 +02:00
|
|
|
|
2011-12-19 23:18:06 +01:00
|
|
|
set<int> ids;
|
|
|
|
while (ids.size() < max) {
|
2016-05-31 10:40:44 +02:00
|
|
|
ids.insert(goodIdFiltered[rand() % goodIdFiltered.size()]);
|
2011-12-19 23:18:06 +01:00
|
|
|
}
|
|
|
|
for (set<int>::const_iterator it = ids.begin(); it != ids.end(); it++) {
|
2012-05-25 15:41:27 +02:00
|
|
|
CService &ip = idToInfo[*it].ip;
|
|
|
|
if (nets[ip.GetNetwork()])
|
2011-12-19 23:18:06 +01:00
|
|
|
ips.insert(ip);
|
|
|
|
}
|
|
|
|
}
|