Implemented destination stamps

This commit is contained in:
Mark Qvist 2026-10-26 17:08:54 +01:00
parent 9a01a20ce6
commit d1da5c3d67
No known key found for this signature in database
4 changed files with 322 additions and 51 deletions

View file

@ -35,6 +35,7 @@ import threading
import RNS
from RNS.Cryptography import Token
from RNS.Stamps import generate_stamp, store_destination_stamp, load_destination_stamp
from .vendor import umsgpack as umsgpack
class Callbacks:
@ -159,6 +160,8 @@ class Destination:
self.type = type
self.direction = direction
self.proof_strategy = Destination.PROVE_NONE
self.stamp = None
self.stamp_value = None
self.ratchets = None
self.ratchets_path = None
self.ratchet_interval = Destination.RATCHET_INTERVAL
@ -190,6 +193,13 @@ class Destination:
self.name_hash = RNS.Identity.full_hash(self.expand_name(None, app_name, *aspects).encode("utf-8"))[:(RNS.Identity.NAME_HASH_LENGTH//8)]
self.hexhash = self.hash.hex()
# Load existing stamp if available
if self.direction == Destination.IN and self.identity != None and self.identity.prv != None and RNS.Transport.STAMP_DESTINATIONS:
stamp_entry = load_destination_stamp(self.hash, self.stamp_material(), RNS.Transport.DESTINATION_COST)
if stamp_entry != None:
self.stamp, self.stamp_value = stamp_entry
RNS.log(f"Loaded stamp with value {self.stamp_value} for {self}", RNS.LOG_DEBUG)
self.default_app_data = None
self.callback = None
self.proofcallback = None
@ -249,12 +259,20 @@ class Destination:
:param app_data: *bytes* containing the app_data.
:param path_response: Internal flag used by :ref:`RNS.Transport<api-transport>`. Ignore.
"""
if self.type != Destination.SINGLE:
raise TypeError("Only SINGLE destination types can be announced")
if self.type != Destination.SINGLE: raise TypeError("Only SINGLE destination types can be announced")
if self.direction != Destination.IN: raise TypeError("Only IN destination types can be announced")
if self.direction != Destination.IN:
raise TypeError("Only IN destination types can be announced")
if send and RNS.Transport.STAMP_DESTINATIONS and self.stamp == None:
# If no stamp is available, queue for background generation
# and announce emission once ready.
RNS.Transport.queue_stamped_announce(self, app_data=app_data, path_response=path_response,
attached_interface=attached_interface, tag=tag)
else:
# Otherwise, dispatch immediately.
return self._announce(app_data=app_data, path_response=path_response,
attached_interface=attached_interface, tag=tag, send=send)
def _announce(self, app_data=None, path_response=False, attached_interface=None, tag=None, send=True):
ratchet = b""
now = time.time()
stale_responses = []
@ -294,6 +312,26 @@ class Destination:
returned_app_data = self.default_app_data()
if isinstance(returned_app_data, bytes):
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 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,
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)
return
self.stamp = stamp
self.stamp_value = stamp_value
store_destination_stamp(self.hash, stamp, stamp_value)
RNS.log(f"Generated and persisted stamp with value {self.stamp_value} for {self}", RNS.LOG_DEBUG)
ext_payload = bytes([RNS.Identity.EXTPAYLOADMARKER, self.get_ext_flags()])+self.stamp
if app_data == None: app_data = ext_payload
else: app_data = app_data+ext_payload
signed_data = self.hash+self.identity.get_public_key()+self.name_hash+random_hash+ratchet
if app_data != None: signed_data += app_data
@ -317,6 +355,12 @@ class Destination:
if send: announce_packet.send()
else: return announce_packet
def stamp_material(self):
return self.hash+self.identity.get_public_key()+self.name_hash
def get_ext_flags(self):
return 0x00
def accepts_links(self, accepts = None):
"""
Set or query whether the destination accepts incoming link requests.

View file

@ -41,6 +41,7 @@ from .vendor import umsgpack as umsgpack
from RNS.Cryptography import X25519PrivateKey, X25519PublicKey, Ed25519PrivateKey, Ed25519PublicKey
from RNS.Cryptography import Token
from RNS.Storage import KnownDestinationsStore
from RNS.Stamps import stamp_workblock, stamp_valid, stamp_value
class Identity:
@ -80,6 +81,10 @@ class Identity:
AES256_BLOCKSIZE = 16 # In bytes
HASHLENGTH = 256 # In bits
SIGLENGTH = KEYSIZE # In bits
STAMPLENGTH = 14 # In bytes
EXTHEADERLENGTH = 2 # In bytes
EXTPAYLOADLENGTH = EXTHEADERLENGTH+STAMPLENGTH
EXTPAYLOADMARKER = 0xC1 # Never used at msgpack object boundaries
NAME_HASH_LENGTH = 80
TRUNCATED_HASHLENGTH = RNS.Reticulum.TRUNCATED_HASHLENGTH
@ -100,19 +105,21 @@ class Identity:
known_destinations_lock = threading.Lock()
@staticmethod
def remember(packet_hash, destination_hash, public_key, app_data = None):
if len(public_key) != Identity.KEYSIZE//8:
raise TypeError("Can't remember "+RNS.prettyhexrep(destination_hash)+", the public key size of "+str(len(public_key))+" is not valid.", RNS.LOG_ERROR)
def remember(packet_hash, destination_hash, public_key, app_data=None, stamp=None, stamp_value=0):
if stamp_value == None: stamp_value = 0
if len(public_key) != Identity.KEYSIZE//8: raise TypeError(f"Can't remember {RNS.prettyhexrep(destination_hash)}, the public key size of {len(public_key)} is not valid.", RNS.LOG_ERROR)
else:
with Identity.known_destinations_lock:
if not destination_hash in Identity.known_destinations:
Identity.known_destinations[destination_hash] = [time.time(), packet_hash, public_key, app_data, 0, None, 0]
Identity.known_destinations[destination_hash] = [time.time(), packet_hash, public_key, app_data, 0, stamp, stamp_value]
else:
entry = Identity.known_destinations[destination_hash]
entry[0] = time.time()
entry[1] = packet_hash
entry[2] = public_key
entry[3] = app_data
entry[5] = stamp
entry[6] = stamp_value
Identity._kd_mark_dirty(destination_hash)
@ -177,6 +184,18 @@ class Identity:
else: return None
@staticmethod
def recall_stamp_value(destination_hash):
"""
Recall last heard announce stamp value for a destination hash.
:param destination_hash: Destination hash as *bytes*.
:returns: *Integer*, or *None* if the destination is unknown.
"""
known_destination = Identity.known_destinations.get(destination_hash)
if not known_destination: return None
else: return known_destination[6]
@staticmethod
def save_known_destinations(background=False, recombine=False):
if recombine: RNS.log(f"Recombining known destinations from disk cache on persist is deprecated, argument ignored", RNS.LOG_WARNING)
@ -637,7 +656,8 @@ class Identity:
return None
@staticmethod
def validate_announce(packet, only_validate_signature=False, signal_blackholed=False):
def validate_announce(packet, only_validate_signature=False, signal_blackholed=False, signal_stamp=False, required_value=None):
if required_value == None: required_value = RNS.Transport.DESTINATION_COST
try:
if packet.packet_type == RNS.Packet.ANNOUNCE:
keysize = Identity.KEYSIZE//8
@ -649,11 +669,14 @@ class Identity:
# Get public key bytes from announce
public_key = packet.data[:keysize]
# Shared format structure
ratchet = b""
name_hash = packet.data[keysize:keysize+name_hash_len]
random_hash = packet.data[keysize+name_hash_len:keysize+name_hash_len+10]
# If the packet context flag is set,
# this announce contains a new ratchet
if packet.context_flag == RNS.Packet.FLAG_SET:
name_hash = packet.data[keysize:keysize+name_hash_len ]
random_hash = packet.data[keysize+name_hash_len:keysize+name_hash_len+10]
ratchet = packet.data[keysize+name_hash_len+10:keysize+name_hash_len+10+ratchetsize]
signature = packet.data[keysize+name_hash_len+10+ratchetsize:keysize+name_hash_len+10+ratchetsize+sig_len]
app_data = b""
@ -663,9 +686,6 @@ class Identity:
# If the packet context flag is not set,
# this announce does not contain a ratchet
else:
ratchet = b""
name_hash = packet.data[keysize:keysize+name_hash_len]
random_hash = packet.data[keysize+name_hash_len:keysize+name_hash_len+10]
signature = packet.data[keysize+name_hash_len+10:keysize+name_hash_len+10+sig_len]
app_data = b""
if len(packet.data) > keysize+name_hash_len+10+sig_len:
@ -676,6 +696,48 @@ class Identity:
if not len(packet.data) > Identity.KEYSIZE//8+Identity.NAME_HASH_LENGTH//8+10+Identity.SIGLENGTH//8:
app_data = None
stamp = None
dst_stamped = None
dst_value = None
stamp_validated = None
if RNS.Transport.STAMPS_ENABLED and app_data and len(app_data) >= Identity.EXTPAYLOADLENGTH:
ext_payload = app_data[-Identity.EXTPAYLOADLENGTH:]
ext_header = ext_payload[:Identity.EXTHEADERLENGTH]
ext_flags = ext_header[1] # Reserved for future extensions
if not ext_header[0] == Identity.EXTPAYLOADMARKER: dst_stamped = False
else:
# RNS.log(f"Checking potential stamp in announce with app_data len {len(app_data)} for {RNS.prettyhexrep(destination_hash)}", RNS.LOG_DEBUG) # TODO: Remove
dst_stamped = True
try:
stamp = ext_payload[Identity.EXTHEADERLENGTH:Identity.EXTHEADERLENGTH+Identity.STAMPLENGTH]
material = destination_hash+public_key+name_hash
wb = stamp_workblock(material, expand_rounds=0)
valid = stamp_valid(stamp, required_value, wb)
if valid:
app_data = app_data[:-Identity.EXTPAYLOADLENGTH]
if not len(app_data): app_data = None
stamp_validated = True
dst_value = stamp_value(wb, stamp)
except Exception as e: RNS.log(f"Announce stamp parsing failed: {e}", RNS.LOG_ERROR)
# TODO: Initial debug only, remove ######
# if RNS.sl(RNS.LOG_DEBUG):
# if not dst_stamped: RNS.log(f"No stamp on announce from {RNS.prettyhexrep(destination_hash)}", RNS.LOG_DEBUG)
# else:
# if not stamp_validated: RNS.log(f"Invalid or insufficient-value stamp for {RNS.prettyhexrep(destination_hash)}", RNS.LOG_DEBUG)
# else: RNS.log(f"Valid stamp with value {dst_value} for {RNS.prettyhexrep(destination_hash)}", RNS.LOG_DEBUG)
#########################################
if RNS.Transport.STAMPS_ENABLED and RNS.Transport.STAMPS_ENFORCED:
if not dst_stamped:
if signal_stamp: return "no_stamp"
else: return False
elif not stamp_validated:
if signal_stamp: return "invalid_stamp"
else: return False
announced_identity = Identity(create_keys=False)
announced_identity.load_public_key(public_key)
@ -705,7 +767,9 @@ class Identity:
RNS.log("This may indicate an attempt to modify network paths, or a random hash collision. The announce was rejected.", RNS.LOG_CRITICAL)
return False
RNS.Identity.remember(packet.get_hash(), destination_hash, public_key, app_data)
RNS.Identity.remember(packet.get_hash(), destination_hash, public_key,
app_data=app_data, stamp=stamp, stamp_value=dst_value)
del announced_identity
if packet.rssi != None or packet.snr != None:

View file

@ -39,12 +39,12 @@ import itertools
import contextlib
import multiprocessing
WORKBLOCK_EXPAND_ROUNDS = 3000
STAMP_SIZE = RNS.Identity.HASHLENGTH//8
PN_VALIDATION_POOL_MIN_SIZE = 256
USE_WORKER_MANAGER = False
POOLING_THRESHOLD_COST = 15
STOP_CHECK_INTERVAL = 64
WORKBLOCK_EXPAND_ROUNDS = 3000
STAMP_SIZE = 32
PN_VALIDATION_POOL_MIN_SIZE = 256
USE_WORKER_MANAGER = False
POOLING_THRESHOLD_COST = 15
STOP_CHECK_INTERVAL = 64
active_jobs = {}
@ -89,15 +89,8 @@ def stamp_workblock(material, expand_rounds=WORKBLOCK_EXPAND_ROUNDS, no_expand=F
return workblock
def stamp_value(workblock, stamp):
value = 0
bits = 256
material = RNS.Identity.full_hash(workblock+stamp)
i = int.from_bytes(material, byteorder="big")
while ((i & (1 << (bits - 1))) == 0):
i = (i << 1)
value += 1
return value
return 256 - int.from_bytes(material, byteorder="big").bit_length()
def stamp_valid(stamp, target_cost, workblock):
target = 0b1 << 256-target_cost
@ -105,8 +98,9 @@ def stamp_valid(stamp, target_cost, workblock):
if int.from_bytes(result, byteorder="big") > target: return False
else: return True
def generate_stamp(message_id, stamp_cost, expand_rounds=WORKBLOCK_EXPAND_ROUNDS, no_expand=False):
RNS.log(f"Generating stamp with cost {stamp_cost} for {RNS.prettyhexrep(message_id)}...", RNS.LOG_DEBUG)
def generate_stamp(message_id, stamp_cost, expand_rounds=WORKBLOCK_EXPAND_ROUNDS, no_expand=False, size=STAMP_SIZE):
if stamp_cost < 0 or stamp_cost > RNS.Transport.STAMP_COST_CAP: raise ValueError("Invalid stamp cost, cannot generate")
RNS.log(f"Generating stamp with cost {stamp_cost} for <{RNS.hexrep(message_id[:16], delimit=False)}{'…' if len(message_id) > 16 else ''}>", RNS.LOG_DEBUG)
workblock = stamp_workblock(message_id, expand_rounds=expand_rounds, no_expand=no_expand)
start_time = time.time()
@ -115,13 +109,13 @@ def generate_stamp(message_id, stamp_cost, expand_rounds=WORKBLOCK_EXPAND_ROUNDS
value = 0
if RNS.vendor.platformutils.is_windows() or RNS.vendor.platformutils.is_darwin():
RNS.log(f"Running stamp generation on {platform}, work limited to single CPU core. This will be slower than ideal.", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
RNS.log(f"Multi-core stamp generation on this platform is not supported, work limited to single CPU core.", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
if RNS.vendor.platformutils.is_windows() or RNS.vendor.platformutils.is_darwin(): stamp, rounds = job_simple(stamp_cost, workblock, message_id)
elif RNS.vendor.platformutils.is_android(): stamp, rounds = job_android(stamp_cost, workblock, message_id)
if RNS.vendor.platformutils.is_windows() or RNS.vendor.platformutils.is_darwin(): stamp, rounds = job_simple(stamp_cost, workblock, message_id, size)
elif RNS.vendor.platformutils.is_android(): stamp, rounds = job_android(stamp_cost, workblock, message_id, size)
else:
if USE_WORKER_MANAGER: stamp, rounds = job_linux_managed(stamp_cost, workblock, message_id) if stamp_cost >= POOLING_THRESHOLD_COST else job_simple(stamp_cost, workblock, message_id)
else: stamp, rounds = job_linux(stamp_cost, workblock, message_id) if stamp_cost >= POOLING_THRESHOLD_COST else job_simple(stamp_cost, workblock, message_id)
if USE_WORKER_MANAGER: stamp, rounds = job_linux_managed(stamp_cost, workblock, message_id, size) if stamp_cost >= POOLING_THRESHOLD_COST else job_simple(stamp_cost, workblock, message_id, size)
else: stamp, rounds = job_linux(stamp_cost, workblock, message_id, size) if stamp_cost >= POOLING_THRESHOLD_COST else job_simple(stamp_cost, workblock, message_id, size)
duration = max(time.time()-start_time, 1E-6)
speed = rounds/duration
@ -163,14 +157,14 @@ def cancel_work(message_id):
RNS.log("Error while terminating stamp generation workers: {e}", RNS.LOG_ERROR)
RNS.trace_exception(e)
def job_simple(stamp_cost, workblock, message_id):
def job_simple(stamp_cost, workblock, message_id, size):
# A simple, single-process stamp generator.
# should work on any platform, and is used
# as a fall-back, in case of limited multi-
# processing and/or acceleration support.
rounds = 0
pstamp = os.urandom(256//8)
pstamp = os.urandom(size)
st = time.time()
active_jobs[message_id] = False
@ -185,7 +179,7 @@ def job_simple(stamp_cost, workblock, message_id):
if active_jobs[message_id]: break
if int.from_bytes(RNS.Identity.full_hash(workblock+pstamp), byteorder="big") <= target: break
pstamp = os.urandom(256//8); rounds += 1
pstamp = os.urandom(size); rounds += 1
if rounds % 250000 == 0:
speed = rounds / max(time.time()-st, 1E-6)
@ -195,7 +189,7 @@ def job_simple(stamp_cost, workblock, message_id):
active_jobs.pop(message_id)
return (None if cancelled else pstamp), rounds
def job_linux_managed(stamp_cost, workblock, message_id):
def job_linux_managed(stamp_cost, workblock, message_id, size):
ctx = multiprocessing.get_context("fork")
cores = multiprocessing.cpu_count()
jobs = cores if cores <= 12 else int(cores/2)
@ -208,7 +202,7 @@ def job_linux_managed(stamp_cost, workblock, message_id):
RNS.log(f"Starting {jobs} stamp generation workers", RNS.LOG_DEBUG)
def job(stop_event, sc, wb, pn):
rounds = 0; pstamp = os.urandom(256//8);
rounds = 0; pstamp = os.urandom(size);
target = 0b1<<256 - sc; c = 0
while True:
@ -219,7 +213,7 @@ def job_linux_managed(stamp_cost, workblock, message_id):
m = wb+pstamp
if int.from_bytes(RNS.Identity.full_hash(m), byteorder="big") <= target: break
pstamp = os.urandom(256//8); rounds += 1
pstamp = os.urandom(size); rounds += 1
if not stop_event.is_set():
stop_event.set()
@ -278,7 +272,7 @@ def job_linux_managed(stamp_cost, workblock, message_id):
return stamp, total_rounds
def job_linux(stamp_cost, workblock, message_id):
def job_linux(stamp_cost, workblock, message_id, size):
allow_kill = True
stamp = None
total_rounds = 0
@ -289,7 +283,7 @@ def job_linux(stamp_cost, workblock, message_id):
rounds_queue = multiprocessing.Queue()
def job(stop_event, pn, sc, wb):
rounds = 0; pstamp = os.urandom(256//8);
rounds = 0; pstamp = os.urandom(size);
target = 0b1<<256 - sc; c = 0
while True:
@ -300,7 +294,7 @@ def job_linux(stamp_cost, workblock, message_id):
m = wb+pstamp
if int.from_bytes(RNS.Identity.full_hash(m), byteorder="big") <= target: break
pstamp = os.urandom(256//8); rounds += 1
pstamp = os.urandom(size); rounds += 1
if not stop_event.is_set():
stop_event.set()
@ -361,7 +355,7 @@ def job_linux(stamp_cost, workblock, message_id):
return stamp, total_rounds
def job_android(stamp_cost, workblock, message_id):
def job_android(stamp_cost, workblock, message_id, size):
# Semaphore support is flaky to non-existent on
# Android, so we need to manually dispatch and
# manage workloads here, while periodically
@ -395,7 +389,7 @@ def job_android(stamp_cost, workblock, message_id):
fh = full_hash
while True:
pstamp = os.urandom(256//8)
pstamp = os.urandom(size)
rounds += 1
if int.from_bytes(fh(wb+pstamp), byteorder="big") <= target:
found_stamp = pstamp; break
@ -442,3 +436,70 @@ def job_android(stamp_cost, workblock, message_id):
active_jobs.pop(message_id)
return stamp, total_rounds
###########################
# Persistence and loading #
###########################
DESTINATION_STAMP_EXTENSION = ".dstamp"
STAMP_FILE_VERSION = 1
def stamps_dir(create=False):
storagepath = RNS.Reticulum.storagepath
if not storagepath: return None
path = os.path.join(storagepath, "stamps")
if create and not os.path.isdir(path):
try: os.makedirs(path, exist_ok=True)
except Exception as e:
RNS.log(f"An error occurred while creating destination stamp storage path: {e}", RNS.LOG_WARNING)
return None
return path
def destination_stamp_file(destination_hash, create=False):
dir_path = stamps_dir(create=create)
if dir_path == None: return None
return os.path.join(dir_path, RNS.hexrep(destination_hash, delimit=False)+DESTINATION_STAMP_EXTENSION)
def store_destination_stamp(destination_hash, stamp, stamp_value):
file_path = destination_stamp_file(destination_hash, create=True)
if file_path == None:
RNS.log("No storage path available, destination stamp not persisted", RNS.LOG_WARNING)
return False
try:
data = msgpack.packb({"v": STAMP_FILE_VERSION, "stamp": stamp,
"value": stamp_value, "generated": time.time()})
temp_path = file_path+f".{os.getpid()}.tmp"
with open(temp_path, "wb") as temp_file: temp_file.write(data)
os.replace(temp_path, file_path)
return True
except Exception as e:
RNS.log(f"An error occurred while persisting destination stamp: {e}", RNS.LOG_WARNING)
return False
def load_destination_stamp(destination_hash, material, required_value):
file_path = destination_stamp_file(destination_hash)
if file_path == None or not os.path.isfile(file_path): return None
try:
with open(file_path, "rb") as stamp_file: entry = msgpack.unpackb(stamp_file.read())
if entry.get("v") != STAMP_FILE_VERSION: return None
workblock = stamp_workblock(material, expand_rounds=0)
stamp = entry["stamp"]
if not isinstance(stamp, bytes) or len(stamp) != RNS.Identity.STAMPLENGTH:
RNS.log(f"Stored stamp for {RNS.prettyhexrep(destination_hash)} has invalid format. Regenerating on next announce.", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
return None
value = stamp_value(workblock, stamp)
if value < required_value:
RNS.log(f"Stored stamp for {RNS.prettyhexrep(destination_hash)} has value {value}, below required {required_value}. Regenerating on next announce.", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
return None
return (stamp, value)
except Exception as e:
RNS.log(f"Ignoring unreadable destination stamp file for {RNS.prettyhexrep(destination_hash)}: {e}", RNS.LOG_WARNING)
return None

View file

@ -95,6 +95,46 @@ class InboundQueues:
dropped = tuple(d for d in self._dropped)
return (sum(heights), heights, dropped)
class WorkerQueues:
__slots__ = ("_cond", "_queues", "_sizes")
def __init__(self, sizes):
self._cond = Condition()
self._queues = [deque() for _ in sizes]
self._sizes = sizes
def put(self, priority, key, item):
with self._cond:
q = self._queues[priority]
if key != None:
i = 0
while i < len(q):
if q[i][0] == key: del q[i]
else: i += 1
if len(q) >= self._sizes[priority]: raise Full
q.append((key, item))
self._cond.notify()
def get(self, block=True, timeout=None):
deadline = None if timeout is None else time.monotonic() + timeout
with self._cond:
while True:
# Scan queues in priority order
for q in self._queues:
if q: return q.popleft()
if not block: raise Empty
remaining = None if deadline is None else deadline - time.monotonic()
if remaining is not None and remaining <= 0: raise Empty
self._cond.wait(remaining)
def qsize(self, priority=None):
with self._cond:
if priority != None: return len(self._queues[priority])
else: return sum(len(q) for q in self._queues)
class Transport:
"""
Through static methods of this class you can interact with the
@ -147,6 +187,24 @@ class Transport:
INBOUND_PR_QUEUE_LENGTH = 128
INBOUND_IL_QUEUE_LENGTH = 8
STAMPS_ENABLED = True # Whether to check for and validate stamps on announces and path requests
STAMPS_ENFORCED = False # Whether to enforce 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_COST_CAP = 100 # Global hard cap on stamp cost to ensure search-space viability at 14-byte stamp sizes
DESTINATION_COST = 18 # Default required announce cost if no specifics configured or determined
DESTINATION_VALUE = 18 # Default target stamp value for emitted announces
PR_COST = 18 # Default required path request cost if no specifics configured or determined
PR_VALUE = 18 # Default target stamp value for emitted path requests
STAMPER_PR_QUEUE_LENGTH = 32 # TODO: Provisional, wire to config handling
STAMPER_AN_QUEUE_LENGTH = 32 # TODO: Provisional, wire to config handling
STAMP_PR_PRIORITY = 0
STAMP_DST_PRIORITY = 1
stamper_thread = None
stamper_thread_lock = Lock()
stamp_queues = WorkerQueues((STAMPER_PR_QUEUE_LENGTH, STAMPER_AN_QUEUE_LENGTH))
STATE_UNKNOWN = 0x00
STATE_UNRESPONSIVE = 0x01
STATE_RESPONSIVE = 0x02
@ -581,6 +639,9 @@ class Transport:
if Transport.USE_INBOUND_QUEUE: threading.Thread(target=Transport.inbound_job, daemon=True).start()
if Transport.USE_OUTBOUND_QUEUE: threading.Thread(target=Transport.outbound_job, daemon=True).start()
# Spawn the stamp worker
Transport.ensure_stamper()
Transport.ready = True
# Synthesize tunnels for any interfaces wanting it
@ -1335,6 +1396,41 @@ class Transport:
RNS.log(f"Error while finalizing transport jobs: {e}", RNS.LOG_ERROR)
RNS.trace_exception(e)
@staticmethod
def ensure_stamper():
if Transport.stamper_thread != None and Transport.stamper_thread.is_alive(): return
with Transport.stamper_thread_lock:
if Transport.stamper_thread == None or not Transport.stamper_thread.is_alive():
Transport.stamper_thread = threading.Thread(target=Transport.stamper_job, daemon=True)
RNS.log("Starting announce stamper worker", RNS.LOG_DEBUG)
Transport.stamper_thread.start()
@staticmethod
def queue_stamped_announce(destination, app_data=None, path_response=False, attached_interface=None, tag=None):
priority = Transport.STAMP_PR_PRIORITY if path_response else Transport.STAMP_DST_PRIORITY
key = None if path_response else destination.hash
try: Transport.stamp_queues.put(priority, key, (destination, app_data, path_response, attached_interface, tag))
except Full:
RNS.log(f"Stamp queue full, dropping announce for {RNS.prettyhexrep(destination.hash)}", RNS.LOG_DEBUG)
return
Transport.ensure_stamper()
@staticmethod
def stamper_job():
while Transport._should_run:
try: key, job = Transport.stamp_queues.get(block=True, timeout=1)
except Empty: continue
destination, app_data, path_response, attached_interface, tag = job
try: destination._announce(app_data=app_data, path_response=path_response,
attached_interface=attached_interface, tag=tag, send=True)
except Exception as e:
RNS.log(f"Error while stamping and emitting announce for {RNS.prettyhexrep(destination.hash)}: {e}", RNS.LOG_ERROR)
RNS.trace_exception(e)
discovery_pr_tx_throttle = 0.5
discovery_pr_tx_lock = Lock()
discovery_pr_handle_lock = Lock()
@ -1898,8 +1994,10 @@ class Transport:
if packet.packet_type == RNS.Packet.ANNOUNCE:
if len(raw) > RNS.Reticulum.MTU: return packet.receiving_interface.protocol_violation(f"Excessive announce packet frame size of {len(raw)} bytes") if packet.receiving_interface else None
if traffic_class < Transport.TC_ANNOUNCE: traffic_class = Transport.TC_ANNOUNCE
announce_signature_valid = RNS.Identity.validate_announce(packet, only_validate_signature=True, signal_blackholed=True)
if announce_signature_valid == "blackholed": return None
announce_signature_valid = RNS.Identity.validate_announce(packet, only_validate_signature=True, signal_blackholed=True, signal_stamp=True)
if announce_signature_valid == "blackholed": return None
elif announce_signature_valid == "no_stamp": return None
elif announce_signature_valid == "invalid_stamp": return None
elif not announce_signature_valid:
return packet.receiving_interface.protocol_violation(f"Invalid announce signature for {RNS.prettyhexrep(packet.destination_hash)}") if packet.receiving_interface else None
@ -2546,7 +2644,11 @@ class Transport:
with Transport.path_table_lock: Transport.path_table[packet.destination_hash] = path_table_entry
Transport.mark_path_unknown_state(packet.destination_hash)
RNS.log("Destination "+RNS.prettyhexrep(packet.destination_hash)+" is now "+str(announce_hops)+" hops away via "+RNS.prettyhexrep(received_from)+" on "+str(packet.receiving_interface), RNS.LOG_PATHING) if RNS.sl(RNS.LOG_PATHING) else None
if RNS.sl(RNS.LOG_PATHING):
destination_value = RNS.Identity.recall_stamp_value(packet.destination_hash)
value_str = f" (value {destination_value})" if destination_value else " (unstamped)"
RNS.log(f"Destination {RNS.prettyhexrep(packet.destination_hash)}{value_str} is now {announce_hops} hops away via {RNS.prettyhexrep(received_from)} on {packet.receiving_interface}", RNS.LOG_PATHING)
if packet.destination_hash in Transport.path_requests:
RNS.Reticulum.get_instance()._used_destination_data(packet.destination_hash)