Skip to content

Commit 59e2719

Browse files
authored
feat(iroh): Add rpc request to add an AddrInfo (#2433)
## Description Add rpc request to add an addr to an endpoint. Without this it is not possible to write some tests using just the client. Move all node related fns into a separate "node client" and impl deref for it so the fns remain available on Iroh. ## Breaking Changes - iroh::client::Iroh: stats only available via deref from iroh::client::node::Client - iroh::client::Iroh: connections only available via deref from iroh::client::node::Client - iroh::client::Iroh: connection_info only available via deref from iroh::client::node::Client - iroh::client::Iroh: status only available via deref from iroh::client::node::Client - iroh::client::Iroh: node_id only available via deref from iroh::client::node::Client - iroh::client::Iroh: node_addr only available via deref from iroh::client::node::Client - iroh::client::Iroh: home_addr only available via deref from iroh::client::node::Client - iroh::client::Iroh: shutdown only available via deref from iroh::client::node::Client ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [ ] Self-review. - [ ] Documentation updates if relevant. - [ ] Tests if relevant. - [ ] All breaking changes documented.
1 parent 6b4435d commit 59e2719

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

iroh/src/client.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use futures_lite::{Stream, StreamExt};
44
use ref_cast::RefCast;
5+
use std::ops::Deref;
56

67
#[doc(inline)]
78
pub use crate::rpc_protocol::RpcService;
@@ -24,10 +25,9 @@ pub(crate) use self::quic::{connect_raw as quic_connect_raw, RPC_ALPN};
2425
pub mod authors;
2526
pub mod blobs;
2627
pub mod docs;
28+
pub mod node;
2729
pub mod tags;
2830

29-
mod node;
30-
3131
/// Iroh rpc client - boxed so that we can have a concrete type.
3232
pub(crate) type RpcClient =
3333
quic_rpc::RpcClient<RpcService, quic_rpc::transport::boxed::Connection<RpcService>>;
@@ -38,6 +38,14 @@ pub struct Iroh {
3838
rpc: RpcClient,
3939
}
4040

41+
impl Deref for Iroh {
42+
type Target = node::Client;
43+
44+
fn deref(&self) -> &Self::Target {
45+
self.node()
46+
}
47+
}
48+
4149
impl Iroh {
4250
/// Create a new high-level client to a Iroh node from the low-level RPC client.
4351
pub fn new(rpc: RpcClient) -> Self {
@@ -63,6 +71,11 @@ impl Iroh {
6371
pub fn tags(&self) -> &tags::Client {
6472
tags::Client::ref_cast(&self.rpc)
6573
}
74+
75+
/// Node client
76+
pub fn node(&self) -> &node::Client {
77+
node::Client::ref_cast(&self.rpc)
78+
}
6679
}
6780

6881
fn flatten<T, E1, E2>(

iroh/src/client/node.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,25 @@ use anyhow::Result;
66
use futures_lite::{Stream, StreamExt};
77
use iroh_base::key::PublicKey;
88
use iroh_net::{endpoint::ConnectionInfo, relay::RelayUrl, NodeAddr, NodeId};
9+
use ref_cast::RefCast;
910
use serde::{Deserialize, Serialize};
1011

1112
use crate::rpc_protocol::{
12-
CounterStats, NodeAddrRequest, NodeConnectionInfoRequest, NodeConnectionInfoResponse,
13-
NodeConnectionsRequest, NodeIdRequest, NodeRelayRequest, NodeShutdownRequest, NodeStatsRequest,
14-
NodeStatusRequest,
13+
CounterStats, NodeAddAddrRequest, NodeAddrRequest, NodeConnectionInfoRequest,
14+
NodeConnectionInfoResponse, NodeConnectionsRequest, NodeIdRequest, NodeRelayRequest,
15+
NodeShutdownRequest, NodeStatsRequest, NodeStatusRequest,
1516
};
1617

17-
use super::{flatten, Iroh};
18+
use super::{flatten, RpcClient};
1819

19-
impl Iroh {
20+
/// Iroh node client.
21+
#[derive(Debug, Clone, RefCast)]
22+
#[repr(transparent)]
23+
pub struct Client {
24+
pub(super) rpc: RpcClient,
25+
}
26+
27+
impl Client {
2028
/// Get statistics of the running node.
2129
pub async fn stats(&self) -> Result<BTreeMap<String, CounterStats>> {
2230
let res = self.rpc.rpc(NodeStatsRequest {}).await??;
@@ -56,6 +64,12 @@ impl Iroh {
5664
Ok(addr)
5765
}
5866

67+
/// Add a known node address to the node.
68+
pub async fn add_node_addr(&self, addr: NodeAddr) -> Result<()> {
69+
self.rpc.rpc(NodeAddAddrRequest { addr }).await??;
70+
Ok(())
71+
}
72+
5973
/// Get the relay server we are connected to.
6074
pub async fn home_relay(&self) -> Result<Option<RelayUrl>> {
6175
let relay = self.rpc.rpc(NodeRelayRequest).await??;

iroh/src/node/rpc.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ use crate::rpc_protocol::{
4646
BlobListRequest, BlobReadAtRequest, BlobReadAtResponse, BlobValidateRequest,
4747
CreateCollectionRequest, CreateCollectionResponse, DeleteTagRequest, DocExportFileRequest,
4848
DocExportFileResponse, DocImportFileRequest, DocImportFileResponse, DocSetHashRequest,
49-
ListTagsRequest, NodeAddrRequest, NodeConnectionInfoRequest, NodeConnectionInfoResponse,
50-
NodeConnectionsRequest, NodeConnectionsResponse, NodeIdRequest, NodeRelayRequest,
51-
NodeShutdownRequest, NodeStatsRequest, NodeStatsResponse, NodeStatusRequest, NodeWatchRequest,
52-
NodeWatchResponse, Request, RpcService, SetTagOption,
49+
ListTagsRequest, NodeAddAddrRequest, NodeAddrRequest, NodeConnectionInfoRequest,
50+
NodeConnectionInfoResponse, NodeConnectionsRequest, NodeConnectionsResponse, NodeIdRequest,
51+
NodeRelayRequest, NodeShutdownRequest, NodeStatsRequest, NodeStatsResponse, NodeStatusRequest,
52+
NodeWatchRequest, NodeWatchResponse, Request, RpcService, SetTagOption,
5353
};
5454

5555
mod docs;
@@ -141,6 +141,7 @@ impl<D: BaoStore> Handler<D> {
141141
.await
142142
}
143143
NodeConnectionInfo(msg) => chan.rpc(msg, self, Self::node_connection_info).await,
144+
NodeAddAddr(msg) => chan.rpc(msg, self, Self::node_add_addr).await,
144145
BlobList(msg) => chan.server_streaming(msg, self, Self::blob_list).await,
145146
BlobListIncomplete(msg) => {
146147
chan.server_streaming(msg, self, Self::blob_list_incomplete)
@@ -996,6 +997,14 @@ impl<D: BaoStore> Handler<D> {
996997
Ok(NodeConnectionInfoResponse { conn_info })
997998
}
998999

1000+
// This method is called as an RPC method, which have to be async
1001+
#[allow(clippy::unused_async)]
1002+
async fn node_add_addr(self, req: NodeAddAddrRequest) -> RpcResult<()> {
1003+
let NodeAddAddrRequest { addr } = req;
1004+
self.inner.endpoint.add_node_addr(addr)?;
1005+
Ok(())
1006+
}
1007+
9991008
async fn create_collection(
10001009
self,
10011010
req: CreateCollectionRequest,

iroh/src/rpc_protocol.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,15 @@ impl RpcMsg<RpcService> for NodeAddrRequest {
365365
type Response = RpcResult<NodeAddr>;
366366
}
367367

368+
#[derive(Serialize, Deserialize, Debug)]
369+
pub struct NodeAddAddrRequest {
370+
pub addr: NodeAddr,
371+
}
372+
373+
impl RpcMsg<RpcService> for NodeAddAddrRequest {
374+
type Response = RpcResult<()>;
375+
}
376+
368377
#[derive(Serialize, Deserialize, Debug)]
369378
pub struct NodeRelayRequest;
370379

@@ -1046,6 +1055,7 @@ pub enum Request {
10461055
NodeStatus(NodeStatusRequest),
10471056
NodeId(NodeIdRequest),
10481057
NodeAddr(NodeAddrRequest),
1058+
NodeAddAddr(NodeAddAddrRequest),
10491059
NodeRelay(NodeRelayRequest),
10501060
NodeStats(NodeStatsRequest),
10511061
NodeShutdown(NodeShutdownRequest),

0 commit comments

Comments
 (0)