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 e19650d
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 56 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
4 changes: 2 additions & 2 deletions crates/topos-p2p/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ pub enum P2PError {
#[error("Unable to create gRPC client")]
UnableToCreateGrpcClient(#[from] OutboundConnectionError),

#[error("Advertised addresses is empty")]
MissingAdvertisedAddresses,
#[error("Public addresses is empty")]
MissingPublicAddresses,
}

#[derive(Error, Debug)]
Expand Down
12 changes: 6 additions & 6 deletions crates/topos-p2p/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct NetworkBuilder<'a> {
discovery_protocol: Option<&'static str>,
peer_key: Option<Keypair>,
listen_addresses: Option<Vec<Multiaddr>>,
advertised_addresses: Option<Vec<Multiaddr>>,
public_addresses: Option<Vec<Multiaddr>>,
store: Option<MemoryStore>,
known_peers: &'a [(PeerId, Multiaddr)],
local_port: Option<u8>,
Expand Down Expand Up @@ -79,8 +79,8 @@ impl<'a> NetworkBuilder<'a> {
self
}

pub fn advertised_addresses(mut self, addresses: Vec<Multiaddr>) -> Self {
self.advertised_addresses = Some(addresses);
pub fn public_addresses(mut self, addresses: Vec<Multiaddr>) -> Self {
self.public_addresses = Some(addresses);

self
}
Expand Down Expand Up @@ -174,9 +174,9 @@ 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() {
let public_addresses = if let Some(addresses) = self.public_addresses.take() {
if addresses.is_empty() {
listen_addr.clone()
} else {
Expand Down Expand Up @@ -204,7 +204,7 @@ impl<'a> NetworkBuilder<'a> {
event_sender,
local_peer_id: peer_id,
listening_on: listen_addr,
advertised_addresses,
public_addresses,
bootstrapped: false,
active_listeners: HashSet::new(),
pending_record_requests: HashMap::new(),
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
20 changes: 6 additions & 14 deletions crates/topos-p2p/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Runtime {
pub(crate) event_sender: mpsc::Sender<Event>,
pub(crate) local_peer_id: PeerId,
pub(crate) listening_on: Vec<Multiaddr>,
pub(crate) advertised_addresses: Vec<Multiaddr>,
pub(crate) public_addresses: Vec<Multiaddr>,
pub(crate) bootstrapped: bool,
pub(crate) is_boot_node: bool,

Expand All @@ -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 @@ -61,20 +54,19 @@ impl Runtime {

self.bootstrapped = true;

debug!("Added external addresses: {:?}", self.advertised_addresses);
for address in &self.advertised_addresses {
debug!("Added external addresses: {:?}", self.public_addresses);
for address in &self.public_addresses {
self.swarm.add_external_address(address.clone());
}

let dht_address = self
.advertised_addresses
.public_addresses
.first()
.map(Multiaddr::to_vec)
.ok_or(P2PError::MissingAdvertisedAddresses)?;
.ok_or(P2PError::MissingPublicAddresses)?;

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
6 changes: 3 additions & 3 deletions crates/topos-p2p/src/tests/command/random_peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn no_random_peer() {

let (client, _, runtime) = crate::network::builder()
.peer_key(local.keypair.clone())
.advertised_addresses(vec![local.addr.clone()])
.public_addresses(vec![local.addr.clone()])
.listen_addresses(vec![local.addr.clone()])
.build()
.await
Expand Down Expand Up @@ -46,7 +46,7 @@ async fn return_a_peer() {

let (client, _, runtime) = crate::network::builder()
.peer_key(local.keypair.clone())
.advertised_addresses(vec![local.addr.clone()])
.public_addresses(vec![local.addr.clone()])
.listen_addresses(vec![local.addr.clone()])
.build()
.await
Expand Down Expand Up @@ -75,7 +75,7 @@ async fn return_a_random_peer_among_100() {

let (client, _, runtime) = crate::network::builder()
.peer_key(local.keypair.clone())
.advertised_addresses(vec![local.addr.clone()])
.public_addresses(vec![local.addr.clone()])
.listen_addresses(vec![local.addr.clone()])
.build()
.await
Expand Down
4 changes: 2 additions & 2 deletions crates/topos-p2p/src/tests/dht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn put_value_in_dht() {
let (_, _, runtime) = crate::network::builder()
.peer_key(peer_2.keypair.clone())
.known_peers(&[(peer_1.peer_id(), peer_1.addr.clone())])
.advertised_addresses(vec![peer_2.addr.clone()])
.public_addresses(vec![peer_2.addr.clone()])
.listen_addresses(vec![peer_2.addr.clone()])
.minimum_cluster_size(1)
.discovery_config(
Expand All @@ -44,7 +44,7 @@ async fn put_value_in_dht() {
Record::new(
input_key.clone(),
runtime
.advertised_addresses
.public_addresses
.first()
.map(Multiaddr::to_vec)
.unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion crates/topos-tce/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct TceConfiguration {
pub minimum_cluster_size: usize,
pub version: &'static str,
pub listen_addresses: Vec<Multiaddr>,
pub advertised_addresses: Vec<Multiaddr>,
pub public_addresses: Vec<Multiaddr>,
}

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion crates/topos-tce/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub async fn run(
.peer_key(key)
.listen_addresses(config.listen_addresses.clone())
.minimum_cluster_size(config.minimum_cluster_size)
.advertised_addresses(config.advertised_addresses.clone())
.public_addresses(config.public_addresses.clone())
.known_peers(&boot_peers)
.grpc_context(grpc_context)
.build()
Expand Down
2 changes: 1 addition & 1 deletion crates/topos-test-sdk/src/tce/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub async fn create_network_worker(
topos_p2p::network::builder()
.peer_key(key.clone())
.known_peers(&known_peers)
.advertised_addresses(addr.clone())
.public_addresses(addr.clone())
.listen_addresses(addr)
.minimum_cluster_size(minimum_cluster_size)
.grpc_context(grpc_context)
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,
public_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 e19650d

Please sign in to comment.