Only poll nodes once a week for new addresses
This commit is contained in:
parent
a8d711b832
commit
966d040f8d
5 changed files with 21 additions and 13 deletions
16
bitcoin.cpp
16
bitcoin.cpp
|
@ -87,9 +87,13 @@ class CNode {
|
||||||
|
|
||||||
void GotVersion() {
|
void GotVersion() {
|
||||||
// printf("\n%s: version %i\n", ToString(you).c_str(), nVersion);
|
// printf("\n%s: version %i\n", ToString(you).c_str(), nVersion);
|
||||||
BeginMessage("getaddr");
|
if (vAddr) {
|
||||||
EndMessage();
|
BeginMessage("getaddr");
|
||||||
doneAfter = time(NULL) + GetTimeout();
|
EndMessage();
|
||||||
|
doneAfter = time(NULL) + GetTimeout();
|
||||||
|
} else {
|
||||||
|
doneAfter = time(NULL) + 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ProcessMessage(string strCommand, CDataStream& vRecv) {
|
bool ProcessMessage(string strCommand, CDataStream& vRecv) {
|
||||||
|
@ -126,7 +130,7 @@ class CNode {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strCommand == "addr") {
|
if (strCommand == "addr" && vAddr) {
|
||||||
vector<CAddress> vAddrNew;
|
vector<CAddress> vAddrNew;
|
||||||
vRecv >> vAddrNew;
|
vRecv >> vAddrNew;
|
||||||
// printf("%s: got %i addresses\n", ToString(you).c_str(), (int)vAddrNew.size());
|
// printf("%s: got %i addresses\n", ToString(you).c_str(), (int)vAddrNew.size());
|
||||||
|
@ -196,7 +200,7 @@ class CNode {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
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.SetType(SER_NETWORK);
|
||||||
vSend.SetVersion(0);
|
vSend.SetVersion(0);
|
||||||
vRecv.SetType(SER_NETWORK);
|
vRecv.SetType(SER_NETWORK);
|
||||||
|
@ -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 {
|
try {
|
||||||
CNode node(cip, vAddr);
|
CNode node(cip, vAddr);
|
||||||
bool ret = node.Run();
|
bool ret = node.Run();
|
||||||
|
|
|
@ -3,6 +3,6 @@
|
||||||
|
|
||||||
#include "protocol.h"
|
#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
|
#endif
|
||||||
|
|
5
db.cpp
5
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);
|
// 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);
|
int64 now = time(NULL);
|
||||||
int cont = 0;
|
int cont = 0;
|
||||||
int tot = unkId.size() + ourId.size();
|
int tot = unkId.size() + ourId.size();
|
||||||
|
@ -54,7 +54,8 @@ bool CAddrDb::Get_(CService &ip, int &wait) {
|
||||||
ourId.push_back(ret);
|
ourId.push_back(ret);
|
||||||
idToInfo[ret].ourLastTry = now;
|
idToInfo[ret].ourLastTry = now;
|
||||||
} else {
|
} else {
|
||||||
ip = idToInfo[ret].ip;
|
ip.service = idToInfo[ret].ip;
|
||||||
|
ip.ourLastSuccess = idToInfo[ret].ourLastSuccess;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while(1);
|
} while(1);
|
||||||
|
|
7
db.h
7
db.h
|
@ -182,6 +182,7 @@ struct CServiceResult {
|
||||||
int nHeight;
|
int nHeight;
|
||||||
int nClientV;
|
int nClientV;
|
||||||
std::string strClientV;
|
std::string strClientV;
|
||||||
|
int64 ourLastSuccess;
|
||||||
};
|
};
|
||||||
|
|
||||||
// seen nodes
|
// seen nodes
|
||||||
|
@ -206,7 +207,7 @@ private:
|
||||||
protected:
|
protected:
|
||||||
// internal routines that assume proper locks are acquired
|
// internal routines that assume proper locks are acquired
|
||||||
void Add_(const CAddress &addr, bool force); // add an address
|
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);
|
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 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_)
|
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)
|
CRITICAL_BLOCK(cs)
|
||||||
Bad_(addr, ban);
|
Bad_(addr, ban);
|
||||||
}
|
}
|
||||||
bool Get(CService &ip, int& wait) {
|
bool Get(CServiceResult &ip, int& wait) {
|
||||||
CRITICAL_BLOCK(cs)
|
CRITICAL_BLOCK(cs)
|
||||||
return Get_(ip, wait);
|
return Get_(ip, wait);
|
||||||
}
|
}
|
||||||
|
@ -326,7 +327,7 @@ public:
|
||||||
CRITICAL_BLOCK(cs) {
|
CRITICAL_BLOCK(cs) {
|
||||||
while (max > 0) {
|
while (max > 0) {
|
||||||
CServiceResult ip = {};
|
CServiceResult ip = {};
|
||||||
if (!Get_(ip.service, wait))
|
if (!Get_(ip, wait))
|
||||||
return;
|
return;
|
||||||
ips.push_back(ip);
|
ips.push_back(ip);
|
||||||
max--;
|
max--;
|
||||||
|
|
4
main.cpp
4
main.cpp
|
@ -125,6 +125,7 @@ extern "C" void* ThreadCrawler(void* data) {
|
||||||
std::vector<CServiceResult> ips;
|
std::vector<CServiceResult> ips;
|
||||||
int wait = 5;
|
int wait = 5;
|
||||||
db.GetMany(ips, 16, wait);
|
db.GetMany(ips, 16, wait);
|
||||||
|
int64 now = time(NULL);
|
||||||
if (ips.empty()) {
|
if (ips.empty()) {
|
||||||
wait *= 1000;
|
wait *= 1000;
|
||||||
wait += rand() % (500 * NTHREADS);
|
wait += rand() % (500 * NTHREADS);
|
||||||
|
@ -138,7 +139,8 @@ extern "C" void* ThreadCrawler(void* data) {
|
||||||
res.nClientV = 0;
|
res.nClientV = 0;
|
||||||
res.nHeight = 0;
|
res.nHeight = 0;
|
||||||
res.strClientV = "";
|
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.ResultMany(ips);
|
||||||
db.Add(addr);
|
db.Add(addr);
|
||||||
|
|
Loading…
Add table
Reference in a new issue