Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/db.cpp
Show All 30 Lines | void CAddrInfo::Update(bool good) { | ||||
// 100.0 * stat1D.reliability, 100.0 * (stat1D.reliability + 1.0 - | // 100.0 * stat1D.reliability, 100.0 * (stat1D.reliability + 1.0 - | ||||
// stat1D.weight), stat1D.count, | // stat1D.weight), stat1D.count, | ||||
// 100.0 * stat1W.reliability, 100.0 * (stat1W.reliability + 1.0 - | // 100.0 * stat1W.reliability, 100.0 * (stat1W.reliability + 1.0 - | ||||
// stat1W.weight), stat1W.count); | // stat1W.weight), stat1W.count); | ||||
} | } | ||||
bool CAddrDb::Get_(CServiceResult &ip, int &wait) { | bool CAddrDb::Get_(CServiceResult &ip, int &wait) { | ||||
int64_t now = time(nullptr); | int64_t now = time(nullptr); | ||||
int cont = 0; | size_t tot = unkId.size() + ourId.size(); | ||||
int tot = unkId.size() + ourId.size(); | |||||
if (tot == 0) { | if (tot == 0) { | ||||
wait = 5; | wait = 5; | ||||
return false; | return false; | ||||
} | } | ||||
do { | do { | ||||
int rnd = rand() % tot; | size_t rnd = rand() % tot; | ||||
int ret; | int ret; | ||||
if (rnd < unkId.size()) { | if (rnd < unkId.size()) { | ||||
std::set<int>::iterator it = unkId.end(); | std::set<int>::iterator it = unkId.end(); | ||||
it--; | it--; | ||||
ret = *it; | ret = *it; | ||||
unkId.erase(it); | unkId.erase(it); | ||||
} else { | } else { | ||||
ret = ourId.front(); | ret = ourId.front(); | ||||
if (time(nullptr) - idToInfo[ret].ourLastTry < MIN_RETRY) | if (time(nullptr) - idToInfo[ret].ourLastTry < MIN_RETRY) { | ||||
return false; | return false; | ||||
} | |||||
ourId.pop_front(); | ourId.pop_front(); | ||||
} | } | ||||
if (idToInfo[ret].ignoreTill && idToInfo[ret].ignoreTill < now) { | if (idToInfo[ret].ignoreTill && idToInfo[ret].ignoreTill < now) { | ||||
ourId.push_back(ret); | ourId.push_back(ret); | ||||
idToInfo[ret].ourLastTry = now; | idToInfo[ret].ourLastTry = now; | ||||
} else { | } else { | ||||
ip.service = idToInfo[ret].ip; | ip.service = idToInfo[ret].ip; | ||||
ip.ourLastSuccess = idToInfo[ret].ourLastSuccess; | ip.ourLastSuccess = idToInfo[ret].ourLastSuccess; | ||||
break; | break; | ||||
} | } | ||||
} while (1); | } while (1); | ||||
nDirty++; | nDirty++; | ||||
return true; | return true; | ||||
} | } | ||||
int CAddrDb::Lookup_(const CService &ip) { | int CAddrDb::Lookup_(const CService &ip) { | ||||
if (ipToId.count(ip)) return ipToId[ip]; | if (ipToId.count(ip)) return ipToId[ip]; | ||||
return -1; | return -1; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | void CAddrDb::Skipped_(const CService &addr) { | ||||
if (id == -1) return; | if (id == -1) return; | ||||
unkId.erase(id); | unkId.erase(id); | ||||
ourId.push_back(id); | ourId.push_back(id); | ||||
// printf("%s: skipped\n", ToString(addr).c_str()); | // printf("%s: skipped\n", ToString(addr).c_str()); | ||||
nDirty++; | nDirty++; | ||||
} | } | ||||
void CAddrDb::Add_(const CAddress &addr, bool force) { | void CAddrDb::Add_(const CAddress &addr, bool force) { | ||||
if (!force && !addr.IsRoutable()) return; | if (!force && !addr.IsRoutable()) { | ||||
return; | |||||
} | |||||
CService ipp(addr); | CService ipp(addr); | ||||
if (banned.count(ipp)) { | if (banned.count(ipp)) { | ||||
time_t bantime = banned[ipp]; | time_t bantime = banned[ipp]; | ||||
if (force || (bantime < time(nullptr) && addr.nTime > bantime)) | if (force || (bantime < time(nullptr) && addr.nTime > bantime)) { | ||||
banned.erase(ipp); | banned.erase(ipp); | ||||
else | } else { | ||||
return; | return; | ||||
} | } | ||||
} | |||||
if (ipToId.count(ipp)) { | if (ipToId.count(ipp)) { | ||||
CAddrInfo &ai = idToInfo[ipToId[ipp]]; | CAddrInfo &ai = idToInfo[ipToId[ipp]]; | ||||
if (addr.nTime > ai.lastTry || ai.services != addr.nServices) { | if (addr.nTime > ai.lastTry || ai.services != addr.nServices) { | ||||
ai.lastTry = addr.nTime; | ai.lastTry = addr.nTime; | ||||
ai.services |= addr.nServices; | ai.services |= addr.nServices; | ||||
// printf("%s: updated\n", ToString(addr).c_str()); | // printf("%s: updated\n", ToString(addr).c_str()); | ||||
} | } | ||||
if (force) { | if (force) { | ||||
ai.ignoreTill = 0; | ai.ignoreTill = 0; | ||||
} | } | ||||
return; | return; | ||||
} | } | ||||
CAddrInfo ai; | CAddrInfo ai; | ||||
ai.ip = ipp; | ai.ip = ipp; | ||||
ai.services = addr.nServices; | ai.services = addr.nServices; | ||||
ai.lastTry = addr.nTime; | ai.lastTry = addr.nTime; | ||||
ai.ourLastTry = 0; | ai.ourLastTry = 0; | ||||
ai.total = 0; | ai.total = 0; | ||||
ai.success = 0; | ai.success = 0; | ||||
int id = nId++; | int id = nId++; | ||||
idToInfo[id] = ai; | idToInfo[id] = ai; | ||||
ipToId[ipp] = id; | ipToId[ipp] = id; | ||||
// printf("%s: added\n", ToString(ipp).c_str(), ipToId[ipp]); | // printf("%s: added\n", ToString(ipp).c_str(), ipToId[ipp]); | ||||
unkId.insert(id); | unkId.insert(id); | ||||
nDirty++; | nDirty++; | ||||
} | } | ||||
void CAddrDb::GetIPs_(std::set<CNetAddr> &ips, uint64_t requestedFlags, int max, | void CAddrDb::GetIPs_(std::set<CNetAddr> &ips, uint64_t requestedFlags, | ||||
const bool *nets) { | uint32_t max, const bool *nets) { | ||||
if (goodId.size() == 0) { | if (goodId.size() == 0) { | ||||
int id = -1; | int id = -1; | ||||
if (ourId.size() == 0) { | if (ourId.size() == 0) { | ||||
if (unkId.size() == 0) return; | if (unkId.size() == 0) { | ||||
return; | |||||
} | |||||
id = *unkId.begin(); | id = *unkId.begin(); | ||||
} else { | } else { | ||||
id = *ourId.begin(); | id = *ourId.begin(); | ||||
} | } | ||||
if (id >= 0 && | if (id >= 0 && | ||||
(idToInfo[id].services & requestedFlags) == requestedFlags) { | (idToInfo[id].services & requestedFlags) == requestedFlags) { | ||||
ips.insert(idToInfo[id].ip); | ips.insert(idToInfo[id].ip); | ||||
} | } | ||||
return; | return; | ||||
} | } | ||||
std::vector<int> goodIdFiltered; | std::vector<int> goodIdFiltered; | ||||
for (std::set<int>::const_iterator it = goodId.begin(); it != goodId.end(); | for (auto &id : goodId) { | ||||
it++) { | if ((idToInfo[id].services & requestedFlags) == requestedFlags) { | ||||
if ((idToInfo[*it].services & requestedFlags) == requestedFlags) | goodIdFiltered.push_back(id); | ||||
goodIdFiltered.push_back(*it); | } | ||||
} | |||||
if (!goodIdFiltered.size()) { | |||||
return; | |||||
} | } | ||||
if (!goodIdFiltered.size()) return; | if (max > goodIdFiltered.size() / 2) { | ||||
max = goodIdFiltered.size() / 2; | |||||
} | |||||
if (max > goodIdFiltered.size() / 2) max = goodIdFiltered.size() / 2; | if (max < 1) { | ||||
if (max < 1) max = 1; | max = 1; | ||||
} | |||||
std::set<int> ids; | std::set<int> ids; | ||||
while (ids.size() < max) { | while (ids.size() < max) { | ||||
ids.insert(goodIdFiltered[rand() % goodIdFiltered.size()]); | ids.insert(goodIdFiltered[rand() % goodIdFiltered.size()]); | ||||
} | } | ||||
for (std::set<int>::const_iterator it = ids.begin(); it != ids.end(); | |||||
it++) { | for (auto &id : ids) { | ||||
CService &ip = idToInfo[*it].ip; | CService &ip = idToInfo[id].ip; | ||||
if (nets[ip.GetNetwork()]) ips.insert(ip); | if (nets[ip.GetNetwork()]) { | ||||
ips.insert(ip); | |||||
} | |||||
} | } | ||||
} | } |