Skip to content

Commit

Permalink
refactor(iroh): remove rpc artifacts from node
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 2, 2024
1 parent ce2d026 commit 1c677e3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
6 changes: 3 additions & 3 deletions iroh/src/client/blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -357,7 +357,7 @@ where
blob_format: BlobFormat,
addr_options: AddrInfoOptions,
) -> Result<BlobTicket> {
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");

Expand Down
28 changes: 23 additions & 5 deletions iroh/src/client/node.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -46,12 +47,18 @@ where
Ok(conn_info)
}

/// Get status information about a node
pub async fn status(&self) -> Result<NodeStatusResponse> {
/// Get status information about a node.
pub async fn status(&self) -> Result<NodeStatus> {
let response = self.rpc.rpc(NodeStatusRequest).await??;
Ok(response)
}

/// Get the id of this node.
pub async fn id(&self) -> Result<NodeId> {
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
Expand All @@ -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<SocketAddr>,
/// The version of the node
pub version: String,
}
17 changes: 12 additions & 5 deletions iroh/src/node/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
};

Expand Down Expand Up @@ -74,6 +75,7 @@ impl<D: BaoStore> Handler<D> {
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) => {
Expand Down Expand Up @@ -777,8 +779,8 @@ impl<D: BaoStore> Handler<D> {
res
}

async fn node_status(self, _: NodeStatusRequest) -> RpcResult<NodeStatusResponse> {
Ok(NodeStatusResponse {
async fn node_status(self, _: NodeStatusRequest) -> RpcResult<NodeStatus> {
Ok(NodeStatus {
addr: self.inner.endpoint.my_addr().await?,
listen_addrs: self
.inner
Expand All @@ -789,6 +791,11 @@ impl<D: BaoStore> Handler<D> {
})
}

#[allow(clippy::unused_async)]
async fn node_id(self, _: NodeIdRequest) -> RpcResult<NodeId> {
Ok(self.inner.secret_key.public())
}

#[allow(clippy::unused_async)]
async fn node_shutdown(self, request: NodeShutdownRequest) {
if request.force {
Expand Down
27 changes: 13 additions & 14 deletions iroh/src/rpc_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -21,6 +21,7 @@ use iroh_bytes::{
use iroh_net::{
key::PublicKey,
magic_endpoint::{ConnectionInfo, NodeAddr},
NodeId,
};

use iroh_sync::{
Expand All @@ -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,
Expand Down Expand Up @@ -356,25 +358,20 @@ impl RpcMsg<ProviderService> 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<ProviderService> for NodeStatusRequest {
type Response = RpcResult<NodeStatusResponse>;
type Response = RpcResult<NodeStatus>;
}

/// 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<SocketAddr>,
/// The version of the node
pub version: String,
pub struct NodeIdRequest;

impl RpcMsg<ProviderService> for NodeIdRequest {
type Response = RpcResult<NodeId>;
}

/// A request to watch for the node status
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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<NodeStatusResponse>),
NodeStatus(RpcResult<NodeStatus>),
NodeId(RpcResult<NodeId>),
NodeStats(RpcResult<NodeStatsResponse>),
NodeConnections(RpcResult<NodeConnectionsResponse>),
NodeConnectionInfo(RpcResult<NodeConnectionInfoResponse>),
Expand Down

0 comments on commit 1c677e3

Please sign in to comment.