tweaks and cleanups
This commit is contained in:
parent
0553a002d4
commit
3cb418361b
4 changed files with 78 additions and 49 deletions
9
db.cpp
9
db.cpp
|
@ -138,13 +138,13 @@ void CAddrDb::Skipped_(const CIPPort &addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void CAddrDb::Add_(const CAddress &addr) {
|
void CAddrDb::Add_(const CAddress &addr, bool force) {
|
||||||
if (!addr.IsRoutable())
|
if (!force && !addr.IsRoutable())
|
||||||
return;
|
return;
|
||||||
CIPPort ipp(addr);
|
CIPPort ipp(addr);
|
||||||
if (banned.count(ipp)) {
|
if (banned.count(ipp)) {
|
||||||
time_t bantime = banned[ipp];
|
time_t bantime = banned[ipp];
|
||||||
if (bantime < time(NULL) && addr.nTime > bantime)
|
if (force || (bantime < time(NULL) && addr.nTime > bantime))
|
||||||
banned.erase(ipp);
|
banned.erase(ipp);
|
||||||
else
|
else
|
||||||
return;
|
return;
|
||||||
|
@ -157,6 +157,9 @@ void CAddrDb::Add_(const CAddress &addr) {
|
||||||
ai.services |= addr.nServices;
|
ai.services |= addr.nServices;
|
||||||
// printf("%s: updated\n", ToString(addr).c_str());
|
// printf("%s: updated\n", ToString(addr).c_str());
|
||||||
}
|
}
|
||||||
|
if (force) {
|
||||||
|
ai.ignoreTill = 0;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
CAddrInfo ai;
|
CAddrInfo ai;
|
||||||
|
|
54
db.h
54
db.h
|
@ -68,10 +68,10 @@ public:
|
||||||
|
|
||||||
if (total <= 3 && success * 2 >= total) return true;
|
if (total <= 3 && success * 2 >= total) return true;
|
||||||
|
|
||||||
if (stat2H.reliability > 0.7 && stat2H.count > 3) return true;
|
if (stat2H.reliability > 0.7 && stat2H.count > 1) return true;
|
||||||
if (stat8H.reliability > 0.6 && stat8H.count > 6) return true;
|
if (stat8H.reliability > 0.6 && stat8H.count > 2) return true;
|
||||||
if (stat1D.reliability > 0.5 && stat1D.count > 12) return true;
|
if (stat1D.reliability > 0.5 && stat1D.count > 4) return true;
|
||||||
if (stat1W.reliability > 0.4 && stat1W.count > 24) return true;
|
if (stat1W.reliability > 0.4 && stat1W.count > 8) return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -116,6 +116,15 @@ public:
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class CAddrDbStats {
|
||||||
|
public:
|
||||||
|
int nBanned;
|
||||||
|
int nAvail;
|
||||||
|
int nTracked;
|
||||||
|
int nNew;
|
||||||
|
int nGood;
|
||||||
|
};
|
||||||
|
|
||||||
// seen nodes
|
// seen nodes
|
||||||
// / \
|
// / \
|
||||||
// (a) banned nodes available nodes--------------
|
// (a) banned nodes available nodes--------------
|
||||||
|
@ -138,7 +147,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); // add an address
|
void Add_(const CAddress &addr, bool force); // add an address
|
||||||
bool Get_(CIPPort &ip, int& wait); // get an IP to test (must call Good_, Bad_, or Skipped_ on result afterwards)
|
bool Get_(CIPPort &ip, int& wait); // get an IP to test (must call Good_, Bad_, or Skipped_ on result afterwards)
|
||||||
void Good_(const CIPPort &ip, int clientV); // mark an IP as good (must have been returned by Get_)
|
void Good_(const CIPPort &ip, int clientV); // mark an IP as good (must have been returned by Get_)
|
||||||
void Bad_(const CIPPort &ip, int ban); // mark an IP as bad (and optionally ban it) (must have been returned by Get_)
|
void Bad_(const CIPPort &ip, int ban); // mark an IP as bad (and optionally ban it) (must have been returned by Get_)
|
||||||
|
@ -148,6 +157,16 @@ protected:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
void GetStats(CAddrDbStats &stats) {
|
||||||
|
SHARED_CRITICAL_BLOCK(cs) {
|
||||||
|
stats.nBanned = banned.size();
|
||||||
|
stats.nAvail = idToInfo.size();
|
||||||
|
stats.nTracked = ourId.size();
|
||||||
|
stats.nGood = goodId.size();
|
||||||
|
stats.nNew = unkId.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// serialization code
|
// serialization code
|
||||||
// format:
|
// format:
|
||||||
// nVersion (0 for now)
|
// nVersion (0 for now)
|
||||||
|
@ -197,30 +216,15 @@ public:
|
||||||
READWRITE(banned);
|
READWRITE(banned);
|
||||||
}
|
}
|
||||||
});)
|
});)
|
||||||
|
|
||||||
// print statistics
|
void Add(const CAddress &addr, bool fForce = false) {
|
||||||
void Stats() {
|
|
||||||
SHARED_CRITICAL_BLOCK(cs) {
|
|
||||||
if (nDirty > 50) {
|
|
||||||
printf("**** %i available (%i tracked, %i new, %i active), %i banned; %i good\n",
|
|
||||||
(int)idToInfo.size(),
|
|
||||||
(int)ourId.size(),
|
|
||||||
(int)unkId.size(),
|
|
||||||
(int)idToInfo.size() - (int)ourId.size() - (int)unkId.size(),
|
|
||||||
(int)banned.size(),
|
|
||||||
(int)goodId.size());
|
|
||||||
nDirty = 0; // hopefully atomic
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void Add(const CAddress &addr) {
|
|
||||||
CRITICAL_BLOCK(cs)
|
CRITICAL_BLOCK(cs)
|
||||||
Add_(addr);
|
Add_(addr, fForce);
|
||||||
}
|
}
|
||||||
void Add(const std::vector<CAddress> &vAddr) {
|
void Add(const std::vector<CAddress> &vAddr, bool fForce = false) {
|
||||||
CRITICAL_BLOCK(cs)
|
CRITICAL_BLOCK(cs)
|
||||||
for (int i=0; i<vAddr.size(); i++)
|
for (int i=0; i<vAddr.size(); i++)
|
||||||
Add_(vAddr[i]);
|
Add_(vAddr[i], fForce);
|
||||||
}
|
}
|
||||||
void Good(const CIPPort &addr, int clientVersion) {
|
void Good(const CIPPort &addr, int clientVersion) {
|
||||||
CRITICAL_BLOCK(cs)
|
CRITICAL_BLOCK(cs)
|
||||||
|
|
2
dns.c
2
dns.c
|
@ -348,7 +348,7 @@ int dnsserver(dns_opt_t *opt) {
|
||||||
do {
|
do {
|
||||||
ssize_t insize = recvfrom(s, inbuf, BUFLEN, 0, (struct sockaddr*)&si_other, &slen);
|
ssize_t insize = recvfrom(s, inbuf, BUFLEN, 0, (struct sockaddr*)&si_other, &slen);
|
||||||
unsigned char *addr = (unsigned char*)&si_other.sin_addr.s_addr;
|
unsigned char *addr = (unsigned char*)&si_other.sin_addr.s_addr;
|
||||||
printf("DNS: Request from %i.%i.%i.%i:%i of %i bytes\n", addr[0], addr[1], addr[2], addr[3], ntohs(si_other.sin_port), (int)insize);
|
printf("DNS: Request %llu from %i.%i.%i.%i:%i of %i bytes\n", (unsigned long long)(++opt->nRequests), addr[0], addr[1], addr[2], addr[3], ntohs(si_other.sin_port), (int)insize);
|
||||||
if (insize > 0) {
|
if (insize > 0) {
|
||||||
ssize_t ret = dnshandle(opt, inbuf, insize, outbuf);
|
ssize_t ret = dnshandle(opt, inbuf, insize, outbuf);
|
||||||
if (ret > 0)
|
if (ret > 0)
|
||||||
|
|
62
main.cpp
62
main.cpp
|
@ -3,7 +3,7 @@
|
||||||
#include "bitcoin.h"
|
#include "bitcoin.h"
|
||||||
#include "db.h"
|
#include "db.h"
|
||||||
|
|
||||||
#define NTHREADS 16
|
#define NTHREADS 32
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -15,7 +15,6 @@ CAddrDb db;
|
||||||
|
|
||||||
extern "C" void* ThreadCrawler(void* data) {
|
extern "C" void* ThreadCrawler(void* data) {
|
||||||
do {
|
do {
|
||||||
db.Stats();
|
|
||||||
CIPPort ip;
|
CIPPort ip;
|
||||||
int wait = 5;
|
int wait = 5;
|
||||||
if (!db.Get(ip, wait)) {
|
if (!db.Get(ip, wait)) {
|
||||||
|
@ -57,16 +56,18 @@ extern "C" int GetIPList(struct in_addr *addr, int max, int ipv4only) {
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static dns_opt_t dns_opt;
|
||||||
|
|
||||||
extern "C" void* ThreadDNS(void*) {
|
extern "C" void* ThreadDNS(void*) {
|
||||||
dns_opt_t opt;
|
dns_opt.host = "seed.bitcoin.sipa.be";
|
||||||
opt.host = "seed.bitcoin.sipa.be";
|
dns_opt.ns = "vps.sipa.be";
|
||||||
opt.ns = "vps.sipa.be";
|
dns_opt.mbox = "sipa.ulyssis.org";
|
||||||
opt.mbox = "sipa.ulyssis.org";
|
dns_opt.datattl = 60;
|
||||||
opt.datattl = 60;
|
dns_opt.nsttl = 40000;
|
||||||
opt.nsttl = 40000;
|
dns_opt.cb = GetIPList;
|
||||||
opt.cb = GetIPList;
|
dns_opt.port = 53;
|
||||||
opt.port = 53;
|
dns_opt.nRequests = 0;
|
||||||
dnsserver(&opt);
|
dnsserver(&dns_opt);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void* ThreadDumper(void*) {
|
extern "C" void* ThreadDumper(void*) {
|
||||||
|
@ -82,24 +83,45 @@ extern "C" void* ThreadDumper(void*) {
|
||||||
} while(1);
|
} while(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "C" void* ThreadStats(void*) {
|
||||||
|
do {
|
||||||
|
CAddrDbStats stats;
|
||||||
|
db.GetStats(stats);
|
||||||
|
printf("*** %i available (%i tracked, %i new, %i active), %i banned; %i good; %llu DNS requests\n", stats.nAvail, stats.nTracked, stats.nNew, stats.nAvail - stats.nTracked - stats.nNew, stats.nBanned, stats.nGood, (unsigned long long)dns_opt.nRequests);
|
||||||
|
Sleep(10000);
|
||||||
|
} while(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const string seeds[] = {"dnsseed.bluematt.me", "bitseed.xf2.org", "dnsseed.bitcoin.dashjr.org", "seed.bitcoin.sipa.be"};
|
||||||
|
|
||||||
|
extern "C" void* ThreadSeeder(void*) {
|
||||||
|
do {
|
||||||
|
for (int i=0; i<sizeof(seeds)/sizeof(seeds[0]); i++) {
|
||||||
|
vector<CIP> ips;
|
||||||
|
LookupHost(seeds[i].c_str(), ips);
|
||||||
|
for (vector<CIP>::iterator it = ips.begin(); it != ips.end(); it++) {
|
||||||
|
db.Add(CIPPort(*it, 8333), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Sleep(1800000);
|
||||||
|
} while(1);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
FILE *f = fopen("dnsseed.dat","r");
|
FILE *f = fopen("dnsseed.dat","r");
|
||||||
if (f) {
|
if (f) {
|
||||||
CAutoFile cf(f);
|
CAutoFile cf(f);
|
||||||
cf >> db;
|
cf >> db;
|
||||||
}
|
}
|
||||||
vector<CIP> ips;
|
pthread_t thread[NTHREADS+4];
|
||||||
LookupHost("dnsseed.bluematt.me", ips);
|
|
||||||
for (vector<CIP>::iterator it = ips.begin(); it != ips.end(); it++) {
|
|
||||||
db.Add(CIPPort(*it, 8333));
|
|
||||||
}
|
|
||||||
pthread_t thread[NTHREADS+2];
|
|
||||||
for (int i=0; i<NTHREADS; i++) {
|
for (int i=0; i<NTHREADS; i++) {
|
||||||
pthread_create(&thread[i], NULL, ThreadCrawler, NULL);
|
pthread_create(&thread[i], NULL, ThreadCrawler, NULL);
|
||||||
}
|
}
|
||||||
pthread_create(&thread[NTHREADS], NULL, ThreadDumper, NULL);
|
pthread_create(&thread[NTHREADS+0], NULL, ThreadSeeder, NULL);
|
||||||
pthread_create(&thread[NTHREADS+1], NULL, ThreadDNS, NULL);
|
pthread_create(&thread[NTHREADS+1], NULL, ThreadDumper, NULL);
|
||||||
for (int i=0; i<NTHREADS+2; i++) {
|
pthread_create(&thread[NTHREADS+2], NULL, ThreadDNS, NULL);
|
||||||
|
pthread_create(&thread[NTHREADS+3], NULL, ThreadStats, NULL);
|
||||||
|
for (int i=0; i<NTHREADS+4; i++) {
|
||||||
void* res;
|
void* res;
|
||||||
pthread_join(thread[i], &res);
|
pthread_join(thread[i], &res);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue