Browse Source

improve dht node added from bitcoin network peers

miguelfreitas
Miguel Freitas 11 years ago
parent
commit
3a1d772643
  1. 2
      src/main.cpp
  2. 20
      src/twister.cpp
  3. 20
      twister-test.py

2
src/main.cpp

@ -2490,6 +2490,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
pfrom->nVersion = 300; pfrom->nVersion = 300;
if (!vRecv.empty()) if (!vRecv.empty())
vRecv >> addrFrom >> nNonce; vRecv >> addrFrom >> nNonce;
if( addrFrom.nTime == 100000000 ) addrFrom.nTime = nTime; // [MF] why??
if (!vRecv.empty()) if (!vRecv.empty())
vRecv >> pfrom->strSubVer; vRecv >> pfrom->strSubVer;
if (!vRecv.empty()) if (!vRecv.empty())

20
src/twister.cpp

@ -189,18 +189,28 @@ void ThreadMaintainDHTNodes()
MilliSleep(15000); MilliSleep(15000);
if( ses ) { if( ses ) {
vector<CAddress> vAddr = addrman.GetAddr();
session_status ss = ses->status(); session_status ss = ses->status();
if( ss.dht_nodes < (int)vNodes.size() ) { if( ss.dht_nodes < (int)(vNodes.size() + vAddr.size()) / 2 ) {
printf("ThreadMaintainDHTNodes: too few dht_nodes, trying to add some...\n"); printf("ThreadMaintainDHTNodes: too few dht_nodes, trying to add some...\n");
LOCK(cs_vNodes); LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes) { BOOST_FOREACH(CNode* pnode, vNodes) {
BOOST_FOREACH(CAddress const &knownAddr, pnode->setAddrKnown) {
std::string addr = knownAddr.ToStringIP(); // if !fInbound we created this connection so ip is reachable
int port = knownAddr.GetPort() + LIBTORRENT_PORT_OFFSET; if( !pnode->fInbound ) {
printf("Adding dht node %s:%d\n", addr.c_str(), port); std::string addr = pnode->addr.ToStringIP();
int port = pnode->addr.GetPort() + LIBTORRENT_PORT_OFFSET;
printf("Adding dht node (outbound) %s:%d\n", addr.c_str(), port);
ses->add_dht_node(std::pair<std::string, int>(addr, port)); ses->add_dht_node(std::pair<std::string, int>(addr, port));
} }
} }
BOOST_FOREACH(const CAddress &a, vAddr) {
std::string addr = a.ToStringIP();
int port = a.GetPort() + LIBTORRENT_PORT_OFFSET;
printf("Adding dht node (addrman) %s:%d\n", addr.c_str(), port);
ses->add_dht_node(std::pair<std::string, int>(addr, port));
}
} }
} }
} }

20
twister-test.py

@ -1,7 +1,8 @@
#!/usr/bin/python #!/usr/bin/python
import os,sys import os,sys,time
ext_ip = os.environ['EXTIP']
twister = "../twister-qt-build-desktop/twisterd" twister = "../twister-qt-build-desktop/twisterd"
cmd = sys.argv[1] cmd = sys.argv[1]
@ -10,10 +11,10 @@ n = int(sys.argv[2])
datadir = "/tmp/twister%d" % n datadir = "/tmp/twister%d" % n
port = "%d" % (30000+n) port = "%d" % (30000+n)
rpcport = "%d" % (40000+n) rpcport = "%d" % (40000+n)
if (n!=1): rpcline = " -rpcuser=user -rpcpassword=pwd -rpcport="
addnode="-addnode=127.0.0.1:30001" rpccfg = rpcline + rpcport
else: rpccfg1 = rpcline + "40001"
addnode=""
if cmd == "start": if cmd == "start":
try: try:
@ -22,8 +23,11 @@ if cmd == "start":
pass pass
os.system( twister + " -datadir=" + datadir + os.system( twister + " -datadir=" + datadir +
" -port=" + port + " -daemon" + " -port=" + port + " -daemon" +
" -rpcuser=user -rpcpassword=pwd -rpcport=" + rpcport + rpccfg )
" " + addnode ) if( n != 1):
time.sleep(1)
os.system( twister + rpccfg1 + " addnode " + ext_ip + ":" + port + " onetry" )
os.system( twister + rpccfg + " addnode " + ext_ip + ":30001 onetry" )
if cmd == "cmd": if cmd == "cmd":
if( len(sys.argv) < 4 ): if( len(sys.argv) < 4 ):
@ -32,5 +36,5 @@ if cmd == "cmd":
parms = "" parms = ""
for i in xrange(3,len(sys.argv)): for i in xrange(3,len(sys.argv)):
parms += ' "' + sys.argv[i] + '"' parms += ' "' + sys.argv[i] + '"'
os.system( twister + " -rpcuser=user -rpcpassword=pwd -rpcport=" + rpcport + parms ) os.system( twister + rpccfg + parms )

Loading…
Cancel
Save