From 33991f459835f17d896bb778cf1831e078c5731f Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Tue, 7 Jun 2016 12:23:47 +0200 Subject: [PATCH] Implement node priority - prioritise connecting to nodes that were online more recently - hidden service nodes have a higher priority if using tor --- src/class_outgoingSynSender.py | 14 +++++++++++--- src/class_receiveDataThread.py | 2 ++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/class_outgoingSynSender.py b/src/class_outgoingSynSender.py index 71d650ed..357b8151 100644 --- a/src/class_outgoingSynSender.py +++ b/src/class_outgoingSynSender.py @@ -35,13 +35,21 @@ class outgoingSynSender(threading.Thread, StoppableThread): shared.knownNodes[self.streamNumber][peer] = time.time() shared.knownNodesLock.release() else: - while True: + while not shared.shutdown: shared.knownNodesLock.acquire() 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() - 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 - time.sleep(1) + time.sleep(0.01) # prevent CPU hogging if something is broken return peer def stopThread(self): diff --git a/src/class_receiveDataThread.py b/src/class_receiveDataThread.py index 5883ab42..a0c9a837 100644 --- a/src/class_receiveDataThread.py +++ b/src/class_receiveDataThread.py @@ -757,6 +757,8 @@ class receiveDataThread(threading.Thread): if not isHostInPrivateIPRange(self.peer.host): with shared.knownNodesLock: 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 self.sendverack()