Skip to content

Commit

Permalink
add identify protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrohd committed Apr 18, 2024
1 parent 1133e5f commit eefed7a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
14 changes: 14 additions & 0 deletions crates/marecchia-core/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use libp2p::{
autonat,
gossipsub::{self, MessageAuthenticity},
identity::Keypair,
identify,
ping, relay,
rendezvous::client as rendezvous,
swarm::NetworkBehaviour,
Expand All @@ -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,
Expand All @@ -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();
Expand All @@ -42,6 +48,7 @@ impl ComposedSwarmBehaviour {
Self {
ping,
//autonat,
identify,
pubsub,
rendezvous,
relay: relay_behaviour,
Expand All @@ -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),
Expand All @@ -63,6 +71,12 @@ impl From<ping::Event> for ComposedSwarmEvent {
}
}

impl From<identify::Event> for ComposedSwarmEvent {
fn from(event: identify::Event) -> Self {
ComposedSwarmEvent::Identify(event)
}
}

impl From<rendezvous::Event> for ComposedSwarmEvent {
fn from(event: rendezvous::Event) -> Self {
ComposedSwarmEvent::Rendezvous(event)
Expand Down
59 changes: 50 additions & 9 deletions crates/marecchia-core/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {:?}",
Expand All @@ -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 {:?}",
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -435,7 +475,7 @@ pub enum Command {
segment_id: String,
sender: oneshot::Sender<Result<Vec<u8>, RequestError>>,
},
Quit
Quit,
}

#[derive(Debug)]
Expand All @@ -448,10 +488,11 @@ impl From<RequestError> 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<SubscriptionError> for RequestError {
Expand Down
2 changes: 1 addition & 1 deletion crates/marecchia-tracker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.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()),
Expand Down

0 comments on commit eefed7a

Please sign in to comment.