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

chore: upgrade experiments to iroh v0.29 #20

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 2 additions & 3 deletions content-discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ missing_debug_implementations = "warn"
unused-async = "warn"

[workspace.dependencies]
iroh-net = "0.28"
iroh-blobs = "0.28"
iroh-base = "0.28"
iroh = "0.29"
iroh-blobs = "0.29"
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ license = "MIT OR Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
iroh-net = { workspace = true }
iroh = { workspace = true }
iroh-blobs = { workspace = true }
iroh-base = { workspace = true }
iroh-mainline-content-discovery = { path = "../iroh-mainline-content-discovery" }
mainline = { version = "2.0.0" }
anyhow = { version = "1", features = ["backtrace"] }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Command line arguments.
use clap::{Parser, Subcommand};
use iroh_base::ticket::BlobTicket;
use iroh::{ticket::BlobTicket, NodeId};
use iroh_blobs::{Hash, HashAndFormat};
use iroh_net::NodeId;
use std::{
fmt::Display,
net::{SocketAddr, SocketAddrV4},
Expand Down
46 changes: 2 additions & 44 deletions content-discovery/iroh-mainline-content-discovery-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ use std::{
str::FromStr,
};

use anyhow::Context;
use args::QueryDhtArgs;
use clap::Parser;
use futures::StreamExt;
use iroh::endpoint;
use iroh_mainline_content_discovery::{
create_quinn_client,
protocol::{AbsoluteTime, Announce, AnnounceKind, Query, QueryFlags, SignedAnnounce},
to_infohash, UdpDiscovery,
};
use iroh_net::endpoint;
use tokio::io::AsyncWriteExt;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

use crate::args::{AnnounceArgs, Args, Commands, QueryArgs};
Expand All @@ -26,7 +24,7 @@ async fn announce(args: AnnounceArgs) -> anyhow::Result<()> {
eprintln!("ANNOUNCE_SECRET environment variable must be set to a valid secret key");
anyhow::bail!("ANNOUNCE_SECRET env var not set");
};
let Ok(key) = iroh_net::key::SecretKey::from_str(&key) else {
let Ok(key) = iroh::key::SecretKey::from_str(&key) else {
anyhow::bail!("ANNOUNCE_SECRET env var is not a valid secret key");
};
let content = args.content.hash_and_format();
Expand Down Expand Up @@ -165,43 +163,3 @@ async fn main() -> anyhow::Result<()> {
Commands::QueryDht(args) => query_dht(args).await,
}
}

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

// Try to canoncialize 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license = "MIT OR Apache-2.0"
# Required features for the protocol types.
#
# The protocol is using postcard, but we don't need a postcard dependency for just the type definitions
iroh-net = { workspace = true }
iroh = { workspace = true }
iroh-blobs = { workspace = true }
serde = { version = "1", features = ["derive"] }
derive_more = { version = "1.0.0-beta.1", features = ["debug", "display", "from", "try_into"] }
Expand Down
24 changes: 12 additions & 12 deletions content-discovery/iroh-mainline-content-discovery/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use futures::{
stream::FusedStream,
FutureExt, Stream, StreamExt,
};
use iroh_blobs::HashAndFormat;
use iroh_net::{
use iroh::{
discovery::{dns::DnsDiscovery, pkarr::PkarrPublisher, Discovery},
Endpoint, NodeId,
};
use iroh_blobs::HashAndFormat;

use crate::protocol::{
AnnounceKind, Query, QueryResponse, Request, Response, SignedAnnounce, ALPN, REQUEST_SIZE_LIMIT,
Expand All @@ -33,7 +33,7 @@ use crate::protocol::{
/// `content` is the content to announce.
/// `kind` is the kind of the announcement. We can claim to have the complete data or only some of it.
pub async fn announce(
connection: iroh_net::endpoint::Connection,
connection: iroh::endpoint::Connection,
signed_announce: SignedAnnounce,
) -> anyhow::Result<()> {
let (mut send, mut recv) = connection.open_bi().await?;
Expand Down Expand Up @@ -186,7 +186,7 @@ pub fn announce_dht(

/// Assume an existing connection to a tracker and query it for peers for some content.
pub async fn query(
connection: iroh_net::endpoint::Connection,
connection: iroh::endpoint::Connection,
args: Query,
) -> anyhow::Result<QueryResponse> {
tracing::info!("connected to {:?}", connection.remote_address());
Expand All @@ -210,9 +210,9 @@ pub fn create_quinn_client(
alpn_protocols: Vec<Vec<u8>>,
keylog: bool,
) -> anyhow::Result<iroh_quinn::Endpoint> {
let secret_key = iroh_net::key::SecretKey::generate();
let secret_key = iroh::key::SecretKey::generate();
let tls_client_config =
iroh_net::tls::make_client_config(&secret_key, None, alpn_protocols, keylog)?;
iroh::tls::make_client_config(&secret_key, None, alpn_protocols, keylog)?;
let mut client_config = iroh_quinn::ClientConfig::new(Arc::new(tls_client_config));
let mut endpoint = iroh_quinn::Endpoint::client(bind_addr)?;
let mut transport_config = iroh_quinn::TransportConfig::default();
Expand All @@ -223,7 +223,7 @@ pub fn create_quinn_client(
}

async fn create_endpoint(
key: iroh_net::key::SecretKey,
key: iroh::key::SecretKey,
ipv4_addr: SocketAddrV4,
ipv6_addr: SocketAddrV6,
publish: bool,
Expand All @@ -233,7 +233,7 @@ async fn create_endpoint(
} else {
Box::new(DnsDiscovery::n0_dns())
};
iroh_net::Endpoint::builder()
iroh::Endpoint::builder()
.secret_key(key)
.discovery(mainline_discovery)
.alpns(vec![ALPN.to_vec()])
Expand Down Expand Up @@ -284,7 +284,7 @@ pub async fn connect(
tracker: &TrackerId,
local_ipv4_addr: SocketAddrV4,
local_ipv6_addr: SocketAddrV6,
) -> anyhow::Result<iroh_net::endpoint::Connection> {
) -> anyhow::Result<iroh::endpoint::Connection> {
match tracker {
TrackerId::Quinn(tracker) => connect_socket(*tracker, local_ipv4_addr.into()).await,
TrackerId::Iroh(tracker) => connect_iroh(*tracker, local_ipv4_addr, local_ipv6_addr).await,
Expand All @@ -297,11 +297,11 @@ async fn connect_iroh(
tracker: NodeId,
local_ipv4_addr: SocketAddrV4,
local_ipv6_addr: SocketAddrV6,
) -> anyhow::Result<iroh_net::endpoint::Connection> {
) -> anyhow::Result<iroh::endpoint::Connection> {
// todo: uncomment once the connection problems are fixed
// for now, a random node id is more reliable.
// let key = load_secret_key(tracker_path(CLIENT_KEY)?).await?;
let key = iroh_net::key::SecretKey::generate();
let key = iroh::key::SecretKey::generate();
let endpoint = create_endpoint(key, local_ipv4_addr, local_ipv6_addr, false).await?;
tracing::info!("trying to connect to tracker at {:?}", tracker);
let connection = endpoint.connect(tracker, ALPN).await?;
Expand All @@ -312,7 +312,7 @@ async fn connect_iroh(
async fn connect_socket(
tracker: SocketAddr,
local_addr: SocketAddr,
) -> anyhow::Result<iroh_net::endpoint::Connection> {
) -> anyhow::Result<iroh::endpoint::Connection> {
let endpoint = create_quinn_client(local_addr, vec![ALPN.to_vec()], false)?;
tracing::info!("trying to connect to tracker at {:?}", tracker);
let connection = endpoint.connect(tracker, "localhost")?.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::{
time::{Duration, SystemTime},
};

use iroh::NodeId;
use iroh_blobs::HashAndFormat;
use iroh_net::NodeId;
use serde::{Deserialize, Serialize};
use serde_big_array::BigArray;

Expand Down Expand Up @@ -118,7 +118,7 @@ impl Deref for SignedAnnounce {

impl SignedAnnounce {
/// Create a new signed announce.
pub fn new(announce: Announce, secret_key: &iroh_net::key::SecretKey) -> anyhow::Result<Self> {
pub fn new(announce: Announce, secret_key: &iroh::key::SecretKey) -> anyhow::Result<Self> {
let announce_bytes = postcard::to_allocvec(&announce)?;
let signature = secret_key.sign(&announce_bytes).to_bytes();
Ok(Self {
Expand All @@ -130,7 +130,7 @@ impl SignedAnnounce {
/// Verify the announce, and return the announce if it's valid.
pub fn verify(&self) -> anyhow::Result<()> {
let announce_bytes = postcard::to_allocvec(&self.announce)?;
let signature = iroh_net::key::Signature::from_bytes(&self.signature);
let signature = iroh::key::Signature::from_bytes(&self.signature);
self.announce.host.verify(&announce_bytes, &signature)?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion content-discovery/iroh-mainline-tracker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ed25519-dalek = "2.1.0"
futures = "0.3.25"
hex = "0.4.3"
humantime = "2.1.0"
iroh-net = { workspace = true }
iroh = { workspace = true }
iroh-blobs = { workspace = true }
mainline = { version = "2.0.0", features = ["async"] }
pkarr = { version = "1.0.1", features = ["async"] }
Expand Down
2 changes: 1 addition & 1 deletion content-discovery/iroh-mainline-tracker/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use std::{
};

use anyhow::Context;
use iroh::NodeId;
use iroh_blobs::{get::Stats, HashAndFormat};
use iroh_mainline_content_discovery::protocol::{AnnounceKind, SignedAnnounce};
use iroh_net::NodeId;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tracing_subscriber::{prelude::*, EnvFilter};

Expand Down
60 changes: 7 additions & 53 deletions content-discovery/iroh-mainline-tracker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use std::{
time::{Duration, Instant},
};

use anyhow::Context;
use clap::Parser;
use iroh::{discovery::pkarr::dht::DhtDiscovery, endpoint::get_remote_node_id, Endpoint, NodeId};
use iroh_blobs::util::fs::load_secret_key;
use iroh_mainline_content_discovery::protocol::ALPN;
use iroh_mainline_tracker::{
io::{
Expand All @@ -20,11 +21,6 @@ use iroh_mainline_tracker::{
options::Options,
tracker::Tracker,
};
use iroh_net::{
discovery::pkarr::dht::DhtDiscovery, endpoint::get_remote_node_id, Endpoint, NodeId,
};

use tokio::io::AsyncWriteExt;

use crate::args::Args;

Expand Down Expand Up @@ -66,7 +62,7 @@ async fn await_relay_region(endpoint: &Endpoint) -> anyhow::Result<()> {
}

async fn create_endpoint(
key: iroh_net::key::SecretKey,
key: iroh::key::SecretKey,
ipv4_addr: SocketAddrV4,
publish: bool,
) -> anyhow::Result<Endpoint> {
Expand All @@ -75,7 +71,7 @@ async fn create_endpoint(
} else {
DhtDiscovery::default()
};
iroh_net::Endpoint::builder()
iroh::Endpoint::builder()
.secret_key(key)
.discovery(Box::new(mainline_discovery))
.alpns(vec![ALPN.to_vec()])
Expand Down Expand Up @@ -191,20 +187,18 @@ async fn main() -> anyhow::Result<()> {

/// Returns default server configuration along with its certificate.
#[allow(clippy::field_reassign_with_default)] // https://github.com/rust-lang/rust-clippy/issues/6527
fn configure_server(
secret_key: &iroh_net::key::SecretKey,
) -> anyhow::Result<iroh_quinn::ServerConfig> {
fn configure_server(secret_key: &iroh::key::SecretKey) -> anyhow::Result<iroh_quinn::ServerConfig> {
make_server_config(secret_key, 8, 1024, vec![ALPN.to_vec()])
}

/// Create a [`quinn::ServerConfig`] with the given secret key and limits.
pub fn make_server_config(
secret_key: &iroh_net::key::SecretKey,
secret_key: &iroh::key::SecretKey,
max_streams: u64,
max_connections: u32,
alpn_protocols: Vec<Vec<u8>>,
) -> anyhow::Result<iroh_quinn::ServerConfig> {
let tls_server_config = iroh_net::tls::make_server_config(secret_key, alpn_protocols, false)?;
let tls_server_config = iroh::tls::make_server_config(secret_key, alpn_protocols, false)?;
let mut server_config = iroh_quinn::ServerConfig::with_crypto(Arc::new(tls_server_config));
let mut transport_config = iroh_quinn::TransportConfig::default();
transport_config
Expand All @@ -216,43 +210,3 @@ pub fn make_server_config(
.max_incoming(max_connections as usize);
Ok(server_config)
}

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

// Try to canoncialize 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)
}
}
4 changes: 2 additions & 2 deletions content-discovery/iroh-mainline-tracker/src/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
};

use bao_tree::ChunkNum;
use iroh::{endpoint::get_remote_node_id, Endpoint, NodeId};
use iroh_blobs::{
get::{fsm::EndBlobNext, Stats},
hashseq::HashSeq,
Expand All @@ -19,7 +20,6 @@ use iroh_mainline_content_discovery::{
},
to_infohash,
};
use iroh_net::{endpoint::get_remote_node_id, Endpoint, NodeId};
use rand::Rng;
use redb::{ReadableTable, RedbValue};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -1263,7 +1263,7 @@ pub async fn get_alpn(connecting: &mut iroh_quinn::Connecting) -> anyhow::Result

/// Accept an incoming connection and extract the client-provided [`NodeId`] and ALPN protocol.
async fn iroh_accept_conn(
mut conn: iroh_net::endpoint::Connecting,
mut conn: iroh::endpoint::Connecting,
) -> anyhow::Result<(NodeId, Vec<u8>, iroh_quinn::Connection)> {
let alpn = conn.alpn().await?;
let conn = conn.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl<'a, T> BatchIter<'a, T> {
}
}

impl<'a, T> Iterator for BatchIter<'a, T> {
impl<T> Iterator for BatchIter<'_, T> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
Expand Down
Loading