Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(iroh)!: Extract net and node rpc #2927

Merged
merged 42 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
4d30302
refactor(iroh): extract docs RPC into iroh-docs
dignifiedquire Oct 30, 2024
a749306
remove dead code
dignifiedquire Oct 30, 2024
c9ff5cd
fix?
divagant-martian Oct 30, 2024
d301ed7
doc needs to be used with a different param
divagant-martian Oct 30, 2024
cea6d32
more fixes
divagant-martian Oct 30, 2024
ac5b0e0
it compiles again
dignifiedquire Oct 31, 2024
b4821d1
fixup cli
dignifiedquire Oct 31, 2024
940b28d
extract authors
dignifiedquire Oct 31, 2024
6fe36b3
Merge branch 'main' into refactor-extract-rpc-docs
rklaehn Nov 7, 2024
b47eb11
Update to latest quic-rpc
rklaehn Nov 7, 2024
f97acb7
fix docs
rklaehn Nov 7, 2024
f60c829
Merge branch 'main' into refactor-extract-rpc-docs
rklaehn Nov 7, 2024
2d671b6
fix default author in example
rklaehn Nov 7, 2024
69c7fb0
remove entry.content_bytes from examples
rklaehn Nov 7, 2024
06d49ca
Merge branch 'main' into refactor-extract-rpc-docs
rklaehn Nov 7, 2024
c911ff8
Merge branch 'main' into refactor-extract-rpc-docs
rklaehn Nov 7, 2024
c8a0e48
Add generics for docs engine
rklaehn Nov 7, 2024
377143e
Merge branch 'main' into refactor-extract-rpc-docs
rklaehn Nov 12, 2024
0b6042f
Go back to .authors(), at least for the client.
rklaehn Nov 12, 2024
11c15fd
Also export authors client
rklaehn Nov 12, 2024
8ab5f83
fix example
rklaehn Nov 12, 2024
97ffc0f
WIP
rklaehn Nov 13, 2024
3e04bd1
Merge branch 'main' into extract-node-rpc
rklaehn Nov 13, 2024
172c890
refactor: Add new crate iroh-node-util (name TBD)...
rklaehn Nov 14, 2024
8288006
Merge branch 'main' into extract-node-rpc
rklaehn Nov 14, 2024
3449d4e
Merge branch 'main' into extract-node-rpc
rklaehn Nov 19, 2024
4f9cf48
remove dead code and remove example
rklaehn Nov 19, 2024
06feab6
remove util methods
rklaehn Nov 19, 2024
8e1d4e9
add a LOICENSE
rklaehn Nov 19, 2024
807c11d
clippy
rklaehn Nov 19, 2024
28d8d96
use a trait
rklaehn Nov 19, 2024
59a62f2
WIP
rklaehn Nov 19, 2024
fa52a9c
wire up metrics
rklaehn Nov 19, 2024
e97e243
Add some docs for the trait to be implemented
rklaehn Nov 19, 2024
1d77a0b
fix docs
rklaehn Nov 19, 2024
1834989
fix dependency versions
rklaehn Nov 19, 2024
d4002c4
Add README.md and description
rklaehn Nov 19, 2024
09124d5
Add docs everywhere it makes sense...
rklaehn Nov 19, 2024
c89780c
format cargo.toml
rklaehn Nov 19, 2024
0f6d98d
Merge branch 'main' into extract-node-rpc
rklaehn Nov 20, 2024
e6da450
PR review
rklaehn Nov 20, 2024
e387c09
Use path directly for iroh-node-util
rklaehn Nov 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 44 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ members = [
"iroh-relay",
"iroh-router",
"net-tools/netwatch",
"net-tools/portmapper",
"net-tools/portmapper", "iroh-node-util",
rklaehn marked this conversation as resolved.
Show resolved Hide resolved
]
resolver = "2"

Expand Down
23 changes: 23 additions & 0 deletions iroh-node-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "iroh-node-util"
version = "0.1.0"
rklaehn marked this conversation as resolved.
Show resolved Hide resolved
edition = "2021"

[dependencies]
anyhow = "1.0.93"
tokio = "1.41.1"
iroh-net = { path = "../iroh-net" }
tempfile = "3.14.0"
strum = "0.26.3"
nested_enum_utils = "0.1.0"
quic-rpc-derive = "0.15.0"
serde = "1.0.215"
serde-error = "0.1.3"
quic-rpc = "0.15.0"
futures-lite = "2.5.0"
ref-cast = "1.0.23"
tracing = "0.1.40"
tokio-util = "0.7.12"

