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:
CIPPort ip;
uint64_t services;
time_t lastTry;
time_t ourLastTry;
int64 lastTry;
int64 ourLastTry;
double reliability;
double timing;
double weight;
@ -35,6 +35,21 @@ public:
void Update(bool good);
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
@ -47,7 +62,7 @@ public:
class CAddrDb {
private:
CCriticalSection cs;
mutable CCriticalSection cs;
int nId; // number of address id's
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)
@ -66,6 +81,53 @@ protected:
void GetIPs_(std::set<CIP>& ips, int max, bool fOnlyIPv4);
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() {
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());

View file

@ -49,18 +49,37 @@ extern "C" void* ThreadDNS(void*) {
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
int main(void) {
FILE *f = fopen("dnsseed.dat","r");
if (f) {
CAutoFile cf(f);
cf >> db;
}
vector<CIP> ips;
LookupHost("dnsseed.bluematt.me", ips);
for (vector<CIP>::iterator it = ips.begin(); it != ips.end(); it++) {
db.Add(CIPPort(*it, 8333));
}
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[NTHREADS-2], NULL, ThreadDumper, NULL);
pthread_create(&thread[NTHREADS-1], NULL, ThreadDNS, NULL);
for (int i=0; i<NTHREADS; i++) {
void* res;