Skip to content

Commit

Permalink
fix: replaced the hack with other events.
Browse files Browse the repository at this point in the history
  • Loading branch information
shawn-zil committed Dec 18, 2024
1 parent 6050023 commit 55dedab
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions zilliqa/src/p2p_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use anyhow::{anyhow, Result};
use cfg_if::cfg_if;
use libp2p::{
autonat,
core::ConnectedPoint,
futures::StreamExt,
gossipsub::{self, IdentTopic, MessageAuthenticity, TopicHash},
identify,
Expand Down Expand Up @@ -127,13 +128,9 @@ impl P2pNode {
autonat_client: autonat::v2::client::Behaviour::default(),
autonat_server: autonat::v2::server::Behaviour::default(),
kademlia: kad::Behaviour::new(peer_id, MemoryStore::new(peer_id)),
// FIXME: This is a hack.
// By exposing the listen addresses, the nodes are able to get the correct remote ip/port to connect to.
// Otherwise, when running locally in docker, the nodes connect to each other via the gateway acting as a NAT router.
// So, the nodes are unable to see each other directly and remain isolated, defeating kademlia and autonat.
identify: identify::Behaviour::new(
identify::Config::new("zilliqa/1.0.0".into(), key_pair.public())
.with_hide_listen_addrs(!cfg!(debug_assertions)),
.with_hide_listen_addrs(true),
),
})
})?
Expand Down Expand Up @@ -247,17 +244,19 @@ impl P2pNode {
let event = event.expect("swarm stream should be infinite");
debug!(?event, "swarm event");
match event {
SwarmEvent::ConnectionEstablished { peer_id, endpoint, .. } => {
let address = match endpoint {
ConnectedPoint::Dialer{ address, .. } => address,
ConnectedPoint::Listener {send_back_addr, .. } => send_back_addr,
};
self.swarm.add_peer_address(peer_id, address);
}
SwarmEvent::NewExternalAddrCandidate { address } => {
self.swarm.add_external_address(address);
}
SwarmEvent::NewListenAddr { address, .. } => {
info!(%address, "P2P swarm listening on");
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received { peer_id, info: identify::Info{ listen_addrs, observed_addr, protocols, .. }, .. })) => {
self.swarm.add_external_address(observed_addr);
if protocols.iter().any(|p| *p == kad::PROTOCOL_NAME) {
for addr in listen_addrs {
self.swarm.add_peer_address(peer_id, addr);
}
}
}
SwarmEvent::NewExternalAddrOfPeer { peer_id, address } => {
self.swarm
.behaviour_mut()
Expand Down

0 comments on commit 55dedab

Please sign in to comment.