mirror of
https://github.com/markqvist/Reticulum.git
synced 2026-11-19 14:36:34 -05:00
Added link establishment cost to announces
This commit is contained in:
parent
c3af19893f
commit
30c6428e0b
4 changed files with 79 additions and 18 deletions
|
|
@ -162,6 +162,7 @@ class Destination:
|
|||
self.proof_strategy = Destination.PROVE_NONE
|
||||
self.stamp = None
|
||||
self.stamp_value = None
|
||||
self.link_cost = None
|
||||
self.ratchets = None
|
||||
self.ratchets_path = None
|
||||
self.ratchet_interval = Destination.RATCHET_INTERVAL
|
||||
|
|
@ -329,7 +330,7 @@ class Destination:
|
|||
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
|
||||
ext_payload = bytes([RNS.Identity.EXTPAYLOADMARKER, self.get_ext_flags()])+self.stamp+bytes([self.get_link_cost()])
|
||||
if app_data == None: app_data = ext_payload
|
||||
else: app_data = app_data+ext_payload
|
||||
|
||||
|
|
@ -361,6 +362,18 @@ class Destination:
|
|||
def get_ext_flags(self):
|
||||
return 0x00
|
||||
|
||||
def get_link_cost(self):
|
||||
"""
|
||||
Returns the link request stamp cost to be announced for this
|
||||
destination, as an integer. If no destination-specific cost has
|
||||
been set, the default link request cost is returned.
|
||||
|
||||
:returns: The announced link request cost as an *integer*.
|
||||
"""
|
||||
cost = self.link_cost
|
||||
if cost == None: cost = RNS.Transport.LR_COST
|
||||
return max(0, min(int(cost), RNS.Transport.STAMP_COST_CAP))
|
||||
|
||||
def accepts_links(self, accepts = None):
|
||||
"""
|
||||
Set or query whether the destination accepts incoming link requests.
|
||||
|
|
|
|||
|
|
@ -84,7 +84,8 @@ class Identity:
|
|||
SIGLENGTH = KEYSIZE # In bits
|
||||
STAMPLENGTH = 14 # In bytes
|
||||
EXTHEADERLENGTH = 2 # In bytes
|
||||
EXTPAYLOADLENGTH = EXTHEADERLENGTH+STAMPLENGTH
|
||||
LRCOSTLENGTH = 1 # In bytes
|
||||
EXTPAYLOADLENGTH = EXTHEADERLENGTH+STAMPLENGTH+LRCOSTLENGTH
|
||||
EXTPAYLOADMARKER = 0xC1 # Never used at msgpack object boundaries
|
||||
|
||||
TRUNCATED_HASHLENGTH = RNS.Reticulum.TRUNCATED_HASHLENGTH
|
||||
|
|
@ -104,13 +105,13 @@ class Identity:
|
|||
known_destinations_lock = threading.Lock()
|
||||
|
||||
@staticmethod
|
||||
def remember(packet_hash, destination_hash, public_key, app_data=None, stamp=None, stamp_value=0):
|
||||
def remember(packet_hash, destination_hash, public_key, app_data=None, stamp=None, stamp_value=0, lr_cost=None):
|
||||
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, stamp, stamp_value]
|
||||
Identity.known_destinations[destination_hash] = [time.time(), packet_hash, public_key, app_data, 0, stamp, stamp_value, lr_cost]
|
||||
else:
|
||||
entry = Identity.known_destinations[destination_hash]
|
||||
entry[0] = time.time()
|
||||
|
|
@ -119,6 +120,7 @@ class Identity:
|
|||
entry[3] = app_data
|
||||
entry[5] = stamp
|
||||
entry[6] = stamp_value
|
||||
entry[7] = lr_cost
|
||||
|
||||
Identity._kd_mark_dirty(destination_hash)
|
||||
|
||||
|
|
@ -195,6 +197,18 @@ class Identity:
|
|||
if not known_destination: return None
|
||||
else: return known_destination[6]
|
||||
|
||||
@staticmethod
|
||||
def recall_link_cost(destination_hash):
|
||||
"""
|
||||
Recall the link request stamp cost announced by a destination.
|
||||
|
||||
:param destination_hash: Destination hash as *bytes*.
|
||||
:returns: *Integer*, or *None* if the destination is unknown or has not announced a cost.
|
||||
"""
|
||||
known_destination = Identity.known_destinations.get(destination_hash)
|
||||
if not known_destination: return None
|
||||
else: return known_destination[7]
|
||||
|
||||
@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)
|
||||
|
|
@ -318,7 +332,11 @@ class Identity:
|
|||
if len(known_destination) == RNS.Reticulum.TRUNCATED_HASHLENGTH//8:
|
||||
if len(loaded_known_destinations[known_destination]) < 5:
|
||||
e = loaded_known_destinations[known_destination]
|
||||
loaded_known_destinations[known_destination] = [e[0], e[1], e[2], e[3], 0]
|
||||
loaded_known_destinations[known_destination] = [e[0], e[1], e[2], e[3], 0, None, 0, None]
|
||||
elif len(loaded_known_destinations[known_destination]) < 8:
|
||||
e = loaded_known_destinations[known_destination]
|
||||
while len(e) < 8: e.append(None if len(e) in (5, 7) else 0)
|
||||
loaded_known_destinations[known_destination] = e
|
||||
|
||||
with Identity.known_destinations_lock:
|
||||
Identity.known_destinations[known_destination] = loaded_known_destinations[known_destination]
|
||||
|
|
@ -699,11 +717,26 @@ class Identity:
|
|||
dst_stamped = None
|
||||
dst_value = None
|
||||
stamp_validated = None
|
||||
dst_lr_cost = 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
|
||||
ext_anchor = ext_payload[-1]
|
||||
|
||||
# The trailing anchor byte carries the announced link request stamp
|
||||
# cost in its 7 least significant bits, and an extension marker in
|
||||
# its most significant bit. If the extension marker is set, the
|
||||
# ext_payload is of a newer and potentially longer format, parsed
|
||||
# through versioned logic not yet implemented in this version, and
|
||||
# must not be interpreted here. As long as announced costs cannot
|
||||
# exceed STAMP_COST_CAP, valid announce ext_payloads will always
|
||||
# present with the extension marker cleared.
|
||||
ext_extended = (ext_anchor & 0x80) != 0
|
||||
ext_lr_cost = ext_anchor & 0x7F
|
||||
known_format = (not ext_extended) and (ext_lr_cost <= RNS.Transport.STAMP_COST_CAP)
|
||||
|
||||
if (not ext_header[0] == Identity.EXTPAYLOADMARKER) or (not known_format): 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
|
||||
|
|
@ -718,6 +751,7 @@ class Identity:
|
|||
if not len(app_data): app_data = None
|
||||
stamp_validated = True
|
||||
dst_value = stamp_value(wb, stamp)
|
||||
dst_lr_cost = ext_lr_cost
|
||||
|
||||
except Exception as e: RNS.log(f"Announce stamp parsing failed: {e}", RNS.LOG_ERROR)
|
||||
|
||||
|
|
@ -767,7 +801,7 @@ class Identity:
|
|||
return False
|
||||
|
||||
RNS.Identity.remember(packet.get_hash(), destination_hash, public_key,
|
||||
app_data=app_data, stamp=stamp, stamp_value=dst_value)
|
||||
app_data=app_data, stamp=stamp, stamp_value=dst_value, lr_cost=dst_lr_cost)
|
||||
|
||||
del announced_identity
|
||||
|
||||
|
|
|
|||
|
|
@ -243,6 +243,8 @@ class KnownDestinationsStore:
|
|||
self._conn = None
|
||||
|
||||
def __create_schema(self):
|
||||
db_version = self._conn.execute("PRAGMA user_version").fetchone()[0]
|
||||
|
||||
self._conn.execute("""CREATE TABLE IF NOT EXISTS known_destinations (
|
||||
destination_hash BLOB PRIMARY KEY,
|
||||
last_heard REAL NOT NULL,
|
||||
|
|
@ -252,6 +254,7 @@ class KnownDestinationsStore:
|
|||
used REAL NOT NULL DEFAULT 0,
|
||||
stamp BLOB,
|
||||
stamp_value INTEGER,
|
||||
lr_cost INTEGER,
|
||||
identity_hash BLOB NOT NULL)""")
|
||||
|
||||
self._conn.execute("""CREATE TABLE IF NOT EXISTS ratchets (
|
||||
|
|
@ -259,14 +262,21 @@ class KnownDestinationsStore:
|
|||
ratchet BLOB NOT NULL,
|
||||
received REAL NOT NULL)""")
|
||||
|
||||
self._conn.execute("PRAGMA user_version = 1")
|
||||
if 0 < db_version < 2:
|
||||
# Version 2 adds the announced link request cost column. On migrated
|
||||
# databases this column trails identity_hash, so all writes must name
|
||||
# their target columns explicitly.
|
||||
self._conn.execute("ALTER TABLE known_destinations ADD COLUMN lr_cost INTEGER")
|
||||
|
||||
self._conn.execute("PRAGMA user_version = 2")
|
||||
self._conn.commit()
|
||||
|
||||
def __entry_row(self, destination_hash, entry):
|
||||
stamp = entry[5] if len(entry) > 5 else None
|
||||
stamp_value = entry[6] if len(entry) > 6 else 0
|
||||
lr_cost = entry[7] if len(entry) > 7 else None
|
||||
identity_hash = hashlib.sha256(entry[2]).digest()[:self.hash_width]
|
||||
return (destination_hash, entry[0], entry[1], entry[2], entry[3], entry[4], stamp, stamp_value, identity_hash)
|
||||
return (destination_hash, entry[0], entry[1], entry[2], entry[3], entry[4], stamp, stamp_value, lr_cost, identity_hash)
|
||||
|
||||
# Caller must hold Identity.known_destinations_lock
|
||||
def mark_dirty(self, destination_hash):
|
||||
|
|
@ -321,7 +331,7 @@ class KnownDestinationsStore:
|
|||
|
||||
try:
|
||||
self._conn.execute("BEGIN")
|
||||
for i in range(0, len(rows), 4096): self._conn.executemany("INSERT INTO known_destinations VALUES (?,?,?,?,?,?,?,?,?) ON CONFLICT(destination_hash) DO UPDATE SET last_heard=excluded.last_heard, packet_hash=excluded.packet_hash, public_key=excluded.public_key, app_data=excluded.app_data, used=excluded.used, stamp=excluded.stamp, stamp_value=excluded.stamp_value, identity_hash=excluded.identity_hash", rows[i:i+4096])
|
||||
for i in range(0, len(rows), 4096): self._conn.executemany("INSERT INTO known_destinations (destination_hash, last_heard, packet_hash, public_key, app_data, used, stamp, stamp_value, lr_cost, identity_hash) VALUES (?,?,?,?,?,?,?,?,?,?) ON CONFLICT(destination_hash) DO UPDATE SET last_heard=excluded.last_heard, packet_hash=excluded.packet_hash, public_key=excluded.public_key, app_data=excluded.app_data, used=excluded.used, stamp=excluded.stamp, stamp_value=excluded.stamp_value, lr_cost=excluded.lr_cost, identity_hash=excluded.identity_hash", rows[i:i+4096])
|
||||
for i in range(0, len(removables), 4096): self._conn.executemany("DELETE FROM known_destinations WHERE destination_hash=?", removables[i:i+4096])
|
||||
for i in range(0, len(ratchet_rows), 4096): self._conn.executemany("INSERT INTO ratchets VALUES (?,?,?) ON CONFLICT(destination_hash) DO UPDATE SET ratchet=excluded.ratchet, received=excluded.received", ratchet_rows[i:i+4096])
|
||||
for i in range(0, len(ratchet_removables), 4096): self._conn.executemany("DELETE FROM ratchets WHERE destination_hash=?", ratchet_removables[i:i+4096])
|
||||
|
|
@ -340,11 +350,11 @@ class KnownDestinationsStore:
|
|||
return len(rows) + len(removables) + len(ratchet_rows) + len(ratchet_removables)
|
||||
|
||||
def load_all(self):
|
||||
cursor = self._conn.execute("SELECT destination_hash, last_heard, packet_hash, public_key, app_data, used, stamp, stamp_value FROM known_destinations")
|
||||
cursor = self._conn.execute("SELECT destination_hash, last_heard, packet_hash, public_key, app_data, used, stamp, stamp_value, lr_cost FROM known_destinations")
|
||||
while True:
|
||||
rows = cursor.fetchmany(4096)
|
||||
if not rows: break
|
||||
for row in rows: yield row[0], [row[1], row[2], row[3], row[4], row[5], row[6], row[7]]
|
||||
for row in rows: yield row[0], [row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]]
|
||||
|
||||
# Caller must hold Identity.known_destinations_lock.
|
||||
def remember_ratchet(self, destination_hash, ratchet, now):
|
||||
|
|
@ -452,12 +462,12 @@ class KnownDestinationsStore:
|
|||
for destination_hash, entry in legacy.items():
|
||||
if len(entry) < 5: continue
|
||||
entry = list(entry)
|
||||
while len(entry) < 7: entry.append(None if len(entry) == 5 else 0)
|
||||
while len(entry) < 8: entry.append(None if len(entry) in (5, 7) else 0)
|
||||
if entry[0] == None or entry[0] <= 0: entry[0] = now
|
||||
rows.append(self.__entry_row(destination_hash, entry))
|
||||
|
||||
self._conn.execute("BEGIN")
|
||||
for i in range(0, len(rows), 4096): self._conn.executemany("INSERT OR REPLACE INTO known_destinations VALUES (?,?,?,?,?,?,?,?,?)", rows[i:i+4096])
|
||||
for i in range(0, len(rows), 4096): self._conn.executemany("INSERT OR REPLACE INTO known_destinations (destination_hash, last_heard, packet_hash, public_key, app_data, used, stamp, stamp_value, lr_cost, identity_hash) VALUES (?,?,?,?,?,?,?,?,?,?)", rows[i:i+4096])
|
||||
self._conn.commit()
|
||||
|
||||
return len(rows)
|
||||
|
|
|
|||
|
|
@ -197,6 +197,7 @@ class Transport:
|
|||
DESTINATION_VALUE = 23 # 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 = 19 # Default required link request cost if not explicitly announced or configured
|
||||
|
||||
STAMPER_PR_QUEUE_LENGTH = 32 # TODO: Provisional, wire to config handling
|
||||
STAMPER_AN_QUEUE_LENGTH = 32 # TODO: Provisional, wire to config handling
|
||||
|
|
@ -2062,7 +2063,7 @@ class Transport:
|
|||
stamp_ok = False
|
||||
if ext_payload != None:
|
||||
if ext_payload[0] == RNS.Identity.EXTPAYLOADMARKER:
|
||||
stamp = ext_payload[RNS.Identity.EXTHEADERLENGTH:]
|
||||
stamp = ext_payload[Transport.PR_EXT_HEADERLENGTH:]
|
||||
stamp_ok = Transport.validate_pr_stamp(stamp, destination_hash, tag_bytes)
|
||||
if not stamp_ok:
|
||||
if ext_payload[1] == 0xFF: RNS.log(f"Unstamped relayed path request from stamp-aware node for {RNS.prettyhexrep(destination_hash)}", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None # TODO: Remove this transitory diagnostics check once enforcement is enabled
|
||||
|
|
@ -2077,7 +2078,7 @@ class Transport:
|
|||
else: # TODO: Remove
|
||||
if RNS.sl(RNS.LOG_DEBUG): # TODO: Remove
|
||||
workblock = stamp_workblock(Transport.pr_stamp_material(destination_hash, tag_bytes), expand_rounds=0) # TODO: Remove
|
||||
dval = stamp_value(workblock, ext_payload[RNS.Identity.EXTHEADERLENGTH:]) # TODO: Remove
|
||||
dval = stamp_value(workblock, ext_payload[Transport.PR_EXT_HEADERLENGTH:]) # TODO: Remove
|
||||
RNS.log(f"Stamped path request for {RNS.prettyhexrep(destination_hash)} with value {dval}", RNS.LOG_DEBUG)
|
||||
|
||||
if packet.receiving_interface: packet.receiving_interface.received_path_request(size=len(raw))
|
||||
|
|
@ -3900,8 +3901,11 @@ class Transport:
|
|||
# Path Request Helpers #
|
||||
########################
|
||||
|
||||
PR_LENGTH = 3*RNS.Identity.TRUNCATED_HASHLENGTH//8
|
||||
PR_STAMPED_LENGTH = PR_LENGTH+RNS.Identity.EXTPAYLOADLENGTH
|
||||
PR_EXT_HEADERLENGTH = 2
|
||||
PR_EXT_STAMPLENGTH = RNS.Identity.STAMPLENGTH
|
||||
PR_EXT_PAYLOADLENGTH = PR_EXT_HEADERLENGTH+PR_EXT_STAMPLENGTH
|
||||
PR_LENGTH = 3*RNS.Identity.TRUNCATED_HASHLENGTH//8
|
||||
PR_STAMPED_LENGTH = PR_LENGTH+PR_EXT_PAYLOADLENGTH
|
||||
@staticmethod
|
||||
def parse_path_request(data):
|
||||
DL = RNS.Identity.TRUNCATED_HASHLENGTH//8
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue