From 88c4c2623ee91a1fbe2a0bc19ccb9ce31ab2303e Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 20 Dec 2011 08:35:22 +0100 Subject: [PATCH] serialization into dnsseed.dat --- db.h | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- main.cpp | 21 ++++++++++++++++- 2 files changed, 85 insertions(+), 4 deletions(-) diff --git a/db.h b/db.h index 2602b00..ff90bfe 100644 --- a/db.h +++ b/db.h @@ -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 idToInfo; // map address id to address info (b,c,d) std::map ipToId; // map ip to id (b,c,d) @@ -66,6 +81,53 @@ protected: void GetIPs_(std::set& ips, int max, bool fOnlyIPv4); public: + + IMPLEMENT_SERIALIZE (({ + int nVersion = 0; + READWRITE(nVersion); + CRITICAL_BLOCK(cs) { + if (fWrite) { + CAddrDb *db = const_cast(this); + int nOur = ourId.size(); + int nUnk = unkId.size(); + READWRITE(nOur); + READWRITE(nUnk); + for (std::deque::const_iterator it = ourId.begin(); it != ourId.end(); it++) { + std::map::iterator ci = db->idToInfo.find(*it); + READWRITE((*ci).second); + } + for (std::set::const_iterator it = unkId.begin(); it != unkId.end(); it++) { + std::map::iterator ci = db->idToInfo.find(*it); + READWRITE((*ci).second); + } + } else { + CAddrDb *db = const_cast(this); + db->nId = 0; + int nOur, nUnk; + READWRITE(nOur); + READWRITE(nUnk); + for (int i=0; inId++; + 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; inId++; + 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()); diff --git a/main.cpp b/main.cpp index d47cc1e..bd9cdca 100644 --- a/main.cpp +++ b/main.cpp @@ -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 ips; LookupHost("dnsseed.bluematt.me", ips); for (vector::iterator it = ips.begin(); it != ips.end(); it++) { db.Add(CIPPort(*it, 8333)); } pthread_t thread[NTHREADS]; - for (int i=0; i