From 86e373f786b42ae6e09aeeaa2222f3d221956b55 Mon Sep 17 00:00:00 2001 From: dozyio Date: Wed, 17 Jul 2024 12:29:01 +0100 Subject: [PATCH] feat: rust peer discovery over gossipsub --- rust-peer/src/main.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/rust-peer/src/main.rs b/rust-peer/src/main.rs index 6bbf28e5..af6c3822 100644 --- a/rust-peer/src/main.rs +++ b/rust-peer/src/main.rs @@ -42,6 +42,7 @@ const LOCAL_KEY_PATH: &str = "./local_key"; const LOCAL_CERT_PATH: &str = "./cert.pem"; const GOSSIPSUB_CHAT_TOPIC: &str = "universal-connectivity"; const GOSSIPSUB_CHAT_FILE_TOPIC: &str = "universal-connectivity-file"; +const GOSSIPSUB_PEER_DISCOVERY: &str = "universal-connectivity-browser-peer-discovery"; const BOOTSTRAP_NODES: [&str; 4] = [ "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN", "/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa", @@ -113,6 +114,7 @@ async fn main() -> Result<()> { let chat_topic_hash = gossipsub::IdentTopic::new(GOSSIPSUB_CHAT_TOPIC).hash(); let file_topic_hash = gossipsub::IdentTopic::new(GOSSIPSUB_CHAT_FILE_TOPIC).hash(); + let peer_discovery_hash = gossipsub::IdentTopic::new(GOSSIPSUB_PEER_DISCOVERY).hash(); let mut tick = futures_timer::Delay::new(TICK_INTERVAL); @@ -181,6 +183,14 @@ async fn main() -> Result<()> { continue; } + if message.topic == peer_discovery_hash { + info!( + "Received peer discovery from {:?}", + message.source + ); + continue; + } + error!("Unexpected gossipsub topic hash: {:?}", message.topic); } SwarmEvent::Behaviour(BehaviourEvent::Gossipsub( @@ -335,6 +345,7 @@ fn create_swarm( // Create/subscribe Gossipsub topics gossipsub.subscribe(&gossipsub::IdentTopic::new(GOSSIPSUB_CHAT_TOPIC))?; gossipsub.subscribe(&gossipsub::IdentTopic::new(GOSSIPSUB_CHAT_FILE_TOPIC))?; + gossipsub.subscribe(&gossipsub::IdentTopic::new(GOSSIPSUB_PEER_DISCOVERY))?; let transport = { let webrtc = webrtc::tokio::Transport::new(local_key.clone(), certificate);