Skip to content

Commit

Permalink
fix(examples): Make collection-provide, hello-world-provide and `…
Browse files Browse the repository at this point in the history
…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

<!-- Optional, if there are any breaking changes document them,
including how to migrate older code. -->
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 <[email protected]>
  • Loading branch information
matheus23 and divagant-martian authored Sep 26, 2024
1 parent 857e513 commit 25c8305
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
11 changes: 8 additions & 3 deletions iroh/examples/collection-provide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(())
}
7 changes: 4 additions & 3 deletions iroh/examples/hello-world-provide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(())
}
12 changes: 8 additions & 4 deletions iroh/examples/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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;
Expand Down Expand Up @@ -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(())
Expand Down

0 comments on commit 25c8305

Please sign in to comment.