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

feat!: update to iroh@0.30.0 #41

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
229 changes: 121 additions & 108 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ serde = { version = "1", features = ["derive"] }
serde-error = "0.1.3"
smallvec = { version = "1.10.0", features = ["serde", "const_new"] }
strum = { version = "0.26.3", optional = true }
ssh-key = { version = "0.6", optional = true, features = ["ed25519"] }
tempfile = { version = "3.10.0", optional = true }
thiserror = "2"
tokio = { version = "1", features = ["fs"] }
Expand Down Expand Up @@ -103,14 +104,15 @@ metrics = ["iroh-metrics/metrics"]
redb = ["dep:redb"]
cli = ["rpc", "dep:clap", "dep:indicatif", "dep:console"]
rpc = [
"dep:quic-rpc",
"dep:quic-rpc-derive",
"dep:nested_enum_utils",
"dep:strum",
"dep:futures-util",
"dep:portable-atomic",
"dep:walkdir",
"downloader",
"dep:quic-rpc",
"dep:quic-rpc-derive",
"dep:nested_enum_utils",
"dep:strum",
"dep:futures-util",
"dep:portable-atomic",
"dep:walkdir",
"dep:ssh-key",
"downloader",
]

example-iroh = [
Expand Down
2 changes: 2 additions & 0 deletions examples/hello-world-fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ async fn main() -> Result<()> {
"node relay server url: {:?}",
node.endpoint()
.home_relay()
.initialized()
.await
.expect("a default relay url should be provided")
.to_string()
dignifiedquire marked this conversation as resolved.
Show resolved Hide resolved
);
Expand Down
2 changes: 1 addition & 1 deletion examples/local-swarm-discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn main() -> anyhow::Result<()> {
setup_logging();
let cli = Cli::parse();

let key = SecretKey::generate();
let key = SecretKey::generate(rand::rngs::OsRng);
let discovery = LocalSwarmDiscovery::new(key.public())?;

println!("Starting iroh node with local node discovery...");
Expand Down
30 changes: 16 additions & 14 deletions src/downloader/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async fn smoke_test() {
Downloader::spawn_for_test(dialer.clone(), getter.clone(), concurrency_limits);

// send a request and make sure the peer is requested the corresponding download
let peer = SecretKey::generate().public();
let peer = SecretKey::generate(rand::thread_rng()).public();
let kind: DownloadKind = HashAndFormat::raw(Hash::new([0u8; 32])).into();
let req = DownloadRequest::new(kind, vec![peer]);
let handle = downloader.queue(req).await;
Expand All @@ -101,7 +101,7 @@ async fn deduplication() {
let (downloader, _lp) =
Downloader::spawn_for_test(dialer.clone(), getter.clone(), concurrency_limits);

let peer = SecretKey::generate().public();
let peer = SecretKey::generate(rand::thread_rng()).public();
let kind: DownloadKind = HashAndFormat::raw(Hash::new([0u8; 32])).into();
let mut handles = Vec::with_capacity(10);
for _ in 0..10 {
Expand Down Expand Up @@ -134,7 +134,7 @@ async fn cancellation() {
let (downloader, _lp) =
Downloader::spawn_for_test(dialer.clone(), getter.clone(), concurrency_limits);

let peer = SecretKey::generate().public();
let peer = SecretKey::generate(rand::thread_rng()).public();
let kind_1: DownloadKind = HashAndFormat::raw(Hash::new([0u8; 32])).into();
let req = DownloadRequest::new(kind_1, vec![peer]);
let handle_a = downloader.queue(req.clone()).await;
Expand Down Expand Up @@ -175,7 +175,7 @@ async fn max_concurrent_requests_total() {
Downloader::spawn_for_test(dialer.clone(), getter.clone(), concurrency_limits);

// send the downloads
let peer = SecretKey::generate().public();
let peer = SecretKey::generate(rand::thread_rng()).public();
let mut handles = Vec::with_capacity(5);
let mut expected_history = Vec::with_capacity(5);
for i in 0..5 {
Expand Down Expand Up @@ -219,7 +219,7 @@ async fn max_concurrent_requests_per_peer() {
Downloader::spawn_for_test(dialer.clone(), getter.clone(), concurrency_limits);

// send the downloads
let peer = SecretKey::generate().public();
let peer = SecretKey::generate(rand::thread_rng()).public();
let mut handles = Vec::with_capacity(5);
for i in 0..5 {
let kind = HashAndFormat::raw(Hash::new([i; 32]));
Expand Down Expand Up @@ -275,7 +275,7 @@ async fn concurrent_progress() {
let (downloader, _lp) =
Downloader::spawn_for_test(dialer.clone(), getter.clone(), Default::default());

let peer = SecretKey::generate().public();
let peer = SecretKey::generate(rand::thread_rng()).public();
let hash = Hash::new([0u8; 32]);
let kind_1 = HashAndFormat::raw(hash);

Expand Down Expand Up @@ -369,11 +369,12 @@ async fn long_queue() {

let (downloader, _lp) =
Downloader::spawn_for_test(dialer.clone(), getter.clone(), concurrency_limits);
let mut rng = rand::thread_rng();
// send the downloads
let nodes = [
SecretKey::generate().public(),
SecretKey::generate().public(),
SecretKey::generate().public(),
SecretKey::generate(&mut rng).public(),
SecretKey::generate(&mut rng).public(),
SecretKey::generate(&mut rng).public(),
];
let mut handles = vec![];
for i in 0..100usize {
Expand Down Expand Up @@ -417,7 +418,7 @@ async fn fail_while_running() {
.boxed()
}));

let node = SecretKey::generate().public();
let node = SecretKey::generate(rand::thread_rng()).public();
let req_success = DownloadRequest::new(blob_success, vec![node]);
let req_fail = DownloadRequest::new(blob_fail, vec![node]);
let handle_success = downloader.queue(req_success).await;
Expand All @@ -437,7 +438,7 @@ async fn retry_nodes_simple() {
let getter = getter::TestingGetter::default();
let (downloader, _lp) =
Downloader::spawn_for_test(dialer.clone(), getter.clone(), Default::default());
let node = SecretKey::generate().public();
let node = SecretKey::generate(rand::thread_rng()).public();
let dial_attempts = Arc::new(AtomicUsize::new(0));
let dial_attempts2 = dial_attempts.clone();
// fail on first dial, then succeed
Expand Down Expand Up @@ -467,7 +468,7 @@ async fn retry_nodes_fail() {
Default::default(),
config,
);
let node = SecretKey::generate().public();
let node = SecretKey::generate(rand::thread_rng()).public();
// fail always
dialer.set_dial_outcome(move |_node| false);

Expand Down Expand Up @@ -504,8 +505,9 @@ async fn retry_nodes_jump_queue() {
let (downloader, _lp) =
Downloader::spawn_for_test(dialer.clone(), getter.clone(), concurrency_limits);

let good_node = SecretKey::generate().public();
let bad_node = SecretKey::generate().public();
let mut rng = rand::thread_rng();
let good_node = SecretKey::generate(&mut rng).public();
let bad_node = SecretKey::generate(&mut rng).public();

dialer.set_dial_outcome(move |node| node == good_node);
let kind1 = HashAndFormat::raw(Hash::new([0u8; 32]));
Expand Down
31 changes: 27 additions & 4 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::{borrow::Borrow, fmt, str::FromStr};

use iroh_base::base32::{self, parse_array_hex_or_base32, HexOrBase32ParseError};
use postcard::experimental::max_size::MaxSize;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

Expand Down Expand Up @@ -52,10 +51,10 @@ impl Hash {
self.0.to_hex().to_string()
}

/// Convert to a base32 string limited to the first 10 bytes for a friendly string
/// Convert to a hex string limited to the first 5bytes for a friendly string
/// representation of the hash.
pub fn fmt_short(&self) -> String {
base32::fmt_short(self.as_bytes())
data_encoding::HEXLOWER.encode(&self.as_bytes()[..5])
}
}

Expand Down Expand Up @@ -134,11 +133,35 @@ impl fmt::Display for Hash {
}
}

#[derive(Debug, thiserror::Error)]
pub enum HexOrBase32ParseError {
#[error("Invalid length")]
DecodeInvalidLength,
#[error("Failed to decode {0}")]
Decode(#[from] data_encoding::DecodeError),
}

impl FromStr for Hash {
type Err = HexOrBase32ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
parse_array_hex_or_base32(s).map(Hash::from)
let mut bytes = [0u8; 32];

let res = if s.len() == 64 {
// hex
data_encoding::HEXLOWER.decode_mut(s.as_bytes(), &mut bytes)
} else {
data_encoding::BASE32_NOPAD.decode_mut(s.to_ascii_uppercase().as_bytes(), &mut bytes)
};
match res {
Ok(len) => {
if len != 32 {
return Err(HexOrBase32ParseError::DecodeInvalidLength);
}
}
Err(partial) => return Err(partial.error.into()),
}
Ok(Self(blake3::Hash::from_bytes(bytes)))
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/rpc/client/blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1903,15 +1903,17 @@ mod tests {
let (relay_map, _relay_url, _guard) = iroh::test_utils::run_relay_server().await?;
let dns_pkarr_server = DnsPkarrServer::run().await?;

let secret1 = SecretKey::generate();
let mut rng = rand::thread_rng();

let secret1 = SecretKey::generate(&mut rng);
let endpoint1 = iroh::Endpoint::builder()
.relay_mode(RelayMode::Custom(relay_map.clone()))
.insecure_skip_relay_cert_verify(true)
.dns_resolver(dns_pkarr_server.dns_resolver())
.secret_key(secret1.clone())
.discovery(dns_pkarr_server.discovery(secret1));
let node1 = Node::memory().endpoint(endpoint1).spawn().await?;
let secret2 = SecretKey::generate();
let secret2 = SecretKey::generate(&mut rng);
let endpoint2 = iroh::Endpoint::builder()
.relay_mode(RelayMode::Custom(relay_map.clone()))
.insecure_skip_relay_cert_verify(true)
Expand Down
9 changes: 6 additions & 3 deletions src/ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,13 @@ mod tests {
use std::net::SocketAddr;

use iroh::{PublicKey, SecretKey};
use iroh_base::base32;
use iroh_test::{assert_eq_hex, hexdump::parse_hexdump};

use super::*;

fn make_ticket() -> BlobTicket {
let hash = Hash::new(b"hi there");
let peer = SecretKey::generate().public();
let peer = SecretKey::generate(rand::thread_rng()).public();
let addr = SocketAddr::from_str("127.0.0.1:1234").unwrap();
let relay_url = None;
BlobTicket {
Expand Down Expand Up @@ -201,7 +200,11 @@ mod tests {
format: BlobFormat::Raw,
hash,
};
let base32 = base32::parse_vec(ticket.to_string().strip_prefix("blob").unwrap()).unwrap();
let encoded = ticket.to_string();
let stripped = encoded.strip_prefix("blob").unwrap();
let base32 = data_encoding::BASE32_NOPAD
.decode(stripped.to_ascii_uppercase().as_bytes())
.unwrap();
let expected = parse_hexdump("
00 # discriminator for variant 0
ae58ff8833241ac82d6ff7611046ed67b5072d142c588d0063e942d9a75502b6 # node id, 32 bytes, see above
Expand Down
16 changes: 13 additions & 3 deletions src/util/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,21 @@ pub async fn load_secret_key(key_path: PathBuf) -> anyhow::Result<iroh::SecretKe

if key_path.exists() {
let keystr = tokio::fs::read(key_path).await?;
let secret_key = SecretKey::try_from_openssh(keystr).context("invalid keyfile")?;

let ser_key = ssh_key::private::PrivateKey::from_openssh(keystr)?;
let ssh_key::private::KeypairData::Ed25519(kp) = ser_key.key_data() else {
bail!("invalid key format");
};
let secret_key = SecretKey::from_bytes(&kp.private.to_bytes());
Ok(secret_key)
} else {
let secret_key = SecretKey::generate();
let ser_key = secret_key.to_openssh()?;
let secret_key = SecretKey::generate(rand::rngs::OsRng);
let ckey = ssh_key::private::Ed25519Keypair {
public: secret_key.public().public().into(),
private: secret_key.secret().into(),
};
let ser_key =
ssh_key::private::PrivateKey::from(ckey).to_openssh(ssh_key::LineEnding::default())?;

// Try to canonicalize if possible
let key_path = key_path.canonicalize().unwrap_or(key_path);
Expand Down
Loading