-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add initial peer-resolver lib implementation
- Loading branch information
Showing
10 changed files
with
1,159 additions
and
72 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
license-file.workspace = true | ||
name = "peer-resolver" | ||
repository.workspace = true | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
anyhow.workspace = true | ||
libp2p = { workspace = true, features = ["identify", "macros", "noise", "rendezvous", "tcp", "tokio", "yamux"] } | ||
thiserror.workspace = true | ||
tokio = { workspace = true, features = ["macros", "time"] } | ||
tracing.workspace = true | ||
|
||
[dev-dependencies] | ||
tokio = { workspace = true, features = ["rt-multi-thread"] } | ||
tracing-subscriber = { workspace = true, features = ["env-filter"] } | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use anyhow::Result; | ||
use peer_resolver::BootstrapSwarm; | ||
use tracing_subscriber::EnvFilter; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let _ = tracing_subscriber::fmt() | ||
.with_env_filter(EnvFilter::from_default_env()) | ||
.try_init(); | ||
|
||
// Results in PeerID 12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN which is | ||
// used as the rendezvous point by the other peer examples. | ||
let keypair = libp2p::identity::Keypair::ed25519_from_bytes([0; 32]).unwrap(); | ||
|
||
let mut swarm = BootstrapSwarm::new(keypair)?; | ||
|
||
swarm.run("/ip4/0.0.0.0/tcp/62649".parse()?).await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use anyhow::Result; | ||
use libp2p::{Multiaddr, PeerId}; | ||
use peer_resolver::ClientSwarm; | ||
use tracing_subscriber::EnvFilter; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let _ = tracing_subscriber::fmt() | ||
.with_env_filter(EnvFilter::from_default_env()) | ||
.try_init(); | ||
|
||
let rendezvous_point = | ||
"12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN".parse::<PeerId>()?; | ||
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(); | ||
|
||
let mut swarm = ClientSwarm::new(keypair, "rendezvous".to_string())?; | ||
|
||
swarm | ||
.initial_discovery(rendezvous_point, rendezvous_point_address) | ||
.await?; | ||
swarm.discovery(1, rendezvous_point).await?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use anyhow::Result; | ||
use libp2p::{Multiaddr, PeerId}; | ||
use peer_resolver::ClientSwarm; | ||
use tracing_subscriber::EnvFilter; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let _ = tracing_subscriber::fmt() | ||
.with_env_filter(EnvFilter::from_default_env()) | ||
.try_init(); | ||
|
||
let rendezvous_point_address = "/ip4/127.0.0.1/tcp/62649".parse::<Multiaddr>()?; | ||
let rendezvous_point = | ||
"12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN".parse::<PeerId>()?; | ||
// Results in peer id 12D3KooWK99VoVxNE7XzyBwXEzW7xhK7Gpv85r9F3V3fyKSUKPH5 | ||
let keypair = libp2p::identity::Keypair::ed25519_from_bytes([1; 32]).unwrap(); | ||
|
||
let mut swarm = ClientSwarm::new(keypair, "rendezvous".to_string())?; | ||
|
||
swarm | ||
.register(rendezvous_point, rendezvous_point_address) | ||
.await?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use libp2p::{ | ||
futures::StreamExt, | ||
identity::Keypair, | ||
rendezvous::server, | ||
swarm::{NetworkBehaviour, SwarmEvent}, | ||
{noise, rendezvous, tcp, yamux, Multiaddr, Swarm}, | ||
}; | ||
|
||
use crate::error::ResolverError; | ||
|
||
/// This struct holds all the behaviour for the bootstrap node. | ||
#[derive(NetworkBehaviour)] | ||
pub struct BootstrapBehaviour { | ||
/// Rendezvous server behaviour for peer discovery | ||
rendezvous: server::Behaviour, | ||
} | ||
|
||
/// This struct is used by bootstrap nodes running a swarm aiding in peer discovery | ||
pub struct BootstrapSwarm { | ||
/// Swarm with [`BootstrapBehaviour`] | ||
swarm: Swarm<BootstrapBehaviour>, | ||
} | ||
|
||
impl BootstrapSwarm { | ||
/// Create a new [`BootstrapSwarm`] with the given keypair. | ||
pub fn new(keypair: Keypair) -> Result<BootstrapSwarm, ResolverError> { | ||
let swarm = libp2p::SwarmBuilder::with_existing_identity(keypair) | ||
.with_tokio() | ||
.with_tcp( | ||
tcp::Config::default(), | ||
noise::Config::new, | ||
yamux::Config::default, | ||
) | ||
.map_err(|_| ResolverError::InvalidTCPConfig)? | ||
.with_behaviour(|_| BootstrapBehaviour { | ||
rendezvous: rendezvous::server::Behaviour::new( | ||
rendezvous::server::Config::default(), | ||
), | ||
}) | ||
.map_err(|_| ResolverError::InvalidBehaviourConfig)? | ||
.build(); | ||
|
||
Ok(BootstrapSwarm { swarm }) | ||
} | ||
|
||
/// Run the rendezvous point (bootstrap node). | ||
/// Listens on the given [`Multiaddr`] | ||
pub async fn run(&mut self, addr: Multiaddr) -> Result<(), ResolverError> { | ||
self.swarm | ||
.listen_on(addr) | ||
.map_err(|_| ResolverError::ListenError)?; | ||
while let Some(event) = self.swarm.next().await { | ||
match event { | ||
SwarmEvent::ConnectionEstablished { peer_id, .. } => { | ||
tracing::info!("Connected to {}", peer_id); | ||
} | ||
SwarmEvent::ConnectionClosed { peer_id, .. } => { | ||
tracing::info!("Disconnected from {}", peer_id); | ||
} | ||
SwarmEvent::Behaviour(BootstrapBehaviourEvent::Rendezvous( | ||
rendezvous::server::Event::PeerRegistered { peer, registration }, | ||
)) => { | ||
tracing::info!( | ||
"Peer {} registered for namespace '{}'", | ||
peer, | ||
registration.namespace | ||
); | ||
} | ||
SwarmEvent::Behaviour(BootstrapBehaviourEvent::Rendezvous( | ||
rendezvous::server::Event::DiscoverServed { | ||
enquirer, | ||
registrations, | ||
}, | ||
)) => { | ||
tracing::info!( | ||
"Served peer {} with {} registrations", | ||
enquirer, | ||
registrations.len() | ||
); | ||
} | ||
_other => {} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.