Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement autonat server behaviour #8

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = [".", "examples/dcutr", "examples/chat"]

[package]
name = "boot-node"
version = "0.4.0"
version = "0.5.0"
authors = ["Calimero Limited <[email protected]>"]
edition = "2021"
repository = "https://github.com/calimero-network/boot-node"
Expand All @@ -15,6 +15,7 @@ clap = { version = "4.5.4", features = ["derive", "env"] }
eyre = "0.6.12"
futures-util = "0.3.30"
libp2p = { version = "0.53.2", features = [
"autonat",
"identify",
"kad",
"macros",
Expand Down
17 changes: 12 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::net::Ipv4Addr;
use clap::Parser;
use libp2p::futures::prelude::*;
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
use libp2p::{identify, identity, kad, ping, relay, rendezvous, Multiaddr, StreamProtocol, Swarm};
use libp2p::{
autonat, identify, identity, kad, ping, relay, rendezvous, Multiaddr, StreamProtocol, Swarm,
};
use tracing::info;
use tracing_subscriber::prelude::*;
use tracing_subscriber::EnvFilter;
Expand All @@ -13,11 +15,12 @@ const CALIMERO_KAD_PROTO_NAME: StreamProtocol = StreamProtocol::new("/calimero/k

#[derive(NetworkBehaviour)]
struct Behaviour {
autonat: autonat::Behaviour,
identify: identify::Behaviour,
kad: kad::Behaviour<kad::store::MemoryStore>,
ping: ping::Behaviour,
rendezvous: rendezvous::server::Behaviour,
relay: relay::Behaviour,
rendezvous: rendezvous::server::Behaviour,
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -61,6 +64,7 @@ async fn main() -> eyre::Result<()> {
)?
.with_quic()
.with_behaviour(|keypair| Behaviour {
autonat: autonat::Behaviour::new(peer_id.clone(), Default::default()),
identify: identify::Behaviour::new(identify::Config::new(
PROTOCOL_VERSION.to_owned(),
keypair.public(),
Expand Down Expand Up @@ -127,6 +131,9 @@ async fn handle_swarm_event(swarm: &mut Swarm<Behaviour>, event: SwarmEvent<Beha

async fn handle_swarm_behaviour_event(swarm: &mut Swarm<Behaviour>, event: BehaviourEvent) {
match event {
BehaviourEvent::Autonat(event) => {
info!("AutoNat event: {event:?}");
}
BehaviourEvent::Identify(event) => {
info!("Identify event: {event:?}");
match event {
Expand All @@ -143,12 +150,12 @@ async fn handle_swarm_behaviour_event(swarm: &mut Swarm<Behaviour>, event: Behav
BehaviourEvent::Kad(event) => {
info!("Kad event: {event:?}");
}
BehaviourEvent::Rendezvous(event) => {
info!("Rendezvous event: {event:?}");
}
BehaviourEvent::Relay(event) => {
info!("Relay event: {event:?}");
}
BehaviourEvent::Rendezvous(event) => {
info!("Rendezvous event: {event:?}");
}
_ => {}
}
}