Implement node priority

- prioritise connecting to nodes that were online more recently
- hidden service nodes have a higher priority if using tor
This commit is contained in:
Peter Šurda 2016-06-07 12:23:47 +02:00
parent c11dd67e50
commit 33991f4598
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
2 changed files with 13 additions and 3 deletions

View File

@ -35,13 +35,21 @@ class outgoingSynSender(threading.Thread, StoppableThread):
shared.knownNodes[self.streamNumber][peer] = time.time() shared.knownNodes[self.streamNumber][peer] = time.time()
shared.knownNodesLock.release() shared.knownNodesLock.release()
else: else:
while True: while not shared.shutdown:
shared.knownNodesLock.acquire() shared.knownNodesLock.acquire()
peer, = random.sample(shared.knownNodes[self.streamNumber], 1) peer, = random.sample(shared.knownNodes[self.streamNumber], 1)
priority = (183600 - (time.time() - shared.knownNodes[self.streamNumber][peer])) / 183600 # 2 days and 3 hours
shared.knownNodesLock.release() shared.knownNodesLock.release()
if shared.config.get('bitmessagesettings', 'socksproxytype') != 'none' or peer.host.find(".onion") == -1: if shared.config.get('bitmessagesettings', 'socksproxytype') != 'none':
if peer.host.find(".onion") == -1:
priority /= 10 # hidden services have 10x priority over plain net
elif peer.host.find(".onion") != -1: # onion address and so proxy
continue
if priority <= 0.001: # everyone has at least this much priority
priority = 0.001
if (random.random() <= priority):
break break
time.sleep(1) time.sleep(0.01) # prevent CPU hogging if something is broken
return peer return peer
def stopThread(self): def stopThread(self):

View File

@ -757,6 +757,8 @@ class receiveDataThread(threading.Thread):
if not isHostInPrivateIPRange(self.peer.host): if not isHostInPrivateIPRange(self.peer.host):
with shared.knownNodesLock: with shared.knownNodesLock:
shared.knownNodes[self.streamNumber][shared.Peer(self.peer.host, self.remoteNodeIncomingPort)] = int(time.time()) shared.knownNodes[self.streamNumber][shared.Peer(self.peer.host, self.remoteNodeIncomingPort)] = int(time.time())
if not self.initiatedConnection:
shared.knownNodes[self.streamNumber][shared.Peer(self.peer.host, self.remoteNodeIncomingPort)] -= 162000 # penalise inbound, 2 days minus 3 hours
shared.needToWriteKnownNodesToDisk = True shared.needToWriteKnownNodesToDisk = True
self.sendverack() self.sendverack()