diff --git a/crates/rns-transport/src/actor/inbound.rs b/crates/rns-transport/src/actor/inbound.rs index 1490be6..b27e22b 100644 --- a/crates/rns-transport/src/actor/inbound.rs +++ b/crates/rns-transport/src/actor/inbound.rs @@ -183,8 +183,8 @@ impl TransportActor { } } - /// Python's end of `preprocess_inbound` for the frame being processed. - pub(super) fn accept_inbound(&mut self, verdict: InboundVerdict) { + /// Python's `preprocess_inbound` verdict on the frame being processed. + pub(super) fn set_inbound_verdict(&mut self, verdict: InboundVerdict) { self.inbound_verdict = verdict; } @@ -431,7 +431,7 @@ impl TransportActor { let path_request = parsed.flags.packet_type != rns_wire::flags::PacketType::Announce && parsed.destination_hash == Self::path_request_dest_hash(); if parsed.flags.packet_type != rns_wire::flags::PacketType::Announce && !path_request { - self.accept_inbound(InboundVerdict::Accepted); + self.set_inbound_verdict(InboundVerdict::Accepted); } // Announce gates run after the packet filter and before the hash is @@ -788,7 +788,7 @@ impl TransportActor { } self.record_interface_event(interface_id, InterfaceEvent::ReceivedAnnounce, raw.len()); self.count_transport_event(InterfaceEvent::ReceivedAnnounce); - self.accept_inbound(InboundVerdict::Accepted); + self.set_inbound_verdict(InboundVerdict::Accepted); if let Some(validated_identity) = validated_identity.as_ref() { let known_public_key = self @@ -1408,8 +1408,8 @@ impl TransportActor { .remove(&rns_wire::hash::packet_hash(raw, header.flags.header_type)); return; } - if let Some(tx) = self.destination_channels.get(&header.destination_hash) - && let Err(e) = tx.try_send(crate::link_messages::DestinationEvent::InboundPacket { + if let Some(tx) = self.destination_channels.get(&header.destination_hash) { + match tx.try_send(crate::link_messages::DestinationEvent::InboundPacket { interface_owner: self .interfaces .get(&interface_id) @@ -1417,11 +1417,21 @@ impl TransportActor { raw: raw.clone(), interface_id, metrics, - }) - { - self.channel_drops += 1; - warn!(dest = hex::encode(header.destination_hash), drops = self.channel_drops, err = %e, - "failed to deliver InboundPacket to local destination (channel full)"); + }) { + // Python's `Link.receive` (Link.py:1034). + Ok(()) + if header.flags.destination_type + == rns_wire::flags::DestinationType::Link => + { + self.count_link_bytes(raw.len(), 0); + } + Ok(()) => {} + Err(e) => { + self.channel_drops += 1; + warn!(dest = hex::encode(header.destination_hash), drops = self.channel_drops, err = %e, + "failed to deliver InboundPacket to local destination (channel full)"); + } + } } return; } @@ -1591,6 +1601,7 @@ impl TransportActor { receiving_owner, requested_mode, accepted_mtu_ceiling, + traffic: 0, }; self.link_table.insert(link_id, link_entry); @@ -2072,8 +2083,8 @@ impl TransportActor { } } - if let Some(tx) = self.destination_channels.get(&header.destination_hash) - && let Err(e) = tx.try_send(crate::link_messages::DestinationEvent::InboundPacket { + if let Some(tx) = self.destination_channels.get(&header.destination_hash) { + match tx.try_send(crate::link_messages::DestinationEvent::InboundPacket { interface_owner: self .interfaces .get(&_interface_id) @@ -2081,11 +2092,18 @@ impl TransportActor { raw: raw.clone(), interface_id: _interface_id, metrics, - }) - { - self.channel_drops += 1; - error!(dest = hex::encode(header.destination_hash), drops = self.channel_drops, err = %e, - "failed to deliver link proof to local link"); + }) { + // Python hands only resource proofs to `Link.receive`. + Ok(()) if header.context == rns_wire::context::PacketContext::ResourcePrf => { + self.count_link_bytes(raw.len(), 0); + } + Ok(()) => {} + Err(e) => { + self.channel_drops += 1; + error!(dest = hex::encode(header.destination_hash), drops = self.channel_drops, err = %e, + "failed to deliver link proof to local link"); + } + } } } @@ -2127,6 +2145,29 @@ impl TransportActor { self.protocol_violation(interface_id); return true; } + let skip_hashlist = matches!( + header.context, + rns_wire::context::PacketContext::Keepalive + | rns_wire::context::PacketContext::Resource + | rns_wire::context::PacketContext::ResourceReq + | rns_wire::context::PacketContext::ResourcePrf + | rns_wire::context::PacketContext::CacheRequest + | rns_wire::context::PacketContext::Channel + ); + let pkt_hash = rns_wire::hash::packet_hash(raw, header.flags.header_type); + // The hashlist check deferred from ingress: Python's packet filter. + if !skip_hashlist && self.packet_hashlist.contains(&pkt_hash) { + trace!( + link_id = hex::encode(header.destination_hash), + packet_kind, "duplicate link-table packet dropped" + ); + self.packet_filter_hit(interface_id); + self.set_inbound_verdict(InboundVerdict::Filtered); + return true; + } + if !from_local_client { + self.count_link_bytes(raw.len(), 0); + } if !self.link_registration_is_current(&entry) || raw.len() > entry.accepted_mtu_ceiling as usize { @@ -2192,29 +2233,17 @@ impl TransportActor { self.local_hops_delta }; } - let skip_hashlist = matches!( - header.context, - rns_wire::context::PacketContext::Keepalive - | rns_wire::context::PacketContext::Resource - | rns_wire::context::PacketContext::ResourceReq - | rns_wire::context::PacketContext::ResourcePrf - | rns_wire::context::PacketContext::CacheRequest - | rns_wire::context::PacketContext::Channel - ); if !skip_hashlist { - let pkt_hash = rns_wire::hash::packet_hash(raw, header.flags.header_type); - if !self.packet_hashlist.insert(pkt_hash) { - trace!( - link_id = hex::encode(header.destination_hash), - packet_kind, "duplicate link-table packet dropped after claim" - ); - return true; - } + self.packet_hashlist.insert(pkt_hash); } if let Some(entry) = self.link_table.get_mut(&header.destination_hash) { entry.timestamp = now_f64(); + entry.traffic = entry.traffic.saturating_add(forwarded.len() as u64); } self.send_to_interface(target_interface, &forwarded); + if !self.is_local_client_interface(target_interface) { + self.count_link_bytes(0, forwarded.len()); + } // Transport.py if let Some(path) = self.path_table.get_mut(&entry.destination_hash) { path.mark_used(); diff --git a/crates/rns-transport/src/actor/link_endpoint.rs b/crates/rns-transport/src/actor/link_endpoint.rs index ad58843..226db8e 100644 --- a/crates/rns-transport/src/actor/link_endpoint.rs +++ b/crates/rns-transport/src/actor/link_endpoint.rs @@ -455,12 +455,27 @@ impl TransportActor { Ok(()) } + /// Python counts every packet an own Link sends in `link_txb` (Packet.py:312). fn try_send_link_endpoint_raw( &mut self, interface_id: InterfaceId, link_id: [u8; 16], role: crate::messages::LinkEndpointRole, raw: &[u8], + ) -> InterfaceSendOutcome { + let outcome = self.try_send_link_endpoint_raw_uncounted(interface_id, link_id, role, raw); + if outcome == InterfaceSendOutcome::Sent { + self.count_link_bytes(0, raw.len()); + } + outcome + } + + fn try_send_link_endpoint_raw_uncounted( + &mut self, + interface_id: InterfaceId, + link_id: [u8; 16], + role: crate::messages::LinkEndpointRole, + raw: &[u8], ) -> InterfaceSendOutcome { use crate::messages::LinkEndpointRole; diff --git a/crates/rns-transport/src/actor/mod.rs b/crates/rns-transport/src/actor/mod.rs index a6dae52..1a98da6 100644 --- a/crates/rns-transport/src/actor/mod.rs +++ b/crates/rns-transport/src/actor/mod.rs @@ -4182,6 +4182,7 @@ mod tests { receiving_owner: relay.interfaces.get(&(1)).map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: Some([0xBB; 16]), @@ -4254,6 +4255,7 @@ mod tests { receiving_owner: relay.interfaces.get(&(1)).map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: None, @@ -4307,6 +4309,7 @@ mod tests { receiving_owner: relay.interfaces.get(&(1)).map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: None, @@ -4362,6 +4365,7 @@ mod tests { receiving_owner: relay.interfaces.get(&(1)).map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: None, @@ -4413,6 +4417,7 @@ mod tests { receiving_owner: relay.interfaces.get(&(1)).map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: None, @@ -4460,6 +4465,7 @@ mod tests { receiving_owner: relay.interfaces.get(&(1)).map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: None, @@ -4516,6 +4522,7 @@ mod tests { receiving_owner: relay.interfaces.get(&(1)).map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: None, @@ -4581,6 +4588,7 @@ mod tests { receiving_owner: relay.interfaces.get(&(1)).map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: None, @@ -4639,6 +4647,7 @@ mod tests { .map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: Some([0xDD; 16]), @@ -4675,6 +4684,7 @@ mod tests { .map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: Some([0xCC; 16]), @@ -8983,6 +8993,7 @@ mod tests { receiving_owner: actor.interfaces.get(&(1)).map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: None, @@ -9024,6 +9035,7 @@ mod tests { receiving_owner: actor.interfaces.get(&(1)).map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: None, @@ -9075,6 +9087,7 @@ mod tests { receiving_owner: actor.interfaces.get(&(1)).map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: None, @@ -9119,6 +9132,7 @@ mod tests { receiving_owner: actor.interfaces.get(&(1)).map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: None, diff --git a/crates/rns-transport/src/actor/outbound.rs b/crates/rns-transport/src/actor/outbound.rs index cf09175..f401eb4 100644 --- a/crates/rns-transport/src/actor/outbound.rs +++ b/crates/rns-transport/src/actor/outbound.rs @@ -715,7 +715,7 @@ impl TransportActor { } // Python counts a path request once its tag and stamp pass these gates, // then classifies it against the updated burst state (Transport.py). - self.accept_inbound(super::inbound::InboundVerdict::Batched); + self.set_inbound_verdict(super::inbound::InboundVerdict::Batched); self.record_interface_event(interface_id, InterfaceEvent::ReceivedPathRequest, raw_len); self.count_transport_event(InterfaceEvent::ReceivedPathRequest); let ingress_limited = self @@ -738,7 +738,7 @@ impl TransportActor { } return; } - self.accept_inbound(super::inbound::InboundVerdict::Accepted); + self.set_inbound_verdict(super::inbound::InboundVerdict::Accepted); self.inflight_path_requests.insert(requested_dest, now); let mut tag_bytes = [0u8; 16]; diff --git a/crates/rns-transport/src/actor/rpc.rs b/crates/rns-transport/src/actor/rpc.rs index c19f632..9126c63 100644 --- a/crates/rns-transport/src/actor/rpc.rs +++ b/crates/rns-transport/src/actor/rpc.rs @@ -187,6 +187,9 @@ impl TransportActor { TransportQuery::GetTrafficTotals => { TransportQueryResponse::TrafficTotals(self.traffic_totals()) } + TransportQuery::GetLinkDistribution => { + TransportQueryResponse::LinkDistribution(self.link_distribution()) + } TransportQuery::GetRateTable => { let entries: Vec = self .rate_table @@ -828,6 +831,8 @@ pub(super) struct TrafficCount { /// Python `r_stat_rssi`, `r_stat_snr` and `r_stat_q` per interface. signal: HashMap, last_pps: Option<(std::time::Instant, u64, u64)>, + /// `link_rxb` and `link_txb` at `last_pps`. + last_link_bytes: (u64, u64), last_count: f64, } @@ -840,6 +845,7 @@ impl Default for TrafficCount { speeds: HashMap::new(), signal: HashMap::new(), last_pps: None, + last_link_bytes: (0, 0), last_count: 0.0, } } @@ -902,6 +908,46 @@ impl TransportActor { self.traffic_count.totals.rx_packets += 1; } + /// Python 1.6.1 `link_rxb` and `link_txb` (Transport.py, Link.py, Packet.py). + pub(super) fn count_link_bytes(&mut self, rx: usize, tx: usize) { + let totals = &mut self.traffic_count.totals; + totals.lrxb = totals.lrxb.saturating_add(rx as u64); + totals.ltxb = totals.ltxb.saturating_add(tx as u64); + } + + /// Python `Transport.link_distribution()`: every link table entry is a + /// relayed Link (Transport.py:3786-3814). + fn link_distribution(&self) -> Vec { + let mut by_destination: HashMap<[u8; 16], crate::messages::LinkDistributionRpcEntry> = + HashMap::new(); + for (_, link) in self.link_table.iter() { + let entry = by_destination + .entry(link.destination_hash) + .or_insert_with(|| crate::messages::LinkDistributionRpcEntry { + destination_hash: link.destination_hash, + ..Default::default() + }); + entry.traffic = entry.traffic.saturating_add(link.traffic); + if link.validated { + entry.active += 1; + } else { + entry.pending += 1; + } + if let Some(interface) = self.interfaces.get(&link.receiving_interface) { + let hash = rns_crypto::sha::sha256(interface.upstream_name().as_bytes()); + if !entry.from_interfaces.contains(&hash) { + entry.from_interfaces.push(hash); + } + } + } + let mut entries: Vec<_> = by_destination.into_values().collect(); + for entry in &mut entries { + entry.from_interfaces.sort_unstable(); + } + entries.sort_unstable_by_key(|entry| entry.destination_hash); + entries + } + /// Python `Transport.tx_packets += 1` per `Transport.transmit` (Transport.py). pub(super) fn count_tx_packet(&mut self) { self.traffic_count.totals.tx_packets += 1; @@ -998,8 +1044,12 @@ impl TransportActor { ((totals.rx_packets.saturating_sub(rxp)) as f64 / elapsed).round() as u64; totals.txpps = ((totals.tx_packets.saturating_sub(txp)) as f64 / elapsed).round() as u64; + let (lrxb, ltxb) = count.last_link_bytes; + totals.lrxs = totals.lrxb.saturating_sub(lrxb) as f64 * 8.0 / elapsed; + totals.ltxs = totals.ltxb.saturating_sub(ltxb) as f64 * 8.0 / elapsed; } count.last_pps = Some((at, totals.rx_packets, totals.tx_packets)); + count.last_link_bytes = (totals.lrxb, totals.ltxb); totals.measured = measured; totals.rxb += deltas[0]; totals.txb += deltas[1]; @@ -1171,6 +1221,7 @@ mod tests { receiving_owner: None, requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, } } diff --git a/crates/rns-transport/src/actor/tests/interface_stats.rs b/crates/rns-transport/src/actor/tests/interface_stats.rs index 732b362..2f3568e 100644 --- a/crates/rns-transport/src/actor/tests/interface_stats.rs +++ b/crates/rns-transport/src/actor/tests/interface_stats.rs @@ -672,3 +672,197 @@ fn a_child_adds_accepted_and_filtered_bytes_to_its_parent() { ); assert_eq!(parent.filtered_tx_bytes, 0); } + +fn transit_link( + actor: &mut TransportActor, + link_id: [u8; 16], + destination_hash: [u8; 16], + validated: bool, +) { + actor.link_table.insert( + link_id, + crate::link_table::LinkEntry { + interface_owner: actor.interfaces.get(&2).map(|entry| entry.owner.clone()), + receiving_owner: actor.interfaces.get(&1).map(|entry| entry.owner.clone()), + requested_mode: 1, + accepted_mtu_ceiling: 500, + traffic: 0, + timestamp: now_f64(), + next_hop: Some([0xBB; 16]), + interface_id: 2, + remaining_hops: 1, + destination_hash, + validated, + proof_timeout: now_f64() + 120.0, + receiving_interface: 1, + taken_hops: 1, + }, + ); +} + +fn link_packet(link_id: [u8; 16], tag: u8) -> Bytes { + packet( + DestinationType::Link, + PacketType::Data, + 0, + None, + link_id, + &[tag; 24], + ) +} + +/// Python 1.6.1 counts relayed Link packets in `link_rxb` and `link_txb` +/// unless they come from or go to a local client, and every forwarded byte +/// in the Link's `IDX_LT_TRXB` (Transport.py:2509-2549). +#[test] +fn relayed_link_packets_count_as_link_traffic() { + let mut actor = hub(); + let mut initiator_rx = attach(&mut actor, 1); + let mut destination_rx = attach(&mut actor, 2); + let link_id = [0x81; 16]; + transit_link(&mut actor, link_id, [0x82; 16], true); + + let outbound = link_packet(link_id, 1); + inbound(&mut actor, outbound.clone(), 1); + assert!( + destination_rx.try_recv().is_ok(), + "forwarded toward the destination" + ); + let inbound_reply = link_packet(link_id, 2); + inbound(&mut actor, inbound_reply.clone(), 2); + assert!( + initiator_rx.try_recv().is_ok(), + "forwarded toward the initiator" + ); + + let both = (outbound.len() + inbound_reply.len()) as u64; + let measured = totals(&mut actor); + assert_eq!((measured.lrxb, measured.ltxb), (both, both)); + assert_eq!(actor.link_table.get(&link_id).unwrap().traffic, both); + + inbound(&mut actor, outbound.clone(), 1); + assert!( + destination_rx.try_recv().is_err(), + "a duplicate is not relayed" + ); + assert_eq!(stats(&mut actor, 1).packet_filter_hits, 1); + assert_eq!( + stats(&mut actor, 1).filtered_rx_bytes, + outbound.len() as u64, + "the deferred duplicate check filters, as Python's packet filter" + ); + assert_eq!(totals(&mut actor).lrxb, both); + + actor.interfaces.get_mut(&1).unwrap().role = InterfaceRole::LocalClient; + // A local client's packets keep zero hops. + actor.link_table.get_mut(&link_id).unwrap().taken_hops = 0; + let from_client = link_packet(link_id, 3); + inbound(&mut actor, from_client.clone(), 1); + assert!(destination_rx.try_recv().is_ok()); + let to_client = link_packet(link_id, 4); + inbound(&mut actor, to_client.clone(), 2); + assert!(initiator_rx.try_recv().is_ok()); + let measured = totals(&mut actor); + assert_eq!( + (measured.lrxb, measured.ltxb), + ( + both + to_client.len() as u64, + both + from_client.len() as u64 + ), + "local client legs are not link traffic" + ); + assert_eq!( + actor.link_table.get(&link_id).unwrap().traffic, + both + (from_client.len() + to_client.len()) as u64, + "the per-link counter keeps every forwarded byte" + ); + + std::thread::sleep(Duration::from_millis(5)); + actor.count_traffic(1_000.0); + let measured = totals(&mut actor); + assert!(measured.lrxs > 0.0 && measured.ltxs > 0.0); +} + +/// Python's `link_distribution()` groups relayed Links by destination with +/// their traffic and the hashes of the interfaces they came in on. +#[test] +fn link_distribution_groups_relayed_links_by_destination() { + let mut actor = hub(); + let _initiator_rx = attach(&mut actor, 1); + let mut destination_rx = attach(&mut actor, 2); + let destination = [0x83; 16]; + transit_link(&mut actor, [0x84; 16], destination, true); + transit_link(&mut actor, [0x85; 16], destination, false); + transit_link(&mut actor, [0x86; 16], [0x87; 16], true); + let relayed = link_packet([0x84; 16], 5); + inbound(&mut actor, relayed.clone(), 1); + assert!(destination_rx.try_recv().is_ok()); + + let TransportQueryResponse::LinkDistribution(entries) = + actor.handle_query(TransportQuery::GetLinkDistribution) + else { + panic!("unexpected link distribution response"); + }; + let from = vec![rns_crypto::sha::sha256(b"stats_1")]; + assert_eq!(entries.len(), 2); + assert_eq!(entries[0].destination_hash, destination); + assert_eq!((entries[0].active, entries[0].pending), (1, 1)); + assert_eq!(entries[0].traffic, relayed.len() as u64); + assert_eq!(entries[0].from_interfaces, from); + assert_eq!(entries[1].destination_hash, [0x87; 16]); + assert_eq!( + (entries[1].active, entries[1].pending, entries[1].traffic), + (1, 0, 0) + ); +} + +/// An own Link's packets count as link traffic: what it receives +/// (`Link.receive`, Link.py:1034) and what it sends (Packet.py:312). +#[test] +fn an_own_links_packets_count_as_link_traffic() { + let mut actor = hub(); + let mut rx = attach(&mut actor, 1); + let link_id = [0x88; 16]; + let (delivery_tx, mut delivery_rx) = mpsc::channel(4); + actor.local_destinations.insert(link_id); + actor.destination_channels.insert(link_id, delivery_tx); + let received_packet = link_packet(link_id, 6); + inbound(&mut actor, received_packet.clone(), 1); + assert!(delivery_rx.try_recv().is_ok()); + + let binding = crate::messages::LinkEndpointBinding { + interface_owner: actor.interfaces.get(&1).map(|entry| entry.owner.id()), + link_id, + interface_id: 1, + role: crate::messages::LinkEndpointRole::Initiator, + }; + let (lifecycle_tx, _lifecycle_rx) = mpsc::unbounded_channel(); + let (bind_tx, _bind_rx) = tokio::sync::oneshot::channel(); + actor.handle_message(TransportMessage::BindLinkEndpoint { + binding, + lifecycle_tx, + result_tx: bind_tx, + }); + let sent = link_packet(link_id, 7); + let (send_tx, mut send_rx) = tokio::sync::oneshot::channel(); + actor.handle_message(TransportMessage::SendLinkEndpoint { + link_id, + role: crate::messages::LinkEndpointRole::Initiator, + request: OutboundRequest { + raw: sent.clone(), + destination_hash: link_id, + }, + result_tx: send_tx, + }); + assert_eq!( + send_rx.try_recv().unwrap(), + crate::messages::LinkEndpointSendResult::Sent + ); + assert!(rx.try_recv().is_ok()); + + let measured = totals(&mut actor); + assert_eq!( + (measured.lrxb, measured.ltxb), + (received_packet.len() as u64, sent.len() as u64) + ); +} diff --git a/crates/rns-transport/src/actor/tests/packet_capacity.rs b/crates/rns-transport/src/actor/tests/packet_capacity.rs index 07341e5..bf3eea3 100644 --- a/crates/rns-transport/src/actor/tests/packet_capacity.rs +++ b/crates/rns-transport/src/actor/tests/packet_capacity.rs @@ -247,6 +247,7 @@ fn packet_capacity_authenticated_proof_obeys_request_ceiling_and_keeps_original_ receiving_owner: Some(first_owner), requested_mode: 1, accepted_mtu_ceiling: 1000, + traffic: 0, }, ); for signal in [SignallingData::new(2, 500), SignallingData::new(1, 1001)] { diff --git a/crates/rns-transport/src/actor/tests/path_request_tags.rs b/crates/rns-transport/src/actor/tests/path_request_tags.rs index 73e0e05..3def718 100644 --- a/crates/rns-transport/src/actor/tests/path_request_tags.rs +++ b/crates/rns-transport/src/actor/tests/path_request_tags.rs @@ -218,6 +218,7 @@ async fn blocked_interface_rediscovery_shares_one_tag() { receiving_owner: None, requested_mode: 0, accepted_mtu_ceiling: 500, + traffic: 0, }, ); }); 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 54238b6..776dce6 100644 --- a/crates/rns-transport/src/actor/tests/slow_media_discovery.rs +++ b/crates/rns-transport/src/actor/tests/slow_media_discovery.rs @@ -520,6 +520,7 @@ fn transit_table_capacity_and_driver_refusal_do_not_create_untracked_forwarding( .map(|entry| entry.owner.clone()), requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now_f64(), next_hop: None, diff --git a/crates/rns-transport/src/link_table.rs b/crates/rns-transport/src/link_table.rs index 79200ba..afddfd9 100644 --- a/crates/rns-transport/src/link_table.rs +++ b/crates/rns-transport/src/link_table.rs @@ -27,6 +27,8 @@ pub struct LinkEntry { pub receiving_owner: Option, pub requested_mode: u8, pub accepted_mtu_ceiling: u32, + /// Bytes forwarded over this Link (Python `IDX_LT_TRXB`). + pub traffic: u64, } /// Metadata retained when an unvalidated link expires before proof. @@ -236,6 +238,7 @@ mod tests { receiving_owner: None, requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: 1000.0, next_hop: None, @@ -264,6 +267,7 @@ mod tests { receiving_owner: None, requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: 1000.0, next_hop: Some([0xDD; 16]), @@ -293,6 +297,7 @@ mod tests { receiving_owner: None, requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: 1000.0, next_hop: None, @@ -329,6 +334,7 @@ mod tests { receiving_owner: None, requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now, next_hop: None, @@ -349,6 +355,7 @@ mod tests { receiving_owner: None, requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: 100.0, next_hop: None, @@ -369,6 +376,7 @@ mod tests { receiving_owner: None, requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now, next_hop: None, @@ -389,6 +397,7 @@ mod tests { receiving_owner: None, requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now, next_hop: None, @@ -426,6 +435,7 @@ mod tests { receiving_owner: None, requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now, next_hop: None, @@ -446,6 +456,7 @@ mod tests { receiving_owner: None, requested_mode: 1, accepted_mtu_ceiling: 500, + traffic: 0, timestamp: now, next_hop: None, diff --git a/crates/rns-transport/src/messages.rs b/crates/rns-transport/src/messages.rs index 8c6a372..db88810 100644 --- a/crates/rns-transport/src/messages.rs +++ b/crates/rns-transport/src/messages.rs @@ -1048,6 +1048,9 @@ pub enum TransportQuery { /// The top-level traffic, packet, link and path counts of Python's /// `get_interface_stats()`, answered as `TrafficTotals`. GetTrafficTotals, + /// Python 1.6.1 `Transport.link_distribution()`: relayed Links grouped by + /// destination, answered as `LinkDistribution`. + GetLinkDistribution, GetRecentAnnounces, /// Return cached identity metadata without updating `last_used`. Locally /// restored cache metadata is trusted; this query performs no fresh signature @@ -1270,6 +1273,7 @@ pub enum TransportQueryResponse { PathTable(Vec), InterfaceStats(Vec), TrafficTotals(TrafficTotalsRpcEntry), + LinkDistribution(Vec), RateTable(Vec), Announces(Vec), RecalledDestination(Option), @@ -1291,6 +1295,22 @@ pub enum TransportQueryResponse { Error(String), } +/// Relayed Links toward one destination (Python `link_distribution`), sorted +/// by destination hash. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +#[non_exhaustive] +pub struct LinkDistributionRpcEntry { + pub destination_hash: [u8; 16], + /// Validated Links. + pub active: u64, + /// Links still awaiting their proof. + pub pending: u64, + /// Bytes the Links forwarded (`IDX_LT_TRXB`). + pub traffic: u64, + /// `Interface.get_hash()` of each interface a request arrived on, sorted. + pub from_interfaces: Vec<[u8; 32]>, +} + /// Snapshot of a cached route. Times are Unix seconds; returned interface /// metadata can use fallback values if the interface is no longer registered. #[derive(Debug, Clone)] @@ -1456,6 +1476,13 @@ pub struct TrafficTotalsRpcEntry { pub ftxb: u64, pub frxs: f64, pub ftxs: f64, + /// Python `link_rxb` and `link_txb`: Link packets relayed in and out other + /// than to or from local clients, and those of this instance's own Links; + /// `link_speed_rx` and `link_speed_tx` in bits per second. + pub lrxb: u64, + pub ltxb: u64, + pub lrxs: f64, + pub ltxs: f64, pub rxpps: u64, pub txpps: u64, /// Python 1.6.1 `rx_packets`: packets that passed preprocessing.