Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
chore: switch to public_addresses
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Paitrault <[email protected]>
  • Loading branch information
Freyskeyd committed Dec 21, 2023
1 parent cc92e34 commit 3c45f4c
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 36 deletions.
7 changes: 0 additions & 7 deletions crates/topos-p2p/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<PeerId>, P2PError> {
let (sender, receiver) = oneshot::channel();
Self::send_command_with_receiver(&self.sender, Command::ConnectedPeers { sender }, receiver)
Expand Down
7 changes: 0 additions & 7 deletions crates/topos-p2p/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ use crate::{

#[derive(Debug)]
pub enum Command {
/// Executed when the node is starting
StartListening {
peer_addr: Multiaddr,
sender: oneshot::Sender<Result<(), P2PError>>,
},

/// Command to ask for the current connected peer id list
ConnectedPeers {
sender: oneshot::Sender<Result<Vec<PeerId>, P2PError>>,
Expand Down Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion crates/topos-p2p/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 0 additions & 5 deletions crates/topos-p2p/src/runtime/handle_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions crates/topos-p2p/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Box<dyn std::error::Error>> {
if self.bootstrapped {
return Err(Box::new(P2PError::BootstrapError(
Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions crates/topos/src/components/node/services/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions crates/topos/src/config/tce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ pub struct P2PConfig {
#[serde(default = "default_listen_addresses")]
pub listen_addresses: Vec<Multiaddr>,
/// List of multiaddresses to advertise to the network
#[serde(default = "default_advertised_addresses")]
pub advertised_addresses: Vec<Multiaddr>,
#[serde(default = "default_public_addresses")]
pub public_addresses: Vec<Multiaddr>,
}

impl Default for P2PConfig {
fn default() -> Self {
Self {
listen_addresses: default_listen_addresses(),
advertised_addresses: default_advertised_addresses(),
public_addresses: default_public_addresses(),
}
}
}
Expand Down Expand Up @@ -99,7 +99,7 @@ fn default_listen_addresses() -> Vec<Multiaddr> {
)]
}

fn default_advertised_addresses() -> Vec<Multiaddr> {
fn default_public_addresses() -> Vec<Multiaddr> {
vec![format!(
"/ip4/{}/tcp/{}",
default_libp2p_api_addr().ip(),
Expand All @@ -108,7 +108,7 @@ fn default_advertised_addresses() -> Vec<Multiaddr> {
.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`
"#,
)]
Expand Down
2 changes: 2 additions & 0 deletions crates/topos/tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand All @@ -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
Expand Down

0 comments on commit 3c45f4c

Please sign in to comment.