From 3c45f4c8d76021c03cbea6954e0fddec62121c4e Mon Sep 17 00:00:00 2001 From: Simon Paitrault Date: Thu, 21 Dec 2023 15:21:02 +0100 Subject: [PATCH] chore: switch to public_addresses Signed-off-by: Simon Paitrault --- crates/topos-p2p/src/client.rs | 7 ------- crates/topos-p2p/src/command.rs | 7 ------- crates/topos-p2p/src/network.rs | 2 +- crates/topos-p2p/src/runtime/handle_command.rs | 5 ----- crates/topos-p2p/src/runtime/mod.rs | 10 +--------- crates/topos/src/components/node/services/process.rs | 4 ++-- crates/topos/src/config/tce.rs | 10 +++++----- crates/topos/tests/config.rs | 2 ++ 8 files changed, 11 insertions(+), 36 deletions(-) diff --git a/crates/topos-p2p/src/client.rs b/crates/topos-p2p/src/client.rs index 68dfb57bd..8c094360f 100644 --- a/crates/topos-p2p/src/client.rs +++ b/crates/topos-p2p/src/client.rs @@ -23,13 +23,6 @@ pub struct NetworkClient { } impl NetworkClient { - pub async fn start_listening(&self, peer_addr: libp2p::Multiaddr) -> Result<(), P2PError> { - let (sender, receiver) = oneshot::channel(); - let command = Command::StartListening { peer_addr, sender }; - - Self::send_command_with_receiver(&self.sender, command, receiver).await - } - pub async fn connected_peers(&self) -> Result, P2PError> { let (sender, receiver) = oneshot::channel(); Self::send_command_with_receiver(&self.sender, Command::ConnectedPeers { sender }, receiver) diff --git a/crates/topos-p2p/src/command.rs b/crates/topos-p2p/src/command.rs index f819e421c..40cf5ac94 100644 --- a/crates/topos-p2p/src/command.rs +++ b/crates/topos-p2p/src/command.rs @@ -10,12 +10,6 @@ use crate::{ #[derive(Debug)] pub enum Command { - /// Executed when the node is starting - StartListening { - peer_addr: Multiaddr, - sender: oneshot::Sender>, - }, - /// Command to ask for the current connected peer id list ConnectedPeers { sender: oneshot::Sender, P2PError>>, @@ -56,7 +50,6 @@ pub enum Command { impl Display for Command { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Command::StartListening { .. } => write!(f, "StartListening"), Command::ConnectedPeers { .. } => write!(f, "ConnectedPeers"), Command::RandomKnownPeer { .. } => write!(f, "RandomKnownPeer"), Command::Disconnect { .. } => write!(f, "Disconnect"), diff --git a/crates/topos-p2p/src/network.rs b/crates/topos-p2p/src/network.rs index 490285ca7..d193953d6 100644 --- a/crates/topos-p2p/src/network.rs +++ b/crates/topos-p2p/src/network.rs @@ -174,7 +174,7 @@ impl<'a> NetworkBuilder<'a> { let listen_addr = self .listen_addresses .take() - .expect("Node cannot listen for connection on empty address"); + .expect("Node requires at least one address to listen for incoming connections"); let advertised_addresses = if let Some(addresses) = self.advertised_addresses.take() { if addresses.is_empty() { diff --git a/crates/topos-p2p/src/runtime/handle_command.rs b/crates/topos-p2p/src/runtime/handle_command.rs index 8540919cc..148f05ba5 100644 --- a/crates/topos-p2p/src/runtime/handle_command.rs +++ b/crates/topos-p2p/src/runtime/handle_command.rs @@ -26,11 +26,6 @@ impl Runtime { _ = response.send(connection); } - Command::StartListening { peer_addr, sender } => { - if sender.send(self.start_listening(peer_addr)).is_err() { - warn!("Unable to notify StartListening response: initiator is dropped"); - } - } Command::ConnectedPeers { sender } => { if sender diff --git a/crates/topos-p2p/src/runtime/mod.rs b/crates/topos-p2p/src/runtime/mod.rs index ff917fdd5..bf7658d18 100644 --- a/crates/topos-p2p/src/runtime/mod.rs +++ b/crates/topos-p2p/src/runtime/mod.rs @@ -45,13 +45,6 @@ mod handle_command; mod handle_event; impl Runtime { - fn start_listening(&mut self, peer_addr: Multiaddr) -> Result<(), P2PError> { - self.swarm - .listen_on(peer_addr) - .map(|_| ()) - .map_err(Into::into) - } - pub async fn bootstrap(mut self) -> Result> { if self.bootstrapped { return Err(Box::new(P2PError::BootstrapError( @@ -73,8 +66,7 @@ impl Runtime { .ok_or(P2PError::MissingAdvertisedAddresses)?; debug!("Starting to listen on {:?}", self.listening_on); - let addresses = self.listening_on.clone(); - for addr in addresses { + for addr in &self.listening_on { if let Err(error) = self.swarm.listen_on(addr.clone()) { error!("Couldn't start listening on {} because of {error:?}", addr); diff --git a/crates/topos/src/components/node/services/process.rs b/crates/topos/src/components/node/services/process.rs index 27bb138ce..23cff7bca 100644 --- a/crates/topos/src/components/node/services/process.rs +++ b/crates/topos/src/components/node/services/process.rs @@ -99,7 +99,7 @@ pub(crate) fn spawn_tce_process( .expect("Unable to generate Multiaddr from `libp2p_api_addr`"); config.p2p.listen_addresses = vec![addr.clone()]; - config.p2p.advertised_addresses = vec![addr]; + config.p2p.public_addresses = vec![addr]; } let tce_config = TceConfiguration { @@ -112,7 +112,7 @@ pub(crate) fn spawn_tce_process( auth_key: keys.network.map(AuthKey::PrivateKey), signing_key: keys.validator.map(AuthKey::PrivateKey), listen_addresses: config.p2p.listen_addresses, - advertised_addresses: config.p2p.advertised_addresses, + advertised_addresses: config.p2p.public_addresses, tce_params, api_addr: config.grpc_api_addr, graphql_api_addr: config.graphql_api_addr, diff --git a/crates/topos/src/config/tce.rs b/crates/topos/src/config/tce.rs index 75ea072c5..a414c8405 100644 --- a/crates/topos/src/config/tce.rs +++ b/crates/topos/src/config/tce.rs @@ -59,15 +59,15 @@ pub struct P2PConfig { #[serde(default = "default_listen_addresses")] pub listen_addresses: Vec, /// List of multiaddresses to advertise to the network - #[serde(default = "default_advertised_addresses")] - pub advertised_addresses: Vec, + #[serde(default = "default_public_addresses")] + pub public_addresses: Vec, } impl Default for P2PConfig { fn default() -> Self { Self { listen_addresses: default_listen_addresses(), - advertised_addresses: default_advertised_addresses(), + public_addresses: default_public_addresses(), } } } @@ -99,7 +99,7 @@ fn default_listen_addresses() -> Vec { )] } -fn default_advertised_addresses() -> Vec { +fn default_public_addresses() -> Vec { vec![format!( "/ip4/{}/tcp/{}", default_libp2p_api_addr().ip(), @@ -108,7 +108,7 @@ fn default_advertised_addresses() -> Vec { .parse() .expect( r#" - Advertised multiaddresses generation failure. + Public multiaddresses generation failure. This is a critical bug that need to be report on `https://github.com/topos-protocol/topos/issues` "#, )] diff --git a/crates/topos/tests/config.rs b/crates/topos/tests/config.rs index 988212551..3916f8466 100644 --- a/crates/topos/tests/config.rs +++ b/crates/topos/tests/config.rs @@ -416,6 +416,7 @@ async fn command_node_up_with_old_config( .env("TOPOS_POLYGON_EDGE_BIN_PATH", &node_edge_path_env) .env("TOPOS_NODE_NAME", node_up_name_env) .env("TOPOS_HOME", node_up_home_env) + .env("RUST_LOG", "topos=info") .arg("up") .stdout(Stdio::piped()) .spawn()?; @@ -426,6 +427,7 @@ async fn command_node_up_with_old_config( let stdout = join.join().unwrap()?.stdout; let stdout = String::from_utf8_lossy(&stdout); + println!("STDOUT: {}", stdout); assert!(stdout.contains(r#"Local node is listening on "/ip4/127.0.0.1/tcp/9091/p2p/"#)); // Cleanup