Skip to content

Commit

Permalink
docs: Add docs to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aidan46 committed Dec 20, 2024
1 parent 83f914a commit 8a7d551
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/peer-resolver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ This command starts the rendezvous server, which will listen for incoming connec
2. Register a peer by running the following command:

```bash
RUST_LOG=info cargo run --example register
RUST_LOG=info cargo run --example identify
```

This command registers a peer with the rendezvous server, allowing the peer to be discovered by other peers.
Expand All @@ -60,7 +60,8 @@ This command registers a peer with the rendezvous server, allowing the peer to b
RUST_LOG=info cargo run --example discovery
```

This command attempts to discover the registered peer using the rendezvous server. If successful, it will print the details of the discovered peer.
This command attempts to continuously discover the registered peer using the rendezvous server at an interval of 2 seconds.
Any newly registered peers will be logged.

[1]: https://docs.rs/libp2p/latest/libp2p/struct.Swarm.html
[2]: https://github.com/libp2p/specs/blob/master/rendezvous/README.md
Expand Down
2 changes: 2 additions & 0 deletions lib/peer-resolver/examples/bootstrap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! This example starts a bootstrap node (rendezvous point).
//! It listened for incoming connections and handles peer registration and discovery.
use anyhow::Result;
use peer_resolver::BootstrapSwarm;
use tracing_subscriber::EnvFilter;
Expand Down
14 changes: 12 additions & 2 deletions lib/peer-resolver/examples/discovery.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! This example attempts to discovery peers registered with the rendezvous point.
//! It continuously, with an interval of 2 seconds, requests any newly registered
//! peers information from the rendezvous point.
//! It will insert or update a hashmap with the peer information as new peer info is discovered.
use std::{
collections::{hash_map::Entry, HashMap},
time::Duration,
Expand All @@ -21,13 +25,14 @@ async fn main() -> Result<()> {
let rendezvous_point_address = "/ip4/127.0.0.1/tcp/62649".parse::<Multiaddr>()?;
// Results in peer id 12D3KooWJWoaqZhDaoEFshF7Rh1bpY9ohihFhzcW6d69Lr2NASuq
let keypair = libp2p::identity::Keypair::ed25519_from_bytes([2; 32]).unwrap();

// Create a new discovery swarm with the above keypair and a timeout of 10 seconds
let mut swarm = DiscoverySwarm::new(keypair, 10)?;
// Set discovery tick for discover request at 2 seconds
let mut discover_tick = tokio::time::interval(Duration::from_secs(2));
// Use hashmap as a mock database for peer information
let mut registration_map = HashMap::new();

// Dial in to the rendezvous point.
swarm.dial(rendezvous_point_address)?;
loop {
tokio::select! {
Expand Down Expand Up @@ -57,6 +62,7 @@ async fn main() -> Result<()> {

// Check registrations
for registration in &registrations {
// Get peer ID from the registration record
let peer_id = registration.record.peer_id();
// skip self
if &peer_id == swarm.local_peer_id() {
Expand All @@ -67,6 +73,9 @@ async fn main() -> Result<()> {
// Enter new registration in the 'db' and update existing if anything changed.
match registration_map.entry(peer_id) {
Entry::Occupied(e) => {
// Peer is already registered.
// Check if the address is already contained in the map.
// Update the address if needed.
let known_addresses: &mut Vec<Multiaddr> = e.into_mut();
for address in addresses {
if !known_addresses.contains(address) {
Expand All @@ -76,6 +85,7 @@ async fn main() -> Result<()> {
}
}
Entry::Vacant(e) => {
// Add the new peer to the mapping.
tracing::info!(%peer_id, "New peer entered with addresses {addresses:#?}");
e.insert(addresses.to_vec());
}
Expand All @@ -85,7 +95,7 @@ async fn main() -> Result<()> {
other => { tracing::debug!("Unhandled event: {other:?}"); }
}
},
// Re-request discovery from the rendezvous point
// Re-request discovery from the rendezvous point after 2 second interval
_ = discover_tick.tick(), if swarm.cookie().is_some() => swarm.discover(rendezvous_point)
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/peer-resolver/examples/identify.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! This example registers a peer with the rendezvous point,
//! allowing the peer to be discovered by other peers.
use anyhow::Result;
use peer_resolver::{Multiaddr, PeerId, RegisterSwarm, MAX_TTL};
use tracing_subscriber::EnvFilter;
Expand Down

0 comments on commit 8a7d551

Please sign in to comment.