Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swarm Bootstrap Nodes #201

Merged
merged 11 commits into from
Aug 15, 2023
1 change: 1 addition & 0 deletions homestar-runtime/fixtures/settings.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ process_collector_interval = 10
[node.network]
events_buffer_len = 1000
websocket_port = 9999
trusted_node_addresses = ["/ip4/127.0.0.1/tcp/9998/ws"]
mriise marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 24 additions & 2 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,8 +55,7 @@ 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
Expand All @@ -65,6 +65,28 @@ pub(crate) async fn new(settings: &settings::Node) -> Result<Swarm<ComposedBehav
Ok(swarm)
}

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

// Listen-on given address
swarm.listen_on(settings.listen_address.to_string().parse()?)?;

Ok(())
}

/// Key data structure for [request_response::Event] messages.
#[derive(Debug, Clone, Serialize, Deserialize, Encode, Decode)]
pub(crate) struct RequestResponseKey {
Expand Down
6 changes: 6 additions & 0 deletions homestar-runtime/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ pub struct Network {
pub(crate) workflow_quorum: usize,
/// Pubkey setup configuration
pub(crate) keypair_config: PubkeyConfig,
/// 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) trusted_node_addresses: Vec<libp2p::Multiaddr>,
}

/// Database-related settings for a homestar node.
Expand Down Expand Up @@ -173,6 +176,7 @@ impl Default for Network {
websocket_capacity: 100,
workflow_quorum: 3,
keypair_config: PubkeyConfig::Random,
trusted_node_addresses: Vec::new(),
}
}
}
Expand Down Expand Up @@ -323,6 +327,8 @@ 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.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