Refactoring for fractional-value stamp support

This commit is contained in:
Mark Qvist committed 2026-10-30 22:14:59 +01:00
1 parent 1df7ebcdd5
commit 6e898e67ad
7 files changed
+101 -52

No files matched your search

+8
View File
@@ -64,6 +64,14 @@ class LXMFPropagationAnnounceHandler:
propagation_stamp_cost_flexibility = int(data[5][1])
peering_cost = int(data[5][2])
metadata = data[6]
# Transitional mechanism, extract the real, fractional-value
# propagation stamp cost if available.
if len(data) > 7:
propagation_stamp_cost = data[7][0]
propagation_stamp_cost_flexibility = data[7][1]
peering_cost = data[7][2]
# TODO: Remove above after 1.6.2 ###########################
if destination_hash in self.lxmrouter.static_peers:
static_peer = self.lxmrouter.peers[destination_hash]
+21 -5
View File
@@ -180,7 +180,12 @@ def stamp_cost_from_app_data(app_data=None):
peer_data = msgpack.unpackb(app_data)
if type(peer_data) == list:
if len(peer_data) < 2: return None
else: return peer_data[1]
else:
if len(peer_data) > 3:
cost_type = type(peer_data[3])
if cost_type == float or cost_type == int: return peer_data[3]
else: return peer_data[1]
else: return peer_data[1]
# Original announce format
else: return None
@@ -218,7 +223,8 @@ def pn_stamp_cost_from_app_data(app_data=None):
else:
if pn_announce_data_is_valid(app_data):
data = msgpack.unpackb(app_data)
return data[5][0]
if len(data) > 7: return data[7][0]
else: return data[5][0]
else: return None
@@ -236,13 +242,23 @@ def pn_announce_data_is_valid(data):
try: int(data[4])
except: raise ValueError("Invalid announce data: Could not decode propagation sync limit")
if type(data[5]) != list: raise ValueError("Invalid announce data: Could not decode stamp costs")
try: int(data[5][0])
try: float(data[5][0])
except: raise ValueError("Invalid announce data: Could not decode target stamp cost")
try: int(data[5][1])
try: float(data[5][1])
except: raise ValueError("Invalid announce data: Could not decode stamp cost flexibility")
try: int(data[5][2])
try: float(data[5][2])
except: raise ValueError("Invalid announce data: Could not decode peering cost")
if type(data[6]) != dict: raise ValueError("Invalid announce data: Could not decode metadata")
# TODO: Transitional mechanism, remove after 1.6.2
if len(data) > 7:
if type(data[7]) != list: raise ValueError("Invalid announce data: Could not decode fractional stamp costs")
try: float(data[7][0])
except: raise ValueError("Invalid announce data: Could not decode target stamp cost")
try: float(data[7][1])
except: raise ValueError("Invalid announce data: Could not decode stamp cost flexibility")
try: float(data[7][2])
except: raise ValueError("Invalid announce data: Could not decode peering cost")
except Exception as e:
RNS.log(f"Could not validate propagation node announce data: {e}", RNS.LOG_DEBUG)
+3 -3
View File
@@ -79,17 +79,17 @@ class LXMPeer:
else: peer.propagation_sync_limit = peer.propagation_transfer_limit
if "propagation_stamp_cost" in dictionary:
try: peer.propagation_stamp_cost = int(dictionary["propagation_stamp_cost"])
try: peer.propagation_stamp_cost = float(dictionary["propagation_stamp_cost"])
except: peer.propagation_stamp_cost = None
else: peer.propagation_stamp_cost = None
if "propagation_stamp_cost_flexibility" in dictionary:
try: peer.propagation_stamp_cost_flexibility = int(dictionary["propagation_stamp_cost_flexibility"])
try: peer.propagation_stamp_cost_flexibility = float(dictionary["propagation_stamp_cost_flexibility"])
except: peer.propagation_stamp_cost_flexibility = None
else: peer.propagation_stamp_cost_flexibility = None
if "peering_cost" in dictionary:
try: peer.peering_cost = int(dictionary["peering_cost"])
try: peer.peering_cost = float(dictionary["peering_cost"])
except: peer.peering_cost = None
else: peer.peering_cost = None
+58 -33
View File
@@ -26,8 +26,12 @@ from .LXMessage import LXMessage
from .Handlers import LXMFDeliveryAnnounceHandler
from .Handlers import LXMFPropagationAnnounceHandler
from RNS.Stamps import quantize_cost
import LXMF.LXStamper as LXStamper
from math import ceil # TODO: Transitory import, remove after RNS 1.6.2
class LXMRouter:
MAX_DELIVERY_ATTEMPTS = 5
PROCESSING_INTERVAL = 4
@@ -52,11 +56,11 @@ class LXMRouter:
ROTATION_HEADROOM_PCT = 10
ROTATION_AR_MAX = 0.5
PEERING_COST = 18
MAX_PEERING_COST = 26
PROPAGATION_COST_MIN = 13
PROPAGATION_COST_FLEX = 3
PROPAGATION_COST = 16
PEERING_COST = 18.0
MAX_PEERING_COST = 26.0
PROPAGATION_COST_MIN = 13.0
PROPAGATION_COST_FLEX = 3.0
PROPAGATION_COST = 16.0
PROPAGATION_LIMIT = 256
SEQUENTIAL_VALIDATION = True
STATIC_SEQUENTIAL = False
@@ -334,14 +338,16 @@ class LXMRouter:
def get_propagation_node_app_data(self):
metadata = self.get_propagation_node_announce_metadata()
node_state = self.propagation_node and not self.from_static_only
stamp_cost = [self.propagation_stamp_cost, self.propagation_stamp_cost_flexibility, self.peering_cost]
stamp_costs = [self.propagation_stamp_cost, self.propagation_stamp_cost_flexibility, self.peering_cost]
ceil_costs = [ceil(self.propagation_stamp_cost), ceil(self.propagation_stamp_cost_flexibility), ceil(self.peering_cost)]
announce_data = [ False, # 0: Legacy LXMF PN support
int(time.time()), # 1: Current node timebase
node_state, # 2: Boolean flag signalling propagation node state
self.propagation_per_transfer_limit, # 3: Per-transfer limit for message propagation in kilobytes
self.propagation_per_sync_limit, # 4: Limit for incoming propagation node syncs
stamp_cost, # 5: Propagation stamp cost for this node
metadata ] # 6: Node metadata
ceil_costs, # 5: Propagation stamp cost for this node
metadata, # 6: Node metadata
stamp_costs ] # 7: Transitional fractional-value stamp cost, will disappear after 1.6.2, where they will be in 5 directly
return msgpack.packb(announce_data)
@@ -388,17 +394,19 @@ class LXMRouter:
def set_inbound_stamp_cost(self, destination_hash, stamp_cost):
if destination_hash in self.delivery_destinations:
delivery_destination = self.delivery_destinations[destination_hash]
cap = RNS.Transport.STAMP_COST_CAP
if stamp_cost == None:
delivery_destination.stamp_cost = None
return True
elif type(stamp_cost) == int:
if stamp_cost < 1:
delivery_destination.stamp_cost = None
elif stamp_cost < 255:
delivery_destination.stamp_cost = stamp_cost
else:
return False
if stamp_cost < 1: delivery_destination.stamp_cost = None
elif stamp_cost < cap: delivery_destination.stamp_cost = stamp_cost
else: return False
return True
elif type(stamp_cost) == float:
if stamp_cost < 1: delivery_destination.stamp_cost = None
elif stamp_cost < cap: delivery_destination.stamp_cost = stamp_cost
else: return False
return True
return False
@@ -434,7 +442,8 @@ class LXMRouter:
pn_app_data = RNS.Identity.recall_app_data(pn_destination_hash)
if pn_announce_data_is_valid(pn_app_data):
pn_config = msgpack.unpackb(pn_app_data)
target_propagation_cost = pn_config[5][0]
if len(pn_config) > 7: target_propagation_cost = pn_config[7][0]
else: target_propagation_cost = pn_config[5][0]
if not target_propagation_cost:
RNS.log(f"Could not retrieve cached propagation node config. Requesting path to propagation node to get target propagation cost...", RNS.LOG_DEBUG)
@@ -446,9 +455,12 @@ class LXMRouter:
pn_app_data = RNS.Identity.recall_app_data(pn_destination_hash)
if pn_announce_data_is_valid(pn_app_data):
pn_config = msgpack.unpackb(pn_app_data)
target_propagation_cost = pn_config[5][0]
if len(pn_config) > 7: target_propagation_cost = pn_config[7][0]
else: target_propagation_cost = pn_config[5][0]
if type(target_propagation_cost) not in (float, int) and target_propagation_cost != None: return None
if not target_propagation_cost: RNS.log("Propagation node stamp cost still unavailable after path request", RNS.LOG_ERROR)
if target_propagation_cost != None: target_propagation_cost = max(0, min(target_propagation_cost, RNS.Transport.STAMP_COST_CAP))
return target_propagation_cost
def set_inbound_propagation_node(self, destination_hash):
@@ -581,7 +593,7 @@ class LXMRouter:
try:
transient_id = bytes.fromhex(components[0])
received = float(components[1])
stamp_value = int(components[2])
stamp_value = quantize_cost(components[2])
filepath = self.messagepath+"/"+filename
msg_size = os.path.getsize(filepath)
file = open(filepath, "rb")
@@ -1050,12 +1062,15 @@ class LXMRouter:
display_name = delivery_destination.display_name.encode("utf-8")
stamp_cost = None
if delivery_destination.stamp_cost != None and type(delivery_destination.stamp_cost) == int:
if delivery_destination.stamp_cost > 0 and delivery_destination.stamp_cost < 255:
stamp_cost = delivery_destination.stamp_cost
if delivery_destination.stamp_cost != None and (type(delivery_destination.stamp_cost) == int or type(delivery_destination.stamp_cost) == float):
if delivery_destination.stamp_cost > 0 and delivery_destination.stamp_cost < RNS.Transport.STAMP_COST_CAP:
stamp_cost = quantize_cost(delivery_destination.stamp_cost)
supported_functionality = [SF_COMPRESSION]
peer_data = [display_name, stamp_cost, supported_functionality]
# TODO: Transitional mechanism, remove after 1.6.2
peer_data = [display_name, ceil(stamp_cost) if stamp_cost else None, supported_functionality, stamp_cost]
# peer_data = [display_name, stamp_cost, supported_functionality]
return msgpack.packb(peer_data)
@@ -1163,7 +1178,7 @@ class LXMRouter:
filename = os.path.split(filepath)[-1]
components = filename.split("_")
if len(components) == 3 and float(components[1]) > 0 and len(os.path.split(components[0])[1]) == (RNS.Identity.HASHLENGTH//8)*2 and int(components[2]) == stamp_value:
if len(components) == 3 and float(components[1]) > 0 and len(os.path.split(components[0])[1]) == (RNS.Identity.HASHLENGTH//8)*2 and quantize_cost(components[2]) == quantize_cost(stamp_value):
timestamp = float(components[1])
if now > timestamp+LXMRouter.MESSAGE_EXPIRY:
RNS.log("Purging message "+RNS.prettyhexrep(transient_id)+" due to expiry", RNS.LOG_EXTREME)
@@ -1481,13 +1496,9 @@ class LXMRouter:
def identity_allowed(self, identity):
if self.auth_required:
if identity.hash in self.allowed_list:
return True
else:
return False
else:
return True
if identity.hash in self.allowed_list: return True
else: return False
else: return True
def message_get_request(self, path, data, request_id, remote_identity, requested_at):
if remote_identity == None: return LXMPeer.ERROR_NO_IDENTITY
@@ -2321,7 +2332,7 @@ class LXMRouter:
return count
def propagation_resource_advertised(self, resource):
if self.from_static_only:
if self.from_static_only and not resource.app_flag_set():
remote_identity = resource.link.get_remote_identity()
if remote_identity == None:
RNS.log(f"Rejecting propagation resource from unidentified peer", RNS.LOG_DEBUG)
@@ -2361,7 +2372,7 @@ class LXMRouter:
for validated_entry in validated_messages:
lxmf_data = validated_entry[1]
stamp_value = validated_entry[2]
stamp_value = quantize_cost(validated_entry[2])
stamp_data = validated_entry[3]
self.lxmf_propagation(lxmf_data, stamp_value=stamp_value, stamp_data=stamp_data)
self.client_propagation_messages_received += 1
@@ -2488,6 +2499,12 @@ class LXMRouter:
remote_peering_cost = pn_config[5][2]
remote_metadata = pn_config[6]
# TODO: Transitional mechanism, remove after 1.6.2
if len(pn_config) > 7:
remote_stamp_cost = pn_config[7][0]
remote_stamp_flex = pn_config[7][1]
remote_peering_cost = pn_config[7][2]
RNS.log(f"Auto-peering with {remote_str} discovered via incoming sync", RNS.LOG_DEBUG) # TODO: Remove debug
self.peer(remote_hash, remote_timebase, remote_transfer_limit, remote_sync_limit, remote_stamp_cost, remote_stamp_flex, remote_peering_cost, remote_metadata)
@@ -2543,7 +2560,7 @@ class LXMRouter:
for validated_entry in validated_messages:
transient_id = validated_entry[0]
lxmf_data = validated_entry[1]
stamp_value = validated_entry[2]
stamp_value = quantize_cost(validated_entry[2])
stamp_data = validated_entry[3]
peer = None
@@ -2626,6 +2643,12 @@ class LXMRouter:
if signal_local_delivery != None: return signal_local_delivery
else:
if self.propagation_node:
if not stamp_value: return # TODO: This is edge-case handling for stamp-less paper messages being
# ingested onto a propagation node, which is currently not possible,
# but could potentially be supported somehow. Will need to figure this
# one out at a more opportune moment. Potential local stamping at time
# of ingress, if allowed by config.
stamp_value = quantize_cost(stamp_value)
stamped_data = lxmf_data+stamp_data
value_component = f"_{stamp_value}" if stamp_value and stamp_value > 0 else ""
file_path = f"{self.messagepath}/{RNS.hexrep(transient_id, delimit=False)}_{received}{value_component}"
@@ -2666,6 +2689,8 @@ class LXMRouter:
lxmf_data = base64.urlsafe_b64decode(uri.replace(LXMessage.URI_SCHEMA+"://", "").replace("/", "")+"==")
transient_id = RNS.Identity.full_hash(lxmf_data)
# TODO: Consider whether to allow duplicate paper messages by
# default, and potential implications in client applications.
router_propagation_result = self.lxmf_propagation(lxmf_data, signal_local_delivery=signal_local_delivery, signal_duplicate=signal_duplicate, allow_duplicate=allow_duplicate, is_paper_message=True)
if router_propagation_result != False:
RNS.log("LXM with transient ID "+RNS.prettyhexrep(transient_id)+" was ingested.", RNS.LOG_DEBUG)
+2 -2
View File
@@ -673,9 +673,9 @@ class LXMessage:
raise ConnectionError("Tried to synthesize resource for LXMF message on a link that was not active")
if self.method == LXMessage.DIRECT:
return RNS.Resource(self.packed, self.__delivery_destination, callback = self.__resource_concluded, progress_callback = self.__update_transfer_progress, auto_compress=self.auto_compress)
return RNS.Resource(self.packed, self.__delivery_destination, callback=self.__resource_concluded, progress_callback=self.__update_transfer_progress, auto_compress=self.auto_compress)
elif self.method == LXMessage.PROPAGATED:
return RNS.Resource(self.propagation_packed, self.__delivery_destination, callback = self.__propagation_resource_concluded, progress_callback = self.__update_transfer_progress)
return RNS.Resource(self.propagation_packed, self.__delivery_destination, app_flag=True, callback=self.__propagation_resource_concluded, progress_callback=self.__update_transfer_progress)
else:
return None
+8 -8
View File
@@ -88,9 +88,9 @@ def apply_config():
active_configuration["peer_announce_interval"] = None
if "lxmf" in lxmd_config and "stamp_cost" in lxmd_config["lxmf"]:
active_configuration["peer_stamp_cost"] = max(1, lxmd_config["lxmf"].as_int("stamp_cost"))
active_configuration["peer_stamp_cost"] = max(1, lxmd_config["lxmf"].as_float("stamp_cost"))
else:
active_configuration["peer_stamp_cost"] = 12
active_configuration["peer_stamp_cost"] = 12.0
if "lxmf" in lxmd_config and "delivery_transfer_max_accepted_size" in lxmd_config["lxmf"]:
active_configuration["delivery_transfer_max_accepted_size"] = lxmd_config["lxmf"].as_float("delivery_transfer_max_accepted_size")
@@ -184,14 +184,14 @@ def apply_config():
active_configuration["propagation_sync_max_accepted_size"] = 256*40
if "propagation" in lxmd_config and "propagation_stamp_cost_target" in lxmd_config["propagation"]:
active_configuration["propagation_stamp_cost_target"] = lxmd_config["propagation"].as_int("propagation_stamp_cost_target")
active_configuration["propagation_stamp_cost_target"] = lxmd_config["propagation"].as_float("propagation_stamp_cost_target")
if active_configuration["propagation_stamp_cost_target"] < LXMF.LXMRouter.PROPAGATION_COST_MIN:
active_configuration["propagation_stamp_cost_target"] = LXMF.LXMRouter.PROPAGATION_COST_MIN
else:
active_configuration["propagation_stamp_cost_target"] = LXMF.LXMRouter.PROPAGATION_COST
if "propagation" in lxmd_config and "propagation_stamp_cost_flexibility" in lxmd_config["propagation"]:
active_configuration["propagation_stamp_cost_flexibility"] = lxmd_config["propagation"].as_int("propagation_stamp_cost_flexibility")
active_configuration["propagation_stamp_cost_flexibility"] = lxmd_config["propagation"].as_float("propagation_stamp_cost_flexibility")
if active_configuration["propagation_stamp_cost_flexibility"] < 0:
active_configuration["propagation_stamp_cost_flexibility"] = 0
else:
@@ -1027,7 +1027,7 @@ autopeer_maxdepth = 6
# You can configure the target stamp cost
# required to deliver messages via this node.
# propagation_stamp_cost_target = 16
# propagation_stamp_cost_target = 16.0
# If set higher than 0, the stamp cost flexi-
# bility option will make this node accept
@@ -1036,13 +1036,13 @@ autopeer_maxdepth = 6
# not from peers directly). This allows the
# network to gradually adjust stamp cost.
# propagation_stamp_cost_flexibility = 3
# propagation_stamp_cost_flexibility = 3.0
# The peering_cost option configures the target
# value required for a remote node to peer with
# and deliver messages to this node.
# peering_cost = 18
# peering_cost = 18.0
# You can configure the maximum peering cost
# of remote nodes that this node will peer with.
@@ -1052,7 +1052,7 @@ autopeer_maxdepth = 6
# more computation time during initial peering
# when generating the peering key.
# remote_peering_cost_max = 26
# remote_peering_cost_max = 26.0
# You can tell the LXMF message router to
# prioritise storage for one or more
+1 -1
View File
@@ -1,2 +1,2 @@
qrcode>=7.4.2
rns>=1.0.0
rns>=1.6.0