Skip to content

Commit 926cb62

Browse files
committed
telephony: refresh route hops after path resolution
1 parent cd84f70 commit 926cb62

2 files changed

Lines changed: 78 additions & 3 deletions

File tree

crates/lxst-telephony/src/lib.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,8 +1363,13 @@ impl TelephonyService {
13631363
discovery_timeout,
13641364
)
13651365
.await?;
1366-
await_path_on_transport(transport_tx, peer.destination_hash, discovery_timeout)
1367-
.await?;
1366+
await_path_on_transport(
1367+
transport_tx.clone(),
1368+
peer.destination_hash,
1369+
discovery_timeout,
1370+
)
1371+
.await?;
1372+
let peer = refresh_peer_hops_from_transport(transport_tx, peer).await?;
13681373
Ok(peer)
13691374
}
13701375
.await;
@@ -2212,6 +2217,7 @@ impl TelephonyRnsEndpoint {
22122217
discovery_timeout,
22132218
)
22142219
.await?;
2220+
let peer = refresh_peer_hops_from_transport(self.transport_tx.clone(), peer).await?;
22152221
self.begin_outgoing_link_with_remote_pubkey(
22162222
core,
22172223
remote_identity,
@@ -2896,6 +2902,40 @@ async fn await_path_on_transport(
28962902
}
28972903
}
28982904

2905+
async fn refresh_peer_hops_from_transport(
2906+
transport_tx: mpsc::Sender<TransportMessage>,
2907+
mut peer: RemoteTelephonyPeer,
2908+
) -> Result<RemoteTelephonyPeer, Error> {
2909+
if let Some(hops) = path_hops_on_transport(transport_tx, peer.destination_hash).await? {
2910+
peer.hops = hops;
2911+
}
2912+
Ok(peer)
2913+
}
2914+
2915+
async fn path_hops_on_transport(
2916+
transport_tx: mpsc::Sender<TransportMessage>,
2917+
destination_hash: [u8; 16],
2918+
) -> Result<Option<u8>, Error> {
2919+
let (response_tx, response_rx) = tokio::sync::oneshot::channel();
2920+
send_transport_async(
2921+
transport_tx,
2922+
TransportMessage::Rpc {
2923+
query: TransportQuery::GetPathTable,
2924+
response_tx,
2925+
},
2926+
)
2927+
.await?;
2928+
2929+
match response_rx.await.map_err(|_| Error::TransportQueryClosed)? {
2930+
TransportQueryResponse::PathTable(entries) => Ok(entries
2931+
.into_iter()
2932+
.find(|entry| entry.hash == destination_hash)
2933+
.map(|entry| entry.hops)),
2934+
TransportQueryResponse::Error(_) => Err(Error::UnexpectedTransportQueryResponse),
2935+
_ => Err(Error::UnexpectedTransportQueryResponse),
2936+
}
2937+
}
2938+
28992939
async fn send_transport_async(
29002940
transport_tx: mpsc::Sender<TransportMessage>,
29012941
message: TransportMessage,

crates/lxst-telephony/src/tests.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rns_crypto::ed25519::Ed25519PrivateKey;
66
use rns_identity::announce::AnnounceData;
77
use rns_link::link::LinkState;
88
use rns_transport::messages::{
9-
AnnounceRpcEntry, TransportMessage, TransportQuery, TransportQueryResponse,
9+
AnnounceRpcEntry, PathTableRpcEntry, TransportMessage, TransportQuery, TransportQueryResponse,
1010
};
1111

1212
fn link(byte: u8) -> LinkId {
@@ -52,6 +52,17 @@ fn announce_entry(
5252
}
5353
}
5454

55+
fn path_entry(destination_hash: [u8; 16], hops: u8) -> PathTableRpcEntry {
56+
PathTableRpcEntry {
57+
hash: destination_hash,
58+
timestamp: 1235.0,
59+
via: None,
60+
hops,
61+
expires: 3600.0,
62+
interface: "test".to_string(),
63+
}
64+
}
65+
5566
fn packet(signals: impl IntoIterator<Item = Signal>) -> Vec<u8> {
5667
LxstPacket::signalling(signals).encode().unwrap()
5768
}
@@ -1284,6 +1295,18 @@ async fn begin_outgoing_link_discovers_announce_and_sends_link_request() {
12841295
assert_eq!(dest, destination_hash);
12851296
reply.send(true).unwrap();
12861297

1298+
let rpc = transport_rx.recv().await.unwrap();
1299+
let TransportMessage::Rpc { query, response_tx } = rpc else {
1300+
panic!("expected path table Rpc, got {rpc:?}");
1301+
};
1302+
assert!(matches!(query, TransportQuery::GetPathTable));
1303+
response_tx
1304+
.send(TransportQueryResponse::PathTable(vec![path_entry(
1305+
destination_hash,
1306+
2,
1307+
)]))
1308+
.unwrap();
1309+
12871310
let request_path = transport_rx.recv().await.unwrap();
12881311
let TransportMessage::RequestPath {
12891312
destination_hash: requested_hash,
@@ -1766,6 +1789,18 @@ async fn telephony_service_call_control_discovers_peer_and_emits_started() {
17661789
assert_eq!(dest, destination_hash);
17671790
reply.send(true).unwrap();
17681791

1792+
let rpc = transport_rx.recv().await.unwrap();
1793+
let TransportMessage::Rpc { query, response_tx } = rpc else {
1794+
panic!("expected path table Rpc, got {rpc:?}");
1795+
};
1796+
assert!(matches!(query, TransportQuery::GetPathTable));
1797+
response_tx
1798+
.send(TransportQueryResponse::PathTable(vec![path_entry(
1799+
destination_hash,
1800+
1,
1801+
)]))
1802+
.unwrap();
1803+
17691804
let request_path = transport_rx.recv().await.unwrap();
17701805
let TransportMessage::RequestPath {
17711806
destination_hash: requested_hash,

0 commit comments

Comments
 (0)