Skip to content

Commit

Permalink
fix: address concerns from PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
mriise committed Jul 20, 2023
1 parent 901f3b2 commit b2e0f00
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion homestar-runtime/fixtures/settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ process_collector_interval = 10
[node.network]
events_buffer_len = 1000
websocket_port = 9999
bootstrap_addresses = ["/ip4/127.0.0.1/tcp/9998/ws"]
trusted_node_addresses = ["/ip4/127.0.0.1/tcp/9998/ws"]
32 changes: 21 additions & 11 deletions homestar-runtime/src/network/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use libp2p::{
};
use serde::{Deserialize, Serialize};
use std::fmt;
use tracing::{info, warn};

/// Build a new [Swarm] with a given transport and a tokio executor.
pub(crate) async fn new(settings: &settings::Node) -> Result<Swarm<ComposedBehaviour>> {
Expand Down Expand Up @@ -54,27 +55,36 @@ pub(crate) async fn new(settings: &settings::Node) -> Result<Swarm<ComposedBehav
)
.build();

// Listen-on given address
swarm.listen_on(settings.network.listen_address.to_string().parse()?)?;
swarm_startup(&mut swarm, &settings.network)?;

// subscribe to `receipts` topic
swarm
.behaviour_mut()
.gossip_subscribe(pubsub::RECEIPTS_TOPIC)?;

Ok(swarm)
}

fn swarm_startup<T: NetworkBehaviour>(
swarm: &mut Swarm<T>,
settings: &settings::Network,
) -> Result<()> {
// Dial bootstrap nodes specified in settings. Failure here shouldn't halt node startup.
for bootstrap_addr in &settings.network.bootstrap_addresses {
for trusted_addr in &settings.trusted_node_addresses {
swarm
.dial(bootstrap_addr.clone())
.dial(trusted_addr.clone())
.map(|_| {
tracing::info!("Successfully dialed configured bootstrap node {bootstrap_addr}")
info!(trusted_address=?trusted_addr, "Successfully dialed configured bootstrap node")
})
// log dial failure and continue
.map_err(|e| tracing::warn!("Failed to dial bootstrap node {e}"))
.map_err(|e| warn!(err=?e, "Failed to dial bootstrap node"))
.ok();
}

// subscribe to `receipts` topic
swarm
.behaviour_mut()
.gossip_subscribe(pubsub::RECEIPTS_TOPIC)?;
// Listen-on given address
swarm.listen_on(settings.listen_address.to_string().parse()?)?;

Ok(swarm)
Ok(())
}

/// Key data structure for [request_response::Event] messages.
Expand Down
8 changes: 4 additions & 4 deletions homestar-runtime/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ pub struct Network {
pub(crate) workflow_quorum: usize,
/// Pubkey setup configuration
pub(crate) keypair_config: PubkeyConfig,
/// Multiaddrs of the bootstrap nodes to connect to on startup
/// Multiaddrs of the trusted nodes to connect to on startup. These addresses are added as explicit peers for gossipsub.
#[serde_as(as = "Vec<serde_with::DisplayFromStr>")]
pub(crate) bootstrap_addresses: Vec<libp2p::Multiaddr>,
pub(crate) trusted_node_addresses: Vec<libp2p::Multiaddr>,
}

/// Database-related settings for a homestar node.
Expand Down Expand Up @@ -176,7 +176,7 @@ impl Default for Network {
websocket_capacity: 100,
workflow_quorum: 3,
keypair_config: PubkeyConfig::Random,
bootstrap_addresses: Vec::new(),
trusted_node_addresses: Vec::new(),
}
}
}
Expand Down Expand Up @@ -327,7 +327,7 @@ mod test {
default_modded_settings.network.events_buffer_len = 1000;
default_modded_settings.network.websocket_port = 9999;
default_modded_settings.shutdown_timeout = Duration::from_secs(20);
default_modded_settings.network.bootstrap_addresses =
default_modded_settings.network.trusted_node_addresses =
vec!["/ip4/127.0.0.1/tcp/9998/ws".to_string().try_into().unwrap()];
assert_eq!(settings.node(), &default_modded_settings);
}
Expand Down

0 comments on commit b2e0f00

Please sign in to comment.