From eefed7a08201bdf796983c6e62cc98d37b8cb83f Mon Sep 17 00:00:00 2001 From: Alessandro Date: Thu, 18 Apr 2024 16:29:02 +0200 Subject: [PATCH] add identify protocol --- crates/marecchia-core/src/behaviour.rs | 14 ++++++ crates/marecchia-core/src/event_loop.rs | 59 +++++++++++++++++++++---- crates/marecchia-tracker/src/main.rs | 2 +- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/crates/marecchia-core/src/behaviour.rs b/crates/marecchia-core/src/behaviour.rs index d09916b..831fd1c 100644 --- a/crates/marecchia-core/src/behaviour.rs +++ b/crates/marecchia-core/src/behaviour.rs @@ -4,6 +4,7 @@ use libp2p::{ autonat, gossipsub::{self, MessageAuthenticity}, identity::Keypair, + identify, ping, relay, rendezvous::client as rendezvous, swarm::NetworkBehaviour, @@ -13,6 +14,7 @@ use libp2p::{ #[behaviour(to_swarm = "ComposedSwarmEvent")] pub struct ComposedSwarmBehaviour { pub ping: ping::Behaviour, + pub identify: identify::Behaviour, pub rendezvous: rendezvous::Behaviour, pub relay: relay::client::Behaviour, pub pubsub: gossipsub::Behaviour, @@ -30,6 +32,10 @@ impl ComposedSwarmBehaviour { let autonat_config = autonat::Config::default(); let _autonat = autonat::Behaviour::new(peer_id, autonat_config); + let identify_config = + identify::Config::new("/marecchia-identify/0.0.1".to_string(), keypair.public()); + let identify = identify::Behaviour::new(identify_config); + let rendezvous = rendezvous::Behaviour::new(keypair.to_owned()); // TODO: FINISH CONFIG let gossipsub_config = gossipsub::Config::default(); @@ -42,6 +48,7 @@ impl ComposedSwarmBehaviour { Self { ping, //autonat, + identify, pubsub, rendezvous, relay: relay_behaviour, @@ -52,6 +59,7 @@ impl ComposedSwarmBehaviour { #[derive(Debug)] pub enum ComposedSwarmEvent { Ping(ping::Event), + Identify(identify::Event), Rendezvous(rendezvous::Event), Relay(relay::client::Event), Gossipsub(gossipsub::Event), @@ -63,6 +71,12 @@ impl From for ComposedSwarmEvent { } } +impl From for ComposedSwarmEvent { + fn from(event: identify::Event) -> Self { + ComposedSwarmEvent::Identify(event) + } +} + impl From for ComposedSwarmEvent { fn from(event: rendezvous::Event) -> Self { ComposedSwarmEvent::Rendezvous(event) diff --git a/crates/marecchia-core/src/event_loop.rs b/crates/marecchia-core/src/event_loop.rs index f9d5598..abd76d9 100644 --- a/crates/marecchia-core/src/event_loop.rs +++ b/crates/marecchia-core/src/event_loop.rs @@ -7,9 +7,9 @@ use libp2p::gossipsub::{self, IdentTopic, SubscriptionError}; use libp2p::multiaddr::Protocol; use libp2p::rendezvous::{client as rendezvous, Cookie, Namespace}; use libp2p::swarm::{Swarm, SwarmEvent}; -use libp2p::{ping, relay, PeerId}; -use wasm_bindgen::JsError; +use libp2p::{identify, ping, relay, PeerId}; use std::collections::{HashMap, VecDeque}; +use wasm_bindgen::JsError; use super::behaviour::*; @@ -178,6 +178,7 @@ impl EventLoop { async fn handle_behaviour_event(&mut self, event: ComposedSwarmEvent) { match event { ComposedSwarmEvent::Ping(event) => self.handle_ping_event(event).await, + ComposedSwarmEvent::Identify(event) => self.handle_identify_event(event).await, ComposedSwarmEvent::Rendezvous(event) => self.handle_rendezvous_event(event).await, ComposedSwarmEvent::Relay(event) => self.handle_relay_event(event).await, ComposedSwarmEvent::Gossipsub(event) => self.handle_gossipsub_event(event).await, @@ -206,6 +207,39 @@ impl EventLoop { } } + async fn handle_identify_event(&mut self, identify_event: identify::Event) { + match identify_event { + identify::Event::Received { peer_id, info } => { + // Received identification information from a peer. + tracing::info!( + "Received identification information from peer {:?} with info {:?}", + peer_id, + info, + ); + } + identify::Event::Sent { peer_id } => { + // Sent identification information to a peer in response to a request. + tracing::info!("Sent identification information to peer {:?}", peer_id); + } + identify::Event::Pushed { peer_id, info } => { + //Identification information of the local node has been actively pushed to a peer + tracing::info!( + "Pushed identification information to peer {:?} with info {:?}", + peer_id, + info + ); + } + identify::Event::Error { peer_id, error } => { + // Failed to send identification information to a peer. + tracing::error!( + "Failed to send identification information to peer {:?} with error {:?}", + peer_id, + error + ); + } + } + } + async fn handle_rendezvous_event(&mut self, rendezvous_event: rendezvous::Event) { match rendezvous_event { rendezvous::Event::Discovered { @@ -286,7 +320,11 @@ impl EventLoop { async fn handle_relay_event(&mut self, event: relay::client::Event) { match event { - relay::client::Event::ReservationReqAccepted { relay_peer_id, renewal, limit } => { + relay::client::Event::ReservationReqAccepted { + relay_peer_id, + renewal, + limit, + } => { // A reservation request has been accepted. tracing::info!( "Reservation request accepted for relay {:?} with renewal {:?} and limit {:?}", @@ -303,7 +341,10 @@ impl EventLoop { limit ); } - relay::client::Event::OutboundCircuitEstablished { relay_peer_id, limit } => { + relay::client::Event::OutboundCircuitEstablished { + relay_peer_id, + limit, + } => { // An outbound circuit has been established. tracing::info!( "Outbound circuit established with relay {:?} with limit {:?}", @@ -328,8 +369,7 @@ impl EventLoop { propagation_source, message.topic.as_str() ); - if let Some(sender) = self.segment_request.remove(message.topic.as_str()) - { + if let Some(sender) = self.segment_request.remove(message.topic.as_str()) { let _ = sender.send(Ok(message.data)); } } @@ -435,7 +475,7 @@ pub enum Command { segment_id: String, sender: oneshot::Sender, RequestError>>, }, - Quit + Quit, } #[derive(Debug)] @@ -448,10 +488,11 @@ impl From for JsError { fn from(error: RequestError) -> Self { match error { RequestError::Timeout => JsError::new("Request timed out"), - RequestError::SubscribeError(e) => JsError::new(&format!("Subscription error: {:?}", e)), + RequestError::SubscribeError(e) => { + JsError::new(&format!("Subscription error: {:?}", e)) + } } } - } impl From for RequestError { diff --git a/crates/marecchia-tracker/src/main.rs b/crates/marecchia-tracker/src/main.rs index 0d36426..3516387 100644 --- a/crates/marecchia-tracker/src/main.rs +++ b/crates/marecchia-tracker/src/main.rs @@ -48,7 +48,7 @@ async fn main() -> Result<(), Box> { .with_bandwidth_metrics(&mut metric_registry) .with_behaviour(|key| SwarmBehaviour { identify: identify::Behaviour::new(identify::Config::new( - "rendezvous-example/0.0.1".to_string(), + "/marecchia-identify/0.0.1".to_string(), key.public(), )), rendezvous: rendezvous::server::Behaviour::new(rendezvous::server::Config::default()),