Browse Source

Only poll nodes once a week for new addresses

pull/1/head
Pieter Wuille 11 years ago
parent
commit
966d040f8d
  1. 16
      bitcoin.cpp
  2. 2
      bitcoin.h
  3. 5
      db.cpp
  4. 7
      db.h
  5. 4
      main.cpp

16
bitcoin.cpp

@ -87,9 +87,13 @@ class CNode { @@ -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 { @@ -126,7 +130,7 @@ class CNode {
return false;
}
if (strCommand == "addr") {
if (strCommand == "addr" && vAddr) {
vector<CAddress> vAddrNew;
vRecv >> vAddrNew;
// printf("%s: got %i addresses\n", ToString(you).c_str(), (int)vAddrNew.size());
@ -196,7 +200,7 @@ class CNode { @@ -196,7 +200,7 @@ class CNode {
}
public:
CNode(const CService& ip, vector<CAddress>& vAddrIn) : you(ip), nHeaderStart(-1), nMessageStart(-1), vAddr(&vAddrIn), ban(0), doneAfter(0), nVersion(0) {
CNode(const CService& ip, vector<CAddress>* 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: @@ -270,7 +274,7 @@ public:
}
};
bool TestNode(const CService &cip, int &ban, int &clientV, std::string &clientSV, int &blocks, vector<CAddress>& vAddr) {
bool TestNode(const CService &cip, int &ban, int &clientV, std::string &clientSV, int &blocks, vector<CAddress>* vAddr) {
try {
CNode node(cip, vAddr);
bool ret = node.Run();

2
bitcoin.h

@ -3,6 +3,6 @@ @@ -3,6 +3,6 @@
#include "protocol.h"
bool TestNode(const CService &cip, int &ban, int &client, std::string &clientSV, int &blocks, std::vector<CAddress>& vAddr);
bool TestNode(const CService &cip, int &ban, int &client, std::string &clientSV, int &blocks, std::vector<CAddress>* vAddr);
#endif

5
db.cpp

@ -30,7 +30,7 @@ void CAddrInfo::Update(bool good) { @@ -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) { @@ -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);

7
db.h

@ -182,6 +182,7 @@ struct CServiceResult { @@ -182,6 +182,7 @@ struct CServiceResult {
int nHeight;
int nClientV;
std::string strClientV;
int64 ourLastSuccess;
};
// seen nodes
@ -206,7 +207,7 @@ private: @@ -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<CServiceResult> &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: @@ -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: @@ -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--;

4
main.cpp

@ -125,6 +125,7 @@ extern "C" void* ThreadCrawler(void* data) { @@ -125,6 +125,7 @@ extern "C" void* ThreadCrawler(void* data) {
std::vector<CServiceResult> 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) { @@ -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);

Loading…
Cancel
Save