Skip to content

Commit

Permalink
use impl IntoIterator instead of BTreeSet
Browse files Browse the repository at this point in the history
That results in much nicer usage it seems
  • Loading branch information
flub committed Oct 18, 2024
1 parent eef9c52 commit 88e972a
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 47 deletions.
4 changes: 2 additions & 2 deletions iroh-base/src/node_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ impl NodeAddr {
pub fn from_parts(
node_id: PublicKey,
relay_url: Option<RelayUrl>,
direct_addresses: BTreeSet<SocketAddr>,
direct_addresses: impl IntoIterator<Item = SocketAddr>,
) -> Self {
Self {
node_id,
info: AddrInfo {
relay_url,
direct_addresses,
direct_addresses: direct_addresses.into_iter().collect(),
},
}
}
Expand Down
5 changes: 2 additions & 3 deletions iroh-base/src/ticket/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ impl<'de> Deserialize<'de> for BlobTicket {

#[cfg(test)]
mod tests {
use std::collections::BTreeSet;
use std::net::SocketAddr;

use iroh_test::{assert_eq_hex, hexdump::parse_hexdump};
Expand All @@ -132,7 +131,7 @@ mod tests {
let relay_url = None;
BlobTicket {
hash,
node: NodeAddr::from_parts(peer, relay_url, BTreeSet::from_iter([addr])),
node: NodeAddr::from_parts(peer, relay_url, [addr]),
format: BlobFormat::HashSeq,
}
}
Expand Down Expand Up @@ -163,7 +162,7 @@ mod tests {
.unwrap();

let ticket = BlobTicket {
node: NodeAddr::from_parts(node_id, None, BTreeSet::new()),
node: NodeAddr::from_parts(node_id, None, []),
format: BlobFormat::Raw,
hash,
};
Expand Down
5 changes: 2 additions & 3 deletions iroh-base/src/ticket/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ impl<'de> Deserialize<'de> for NodeTicket {

#[cfg(test)]
mod tests {
use std::collections::BTreeSet;
use std::net::{Ipv4Addr, SocketAddr};

use iroh_test::{assert_eq_hex, hexdump::parse_hexdump};
Expand All @@ -128,7 +127,7 @@ mod tests {
let addr = SocketAddr::from((Ipv4Addr::LOCALHOST, 1234));
let relay_url = None;
NodeTicket {
node: NodeAddr::from_parts(peer, relay_url, BTreeSet::from_iter([addr])),
node: NodeAddr::from_parts(peer, relay_url, [addr]),
}
}

Expand Down Expand Up @@ -158,7 +157,7 @@ mod tests {
node: NodeAddr::from_parts(
node_id,
Some("http://derp.me./".parse().unwrap()),
BTreeSet::from_iter(["127.0.0.1:1024".parse().unwrap()]),
["127.0.0.1:1024".parse().unwrap()],
),
};
let base32 = base32::parse_vec(ticket.to_string().strip_prefix("node").unwrap()).unwrap();
Expand Down
10 changes: 4 additions & 6 deletions iroh-cli/src/commands/blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use iroh::{
net::{key::PublicKey, relay::RelayUrl, NodeAddr},
};
use std::{
collections::{BTreeMap, BTreeSet, HashMap},
collections::{BTreeMap, HashMap},
net::SocketAddr,
path::PathBuf,
time::Duration,
Expand Down Expand Up @@ -189,7 +189,7 @@ impl BlobCommands {
match self {
Self::Get {
ticket,
address,
mut address,
relay_url,
recursive,
override_addresses,
Expand All @@ -208,10 +208,9 @@ impl BlobCommands {
let NodeAddr { node_id, info } = node_addr;
let addresses = if override_addresses {
// use only the cli supplied ones
BTreeSet::from_iter(address)
address
} else {
// use both the cli supplied ones and the ticket ones
let mut address = BTreeSet::from_iter(address);
address.extend(info.direct_addresses);
address
};
Expand Down Expand Up @@ -243,8 +242,7 @@ impl BlobCommands {
bail!("missing NodeId");
};

let node_addr =
NodeAddr::from_parts(node, relay_url, BTreeSet::from_iter(address));
let node_addr = NodeAddr::from_parts(node, relay_url, address);
(node_addr, hash, blob_format)
}
};
Expand Down
6 changes: 3 additions & 3 deletions iroh-cli/src/commands/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use rand::Rng;
use ratatui::{prelude::*, widgets::*};
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeSet, HashMap},
collections::HashMap,
io,
net::SocketAddr,
num::NonZeroU16,
Expand Down Expand Up @@ -698,7 +698,7 @@ async fn make_endpoint(
async fn connect(
node_id: NodeId,
secret_key: SecretKey,
direct_addresses: BTreeSet<SocketAddr>,
direct_addresses: Vec<SocketAddr>,
relay_url: Option<RelayUrl>,
relay_map: Option<RelayMap>,
discovery: Option<Box<dyn Discovery>>,
Expand Down Expand Up @@ -1136,7 +1136,7 @@ pub async fn run(command: Commands, config: &NodeConfig) -> anyhow::Result<()> {
connect(
dial,
secret_key,
BTreeSet::from_iter(remote_endpoint),
remote_endpoint,
relay_url,
relay_map,
discovery,
Expand Down
3 changes: 1 addition & 2 deletions iroh-docs/src/ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ impl std::str::FromStr for DocTicket {

#[cfg(test)]
mod tests {
use std::collections::BTreeSet;
use std::str::FromStr;

use crate::NamespaceId;
Expand All @@ -88,7 +87,7 @@ mod tests {

let ticket = DocTicket {
capability: Capability::Read(namespace_id),
nodes: vec![NodeAddr::from_parts(node_id, None, BTreeSet::new())],
nodes: vec![NodeAddr::from_parts(node_id, None, [])],
};
let base32 = base32::parse_vec(ticket.to_string().strip_prefix("doc").unwrap()).unwrap();
let expected = parse_hexdump("
Expand Down
2 changes: 1 addition & 1 deletion iroh-gossip/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ fn our_peer_data(endpoint: &Endpoint, direct_addresses: &BTreeSet<DirectAddr>) -
let addr = NodeAddr::from_parts(
endpoint.node_id(),
endpoint.home_relay(),
direct_addresses.iter().map(|x| x.addr).collect(),
direct_addresses.iter().map(|x| x.addr),
);
encode_peer_data(&addr.info)
}
Expand Down
8 changes: 2 additions & 6 deletions iroh-net/examples/connect-unreliable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! This example uses the default relay servers to attempt to holepunch, and will use that relay server to relay packets if the two devices cannot establish a direct UDP connection.
//!
//! Run the `listen-unreliable` example first (`iroh-net/examples/listen-unreliable.rs`), which will give you instructions on how to run this example to watch two nodes connect and exchange bytes.
use std::{collections::BTreeSet, net::SocketAddr};
use std::net::SocketAddr;

use anyhow::Context;
use clap::Parser;
Expand Down Expand Up @@ -73,11 +73,7 @@ async fn main() -> anyhow::Result<()> {
.expect("should be connected to a relay server, try calling `endpoint.local_endpoints()` or `endpoint.connect()` first, to ensure the endpoint has actually attempted a connection before checking for the connected relay server");
println!("node relay server url: {relay_url}\n");
// Build a `NodeAddr` from the node_id, relay url, and UDP addresses.
let addr = NodeAddr::from_parts(
args.node_id,
Some(args.relay_url),
BTreeSet::from_iter(args.addrs),
);
let addr = NodeAddr::from_parts(args.node_id, Some(args.relay_url), args.addrs);

// Attempt to connect, over the given ALPN.
// Returns a QUIC connection.
Expand Down
7 changes: 1 addition & 6 deletions iroh-net/examples/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//! This example uses the default relay servers to attempt to holepunch, and will use that relay server to relay packets if the two devices cannot establish a direct UDP connection.
//!
//! Run the `listen` example first (`iroh-net/examples/listen.rs`), which will give you instructions on how to run this example to watch two nodes connect and exchange bytes.
use std::collections::BTreeSet;
use std::net::SocketAddr;

use anyhow::Context;
Expand Down Expand Up @@ -71,11 +70,7 @@ async fn main() -> anyhow::Result<()> {
.expect("should be connected to a relay server, try calling `endpoint.local_endpoints()` or `endpoint.connect()` first, to ensure the endpoint has actually attempted a connection before checking for the connected relay server");
println!("node relay server url: {relay_url}\n");
// Build a `NodeAddr` from the node_id, relay url, and UDP addresses.
let addr = NodeAddr::from_parts(
args.node_id,
Some(args.relay_url),
BTreeSet::from_iter(args.addrs),
);
let addr = NodeAddr::from_parts(args.node_id, Some(args.relay_url), args.addrs);

// Attempt to connect, over the given ALPN.
// Returns a Quinn connection.
Expand Down
7 changes: 5 additions & 2 deletions iroh-net/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,11 @@ impl Endpoint {
.await
.ok_or(anyhow!("No IP endpoints found"))?;
let relay = self.home_relay();
let addrs = addrs.into_iter().map(|x| x.addr).collect();
Ok(NodeAddr::from_parts(self.node_id(), relay, addrs))
Ok(NodeAddr::from_parts(
self.node_id(),
relay,
addrs.into_iter().map(|x| x.addr),
))
}

/// Returns the [`RelayUrl`] of the Relay server used as home relay.
Expand Down
3 changes: 1 addition & 2 deletions iroh/src/client/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use super::{flatten, RpcClient};
///
/// # Examples
/// ```
/// use std::collections::BTreeSet;
/// use std::str::FromStr;
/// use iroh_base::{key::NodeId, node_addr::{RelayUrl, NodeAddr}};
/// use url::Url;
Expand All @@ -57,7 +56,7 @@ use super::{flatten, RpcClient};
/// // the home relay
/// Some(relay_url),
/// // the direct addresses
/// BTreeSet::from_iter(["120.0.0.1:0".parse().unwrap()]),
/// ["120.0.0.1:0".parse().unwrap()],
/// );
/// net_client.add_node_addr(addr).await?;
/// // Shut down the node. Passing `true` will force the shutdown, passing in
Expand Down
25 changes: 14 additions & 11 deletions iroh/tests/provide.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{BTreeMap, BTreeSet},
collections::BTreeMap,
net::SocketAddr,
ops::Range,
time::{Duration, Instant},
Expand Down Expand Up @@ -120,7 +120,10 @@ async fn empty_files() -> Result<()> {

/// Create new get options with the given node id and addresses, using a
/// randomly generated secret key.
fn get_options(node_id: NodeId, addrs: BTreeSet<SocketAddr>) -> (SecretKey, NodeAddr) {
fn get_options(
node_id: NodeId,
addrs: impl IntoIterator<Item = SocketAddr>,
) -> (SecretKey, NodeAddr) {
let relay_map = default_relay_map();
let peer = iroh_net::NodeAddr::from_parts(
node_id,
Expand Down Expand Up @@ -150,7 +153,7 @@ async fn multiple_clients() -> Result<()> {

tasks.push(node.local_pool_handle().spawn(move || {
async move {
let (secret_key, peer) = get_options(peer_id, BTreeSet::from_iter(addrs));
let (secret_key, peer) = get_options(peer_id, addrs);
let expected_data = &content;
let expected_name = name;
let request = GetRequest::all(hash);
Expand Down Expand Up @@ -218,7 +221,7 @@ where
let node = test_node(mdb.clone()).spawn().await?;

let addrs = node.local_endpoint_addresses().await?;
let (secret_key, peer) = get_options(node.node_id(), BTreeSet::from_iter(addrs));
let (secret_key, peer) = get_options(node.node_id(), addrs);
let request = GetRequest::all(collection_hash);
let (collection, children, _stats) =
run_collection_get_request(secret_key, peer, request).await?;
Expand Down Expand Up @@ -250,7 +253,7 @@ async fn test_server_close() {
let node_addr = node.local_endpoint_addresses().await.unwrap();
let peer_id = node.node_id();

let (secret_key, peer) = get_options(peer_id, BTreeSet::from_iter(node_addr));
let (secret_key, peer) = get_options(peer_id, node_addr);
let request = GetRequest::all(hash);
let (_collection, _children, _stats) = run_collection_get_request(secret_key, peer, request)
.await
Expand Down Expand Up @@ -286,7 +289,7 @@ async fn test_ipv6() {
let addrs = node.local_endpoint_addresses().await.unwrap();
let peer_id = node.node_id();
tokio::time::timeout(Duration::from_secs(10), async move {
let (secret_key, peer) = get_options(peer_id, BTreeSet::from_iter(addrs));
let (secret_key, peer) = get_options(peer_id, addrs);
let request = GetRequest::all(hash);
run_collection_get_request(secret_key, peer, request).await
})
Expand All @@ -313,7 +316,7 @@ async fn test_not_found() {
let addrs = node.local_endpoint_addresses().await.unwrap();
let peer_id = node.node_id();
tokio::time::timeout(Duration::from_secs(10), async move {
let (secret_key, peer) = get_options(peer_id, BTreeSet::from_iter(addrs));
let (secret_key, peer) = get_options(peer_id, addrs);
let request = GetRequest::single(hash);
let res = run_collection_get_request(secret_key, peer, request).await;
if let Err(cause) = res {
Expand Down Expand Up @@ -355,7 +358,7 @@ async fn test_chunk_not_found_1() {
let addrs = node.local_endpoint_addresses().await.unwrap();
let peer_id = node.node_id();
tokio::time::timeout(Duration::from_secs(10), async move {
let (secret_key, peer) = get_options(peer_id, BTreeSet::from_iter(addrs));
let (secret_key, peer) = get_options(peer_id, addrs);
let request = GetRequest::single(hash);
let res = run_collection_get_request(secret_key, peer, request).await;
if let Err(cause) = res {
Expand Down Expand Up @@ -438,7 +441,7 @@ async fn test_run_fsm() {
let addrs = node.local_endpoint_addresses().await.unwrap();
let peer_id = node.node_id();
tokio::time::timeout(Duration::from_secs(10), async move {
let (secret_key, peer) = get_options(peer_id, BTreeSet::from_iter(addrs));
let (secret_key, peer) = get_options(peer_id, addrs);
let request = GetRequest::all(hash);
let (collection, children, _) =
run_collection_get_request(secret_key, peer, request).await?;
Expand Down Expand Up @@ -490,7 +493,7 @@ async fn test_size_request_blob() {
let peer_id = node.node_id();
tokio::time::timeout(Duration::from_secs(10), async move {
let request = GetRequest::last_chunk(hash);
let (secret_key, peer) = get_options(peer_id, BTreeSet::from_iter(addrs));
let (secret_key, peer) = get_options(peer_id, addrs);
let connection = dial(secret_key, peer).await?;
let response = fsm::start(connection, request);
let connected = response.next().await?;
Expand Down Expand Up @@ -528,7 +531,7 @@ async fn test_collection_stat() {
hash,
RangeSpecSeq::from_ranges_infinite([ChunkRanges::all(), ranges]),
);
let (secret_key, peer) = get_options(peer_id, BTreeSet::from_iter(addrs));
let (secret_key, peer) = get_options(peer_id, addrs);
let (_collection, items, _stats) =
run_collection_get_request(secret_key, peer, request).await?;
// we should get the first <=1024 bytes and the last chunk of each child
Expand Down

0 comments on commit 88e972a

Please sign in to comment.