From 27de48acc160f1bf56e4a591ddd4f923893b60cc Mon Sep 17 00:00:00 2001 From: DeFiDude <59237470+DeFiDude@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:33:47 -0700 Subject: [PATCH] transport: skip interfaces that already sent us a queued announce --- crates/rns-transport/src/actor/inbound.rs | 16 ++ crates/rns-transport/src/actor/maintenance.rs | 7 +- crates/rns-transport/src/actor/mod.rs | 33 +++- crates/rns-transport/src/actor/outbound.rs | 2 + .../src/actor/tests/announce_admission.rs | 2 + .../src/actor/tests/announce_rebroadcast.rs | 172 ++++++++++++++++-- crates/rns-transport/src/announce.rs | 11 ++ crates/rns-transport/src/messages.rs | 11 ++ 8 files changed, 229 insertions(+), 25 deletions(-) diff --git a/crates/rns-transport/src/actor/inbound.rs b/crates/rns-transport/src/actor/inbound.rs index 1cfa971..60c1bd3 100644 --- a/crates/rns-transport/src/actor/inbound.rs +++ b/crates/rns-transport/src/actor/inbound.rs @@ -1000,6 +1000,8 @@ impl TransportActor { block_rebroadcast: false, attached_interface: None, source_interface: Some(interface_id), + packet_hash: announce_packet_hash, + dedup_interfaces: Vec::new(), }; self.announce_table .insert(header.destination_hash, announce_entry); @@ -1095,6 +1097,18 @@ impl TransportActor { "path learned from announce" ); } else { + // Transport.py: a copy of the queued announce from an interface + // off a shared medium needs no rebroadcast back onto it. + if self + .interfaces + .get(&interface_id) + .is_some_and(|entry| !entry.shared_medium) + && let Some(queued) = self.announce_table.get_mut(&header.destination_hash) + && queued.packet_hash == announce_packet_hash + && !queued.dedup_interfaces.contains(&interface_id) + { + queued.dedup_interfaces.push(interface_id); + } debug!( dest = hex::encode(header.destination_hash), hops = header.hops, @@ -1150,6 +1164,8 @@ impl TransportActor { block_rebroadcast: false, attached_interface: None, source_interface: Some(interface_id), + packet_hash: announce_packet_hash, + dedup_interfaces: Vec::new(), }, ); } diff --git a/crates/rns-transport/src/actor/maintenance.rs b/crates/rns-transport/src/actor/maintenance.rs index 98c9775..473b006 100644 --- a/crates/rns-transport/src/actor/maintenance.rs +++ b/crates/rns-transport/src/actor/maintenance.rs @@ -93,9 +93,10 @@ impl TransportActor { e.source_interface, e.attached_interface, e.block_rebroadcast, + e.dedup_interfaces.clone(), ) }); - if let Some((raw, _source_iface, Some(attached_interface), true)) = send_info { + if let Some((raw, _source_iface, Some(attached_interface), true, _)) = send_info { let timestamp = self.announce_table.get(&hash).unwrap().timestamp; self.admit_cached_response( *hash.as_bytes(), @@ -112,8 +113,8 @@ impl TransportActor { // The helper excludes an exact shared IPC peer and applies // mode policy. A broadcast radio may need same-interface // retransmission to reach a different physical neighbor. - if let Some((raw, source_iface, _, _)) = send_info { - self.broadcast_announce_on_interfaces(&raw, source_iface); + if let Some((raw, source_iface, _, _, skip)) = send_info { + self.broadcast_announce_skipping(&raw, source_iface, &skip); } } self.announce_table.cull_exhausted(PATHFINDER_R); diff --git a/crates/rns-transport/src/actor/mod.rs b/crates/rns-transport/src/actor/mod.rs index 83be0db..ff2c17a 100644 --- a/crates/rns-transport/src/actor/mod.rs +++ b/crates/rns-transport/src/actor/mod.rs @@ -2284,8 +2284,14 @@ impl TransportActor { /// non-local Internal egress and hop-0 announces send at once, uncapped. /// Otherwise an announce sends at once when the queue is empty, the cap /// window is open and the bitrate is known; else it is queued, replacing a - /// queued copy only when newer, and refused when the queue is full. - fn broadcast_announce_on_interfaces(&mut self, raw: &[u8], except: Option) { + /// queued copy only when newer, and refused when the queue is full. The + /// `skip` interfaces are Python's `blocked_interfaces`. + fn broadcast_announce_skipping( + &mut self, + raw: &[u8], + except: Option, + skip: &[InterfaceId], + ) { let destination_hash = rns_wire::header::PacketHeader::unpack(raw) .ok() .map(|(h, _)| h.destination_hash) @@ -2302,7 +2308,9 @@ impl TransportActor { .interfaces .keys() .copied() - .filter(|id| self.interface_allows_announce(*id, &destination_hash, except)) + .filter(|id| { + !skip.contains(id) && self.interface_allows_announce(*id, &destination_hash, except) + }) .collect(); for id in ids { @@ -2365,6 +2373,11 @@ impl TransportActor { } } + #[cfg(test)] + fn broadcast_announce_on_interfaces(&mut self, raw: &[u8], except: Option) { + self.broadcast_announce_skipping(raw, except, &[]); + } + /// Python `Transport.transmit` then `sent_announce`: the announce counts /// as sent whatever the interface does with it. fn send_queued_announce(&mut self, id: InterfaceId, raw: &[u8]) { @@ -2459,9 +2472,10 @@ impl TransportActor { e.source_interface, e.attached_interface, e.block_rebroadcast, + e.dedup_interfaces.clone(), ) }); - if let Some((raw, _source_iface, Some(attached_interface), true)) = send_info { + if let Some((raw, _source_iface, Some(attached_interface), true, _)) = send_info { self.send_to_interface(attached_interface, &raw); self.announce_table.remove(&hash); if let Some(held_entry) = self.held_announces.remove(hash.as_bytes()) { @@ -2473,8 +2487,8 @@ impl TransportActor { entry.retries += 1; entry.retransmit_timeout = far_future + PATHFINDER_G + rand_window(); } - if let Some((raw, source_iface, _, _)) = send_info { - self.broadcast_announce_on_interfaces(&raw, source_iface); + if let Some((raw, source_iface, _, _, skip)) = send_info { + self.broadcast_announce_skipping(&raw, source_iface, &skip); } } self.flush_announce_queues(); @@ -2922,6 +2936,7 @@ mod tests { announces_from_internal: true, announces_to_internal: None, gravity: 0, + shared_medium: false, destination_cost: None, pr_cost: None, ingress: crate::ingress::IngressController::new(), @@ -3446,6 +3461,8 @@ mod tests { block_rebroadcast: false, attached_interface: None, source_interface: None, + packet_hash: [0; 32], + dedup_interfaces: Vec::new(), }, ); @@ -5944,6 +5961,8 @@ mod tests { block_rebroadcast: false, attached_interface: None, source_interface: None, + packet_hash: [0; 32], + dedup_interfaces: Vec::new(), }; actor.announce_table.insert(dest_hash, announce_entry); @@ -12556,6 +12575,8 @@ mod tests { block_rebroadcast: false, attached_interface: None, source_interface: Some(2), + packet_hash: [0; 32], + dedup_interfaces: Vec::new(), }, ); diff --git a/crates/rns-transport/src/actor/outbound.rs b/crates/rns-transport/src/actor/outbound.rs index 21c0497..d24a426 100644 --- a/crates/rns-transport/src/actor/outbound.rs +++ b/crates/rns-transport/src/actor/outbound.rs @@ -920,6 +920,8 @@ impl TransportActor { block_rebroadcast: true, attached_interface: Some(interface_id), source_interface: None, + packet_hash: path.packet_hash.unwrap_or_default(), + dedup_interfaces: Vec::new(), }, ); if !self.shared_instance_client_mode { diff --git a/crates/rns-transport/src/actor/tests/announce_admission.rs b/crates/rns-transport/src/actor/tests/announce_admission.rs index e220904..da56d23 100644 --- a/crates/rns-transport/src/actor/tests/announce_admission.rs +++ b/crates/rns-transport/src/actor/tests/announce_admission.rs @@ -81,6 +81,8 @@ fn cache_entry(raw: &Bytes, timestamp: f64, attached: bool) -> crate::announce:: block_rebroadcast: attached, attached_interface: attached.then_some(1), source_interface: Some(2), + packet_hash: [0; 32], + dedup_interfaces: Vec::new(), } } diff --git a/crates/rns-transport/src/actor/tests/announce_rebroadcast.rs b/crates/rns-transport/src/actor/tests/announce_rebroadcast.rs index 76feca7..d5c0094 100644 --- a/crates/rns-transport/src/actor/tests/announce_rebroadcast.rs +++ b/crates/rns-transport/src/actor/tests/announce_rebroadcast.rs @@ -1,9 +1,32 @@ -//! Python 1.6.1 rebroadcast rules (Transport.py): an unresponsive path +//! Python 1.6.1 rebroadcast rules (Transport.py): a queued announce heard +//! again on an interface off a shared medium is not sent back there +//! (`IDX_AT_DEDUP_IFS`, `blocked_interfaces`), and an unresponsive path //! replaced by the same emission is not rebroadcast (`table_insertion_only`). use super::*; const APP: &str = "rebroadcast.test"; +struct Hub { + actor: TransportActor, + rx: HashMap>, +} + +/// A transport node with interfaces 1–3; `shared` marks shared-medium ones. +fn hub(shared: &[InterfaceId]) -> Hub { + let (mut actor, _) = TransportActor::new(); + actor.is_transport_enabled = true; + actor.transport_identity_hash = Some([0x42; 16]); + let mut rx = HashMap::new(); + for id in [1, 2, 3] { + let (mut entry, receiver) = make_test_interface(&format!("rebroadcast_{id}")); + entry.ingress = crate::ingress::IngressController::disabled(); + entry.shared_medium = shared.contains(&id); + actor.interfaces.insert(id, entry); + rx.insert(id, receiver); + } + Hub { actor, rx } +} + fn inbound(actor: &mut TransportActor, raw: &Bytes, interface_id: InterfaceId) { actor.on_inbound(InboundPacket { raw: raw.clone(), @@ -14,33 +37,150 @@ fn inbound(actor: &mut TransportActor, raw: &Bytes, interface_id: InterfaceId) { }); } +impl Hub { + /// Frames sent on an interface since the last call. + fn sent(&mut self, id: InterfaceId) -> usize { + let rx = self.rx.get_mut(&id).unwrap(); + let mut sent = 0; + while rx.try_recv().is_ok() { + sent += 1; + } + sent + } + + fn drain(&mut self) { + for id in [1, 2, 3] { + self.sent(id); + } + } +} + +fn announce(hops: u8) -> (Bytes, [u8; 16]) { + let identity = rns_identity::identity::Identity::new(); + make_announce_for_with_random_blob(&identity, APP, hops, random_blob(0x3C, 5000)) +} + +#[test] +fn a_queued_announce_heard_again_off_a_shared_medium_skips_that_interface() { + let _clock = crate::test_clock::Clock::at(1_800_000_000.0); + let mut hub = hub(&[]); + let (raw, destination) = announce(0); + inbound(&mut hub.actor, &raw, 1); + inbound(&mut hub.actor, &raw, 2); + assert_eq!( + hub.actor + .announce_table + .get(&destination) + .unwrap() + .dedup_interfaces, + vec![2] + ); + hub.drain(); + + hub.actor.flush_pending_announces(); + + assert_eq!(hub.sent(2), 0, "interface 2 already has it"); + assert_eq!((hub.sent(1), hub.sent(3)), (1, 1)); +} + +#[test] +fn a_copy_heard_on_a_shared_medium_is_still_rebroadcast_there() { + let _clock = crate::test_clock::Clock::at(1_800_000_000.0); + let mut hub = hub(&[2]); + let (raw, destination) = announce(0); + inbound(&mut hub.actor, &raw, 1); + inbound(&mut hub.actor, &raw, 2); + assert!( + hub.actor + .announce_table + .get(&destination) + .unwrap() + .dedup_interfaces + .is_empty() + ); + hub.drain(); + + hub.actor.flush_pending_announces(); + + assert_eq!((hub.sent(1), hub.sent(2), hub.sent(3)), (1, 1, 1)); +} + +/// Only a copy of the queued packet itself counts (Transport.py compares +/// packet hashes). +#[test] +fn a_different_announce_for_the_destination_is_not_a_copy() { + let _clock = crate::test_clock::Clock::at(1_800_000_000.0); + let mut hub = hub(&[]); + let identity = rns_identity::identity::Identity::new(); + let (current, destination) = + make_announce_for_with_random_blob(&identity, APP, 0, random_blob(0x01, 5000)); + let (older, _) = make_announce_for_with_random_blob(&identity, APP, 0, random_blob(0x02, 4999)); + inbound(&mut hub.actor, ¤t, 1); + inbound(&mut hub.actor, &older, 2); + assert!( + hub.actor + .announce_table + .get(&destination) + .unwrap() + .dedup_interfaces + .is_empty() + ); +} + +/// The path response retransmits on its attached interface whatever the +/// dedup set says (Transport.py `outbound` with `attached_interface`). +#[tokio::test(start_paused = true)] +async fn an_attached_path_response_ignores_the_dedup_set() { + let mut hub = hub(&[]); + let (raw, destination) = announce(1); + let response = Bytes::from( + hub.actor + .path_response_from_cached_announce(&raw, destination, 1) + .unwrap(), + ); + let now = now_f64(); + hub.actor.announce_table.insert( + destination, + crate::announce::AnnounceEntry { + timestamp: now, + retransmit_timeout: now - 1.0, + retries: PATHFINDER_R, + hops: 1, + packet_raw: response.to_vec(), + local_rebroadcasts: 0, + block_rebroadcast: true, + attached_interface: Some(2), + source_interface: None, + packet_hash: [0; 32], + dedup_interfaces: vec![2], + }, + ); + hub.actor.last_announces_check = 0.0; + + hub.actor.on_tick(); + + assert_eq!(hub.rx.get_mut(&2).unwrap().try_recv().unwrap(), response); +} + /// Transport.py `table_insertion_only`: the same emission with more hops /// replaces an unresponsive path but queues no rebroadcast. #[test] fn an_unresponsive_path_taken_by_the_same_emission_is_not_rebroadcast() { let _clock = crate::test_clock::Clock::at(1_800_000_000.0); - let (mut actor, _) = TransportActor::new(); - actor.is_transport_enabled = true; - actor.transport_identity_hash = Some([0x42; 16]); - let mut rx = Vec::new(); - for id in [1, 2] { - let (entry, receiver) = make_test_interface(&format!("rebroadcast_{id}")); - actor.interfaces.insert(id, entry); - rx.push(receiver); - } + let mut hub = hub(&[]); let identity = rns_identity::identity::Identity::new(); let blob = random_blob(0xA4, 6000); let (current, destination) = make_announce_for_with_random_blob(&identity, APP, 1, blob); let (replay, _) = make_announce_for_with_random_blob(&identity, APP, 5, blob); - inbound(&mut actor, ¤t, 1); - actor.announce_table.remove(&destination); - actor + inbound(&mut hub.actor, ¤t, 1); + hub.actor.announce_table.remove(&destination); + hub.actor .path_table .set_state(destination, crate::constants::PathState::Unresponsive); - inbound(&mut actor, &replay, 2); + inbound(&mut hub.actor, &replay, 2); - let path = actor.path_table.get(&destination).unwrap(); + let path = hub.actor.path_table.get(&destination).unwrap(); assert_eq!((path.hops, path.interface_id), (6, 2)); - assert!(actor.announce_table.get(&destination).is_none()); + assert!(hub.actor.announce_table.get(&destination).is_none()); } diff --git a/crates/rns-transport/src/announce.rs b/crates/rns-transport/src/announce.rs index 786908f..03c9ecd 100644 --- a/crates/rns-transport/src/announce.rs +++ b/crates/rns-transport/src/announce.rs @@ -25,6 +25,11 @@ pub struct AnnounceEntry { /// Receiving interface used by egress policy. Shared IPC peers are excluded /// from an echo; an eligible broadcast radio may relay on the same interface. pub source_interface: Option, + /// Hash of the announce as received, Python's `IDX_AT_PACKET.packet_hash`. + pub packet_hash: [u8; 32], + /// Interfaces off a shared medium that already sent us this announce; the + /// rebroadcast skips them (Python `IDX_AT_DEDUP_IFS`). + pub dedup_interfaces: Vec, } /// Destination-keyed rebroadcast records. Insertions replace matching entries; @@ -119,6 +124,8 @@ mod tests { block_rebroadcast: false, attached_interface: None, source_interface: None, + packet_hash: [0; 32], + dedup_interfaces: Vec::new(), }, ); assert_eq!(table.len(), 1); @@ -140,6 +147,8 @@ mod tests { block_rebroadcast: false, attached_interface: None, source_interface: None, + packet_hash: [0; 32], + dedup_interfaces: Vec::new(), }, ); table.insert( @@ -154,6 +163,8 @@ mod tests { block_rebroadcast: false, attached_interface: None, source_interface: None, + packet_hash: [0; 32], + dedup_interfaces: Vec::new(), }, ); diff --git a/crates/rns-transport/src/messages.rs b/crates/rns-transport/src/messages.rs index 50b8794..95c46ee 100644 --- a/crates/rns-transport/src/messages.rs +++ b/crates/rns-transport/src/messages.rs @@ -422,6 +422,10 @@ pub struct InterfaceEntry { /// Path preference among equal announces; a copy of the same emission /// heard on a higher-gravity interface replaces the path. pub gravity: i64, + /// Python `shared_medium`: other peers may not hear what one peer sent, + /// as on a radio, a serial line or UDP. A queued announce heard again on + /// an interface that is not shared is not rebroadcast there. + pub shared_medium: bool, /// Configured announce stamp requirement; `None` follows /// `StampPolicy::destination_cost`. Resolve with `StampPolicy::interface_destination_cost`. pub destination_cost: Option, @@ -490,6 +494,7 @@ impl InterfaceEntry { announces_from_internal: true, announces_to_internal: None, gravity: 0, + shared_medium: false, destination_cost: None, pr_cost: None, } @@ -554,6 +559,12 @@ impl InterfaceEntry { self } + /// Mark the interface a shared medium (Python `shared_medium`). + pub fn with_shared_medium(mut self, shared_medium: bool) -> Self { + self.shared_medium = shared_medium; + self + } + /// Store an IFAC key and size in bytes. This builder does not validate the /// size; `effective_clear_mtu` rejects active sizes outside 1..=64. pub fn with_ifac(mut self, key: [u8; 64], size: usize) -> Self {