Skip to content

Commit

Permalink
Use mdns and kademlia to get integration test working
Browse files Browse the repository at this point in the history
  • Loading branch information
ryardley committed Nov 22, 2024
1 parent e71ffed commit c631f94
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
8 changes: 5 additions & 3 deletions packages/ciphernode/p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ async-trait = { workspace = true }
futures = { workspace = true }
libp2p = { workspace = true, features = [
"async-std",
"gossipsub",
"identify",
"kad",
"macros",
"mdns",
"noise",
"ping",
"kad",
"quic",
"tcp",
"tokio",
"yamux",
"gossipsub",
"quic",
] }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
Expand Down
37 changes: 31 additions & 6 deletions packages/ciphernode/p2p/src/libp2p_router.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use libp2p::connection_limits::ConnectionLimits;
use libp2p::identify;
use libp2p::{
connection_limits, futures::StreamExt, gossipsub, identify::Behaviour as IdentifyBehaviour,
identity, kad::store::MemoryStore, kad::Behaviour as KademliaBehaviour,
swarm::NetworkBehaviour, swarm::SwarmEvent,
};
use libp2p::{identify, mdns, noise, tcp, yamux};
use std::collections::hash_map::DefaultHasher;
use std::error::Error;
use std::hash::{Hash, Hasher};
Expand All @@ -18,6 +18,7 @@ pub struct NodeBehaviour {
gossipsub: gossipsub::Behaviour,
kademlia: KademliaBehaviour<MemoryStore>,
connection_limits: connection_limits::Behaviour,
mdns: mdns::tokio::Behaviour,
identify: IdentifyBehaviour,
}

Expand Down Expand Up @@ -82,6 +83,11 @@ impl EnclaveRouter {
|id| libp2p::SwarmBuilder::with_existing_identity(id),
)
.with_tokio()
.with_tcp(
tcp::Config::default(),
noise::Config::new,
yamux::Config::default,
)?
.with_quic()
.with_behaviour(|key| {
let gossipsub = gossipsub::Behaviour::new(
Expand All @@ -90,19 +96,24 @@ impl EnclaveRouter {
)
.expect("Failed to create gossipsub behavior");

NodeBehaviour {
let mdns = mdns::tokio::Behaviour::new(
mdns::Config::default(),
key.public().to_peer_id(),
)?;
Ok(NodeBehaviour {
gossipsub,
kademlia: KademliaBehaviour::new(
key.public().to_peer_id(),
MemoryStore::new(key.public().to_peer_id()),
),
mdns,
connection_limits,
identify: identify_config,
}
})
})?
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60)))
.build();

self.swarm = Some(swarm);
Ok(self)
}

Expand All @@ -124,7 +135,10 @@ impl EnclaveRouter {
.as_mut()
.unwrap()
.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse()?)?;

self.swarm
.as_mut()
.unwrap()
.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
loop {
select! {
Some(line) = self.cmd_rx.recv() => {
Expand Down Expand Up @@ -152,7 +166,18 @@ impl EnclaveRouter {
SwarmEvent::Behaviour(NodeBehaviourEvent::Kademlia(e)) => {
debug!("Kademlia event: {:?}", e);
}

SwarmEvent::Behaviour(NodeBehaviourEvent::Mdns(mdns::Event::Discovered(list))) => {
for (peer_id, _multiaddr) in list {
trace!("mDNS discovered a new peer: {peer_id}");
self.swarm.as_mut().unwrap().behaviour_mut().gossipsub.add_explicit_peer(&peer_id);
}
},
SwarmEvent::Behaviour(NodeBehaviourEvent::Mdns(mdns::Event::Expired(list))) => {
for (peer_id, _multiaddr) in list {
trace!("mDNS discover peer has expired: {peer_id}");
self.swarm.as_mut().unwrap().behaviour_mut().gossipsub.remove_explicit_peer(&peer_id);
}
},

SwarmEvent::Behaviour(NodeBehaviourEvent::Gossipsub(gossipsub::Event::Message {
propagation_source: peer_id,
Expand Down
20 changes: 10 additions & 10 deletions packages/ciphernode/p2p/src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ impl P2p {
libp2p.connect_swarm()?;
libp2p.join_topic("enclave-keygen-01")?;

let address_quic = Multiaddr::from(IpAddr::V6(Ipv6Addr::UNSPECIFIED))
.with(Protocol::Udp(PORT_QUIC))
.with(Protocol::QuicV1);

libp2p
.swarm
.as_mut()
.unwrap()
.listen_on(address_quic.clone())
.expect("listen on quic");
// let address_quic = Multiaddr::from(IpAddr::V6(Ipv6Addr::UNSPECIFIED))
// .with(Protocol::Udp(PORT_QUIC))
// .with(Protocol::QuicV1);
//
// libp2p
// .swarm
// .as_mut()
// .unwrap()
// .listen_on(address_quic.clone())
// .expect("listen on quic");

let p2p_addr = Self::spawn_and_listen(bus, tx, rx);
let handle = tokio::spawn(async move { libp2p.start().await.unwrap() });
Expand Down

0 comments on commit c631f94

Please sign in to comment.