From 739ff7b4396d6e255c21daa9e03f778a1e55beef Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Sun, 1 Dec 2019 22:08:16 +0200 Subject: [PATCH] Add knownnodes only by knownnodes.addKnownNode(), now it can update lastseen. --- src/knownnodes.py | 25 ++++++++++++++++++++----- src/network/bmproto.py | 11 ++--------- src/network/tcp.py | 8 ++------ 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/knownnodes.py b/src/knownnodes.py index bb588fcb..26aa2f45 100644 --- a/src/knownnodes.py +++ b/src/knownnodes.py @@ -65,7 +65,6 @@ def json_deserialize_knownnodes(source): info = node['info'] peer = Peer(str(peer['host']), peer.get('port', 8444)) knownNodes[node['stream']][peer] = info - if not (knownNodesActual or info.get('self')) and peer not in DEFAULT_NODES: knownNodesActual = True @@ -95,11 +94,27 @@ def saveKnownNodes(dirName=None): def addKnownNode(stream, peer, lastseen=None, is_self=False): - """Add a new node to the dict""" + """ + Add a new node to the dict or update lastseen if it already exists + """ + # pylint: disable=too-many-branches + rating = 0.0 + if not lastseen: + # FIXME: maybe about 28 days? + lastseen = int(time.time()) + else: + lastseen = int(lastseen) + try: + knownNodes[stream][peer]['lastseen'] = lastseen + except KeyError: + pass + else: + return + knownNodes[stream][peer] = { - "lastseen": lastseen or time.time(), - "rating": 1 if is_self else 0, - "self": is_self, + 'lastseen': lastseen, + 'rating': rating or 1 if is_self else 0, + 'self': is_self, } diff --git a/src/network/bmproto.py b/src/network/bmproto.py index 637e2ba7..48c8c37c 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -456,15 +456,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker): if len(knownnodes.knownNodes[stream]) < \ BMConfigParser().safeGetInt("knownnodes", "maxnodes"): with knownnodes.knownNodesLock: - try: - knownnodes.knownNodes[stream][peer]["lastseen"] = \ - seenTime - except (TypeError, KeyError): - knownnodes.knownNodes[stream][peer] = { - "lastseen": seenTime, - "rating": 0, - "self": False, - } + knownnodes.addKnownNode(stream, peer, seenTime) + # since we don't track peers outside of knownnodes, # only spread if in knownnodes to prevent flood # DISABLED TO WORKAROUND FLOOD/LEAK diff --git a/src/network/tcp.py b/src/network/tcp.py index 18fd2e1a..45e7389b 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -256,12 +256,8 @@ class TCPConnection(BMProto, TLSDispatcher): TLSDispatcher.handle_read(self) if self.isOutbound and self.fullyEstablished: for s in self.streams: - try: - with knownnodes.knownNodesLock: - knownnodes.knownNodes[s][self.destination][ - "lastseen"] = time.time() - except KeyError: - pass + with knownnodes.knownNodesLock: + knownnodes.addKnownNode(s, self.destination, time.time()) receiveDataQueue.put(self.destination) def handle_write(self):