diff --git a/iroh/src/client/blobs.rs b/iroh/src/client/blobs.rs index ba79d452b25..08afb37ae5a 100644 --- a/iroh/src/client/blobs.rs +++ b/iroh/src/client/blobs.rs @@ -35,8 +35,8 @@ use crate::rpc_protocol::{ BlobDeleteBlobRequest, BlobDownloadRequest, BlobExportRequest, BlobGetCollectionRequest, BlobGetCollectionResponse, BlobListCollectionsRequest, BlobListIncompleteRequest, BlobListRequest, BlobReadAtRequest, BlobReadAtResponse, BlobValidateRequest, - CreateCollectionRequest, CreateCollectionResponse, NodeStatusRequest, NodeStatusResponse, - ProviderService, SetTagOption, WrapOption, + CreateCollectionRequest, CreateCollectionResponse, NodeStatusRequest, ProviderService, + SetTagOption, WrapOption, }; use super::{flatten, Iroh}; @@ -357,7 +357,7 @@ where blob_format: BlobFormat, addr_options: AddrInfoOptions, ) -> Result { - let NodeStatusResponse { mut addr, .. } = self.rpc.rpc(NodeStatusRequest).await??; + let mut addr = self.rpc.rpc(NodeStatusRequest).await??.addr; addr.apply_options(addr_options); let ticket = BlobTicket::new(addr, hash, blob_format).expect("correct ticket"); diff --git a/iroh/src/client/node.rs b/iroh/src/client/node.rs index 5622f6724ae..57d4bf3db52 100644 --- a/iroh/src/client/node.rs +++ b/iroh/src/client/node.rs @@ -1,16 +1,17 @@ //! API to manage the iroh node itself. -use std::collections::BTreeMap; +use std::{collections::BTreeMap, net::SocketAddr}; use anyhow::Result; use futures_lite::{Stream, StreamExt}; use iroh_base::key::PublicKey; -use iroh_net::magic_endpoint::ConnectionInfo; +use iroh_net::{magic_endpoint::ConnectionInfo, NodeAddr, NodeId}; use quic_rpc::{RpcClient, ServiceConnection}; +use serde::{Deserialize, Serialize}; use crate::rpc_protocol::{ CounterStats, NodeConnectionInfoRequest, NodeConnectionInfoResponse, NodeConnectionsRequest, - NodeShutdownRequest, NodeStatsRequest, NodeStatusRequest, NodeStatusResponse, ProviderService, + NodeIdRequest, NodeShutdownRequest, NodeStatsRequest, NodeStatusRequest, ProviderService, }; use super::flatten; @@ -46,12 +47,18 @@ where Ok(conn_info) } - /// Get status information about a node - pub async fn status(&self) -> Result { + /// Get status information about a node. + pub async fn status(&self) -> Result { let response = self.rpc.rpc(NodeStatusRequest).await??; Ok(response) } + /// Get the id of this node. + pub async fn id(&self) -> Result { + let id = self.rpc.rpc(NodeIdRequest).await??; + Ok(id) + } + /// Shutdown the node. /// /// If `force` is true, the node will be killed instantly without waiting for things to @@ -61,3 +68,14 @@ where Ok(()) } } + +/// The response to a version request +#[derive(Debug, Serialize, Deserialize)] +pub struct NodeStatus { + /// The node id and socket addresses of this node. + pub addr: NodeAddr, + /// The bound listening addresses of the node + pub listen_addrs: Vec, + /// The version of the node + pub version: String, +} diff --git a/iroh/src/node/rpc.rs b/iroh/src/node/rpc.rs index e387b1e44fe..e616fdbd8a5 100644 --- a/iroh/src/node/rpc.rs +++ b/iroh/src/node/rpc.rs @@ -24,7 +24,7 @@ use iroh_bytes::{ HashAndFormat, }; use iroh_io::AsyncSliceReader; -use iroh_net::{MagicEndpoint, NodeAddr}; +use iroh_net::{MagicEndpoint, NodeAddr, NodeId}; use quic_rpc::{ server::{RpcChannel, RpcServerError}, ServiceEndpoint, @@ -33,6 +33,7 @@ use tokio_util::task::LocalPoolHandle; use tracing::{debug, info}; use crate::client::blobs::{BlobInfo, CollectionInfo, DownloadMode, IncompleteBlobInfo}; +use crate::client::node::NodeStatus; use crate::client::tags::TagInfo; use crate::rpc_protocol::{ BlobAddPathRequest, BlobAddPathResponse, BlobAddStreamRequest, BlobAddStreamResponse, @@ -43,8 +44,8 @@ use crate::rpc_protocol::{ CreateCollectionRequest, CreateCollectionResponse, DeleteTagRequest, DocExportFileRequest, DocExportFileResponse, DocImportFileRequest, DocImportFileResponse, DocImportProgress, DocSetHashRequest, ListTagsRequest, NodeConnectionInfoRequest, NodeConnectionInfoResponse, - NodeConnectionsRequest, NodeConnectionsResponse, NodeShutdownRequest, NodeStatsRequest, - NodeStatsResponse, NodeStatusRequest, NodeStatusResponse, NodeWatchRequest, NodeWatchResponse, + NodeConnectionsRequest, NodeConnectionsResponse, NodeIdRequest, NodeShutdownRequest, + NodeStatsRequest, NodeStatsResponse, NodeStatusRequest, NodeWatchRequest, NodeWatchResponse, ProviderRequest, ProviderService, SetTagOption, }; @@ -74,6 +75,7 @@ impl Handler { match msg { NodeWatch(msg) => chan.server_streaming(msg, handler, Self::node_watch).await, NodeStatus(msg) => chan.rpc(msg, handler, Self::node_status).await, + NodeId(msg) => chan.rpc(msg, handler, Self::node_id).await, NodeShutdown(msg) => chan.rpc(msg, handler, Self::node_shutdown).await, NodeStats(msg) => chan.rpc(msg, handler, Self::node_stats).await, NodeConnections(msg) => { @@ -777,8 +779,8 @@ impl Handler { res } - async fn node_status(self, _: NodeStatusRequest) -> RpcResult { - Ok(NodeStatusResponse { + async fn node_status(self, _: NodeStatusRequest) -> RpcResult { + Ok(NodeStatus { addr: self.inner.endpoint.my_addr().await?, listen_addrs: self .inner @@ -789,6 +791,11 @@ impl Handler { }) } + #[allow(clippy::unused_async)] + async fn node_id(self, _: NodeIdRequest) -> RpcResult { + Ok(self.inner.secret_key.public()) + } + #[allow(clippy::unused_async)] async fn node_shutdown(self, request: NodeShutdownRequest) { if request.force { diff --git a/iroh/src/rpc_protocol.rs b/iroh/src/rpc_protocol.rs index a0efd8a71d2..08c7a0ad012 100644 --- a/iroh/src/rpc_protocol.rs +++ b/iroh/src/rpc_protocol.rs @@ -7,7 +7,7 @@ //! response, while others like provide have a stream of responses. //! //! Note that this is subject to change. The RPC protocol is not yet stable. -use std::{collections::BTreeMap, net::SocketAddr, path::PathBuf}; +use std::{collections::BTreeMap, path::PathBuf}; use bytes::Bytes; use derive_more::{From, TryInto}; @@ -21,6 +21,7 @@ use iroh_bytes::{ use iroh_net::{ key::PublicKey, magic_endpoint::{ConnectionInfo, NodeAddr}, + NodeId, }; use iroh_sync::{ @@ -42,6 +43,7 @@ pub use iroh_bytes::{provider::AddProgress, store::ValidateProgress}; use crate::{ client::{ blobs::{BlobInfo, CollectionInfo, DownloadMode, IncompleteBlobInfo}, + node::NodeStatus, tags::TagInfo, }, sync_engine::LiveEvent, @@ -356,25 +358,20 @@ impl RpcMsg for NodeShutdownRequest { type Response = (); } -/// A request to get information about the identity of the node -/// -/// See [`NodeStatusResponse`] for the response. +/// A request to get information about the status of the node. #[derive(Serialize, Deserialize, Debug)] pub struct NodeStatusRequest; impl RpcMsg for NodeStatusRequest { - type Response = RpcResult; + type Response = RpcResult; } -/// The response to a version request +/// A request to get information the identity of the node. #[derive(Serialize, Deserialize, Debug)] -pub struct NodeStatusResponse { - /// The node id and socket addresses of this node. - pub addr: NodeAddr, - /// The bound listening addresses of the node - pub listen_addrs: Vec, - /// The version of the node - pub version: String, +pub struct NodeIdRequest; + +impl RpcMsg for NodeIdRequest { + type Response = RpcResult; } /// A request to watch for the node status @@ -1066,6 +1063,7 @@ pub struct ProviderService; #[derive(strum::Display, Debug, Serialize, Deserialize, From, TryInto)] pub enum ProviderRequest { NodeStatus(NodeStatusRequest), + NodeId(NodeIdRequest), NodeStats(NodeStatsRequest), NodeShutdown(NodeShutdownRequest), NodeConnections(NodeConnectionsRequest), @@ -1123,7 +1121,8 @@ pub enum ProviderRequest { #[allow(missing_docs, clippy::large_enum_variant)] #[derive(Debug, Serialize, Deserialize, From, TryInto)] pub enum ProviderResponse { - NodeStatus(RpcResult), + NodeStatus(RpcResult), + NodeId(RpcResult), NodeStats(RpcResult), NodeConnections(RpcResult), NodeConnectionInfo(RpcResult),