From 8cff18f8ce02790b709f9c091a75571199437ba2 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sat, 2 Jul 2022 15:15:47 +0200 Subject: [PATCH] Improved cache handling --- RNS/Identity.py | 8 +++++++- RNS/Reticulum.py | 6 +++--- RNS/Transport.py | 48 +++++++++++++++++++++++++----------------------- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/RNS/Identity.py b/RNS/Identity.py index 5ee9cb3..7bddb4e 100644 --- a/RNS/Identity.py +++ b/RNS/Identity.py @@ -135,8 +135,14 @@ class Identity: if os.path.isfile(RNS.Reticulum.storagepath+"/known_destinations"): try: file = open(RNS.Reticulum.storagepath+"/known_destinations","rb") - Identity.known_destinations = umsgpack.load(file) + loaded_known_destinations = umsgpack.load(file) file.close() + + Identity.known_destinations = {} + for known_destination in loaded_known_destinations: + if len(known_destination) == RNS.Reticulum.TRUNCATED_HASHLENGTH//8: + Identity.known_destinations[known_destination] = loaded_known_destinations[known_destination] + RNS.log("Loaded "+str(len(Identity.known_destinations))+" known destination from storage", RNS.LOG_VERBOSE) except: RNS.log("Error loading known destinations from disk, file will be recreated on exit", RNS.LOG_ERROR) diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index 8e25c3c..a2a1b40 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -126,7 +126,7 @@ class Reticulum: MDU = MTU - HEADER_MAXSIZE - IFAC_MIN_SIZE - CACHE_TIME = 24*60*60 + RESOURCE_CACHE = 24*60*60 JOB_INTERVAL = 15*60 router = None @@ -893,7 +893,7 @@ class Reticulum: filepath = self.resourcepath + "/" + filename mtime = os.path.getmtime(filepath) age = now - mtime - if age > Reticulum.CACHE_TIME: + if age > Reticulum.RESOURCE_CACHE: os.unlink(filepath) except Exception as e: @@ -906,7 +906,7 @@ class Reticulum: filepath = self.cachepath + "/" + filename mtime = os.path.getmtime(filepath) age = now - mtime - if age > Reticulum.CACHE_TIME: + if age > RNS.Transport.DESTINATION_TIMEOUT: os.unlink(filepath) except Exception as e: diff --git a/RNS/Transport.py b/RNS/Transport.py index 4a4e298..319ba8e 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -68,7 +68,7 @@ class Transport: PATH_REQUEST_RW = 2 # Path request random window LINK_TIMEOUT = RNS.Link.STALE_TIME * 1.25 - REVERSE_TIMEOUT = 30*60 # Reverse table entries are removed after max 30 minutes + REVERSE_TIMEOUT = 30*60 # Reverse table entries are removed after 30 minutes DESTINATION_TIMEOUT = 60*60*24*7 # Destination table entries are removed if unused for one week MAX_RECEIPTS = 1024 # Maximum number of receipts to keep track of MAX_RATE_TIMESTAMPS = 16 # Maximum number of announce timestamps to keep per destination @@ -181,29 +181,31 @@ class Transport: for serialised_entry in serialised_destinations: destination_hash = serialised_entry[0] - timestamp = serialised_entry[1] - received_from = serialised_entry[2] - hops = serialised_entry[3] - expires = serialised_entry[4] - random_blobs = serialised_entry[5] - receiving_interface = Transport.find_interface_from_hash(serialised_entry[6]) - announce_packet = Transport.get_cached_packet(serialised_entry[7]) - if announce_packet != None and receiving_interface != None: - announce_packet.unpack() - # We increase the hops, since reading a packet - # from cache is equivalent to receiving it again - # over an interface. It is cached with it's non- - # increased hop-count. - announce_packet.hops += 1 - Transport.destination_table[destination_hash] = [timestamp, received_from, hops, expires, random_blobs, receiving_interface, announce_packet] - RNS.log("Loaded path table entry for "+RNS.prettyhexrep(destination_hash)+" from storage", RNS.LOG_DEBUG) - else: - RNS.log("Could not reconstruct path table entry from storage for "+RNS.prettyhexrep(destination_hash), RNS.LOG_DEBUG) - if announce_packet == None: - RNS.log("The announce packet could not be loaded from cache", RNS.LOG_DEBUG) - if receiving_interface == None: - RNS.log("The interface is no longer available", RNS.LOG_DEBUG) + if len(destination_hash) == RNS.Reticulum.TRUNCATED_HASHLENGTH//8: + timestamp = serialised_entry[1] + received_from = serialised_entry[2] + hops = serialised_entry[3] + expires = serialised_entry[4] + random_blobs = serialised_entry[5] + receiving_interface = Transport.find_interface_from_hash(serialised_entry[6]) + announce_packet = Transport.get_cached_packet(serialised_entry[7]) + + if announce_packet != None and receiving_interface != None: + announce_packet.unpack() + # We increase the hops, since reading a packet + # from cache is equivalent to receiving it again + # over an interface. It is cached with it's non- + # increased hop-count. + announce_packet.hops += 1 + Transport.destination_table[destination_hash] = [timestamp, received_from, hops, expires, random_blobs, receiving_interface, announce_packet] + RNS.log("Loaded path table entry for "+RNS.prettyhexrep(destination_hash)+" from storage", RNS.LOG_DEBUG) + else: + RNS.log("Could not reconstruct path table entry from storage for "+RNS.prettyhexrep(destination_hash), RNS.LOG_DEBUG) + if announce_packet == None: + RNS.log("The announce packet could not be loaded from cache", RNS.LOG_DEBUG) + if receiving_interface == None: + RNS.log("The interface is no longer available", RNS.LOG_DEBUG) if len(Transport.destination_table) == 1: specifier = "entry"