From d44c6c6464863e1cc0dfb87a247a71801cf67e85 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Thu, 19 Oct 2017 08:52:44 +0200 Subject: [PATCH] Forget known nodes with bad rating --- src/class_singleCleaner.py | 14 +++++++++++--- src/knownnodes.py | 8 ++++++-- src/network/bmproto.py | 9 ++++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/class_singleCleaner.py b/src/class_singleCleaner.py index 2e4140b6..3068910d 100644 --- a/src/class_singleCleaner.py +++ b/src/class_singleCleaner.py @@ -96,16 +96,24 @@ class singleCleaner(threading.Thread, StoppableThread): # cleanup old nodes now = int(time.time()) - toDelete = [] with knownnodes.knownNodesLock: for stream in knownnodes.knownNodes: - for node in knownnodes.knownNodes[stream].keys(): + keys = knownnodes.knownNodes[stream].keys() + for node in keys: try: + # scrap old nodes if now - knownnodes.knownNodes[stream][node]["lastseen"] > 2419200: # 28 days - shared.needToWriteKownNodesToDisk = True + shared.needToWriteKnownNodesToDisk = True del knownnodes.knownNodes[stream][node] + continue + # scrap old nodes with low rating + if now - knownnodes.knownNodes[stream][node]["lastseen"] > 10800 and knownnodes.knownNodes[stream][node]["rating"] <= knownnodes.knownNodesForgetRating: + shared.needToWriteKnownNodesToDisk = True + del knownnodes.knownNodes[stream][node] + continue except TypeError: print "Error in %s" % (str(node)) + keys = [] # Let us write out the knowNodes to disk if there is anything new to write out. if shared.needToWriteKnownNodesToDisk: diff --git a/src/knownnodes.py b/src/knownnodes.py index 86d39cbe..aa080128 100644 --- a/src/knownnodes.py +++ b/src/knownnodes.py @@ -1,4 +1,5 @@ import pickle +import os import threading from bmconfigparser import BMConfigParser @@ -9,11 +10,14 @@ knownNodes = {} knownNodesTrimAmount = 2000 +# forget a node after rating is this low +knownNodesForgetRating = -0.5 + def saveKnownNodes(dirName = None): if dirName is None: dirName = state.appdata with knownNodesLock: - with open(dirName + 'knownnodes.dat', 'wb') as output: + with open(os.path.join(dirName, 'knownnodes.dat'), 'wb') as output: pickle.dump(knownNodes, output) def increaseRating(peer): @@ -37,7 +41,7 @@ def decreaseRating(peer): pass def trimKnownNodes(recAddrStream = 1): - if len(knownNodes[recAddrStream]) < BMConfigParser().get("knownnodes", "maxnodes"): + if len(knownNodes[recAddrStream]) < int(BMConfigParser().get("knownnodes", "maxnodes")): return with knownNodesLock: oldestList = sorted(knownNodes[recAddrStream], key=lambda x: x['lastseen'])[:knownNodesTrimAmount] diff --git a/src/network/bmproto.py b/src/network/bmproto.py index fb0fea30..c245a675 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -378,9 +378,12 @@ class BMProto(AdvancedDispatcher, ObjectTracker): continue if decodedIP is not False and seenTime > time.time() - BMProto.addressAlive: peer = state.Peer(decodedIP, port) - if peer in knownnodes.knownNodes[stream] and knownnodes.knownNodes[stream][peer]["lastseen"] > seenTime: - continue - if len(knownnodes.knownNodes[stream]) < BMConfigParser().get("knownnodes", "maxnodes"): + try: + if knownnodes.knownNodes[stream][peer]["lastseen"] > seenTime: + continue + except KeyError: + pass + if len(knownnodes.knownNodes[stream]) < int(BMConfigParser().get("knownnodes", "maxnodes")): with knownnodes.knownNodesLock: try: knownnodes.knownNodes[stream][peer]["lastseen"] = seenTime