From 25c830574d54652a772cffd7d29e3fb386d37c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Thu, 26 Sep 2024 22:07:58 +0200 Subject: [PATCH] fix(examples): Make `collection-provide`, `hello-world-provide` and `rpc` work again (#2749) ## Description They were broken, because - we changed `.share` to default to a node-id-only ticket and - `node.shutdown().await` doesn't wait for Ctrl+C anymore, instead it immediately shuts down the node. ## Breaking Changes None ## Notes & open questions I basically restored the examples original behavior. It's an open question whether we want to simplify the examples by making the tickets node-id-only. Also: Testing. Not sure if we can/should. Ideas welcome! (To prevent this from happening again.) ## Change checklist - [x] Self-review. - ~~[ ] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant.~~ - ~~[ ] Tests if relevant.~~ - [x] All breaking changes documented. --------- Co-authored-by: Diva M --- iroh/examples/collection-provide.rs | 11 ++++++++--- iroh/examples/hello-world-provide.rs | 7 ++++--- iroh/examples/rpc.rs | 12 ++++++++---- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/iroh/examples/collection-provide.rs b/iroh/examples/collection-provide.rs index 867b2ac5e3..429288a2f2 100644 --- a/iroh/examples/collection-provide.rs +++ b/iroh/examples/collection-provide.rs @@ -7,6 +7,7 @@ //! run this example from the project root: //! $ cargo run --example collection-provide use iroh::blobs::{format::collection::Collection, util::SetTagOption, BlobFormat}; +use iroh_base::node_addr::AddrInfoOptions; use tracing_subscriber::{prelude::*, EnvFilter}; // set the RUST_LOG env var to one of {debug,info,warn} to see logging info @@ -45,7 +46,11 @@ async fn main() -> anyhow::Result<()> { // tickets wrap all details needed to get a collection let ticket = node .blobs() - .share(hash, BlobFormat::HashSeq, Default::default()) + .share( + hash, + BlobFormat::HashSeq, + AddrInfoOptions::RelayAndAddresses, + ) .await?; // print some info about the node @@ -66,8 +71,8 @@ async fn main() -> anyhow::Result<()> { // print the ticket, containing all the above information println!("\nin another terminal, run:"); println!("\tcargo run --example collection-fetch {}", ticket); - // wait for the node to finish, this will block indefinitely - // stop with SIGINT (ctrl+c) + // block until SIGINT is received (ctrl+c) + tokio::signal::ctrl_c().await?; node.shutdown().await?; Ok(()) } diff --git a/iroh/examples/hello-world-provide.rs b/iroh/examples/hello-world-provide.rs index 8fe0e9c12a..15c21801de 100644 --- a/iroh/examples/hello-world-provide.rs +++ b/iroh/examples/hello-world-provide.rs @@ -3,6 +3,7 @@ //! This is using an in memory database and a random node id. //! run this example from the project root: //! $ cargo run --example hello-world-provide +use iroh_base::node_addr::AddrInfoOptions; use tracing_subscriber::{prelude::*, EnvFilter}; // set the RUST_LOG env var to one of {debug,info,warn} to see logging info @@ -28,7 +29,7 @@ async fn main() -> anyhow::Result<()> { // create a ticket let ticket = node .blobs() - .share(res.hash, res.format, Default::default()) + .share(res.hash, res.format, AddrInfoOptions::RelayAndAddresses) .await?; // print some info about the node @@ -49,8 +50,8 @@ async fn main() -> anyhow::Result<()> { // print the ticket, containing all the above information println!("\nin another terminal, run:"); println!("\t cargo run --example hello-world-fetch {}", ticket); - // wait for the node to finish, this will block indefinitely - // stop with SIGINT (ctrl+c) + // block until SIGINT is received (ctrl+c) + tokio::signal::ctrl_c().await?; node.shutdown().await?; Ok(()) } diff --git a/iroh/examples/rpc.rs b/iroh/examples/rpc.rs index 29e5a4773d..e331cf7d67 100644 --- a/iroh/examples/rpc.rs +++ b/iroh/examples/rpc.rs @@ -2,10 +2,11 @@ //! //! Run this example with //! $ cargo run --features=examples --example rpc -//! Then in another terminal, run any of the normal iroh CLI commands, which you can run from -//! cargo as well: -//! $ cargo run net stats -//! The `net stats` command will reach out over RPC to the node constructed in the example +//! This will print the rpc address of the node. Copy it to use it to connect from the CLI. +//! Then in another terminal, run any of the normal iroh CLI commands supplying the rpc address, +//! which you can run from cargo as well, +//! $ cargo run -- --rpc-addr net node-addr +//! The `net node-addr` command will reach out over RPC to the node constructed in the example. use clap::Parser; use iroh_blobs::store::Store; @@ -38,8 +39,11 @@ where for addr in addrs { println!(" {}", addr); } + let rpc_addr = node.my_rpc_addr().expect("rpc enabled"); + println!("Started node with RPC enabled ({rpc_addr}). Exit with Ctrl+C"); // wait for the node to finish, this will block indefinitely // stop with SIGINT (ctrl+c) + tokio::signal::ctrl_c().await?; node.shutdown().await?; Ok(())