rpc: path table entries carry interface_hash and state

This commit is contained in:
DeFiDude committed 2026-11-18 13:05:37 -07:00
1 parent 9b9226218d
commit 80b64223dc
9 files changed
+183 -38

No files matched your search

@@ -197,6 +197,8 @@ fn handle_path(tx: &mpsc::Sender<TransportMessage>, data: &[u8]) -> Option<Vec<u
used: e.used,
lr_cost: e.lr_cost,
interface: e.interface,
interface_hash: Some(e.interface_hash.to_vec()),
state: Some(e.state as u8),
});
}
}
@@ -455,6 +457,8 @@ mod tests {
entry.used = 0.0;
entry.lr_cost = None;
entry.interface = "MockIf".to_string();
entry.interface_hash = rns_crypto::sha::sha256(b"MockIf");
entry.state = rns_transport::constants::PathState::Responsive;
entry.interface_id = 1;
entry.interface_mode =
rns_transport::constants::InterfaceMode::Full;
@@ -754,6 +758,29 @@ mod tests {
.1
.as_u64();
assert_eq!(hops, Some(2));
// Transport.py 1.6.1: remote `/path` uses get_path_table's keys.
let keys: Vec<&str> = entry.iter().filter_map(|(k, _)| k.as_str()).collect();
assert_eq!(
keys,
[
"hash",
"timestamp",
"via",
"hops",
"expires",
"value",
"used",
"lr_cost",
"interface",
"interface_hash",
"state"
]
);
assert_eq!(
entry[9].1.as_slice(),
Some(&rns_crypto::sha::sha256(b"MockIf")[..])
);
assert_eq!(entry[10].1.as_u64(), Some(2));
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+63 -2
View File
@@ -205,6 +205,13 @@ pub struct PathTableEntry {
#[serde(default)]
pub lr_cost: Option<f64>,
pub interface: String,
/// SHA-256 of upstream's `str(interface)`; `None` from a peer before 1.6.1.
#[serde(default)]
pub interface_hash: Option<Vec<u8>>,
/// Path state: 0 unknown, 1 unresponsive, 2 responsive (Transport.py);
/// `None` from a peer before 1.6.1.
#[serde(default)]
pub state: Option<u8>,
}
/// One interface of Python's `get_interface_stats()` (Reticulum.py), field for
@@ -1025,6 +1032,19 @@ fn response_to_py_value(resp: &RpcResponse) -> PyValue {
e.lr_cost.map(PyValue::Float).unwrap_or(PyValue::None),
),
("interface", PyValue::String(e.interface.clone())),
(
"interface_hash",
e.interface_hash
.clone()
.map(PyValue::Bytes)
.unwrap_or(PyValue::None),
),
(
"state",
e.state
.map(|state| PyValue::Int(i128::from(state)))
.unwrap_or(PyValue::None),
),
])
})
.collect(),
@@ -1583,6 +1603,11 @@ fn parse_path_table(value: &PyValue) -> Result<Vec<PathTableEntry>, RpcError> {
interface: dict_get(m, "interface")
.and_then(py_string)
.unwrap_or_default(),
interface_hash: match dict_get(m, "interface_hash") {
Some(PyValue::Bytes(v)) => Some(v.clone()),
_ => None,
},
state: dict_get(m, "state").and_then(py_u8),
})
})
.collect()
@@ -2844,6 +2869,8 @@ mod tests {
used: 0.0,
lr_cost: None,
interface: "TCPInterface[test]".to_string(),
interface_hash: None,
state: None,
};
let resp = RpcResponse::PathTable(vec![entry]);
let encoded = encode_response(&resp).unwrap();
@@ -3527,8 +3554,14 @@ mod tests {
const FX_REQ_IDENTITY_DATA_RETAIN: &str = "82ad6964656e746974795f64617461a672657461696ead6964656e746974795f68617368c410dddddddddddddddddddddddddddddddd";
// Path-table replies: RNS 1.6.0 umsgpack over the dict get_path_table builds
// (Reticulum.py), unknown and stamped destinations.
const FX_RESP_PATH_TABLE: &str = "9189a468617368c410aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa974696d657374616d70cb41d26580b4800000a3766961c410bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbba4686f707303a765787069726573cb41d267cf54800000a576616c7565c0a47573656400a76c725f636f7374c0a9696e74657266616365b2544350496e746572666163655b746573745d";
const FX_RESP_PATH_TABLE_STAMPED: &str = "9189a468617368c410aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa974696d657374616d70cb41d26580b4800000a3766961c410bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbba4686f707303a765787069726573cb41d267cf54800000a576616c7565cb4018500000000000a475736564cb41d26580cfe00000a76c725f636f7374cb400a000000000000a9696e74657266616365b2544350496e746572666163655b746573745d";
const FX_RESP_PATH_TABLE_160: &str = "9189a468617368c410aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa974696d657374616d70cb41d26580b4800000a3766961c410bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbba4686f707303a765787069726573cb41d267cf54800000a576616c7565c0a47573656400a76c725f636f7374c0a9696e74657266616365b2544350496e746572666163655b746573745d";
const FX_RESP_PATH_TABLE_STAMPED_160: &str = "9189a468617368c410aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa974696d657374616d70cb41d26580b4800000a3766961c410bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbba4686f707303a765787069726573cb41d267cf54800000a576616c7565cb4018500000000000a475736564cb41d26580cfe00000a76c725f636f7374cb400a000000000000a9696e74657266616365b2544350496e746572666163655b746573745d";
// The same from RNS 1.6.1, which adds `interface_hash` and `state`:
// `Reticulum.get_path_table` run on a Transport.path_table entry whose
// interface prints as "TCPInterface[test]", with path state 0 and 2,
// packed with RNS.vendor.umsgpack.packb.
const FX_RESP_PATH_TABLE: &str = "918ba468617368c410aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa974696d657374616d70cb41d26580b4800000a3766961c410bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbba4686f707303a765787069726573cb41d267cf54800000a576616c7565c0a47573656400a76c725f636f7374c0a9696e74657266616365b2544350496e746572666163655b746573745dae696e746572666163655f68617368c4206e5082481873def8612282d21b981c90430ecd80bf5a877711f83dabb06ae938a5737461746500";
const FX_RESP_PATH_TABLE_STAMPED: &str = "918ba468617368c410aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa974696d657374616d70cb41d26580b4800000a3766961c410bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbba4686f707303a765787069726573cb41d267cf54800000a576616c7565cb4018500000000000a475736564cb41d26580cfe00000a76c725f636f7374cb400a000000000000a9696e74657266616365b2544350496e746572666163655b746573745dae696e746572666163655f68617368c4206e5082481873def8612282d21b981c90430ecd80bf5a877711f83dabb06ae938a5737461746502";
const FX_RESP_BLACKHOLE_DICT: &str = "81c4104242424242424242424242424242424283a6736f75726365c410aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa5756e74696ccb4093480000000000a6726561736f6ea6706172697479";
const FX_INT_EDGES: &str = "dc0014007fcc80ccffcd0100cdffffce00010000ceffffffffcf0000000100000000cfffffffffffffffffffe0d0dfd080d1ff7fd18000d2ffff7fffd280000000d3ffffffff7fffffffd38000000000000000";
const FX_STR_EDGES: &str = "93a0bf61616161616161616161616161616161616161616161616161616161616161d9206161616161616161616161616161616161616161616161616161616161616161";
@@ -3627,6 +3660,8 @@ mod tests {
used: 0.0,
lr_cost: None,
interface: "TCPInterface[test]".to_string(),
interface_hash: Some(rns_crypto::sha::sha256(b"TCPInterface[test]").to_vec()),
state: Some(0),
}]);
assert_eq!(
hex::encode(encode_response(&resp).unwrap()),
@@ -3644,9 +3679,28 @@ mod tests {
assert_eq!(entries[0].interface, "TCPInterface[test]");
assert_eq!(entries[0].value, None);
assert_eq!(entries[0].lr_cost, None);
assert_eq!(entries[0].state, Some(0));
}
other => panic!("wrong variant: {other:?}"),
}
// A 1.6.0 instance sends neither key.
for fixture in [FX_RESP_PATH_TABLE_160, FX_RESP_PATH_TABLE_STAMPED_160] {
match decode_response_for_request(
&hex::decode(fixture).unwrap(),
&RpcRequest::GetPathTable { max_hops: None },
)
.unwrap()
{
RpcResponse::PathTable(entries) => {
assert_eq!(entries[0].interface, "TCPInterface[test]");
assert_eq!(
(&entries[0].interface_hash, entries[0].state),
(&None, None)
);
}
other => panic!("wrong variant: {other:?}"),
}
}
}
#[test]
@@ -3661,6 +3715,8 @@ mod tests {
used: 1234567999.5,
lr_cost: Some(3.25),
interface: "TCPInterface[test]".to_string(),
interface_hash: Some(rns_crypto::sha::sha256(b"TCPInterface[test]").to_vec()),
state: Some(2),
};
let resp = RpcResponse::PathTable(vec![entry]);
assert_eq!(
@@ -3677,6 +3733,11 @@ mod tests {
assert_eq!(entries[0].value, Some(6.078125));
assert_eq!(entries[0].used, 1234567999.5);
assert_eq!(entries[0].lr_cost, Some(3.25));
assert_eq!(entries[0].state, Some(2));
assert_eq!(
entries[0].interface_hash.as_deref(),
Some(&rns_crypto::sha::sha256(b"TCPInterface[test]")[..])
);
}
other => panic!("wrong variant: {other:?}"),
}
+2
View File
@@ -616,6 +616,8 @@ async fn process_rpc_request(
used: e.used,
lr_cost: e.lr_cost,
interface: e.interface,
interface_hash: Some(e.interface_hash.to_vec()),
state: Some(e.state as u8),
})
.collect();
RpcResponse::PathTable(rpc_entries)
+2
View File
@@ -1525,6 +1525,8 @@ mod tests {
used,
lr_cost: None,
interface: String::new(),
interface_hash: None,
state: None,
};
let mut entries = vec![entry(1, 0.0), entry(2, 20.0), entry(3, 0.0), entry(4, 30.0)];
sort_by_usage(&mut entries);
+1 -1
View File
@@ -2624,7 +2624,6 @@ mod tests {
mod discovery_api;
mod discovery_public_api;
mod held_announces;
mod hop_limiter;
mod inbound_queues;
mod interface_stats;
mod link_endpoint_dispatch;
@@ -2633,6 +2632,7 @@ mod tests {
mod packet_capacity;
mod path_request_tags;
mod path_table_lifecycle;
mod paths_and_filters;
mod restored_state;
mod slow_media_discovery;
mod transport_api_defaults;
+7 -3
View File
@@ -13,11 +13,13 @@ impl TransportActor {
.path_table
.iter_live()
.map(|(hash, entry)| {
let iface_name = self
.interfaces
.get(&entry.interface_id)
let iface = self.interfaces.get(&entry.interface_id);
let iface_name = iface
.map(|e| e.name.clone())
.unwrap_or_else(|| format!("interface_{}", entry.interface_id));
let interface_hash = crate::persistence::interface_hash_from_name(
iface.map_or(iface_name.as_str(), |e| e.upstream_name()),
);
let hash = hash.into_bytes();
let known = self.recent_announces.get(&hash);
PathTableRpcEntry {
@@ -30,6 +32,8 @@ impl TransportActor {
used: entry.used,
lr_cost: known.and_then(|k| k.lr_cost),
interface: iface_name,
interface_hash,
state: self.path_table.get_state(&hash),
interface_id: entry.interface_id,
interface_mode: self
.interfaces
@@ -1,32 +0,0 @@
//! Per-interface hop limits on inbound packets (Transport.py 1.6.1, bb63dd44).
use super::*;
/// The interface's limit applies before the instance-wide one, on wire hops;
/// 0 is off, as `None` is.
#[test]
fn interface_hop_limiter_drops_at_its_limit() {
let (mut actor, _tx) = TransportActor::new();
let mut keep = Vec::new();
for (id, limit) in [(1, Some(1)), (2, Some(0)), (3, None)] {
let (mut entry, rx) = make_test_interface(&format!("iface{id}"));
entry.hop_limiter = limit;
actor.interfaces.insert(id, entry);
keep.push(rx);
}
let mut learned = Vec::new();
for id in 1..=3 {
for hops in [0, 1] {
let (raw, dest) = make_valid_announce(&format!("hoplimit.i{id}.h{hops}"), hops);
actor.on_inbound(InboundPacket {
raw,
interface_id: id,
rssi: None,
snr: None,
q: None,
});
learned.push(actor.path_table.has_path(&dest));
}
}
assert_eq!(learned, [true, false, true, true, true, true]);
}
@@ -0,0 +1,74 @@
//! Per-interface hop limits and the path-table getter (Transport.py 1.6.1).
use super::*;
/// The interface's limit applies before the instance-wide one, on wire hops;
/// 0 is off, as `None` is.
#[test]
fn interface_hop_limiter_drops_at_its_limit() {
let (mut actor, _tx) = TransportActor::new();
let mut keep = Vec::new();
for (id, limit) in [(1, Some(1)), (2, Some(0)), (3, None)] {
let (mut entry, rx) = make_test_interface(&format!("iface{id}"));
entry.hop_limiter = limit;
actor.interfaces.insert(id, entry);
keep.push(rx);
}
let mut learned = Vec::new();
for id in 1..=3 {
for hops in [0, 1] {
let (raw, dest) = make_valid_announce(&format!("hoplimit.i{id}.h{hops}"), hops);
actor.on_inbound(InboundPacket {
raw,
interface_id: id,
rssi: None,
snr: None,
q: None,
});
learned.push(actor.path_table.has_path(&dest));
}
}
assert_eq!(learned, [true, false, true, true, true, true]);
}
/// Reticulum.py 1.6.1 (c90a5d58, 41260ea1): each path names its interface's
/// hash, as interface stats do, and its state.
#[test]
fn path_table_entries_carry_interface_hash_and_state() {
let (mut actor, _tx) = TransportActor::new();
let (mut named, _named_rx) = make_test_interface("radio");
named.inspection = Some(
crate::messages::InterfaceInspectionSource::new(Default::default).with_identity(
crate::messages::InterfaceIdentity::new(
"TCPInterface[radio/127.0.0.1:4242]",
"radio",
"TCPInterface",
),
),
);
actor.interfaces.insert(1, named);
let (plain, _plain_rx) = make_test_interface("plain");
actor.interfaces.insert(2, plain);
let (a, b) = ([0x61; 16], [0x62; 16]);
actor
.path_table
.insert(a, PathEntry::new(None, 1, 1, InterfaceMode::Full));
actor
.path_table
.insert(b, PathEntry::new(None, 1, 2, InterfaceMode::Full));
actor.mark_path_responsive(&a);
let TransportQueryResponse::PathTable(entries) =
actor.handle_query(TransportQuery::GetPathTable)
else {
panic!("expected a path table");
};
let entry = |hash| entries.iter().find(|e| e.hash == hash).unwrap();
assert_eq!(
entry(a).interface_hash,
rns_crypto::sha::sha256(b"TCPInterface[radio/127.0.0.1:4242]")
);
assert_eq!(entry(a).state, PathState::Responsive);
assert_eq!(entry(b).interface_hash, rns_crypto::sha::sha256(b"plain"));
assert_eq!(entry(b).state, PathState::Unknown);
}
+7
View File
@@ -1382,6 +1382,11 @@ pub struct PathTableRpcEntry {
/// Announced link-request cost, if any.
pub lr_cost: Option<f64>,
pub interface: String,
/// Upstream's `Interface.get_hash()`, SHA-256 of `str(interface)`: the
/// `hash` interface stats report for the same interface.
pub interface_hash: [u8; 32],
/// Python `Transport.get_path_state`.
pub state: crate::constants::PathState,
pub interface_id: InterfaceId,
pub interface_mode: InterfaceMode,
pub interface_role: InterfaceRole,
@@ -1882,6 +1887,8 @@ impl Default for PathTableRpcEntry {
used: 0.0,
lr_cost: None,
interface: String::new(),
interface_hash: [0; 32],
state: crate::constants::PathState::Unknown,
interface_id: 0,
interface_mode: InterfaceMode::Full,
interface_role: InterfaceRole::Normal,