serialization into dnsseed.dat

This commit is contained in:
Pieter Wuille 2011-12-20 08:35:22 +01:00
parent 6b68ff0a60
commit 88c4c2623e
2 changed files with 85 additions and 4 deletions

68
db.h
View file

@ -17,8 +17,8 @@ class CAddrInfo {
private: private:
CIPPort ip; CIPPort ip;
uint64_t services; uint64_t services;
time_t lastTry; int64 lastTry;
time_t ourLastTry; int64 ourLastTry;
double reliability; double reliability;
double timing; double timing;
double weight; double weight;
@ -35,6 +35,21 @@ public:
void Update(bool good); void Update(bool good);
friend class CAddrDb; friend class CAddrDb;
IMPLEMENT_SERIALIZE (
int version = 0;
READWRITE(version);
READWRITE(ip);
READWRITE(services);
READWRITE(lastTry);
READWRITE(ourLastTry);
READWRITE(reliability);
READWRITE(timing);
READWRITE(weight);
READWRITE(count);
READWRITE(total);
READWRITE(success);
)
}; };
// seen nodes // seen nodes
@ -47,7 +62,7 @@ public:
class CAddrDb { class CAddrDb {
private: private:
CCriticalSection cs; mutable CCriticalSection cs;
int nId; // number of address id's int nId; // number of address id's
std::map<int, CAddrInfo> idToInfo; // map address id to address info (b,c,d) 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::map<CIPPort, int> ipToId; // map ip to id (b,c,d)
@ -66,6 +81,53 @@ protected:
void GetIPs_(std::set<CIP>& ips, int max, bool fOnlyIPv4); void GetIPs_(std::set<CIP>& ips, int max, bool fOnlyIPv4);
public: public:
IMPLEMENT_SERIALIZE (({
int nVersion = 0;
READWRITE(nVersion);
CRITICAL_BLOCK(cs) {
if (fWrite) {
CAddrDb *db = const_cast<CAddrDb*>(this);
int nOur = ourId.size();
int nUnk = unkId.size();
READWRITE(nOur);
READWRITE(nUnk);
for (std::deque<int>::const_iterator it = ourId.begin(); it != ourId.end(); it++) {
std::map<int, CAddrInfo>::iterator ci = db->idToInfo.find(*it);
READWRITE((*ci).second);
}
for (std::set<int>::const_iterator it = unkId.begin(); it != unkId.end(); it++) {
std::map<int, CAddrInfo>::iterator ci = db->idToInfo.find(*it);
READWRITE((*ci).second);
}
} else {
CAddrDb *db = const_cast<CAddrDb*>(this);
db->nId = 0;
int nOur, nUnk;
READWRITE(nOur);
READWRITE(nUnk);
for (int i=0; i<nOur; i++) {
CAddrInfo info;
READWRITE(info);
int id = db->nId++;
db->idToInfo[id] = info;
db->ipToId[info.ip] = id;
db->ourId.push_back(id);
if (info.IsGood()) db->goodId.insert(id);
}
for (int i=0; i<nUnk; i++) {
CAddrInfo info;
READWRITE(info);
int id = db->nId++;
db->idToInfo[id] = info;
db->ipToId[info.ip] = id;
db->unkId.insert(id);
}
}
READWRITE(banned);
}
});)
void Stats() { void Stats() {
CRITICAL_BLOCK(cs) CRITICAL_BLOCK(cs)
printf("**** %i good, %lu our, %i unk, %i banned; %i known ips\n", (int)goodId.size(), (unsigned long)ourId.size(), (int)unkId.size(), (int)banned.size(), (int)ipToId.size()); printf("**** %i good, %lu our, %i unk, %i banned; %i known ips\n", (int)goodId.size(), (unsigned long)ourId.size(), (int)unkId.size(), (int)banned.size(), (int)ipToId.size());

View file

@ -49,18 +49,37 @@ extern "C" void* ThreadDNS(void*) {
dnsserver(); dnsserver();
} }
extern "C" void* ThreadDumper(void*) {
do {
Sleep(100000);
{
FILE *f = fopen("dnsseed.dat","w+");
if (f) {
CAutoFile cf(f);
cf << db;
}
}
} while(1);
}
#define NTHREADS 100 #define NTHREADS 100
int main(void) { int main(void) {
FILE *f = fopen("dnsseed.dat","r");
if (f) {
CAutoFile cf(f);
cf >> db;
}
vector<CIP> ips; vector<CIP> ips;
LookupHost("dnsseed.bluematt.me", ips); LookupHost("dnsseed.bluematt.me", ips);
for (vector<CIP>::iterator it = ips.begin(); it != ips.end(); it++) { for (vector<CIP>::iterator it = ips.begin(); it != ips.end(); it++) {
db.Add(CIPPort(*it, 8333)); db.Add(CIPPort(*it, 8333));
} }
pthread_t thread[NTHREADS]; pthread_t thread[NTHREADS];
for (int i=0; i<NTHREADS-1; i++) { for (int i=0; i<NTHREADS-2; i++) {
pthread_create(&thread[i], NULL, ThreadCrawler, NULL); pthread_create(&thread[i], NULL, ThreadCrawler, NULL);
} }
pthread_create(&thread[NTHREADS-2], NULL, ThreadDumper, NULL);
pthread_create(&thread[NTHREADS-1], NULL, ThreadDNS, NULL); pthread_create(&thread[NTHREADS-1], NULL, ThreadDNS, NULL);
for (int i=0; i<NTHREADS; i++) { for (int i=0; i<NTHREADS; i++) {
void* res; void* res;