diff --git a/CHANGELOG.md b/CHANGELOG.md index cb8362f..60d472b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Interface start-up code is compiled once instead of again in every crate that starts the runtime, which makes `rnsd-rs` about 1.5 MB smaller. - Bluetooth RNodes report battery, temperature, and channel load in the interface stats, as USB RNodes already do (`battery_state`, `battery_percent`, `cpu_temp`, `noise_floor`). - Bluetooth Peer: on Android, a link the app closes after a failed write reports `Disconnected`, so the peer no longer stays listed as connected. On Linux, disconnecting a peer reports it too, and the Central role keeps looking for a Bluetooth adapter that appears after start-up instead of giving up until restart. +- Path requests are handled as in Python Reticulum 1.6.0. A node that knows a path but can't read its cached announce ignores the request instead of searching the network and blocking later requests for 45 seconds. A queued path response is dropped once other nodes are heard answering. Requests that arrive during a path-request burst are counted and checked before they wait, so duplicates no longer crowd out real ones. A search answer now also reaches a requester on the roaming interface it arrived on. - Announce handler events carry `emitted`, the time the announcer stamped into the announce, so an application can tell an older cached announce in a path response from a newer one. `AnnounceHandlerEvent` struct literals need the new field. ## 1.5.0 - 2026-11-13 diff --git a/crates/rns-transport/src/actor/mod.rs b/crates/rns-transport/src/actor/mod.rs index 74ef493..7063d68 100644 --- a/crates/rns-transport/src/actor/mod.rs +++ b/crates/rns-transport/src/actor/mod.rs @@ -89,8 +89,6 @@ pub struct TransportActor { rx: mpsc::Receiver, inbound_queues: Arc, inbound_rx: crate::inbound_queues::InboundReceivers, - /// Set while an ingress-limited queue item is processed. - processing_ingress_limited: bool, mailbox: mpsc::WeakSender, announce_dispatch_tx: mpsc::Sender, announce_dispatch_rx: mpsc::Receiver, @@ -501,7 +499,6 @@ impl TransportActor { rx, inbound_queues, inbound_rx, - processing_ingress_limited: false, mailbox: tx.downgrade(), announce_dispatch_tx, announce_dispatch_rx, @@ -696,8 +693,8 @@ impl TransportActor { _ = tokio::time::sleep(announce_wake.unwrap_or_default()), if announce_wake.is_some() => { self.process_announce_queues(now_f64()); } - (class, msg) = std::future::poll_fn(|cx| self.inbound_rx.poll_recv(cx, &self.inbound_queues)) => { - self.handle_inbound_event(class, msg); + (_, msg) = std::future::poll_fn(|cx| self.inbound_rx.poll_recv(cx, &self.inbound_queues)) => { + self.handle_inbound_event(msg); } Some(request) = self.announce_dispatch_rx.recv() => { self.handle_announce_dispatch(request); @@ -743,33 +740,23 @@ impl TransportActor { } } - /// Path requests from an interface in a path-request burst wait in the + /// Path requests classified during a path-request burst wait in the /// ingress-limited queue, behind every other class (Transport.py). - fn handle_inbound_event( - &mut self, - class: crate::inbound_queues::TrafficClass, - item: crate::inbound_queues::Queued, - ) { - use crate::inbound_queues::{Queued, TrafficClass}; + fn handle_inbound_event(&mut self, item: crate::inbound_queues::Queued) { + use crate::inbound_queues::Queued; match item { Queued::ReleasedAnnounce(announce) => self.on_released_announce(announce), - Queued::Message(msg) - if class == TrafficClass::PathRequest - && matches!(&*msg, TransportMessage::InterfaceEvent { interface_id, .. } - if self - .interfaces - .get_mut(interface_id) - .is_some_and(|entry| entry.ingress.should_ingress_limit_pr())) => - { - self.inbound_rx - .push_ingress_limited(&self.inbound_queues, Queued::Message(msg)); - } - Queued::Message(msg) => { - // Python carries the class on the packet into path_request (Transport.py). - self.processing_ingress_limited = class == TrafficClass::IngressLimited; - self.handle_message(*msg); - self.processing_ingress_limited = false; + Queued::PathRequest(request) => { + // Transport.py `_inbound` drops packets from an offline interface. + if self + .interfaces + .get(&request.interface_id) + .is_some_and(|entry| !interface_marked_offline(entry)) + { + self.path_request(*request, true); + } } + Queued::Message(msg) => self.handle_message(*msg), } } @@ -11171,6 +11158,16 @@ mod tests { } } + /// Answer the path requests a burst deferred, as the actor loop does. + fn drain_ingress_limited(actor: &mut TransportActor) { + let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); + while let std::task::Poll::Ready((_, item)) = + actor.inbound_rx.poll_recv(&mut cx, &actor.inbound_queues) + { + actor.handle_inbound_event(item); + } + } + fn enable_and_prime_egress_pr_limit(actor: &mut TransportActor, interface_id: InterfaceId) { let ingress = &mut actor .interfaces @@ -11539,6 +11536,7 @@ mod tests { let requested = [0xE0; 16]; actor.handle_inbound_path_request(&make_path_request_payload(requested, None), 1); + drain_ingress_limited(&mut actor); assert!( boundary_rx.try_recv().is_err(), @@ -11567,6 +11565,7 @@ mod tests { actor.destination_channels.insert(dest, event_tx); actor.handle_inbound_path_request(&make_path_request_payload_with_tag(dest, None, tag), 1); + drain_ingress_limited(&mut actor); let event = event_rx .try_recv() @@ -11627,6 +11626,7 @@ mod tests { ); actor.handle_inbound_path_request(&make_path_request_payload(dest, None), 1); + drain_ingress_limited(&mut actor); let queued = actor .announce_table @@ -11651,6 +11651,7 @@ mod tests { let requested = [0xE3; 16]; actor.handle_inbound_path_request(&make_path_request_payload(requested, None), 1); + drain_ingress_limited(&mut actor); gateway_rx .try_recv() @@ -12310,6 +12311,119 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + /// A known path whose announce cannot be read is answered by ignoring the + /// request (Transport.path_request): no search, nothing left in flight, so + /// the next request reads the cache again. + #[test] + fn uncached_known_path_is_ignored_without_holding_the_in_flight_gate() { + for mode in [InterfaceMode::Gateway, InterfaceMode::Full] { + let dir = std::env::temp_dir().join(format!("rns_path_request_cache_miss_{mode:?}")); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + + let (mut actor, _tx) = TransportActor::new(); + actor.is_transport_enabled = true; + actor.transport_identity_hash = Some([0x44; 16]); + actor.storage_dir = Some(dir.clone()); + actor.kd.attach_storage(&dir); + + let (mut requestor, _requestor_rx) = make_test_interface("requestor"); + requestor.mode = mode; + requestor.ingress = crate::ingress::IngressController::disabled(); + actor.interfaces.insert(1, requestor); + let (mut boundary, mut boundary_rx) = make_test_interface("boundary"); + boundary.mode = InterfaceMode::Boundary; + actor.interfaces.insert(2, boundary); + + let (raw, dest) = make_valid_announce("test.path.request.cache_miss", 1); + let ph = rns_wire::hash::packet_hash(&raw, rns_wire::flags::HeaderType::Header1); + let mut path_entry = + crate::path_table::PathEntry::new(Some([0xAB; 16]), 2, 2, InterfaceMode::Full); + path_entry.packet_hash = Some(ph); + actor.path_table.insert(dest, path_entry); + let request = + |tag: u8| make_path_request_payload_with_tag(dest, Some([0xCD; 16]), [tag; 16]); + + for tag in [0xA1, 0xB2] { + actor.handle_inbound_path_request(&request(tag), 1); + assert!(boundary_rx.try_recv().is_err(), "{mode:?}"); + assert!(actor.announce_table.get(&dest).is_none(), "{mode:?}"); + assert!( + !actor.discovery_path_requests.contains_key(&dest), + "{mode:?}" + ); + assert!( + !actor.inflight_path_requests.contains_key(&dest), + "{mode:?}" + ); + } + + crate::store::seed_cached_announce(&dir, &ph, &raw, None).unwrap(); + actor.handle_inbound_path_request(&request(0xC3), 1); + let entry = actor + .announce_table + .get(&dest) + .expect("answered once readable"); + assert_eq!(entry.retries, PATHFINDER_R, "{mode:?}"); + + let _ = std::fs::remove_dir_all(&dir); + } + } + + /// Transport.py queues a path response with `PATHFINDER_R` retries, so + /// hearing other nodes answer first cancels it. + #[test] + fn overheard_answers_cancel_a_queued_path_response() { + for (copies, raw_hops) in [(2u8, 2u8), (1, 3)] { + let dir = + std::env::temp_dir().join(format!("rns_path_response_cancel_{copies}_{raw_hops}")); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + + let (mut actor, _tx) = TransportActor::new(); + actor.is_transport_enabled = true; + actor.transport_identity_hash = Some([0x44; 16]); + actor.storage_dir = Some(dir.clone()); + actor.kd.attach_storage(&dir); + let (requestor, _requestor_rx) = make_test_interface("requestor"); + actor.interfaces.insert(1, requestor); + let (next_hop_iface, _rx2) = make_test_interface("next_hop_iface"); + actor.interfaces.insert(2, next_hop_iface); + + let (raw, dest) = make_valid_announce("test.path.response.cancel", 1); + let ph = rns_wire::hash::packet_hash(&raw, rns_wire::flags::HeaderType::Header1); + crate::store::seed_cached_announce(&dir, &ph, &raw, None).unwrap(); + let mut path_entry = + crate::path_table::PathEntry::new(Some([0xAB; 16]), 2, 2, InterfaceMode::Gateway); + path_entry.packet_hash = Some(ph); + // The path came from this announce, so the copies heard below are replays. + let blob_at = rns_wire::constants::HEADER_MINSIZE + 64 + 10; + path_entry.add_random_blob(raw[blob_at..blob_at + 10].try_into().unwrap()); + actor.path_table.insert(dest, path_entry); + + actor.handle_inbound_path_request(&make_path_request_payload(dest, None), 1); + assert!(actor.announce_table.get(&dest).is_some()); + + for relay in 0..copies { + let mut heard = vec![raw[0] | 0x50, raw_hops]; + heard.extend_from_slice(&[0x60 + relay; 16]); + heard.extend_from_slice(&raw[2..]); + actor.on_inbound(InboundPacket { + raw: Bytes::from(heard), + interface_id: 1, + rssi: None, + snr: None, + q: None, + }); + } + assert!( + actor.announce_table.get(&dest).is_none(), + "copies={copies} raw_hops={raw_hops}" + ); + let _ = std::fs::remove_dir_all(&dir); + } + } + #[test] fn known_path_response_restores_displaced_announce() { let dir = std::env::temp_dir().join("rns_path_request_held"); diff --git a/crates/rns-transport/src/actor/outbound.rs b/crates/rns-transport/src/actor/outbound.rs index 9dc4cd6..7b1eb95 100644 --- a/crates/rns-transport/src/actor/outbound.rs +++ b/crates/rns-transport/src/actor/outbound.rs @@ -674,7 +674,6 @@ impl TransportActor { data.get(PR_LENGTH..).and_then(|ext| ext.try_into().ok()); (Some(id), &data[32..48], ext) }; - let tag_bytes = Some(tag); let now = now_f64(); let mut unique_tag = [0u8; 32]; @@ -714,19 +713,21 @@ impl TransportActor { } } } - // Python counts a path request once its tag and stamp pass these gates - // (Transport.py). + // Python counts a path request once its tag and stamp pass these gates, + // then classifies it against the updated burst state (Transport.py). self.record_interface_event(interface_id, InterfaceEvent::ReceivedPathRequest, raw_len); self.count_transport_event(InterfaceEvent::ReceivedPathRequest); + let ingress_limited = self + .interfaces + .get_mut(&interface_id) + .is_some_and(|iface| iface.ingress.should_ingress_limit_pr()); // Transport.py: a request for a destination already // in flight joins the waiting requesters instead of being processed. if self.inflight_path_requests.contains_key(&requested_dest) { // A leaf answers batched requesters only for its local clients; // upstream would also emit PATH_RESPONSEs naming the leaf as relay. - if (self.is_transport_enabled || is_from_local_client) - && !self.processing_ingress_limited - { + if (self.is_transport_enabled || is_from_local_client) && !ingress_limited { pathing!( dest = %hex::encode(requested_dest), interface_id, @@ -738,6 +739,50 @@ impl TransportActor { } self.inflight_path_requests.insert(requested_dest, now); + let mut tag_bytes = [0u8; 16]; + tag_bytes.copy_from_slice(tag); + let request = crate::inbound_queues::PendingPathRequest { + interface_id, + destination: requested_dest, + requestor: requestor_transport_id, + tag: tag_bytes, + ext, + }; + if ingress_limited { + // Transport.py: answered after every other class, still in flight. + self.inbound_rx.push_ingress_limited( + &self.inbound_queues, + crate::inbound_queues::Queued::PathRequest(Box::new(request)), + ); + return; + } + self.path_request(request, false); + } + + /// Python `Transport.path_request` for a request past the inbound gates. + pub(super) fn path_request( + &mut self, + request: crate::inbound_queues::PendingPathRequest, + ingress_limited: bool, + ) { + let crate::inbound_queues::PendingPathRequest { + interface_id, + destination: requested_dest, + requestor: requestor_transport_id, + tag, + ext, + } = request; + let tag = &tag[..]; + let tag_bytes = Some(tag); + let now = now_f64(); + let is_from_local_client = self.is_local_client_interface(interface_id); + // Transport.py consults the limiter for every request it processes. + let should_ingress_limit = ingress_limited + || self + .interfaces + .get_mut(&interface_id) + .is_some_and(|iface| iface.ingress.should_ingress_limit_pr()); + if self.local_destinations.contains(&requested_dest) { pathing!( dest = %hex::encode(requested_dest), @@ -787,81 +832,86 @@ impl TransportActor { .is_some_and(|iface| iface.mode == InterfaceMode::Roaming) && path.interface_id == interface_id; - if requestor_is_next_hop || same_roaming_interface { - // Transport.py: answered, even when declined. - self.inflight_path_requests.remove(&requested_dest); - } - if requestor_is_next_hop { + // Transport.py: a known path answers the request even when the + // reply is declined or its announce cannot be read. + let Some(raw) = path + .packet_hash + .and_then(|hash| self.cached_announce_raw(&hash)) + else { pathing!( dest = %hex::encode(requested_dest), - "not answering path request — requester is our next hop" + "ignoring path request — cached announce for the known path is unavailable" ); + self.inflight_path_requests.remove(&requested_dest); return; - } + }; if same_roaming_interface { pathing!( dest = %hex::encode(requested_dest), "not answering path request on roaming interface learned from same interface" ); - return; - } - - let cached_raw: Option<(u8, Vec)> = path - .packet_hash - .and_then(|hash| self.cached_announce_raw(&hash)) - .map(|raw| (path.hops, raw)); - if let Some((hops, raw)) = cached_raw { - // Python 1.3.8 Transport.py: unparseable cached announce - // aborts the answer entirely (no fall-through to discovery). - let Some(response) = - self.path_response_from_cached_announce(&raw, requested_dest, hops) - else { - return; - }; - pathing!( - dest = %hex::encode(requested_dest), - hops, - "answering path request — path is known, queueing path response" - ); - let now = now_f64(); - let path_on_local_client = self.is_local_client_interface(path.interface_id); - let response_delay = if is_from_local_client || path_on_local_client { - 0.0 - } else { - let roaming_extra = if self - .interfaces - .get(&interface_id) - .is_some_and(|iface| iface.mode == InterfaceMode::Roaming) - { - PATH_REQUEST_RG - } else { - 0.0 - }; - PATH_REQUEST_GRACE + roaming_extra - }; - if let Some(held_entry) = self.announce_table.remove(&requested_dest) { - self.held_announces.insert(requested_dest, held_entry); - } - self.announce_table.insert( - requested_dest, - crate::announce::AnnounceEntry { - timestamp: now, - retransmit_timeout: now + response_delay, - retries: 0, - hops, - packet_raw: response, - local_rebroadcasts: 0, - block_rebroadcast: true, - attached_interface: Some(interface_id), - source_interface: None, - }, - ); - if !self.shared_instance_client_mode { - self.use_destination(&requested_dest); - } self.inflight_path_requests.remove(&requested_dest); return; } + // Transport.py: an unparseable cached announce aborts the answer, + // leaving the request in flight. + let Some(response) = + self.path_response_from_cached_announce(&raw, requested_dest, path.hops) + else { + return; + }; + if requestor_is_next_hop { + pathing!( + dest = %hex::encode(requested_dest), + "not answering path request — requester is our next hop" + ); + self.inflight_path_requests.remove(&requested_dest); + return; + } + let hops = path.hops; + pathing!( + dest = %hex::encode(requested_dest), + hops, + "answering path request — path is known, queueing path response" + ); + let path_on_local_client = self.is_local_client_interface(path.interface_id); + let response_delay = if is_from_local_client || path_on_local_client { + 0.0 + } else { + let roaming_extra = if self + .interfaces + .get(&interface_id) + .is_some_and(|iface| iface.mode == InterfaceMode::Roaming) + { + PATH_REQUEST_RG + } else { + 0.0 + }; + PATH_REQUEST_GRACE + roaming_extra + }; + if let Some(held_entry) = self.announce_table.remove(&requested_dest) { + self.held_announces.insert(requested_dest, held_entry); + } + self.announce_table.insert( + requested_dest, + crate::announce::AnnounceEntry { + timestamp: now, + retransmit_timeout: now + response_delay, + // Transport.py: lets an overheard answer cancel this one. + retries: PATHFINDER_R, + hops, + packet_raw: response, + local_rebroadcasts: 0, + block_rebroadcast: true, + attached_interface: Some(interface_id), + source_interface: None, + }, + ); + if !self.shared_instance_client_mode { + self.use_destination(&requested_dest); + } + self.inflight_path_requests.remove(&requested_dest); + return; } // Python 1.3.8 Transport.py: recursive_prs forces discovery @@ -886,12 +936,7 @@ impl TransportActor { None, ); } else if self.is_transport_enabled && should_search_unknown { - let ingress_limited = self.processing_ingress_limited - || self - .interfaces - .get_mut(&interface_id) - .is_some_and(|iface| iface.ingress.should_ingress_limit_pr()); - if ingress_limited { + if should_ingress_limit { pathing!( dest = %hex::encode(requested_dest), interface_id, @@ -1336,14 +1381,14 @@ mod cache_miss_tests { use crate::messages::InterfaceEntry; use crate::path_table::PathEntry; + /// Transport.py answers a known path whose announce cannot be read by + /// ignoring the request: no discovery, and nothing left in flight. #[test] - fn live_path_without_cached_announce_uses_normal_discovery_gates() { - // A Boundary requester searches only Boundary and Gateway interfaces - // (Transport.py), so the Full route gets no request. - for (mode, discovers, sent) in [ - (InterfaceMode::Gateway, true, true), - (InterfaceMode::Boundary, true, false), - (InterfaceMode::Full, false, false), + fn live_path_without_cached_announce_is_ignored() { + for mode in [ + InterfaceMode::Gateway, + InterfaceMode::Boundary, + InterfaceMode::Full, ] { let (mut actor, _actor_tx) = TransportActor::new(); actor.is_transport_enabled = true; @@ -1383,11 +1428,9 @@ mod cache_miss_tests { let mut request = requested.to_vec(); request.extend_from_slice(&[0x55; 16]); actor.handle_inbound_path_request(&request, 1); - assert_eq!(egress_rx.try_recv().is_ok(), sent); - assert_eq!( - actor.discovery_path_requests.contains_key(&requested), - discovers - ); + assert!(egress_rx.try_recv().is_err(), "{mode:?}"); + assert!(!actor.discovery_path_requests.contains_key(&requested)); + assert!(!actor.inflight_path_requests.contains_key(&requested)); assert!(actor.announce_table.is_empty()); } } diff --git a/crates/rns-transport/src/actor/recursive_discovery.rs b/crates/rns-transport/src/actor/recursive_discovery.rs index 1fc7beb..088b9f3 100644 --- a/crates/rns-transport/src/actor/recursive_discovery.rs +++ b/crates/rns-transport/src/actor/recursive_discovery.rs @@ -314,8 +314,7 @@ impl TransportActor { && self.interfaces.get(&id).is_some_and(|entry| { !interface_marked_offline(entry) // A radio can serve hidden peers; an IPC edge is one peer. - && (id != incoming || (entry.role == InterfaceRole::Normal - && entry.mode != InterfaceMode::Roaming)) + && (id != incoming || entry.role == InterfaceRole::Normal) }); if eligible { self.send_or_retain_announce(id, response, true); diff --git a/crates/rns-transport/src/actor/tests/held_announces.rs b/crates/rns-transport/src/actor/tests/held_announces.rs index 428fa80..d87bcb8 100644 --- a/crates/rns-transport/src/actor/tests/held_announces.rs +++ b/crates/rns-transport/src/actor/tests/held_announces.rs @@ -41,7 +41,7 @@ fn release_held(actor: &mut TransportActor) { actor.inbound_rx.poll_recv(&mut cx, &actor.inbound_queues) { assert_eq!(class, crate::inbound_queues::TrafficClass::IngressLimited); - actor.handle_inbound_event(class, item); + actor.handle_inbound_event(item); } } diff --git a/crates/rns-transport/src/actor/tests/inbound_queues.rs b/crates/rns-transport/src/actor/tests/inbound_queues.rs index 7f9135e..87d4236 100644 --- a/crates/rns-transport/src/actor/tests/inbound_queues.rs +++ b/crates/rns-transport/src/actor/tests/inbound_queues.rs @@ -32,6 +32,12 @@ fn inbound(raw: Bytes) -> TransportMessage { } fn path_request(tag: u8) -> Bytes { + path_request_for(&make_path_request_payload_with_tag( + [0x77; 16], None, [tag; 16], + )) +} + +fn path_request_for(payload: &[u8]) -> Bytes { let header = rns_wire::header::PacketHeader { flags: rns_wire::flags::PacketFlags { header_type: rns_wire::flags::HeaderType::Header1, @@ -49,9 +55,7 @@ fn path_request(tag: u8) -> Bytes { context: rns_wire::context::PacketContext::None, }; let mut raw = header.pack(); - raw.extend_from_slice(&make_path_request_payload_with_tag( - [0x77; 16], None, [tag; 16], - )); + raw.extend_from_slice(payload); Bytes::from(raw) } @@ -130,14 +134,17 @@ fn queued_classes_drain_data_first_then_announce_then_path_request() { fn path_requests_during_a_path_request_burst_wait_behind_every_other_class() { let (mut actor, sender) = registered(lengths(8, 8, 8, 1)); prime_ingress_pr_burst(&mut actor, 1); - for tag in 1..=3u8 { - sender.try_send(inbound(path_request(tag))).unwrap(); + for dest in 1..=3u8 { + let payload = make_path_request_payload_with_tag([dest; 16], None, [dest; 16]); + sender + .try_send(inbound(path_request_for(&payload))) + .unwrap(); } - // Each path request leaves the PR queue for the one-slot IL queue. + // Each path request passes its gates, then waits in the one-slot IL queue. for _ in 0..3 { let (class, msg) = next(&mut actor).unwrap(); assert_eq!(class, TrafficClass::PathRequest); - actor.handle_inbound_event(class, msg); + actor.handle_inbound_event(msg); } let snapshot = actor.inbound_queues.snapshot(); assert_eq!(snapshot.heights, [0, 0, 0, 1]); @@ -151,15 +158,58 @@ fn path_requests_during_a_path_request_burst_wait_behind_every_other_class() { assert_eq!(actor.inbound_queues.snapshot().heights[3], 0); } +/// Transport.py classifies after the size, tag, and stamp gates, so a +/// rejected request never takes an ingress-limited slot. +#[test] +fn a_rejected_path_request_takes_no_ingress_limited_slot() { + let (mut actor, sender) = registered(lengths(8, 8, 8, 1)); + prime_ingress_pr_burst(&mut actor, 1); + sender + .try_send(inbound(path_request_for(&[0x31; 16]))) + .unwrap(); + let payload = make_path_request_payload_with_tag([0x32; 16], None, [0x32; 16]); + sender + .try_send(inbound(path_request_for(&payload))) + .unwrap(); + while let Some((_, msg)) = next(&mut actor) { + actor.handle_inbound_event(msg); + if actor.inbound_queues.snapshot().heights[3] == 1 { + break; + } + } + let snapshot = actor.inbound_queues.snapshot(); + assert_eq!(snapshot.heights, [0, 0, 0, 1]); + assert_eq!(snapshot.dropped, [0; 4]); + assert!(actor.inflight_path_requests.contains_key(&[0x32; 16])); + assert!(!actor.inflight_path_requests.contains_key(&[0x31; 16])); +} + +/// Transport.py registers a request as in flight on receipt, so a second +/// request during the burst is dropped instead of queued behind the first. +#[test] +fn a_queued_ingress_limited_path_request_is_already_in_flight() { + let (mut actor, sender) = registered(lengths(8, 8, 8, 8)); + actor.is_transport_enabled = true; + prime_ingress_pr_burst(&mut actor, 1); + for tag in [1u8, 2] { + sender.try_send(inbound(path_request(tag))).unwrap(); + let (_, msg) = next(&mut actor).unwrap(); + actor.handle_inbound_event(msg); + } + assert!(actor.inflight_path_requests.contains_key(&[0x77; 16])); + assert_eq!(actor.inbound_queues.snapshot().heights[3], 1); + assert!( + !actor.discovery_path_requests.contains_key(&[0x77; 16]), + "an ingress-limited duplicate is not batched" + ); +} + /// Python decides at preprocess time and passes `ingress_limited` into /// `path_request`, so a queued ingress-limited request is never forwarded /// recursively even if the burst ended meanwhile (Transport.py). #[test] fn an_ingress_limited_path_request_is_not_forwarded_recursively() { - for (class, forwarded) in [ - (TrafficClass::PathRequest, true), - (TrafficClass::IngressLimited, false), - ] { + for limited in [false, true] { let (mut actor, tx) = TransportActor::new_with_queues(64, 1024, lengths(8, 8, 8, 8)); actor.is_transport_enabled = true; let (mut entry, _wire) = make_test_interface("requestor"); @@ -168,9 +218,24 @@ fn an_ingress_limited_path_request_is_not_forwarded_recursively() { actor.handle_message(TransportMessage::RegisterInterface { id: 1, entry }); let (outbound, mut outbound_rx) = make_test_interface("outbound"); actor.interfaces.insert(2, outbound); + if limited { + prime_ingress_pr_burst(&mut actor, 1); + } sender.try_send(inbound(path_request(1))).unwrap(); let (_, item) = next(&mut actor).unwrap(); - actor.handle_inbound_event(class, item); - assert_eq!(outbound_rx.try_recv().is_ok(), forwarded, "{class:?}"); + actor.handle_inbound_event(item); + if limited { + assert!(outbound_rx.try_recv().is_err(), "deferred"); + actor.interfaces.get_mut(&1).unwrap().ingress = + crate::ingress::IngressController::new(); + let (class, item) = next(&mut actor).unwrap(); + assert_eq!(class, TrafficClass::IngressLimited); + actor.handle_inbound_event(item); + } + assert_eq!( + outbound_rx.try_recv().is_ok(), + !limited, + "limited={limited}" + ); } } diff --git a/crates/rns-transport/src/actor/tests/slow_media_discovery.rs b/crates/rns-transport/src/actor/tests/slow_media_discovery.rs index eb0b512..54238b6 100644 --- a/crates/rns-transport/src/actor/tests/slow_media_discovery.rs +++ b/crates/rns-transport/src/actor/tests/slow_media_discovery.rs @@ -106,7 +106,8 @@ fn recursive_timeout_samples_only_known_online_useful_egress_and_stays_bounded() #[test] fn recursive_requesters_share_one_fanout_and_response_reaches_same_radio() { - for (mode, same_response) in [(InterfaceMode::Full, true), (InterfaceMode::Roaming, false)] { + // Transport.py answers every requester, roaming radios included. + for mode in [InterfaceMode::Full, InterfaceMode::Roaming] { let start = now_f64(); let clock = crate::test_clock::Clock::at(start); let mut actor = relay(); @@ -140,11 +141,7 @@ fn recursive_requesters_share_one_fanout_and_response_reaches_same_radio() { assert!(second_rx.try_recv().is_err()); inbound(&mut actor, announce.clone(), 2); assert_response(first_rx.try_recv().unwrap(), dest); - if same_response { - assert_response(second_rx.try_recv().unwrap(), dest); - } else { - assert!(second_rx.try_recv().is_err()); - } + assert_response(second_rx.try_recv().unwrap(), dest); inbound(&mut actor, announce, 2); assert!(first_rx.try_recv().is_err() && second_rx.try_recv().is_err()); assert!(actor.discovery_path_requests.is_empty()); diff --git a/crates/rns-transport/src/inbound_queues.rs b/crates/rns-transport/src/inbound_queues.rs index 993ca33..aff0cff 100644 --- a/crates/rns-transport/src/inbound_queues.rs +++ b/crates/rns-transport/src/inbound_queues.rs @@ -85,6 +85,18 @@ pub(crate) enum Queued { Message(Box), /// A held announce the interface released (Interface.py). ReleasedAnnounce(crate::ingress::HeldAnnounce), + /// A path request classified during its interface's burst, gates passed. + PathRequest(Box), +} + +/// A path request past the inbound gates, as `Transport.path_request` takes it. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct PendingPathRequest { + pub(crate) interface_id: crate::messages::InterfaceId, + pub(crate) destination: [u8; 16], + pub(crate) requestor: Option<[u8; 16]>, + pub(crate) tag: [u8; 16], + pub(crate) ext: Option<[u8; 16]>, } pub(crate) struct InboundReceivers {