Add knownnodes only by knownnodes.addKnownNode(),

now it can update lastseen.
This commit is contained in:
Dmitri Bogomolov 2019-12-01 22:08:16 +02:00
parent 1699c2b015
commit 739ff7b439
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13
3 changed files with 24 additions and 20 deletions

View File

@ -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,
}

View File

@ -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

View File

@ -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):