Skip to content

Commit 7592271

Browse files
committed
Rename node id and node addr to endpoint id and endpoint addr everywhere
Also use latest way to configure n0 discovery Test failing: connection_pool_errors
1 parent f02a041 commit 7592271

File tree

15 files changed

+207
-202
lines changed

15 files changed

+207
-202
lines changed

examples/compression.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{fmt::Debug, path::PathBuf};
88
use anyhow::Result;
99
use clap::Parser;
1010
use common::setup_logging;
11-
use iroh::protocol::ProtocolHandler;
11+
use iroh::{endpoint::presets, protocol::ProtocolHandler};
1212
use iroh_blobs::{
1313
api::Store,
1414
get::StreamPair,
@@ -27,7 +27,7 @@ use crate::common::get_or_generate_secret_key;
2727
#[derive(Debug, Parser)]
2828
#[command(version, about)]
2929
pub enum Args {
30-
/// Limit requests by node id
30+
/// Limit requests by endpoint id
3131
Provide {
3232
/// Path for files to add.
3333
path: PathBuf,
@@ -160,7 +160,7 @@ impl<C: Compression> ProtocolHandler for CompressedBlobsProtocol<C> {
160160
.events
161161
.client_connected(|| ClientConnected {
162162
connection_id,
163-
node_id: connection.remote_node_id().ok(),
163+
endpoint_id: connection.remote_id().ok(),
164164
})
165165
.await
166166
{
@@ -186,7 +186,7 @@ async fn main() -> Result<()> {
186186
let secret = get_or_generate_secret_key()?;
187187
let endpoint = iroh::Endpoint::builder()
188188
.secret_key(secret)
189-
.discovery_n0()
189+
.preset(presets::N0)
190190
.bind()
191191
.await?;
192192
let compression = lz4::Compression;
@@ -198,7 +198,7 @@ async fn main() -> Result<()> {
198198
let router = iroh::protocol::Router::builder(endpoint.clone())
199199
.accept(lz4::Compression::ALPN, blobs)
200200
.spawn();
201-
let ticket = BlobTicket::new(endpoint.node_id().into(), tag.hash, tag.format);
201+
let ticket = BlobTicket::new(endpoint.id().into(), tag.hash, tag.format);
202202
println!("Serving blob with hash {}", tag.hash);
203203
println!("Ticket: {ticket}");
204204
println!("Node is running. Press Ctrl-C to exit.");
@@ -209,7 +209,7 @@ async fn main() -> Result<()> {
209209
Args::Get { ticket, target } => {
210210
let store = MemStore::new();
211211
let conn = endpoint
212-
.connect(ticket.node_addr().clone(), lz4::Compression::ALPN)
212+
.connect(ticket.addr().clone(), lz4::Compression::ALPN)
213213
.await?;
214214
let connection_id = conn.stable_id() as u64;
215215
let (send, recv) = conn.open_bi().await?;

examples/custom-protocol.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
//!
1818
//! cargo run --example custom-protocol -- listen "hello-world" "foo-bar" "hello-moon"
1919
//!
20-
//! This spawns an iroh nodes with three blobs. It will print the node's node id.
20+
//! This spawns an iroh node with three blobs. It will print the node's endpoint id.
2121
//!
2222
//! In another terminal, run
2323
//!
24-
//! cargo run --example custom-protocol -- query <node-id> hello
24+
//! cargo run --example custom-protocol -- query <endpoint-id> hello
2525
//!
26-
//! Replace <node-id> with the node id from above. This will connect to the listening node with our
26+
//! Replace <endpoint-id> with the endpoint id from above. This will connect to the listening node with our
2727
//! custom protocol and query for the string `hello`. The listening node will return a list of
2828
//! blob hashes that contain `hello`. We will then download all these blobs with iroh-blobs,
2929
//! and then print a list of the hashes with their content.
@@ -44,9 +44,9 @@ use anyhow::Result;
4444
use clap::Parser;
4545
use iroh::{
4646
discovery::pkarr::PkarrResolver,
47-
endpoint::Connection,
47+
endpoint::{presets, Connection},
4848
protocol::{AcceptError, ProtocolHandler, Router},
49-
Endpoint, NodeId,
49+
Endpoint, EndpointId,
5050
};
5151
use iroh_blobs::{api::Store, store::mem::MemStore, BlobsProtocol, Hash};
5252
mod common;
@@ -67,8 +67,8 @@ pub enum Command {
6767
},
6868
/// Query a remote node for data and print the results.
6969
Query {
70-
/// The node id of the node we want to query.
71-
node_id: NodeId,
70+
/// The endpoint id of the node we want to query.
71+
endpoint_id: EndpointId,
7272
/// The text we want to match.
7373
query: String,
7474
},
@@ -81,15 +81,15 @@ pub enum Command {
8181
const ALPN: &[u8] = b"iroh-example/text-search/0";
8282

8383
async fn listen(text: Vec<String>) -> Result<()> {
84-
// allow the user to provide a secret so we can have a stable node id.
84+
// allow the user to provide a secret so we can have a stable endpoint id.
8585
// This is only needed for the listen side.
8686
let secret_key = get_or_generate_secret_key()?;
8787
// Use an in-memory store for this example. You would use a persistent store in production code.
8888
let store = MemStore::new();
8989
// Create an endpoint with the secret key and discovery publishing to the n0 dns server enabled.
9090
let endpoint = Endpoint::builder()
9191
.secret_key(secret_key)
92-
.discovery_n0()
92+
.preset(presets::N0)
9393
.bind()
9494
.await?;
9595
// Build our custom protocol handler. The `builder` exposes access to various subsystems in the
@@ -108,30 +108,27 @@ async fn listen(text: Vec<String>) -> Result<()> {
108108
.accept(iroh_blobs::ALPN, blobs.clone())
109109
.spawn();
110110

111-
// Print our node id, so clients know how to connect to us.
112-
let node_id = node.endpoint().node_id();
113-
println!("our node id: {node_id}");
111+
// Print our endpoint id, so clients know how to connect to us.
112+
let node_id = node.endpoint().id();
113+
println!("our endpoint id: {node_id}");
114114

115115
// Wait for Ctrl-C to be pressed.
116116
tokio::signal::ctrl_c().await?;
117117
node.shutdown().await?;
118118
Ok(())
119119
}
120120

121-
async fn query(node_id: NodeId, query: String) -> Result<()> {
121+
async fn query(endpoint_id: EndpointId, query: String) -> Result<()> {
122122
// Build a in-memory node. For production code, you'd want a persistent node instead usually.
123123
let store = MemStore::new();
124124
// Create an endpoint with a random secret key and no discovery publishing.
125125
// For a client we just need discovery resolution via the n0 dns server, which
126126
// the PkarrResolver provides.
127-
let endpoint = Endpoint::builder()
128-
.add_discovery(PkarrResolver::n0_dns())
129-
.bind()
130-
.await?;
127+
let endpoint = Endpoint::builder().preset(presets::N0).bind().await?;
131128
// Query the remote node.
132129
// This will send the query over our custom protocol, read hashes on the reply stream,
133130
// and download each hash over iroh-blobs.
134-
let hashes = query_remote(&endpoint, &store, node_id, &query).await?;
131+
let hashes = query_remote(&endpoint, &store, endpoint_id, &query).await?;
135132

136133
// Print out our query results.
137134
for hash in hashes {
@@ -157,10 +154,10 @@ async fn main() -> Result<()> {
157154
listen(text).await?;
158155
}
159156
Command::Query {
160-
node_id,
157+
endpoint_id,
161158
query: query_text,
162159
} => {
163-
query(node_id, query_text).await?;
160+
query(endpoint_id, query_text).await?;
164161
}
165162
}
166163

@@ -180,8 +177,8 @@ impl ProtocolHandler for BlobSearch {
180177
/// the connection lasts.
181178
async fn accept(&self, connection: Connection) -> std::result::Result<(), AcceptError> {
182179
let this = self.clone();
183-
// We can get the remote's node id from the connection.
184-
let node_id = connection.remote_node_id()?;
180+
// We can get the remote's endpoint id from the connection.
181+
let node_id = connection.remote_id()?;
185182
println!("accepted connection from {node_id}");
186183

187184
// Our protocol is a simple request-response protocol, so we expect the
@@ -269,14 +266,14 @@ impl BlobSearch {
269266
pub async fn query_remote(
270267
endpoint: &Endpoint,
271268
store: &Store,
272-
node_id: NodeId,
269+
endpoint_id: EndpointId,
273270
query: &str,
274271
) -> Result<Vec<Hash>> {
275272
// Establish a connection to our node.
276-
// We use the default node discovery in iroh, so we can connect by node id without
273+
// We use the default node discovery in iroh, so we can connect by endpoint id without
277274
// providing further information.
278-
let conn = endpoint.connect(node_id, ALPN).await?;
279-
let blobs_conn = endpoint.connect(node_id, iroh_blobs::ALPN).await?;
275+
let conn = endpoint.connect(endpoint_id, ALPN).await?;
276+
let blobs_conn = endpoint.connect(endpoint_id, iroh_blobs::ALPN).await?;
280277

281278
// Open a bi-directional in our connection.
282279
let (mut send, mut recv) = conn.open_bi().await?;

examples/get-blob.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async fn main() -> anyhow::Result<()> {
3838
"This example only supports raw blobs."
3939
);
4040
let connection = endpoint
41-
.connect(ticket.node_addr().node_id, iroh_blobs::ALPN)
41+
.connect(ticket.addr().endpoint_id, iroh_blobs::ALPN)
4242
.await?;
4343
let mut progress = iroh_blobs::get::request::get_blob(connection, ticket.hash());
4444
let stats = if cli.progress {

examples/limit.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Example how to limit blob requests by hash and node id, and to add
1+
/// Example how to limit blob requests by hash and endpoint id, and to add
22
/// throttling or limiting the maximum number of connections.
33
///
44
/// Limiting is done via a fn that returns an EventSender and internally
@@ -21,7 +21,7 @@ use std::{
2121
use anyhow::Result;
2222
use clap::Parser;
2323
use common::setup_logging;
24-
use iroh::{protocol::Router, NodeAddr, NodeId, SecretKey};
24+
use iroh::{endpoint::presets, protocol::Router, EndpointAddr, EndpointId, SecretKey};
2525
use iroh_blobs::{
2626
provider::events::{
2727
AbortReason, ConnectMode, EventMask, EventSender, ProviderMessage, RequestMode,
@@ -38,14 +38,14 @@ use crate::common::get_or_generate_secret_key;
3838
#[derive(Debug, Parser)]
3939
#[command(version, about)]
4040
pub enum Args {
41-
/// Limit requests by node id
42-
ByNodeId {
41+
/// Limit requests by endpoint id
42+
ByEndpointId {
4343
/// Path for files to add.
4444
paths: Vec<PathBuf>,
4545
#[clap(long("allow"))]
46-
/// Nodes that are allowed to download content.
47-
allowed_nodes: Vec<NodeId>,
48-
/// Number of secrets to generate for allowed node ids.
46+
/// Endpoints that are allowed to download content.
47+
allowed_endpoints: Vec<EndpointId>,
48+
/// Number of secrets to generate for allowed endpoint ids.
4949
#[clap(long, default_value_t = 1)]
5050
secrets: usize,
5151
},
@@ -77,7 +77,7 @@ pub enum Args {
7777
},
7878
}
7979

80-
fn limit_by_node_id(allowed_nodes: HashSet<NodeId>) -> EventSender {
80+
fn limit_by_node_id(allowed_nodes: HashSet<EndpointId>) -> EventSender {
8181
let mask = EventMask {
8282
// We want a request for each incoming connection so we can accept
8383
// or reject them. We don't need any other events.
@@ -88,17 +88,17 @@ fn limit_by_node_id(allowed_nodes: HashSet<NodeId>) -> EventSender {
8888
n0_future::task::spawn(async move {
8989
while let Some(msg) = rx.recv().await {
9090
if let ProviderMessage::ClientConnected(msg) = msg {
91-
let res = match msg.node_id {
92-
Some(node_id) if allowed_nodes.contains(&node_id) => {
93-
println!("Client connected: {node_id}");
91+
let res: std::result::Result<(), AbortReason> = match msg.endpoint_id {
92+
Some(endpoint_id) if allowed_nodes.contains(&endpoint_id) => {
93+
println!("Client connected: {endpoint_id}");
9494
Ok(())
9595
}
96-
Some(node_id) => {
97-
println!("Client rejected: {node_id}");
96+
Some(endpoint_id) => {
97+
println!("Client rejected: {endpoint_id}");
9898
Err(AbortReason::Permission)
9999
}
100100
None => {
101-
println!("Client rejected: no node id");
101+
println!("Client rejected: no endpoint id");
102102
Err(AbortReason::Permission)
103103
}
104104
};
@@ -206,7 +206,7 @@ fn limit_max_connections(max_connections: usize) -> EventSender {
206206
match msg {
207207
ProviderMessage::ClientConnected(msg) => {
208208
let connection_id = msg.connection_id;
209-
let node_id = msg.node_id;
209+
let node_id = msg.endpoint_id;
210210
let res = if let Ok(n) = requests.inc() {
211211
println!("Accepting connection {n}, node_id {node_id:?}, connection_id {connection_id}");
212212
Ok(())
@@ -233,50 +233,50 @@ async fn main() -> Result<()> {
233233
let secret = get_or_generate_secret_key()?;
234234
let endpoint = iroh::Endpoint::builder()
235235
.secret_key(secret)
236-
.discovery_n0()
236+
.preset(presets::N0)
237237
.bind()
238238
.await?;
239239
match args {
240240
Args::Get { ticket } => {
241241
let connection = endpoint
242-
.connect(ticket.node_addr().clone(), iroh_blobs::ALPN)
242+
.connect(ticket.addr().clone(), iroh_blobs::ALPN)
243243
.await?;
244244
let (data, stats) = iroh_blobs::get::request::get_blob(connection, ticket.hash())
245245
.bytes_and_stats()
246246
.await?;
247247
println!("Downloaded {} bytes", data.len());
248248
println!("Stats: {stats:?}");
249249
}
250-
Args::ByNodeId {
250+
Args::ByEndpointId {
251251
paths,
252-
allowed_nodes,
252+
allowed_endpoints,
253253
secrets,
254254
} => {
255-
let mut allowed_nodes = allowed_nodes.into_iter().collect::<HashSet<_>>();
255+
let mut allowed_endpoints = allowed_endpoints.into_iter().collect::<HashSet<_>>();
256256
if secrets > 0 {
257-
println!("Generating {secrets} new secret keys for allowed nodes:");
257+
println!("Generating {secrets} new secret keys for allowed endpoints:");
258258
let mut rand = rng();
259259
for _ in 0..secrets {
260260
let secret = SecretKey::generate(&mut rand);
261261
let public = secret.public();
262-
allowed_nodes.insert(public);
262+
allowed_endpoints.insert(public);
263263
println!("IROH_SECRET={}", hex::encode(secret.to_bytes()));
264264
}
265265
}
266266

267267
let store = MemStore::new();
268268
let hashes = add_paths(&store, paths).await?;
269-
let events = limit_by_node_id(allowed_nodes.clone());
269+
let events = limit_by_node_id(allowed_endpoints.clone());
270270
let (router, addr) = setup(store, events).await?;
271271

272272
for (path, hash) in hashes {
273273
let ticket = BlobTicket::new(addr.clone(), hash, BlobFormat::Raw);
274274
println!("{}: {ticket}", path.display());
275275
}
276276
println!();
277-
println!("Node id: {}\n", router.endpoint().node_id());
278-
for id in &allowed_nodes {
279-
println!("Allowed node: {id}");
277+
println!("Endpoint id: {}\n", router.endpoint().id());
278+
for id in &allowed_endpoints {
279+
println!("Allowed endpoint: {id}");
280280
}
281281

282282
tokio::signal::ctrl_c().await?;
@@ -350,15 +350,15 @@ async fn add_paths(store: &MemStore, paths: Vec<PathBuf>) -> Result<HashMap<Path
350350
Ok(hashes)
351351
}
352352

353-
async fn setup(store: MemStore, events: EventSender) -> Result<(Router, NodeAddr)> {
353+
async fn setup(store: MemStore, events: EventSender) -> Result<(Router, EndpointAddr)> {
354354
let secret = get_or_generate_secret_key()?;
355355
let endpoint = iroh::Endpoint::builder()
356-
.discovery_n0()
356+
.preset(presets::N0)
357357
.secret_key(secret)
358358
.bind()
359359
.await?;
360360
endpoint.online().await;
361-
let addr = endpoint.node_addr();
361+
let addr = endpoint.addr();
362362
let blobs = BlobsProtocol::new(&store, Some(events));
363363
let router = Router::builder(endpoint)
364364
.accept(iroh_blobs::ALPN, blobs)

0 commit comments

Comments
 (0)