Changeset View
Changeset View
Standalone View
Standalone View
src/seeder/db.h
#ifndef BITCOIN_SEEDER_DB_H | #ifndef BITCOIN_SEEDER_DB_H | ||||
#define BITCOIN_SEEDER_DB_H | #define BITCOIN_SEEDER_DB_H | ||||
#include "netbase.h" | #include "netbase.h" | ||||
#include "protocol.h" | #include "protocol.h" | ||||
#include "sync.h" | #include "sync.h" | ||||
#include "util.h" | #include "util.h" | ||||
#include "version.h" | |||||
#include <cmath> | #include <cmath> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <deque> | #include <deque> | ||||
#include <map> | #include <map> | ||||
#include <set> | #include <set> | ||||
#include <vector> | #include <vector> | ||||
Show All 23 Lines | public: | ||||
void Update(bool good, int64_t age, double tau) { | void Update(bool good, int64_t age, double tau) { | ||||
double f = exp(-age / tau); | double f = exp(-age / tau); | ||||
reliability = reliability * f + (good ? (1.0 - f) : 0); | reliability = reliability * f + (good ? (1.0 - f) : 0); | ||||
count = count * f + 1; | count = count * f + 1; | ||||
weight = weight * f + (1.0 - f); | weight = weight * f + (1.0 - f); | ||||
} | } | ||||
IMPLEMENT_SERIALIZE(READWRITE(weight); READWRITE(count); | ADD_SERIALIZE_METHODS; | ||||
READWRITE(reliability);) | |||||
template <typename Stream, typename Operation> | |||||
inline void SerializationOp(Stream &s, Operation ser_action) { | |||||
READWRITE(weight); | |||||
READWRITE(count); | |||||
READWRITE(reliability); | |||||
} | |||||
friend class CAddrInfo; | friend class CAddrInfo; | ||||
}; | }; | ||||
class CAddrReport { | class CAddrReport { | ||||
public: | public: | ||||
CService ip; | CService ip; | ||||
int clientVersion; | int clientVersion; | ||||
▲ Show 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | int GetIgnoreTime() const { | ||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
void Update(bool good); | void Update(bool good); | ||||
friend class CAddrDb; | friend class CAddrDb; | ||||
IMPLEMENT_SERIALIZE(uint8_t version = 4; READWRITE(version); READWRITE(ip); | ADD_SERIALIZE_METHODS; | ||||
READWRITE(services); READWRITE(lastTry); | |||||
uint8_t tried = ourLastTry != 0; READWRITE(tried); | template <typename Stream, typename Operation> | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | |||||
uint8_t version = 4; | |||||
READWRITE(version); | |||||
READWRITE(ip); | |||||
READWRITE(services); | |||||
READWRITE(lastTry); | |||||
uint8_t tried = ourLastTry != 0; | |||||
READWRITE(tried); | |||||
if (tried) { | if (tried) { | ||||
READWRITE(ourLastTry); | READWRITE(ourLastTry); | ||||
READWRITE(ignoreTill); | READWRITE(ignoreTill); | ||||
READWRITE(stat2H); | READWRITE(stat2H); | ||||
READWRITE(stat8H); | READWRITE(stat8H); | ||||
READWRITE(stat1D); | READWRITE(stat1D); | ||||
READWRITE(stat1W); | READWRITE(stat1W); | ||||
if (version >= 1) | if (version >= 1) { | ||||
READWRITE(stat1M); | READWRITE(stat1M); | ||||
else if (!fWrite) | } else if (!ser_action.ForRead()) { | ||||
*((CAddrStat *)(&stat1M)) = stat1W; | *((CAddrStat *)(&stat1M)) = stat1W; | ||||
} | |||||
READWRITE(total); | READWRITE(total); | ||||
READWRITE(success); | READWRITE(success); | ||||
READWRITE(clientVersion); | READWRITE(clientVersion); | ||||
if (version >= 2) READWRITE(clientSubVersion); | if (version >= 2) READWRITE(clientSubVersion); | ||||
if (version >= 3) READWRITE(blocks); | if (version >= 3) READWRITE(blocks); | ||||
if (version >= 4) READWRITE(ourLastSuccess); | if (version >= 4) READWRITE(ourLastSuccess); | ||||
}) | } | ||||
} | |||||
}; | }; | ||||
class CAddrDbStats { | class CAddrDbStats { | ||||
public: | public: | ||||
int nBanned; | int nBanned; | ||||
int nAvail; | int nAvail; | ||||
int nTracked; | int nTracked; | ||||
int nNew; | int nNew; | ||||
▲ Show 20 Lines • Show All 97 Lines • ▼ Show 20 Lines | public: | ||||
// nVersion (0 for now) | // nVersion (0 for now) | ||||
// n (number of ips in (b,c,d)) | // n (number of ips in (b,c,d)) | ||||
// CAddrInfo[n] | // CAddrInfo[n] | ||||
// banned | // banned | ||||
// acquires a shared lock (this does not suffice for read mode, but we | // acquires a shared lock (this does not suffice for read mode, but we | ||||
// assume that only happens at startup, single-threaded) | // assume that only happens at startup, single-threaded) | ||||
// this way, dumping does not interfere with GetIPs_, which is called from | // this way, dumping does not interfere with GetIPs_, which is called from | ||||
// the DNS thread | // the DNS thread | ||||
IMPLEMENT_SERIALIZE(({ | template <typename Stream> void Serialize(Stream &s) const { | ||||
int nVersion = 0; | |||||
READWRITE(nVersion); | |||||
LOCK(cs); | LOCK(cs); | ||||
if (fWrite) { | |||||
int nVersion = 0; | |||||
s << nVersion; | |||||
CAddrDb *db = const_cast<CAddrDb *>(this); | CAddrDb *db = const_cast<CAddrDb *>(this); | ||||
int n = ourId.size() + unkId.size(); | int n = ourId.size() + unkId.size(); | ||||
READWRITE(n); | s << n; | ||||
for (std::deque<int>::const_iterator it = | for (std::deque<int>::const_iterator it = ourId.begin(); | ||||
ourId.begin(); | |||||
it != ourId.end(); it++) { | it != ourId.end(); it++) { | ||||
std::map<int, CAddrInfo>::iterator ci = | std::map<int, CAddrInfo>::iterator ci = db->idToInfo.find(*it); | ||||
db->idToInfo.find(*it); | s << (*ci).second; | ||||
READWRITE((*ci).second); | |||||
} | } | ||||
for (std::set<int>::const_iterator it = | for (std::set<int>::const_iterator it = unkId.begin(); | ||||
unkId.begin(); | |||||
it != unkId.end(); it++) { | it != unkId.end(); it++) { | ||||
std::map<int, CAddrInfo>::iterator ci = | std::map<int, CAddrInfo>::iterator ci = db->idToInfo.find(*it); | ||||
db->idToInfo.find(*it); | s << (*ci).second; | ||||
READWRITE((*ci).second); | |||||
} | } | ||||
} else { | s << banned; | ||||
} | |||||
template <typename Stream> void Unserialize(Stream &s) { | |||||
LOCK(cs); | |||||
int nVersion; | |||||
s >> nVersion; | |||||
CAddrDb *db = const_cast<CAddrDb *>(this); | CAddrDb *db = const_cast<CAddrDb *>(this); | ||||
db->nId = 0; | db->nId = 0; | ||||
int n; | int n; | ||||
READWRITE(n); | s >> n; | ||||
for (int i = 0; i < n; i++) { | for (int i = 0; i < n; i++) { | ||||
CAddrInfo info; | CAddrInfo info; | ||||
READWRITE(info); | s >> info; | ||||
if (!info.GetBanTime()) { | if (!info.GetBanTime()) { | ||||
int id = db->nId++; | int id = db->nId++; | ||||
db->idToInfo[id] = info; | db->idToInfo[id] = info; | ||||
db->ipToId[info.ip] = id; | db->ipToId[info.ip] = id; | ||||
if (info.ourLastTry) { | if (info.ourLastTry) { | ||||
db->ourId.push_back(id); | db->ourId.push_back(id); | ||||
if (info.IsGood()) | if (info.IsGood()) db->goodId.insert(id); | ||||
db->goodId.insert(id); | |||||
} else { | } else { | ||||
db->unkId.insert(id); | db->unkId.insert(id); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
db->nDirty++; | db->nDirty++; | ||||
s >> banned; | |||||
} | } | ||||
READWRITE(banned); | |||||
});) | |||||
void Add(const CAddress &addr, bool fForce = false) { | void Add(const CAddress &addr, bool fForce = false) { | ||||
LOCK(cs); | LOCK(cs); | ||||
Add_(addr, fForce); | Add_(addr, fForce); | ||||
} | } | ||||
void Add(const std::vector<CAddress> &vAddr, bool fForce = false) { | void Add(const std::vector<CAddress> &vAddr, bool fForce = false) { | ||||
LOCK(cs); | LOCK(cs); | ||||
for (size_t i = 0; i < vAddr.size(); i++) { | for (size_t i = 0; i < vAddr.size(); i++) { | ||||
▲ Show 20 Lines • Show All 48 Lines • Show Last 20 Lines |