Skip to content

Commit a6c1354

Browse files
committed
Add stable node id as well as latency measurement and add ootion to listenw without 0rtt
1 parent 255a992 commit a6c1354

File tree

3 files changed

+61
-17
lines changed

3 files changed

+61
-17
lines changed

Cargo.lock

Lines changed: 12 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

irpc-iroh/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ tracing-subscriber = { workspace = true, features = ["fmt"] }
2929
irpc-derive = { version = "0.5.0", path = "../irpc-derive" }
3030
clap = { version = "4.5.41", features = ["derive"] }
3131
futures-util.workspace = true
32+
hex = "0.4.3"
33+
rand = "0.8.5"

irpc-iroh/examples/0rtt.rs

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
use std::{env::Args, time::Instant, usize};
1+
use std::{
2+
env,
3+
str::FromStr,
4+
time::{Duration, Instant},
5+
};
26

3-
use anyhow::Result;
7+
use anyhow::{Context, Result};
48
use clap::Parser;
5-
use iroh::{protocol::Router, Endpoint, NodeAddr, Watcher};
9+
use iroh::{protocol::Router, Endpoint, NodeAddr, SecretKey, Watcher};
610
use iroh_base::ticket::NodeTicket;
711
use ping::EchoApi;
12+
use rand::SeedableRng;
813

914
#[tokio::main]
1015
async fn main() -> Result<()> {
@@ -13,7 +18,8 @@ async fn main() -> Result<()> {
1318
match args {
1419
cli::Args::Listen { use_0rtt } => {
1520
let (server_router, server_addr) = {
16-
let endpoint = Endpoint::builder().bind().await?;
21+
let secret_key = get_or_generate_secret_key()?;
22+
let endpoint = Endpoint::builder().secret_key(secret_key).bind().await?;
1723
endpoint.home_relay().initialized().await;
1824
let addr = endpoint.node_addr().initialized().await;
1925
let api = EchoApi::spawn();
@@ -41,6 +47,7 @@ async fn main() -> Result<()> {
4147
n,
4248
delay_ms,
4349
use_0rtt,
50+
wait_for_ticket,
4451
} => {
4552
let n = n
4653
.iter()
@@ -56,11 +63,26 @@ async fn main() -> Result<()> {
5663
let msg = i.to_be_bytes();
5764
let t0 = Instant::now();
5865
let res = api.echo_0rtt(msg.to_vec()).await;
59-
drop(api);
66+
let latency = endpoint.remote_info(addr.node_id).and_then(|x| x.latency);
67+
if wait_for_ticket {
68+
tokio::spawn(async move {
69+
let latency = latency.unwrap_or(Duration::from_millis(500));
70+
tokio::time::sleep(latency * 2).await;
71+
drop(api);
72+
});
73+
} else {
74+
drop(api);
75+
}
6076
match res {
6177
Ok(data) => {
6278
let elapsed = t0.elapsed();
6379
assert!(data == msg);
80+
println!(
81+
"latency: {}",
82+
latency
83+
.map(|x| format!("{}ms", x.as_micros() as f64 / 1000.0))
84+
.unwrap_or("unknown".into())
85+
);
6486
println!("{}ms", elapsed.as_micros() as f64 / 1000.0);
6587
}
6688
Err(err) => {
@@ -76,10 +98,26 @@ async fn main() -> Result<()> {
7698
Ok(())
7799
}
78100

101+
/// Gets a secret key from the IROH_SECRET environment variable or generates a new random one.
102+
/// If the environment variable is set, it must be a valid string representation of a secret key.
103+
pub fn get_or_generate_secret_key() -> Result<SecretKey> {
104+
if let Ok(secret) = env::var("IROH_SECRET") {
105+
// Parse the secret key from string
106+
SecretKey::from_str(&secret).context("Invalid secret key format")
107+
} else {
108+
// Generate a new random key
109+
let secret_key = SecretKey::generate(&mut rand::rngs::StdRng::from_entropy());
110+
println!(
111+
"Generated new secret key: {}",
112+
hex::encode(secret_key.to_bytes())
113+
);
114+
println!("To reuse this key, set the IROH_SECRET environment variable to this value");
115+
Ok(secret_key)
116+
}
117+
}
118+
79119
mod cli {
80-
use anyhow::Result;
81120
use clap::Parser;
82-
use iroh::NodeId;
83121
use iroh_base::ticket::NodeTicket;
84122

85123
#[derive(Debug, Parser)]
@@ -96,6 +134,8 @@ mod cli {
96134
use_0rtt: bool,
97135
#[clap(long, default_value = "1000")]
98136
delay_ms: u64,
137+
#[clap(long, default_value = "false")]
138+
wait_for_ticket: bool,
99139
},
100140
}
101141
}

0 commit comments

Comments
 (0)