From d7749b0b06008800601d18cd7be6e87b648d619b Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Wed, 28 Oct 2026 17:34:16 +0100 Subject: [PATCH] Stamp config wiring, path request stamp timeout, destination stamp handling --- RNS/Destination.py | 19 +++++++++-- RNS/Reticulum.py | 52 ++++++++++++++++++++++++++++++ RNS/Transport.py | 33 ++++++++++++++----- RNS/Utilities/rnsd.py | 75 +++++++++++++++++++++++++++++++++++++++++++ docs/source/using.rst | 8 +++-- 5 files changed, 173 insertions(+), 14 deletions(-) diff --git a/RNS/Destination.py b/RNS/Destination.py index 8e962749..c44b3b70 100755 --- a/RNS/Destination.py +++ b/RNS/Destination.py @@ -162,6 +162,7 @@ class Destination: self.proof_strategy = Destination.PROVE_NONE self.stamp = None self.stamp_value = None + self._target_value = RNS.Transport.DESTINATION_VALUE self.link_cost = None self.ratchets = None self.ratchets_path = None @@ -315,11 +316,11 @@ class Destination: app_data = returned_app_data if RNS.Transport.STAMP_DESTINATIONS: - if self.stamp == None: - stamp_entry = load_destination_stamp(self.hash, self.stamp_material(), RNS.Transport.DESTINATION_COST) + if self.stamp == None or self.stamp_value == None or self.stamp_value < self.target_value: + stamp_entry = load_destination_stamp(self.hash, self.stamp_material(), self.target_value) if stamp_entry != None: self.stamp, self.stamp_value = stamp_entry else: - stamp, stamp_value = generate_stamp(self.stamp_material(), stamp_cost=RNS.Transport.DESTINATION_VALUE, + stamp, stamp_value = generate_stamp(self.stamp_material(), stamp_cost=self.target_value, expand_rounds=0, size=RNS.Identity.STAMPLENGTH) if stamp == None: RNS.log(f"Could not generate destination stamp for {RNS.prettyhexrep(self.hash)}, announce emission cancelled", RNS.LOG_WARNING) @@ -362,6 +363,18 @@ class Destination: def get_ext_flags(self): return 0x00 + @property + def target_value(self): + return min(RNS.Transport.STAMP_COST_CAP, max(int(self._target_value), RNS.Transport.DESTINATION_VALUE)) + + def set_value(self, value): + """ + Sets the target stamp value for this destination. + + :param value: The target value as an *integer*. + """ + self._target_value = min(RNS.Transport.STAMP_COST_CAP, max(int(value), RNS.Transport.DESTINATION_VALUE)) + def set_link_cost(self, cost): """ Sets the required link establishment stamp cost for this destination. Set to 0 to disable. diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index 54b67664..631e3c76 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -471,6 +471,54 @@ class Reticulum: value = self.config["logging"].as_bool(option) RNS.logtimestamps = bool(value) + if "stamps" in self.config: + for option in self.config["stamps"]: + if option == "enforce_stamps": + value = self.config["stamps"].as_bool(option) + if value == True: + RNS.Transport.STAMPS_ENFORCED = True + RNS.Transport.LR_STAMPS_ENFORCED = True + + if option == "stamp_destinations": + value = self.config["stamps"].as_bool(option) + if value == True: RNS.Transport.STAMP_DESTINATIONS = True + + if option == "stamp_prs": + value = self.config["stamps"].as_bool(option) + if value == True: RNS.Transport.STAMP_PRS = True + + if option == "stamp_lrs": + value = self.config["stamps"].as_bool(option) + if value == True: RNS.Transport.STAMP_LRS = True + + if option == "destination_cost": + v = self.config["stamps"].as_int(option) + RNS.Transport.DESTINATION_COST = max(0, min(v, RNS.Transport.STAMP_COST_CAP)) + + if option == "destination_value": + v = self.config["stamps"].as_int(option) + RNS.Transport.DESTINATION_VALUE = max(0, min(v, RNS.Transport.STAMP_COST_CAP)) + + if option == "pr_cost": + v = self.config["stamps"].as_int(option) + RNS.Transport.PR_COST = max(0, min(v, RNS.Transport.STAMP_COST_CAP)) + + if option == "pr_value": + v = self.config["stamps"].as_int(option) + RNS.Transport.PR_VALUE = max(0, min(v, RNS.Transport.STAMP_COST_CAP)) + + if option == "lr_cost": + v = self.config["stamps"].as_int(option) + RNS.Transport.LR_COST = max(0, min(v, RNS.Transport.LINK_COST_CAP)) + + if option == "lr_stamp_timeout": + v = self.config["stamps"].as_float(option) + RNS.Transport.LINK_STAMP_TIMEOUT = max(0.020, v) + + if option == "pr_stamp_timeout": + v = self.config["stamps"].as_float(option) + RNS.Transport.PR_STAMP_TIMEOUT = max(0.020, v) + if "reticulum" in self.config: for option in self.config["reticulum"]: value = self.config["reticulum"][option] @@ -509,6 +557,10 @@ class Reticulum: if option == "enable_transport": v = self.config["reticulum"].as_bool(option) if v == True: Reticulum.__transport_enabled = True + + if option == "hop_limiter": + v = self.config["reticulum"].as_int(option) + RNS.Transport.HOP_LIMITER = max(1, min(v, RNS.Transport.PATHFINDER_M)) if option == "static_transport_identity": v = self.config["reticulum"].as_bool(option) diff --git a/RNS/Transport.py b/RNS/Transport.py index da234183..54366c3d 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -45,7 +45,7 @@ from threading import Lock, Condition from .vendor import umsgpack as umsgpack from RNS.Interfaces.BackboneInterface import BackboneInterface from RNS.Storage import AnnounceRateTable, PacketHashlist, AnnounceCache -from RNS.Stamps import generate_stamp, stamp_workblock, stamp_valid, stamp_value +from RNS.Stamps import generate_stamp, stamp_workblock, stamp_valid, stamp_value, cancel_work class InboundQueues: __slots__ = ("_cond", "_queues", "_sizes", "_dropped", "_hw_mark") @@ -193,19 +193,20 @@ class Transport: LR_STAMPS_ENFORCED = False # Whether to enforce link request stamp requirements STAMP_DESTINATIONS = False # Whether to stamp destination announces emitted from this instance STAMP_PRS = False # Whether to stamp path requests emitted from this instance - STAMP_LRS = True # Whether to stamp link requests emitted from this instance + STAMP_LRS = False # Whether to stamp link requests emitted from this instance STAMP_COST_CAP = 100 # Global hard cap on stamp cost to ensure search-space viability at 14-byte stamp sizes LINK_COST_CAP = 40 # Global hard cap on link request cost, separate from detached stamps DESTINATION_COST = 23 # Default required announce cost if no specifics configured or determined - DESTINATION_VALUE = 23 # Default target stamp value for emitted announces + DESTINATION_VALUE = 25 # Default target stamp value for emitted announces PR_COST = 19 # Default required path request cost if no specifics configured or determined PR_VALUE = 19 # Default target stamp value for emitted path requests LR_COST = 11 # Default required link request cost if not explicitly announced or configured - LINK_STAMP_TIMEOUT = 15 # TODO: Provisional, set sensible default and wire to config - HOP_LIMITER = PATHFINDER_M # TODO: Locally configurable hop limiter, wire to config + LINK_STAMP_TIMEOUT = 15 # Default link request stamp generation timeout + PR_STAMP_TIMEOUT = 15 # Default path requset stamp generation timeout + HOP_LIMITER = PATHFINDER_M - STAMPER_PR_QUEUE_LENGTH = 32 # TODO: Provisional, wire to config handling - STAMPER_AN_QUEUE_LENGTH = 32 # TODO: Provisional, wire to config handling + STAMPER_PR_QUEUE_LENGTH = 32 # TODO: Provisional, wire to config + STAMPER_AN_QUEUE_LENGTH = 32 # TODO: Provisional, wire to config STAMP_PR_PRIORITY = 0 STAMP_DST_PRIORITY = 1 stamper_thread = None @@ -517,6 +518,8 @@ class Transport: # Load transport-related data if not Transport.owner.is_connected_to_shared_instance: Transport.init_announce_cache() + if Transport.HOP_LIMITER != Transport.PATHFINDER_M: RNS.log(f"Transport hop limit set to {Transport.HOP_LIMITER}", RNS.LOG_VERBOSE) + if RNS.Reticulum.transport_enabled(): path_table_path = RNS.Reticulum.storagepath+"/destination_table" tunnel_table_path = RNS.Reticulum.storagepath+"/tunnels" @@ -3970,8 +3973,22 @@ class Transport: material = Transport.pr_stamp_material(destination_hash, request_tag) with Transport.pr_stamp_cache_lock: stamp = Transport.pr_stamp_cache.get(material, None) - if not stamp: stamp, value = generate_stamp(material, stamp_cost=Transport.PR_VALUE, expand_rounds=0, size=RNS.Identity.STAMPLENGTH) + if not stamp: + stamp_status = None + stamp_job_id = material + def stamp_watchdog(): + deadline = time.time() + RNS.Transport.PR_STAMP_TIMEOUT + while stamp_status == None and time.time() <= deadline: time.sleep(1) + if time.time() > deadline and stamp_job_id and not stamp_status: cancel_work(stamp_job_id) + + threading.Thread(target=stamp_watchdog, daemon=True).start() + stamp, value = generate_stamp(material, stamp_cost=Transport.PR_VALUE, expand_rounds=0, size=RNS.Identity.STAMPLENGTH) + stamp_job_id = None + + if stamp: stamp_status = True + else: + stamp_status = False RNS.log(f"Could not generate path request stamp for {RNS.prettyhexrep(destination_hash)}, path request emission cancelled", RNS.LOG_WARNING) return diff --git a/RNS/Utilities/rnsd.py b/RNS/Utilities/rnsd.py index 24a3e35a..a24ab076 100755 --- a/RNS/Utilities/rnsd.py +++ b/RNS/Utilities/rnsd.py @@ -122,6 +122,7 @@ share_instance = yes instance_name = default + # Some platforms don't support domain sockets, and if that # is the case, you can isolate different instances by # specifying a unique set of ports for each: @@ -289,6 +290,12 @@ instance_name = default # blackhole_update_interval = 60 +# You can enable a hop limiter, that will drop all packets +# exceeding the specified maximum hop count. + +# hop_limiter = 16 + + # When not running as a transport node, it is possible to # force the use of the same, static transport identity # for every instance start. Defaults to using a new identity @@ -296,6 +303,74 @@ instance_name = default # static_transport_identity = no +[stamps] +# This section configures stamping behaviour and defaults. +# In most cases, you do not need to change this, but on +# constrained systems especially, tuning these parameters +# can be very beneficial + +# During the 1.6.x phased transition to network stamp usage, +# it is possible to enable early opt-in on stamp generation +# and enforcement. RNS 1.6.0 includes all the required stamp +# generation and verification functionality, and actively +# validates stamps, while leaving generation, emission and +# enforcement disabled by default. Version 1.6.1 will enable +# generation and emission by default, and version 1.6.2 will +# finally activate enforcement. Any option can be enabled +# early by configuration. + +# stamp_destinations = yes +# stamp_prs = yes +# stamp_lrs = yes +# enforce_stamps = yes + + +# The default required stamp cost for an announce, and its +# destination to be considered valid. This applies both to +# announces received directly, and transported by this node, +# if transport is enabled. Any announced destination not +# meeting the cost will be ignored. + +# destination_cost = 23 + + +# The default value this instance will target when generating +# new destination stamps. An application may set a higher +# target, but applications requesting a lower value than the +# default will always generate at least the default value. +# Destination stamps are cached and reused, so a one-time +# high-value generation is usually a good idea. + +# destination_value = 25 + + +# The default required path request stamp cost. Path requests +# that do not meet the requirement are ignored. + +# pr_cost = 19 + + +# The default target value of path request stamps generated +# by this instance. + +# pr_value = 19 + + +# The default link request stamp cost to use, if one has not +# been announced by the destination the link is established +# to, or if it is unknown. In practice, all destinations will +# include their required link cost in each announce, and this +# option is rarely necessary. + +# lr_cost = 11 + + +# The maximum amount of time to spend attempting to generate +# a link request and path request stamps, in seconds. + +# lr_stamp_timeout = 15 +# pr_stamp_timeout = 15 + [logging] # Valid log levels are 0 through 8: diff --git a/docs/source/using.rst b/docs/source/using.rst index 0333fb80..8cdb187b 100644 --- a/docs/source/using.rst +++ b/docs/source/using.rst @@ -193,12 +193,14 @@ change anything, and you may already be connected to a wider network. If not, you will probably need to add relevant *interfaces* to the configuration, in order to communicate with other systems. -You can generate a much more verbose configuration example by running the command: +The example above does not document all the available options, only the basics. +To explore all the various configuration options available, you can generate a +much more verbose and complete configuration example by running the command: ``rnsd --exampleconfig`` -The output includes examples for most interface types supported -by Reticulum, along with additional options and configuration parameters. +The output includes examples for most interface types supported by Reticulum, +along with additional options and configuration parameters. It is a good idea to read the comments and explanations in the above default config. It will teach you the basic concepts you need to understand to configure your network.