2022-06-17 08:54:04 +02:00
|
|
|
import time
|
2025-04-17 13:31:00 +02:00
|
|
|
import threading
|
2022-06-17 08:54:04 +02:00
|
|
|
import RNS
|
|
|
|
|
import RNS.vendor.umsgpack as msgpack
|
|
|
|
|
|
2024-11-23 12:49:01 +01:00
|
|
|
from .LXMF import APP_NAME, stamp_cost_from_app_data, pn_announce_data_is_valid
|
2022-06-17 08:54:04 +02:00
|
|
|
from .LXMessage import LXMessage
|
|
|
|
|
|
|
|
|
|
class LXMFDeliveryAnnounceHandler:
|
|
|
|
|
def __init__(self, lxmrouter):
|
2025-10-30 19:45:40 +01:00
|
|
|
self.aspect_filter = APP_NAME+".delivery"
|
2024-09-06 19:55:18 +02:00
|
|
|
self.receive_path_responses = True
|
2025-10-30 19:45:40 +01:00
|
|
|
self.lxmrouter = lxmrouter
|
2022-06-17 08:54:04 +02:00
|
|
|
|
|
|
|
|
def received_announce(self, destination_hash, announced_identity, app_data):
|
2025-12-02 20:17:46 +01:00
|
|
|
try:
|
|
|
|
|
stamp_cost = stamp_cost_from_app_data(app_data)
|
|
|
|
|
self.lxmrouter.update_stamp_cost(destination_hash, stamp_cost)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2026-05-06 17:19:16 +02:00
|
|
|
RNS.log(f"Could not decode stamp cost from announce data. The contained exception was: {e}", RNS.LOG_DEBUG)
|
2025-12-02 20:17:46 +01:00
|
|
|
|
2022-06-17 08:54:04 +02:00
|
|
|
for lxmessage in self.lxmrouter.pending_outbound:
|
2025-10-30 19:45:40 +01:00
|
|
|
if destination_hash == lxmessage.destination_hash:
|
2024-09-30 19:27:40 +02:00
|
|
|
if lxmessage.method == LXMessage.DIRECT or lxmessage.method == LXMessage.OPPORTUNISTIC:
|
2022-06-17 08:54:04 +02:00
|
|
|
lxmessage.next_delivery_attempt = time.time()
|
|
|
|
|
|
2025-04-17 13:31:00 +02:00
|
|
|
def outbound_trigger():
|
2025-12-02 20:17:46 +01:00
|
|
|
while self.lxmrouter.outbound_processing_lock.locked(): time.sleep(0.1)
|
2025-04-17 13:31:00 +02:00
|
|
|
self.lxmrouter.process_outbound()
|
2022-06-17 08:54:04 +02:00
|
|
|
|
2025-04-17 13:31:00 +02:00
|
|
|
threading.Thread(target=outbound_trigger, daemon=True).start()
|
2022-06-17 08:54:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class LXMFPropagationAnnounceHandler:
|
|
|
|
|
def __init__(self, lxmrouter):
|
2025-10-30 19:45:40 +01:00
|
|
|
self.aspect_filter = APP_NAME+".propagation"
|
2025-10-30 15:18:09 +01:00
|
|
|
self.receive_path_responses = True
|
2025-10-30 19:45:40 +01:00
|
|
|
self.lxmrouter = lxmrouter
|
2022-06-17 08:54:04 +02:00
|
|
|
|
2025-10-30 15:18:09 +01:00
|
|
|
def received_announce(self, destination_hash, announced_identity, app_data, announce_packet_hash, is_path_response):
|
2025-10-31 17:11:17 +01:00
|
|
|
try:
|
|
|
|
|
if type(app_data) == bytes:
|
|
|
|
|
if self.lxmrouter.propagation_node:
|
2025-10-31 21:45:40 +01:00
|
|
|
if pn_announce_data_is_valid(app_data):
|
|
|
|
|
data = msgpack.unpackb(app_data)
|
2025-10-31 17:11:17 +01:00
|
|
|
node_timebase = int(data[1])
|
|
|
|
|
propagation_enabled = data[2]
|
|
|
|
|
propagation_transfer_limit = int(data[3])
|
|
|
|
|
propagation_sync_limit = int(data[4])
|
|
|
|
|
propagation_stamp_cost = int(data[5][0])
|
|
|
|
|
propagation_stamp_cost_flexibility = int(data[5][1])
|
|
|
|
|
peering_cost = int(data[5][2])
|
|
|
|
|
metadata = data[6]
|
|
|
|
|
|
|
|
|
|
if destination_hash in self.lxmrouter.static_peers:
|
2025-11-02 02:27:57 +01:00
|
|
|
static_peer = self.lxmrouter.peers[destination_hash]
|
2025-10-31 17:11:17 +01:00
|
|
|
if not is_path_response or static_peer.last_heard == 0:
|
2025-10-30 15:18:09 +01:00
|
|
|
self.lxmrouter.peer(destination_hash=destination_hash,
|
|
|
|
|
timestamp=node_timebase,
|
|
|
|
|
propagation_transfer_limit=propagation_transfer_limit,
|
|
|
|
|
propagation_sync_limit=propagation_sync_limit,
|
2025-10-30 15:39:00 +01:00
|
|
|
propagation_stamp_cost=propagation_stamp_cost,
|
2025-10-30 16:43:26 +01:00
|
|
|
propagation_stamp_cost_flexibility=propagation_stamp_cost_flexibility,
|
2025-10-31 13:53:59 +01:00
|
|
|
peering_cost=peering_cost,
|
2025-10-30 16:43:26 +01:00
|
|
|
metadata=metadata)
|
2025-10-30 15:18:09 +01:00
|
|
|
|
2025-10-31 17:11:17 +01:00
|
|
|
else:
|
|
|
|
|
if self.lxmrouter.autopeer and not is_path_response:
|
|
|
|
|
if propagation_enabled == True:
|
|
|
|
|
if RNS.Transport.hops_to(destination_hash) <= self.lxmrouter.autopeer_maxdepth:
|
|
|
|
|
self.lxmrouter.peer(destination_hash=destination_hash,
|
|
|
|
|
timestamp=node_timebase,
|
|
|
|
|
propagation_transfer_limit=propagation_transfer_limit,
|
|
|
|
|
propagation_sync_limit=propagation_sync_limit,
|
|
|
|
|
propagation_stamp_cost=propagation_stamp_cost,
|
|
|
|
|
propagation_stamp_cost_flexibility=propagation_stamp_cost_flexibility,
|
|
|
|
|
peering_cost=peering_cost,
|
|
|
|
|
metadata=metadata)
|
2025-10-30 15:18:09 +01:00
|
|
|
|
2025-10-31 17:11:17 +01:00
|
|
|
else:
|
|
|
|
|
if destination_hash in self.lxmrouter.peers:
|
|
|
|
|
RNS.log(f"Peer {self.lxmrouter.peers[destination_hash]} moved outside auto-peering range, breaking peering...")
|
|
|
|
|
self.lxmrouter.unpeer(destination_hash, node_timebase)
|
2025-10-30 17:03:05 +01:00
|
|
|
|
2025-10-31 17:11:17 +01:00
|
|
|
elif propagation_enabled == False:
|
|
|
|
|
self.lxmrouter.unpeer(destination_hash, node_timebase)
|
2025-10-30 15:18:09 +01:00
|
|
|
|
2025-10-31 17:11:17 +01:00
|
|
|
except Exception as e:
|
|
|
|
|
RNS.log("Error while evaluating propagation node announce, ignoring announce.", RNS.LOG_DEBUG)
|
|
|
|
|
RNS.log(f"The contained exception was: {str(e)}", RNS.LOG_DEBUG)
|