From 1a7d5aaa90a657490986e6475083d02de748c295 Mon Sep 17 00:00:00 2001 From: DeFiDude <59237470+DeFiDude@users.noreply.github.com> Date: Tue, 10 Nov 2026 20:57:10 -0700 Subject: [PATCH] lxmf: send and honour Resource cancels in delivery and propagation --- crates/lxmf-core/src/link_delivery.rs | 9 ++++- crates/lxmf-core/src/propagation_client.rs | 41 ++++++++++++++++++++-- crates/lxmf-core/src/propagation_sync.rs | 17 ++++++++- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/crates/lxmf-core/src/link_delivery.rs b/crates/lxmf-core/src/link_delivery.rs index dcf1508..b75ca60 100644 --- a/crates/lxmf-core/src/link_delivery.rs +++ b/crates/lxmf-core/src/link_delivery.rs @@ -5940,6 +5940,8 @@ fn drive_inbound_resource_watchdogs( let resource_id = drop_inbound_resource(delivery, resource_hash); notify_inbound_resource_concluded(concluded_handler, *link_id, resource_id); } + // Resource.cancel() sends RCL only on an active Link (Resource.py:1133-1139). + TransferAction::SendCancel(_, _) if delivery.link.state != LinkState::Active => {} retry => { if dispatch_inbound_resource_action( link_id, @@ -6232,7 +6234,12 @@ fn dispatch_action( ActionOutcome::Fail(reason) } TransferAction::SendCancel(cancel_type, resource_hash) => { - if let Ok(encrypted) = delivery.link.encrypt(&resource_hash) { + // Resource.cancel() sends ICL/RCL only on an active Link (Resource.py:1126, 1134). + let active = delivery.link.state == LinkState::Active; + if let Some(encrypted) = active + .then(|| delivery.link.encrypt(&resource_hash).ok()) + .flatten() + { let context = match cancel_type { rns_protocol::resource::CancelType::Icl => { rns_wire::context::PacketContext::ResourceIcl diff --git a/crates/lxmf-core/src/propagation_client.rs b/crates/lxmf-core/src/propagation_client.rs index 3f29f26..8345bdd 100644 --- a/crates/lxmf-core/src/propagation_client.rs +++ b/crates/lxmf-core/src/propagation_client.rs @@ -29,7 +29,7 @@ use std::time::{Duration, Instant}; use bytes::Bytes; use rns_crypto::ed25519::{Ed25519PrivateKey, Ed25519PublicKey}; -use rns_link::link::{CloseReason, Link, LinkAction}; +use rns_link::link::{CloseReason, Link, LinkAction, LinkState}; use rns_link::stamp::{LinkRequestStamper, LinkStampPolicy}; use rns_protocol::resource::{ InboundTransfer, MAX_EFFICIENT_SIZE, MAX_RESOURCE_SIZE, MAX_SEGMENTS, MultiSegmentInbound, @@ -2215,11 +2215,17 @@ impl PropagationClient { if self.status.state == PropagationClientState::Receiving { let mut retry_requests = Vec::new(); + let mut cancel = None; + let mut failed = false; for transfer in self.inbound_resources.values_mut() { match transfer.check_timeout() { TransferAction::SendRequest(request) => retry_requests.push(request), + TransferAction::SendCancel(_, hash) => { + cancel = Some(hash); + break; + } TransferAction::Failed(_) => { - self.fail(); + failed = true; break; } _ => {} @@ -2231,6 +2237,23 @@ impl PropagationClient { &request, ); } + if let Some(hash) = cancel { + // Resource.cancel() sends RCL only on an active Link (Resource.py:1133-1139). + if self + .link + .as_ref() + .is_some_and(|link| link.state == LinkState::Active) + { + self.send_encrypted_resource_control( + rns_wire::context::PacketContext::ResourceRcl, + &hash, + ); + } + failed = true; + } + if failed { + self.fail(); + } } match self.status.state { @@ -2552,6 +2575,20 @@ impl PropagationClient { &data, ); } + Some(TransferAction::SendCancel(_, hash)) => { + // Resource.cancel() sends ICL only on an active Link (Resource.py:1124-1132). + if self + .link + .as_ref() + .is_some_and(|link| link.state == LinkState::Active) + { + self.send_encrypted_resource_control( + rns_wire::context::PacketContext::ResourceIcl, + &hash, + ); + } + self.fail(); + } Some(TransferAction::Failed(_)) => self.fail(), _ => {} } diff --git a/crates/lxmf-core/src/propagation_sync.rs b/crates/lxmf-core/src/propagation_sync.rs index b9197cd..077d3be 100644 --- a/crates/lxmf-core/src/propagation_sync.rs +++ b/crates/lxmf-core/src/propagation_sync.rs @@ -19,7 +19,7 @@ use std::time::{Duration, Instant}; use bytes::Bytes; use rns_crypto::ed25519::{Ed25519PrivateKey, Ed25519PublicKey}; -use rns_link::link::{CloseReason, Link, LinkAction}; +use rns_link::link::{CloseReason, Link, LinkAction, LinkState}; use rns_link::stamp::{LinkRequestStamper, LinkStampPolicy}; use rns_protocol::resource::{ MAX_EFFICIENT_SIZE, MultiSegmentOutbound, OutboundTransfer, ResourceError, TransferAction, @@ -1776,6 +1776,21 @@ impl PropagationSyncTask { } break; } + TransferAction::SendCancel(_, resource_hash) => { + // Resource.cancel() sends ICL only on an active Link (Resource.py:1124-1132). + if self + .link + .as_ref() + .is_some_and(|link| link.state == LinkState::Active) + { + let _ = self.send_resource_packet( + &resource_hash, + rns_wire::context::PacketContext::ResourceIcl, + ); + } + self.fail_active_transfer(); + break; + } TransferAction::None => break, _ => break, }