[lints]
workspace = true
46 changes: 46 additions & 0 deletions iroh-node-util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//! Utilities for building iroh nodes.
pub mod rpc;

use std::path::PathBuf;

use anyhow::Context;
use iroh_net::key::SecretKey;
use tokio::io::AsyncWriteExt;

/// Loads a [`SecretKey`] from the provided file, or stores a newly generated one
/// at the given location.
pub async fn load_secret_key(key_path: PathBuf) -> anyhow::Result<SecretKey> {
if key_path.exists() {
let keystr = tokio::fs::read(key_path).await?;
let secret_key = SecretKey::try_from_openssh(keystr).context("invalid keyfile")?;
Ok(secret_key)
} else {
let secret_key = SecretKey::generate();
let ser_key = secret_key.to_openssh()?;

// Try to canonicalize if possible
let key_path = key_path.canonicalize().unwrap_or(key_path);
let key_path_parent = key_path.parent().ok_or_else(|| {
anyhow::anyhow!("no parent directory found for '{}'", key_path.display())
})?;
tokio::fs::create_dir_all(&key_path_parent).await?;

// write to tempfile
let (file, temp_file_path) = tempfile::NamedTempFile::new_in(key_path_parent)
.context("unable to create tempfile")?
.into_parts();
let mut file = tokio::fs::File::from_std(file);
file.write_all(ser_key.as_bytes())
.await
.context("unable to write keyfile")?;
file.flush().await?;
drop(file);

// move file
tokio::fs::rename(temp_file_path, key_path)
.await
.context("failed to rename keyfile")?;

Ok(secret_key)
}
}
3 changes: 3 additions & 0 deletions iroh-node-util/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod client;
pub mod proto;
pub mod server;
19 changes: 19 additions & 0 deletions iroh-node-util/src/rpc/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use anyhow::Result;
use futures_lite::{Stream, StreamExt};

pub mod net;
pub mod node;

fn flatten<T, E1, E2>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add this to quic-rpc? its used everywhere 🤣

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this used? In the context of streaming responses, right? I can maybe add it somewhere in client as an util...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not super urgent, but I am adding it to quic-rpc: n0-computer/quic-rpc#120

s: impl Stream<Item = Result<Result<T, E1>, E2>>,
) -> impl Stream<Item = Result<T>>
where
E1: std::error::Error + Send + Sync + 'static,
E2: std::error::Error + Send + Sync + 'static,
{
s.map(|res| match res {
Ok(Ok(res)) => Ok(res),
Ok(Err(err)) => Err(err.into()),
Err(err) => Err(err.into()),
})
}
26 changes: 16 additions & 10 deletions iroh/src/client/net.rs → iroh-node-util/src/rpc/client/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ use std::net::SocketAddr;

use anyhow::Result;
use futures_lite::{Stream, StreamExt};
use iroh_base::node_addr::RelayUrl;
use iroh_net::{endpoint::RemoteInfo, NodeAddr, NodeId};
use ref_cast::RefCast;
use iroh_net::{endpoint::RemoteInfo, relay::RelayUrl, NodeAddr, NodeId};
use quic_rpc::RpcClient;
use serde::{Deserialize, Serialize};

use super::{flatten, RpcClient};
use crate::rpc_protocol::net::{
AddAddrRequest, AddrRequest, IdRequest, RelayRequest, RemoteInfoRequest, RemoteInfoResponse,
RemoteInfosIterRequest,
use super::flatten;
use crate::rpc::proto::{
net::{
AddAddrRequest, AddrRequest, IdRequest, RelayRequest, RemoteInfoRequest,
RemoteInfoResponse, RemoteInfosIterRequest,
},
RpcService,
};

/// Iroh netx Client.
/// Iroh net Client.
///
/// Cheaply clonable and threadsafe. Use the iroh `net::Client` to access the
/// iroh net methods from a different thread, process, or remote machine.
Expand Down Expand Up @@ -70,13 +72,17 @@ use crate::rpc_protocol::net::{
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, RefCast)]
#[derive(Debug, Clone)]
#[repr(transparent)]
pub struct Client {
pub(super) rpc: RpcClient,
pub(super) rpc: RpcClient<RpcService>,
}

impl Client {
pub fn new(rpc: RpcClient<RpcService>) -> Self {
rklaehn marked this conversation as resolved.
Show resolved Hide resolved
Self { rpc }
}

/// Fetches information about currently known remote nodes.
///
/// This streams a *current snapshot*. It does not keep the stream open after finishing
Expand Down
Loading
Loading