CServiceResult and batch scanning

This commit is contained in:
Pieter Wuille 2013-04-13 22:37:06 +02:00
parent 321b221a34
commit 13b26a896d
2 changed files with 46 additions and 13 deletions

32
db.h
View File

@ -162,6 +162,15 @@ public:
int nAge; int nAge;
}; };
struct CServiceResult {
CService service;
bool fGood;
int nBanTime;
int nHeight;
int nClientV;
std::string strClientV;
};
// seen nodes // seen nodes
// / \ // / \
// (a) banned nodes available nodes-------------- // (a) banned nodes available nodes--------------
@ -185,6 +194,7 @@ 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_(CService &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 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_)
void Skipped_(const CService &ip); // mark an IP as skipped (must have been returned by Get_) void Skipped_(const CService &ip); // mark an IP as skipped (must have been returned by Get_)
@ -299,6 +309,28 @@ public:
CRITICAL_BLOCK(cs) CRITICAL_BLOCK(cs)
return Get_(ip, wait); return Get_(ip, wait);
} }
void GetMany(std::vector<CServiceResult> &ips, int max, int& wait) {
CRITICAL_BLOCK(cs) {
while (max > 0) {
CServiceResult ip = {};
if (!Get_(ip.service, wait))
return;
ips.push_back(ip);
max--;
}
}
}
void ResultMany(const std::vector<CServiceResult> &ips) {
CRITICAL_BLOCK(cs) {
for (int i=0; i<ips.size(); i++) {
if (ips[i].fGood) {
Good_(ips[i].service, ips[i].nClientV, ips[i].strClientV, ips[i].nHeight);
} else {
Bad_(ips[i].service, ips[i].nBanTime);
}
}
}
}
void GetIPs(std::set<CNetAddr>& ips, int max, const bool *nets) { void GetIPs(std::set<CNetAddr>& ips, int max, const bool *nets) {
SHARED_CRITICAL_BLOCK(cs) SHARED_CRITICAL_BLOCK(cs)
GetIPs_(ips, max, nets); GetIPs_(ips, max, nets);

View File

@ -120,26 +120,27 @@ CAddrDb db;
extern "C" void* ThreadCrawler(void* data) { extern "C" void* ThreadCrawler(void* data) {
do { do {
CService ip; std::vector<CServiceResult> ips;
int wait = 5; int wait = 5;
if (!db.Get(ip, wait)) { db.GetMany(ips, 100, wait);
if (ips.empty()) {
wait *= 1000; wait *= 1000;
wait += rand() % (500 * NTHREADS); wait += rand() % (500 * NTHREADS);
Sleep(wait); Sleep(wait);
continue; continue;
} }
int ban = 0; printf("Got %i IPs to test!\n", (int)ips.size());
vector<CAddress> addr; vector<CAddress> addr;
int clientV = 0; for (int i=0; i<ips.size(); i++) {
int blocks = 0; CServiceResult &res = ips[i];
std::string clientSV; res.nBanTime = 0;
bool ret = TestNode(ip,ban,clientV,clientSV,blocks,addr); res.nClientV = 0;
db.Add(addr); res.nHeight = 0;
if (ret) { res.strClientV = "";
db.Good(ip, clientV, clientSV, blocks); res.fGood = TestNode(res.service,res.nBanTime,res.nClientV,res.strClientV,res.nHeight,addr);
} else {
db.Bad(ip, ban);
} }
db.ResultMany(ips);
db.Add(addr);
} while(1); } while(1);
} }
@ -376,6 +377,7 @@ int main(int argc, char **argv) {
db.ResetIgnores(); db.ResetIgnores();
printf("done\n"); printf("done\n");
} }
pthread_t threadDns, threadSeed, threadDump, threadStats;
if (fDNS) { if (fDNS) {
printf("Starting %i DNS threads for %s on %s (port %i)...", opts.nDnsThreads, opts.host, opts.ns, opts.nPort); printf("Starting %i DNS threads for %s on %s (port %i)...", opts.nDnsThreads, opts.host, opts.ns, opts.nPort);
dnsThread.clear(); dnsThread.clear();
@ -387,7 +389,6 @@ int main(int argc, char **argv) {
} }
printf("done\n"); printf("done\n");
} }
pthread_t threadDns, threadSeed, threadDump, threadStats;
printf("Starting seeder..."); printf("Starting seeder...");
pthread_create(&threadSeed, NULL, ThreadSeeder, NULL); pthread_create(&threadSeed, NULL, ThreadSeeder, NULL);
printf("done\n"); printf("done\n");