From 966d040f8d976bc824821c5b2bb778363b5128fa Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 25 Apr 2013 01:55:26 +0200 Subject: [PATCH] Only poll nodes once a week for new addresses --- bitcoin.cpp | 16 ++++++++++------ bitcoin.h | 2 +- db.cpp | 5 +++-- db.h | 7 ++++--- main.cpp | 4 +++- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/bitcoin.cpp b/bitcoin.cpp index 6494c1f..720126a 100644 --- a/bitcoin.cpp +++ b/bitcoin.cpp @@ -87,9 +87,13 @@ class CNode { void GotVersion() { // printf("\n%s: version %i\n", ToString(you).c_str(), nVersion); - BeginMessage("getaddr"); - EndMessage(); - doneAfter = time(NULL) + GetTimeout(); + if (vAddr) { + BeginMessage("getaddr"); + EndMessage(); + doneAfter = time(NULL) + GetTimeout(); + } else { + doneAfter = time(NULL) + 1; + } } bool ProcessMessage(string strCommand, CDataStream& vRecv) { @@ -126,7 +130,7 @@ class CNode { return false; } - if (strCommand == "addr") { + if (strCommand == "addr" && vAddr) { vector vAddrNew; vRecv >> vAddrNew; // printf("%s: got %i addresses\n", ToString(you).c_str(), (int)vAddrNew.size()); @@ -196,7 +200,7 @@ class CNode { } public: - CNode(const CService& ip, vector& vAddrIn) : you(ip), nHeaderStart(-1), nMessageStart(-1), vAddr(&vAddrIn), ban(0), doneAfter(0), nVersion(0) { + CNode(const CService& ip, vector* vAddrIn) : you(ip), nHeaderStart(-1), nMessageStart(-1), vAddr(vAddrIn), ban(0), doneAfter(0), nVersion(0) { vSend.SetType(SER_NETWORK); vSend.SetVersion(0); vRecv.SetType(SER_NETWORK); @@ -270,7 +274,7 @@ public: } }; -bool TestNode(const CService &cip, int &ban, int &clientV, std::string &clientSV, int &blocks, vector& vAddr) { +bool TestNode(const CService &cip, int &ban, int &clientV, std::string &clientSV, int &blocks, vector* vAddr) { try { CNode node(cip, vAddr); bool ret = node.Run(); diff --git a/bitcoin.h b/bitcoin.h index 609caa3..2ae9ffe 100644 --- a/bitcoin.h +++ b/bitcoin.h @@ -3,6 +3,6 @@ #include "protocol.h" -bool TestNode(const CService &cip, int &ban, int &client, std::string &clientSV, int &blocks, std::vector& vAddr); +bool TestNode(const CService &cip, int &ban, int &client, std::string &clientSV, int &blocks, std::vector* vAddr); #endif diff --git a/db.cpp b/db.cpp index 592d68a..f7020f2 100644 --- a/db.cpp +++ b/db.cpp @@ -30,7 +30,7 @@ void CAddrInfo::Update(bool good) { // 100.0 * stat1W.reliability, 100.0 * (stat1W.reliability + 1.0 - stat1W.weight), stat1W.count); } -bool CAddrDb::Get_(CService &ip, int &wait) { +bool CAddrDb::Get_(CServiceResult &ip, int &wait) { int64 now = time(NULL); int cont = 0; int tot = unkId.size() + ourId.size(); @@ -54,7 +54,8 @@ bool CAddrDb::Get_(CService &ip, int &wait) { ourId.push_back(ret); idToInfo[ret].ourLastTry = now; } else { - ip = idToInfo[ret].ip; + ip.service = idToInfo[ret].ip; + ip.ourLastSuccess = idToInfo[ret].ourLastSuccess; break; } } while(1); diff --git a/db.h b/db.h index 1e17733..7108cc6 100644 --- a/db.h +++ b/db.h @@ -182,6 +182,7 @@ struct CServiceResult { int nHeight; int nClientV; std::string strClientV; + int64 ourLastSuccess; }; // seen nodes @@ -206,7 +207,7 @@ private: protected: // internal routines that assume proper locks are acquired void Add_(const CAddress &addr, bool force); // add an address - bool Get_(CService &ip, int& wait); // get an IP to test (must call Good_, Bad_, or Skipped_ on result afterwards) + bool Get_(CServiceResult &ip, int& wait); // get an IP to test (must call Good_, Bad_, or Skipped_ on result afterwards) bool GetMany_(std::vector &ips, int max, int& wait); void Good_(const CService &ip, int clientV, std::string clientSV, int blocks); // mark an IP as good (must have been returned by Get_) void Bad_(const CService &ip, int ban); // mark an IP as bad (and optionally ban it) (must have been returned by Get_) @@ -318,7 +319,7 @@ public: CRITICAL_BLOCK(cs) Bad_(addr, ban); } - bool Get(CService &ip, int& wait) { + bool Get(CServiceResult &ip, int& wait) { CRITICAL_BLOCK(cs) return Get_(ip, wait); } @@ -326,7 +327,7 @@ public: CRITICAL_BLOCK(cs) { while (max > 0) { CServiceResult ip = {}; - if (!Get_(ip.service, wait)) + if (!Get_(ip, wait)) return; ips.push_back(ip); max--; diff --git a/main.cpp b/main.cpp index ca745af..bbf76fc 100644 --- a/main.cpp +++ b/main.cpp @@ -125,6 +125,7 @@ extern "C" void* ThreadCrawler(void* data) { std::vector ips; int wait = 5; db.GetMany(ips, 16, wait); + int64 now = time(NULL); if (ips.empty()) { wait *= 1000; wait += rand() % (500 * NTHREADS); @@ -138,7 +139,8 @@ extern "C" void* ThreadCrawler(void* data) { res.nClientV = 0; res.nHeight = 0; res.strClientV = ""; - res.fGood = TestNode(res.service,res.nBanTime,res.nClientV,res.strClientV,res.nHeight,addr); + bool getaddr = res.ourLastSuccess + 604800 < now; + res.fGood = TestNode(res.service,res.nBanTime,res.nClientV,res.strClientV,res.nHeight,getaddr ? &addr : NULL); } db.ResultMany(ips); db.Add(addr);