diff --git a/iroh-cli/src/commands/start.rs b/iroh-cli/src/commands/start.rs index 8310f5a21e0..ec22c26de87 100644 --- a/iroh-cli/src/commands/start.rs +++ b/iroh-cli/src/commands/start.rs @@ -77,7 +77,7 @@ where let node = start_node(iroh_data_root, relay_map).await?; drop(spinner); - eprintln!("{}", welcome_message(&node).await?); + eprintln!("{}", welcome_message(&node)?); let client = node.client().clone(); @@ -141,11 +141,11 @@ pub(crate) async fn start_node( .await } -async fn welcome_message(node: &Node) -> Result { +fn welcome_message(node: &Node) -> Result { let msg = format!( "{}\nNode ID: {}\n", "Iroh is running".green(), - node.node_id().await?, + node.node_id() ); Ok(msg) diff --git a/iroh/examples/collection-fetch.rs b/iroh/examples/collection-fetch.rs index 7df862503f0..e35f61ba95a 100644 --- a/iroh/examples/collection-fetch.rs +++ b/iroh/examples/collection-fetch.rs @@ -37,7 +37,7 @@ async fn main() -> Result<()> { let node = iroh::node::Node::memory().spawn().await?; println!("fetching hash: {}", ticket.hash()); - println!("node id: {}", node.node_id().await?); + println!("node id: {}", node.node_id()); println!("node listening addresses:"); let addrs = node.my_addr().await?; for addr in addrs.direct_addresses() { @@ -46,7 +46,6 @@ async fn main() -> Result<()> { println!( "node relay server url: {:?}", node.my_relay() - .await? .expect("a default relay url should be provided") .to_string() ); diff --git a/iroh/examples/hello-world-fetch.rs b/iroh/examples/hello-world-fetch.rs index 9e9f721ba70..71672845a81 100644 --- a/iroh/examples/hello-world-fetch.rs +++ b/iroh/examples/hello-world-fetch.rs @@ -37,7 +37,7 @@ async fn main() -> Result<()> { let node = iroh::node::Node::memory().spawn().await?; println!("fetching hash: {}", ticket.hash()); - println!("node id: {}", node.node_id().await?); + println!("node id: {}", node.node_id()); println!("node listening addresses:"); let addrs = node.my_addr().await?; for addr in addrs.direct_addresses() { @@ -46,7 +46,6 @@ async fn main() -> Result<()> { println!( "node relay server url: {:?}", node.my_relay() - .await? .expect("a default relay url should be provided") .to_string() ); diff --git a/iroh/examples/rpc.rs b/iroh/examples/rpc.rs index ce10e0b15fa..f3abdafc528 100644 --- a/iroh/examples/rpc.rs +++ b/iroh/examples/rpc.rs @@ -31,7 +31,7 @@ where .await?; // print some info about the node - let peer = node.node_id().await?; + let peer = node.node_id(); let addrs = node.local_endpoint_addresses().await?; println!("node PeerID: {peer}"); println!("node listening addresses:"); diff --git a/iroh/src/node.rs b/iroh/src/node.rs index f98a4265fc1..e6928556c5e 100644 --- a/iroh/src/node.rs +++ b/iroh/src/node.rs @@ -12,6 +12,7 @@ use std::sync::Arc; use anyhow::{anyhow, Result}; use futures_lite::{future::Boxed as BoxFuture, FutureExt, StreamExt}; +use iroh_base::key::PublicKey; use iroh_blobs::downloader::Downloader; use iroh_blobs::store::Store as BaoStore; use iroh_net::util::AbortingJoinHandle; @@ -171,6 +172,11 @@ impl Node { self.inner.local_endpoint_addresses().await } + /// Returns the [`PublicKey`] of the node. + pub fn node_id(&self) -> PublicKey { + self.inner.secret_key.public() + } + /// Subscribe to [`Event`]s emitted from the node, informing about connections and /// progress. /// @@ -198,6 +204,11 @@ impl Node { &self.inner.rt } + /// Get the relay server we are connected to. + pub fn my_relay(&self) -> Option { + self.inner.endpoint.my_relay() + } + /// Aborts the node. /// /// This does not gracefully terminate currently: all connections are closed and @@ -398,7 +409,7 @@ mod tests { let AddOutcome { hash, .. } = node1.blobs.add_bytes(b"foo".to_vec()).await?; // create a node addr with only a relay URL, no direct addresses - let addr = NodeAddr::new(node1.node_id().await?).with_relay_url(relay_url); + let addr = NodeAddr::new(node1.node_id()).with_relay_url(relay_url); node2.blobs.download(hash, addr).await?.await?; assert_eq!( node2 @@ -441,7 +452,7 @@ mod tests { let hash = node1.blobs.add_bytes(b"foo".to_vec()).await?.hash; // create a node addr with node id only - let addr = NodeAddr::new(node1.node_id().await?); + let addr = NodeAddr::new(node1.node_id()); node2.blobs.download(hash, addr).await?.await?; assert_eq!( node2 diff --git a/iroh/tests/provide.rs b/iroh/tests/provide.rs index 919f6bf5c98..2c382732014 100644 --- a/iroh/tests/provide.rs +++ b/iroh/tests/provide.rs @@ -153,7 +153,7 @@ async fn multiple_clients() -> Result<()> { let file_hash: Hash = expect_hash; let name = expect_name; let addrs = node.local_address(); - let peer_id = node.node_id().await?; + let peer_id = node.node_id(); let content = content.to_vec(); tasks.push(node.local_pool_handle().spawn_pinned(move || { @@ -237,7 +237,7 @@ where .await?; let addrs = node.local_endpoint_addresses().await?; - let (secret_key, peer) = get_options(node.node_id().await?, addrs); + let (secret_key, peer) = get_options(node.node_id(), addrs); let request = GetRequest::all(collection_hash); let (collection, children, _stats) = run_collection_get_request(secret_key, peer, request).await?; @@ -321,7 +321,7 @@ async fn test_server_close() { let hash = db.insert_many(collection.to_blobs()).unwrap(); let node = test_node(db).spawn().await.unwrap(); let node_addr = node.local_endpoint_addresses().await.unwrap(); - let peer_id = node.node_id().await.unwrap(); + let peer_id = node.node_id(); let (events_sender, mut events_recv) = mpsc::unbounded_channel(); node.subscribe(move |event| { @@ -393,7 +393,7 @@ async fn test_ipv6() { } }; let addrs = node.local_endpoint_addresses().await.unwrap(); - let peer_id = node.node_id().await.unwrap(); + let peer_id = node.node_id(); tokio::time::timeout(Duration::from_secs(10), async move { let (secret_key, peer) = get_options(peer_id, addrs); let request = GetRequest::all(hash); @@ -421,7 +421,7 @@ async fn test_not_found() { } }; let addrs = node.local_endpoint_addresses().await.unwrap(); - let peer_id = node.node_id().await.unwrap(); + let peer_id = node.node_id(); tokio::time::timeout(Duration::from_secs(10), async move { let (secret_key, peer) = get_options(peer_id, addrs); let request = GetRequest::single(hash); @@ -464,7 +464,7 @@ async fn test_chunk_not_found_1() { } }; let addrs = node.local_endpoint_addresses().await.unwrap(); - let peer_id = node.node_id().await.unwrap(); + let peer_id = node.node_id(); tokio::time::timeout(Duration::from_secs(10), async move { let (secret_key, peer) = get_options(peer_id, addrs); let request = GetRequest::single(hash); @@ -544,7 +544,7 @@ async fn test_run_fsm() { let (db, hash) = create_test_db([("a", b"hello"), ("b", b"world")]); let node = test_node(db).spawn().await.unwrap(); let addrs = node.local_endpoint_addresses().await.unwrap(); - let peer_id = node.node_id().await.unwrap(); + let peer_id = node.node_id(); tokio::time::timeout(Duration::from_secs(10), async move { let (secret_key, peer) = get_options(peer_id, addrs); let request = GetRequest::all(hash); @@ -593,7 +593,7 @@ async fn test_size_request_blob() { let hash = Hash::from(*hashes.values().next().unwrap()); let node = test_node(db).spawn().await.unwrap(); let addrs = node.local_endpoint_addresses().await.unwrap(); - let peer_id = node.node_id().await.unwrap(); + let peer_id = node.node_id(); tokio::time::timeout(Duration::from_secs(10), async move { let request = GetRequest::last_chunk(hash); let (secret_key, peer) = get_options(peer_id, addrs); @@ -621,7 +621,7 @@ async fn test_collection_stat() { let (db, hash) = create_test_db([("a", &child1), ("b", &child2)]); let node = test_node(db.clone()).spawn().await.unwrap(); let addrs = node.local_endpoint_addresses().await.unwrap(); - let peer_id = node.node_id().await.unwrap(); + let peer_id = node.node_id(); tokio::time::timeout(Duration::from_secs(10), async move { // first 1024 bytes let header = ChunkRanges::from(..ChunkNum(1)); diff --git a/iroh/tests/sync.rs b/iroh/tests/sync.rs index d73cff3d9d2..74f3a8880a9 100644 --- a/iroh/tests/sync.rs +++ b/iroh/tests/sync.rs @@ -49,7 +49,7 @@ fn spawn_node( async move { let node = test_node(secret_key); let node = node.spawn().await?; - info!(?i, me = %node.node_id().await.unwrap().fmt_short(), "node spawned"); + info!(?i, me = %node.node_id().fmt_short(), "node spawned"); Ok(node) } } @@ -84,7 +84,7 @@ async fn sync_simple() -> Result<()> { let clients = nodes.iter().map(|node| node.client()).collect::>(); // create doc on node0 - let peer0 = nodes[0].node_id().await?; + let peer0 = nodes[0].node_id(); let author0 = clients[0].authors.create().await?; let doc0 = clients[0].docs.create().await?; let hash0 = doc0 @@ -98,7 +98,7 @@ async fn sync_simple() -> Result<()> { let mut events0 = doc0.subscribe().await?; info!("node1: join"); - let peer1 = nodes[1].node_id().await?; + let peer1 = nodes[1].node_id(); let doc1 = clients[1].docs.import(ticket.clone()).await?; let mut events1 = doc1.subscribe().await?; info!("node1: assert 4 events"); @@ -253,7 +253,7 @@ async fn sync_full_basic() -> Result<()> { .collect::>(); // peer0: create doc and ticket - let peer0 = nodes[0].node_id().await?; + let peer0 = nodes[0].node_id(); let author0 = clients[0].authors.create().await?; let doc0 = clients[0].docs.create().await?; let mut events0 = doc0.subscribe().await?; @@ -275,7 +275,7 @@ async fn sync_full_basic() -> Result<()> { .await?; info!("peer1: spawn"); - let peer1 = nodes[1].node_id().await?; + let peer1 = nodes[1].node_id(); let author1 = clients[1].authors.create().await?; info!("peer1: join doc"); let doc1 = clients[1].docs.import(ticket.clone()).await?; @@ -342,7 +342,7 @@ async fn sync_full_basic() -> Result<()> { nodes.push(spawn_node(nodes.len(), &mut rng).await?); clients.push(nodes.last().unwrap().client().clone()); let doc2 = clients[2].docs.import(ticket).await?; - let peer2 = nodes[2].node_id().await?; + let peer2 = nodes[2].node_id(); let mut events2 = doc2.subscribe().await?; info!("peer2: wait for 8 events (from sync with peers)"); @@ -486,7 +486,7 @@ async fn test_sync_via_relay() -> Result<()> { .insecure_skip_relay_cert_verify(true) .spawn() .await?; - let node1_id = node1.node_id().await?; + let node1_id = node1.node_id(); let node2 = Node::memory() .bind_port(0) .relay_mode(RelayMode::Custom(relay_map.clone())) @@ -581,7 +581,7 @@ async fn sync_restart_node() -> Result<()> { .node_discovery(discovery_server.discovery(secret_key_1.clone()).into()) .spawn() .await?; - let id1 = node1.node_id().await?; + let id1 = node1.node_id(); // create doc & ticket on node1 let doc1 = node1.docs.create().await?; @@ -600,7 +600,7 @@ async fn sync_restart_node() -> Result<()> { .node_discovery(discovery_server.discovery(secret_key_2.clone()).into()) .spawn() .await?; - let id2 = node2.node_id().await?; + let id2 = node2.node_id(); let author2 = node2.authors.create().await?; let doc2 = node2.docs.import(ticket.clone()).await?; @@ -644,7 +644,7 @@ async fn sync_restart_node() -> Result<()> { .node_discovery(discovery_server.discovery(secret_key_1.clone()).into()) .spawn() .await?; - assert_eq!(id1, node1.node_id().await?); + assert_eq!(id1, node1.node_id()); let doc1 = node1.docs.open(doc1.id()).await?.expect("doc to exist"); let mut events1 = doc1.subscribe().await?; @@ -849,10 +849,7 @@ async fn sync_big() -> Result<()> { }); let nodes = spawn_nodes(n_nodes, &mut rng).await?; - let mut node_ids = Vec::new(); - for node in &nodes { - node_ids.push(node.node_id().await?); - } + let node_ids = nodes.iter().map(|node| node.node_id()).collect::>(); let clients = nodes.iter().map(|node| node.client()).collect::>(); let authors = collect_futures(clients.iter().map(|c| c.authors.create())).await?;