Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/db.cpp
| #include "db.h" | #include "db.h" | ||||
| #include <stdlib.h> | #include <stdlib.h> | ||||
| using namespace std; | using namespace std; | ||||
| void CAddrInfo::Update(bool good) { | void CAddrInfo::Update(bool good) { | ||||
| uint32_t now = time(NULL); | uint32_t now = time(nullptr); | ||||
| if (ourLastTry == 0) ourLastTry = now - MIN_RETRY; | if (ourLastTry == 0) ourLastTry = now - MIN_RETRY; | ||||
| int age = now - ourLastTry; | int age = now - ourLastTry; | ||||
| lastTry = now; | lastTry = now; | ||||
| ourLastTry = now; | ourLastTry = now; | ||||
| total++; | total++; | ||||
| if (good) { | if (good) { | ||||
| success++; | success++; | ||||
| ourLastSuccess = now; | ourLastSuccess = now; | ||||
| Show All 15 Lines | void CAddrInfo::Update(bool good) { | ||||
| // stat8H.weight), stat8H.count, | // stat8H.weight), stat8H.count, | ||||
| // 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 now = time(NULL); | int64 now = time(nullptr); | ||||
| int cont = 0; | int cont = 0; | ||||
| int 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; | int rnd = rand() % tot; | ||||
| int ret; | int ret; | ||||
| if (rnd < unkId.size()) { | if (rnd < unkId.size()) { | ||||
| set<int>::iterator it = unkId.end(); | 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(NULL) - idToInfo[ret].ourLastTry < MIN_RETRY) return false; | if (time(nullptr) - idToInfo[ret].ourLastTry < MIN_RETRY) | ||||
| 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; | ||||
| Show All 30 Lines | |||||
| } | } | ||||
| void CAddrDb::Bad_(const CService &addr, int ban) { | void CAddrDb::Bad_(const CService &addr, int ban) { | ||||
| int id = Lookup_(addr); | int id = Lookup_(addr); | ||||
| if (id == -1) return; | if (id == -1) return; | ||||
| unkId.erase(id); | unkId.erase(id); | ||||
| CAddrInfo &info = idToInfo[id]; | CAddrInfo &info = idToInfo[id]; | ||||
| info.Update(false); | info.Update(false); | ||||
| uint32_t now = time(NULL); | uint32_t now = time(nullptr); | ||||
| int ter = info.GetBanTime(); | int ter = info.GetBanTime(); | ||||
| if (ter) { | if (ter) { | ||||
| // printf("%s: terrible\n", ToString(addr).c_str()); | // printf("%s: terrible\n", ToString(addr).c_str()); | ||||
| if (ban < ter) ban = ter; | if (ban < ter) ban = ter; | ||||
| } | } | ||||
| if (ban > 0) { | if (ban > 0) { | ||||
| // printf("%s: ban for %i seconds\n", ToString(addr).c_str(), ban); | // printf("%s: ban for %i seconds\n", ToString(addr).c_str(), ban); | ||||
| banned[info.ip] = ban + now; | banned[info.ip] = ban + now; | ||||
| Show All 20 Lines | void CAddrDb::Skipped_(const CService &addr) { | ||||
| 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(NULL) && 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; | ||||
| ▲ Show 20 Lines • Show All 60 Lines • Show Last 20 Lines | |||||