From 3cd4ecadaa24b9cdbdb45dff2b750237cef16b7d Mon Sep 17 00:00:00 2001 From: DeFiDude <59237470+DeFiDude@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:41:47 -0700 Subject: [PATCH] transport: AutoInterface parents count their peers, and known destinations list and load for the RPC --- crates/rns-transport/src/actor/rpc.rs | 43 ++++++++++++++++--- .../rns-transport/src/actor/store_worker.rs | 43 ++++++++++++------- .../src/actor/tests/interface_stats.rs | 25 +++++++++++ crates/rns-transport/src/messages.rs | 19 +++++++- crates/rns-transport/src/tunnel.rs | 17 ++++++++ 5 files changed, 125 insertions(+), 22 deletions(-) diff --git a/crates/rns-transport/src/actor/rpc.rs b/crates/rns-transport/src/actor/rpc.rs index 4511d64..d1ddeb2 100644 --- a/crates/rns-transport/src/actor/rpc.rs +++ b/crates/rns-transport/src/actor/rpc.rs @@ -694,6 +694,22 @@ impl TransportActor { self.cleanup_known_destinations(crate::actor::now_f64()); TransportQueryResponse::IntResult(self.recent_announces.len() as i64) } + TransportQuery::GetKnownDestinations => { + let mut rows: Vec<_> = self + .recent_announces + .values() + .filter_map(super::store_worker::known_destination_row) + .collect(); + rows.sort_by_key(|row| row.destination_hash); + TransportQueryResponse::KnownDestinations(rows) + } + TransportQuery::KnownDestinationsLoaded => { + TransportQueryResponse::BoolResult(self.kd.loaded) + } + TransportQuery::LoadKnownDestinations { rows } => { + self.merge_known_destinations(rows); + TransportQueryResponse::Ok + } TransportQuery::ResolveIdentityHash { input } => { // First treat input as a destination hash. if let Some(entry) = self.recent_announces.get(&input) @@ -889,13 +905,30 @@ impl TransportActor { else { return; }; - match event { - InterfaceEvent::ReceivedAnnounce => parent.ingress.spawned_received_announce(size), - InterfaceEvent::SentAnnounce => parent.ingress.spawned_sent_announce(size), - InterfaceEvent::ReceivedPathRequest => { + // AutoInterface keeps the base Interface counters, so its peers' events + // count on it too (AutoInterface.py, Interface.py). + let counts_children = parent + .upstream_identity() + .is_some_and(|identity| identity.class_name == "AutoInterface"); + match (event, counts_children) { + (InterfaceEvent::ReceivedAnnounce, true) => { + parent.ingress.received_announce_bytes(size) + } + (InterfaceEvent::SentAnnounce, true) => parent.ingress.sent_announce_bytes(size), + (InterfaceEvent::ReceivedPathRequest, true) => { + parent.ingress.received_path_request_bytes(size) + } + (InterfaceEvent::SentPathRequest, true) => parent.ingress.sent_path_request_bytes(size), + (InterfaceEvent::ReceivedAnnounce, false) => { + parent.ingress.spawned_received_announce(size) + } + (InterfaceEvent::SentAnnounce, false) => parent.ingress.spawned_sent_announce(size), + (InterfaceEvent::ReceivedPathRequest, false) => { parent.ingress.spawned_received_path_request(size) } - InterfaceEvent::SentPathRequest => parent.ingress.spawned_sent_path_request(size), + (InterfaceEvent::SentPathRequest, false) => { + parent.ingress.spawned_sent_path_request(size) + } } } diff --git a/crates/rns-transport/src/actor/store_worker.rs b/crates/rns-transport/src/actor/store_worker.rs index 478283d..6c3565d 100644 --- a/crates/rns-transport/src/actor/store_worker.rs +++ b/crates/rns-transport/src/actor/store_worker.rs @@ -68,6 +68,8 @@ struct Started { rows: Vec, /// The announce cache is in its database; false keeps it in the actor's memory. announces_stored: bool, + /// A client read the owner's known destinations database. + loaded: bool, } /// Outcome of one job, delivered to the actor. @@ -225,8 +227,10 @@ fn run( let started = if owner { start_owner(&mut slot, &mut announces, &source, now) } else { + let rows = load_for_client(&source); Started { - rows: load_for_client(&source), + loaded: rows.is_some(), + rows: rows.unwrap_or_default(), announces_stored: false, } }; @@ -261,30 +265,32 @@ fn start_owner( Started { rows, announces_stored, + loaded: true, } } /// A shared-instance client reads the owner's database once, read-only, and -/// nothing else (`Identity.load_known_destinations`, shared-instance branch). -fn load_for_client(source: &StoreSource) -> Vec { +/// nothing else (`Identity.load_known_destinations`, shared-instance branch); +/// `None` when there is no database to read. +fn load_for_client(source: &StoreSource) -> Option> { match source { - StoreSource::Memory => Vec::new(), + StoreSource::Memory => None, #[cfg(feature = "sqlite-store")] StoreSource::Sqlite { known, .. } => { - // Upstream logs an error for a missing file too; a client whose - // directory is not the owner's has none, and that is not a fault. + // A client whose directory is not the owner's has none; the + // runtime then asks the shared instance (split mode). if !known.is_file() { debug!("no known destinations database to load as a shared-instance client"); - return Vec::new(); + return None; } - let read = SqliteKnownDestinations::open_ro(known) - .and_then(|mut reader| read_rows(&mut reader)); - read.unwrap_or_else(|e| { - error!( - "could not load known destinations from the shared instance's database: {e}; continuing with an empty table" - ); - Vec::new() - }) + SqliteKnownDestinations::open_ro(known) + .and_then(|mut reader| read_rows(&mut reader)) + .map_err(|e| { + debug!( + "could not load known destinations from the shared instance's database: {e}" + ) + }) + .ok() } } } @@ -709,6 +715,8 @@ pub(super) struct KdState { pub(super) results: tokio_mpsc::UnboundedReceiver, /// Cached announces not yet known to be stored. announces: AnnouncePending, + /// Start-up read a known destinations database. + pub(super) loaded: bool, #[cfg(feature = "sqlite-store")] db_path: Option, #[cfg(feature = "sqlite-store")] @@ -733,6 +741,7 @@ impl KdState { in_memory: true, ..AnnouncePending::default() }, + loaded: false, #[cfg(feature = "sqlite-store")] db_path: None, #[cfg(feature = "sqlite-store")] @@ -801,11 +810,13 @@ impl KdState { if owner && !started.announces_stored { self.announces.in_memory = true; } + self.loaded = started.loaded; started.rows } None => { error!("the store worker is not running; continuing without stored state"); self.announces.in_memory = true; + self.loaded = false; Vec::new() } } @@ -1237,7 +1248,7 @@ fn open_announce_store(source: &StoreSource) -> Option Option { +pub(super) fn known_destination_row(entry: &RecentAnnounce) -> Option { let public_key = entry.public_key?; let mut row = KnownDestinationRow::new(entry.dest_hash, entry.timestamp, public_key); row.packet_hash = entry.packet_hash; diff --git a/crates/rns-transport/src/actor/tests/interface_stats.rs b/crates/rns-transport/src/actor/tests/interface_stats.rs index 2f3568e..3c15131 100644 --- a/crates/rns-transport/src/actor/tests/interface_stats.rs +++ b/crates/rns-transport/src/actor/tests/interface_stats.rs @@ -474,6 +474,31 @@ fn a_parent_takes_its_childrens_announce_bytes_but_not_their_counts() { assert_eq!(stats(&mut actor, 2).announces_received, 1); } +/// AutoInterface keeps the base counters, so its peers' announces count on it +/// as well (AutoInterface.py, Interface.py). +#[test] +fn an_auto_interface_counts_its_peers_announces() { + let mut actor = hub(); + let (mut parent, _parent_rx) = make_test_interface("auto"); + parent.inspection = Some( + crate::messages::InterfaceInspectionSource::new(Default::default).with_identity( + crate::messages::InterfaceIdentity::bracketed("AutoInterface", "auto"), + ), + ); + actor.interfaces.insert(1, parent); + let (mut peer, _peer_rx) = make_test_interface("auto/peer"); + peer.parent_id = Some(1); + actor.interfaces.insert(2, peer); + let (announce, _) = make_valid_announce("stats.auto.peer", 0); + let size = announce.len() as u64; + inbound(&mut actor, announce, 2); + let parent = stats(&mut actor, 1); + assert_eq!( + (parent.announces_received, parent.announce_rx_bytes), + (1, size) + ); +} + /// Totals sum top-level interfaces other than the shared-instance server and /// keep what they counted when an interface leaves (Transport.py). #[test] diff --git a/crates/rns-transport/src/messages.rs b/crates/rns-transport/src/messages.rs index 9611316..7a6031b 100644 --- a/crates/rns-transport/src/messages.rs +++ b/crates/rns-transport/src/messages.rs @@ -467,10 +467,15 @@ impl InterfaceEntry { /// registered name. Persisted tables name an interface by its hash /// (`Interface.get_hash`). pub(crate) fn upstream_name(&self) -> &str { + self.upstream_identity() + .map_or(self.name.as_str(), |identity| identity.name.as_str()) + } + + /// How upstream names the interface, where the driver supplies it. + pub(crate) fn upstream_identity(&self) -> Option<&InterfaceIdentity> { self.inspection .as_ref() .and_then(InterfaceInspectionSource::identity) - .map_or(self.name.as_str(), |identity| identity.name.as_str()) } /// Minimal entry with defaults for everything optional. Use the @@ -1277,6 +1282,17 @@ pub enum TransportQuery { /// Run announce-metadata cleanup now and return the remaining count. Periodic /// cleanup is additionally subject to its interval and startup-grace gates. CleanKnownDestinations, + /// Python `Reticulum.get_known_destinations`: every known destination with + /// an identity key, as `KnownDestinations` rows. + GetKnownDestinations, + /// Whether this shared-instance client read the owner's known destinations + /// database at start-up (`Identity.load_known_destinations`); `BoolResult`. + KnownDestinationsLoaded, + /// Python 1.6.1's split-mode fallback: known destinations a shared-instance + /// client fetched over RPC, merged as rows read from the database are. + LoadKnownDestinations { + rows: Vec, + }, /// Resolve a raw 16-byte value as a destination first, then as an identity hash, /// using cached public keys. Returns `HashResult(None)` on a miss. This does /// not freshly authenticate the cache or contact a peer. @@ -1325,6 +1341,7 @@ pub enum TransportQueryResponse { InterfaceStats(Vec), TrafficTotals(TrafficTotalsRpcEntry), LinkDistribution(Vec), + KnownDestinations(Vec), RateTable(Vec), Announces(Vec), RecalledDestination(Option), diff --git a/crates/rns-transport/src/tunnel.rs b/crates/rns-transport/src/tunnel.rs index 5f14d0f..a7f05e3 100644 --- a/crates/rns-transport/src/tunnel.rs +++ b/crates/rns-transport/src/tunnel.rs @@ -335,6 +335,23 @@ pub enum TunnelExtError { } impl RemoteTransportInfo { + /// The fields as a shared instance's interface stats carry them. + pub fn new( + implementation: String, + version: Vec, + transport: bool, + destination_cost: f64, + pr_cost: f64, + ) -> Self { + Self { + implementation, + version, + transport, + destination_cost, + pr_cost, + } + } + /// Python 1.6.1's extension checks (Transport.py:3255-3288), except that /// integer costs are accepted: Python sends one for a clamped cost. pub fn decode(ext: &[u8]) -> Result {