diff --git a/crates/lxmf-core/src/admission_store.rs b/crates/lxmf-core/src/admission_store.rs index ef66b8c..a657202 100644 --- a/crates/lxmf-core/src/admission_store.rs +++ b/crates/lxmf-core/src/admission_store.rs @@ -43,6 +43,8 @@ struct UserDeletedMessage { struct Inner { journal: PathBuf, bodies: PathBuf, + /// The only directory journaled bodies may be moved in from. + legacy_bodies: Option, committed: HashMap, deleted: HashMap<([u8; 16], [u8; 32]), UserDeletedMessage>, } @@ -85,6 +87,24 @@ impl FileAdmissionStore { /// Parent is the daemon's already configured state directory. A strict /// outside bootstrap receipt witnesses creation of journal/body directories. pub fn open(state_dir: &Path, bodies: &Path) -> Result { + Self::open_inner(state_dir, bodies, None) + } + + /// [`Self::open`] for a store whose bodies used to live in `legacy_bodies`: + /// a journaled body still there is moved into `bodies`. + pub fn open_moving_bodies_from( + state_dir: &Path, + bodies: &Path, + legacy_bodies: &Path, + ) -> Result { + Self::open_inner(state_dir, bodies, Some(legacy_bodies)) + } + + fn open_inner( + state_dir: &Path, + bodies: &Path, + legacy_bodies: Option<&Path>, + ) -> Result { let journal = state_dir.join("download-admission-v1"); let parent = state_dir .ancestors() @@ -103,6 +123,7 @@ impl FileAdmissionStore { let mut inner = Inner { journal, bodies: bodies.to_owned(), + legacy_bodies: legacy_bodies.map(Path::to_owned), committed: HashMap::new(), deleted: HashMap::new(), }; @@ -265,7 +286,13 @@ impl FileAdmissionStore { .committed .values() .filter(|c| !c.hook_completed) - .filter_map(|c| Some((c.intent.hook_event?, c.intent.body_path.clone()?))) + .filter_map(|c| { + c.intent.body_path.as_ref()?; + Some(( + c.intent.hook_event?, + inner.body_path(&c.intent.record.message_id), + )) + }) .collect() } @@ -413,6 +440,9 @@ impl DurableAdmissionBackend for FileAdmissionCommit { } impl Inner { + fn body_path(&self, message_id: &[u8; 32]) -> PathBuf { + self.bodies.join(format!("{}.lxm", hex::encode(message_id))) + } fn intent_path(&self, id: &[u8; 32]) -> PathBuf { self.journal.join(format!("{}.intent", hex::encode(id))) } @@ -431,12 +461,21 @@ impl Inner { return Err(other("message was explicitly deleted")); } if let Some(path) = &intent.body_path { - if path.parent() != Some(self.bodies.as_path()) - || path.file_name().and_then(|n| n.to_str()) - != Some(format!("{}.lxm", hex::encode(intent.record.message_id)).as_str()) + if path.file_name().and_then(|n| n.to_str()) + != Some(format!("{}.lxm", hex::encode(intent.record.message_id)).as_str()) { return Err(other("admission archive path escapes configured inbox")); } + // A journal written before the bodies moved names the old directory. + let journaled = path; + let path = &self.body_path(&intent.record.message_id); + if !path.exists() + && self.legacy_bodies.is_some() + && journaled.parent() == self.legacy_bodies.as_deref() + && journaled.exists() + { + std::fs::rename(journaled, path).map_err(other)?; + } let body = read_bounded(path, rns_protocol::resource::MAX_RESOURCE_SIZE)?; if Some(rns_crypto::sha::full_hash(&body)) != intent.record.stored_message_wire_hash { return Err(other("admission archive content mismatch")); @@ -606,4 +645,70 @@ mod tests { .unwrap(); assert!(recovered.pending_hooks().is_empty()); } + + /// A store whose one committed body sits in `/messages`. + fn store_with_body_in_inbox() -> (tempfile::TempDir, PathBuf, String, AdmissionScope, Vec) { + let temp = tempfile::tempdir().unwrap(); + let inbox = temp.path().join("messages"); + let store = FileAdmissionStore::open(temp.path(), &inbox).unwrap(); + let message = crate::message::test_message( + [1; 16], + [2; 16], + "", + "body", + crate::constants::DeliveryMethod::Propagated, + ); + let scope = AdmissionScope { + identity: [3; 16], + node: [4; 16], + transient_id: [5; 32], + }; + let record = AdmissionRecord { + scope, + message_id: message.message_id(), + received_wire_hash: [0; 32], + stored_message_wire_hash: None, + disposition: DurableDisposition::Committed, + committed_at: 100, + }; + DurableAdmissionReceipt::commit(store.prepare( + record, + Bytes::copy_from_slice(message.wire_bytes()), + true, + )) + .unwrap(); + drop(store); + let name = format!("{}.lxm", hex::encode(message.message_id())); + assert!(inbox.join(&name).exists()); + (temp, inbox, name, scope, message.wire_bytes().to_vec()) + } + + #[test] + fn bodies_journaled_in_the_inbox_move_to_a_new_bodies_directory_on_open() { + let (temp, inbox, name, scope, wire) = store_with_body_in_inbox(); + let bodies = temp.path().join("bodies"); + let reopened = + FileAdmissionStore::open_moving_bodies_from(temp.path(), &bodies, &inbox).unwrap(); + assert!(!inbox.join(&name).exists()); + assert_eq!(std::fs::read(bodies.join(&name)).unwrap(), wire); + assert_eq!( + reopened.haves(scope.identity, scope.node, 101), + vec![scope.transient_id.to_vec()] + ); + assert_eq!(reopened.pending_hooks()[0].1, bodies.join(&name)); + } + + #[test] + fn bodies_are_moved_in_only_from_the_named_legacy_directory() { + let (temp, inbox, name, _, _) = store_with_body_in_inbox(); + let bodies = temp.path().join("bodies"); + let elsewhere = temp.path().join("elsewhere"); + for _ in [ + FileAdmissionStore::open(temp.path(), &bodies), + FileAdmissionStore::open_moving_bodies_from(temp.path(), &bodies, &elsewhere), + ] { + assert!(inbox.join(&name).exists()); + assert!(!bodies.join(&name).exists()); + } + } } diff --git a/crates/lxmf-core/src/handlers.rs b/crates/lxmf-core/src/handlers.rs index 0ca72da..1e1331b 100644 --- a/crates/lxmf-core/src/handlers.rs +++ b/crates/lxmf-core/src/handlers.rs @@ -49,6 +49,43 @@ pub enum CompressionSupport { Supported, } +/// A per-transfer or per-sync limit as Python holds it: lxmd's built-in defaults +/// are ints, configured values come from `as_float` (lxmd.py:165-184@1.3.0), +/// and the announce packs whichever it has (LXMRouter.py:394-395). +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum PnLimit { + Int(u64), + Float(f64), +} + +impl PnLimit { + pub fn as_f64(self) -> f64 { + match self { + Self::Int(n) => n as f64, + Self::Float(f) => f, + } + } + + fn to_value(self) -> rmpv::Value { + match self { + Self::Int(n) => rmpv::Value::from(n), + Self::Float(f) => rmpv::Value::F64(f), + } + } +} + +impl From for PnLimit { + fn from(n: u64) -> Self { + Self::Int(n) + } +} + +impl From for PnLimit { + fn from(f: f64) -> Self { + Self::Float(f) + } +} + /// Parsed propagation-node announce data. /// /// Wire layout is an 8-element msgpack array: @@ -69,9 +106,9 @@ pub struct PropagationNodeAnnounceData { /// True when the node is actively serving propagation. pub node_state: bool, /// Per-transfer limit in decimal kilobytes (1 kB = 1000 bytes). - pub transfer_limit: u64, + pub transfer_limit: PnLimit, /// Per-sync limit in decimal kilobytes (1 kB = 1000 bytes). - pub sync_limit: u64, + pub sync_limit: PnLimit, /// Propagation stamp cost, flexibility and peering cost as announced. pub stamp_cost: f64, pub stamp_flex: f64, @@ -87,8 +124,8 @@ pub struct PropagationNodeAnnounceData { impl PropagationNodeAnnounceData { pub fn new( node_state: bool, - transfer_limit: u64, - sync_limit: u64, + transfer_limit: impl Into, + sync_limit: impl Into, stamp_cost: f64, stamp_flex: f64, peering_cost: f64, @@ -100,8 +137,8 @@ impl PropagationNodeAnnounceData { .unwrap_or_default() .as_secs() as i64, node_state, - transfer_limit, - sync_limit, + transfer_limit: transfer_limit.into(), + sync_limit: sync_limit.into(), stamp_cost, stamp_flex, peering_cost, @@ -158,8 +195,8 @@ pub fn get_propagation_node_app_data(data: &PropagationNodeAnnounceData) -> Vec< Value::Boolean(false), Value::from(data.timebase), Value::Boolean(data.node_state), - Value::from(data.transfer_limit), - Value::from(data.sync_limit), + data.transfer_limit.to_value(), + data.sync_limit.to_value(), ceil_costs, metadata, stamp_costs, @@ -414,7 +451,7 @@ pub fn pn_announce_data_is_valid(data: &[u8]) -> bool { /// Parse propagation-node announce data that [`pn_announce_data_is_valid`] accepts. /// -/// Limits are extracted with `int()` as the Python announce handler does. +/// Limits keep their wire form ([`PnLimit`]); the announce handler reads them with `int()`. /// Costs come from the fractional slot 7 when present, else from `int()` of /// slot 5, as the handler takes them. Limits below zero become zero; announces with a /// non-finite cost are refused. String [`PN_META_IMPL_NAME`] and @@ -425,9 +462,18 @@ pub fn parse_pn_announce_data(data: &[u8]) -> Option Option { + let raw = python_index(&value, index)?; + let int = python_int(&raw)?; + Some(match raw { + rmpv::Value::F32(f) => PnLimit::Float(f64::from(f).max(0.0)), + rmpv::Value::F64(f) => PnLimit::Float(f.max(0.0)), + _ => PnLimit::Int(int.clamp(0, i128::from(u64::MAX)) as u64), + }) + }; + let transfer_limit = limit_at(3)?; + let sync_limit = limit_at(4)?; let ceil_costs = python_index(&value, 5)?; for index in 0..3 { python_int(&python_index(&ceil_costs, index)?)?; @@ -1015,8 +1061,11 @@ mod tests { let parsed = parse_pn_announce_data(&packed).unwrap(); assert!(!parsed.legacy); assert!(parsed.node_state); - assert_eq!(parsed.transfer_limit, PROPAGATION_LIMIT as u64); - assert_eq!(parsed.sync_limit, SYNC_LIMIT as u64); + assert_eq!( + parsed.transfer_limit, + PnLimit::Int(PROPAGATION_LIMIT as u64) + ); + assert_eq!(parsed.sync_limit, PnLimit::Int(SYNC_LIMIT as u64)); assert_eq!(parsed.stamp_cost, PROPAGATION_COST); assert_eq!(parsed.stamp_flex, PROPAGATION_COST_FLEX); assert_eq!(parsed.peering_cost, PEERING_COST); @@ -1044,8 +1093,8 @@ mod tests { let parsed = parse_pn_announce_data(&packed).unwrap(); assert!(parsed.node_state); - assert_eq!(parsed.transfer_limit, 10240); - assert_eq!(parsed.sync_limit, 10240); + assert_eq!(parsed.transfer_limit, PnLimit::Float(10240.0)); + assert_eq!(parsed.sync_limit, PnLimit::Float(10240.0)); assert_eq!(parsed.stamp_cost, 16.0); } @@ -1097,7 +1146,7 @@ mod tests { // Python accepts these (int() succeeds); the typed values are clamped. let negative = parse_pn_announce_data(&encode(Value::from(-1), Value::from(16))).unwrap(); - assert_eq!(negative.transfer_limit, 0); + assert_eq!(negative.transfer_limit, PnLimit::Int(0)); let large = parse_pn_announce_data(&encode(Value::from(10240), Value::from(256))).unwrap(); assert_eq!(large.stamp_cost, 256.0); let fractional = diff --git a/crates/lxmf-core/src/router.rs b/crates/lxmf-core/src/router.rs index becb12d..ef8fc63 100644 --- a/crates/lxmf-core/src/router.rs +++ b/crates/lxmf-core/src/router.rs @@ -339,6 +339,16 @@ pub struct LxmRouter { claims_dir: Option, } +/// `RouterConfig` holds limits as f64: a whole number announces as Python's int +/// default, anything else as the float LXMRouter was given. +fn router_limit(limit_kb: f64) -> crate::handlers::PnLimit { + if limit_kb.fract() == 0.0 && (0.0..=u64::MAX as f64).contains(&limit_kb) { + crate::handlers::PnLimit::Int(limit_kb as u64) + } else { + crate::handlers::PnLimit::Float(limit_kb) + } +} + /// Delivered destinations a router holds for the embedder to retain. const MAX_PENDING_RETAINS: usize = 256; @@ -1780,8 +1790,8 @@ impl LxmRouter { let mut data = crate::handlers::PropagationNodeAnnounceData::new( self.config.propagation_enabled && !self.config.ext.from_static_only, - self.config.propagation_limit_kb as u64, - self.config.sync_limit_kb as u64, + router_limit(self.config.propagation_limit_kb), + router_limit(self.config.sync_limit_kb), self.config.propagation_stamp_cost, self.config.propagation_stamp_flex, self.config.ext.peering_cost, diff --git a/crates/lxmf-tools/src/commands/lxmd.rs b/crates/lxmf-tools/src/commands/lxmd.rs index d5ad33b..5c9099e 100644 --- a/crates/lxmf-tools/src/commands/lxmd.rs +++ b/crates/lxmf-tools/src/commands/lxmd.rs @@ -362,6 +362,11 @@ fn apply_propagation_store_commit( } _ => None, }; + // LXMRouter keeps one processed-ID store; stored entries are claimed there at once. + let now = now_f64(); + for (transient_id, _) in &result.committed { + router.propagation_store.claim_processed(*transient_id, now); + } distribution.extend( result .committed @@ -984,6 +989,8 @@ fn control_timeout(args: &Args) -> f64 { } } +const ADMISSION_BODIES_DIR: &str = "download-admission-bodies"; + fn now_f64() -> f64 { std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) @@ -1096,11 +1103,16 @@ fn queue_path_request( fn queue_unknown_propagation_node_path_request( transport_tx: &mpsc::Sender, + router: &mut LxmRouter, + medium: &MediumSnapshot, node: [u8; 16], last_propagation_check: &mut f64, now: f64, ) -> bool { *last_propagation_check = now; + if !router.request_path(node, medium, now) { + return true; + } if let Err(e) = transport_tx.try_send(TransportMessage::RequestPath { destination_hash: node, }) { @@ -1291,8 +1303,8 @@ fn backchannel_error_from_runtime( fn propagation_node_app_data(config: &DaemonConfig) -> Vec { let mut pn_data = lxmf_core::handlers::PropagationNodeAnnounceData::new( config.propagation_enabled && !config.from_static_only, - config.propagation_limit_kb as u64, - config.sync_limit_kb as u64, + config.propagation_limit_kb, + config.sync_limit_kb, config.propagation_stamp_cost, config.propagation_stamp_flex, config.peering_cost, @@ -1303,17 +1315,21 @@ fn propagation_node_app_data(config: &DaemonConfig) -> Vec { propagation_announce_app_data(&pn_data) } +/// Announce requests the daemon has not served yet. A burst of path requests +/// past this is dropped; the requester asks again. +const DELIVERY_ANNOUNCE_BACKLOG: usize = 64; + /// Delivery destination events reach the LinkManager; announce requests go to /// the daemon, which owns the delivery ratchets, app data and stamp. async fn forward_delivery_events( mut events: mpsc::Receiver, link_tx: mpsc::Sender, - announce_tx: mpsc::UnboundedSender, + announce_tx: mpsc::Sender, ) { while let Some(event) = events.recv().await { match event { DestinationEvent::AnnounceRequested(request) => { - let _ = announce_tx.send(request); + let _ = announce_tx.try_send(request); } event => { if link_tx.send(event).await.is_err() { @@ -1643,6 +1659,8 @@ struct LxmdRunner { node_policies: HashMap<[u8; 16], AdvertisedNodePolicy>, propagation_envelopes: HashMap<[u8; 32], PropagationEnvelope>, pending_propagation_envelopes: HashMap<[u8; 32], PendingPropagationEnvelope>, + /// Propagated messages waiting for the node's stamp cost, with their failure deadline. + node_cost_deadlines: HashMap<[u8; 32], f64>, propagation_preparation_tasks: Vec>, ratchets_dir: PathBuf, delivery_ratchets: DeliveryRatchetState, @@ -1650,6 +1668,8 @@ struct LxmdRunner { known_identities: HashMap, /// Identities asked of Transport's validated announce cache. identity_recalls: HashMap<[u8; 16], oneshot::Receiver>, + /// Outbound destinations whose persisted announce app data was recalled once. + compression_recalled: HashSet<[u8; 16]>, /// Reaches the shared instance when attached to one. control_plane: Option, retain_task: Option>, @@ -1699,7 +1719,7 @@ struct LxmdRunner { transport_tx: mpsc::Sender, pending_runtime_transport: VecDeque, delivery_stamps: DestinationStamps, - delivery_announce_rx: mpsc::UnboundedReceiver, + delivery_announce_rx: mpsc::Receiver, /// Path responses refused because wall time has not advanced past the last announce. coalesced_path_responses: VecDeque, control_link_command_tx: Option>, @@ -1779,6 +1799,20 @@ impl LxmdRunner { } } + /// A transient ID claimed by local delivery is a duplicate for propagation intake. + fn share_processed_claim(&self, transient_id: [u8; 32]) { + if let Some(node) = self.propagation_node.as_ref() + && let Ok(mut node) = node.lock() + && let Some(seen) = self + .router + .propagation_store + .locally_processed_ids() + .get(&transient_id) + { + node.merge_processed_ids([(&transient_id, seen)]); + } + } + async fn install_announce_subscriptions( &mut self, rns_handle: &ReticulumHandle, @@ -2120,7 +2154,7 @@ impl LxmdRunner { mpsc::unbounded_channel::(); let (delivery_link_tx, delivery_link_rx) = mpsc::channel(256); - let (delivery_announce_tx, delivery_announce_rx) = mpsc::unbounded_channel(); + let (delivery_announce_tx, delivery_announce_rx) = mpsc::channel(DELIVERY_ANNOUNCE_BACKLOG); tokio::spawn(forward_delivery_events( delivery_rx, delivery_link_tx, @@ -2213,16 +2247,19 @@ impl LxmdRunner { .iter() .filter_map(|peer| parse_destination_hash(peer).ok()) .collect::>(); - let pn_config = PropagationNodeConfig { - max_storage: config - .message_storage_limit - .unwrap_or(configured_kilobytes_to_bytes(config.propagation_limit_kb)), - max_message_size: configured_kilobytes_to_bytes(config.propagation_limit_kb), - max_offer_size: configured_kilobytes_to_bytes(config.sync_limit_kb), - max_message_age: lxmf_core::constants::MESSAGE_EXPIRY, - min_stamp_cost: config.min_accepted_stamp_cost(), - peering_cost: config.peering_cost, - }; + let pn_config = + PropagationNodeConfig { + max_storage: config.message_storage_limit.unwrap_or( + configured_kilobytes_to_bytes(config.propagation_limit_kb.as_f64()), + ), + max_message_size: configured_kilobytes_to_bytes( + config.propagation_limit_kb.as_f64(), + ), + max_offer_size: configured_kilobytes_to_bytes(config.sync_limit_kb.as_f64()), + max_message_age: lxmf_core::constants::MESSAGE_EXPIRY, + min_stamp_cost: config.min_accepted_stamp_cost(), + peering_cost: config.peering_cost, + }; let prop_storage_path = paths.propagation_store_dir.clone(); let pn = Arc::new(Mutex::new(PropagationNode::with_storage( pn_config, @@ -2240,7 +2277,7 @@ impl LxmdRunner { let pn_admission = Arc::new(Mutex::new(PnInboundRuntime::new( config.to_inbound_admission_config(), static_peer_hashes, - configured_kilobytes_to_bytes(config.sync_limit_kb), + configured_kilobytes_to_bytes(config.sync_limit_kb.as_f64()), ))); propagation_admission = Some(pn_admission.clone()); @@ -2311,8 +2348,9 @@ impl LxmdRunner { let get_access = control_state.clone(); let get_workers = prop_get_workers.clone(); let get_served = client_propagation_served_tx.clone(); - let response_limit = configured_kilobytes_to_bytes(config.propagation_limit_kb) - .clamp(1, 64 * 1024 * 1024); + let response_limit = + configured_kilobytes_to_bytes(config.propagation_limit_kb.as_f64()) + .clamp(1, 64 * 1024 * 1024); if !prop_link_mgr.register_deferred_request_handler( lxmf_core::constants::MESSAGE_GET_PATH, rns_identity::destination::AllowPolicy::AllowAll, @@ -2549,8 +2587,13 @@ impl LxmdRunner { let messages_dir = paths.messages_dir.clone(); std::fs::create_dir_all(&messages_dir)?; - let admission_store = match FileAdmissionStore::open(&paths.router_state_dir, &messages_dir) - { + // Upstream's messages directory holds only write_to_directory containers + // (lxmd.py:302-307@1.3.0); the admission store keeps its raw bodies privately. + let admission_store = match FileAdmissionStore::open_moving_bodies_from( + &paths.router_state_dir, + &paths.router_state_dir.join(ADMISSION_BODIES_DIR), + &messages_dir, + ) { Ok(store) => Some(store), Err(error) => { tracing::error!(%error,"durable download storage unavailable; propagation downloads disabled"); @@ -2591,12 +2634,14 @@ impl LxmdRunner { .unwrap_or_default(), propagation_envelopes: HashMap::new(), pending_propagation_envelopes: HashMap::new(), + node_cost_deadlines: HashMap::new(), propagation_preparation_tasks: Vec::new(), ratchets_dir: paths.ratchets_dir, delivery_ratchets, received_ratchets, known_identities, identity_recalls: HashMap::new(), + compression_recalled: HashSet::new(), control_plane: None, retain_task: None, route_hops: HashMap::new(), @@ -2761,7 +2806,7 @@ impl LxmdRunner { self.router.set_autopeer(self.config.autopeer); self.router.set_max_peers(self.config.max_peers); self.router - .set_propagation_limit(self.config.propagation_limit_kb); + .set_propagation_limit(self.config.propagation_limit_kb.as_f64()); self.router.set_stamp_requirements( self.config.propagation_stamp_cost, self.config.propagation_stamp_flex, @@ -3093,9 +3138,6 @@ impl LxmdRunner { "failed to remove persisted propagation peer: {error}" ); } - if let Err(e) = self.router.save_state(&self.data_dir) { - tracing::warn!("Failed to save router state after control unpeer: {e}"); - } tracing::info!(peer = %hex::encode(peer_hash), "control: unpeered peer"); } } @@ -3369,7 +3411,10 @@ impl LxmdRunner { peer = %hex::encode(peer_hash), "no path to peer exists, requesting" ); - queue_path_request(&self.transport_tx, *peer_hash, false, "peer sync"); + // LXMPeer.sync asks through the debounced LXMRouter.request_path. + if self.router.request_path(*peer_hash, &self.medium, now) { + queue_path_request(&self.transport_tx, *peer_hash, false, "peer sync"); + } check.grace_until = Some(now + lxmf_core::constants::PATH_REQUEST_GRACE); } } else if check.grace_until.is_some_and(|until| now >= until) { @@ -3500,8 +3545,8 @@ impl LxmdRunner { if self.router.autopeer(AutopeerCandidate { destination_hash: peer_hash, timebase: pn.timebase as f64, - transfer_limit: Some(pn.transfer_limit as f64), - sync_limit: Some(pn.sync_limit as f64), + transfer_limit: Some(pn.transfer_limit.as_f64()), + sync_limit: Some(pn.sync_limit.as_f64()), stamp_cost: Some(pn.stamp_cost), stamp_flexibility: Some(pn.stamp_flex), peering_cost: Some(pn.peering_cost), @@ -3811,7 +3856,7 @@ impl LxmdRunner { let mut fresh: HashMap<[u8; 16], u8> = HashMap::with_capacity(entries.len()); for entry in entries { if entry.expires > now { - fresh.insert(entry.hash, entry.hops.max(1)); + fresh.insert(entry.hash, entry.hops); } } self.route_hops = fresh; @@ -4174,7 +4219,37 @@ impl LxmdRunner { inputs } + /// `LXMessage.determine_compression_support` reads the recalled app data, which + /// survives restarts in the transport's announce store. + fn recall_outbound_app_data(&mut self) { + let destinations: Vec<[u8; 16]> = self + .router + .pending_outbound + .iter() + .map(|message| message.destination_hash()) + .filter(|destination| !self.compression_recalled.contains(destination)) + .collect(); + for destination in destinations { + if self.identity_recalls.contains_key(&destination) { + continue; + } + let (response_tx, response_rx) = oneshot::channel(); + if self + .transport_tx + .try_send(TransportMessage::Rpc { + query: TransportQuery::RecallDestination { dest: destination }, + response_tx, + }) + .is_ok() + { + self.compression_recalled.insert(destination); + self.identity_recalls.insert(destination, response_rx); + } + } + } + fn run_outbound(&mut self) { + self.recall_outbound_app_data(); let propagation_unready = self.prepare_propagation_envelopes(); let mut inputs = self.outbound_inputs(); inputs.propagation_unready = propagation_unready; @@ -4290,6 +4365,8 @@ impl LxmdRunner { } else if let Some(node) = self.router.outbound_propagation_node && queue_unknown_propagation_node_path_request( &self.transport_tx, + &mut self.router, + &self.medium, node, &mut self.last_propagation_check, now, @@ -4380,7 +4457,6 @@ impl LxmdRunner { } } if self.checkpoint_task.is_none() && now - self.last_crypto_save > 300.0 { - self.merge_processed_ids(); let checkpoint = DaemonCheckpoint { ratchets: self.ratchets_dir.clone(), received: self.received_ratchets.clone(), @@ -4435,8 +4511,7 @@ impl LxmdRunner { hops = event.hops, "received announce" ); - self.route_hops - .insert(event.destination_hash, event.hops.max(1)); + self.route_hops.insert(event.destination_hash, event.hops); if event.name_hash == delivery_name_hash { if let Some(name) = event @@ -4493,7 +4568,7 @@ impl LxmdRunner { event.destination_hash, AdvertisedNodePolicy { stamp_cost: outbound_cost, - transfer_limit_kb: pn.transfer_limit as f64, + transfer_limit_kb: pn.transfer_limit.as_f64().trunc(), recorded_at: now_f64(), }, ); @@ -4519,8 +4594,8 @@ impl LxmdRunner { peer_changed = self.router.autopeer(AutopeerCandidate { destination_hash: event.destination_hash, timebase: pn.timebase as f64, - transfer_limit: Some(pn.transfer_limit as f64), - sync_limit: Some(pn.sync_limit as f64), + transfer_limit: Some(pn.transfer_limit.as_f64().trunc()), + sync_limit: Some(pn.sync_limit.as_f64().trunc()), stamp_cost: Some(pn.stamp_cost), stamp_flexibility: Some(pn.stamp_flex), peering_cost: Some(pn.peering_cost), @@ -4627,7 +4702,8 @@ impl LxmdRunner { len = plaintext.len(), "received decrypted packet via link" ); - self.handle_link_delivered_data(&plaintext); + // A link packet carries its Link ID as the ratchet ID (Link.py:1039@1.6.0). + self.handle_link_delivered_data_on(&plaintext, Some(link_id)); } while let Ok(packet) = self.prop_link_packet_rx.try_recv() { @@ -4784,13 +4860,14 @@ impl LxmdRunner { len = completion.data.len(), "resource transfer completed on link" ); - self.handle_link_delivered_data(&completion.data); + // A resource delivery records its Link ID as the ratchet ID (LXMRouter.py:2325-2327). + self.handle_link_delivered_data_on(&completion.data, Some(completion.link_id)); } _ => {} } } - fn handle_link_delivered_data(&mut self, data: &[u8]) { + fn handle_link_delivered_data_on(&mut self, data: &[u8], link_id: Option<[u8; 16]>) { if data.is_empty() { return; } @@ -4813,7 +4890,12 @@ impl LxmdRunner { len = msg.content_bytes().len(), "inbound LXMF message via link" ); - self.admit_inbound_message(msg, DeliveryMethod::Direct); + self.admit_inbound_message( + msg, + DeliveryMethod::Direct, + link_id.as_ref().map(|id| id.as_slice()), + None, + ); } Err(e) => { tracing::debug!("link data not an LXMF message: {e}"); @@ -4837,7 +4919,7 @@ impl LxmdRunner { fn spawn_propagation_validation(&self, job: PnValidationJob) { let token = job.token(); let link_id = job.link_id(); - let max_transfer_bytes = configured_kilobytes_to_bytes(self.config.sync_limit_kb); + let max_transfer_bytes = configured_kilobytes_to_bytes(self.config.sync_limit_kb.as_f64()); let min_cost = self.config.min_accepted_stamp_cost(); let result_tx = self.prop_validation_tx.clone(); @@ -4982,7 +5064,9 @@ impl LxmdRunner { data, receipt, node, - max_transfer_bytes: configured_kilobytes_to_bytes(self.config.propagation_limit_kb), + max_transfer_bytes: configured_kilobytes_to_bytes( + self.config.propagation_limit_kb.as_f64(), + ), min_cost: self.config.min_accepted_stamp_cost(), #[cfg(test)] store_delay: self.pn_packet_store_delay, @@ -5238,6 +5322,7 @@ impl LxmdRunner { &receipt, verified.as_ref(), ); + self.share_processed_claim(receipt.record().scope.transient_id); if receipt.record().disposition == DurableDisposition::Committed { self.store_propagated_container(result.message.clone()); } @@ -5433,7 +5518,40 @@ impl LxmdRunner { )); } - fn handle_inbound_packet(&mut self, raw: &[u8]) { + /// `LXMRouter.delivery_packet` reads the packet's RSSI, SNR and Q from + /// Reticulum's per-packet caches (LXMRouter.py:2277-2283@1.3.0). + async fn inbound_packet_phy(&self, raw: &[u8]) -> Option { + let (header, _) = rns_wire::header::PacketHeader::unpack(raw).ok()?; + let packet_hash = rns_wire::hash::packet_hash(raw, header.flags.header_type); + let query = |query| { + let (response_tx, response_rx) = oneshot::channel(); + let sent = self + .transport_tx + .try_send(TransportMessage::Rpc { query, response_tx }) + .is_ok(); + async move { + if !sent { + return None; + } + match tokio::time::timeout(Duration::from_millis(100), response_rx).await { + Ok(Ok(TransportQueryResponse::FloatResult(value))) => value, + _ => None, + } + } + }; + let (rssi, snr, q) = tokio::join!( + query(TransportQuery::GetPacketRssi { packet_hash }), + query(TransportQuery::GetPacketSnr { packet_hash }), + query(TransportQuery::GetPacketQ { packet_hash }), + ); + (rssi.is_some() || snr.is_some() || q.is_some()).then_some(lxmf_core::message::PhyStats { + rssi, + snr, + q, + }) + } + + fn handle_inbound_packet(&mut self, raw: &[u8], phy: Option) { let (header, rest) = match rns_wire::header::PacketHeader::unpack(raw) { Ok(r) => r, Err(e) => { @@ -5447,8 +5565,8 @@ impl LxmdRunner { return; } - let plaintext = match self.decrypt_inbound(payload) { - Some(pt) => pt, + let (plaintext, ratchet_id) = match self.decrypt_inbound(payload) { + Some(decrypted) => (decrypted.plaintext, decrypted.ratchet_id), None => { tracing::warn!("failed to decrypt inbound packet"); return; @@ -5493,7 +5611,13 @@ impl LxmdRunner { "inbound LXMF message received" ); - self.admit_inbound_message(msg, DeliveryMethod::Opportunistic); + // LXMRouter.delivery_packet passes packet.ratchet_id (LXMRouter.py:2285). + self.admit_inbound_message( + msg, + DeliveryMethod::Opportunistic, + ratchet_id.as_ref().map(|id| id.as_slice()), + phy, + ); } Err(e) => { tracing::warn!("failed to unpack LXMF message: {e}"); @@ -5504,7 +5628,14 @@ impl LxmdRunner { /// Python `lxmf_delivery` gating (LXMRouter.py:2163-2257): blackholed /// source, ticket learning from a validated signature, stamp, ignored /// source, then delivery. Unknown or invalid signatures are delivered flagged. - fn admit_inbound_message(&mut self, mut msg: LxMessage, method: DeliveryMethod) { + fn admit_inbound_message( + &mut self, + mut msg: LxMessage, + method: DeliveryMethod, + ratchet_id: Option<&[u8]>, + phy: Option, + ) { + msg.record_delivery(None, ratchet_id, None, phy); msg.source_blackholed = self.source_blackholed(&msg.source_hash()); if msg.source_blackholed { tracing::debug!( @@ -5647,10 +5778,37 @@ impl LxmdRunner { }) .cloned() .collect(); + let now = now_f64(); + let queued_ids: HashSet<[u8; 32]> = queued.iter().map(|m| m.message_id()).collect(); + self.node_cost_deadlines + .retain(|id, _| queued_ids.contains(id)); for message in queued { let id = message.message_id(); - match self.pack_message_for_propagation(&message, node) { + let prepared = self.pack_message_for_propagation(&message, node); + if !matches!( + prepared, + Err(PropagationPreparationError::Missing( + MissingPropagationInput::NodePolicy + )) + ) { + self.node_cost_deadlines.remove(&id); + } + match prepared { Ok(_) => {} + // LXMRouter.get_outbound_propagation_cost waits path_request_wait, then the message fails. + Err(PropagationPreparationError::Missing(MissingPropagationInput::NodePolicy)) + if self + .node_cost_deadlines + .get(&id) + .is_some_and(|deadline| now >= *deadline) => + { + self.node_cost_deadlines.remove(&id); + self.propagation_envelopes.remove(&id); + self.pending_propagation_envelopes.remove(&id); + self.last_delivery_failure = + Some("propagation node stamp cost unavailable".to_string()); + self.router.mark_outbound_failed(&id); + } Err(PropagationPreparationError::Missing( MissingPropagationInput::NodeIdentity | MissingPropagationInput::NodePath, )) => {} @@ -5660,8 +5818,13 @@ impl LxmdRunner { } else { node }; + if missing == MissingPropagationInput::NodePolicy { + self.node_cost_deadlines + .entry(id) + .or_insert(now + self.medium.path_request_wait()); + } self.recall_identity(wanted); - if self.router.request_path(wanted, &self.medium, now_f64()) { + if self.router.request_path(wanted, &self.medium, now) { queue_path_request( &self.transport_tx, wanted, @@ -5912,7 +6075,8 @@ impl LxmdRunner { let prepared = propagation_readiness( prop_hash, self.known_identities.contains_key(&hex::encode(prop_hash)), - self.route_hops.contains_key(&prop_hash), + // LXMRouter checks RNS.Transport.has_path; the medium snapshot is refreshed per tick. + self.medium.has_path(&prop_hash), self.node_policies.get(&prop_hash).copied(), self.known_identities.contains_key(&dest_hex), now_f64(), @@ -6014,7 +6178,10 @@ impl LxmdRunner { Err(PropagationPreparationError::Pending) } - fn decrypt_inbound(&self, ciphertext: &[u8]) -> Option> { + fn decrypt_inbound( + &self, + ciphertext: &[u8], + ) -> Option { let prv_keys = self.delivery_ratchets.ring().private_keys(); let refs: Vec<&[u8; 32]> = prv_keys.iter().collect(); let ratchets = if refs.is_empty() { @@ -6022,7 +6189,9 @@ impl LxmdRunner { } else { Some(refs.as_slice()) }; - self.identity.decrypt(ciphertext, ratchets, false).ok() + self.identity + .decrypt_with_ratchet_id(ciphertext, ratchets, false) + .ok() } fn create_delivery_proof(&self, raw_packet: &[u8]) -> Option> { @@ -6255,11 +6424,11 @@ pub(crate) async fn main() { .map_err(std::io::Error::other)?; let config = DaemonConfig::from_config(&source).map_err(std::io::Error::other)?; let policy = RecoveryPolicy { - maximum_store_bytes: config - .message_storage_limit - .unwrap_or(configured_kilobytes_to_bytes(config.propagation_limit_kb)), + maximum_store_bytes: config.message_storage_limit.unwrap_or( + configured_kilobytes_to_bytes(config.propagation_limit_kb.as_f64()), + ), maximum_message_bytes: configured_kilobytes_to_bytes( - config.propagation_limit_kb, + config.propagation_limit_kb.as_f64(), ), minimum_stamp_cost: config.min_accepted_stamp_cost(), }; @@ -6865,7 +7034,8 @@ pub(crate) async fn main() { runner.tick(); } Some(raw) = runner.inbound_raw_rx.recv() => { - runner.handle_inbound_packet(&raw); + let phy = runner.inbound_packet_phy(&raw).await; + runner.handle_inbound_packet(&raw, phy); } Some(request) = runner.delivery_announce_rx.recv() => { if let Err(error) = runner.request_delivery_announce(request) { @@ -6875,8 +7045,8 @@ pub(crate) async fn main() { outcome = runner.delivery_stamps.recv() => { runner.on_delivery_stamp(outcome); } - Some((plaintext, _link_id)) = runner.link_packet_rx.recv() => { - runner.handle_link_delivered_data(&plaintext); + Some((plaintext, link_id)) = runner.link_packet_rx.recv() => { + runner.handle_link_delivered_data_on(&plaintext, Some(link_id)); runner.drain_link_packets(); } Some(event) = runner.delivery_accounting_rx.recv() => { @@ -7406,19 +7576,19 @@ mod tests { // Unknown senders may be delivered, but cannot teach a ticket. An // invalid signature under a recalled full identity cannot do so either. - runner.handle_link_delivered_data(message.wire_bytes()); + runner.handle_link_delivered_data_on(message.wire_bytes(), None); assert!(runner.router.get_outbound_ticket(&source).is_none()); runner .known_identities .insert(hex::encode(source), sender.get_public_key()); let mut invalid = message.wire_bytes().to_vec(); invalid[32] ^= 1; - runner.handle_link_delivered_data(&invalid); + runner.handle_link_delivered_data_on(&invalid, None); assert!(runner.router.get_outbound_ticket(&source).is_none()); // The exact duplicate is authenticated after identity recall and must // still teach the ticket, without producing another delivery event. - runner.handle_link_delivered_data(message.wire_bytes()); + runner.handle_link_delivered_data_on(message.wire_bytes(), None); assert_eq!(runner.router.get_outbound_ticket(&source), Some(token)); assert_eq!(deliveries.load(std::sync::atomic::Ordering::Relaxed), 1); runner.router.save_state(&runner.data_dir).unwrap(); @@ -7476,6 +7646,309 @@ mod tests { .unwrap() } + #[tokio::test] + async fn control_unpeer_leaves_claims_rotated_by_a_running_checkpoint() { + let temp = tempfile::tempdir().unwrap(); + let (tx, _rx) = mpsc::channel::(64); + initialize_storage_namespace(temp.path()).unwrap(); + let mut runner = LxmdRunner::new(DaemonConfig::default(), temp.path(), tx, None).unwrap(); + let data_dir = runner.data_dir.clone(); + runner.router.load_state(&data_dir).unwrap(); + let message = signed_fixture( + runner.lxmf_dest_hash, + [0xCD; 16], + "", + "claimed", + DeliveryMethod::Direct, + ); + assert!(runner.router.deliver_inbound(&message, false)); + let checkpoint = runner.router.state_snapshot(); + let saving = data_dir.join(format!( + "{}.saving", + lxmf_core::persist::DELIVERY_CLAIMS_FILE + )); + assert!(saving.exists()); + + let peer_hash = [0x44; 16]; + runner + .router + .peers + .insert(peer_hash, LxmPeer::new(peer_hash)); + let (command_tx, command_rx) = mpsc::unbounded_channel(); + runner.control_command_rx = command_rx; + command_tx.send(ControlCommand::Unpeer(peer_hash)).unwrap(); + runner.drain_control_commands(); + + assert!(!runner.router.peers.contains_key(&peer_hash)); + // The checkpoint that rotated the claims still owns them until it commits. + assert!(saving.exists()); + checkpoint.save_state(&data_dir).unwrap(); + assert!(!saving.exists()); + } + + #[tokio::test] + async fn a_directly_connected_peer_keeps_its_zero_hop_distance() { + let temp = tempfile::tempdir().unwrap(); + let (tx, mut rx) = mpsc::channel::(64); + initialize_storage_namespace(temp.path()).unwrap(); + let mut runner = LxmdRunner::new(DaemonConfig::default(), temp.path(), tx, None).unwrap(); + let neighbour = [0x0D; 16]; + let responder = tokio::spawn(async move { + while let Some(message) = rx.recv().await { + if let TransportMessage::Rpc { + query: TransportQuery::GetPathTable, + response_tx, + } = message + { + let entry = rns_transport::messages::PathTableRpcEntry { + hash: neighbour, + timestamp: now_f64(), + via: Some(neighbour), + hops: 0, + expires: now_f64() + 600.0, + value: None, + used: 0.0, + lr_cost: None, + interface: String::new(), + interface_id: 1, + interface_mode: rns_transport::constants::InterfaceMode::Full, + interface_role: Default::default(), + }; + let _ = response_tx.send(TransportQueryResponse::PathTable(vec![entry])); + return; + } + } + }); + runner.refresh_route_hops_from_transport().await; + responder.abort(); + // Transport.hops_to reports the path table's hop count unchanged. + assert_eq!(runner.route_hops.get(&neighbour), Some(&0)); + } + + #[tokio::test] + async fn outbound_compression_support_comes_from_recalled_app_data_after_restart() { + let temp = tempfile::tempdir().unwrap(); + let (tx, mut rx) = mpsc::channel::(64); + initialize_storage_namespace(temp.path()).unwrap(); + let mut runner = LxmdRunner::new(DaemonConfig::default(), temp.path(), tx, None).unwrap(); + while rx.try_recv().is_ok() {} + let recipient = Identity::new(); + let recipient_hash = [0x7E; 16]; + let draft = LxMessageDraft::new( + recipient_hash, + runner.lxmf_dest_hash, + "", + "after restart", + DeliveryMethod::Direct, + ) + .unwrap(); + runner + .router + .submit_signed( + draft + .sign(&runner.identity.get_signing_key().unwrap()) + .unwrap(), + ) + .unwrap(); + assert!(runner.router.compression_supported(&recipient_hash)); + + runner.recall_outbound_app_data(); + let mut answered = false; + while let Ok(message) = rx.try_recv() { + if let TransportMessage::Rpc { + query: TransportQuery::RecallDestination { dest }, + response_tx, + } = message + && dest == recipient_hash + { + // Announce app data whose feature list lacks SF_COMPRESSION. + let entry = rns_transport::messages::RecalledDestinationRpcEntry { + dest_hash: recipient_hash, + public_key: recipient.get_public_key(), + app_data: Some(vec![0x93, 0xc4, 0x01, b'n', 0xc0, 0x90]), + ratchet: None, + hops: 1, + timestamp: now_f64(), + }; + let _ = response_tx.send(TransportQueryResponse::RecalledDestination(Some(entry))); + answered = true; + } + } + assert!( + answered, + "outbound processing recalls the destination's app data" + ); + runner.drain_identity_recalls(); + assert!(!runner.router.compression_supported(&recipient_hash)); + } + + #[tokio::test] + async fn an_opportunistic_message_records_its_ratchet_and_packet_phy_stats() { + let temp = tempfile::tempdir().unwrap(); + let (tx, mut rx) = mpsc::channel::(64); + initialize_storage_namespace(temp.path()).unwrap(); + let mut runner = LxmdRunner::new(DaemonConfig::default(), temp.path(), tx, None).unwrap(); + if runner + .delivery_ratchets + .ring() + .current_public_key() + .is_none() + { + let _ = runner.create_announce_packet(DeliveryAnnounceKind::Broadcast, None); + } + let ratchet_pub = runner + .delivery_ratchets + .ring() + .current_public_key() + .expect("delivery ratchet"); + let seen = Arc::new(Mutex::new(None)); + let observed = Arc::clone(&seen); + runner.router.register_delivery_callback(move |message| { + *observed.lock().unwrap() = Some(( + message.ratchet_id.clone(), + message.rssi, + message.snr, + message.q, + )); + }); + let message = signed_fixture( + runner.lxmf_dest_hash, + [0xCE; 16], + "", + "ratchet", + DeliveryMethod::Opportunistic, + ); + let packed = message.pack().unwrap(); + let ciphertext = runner + .identity + .encrypt(&packed[16..], Some(&ratchet_pub)) + .unwrap(); + let header = rns_wire::header::PacketHeader { + flags: rns_wire::flags::PacketFlags { + header_type: rns_wire::flags::HeaderType::Header1, + context_flag: false, + transport_type: rns_wire::flags::TransportType::Broadcast, + destination_type: rns_wire::flags::DestinationType::Single, + packet_type: rns_wire::flags::PacketType::Data, + }, + hops: 0, + transport_id: None, + destination_hash: runner.lxmf_dest_hash, + context: rns_wire::context::PacketContext::None, + }; + let mut packet = header.pack(); + packet.extend_from_slice(&ciphertext); + + while rx.try_recv().is_ok() {} + let packet_hash = + rns_wire::hash::packet_hash(&packet, rns_wire::flags::HeaderType::Header1); + let responder = tokio::spawn(async move { + while let Some(message) = rx.recv().await { + if let TransportMessage::Rpc { query, response_tx } = message { + let value = match query { + TransportQuery::GetPacketRssi { packet_hash: h } if h == packet_hash => { + Some(-91.0) + } + TransportQuery::GetPacketSnr { packet_hash: h } if h == packet_hash => { + Some(4.5) + } + TransportQuery::GetPacketQ { packet_hash: h } if h == packet_hash => None, + _ => continue, + }; + let _ = response_tx.send(TransportQueryResponse::FloatResult(value)); + } + } + }); + let phy = runner.inbound_packet_phy(&packet).await; + responder.abort(); + runner.handle_inbound_packet(&packet, phy); + + assert_eq!( + seen.lock().unwrap().clone(), + Some(( + Some(Identity::ratchet_id(&ratchet_pub).to_vec()), + Some(-91.0), + Some(4.5), + None + )) + ); + } + + #[tokio::test] + async fn a_propagated_message_fails_when_the_node_cost_stays_unknown() { + let temp = tempfile::tempdir().unwrap(); + let (tx, _rx) = mpsc::channel::(64); + initialize_storage_namespace(temp.path()).unwrap(); + let mut runner = LxmdRunner::new(DaemonConfig::default(), temp.path(), tx, None).unwrap(); + let node_hash = [0x5B; 16]; + let recipient = [0x6C; 16]; + for hash in [node_hash, recipient] { + runner + .known_identities + .insert(hex::encode(hash), Identity::new().get_public_key()); + } + runner.medium.routes.insert( + node_hash, + RouteInfo { + hops: 1, + first_hop_timeout: 6.0, + }, + ); + runner.router.outbound_propagation_node = Some(node_hash); + let draft = LxMessageDraft::new( + recipient, + runner.lxmf_dest_hash, + "", + "no node cost", + DeliveryMethod::Propagated, + ) + .unwrap(); + runner + .router + .submit_signed( + draft + .sign(&runner.identity.get_signing_key().unwrap()) + .unwrap(), + ) + .unwrap(); + let id = runner.router.pending_outbound[0].message_id(); + + assert!(runner.prepare_propagation_envelopes().contains(&id)); + assert_eq!(runner.router.pending_outbound.len(), 1); + // The path_request_wait deadline passes with no announce carrying the cost. + *runner.node_cost_deadlines.get_mut(&id).unwrap() = now_f64() - 1.0; + runner.prepare_propagation_envelopes(); + assert!(runner.router.pending_outbound.is_empty()); + } + + #[tokio::test] + async fn propagated_readiness_needs_a_current_transport_path_to_the_node() { + let temp = tempfile::tempdir().unwrap(); + let (tx, _rx) = mpsc::channel::(64); + initialize_storage_namespace(temp.path()).unwrap(); + let mut runner = LxmdRunner::new(DaemonConfig::default(), temp.path(), tx, None).unwrap(); + let node = Identity::new(); + let node_hash = [0x5A; 16]; + runner + .known_identities + .insert(hex::encode(node_hash), node.get_public_key()); + // A cached hop count outlives the transport path it was learned from. + runner.route_hops.insert(node_hash, 1); + let message = signed_fixture( + [0x6B; 16], + runner.lxmf_dest_hash, + "", + "propagated", + DeliveryMethod::Propagated, + ); + assert!(matches!( + runner.pack_message_for_propagation(&message, node_hash), + Err(PropagationPreparationError::Missing( + MissingPropagationInput::NodePath + )) + )); + } + #[tokio::test] async fn get_workers_are_bounded_and_cancelled_shutdown_keeps_actual_jobs_owned() { let workers = PnGetWorkers::default(); @@ -7713,6 +8186,44 @@ mod tests { assert!(!propagation_client_allowed(&restricted, None)); } + #[tokio::test] + async fn announce_request_bursts_are_bounded_and_do_not_hold_link_events() { + let (events_tx, events_rx) = mpsc::channel(16); + let (link_tx, mut link_rx) = mpsc::channel(16); + let (announce_tx, mut announce_rx) = mpsc::channel(DELIVERY_ANNOUNCE_BACKLOG); + let forwarder = tokio::spawn(forward_delivery_events(events_rx, link_tx, announce_tx)); + + // Nothing drains the announce backlog while the burst arrives. + for tag in 0..(DELIVERY_ANNOUNCE_BACKLOG as u32 * 4) { + let mut request = AnnounceRequest::normal(DELIVERY_APP_NAME.to_string()); + request.path_response = true; + request.tag = Some(tag.to_be_bytes().to_vec()); + events_tx + .send(DestinationEvent::AnnounceRequested(request)) + .await + .unwrap(); + } + events_tx + .send(DestinationEvent::LinkEstablished { link_id: [7; 16] }) + .await + .unwrap(); + assert!(matches!( + link_rx.recv().await, + Some(DestinationEvent::LinkEstablished { link_id }) if link_id == [7; 16] + )); + + drop(events_tx); + forwarder.await.unwrap(); + let mut kept = Vec::new(); + while let Some(request) = announce_rx.recv().await { + kept.push(request.tag.unwrap()); + } + // The oldest requests are kept, in order; the overflow is dropped. + assert_eq!(kept.len(), DELIVERY_ANNOUNCE_BACKLOG); + assert_eq!(kept[0], 0u32.to_be_bytes()); + assert_eq!(kept[1], 1u32.to_be_bytes()); + } + #[test] fn propagation_accounting_counts_duplicates_and_distributes_only_new_entries() { let peer_hash = [0x21; 16]; @@ -7738,6 +8249,7 @@ mod tests { assert!(peer.handled_messages.contains(&first_id)); assert!(peer.handled_messages.contains(&second_id)); assert_eq!(distribution, vec![(first_id, Some(peer_hash))]); + assert!(router.propagation_store.is_locally_processed(&first_id)); apply_propagation_store_commit( &mut router, @@ -7753,6 +8265,40 @@ mod tests { assert_eq!(distribution.last(), Some(&([0x41; 32], None))); } + #[tokio::test] + async fn a_locally_processed_id_is_a_duplicate_for_propagation_intake_at_once() { + let temp = tempfile::tempdir().unwrap(); + let (tx, _rx) = mpsc::channel::(64); + initialize_storage_namespace(temp.path()).unwrap(); + let mut runner = LxmdRunner::new(DaemonConfig::default(), temp.path(), tx, None).unwrap(); + let node = Arc::new(Mutex::new( + PropagationNode::with_storage( + PropagationNodeConfig::default(), + [0xAA; 16], + temp.path().join("store"), + ) + .unwrap(), + )); + runner.propagation_node = Some(Arc::clone(&node)); + let mut blob = vec![0x42; 16 + 64]; + let transient_id = rns_crypto::sha::full_hash(&blob); + blob.extend_from_slice(&[0; 32]); + runner + .router + .propagation_store + .claim_processed(transient_id, now_f64()); + + runner.share_processed_claim(transient_id); + + let blob = + lxmf_core::propagation_node::ValidatedOpaquePropagationBlob::validate(blob.into(), 0.0) + .unwrap(); + assert!(matches!( + node.lock().unwrap().plan_opaque_admission(blob), + lxmf_core::propagation_node::PropagationAdmission::AlreadyProcessed + )); + } + #[test] fn lossless_queue_high_water_only_moves_forward() { let mut high_water = 0; @@ -8151,8 +8697,16 @@ mod tests { let mut last = 0.0; let now = 1234.5; + let mut router = LxmRouter::new(Default::default()); + let medium = MediumSnapshot::default(); + assert!(queue_unknown_propagation_node_path_request( - &tx, node, &mut last, now + &tx, + &mut router, + &medium, + node, + &mut last, + now )); assert_eq!(last, now); match rx.try_recv().expect("path request") { @@ -8161,6 +8715,16 @@ mod tests { } other => panic!("expected RequestPath, got {other:?}"), } + // LXMRouter.request_path debounces a repeat inside path_request_wait. + assert!(queue_unknown_propagation_node_path_request( + &tx, + &mut router, + &medium, + node, + &mut last, + now + 1.0 + )); + assert!(rx.try_recv().is_err()); } #[test] @@ -8175,7 +8739,12 @@ mod tests { .expect("fill test channel"); assert!(!queue_unknown_propagation_node_path_request( - &tx, node, &mut last, 1234.5 + &tx, + &mut LxmRouter::new(Default::default()), + &MediumSnapshot::default(), + node, + &mut last, + 1234.5 )); assert_eq!(last, 1234.5); } @@ -8369,8 +8938,8 @@ mod tests { let mut packet = header.pack(); packet.extend_from_slice(&ciphertext); - runner.handle_inbound_packet(&packet); - runner.handle_link_delivered_data(&packed); + runner.handle_inbound_packet(&packet, None); + runner.handle_link_delivered_data_on(&packed, None); assert_eq!(deliveries.load(Ordering::Relaxed), 1); assert!(runner.router.has_message(&message_id)); diff --git a/crates/lxmf-tools/src/daemon.rs b/crates/lxmf-tools/src/daemon.rs index b408181..eb7e8be 100644 --- a/crates/lxmf-tools/src/daemon.rs +++ b/crates/lxmf-tools/src/daemon.rs @@ -3,6 +3,7 @@ //! Python reference: LXMF/Utilities/lxmd.py. use lxmf_core::constants::*; +pub use lxmf_core::handlers::PnLimit; use lxmf_core::propagation_admission::{ PN_DEFAULT_MAX_INBOUND_SYNCS, PN_MIN_MAX_INBOUND_SYNCS, PnInboundAdmissionConfig, }; @@ -32,8 +33,8 @@ pub struct PythonLxmdConfig { pub max_inbound_syncs: i64, pub node_announce_interval: Option, pub message_storage_limit: f64, - pub propagation_transfer_max_accepted_size: f64, - pub propagation_sync_max_accepted_size: f64, + pub propagation_transfer_max_accepted_size: PnLimit, + pub propagation_sync_max_accepted_size: PnLimit, pub propagation_stamp_cost_target: f64, pub propagation_stamp_cost_flexibility: f64, pub peering_cost: f64, @@ -148,13 +149,8 @@ impl PythonLxmdConfig { "propagation", "propagation_transfer_max_accepted_size", )?; - let propagation_transfer_max_accepted_size = get_float_or_floor( - propagation, - "propagation", - "propagation_message_max_accepted_size", - 256.0, - 0.38, - )?; + let propagation_transfer_max_accepted_size = + get_limit(propagation, "propagation_message_max_accepted_size", 256)?; let peer_stamp_cost = generated_cost( floored_cost(lxmf, "lxmf", "stamp_cost", 1.0, 12.0)?, @@ -269,12 +265,10 @@ impl PythonLxmdConfig { 0.005, )?, propagation_transfer_max_accepted_size, - propagation_sync_max_accepted_size: get_float_or_floor( + propagation_sync_max_accepted_size: get_limit( propagation, - "propagation", "propagation_sync_max_accepted_size", - 256.0 * 40.0, - 0.38, + 256 * 40, )?, propagation_stamp_cost_target, propagation_stamp_cost_flexibility, @@ -373,6 +367,19 @@ fn get_float_or_floor( Ok(get_cost(section, section_name, key)?.map_or(default, |value| value.max(floor))) } +/// lxmd's size limits: an int default, or the configured `as_float` value floored at 0.38. +fn get_limit( + section: Option<&ConfigSection>, + key: &'static str, + default: u64, +) -> Result { + Ok( + get_cost(section, "propagation", key)?.map_or(PnLimit::Int(default), |value| { + PnLimit::Float(value.max(0.38)) + }), + ) +} + fn get_list(section: Option<&ConfigSection>, key: &str) -> Vec { section .and_then(|sec| sec.get_list(key)) @@ -421,8 +428,8 @@ pub struct DaemonConfig { pub sequential_pn_stamp_validation: bool, pub static_peers_bypass_sequential: bool, pub max_inbound_syncs: usize, - pub propagation_limit_kb: f64, - pub sync_limit_kb: f64, + pub propagation_limit_kb: PnLimit, + pub sync_limit_kb: PnLimit, pub on_inbound_command: Option, pub node_announce_at_start: bool, pub node_announce_interval: Option, @@ -460,8 +467,8 @@ impl Default for DaemonConfig { sequential_pn_stamp_validation: true, static_peers_bypass_sequential: true, max_inbound_syncs: PN_DEFAULT_MAX_INBOUND_SYNCS, - propagation_limit_kb: PROPAGATION_LIMIT as f64, - sync_limit_kb: SYNC_LIMIT as f64, + propagation_limit_kb: PnLimit::Int(PROPAGATION_LIMIT as u64), + sync_limit_kb: PnLimit::Int(SYNC_LIMIT as u64), on_inbound_command: None, node_announce_at_start: false, node_announce_interval: None, @@ -487,9 +494,9 @@ impl DaemonConfig { propagation_enabled: self.propagation_enabled, autopeer: self.autopeer, max_peers: self.max_peers, - propagation_limit_kb: self.propagation_limit_kb, + propagation_limit_kb: self.propagation_limit_kb.as_f64(), delivery_limit_kb: self.delivery_transfer_max_accepted_size, - sync_limit_kb: self.sync_limit_kb, + sync_limit_kb: self.sync_limit_kb.as_f64(), propagation_stamp_cost: self.propagation_stamp_cost, propagation_stamp_flex: self.propagation_stamp_flex, stamp_cost: self.stamp_cost, @@ -587,13 +594,15 @@ impl DaemonConfig { && get_float(Some(sec), "propagation_transfer_max_accepted_size").is_none() && let Some(limit) = sec.get_uint("propagation_limit") { - dc.propagation_limit_kb = limit as f64; + dc.propagation_limit_kb = PnLimit::Int(limit); } dc.enforce_stamps = sec.get_bool_or("enforce_stamps", false); } // LXMRouter raises the per-sync limit to the per-transfer limit. - dc.sync_limit_kb = dc.sync_limit_kb.max(dc.propagation_limit_kb); + if dc.sync_limit_kb.as_f64() < dc.propagation_limit_kb.as_f64() { + dc.sync_limit_kb = dc.propagation_limit_kb; + } if let Some(sec) = config.section("control") { if !dc.auth_required { @@ -852,8 +861,8 @@ mod tests { dc.to_inbound_admission_config(), PnInboundAdmissionConfig::default() ); - assert_eq!(dc.propagation_limit_kb, 256.0); - assert_eq!(dc.sync_limit_kb, 10_240.0); + assert_eq!(dc.propagation_limit_kb.as_f64(), 256.0); + assert_eq!(dc.sync_limit_kb.as_f64(), 10_240.0); assert!(!dc.node_announce_at_start); assert_eq!(dc.node_announce_interval, None); assert_eq!(dc.message_storage_limit, Some(500_000_000)); @@ -883,8 +892,8 @@ mod tests { assert_eq!(py.max_inbound_syncs, PN_DEFAULT_MAX_INBOUND_SYNCS as i64); assert_eq!(py.node_announce_interval, None); assert_eq!(py.message_storage_limit, 500.0); - assert_eq!(py.propagation_transfer_max_accepted_size, 256.0); - assert_eq!(py.propagation_sync_max_accepted_size, 10240.0); + assert_eq!(py.propagation_transfer_max_accepted_size.as_f64(), 256.0); + assert_eq!(py.propagation_sync_max_accepted_size.as_f64(), 10240.0); assert_eq!(py.propagation_stamp_cost_target, 16.0); assert_eq!(py.propagation_stamp_cost_flexibility, 3.0); assert_eq!(py.peering_cost, 18.0); @@ -934,8 +943,8 @@ loglevel = 6 assert_eq!(py.peer_stamp_cost, 1.0); assert_eq!(py.delivery_transfer_max_accepted_size, 0.38); assert_eq!(py.message_storage_limit, 0.005); - assert_eq!(py.propagation_transfer_max_accepted_size, 0.38); - assert_eq!(py.propagation_sync_max_accepted_size, 0.38); + assert_eq!(py.propagation_transfer_max_accepted_size.as_f64(), 0.38); + assert_eq!(py.propagation_sync_max_accepted_size.as_f64(), 0.38); assert_eq!(py.propagation_stamp_cost_target, 13.0); assert_eq!(py.propagation_stamp_cost_flexibility, 0.0); assert_eq!(py.peering_cost, 0.0); @@ -967,7 +976,8 @@ propagation_transfer_max_accepted_size = 12 let py = PythonLxmdConfig::from_config(&config).unwrap(); assert_eq!( - py.propagation_transfer_max_accepted_size, 256.0, + py.propagation_transfer_max_accepted_size.as_f64(), + 256.0, "Python 0.9.6 ignores legacy propagation_transfer_max_accepted_size unless the newer key is set" ); } @@ -982,7 +992,8 @@ propagation_transfer_max_accepted_size = 12 let dc = DaemonConfig::from_config(&config).unwrap(); assert_eq!( - dc.propagation_limit_kb, 256.0, + dc.propagation_limit_kb.as_f64(), + 256.0, "DaemonConfig should match Python 0.9.6 handling of legacy propagation_transfer_max_accepted_size" ); } @@ -1074,8 +1085,8 @@ from_static_only = yes assert!(dc.node_announce_at_start); assert_eq!(dc.node_announce_interval, Some(120)); assert_eq!(dc.message_storage_limit, Some(5_000)); - assert_eq!(dc.propagation_limit_kb, 0.38); - assert_eq!(dc.sync_limit_kb, 0.38); + assert_eq!(dc.propagation_limit_kb.as_f64(), 0.38); + assert_eq!(dc.sync_limit_kb.as_f64(), 0.38); assert_eq!(dc.propagation_stamp_cost, 13.0); assert_eq!(dc.propagation_stamp_flex, 0.0); assert_eq!(dc.peering_cost, 0.0);