Skip to content

Commit

Permalink
refactor: use prefix map for network config
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed May 26, 2022
1 parent 925e272 commit bb1868b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 100 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Expand Up @@ -11,7 +11,6 @@ edition = "2018"
color-eyre = "~0.6.0"
dirs-next = "~1.0.1"
eyre = "~0.6.5"
serde_json = "~1.0.62"
structopt = "~0.3.21"
tracing = "~0.1.26"
tracing-subscriber = "~0.3.1"
28 changes: 1 addition & 27 deletions src/cmd.rs
Expand Up @@ -3,7 +3,6 @@ use std::{
borrow::Cow,
ffi::{OsStr, OsString},
fmt,
net::SocketAddr,
path::Path,
process::{Command, Stdio},
thread,
Expand Down Expand Up @@ -104,13 +103,7 @@ impl<'a> NodeCmd<'a> {
Ok(String::from_utf8_lossy(&version).trim().to_string())
}

pub(crate) fn run(
&self,
node_name: &str,
node_dir: &Path,
contacts: &[SocketAddr],
genesis_key: Option<&str>,
) -> Result<()> {
pub(crate) fn run(&self, node_name: &str, node_dir: &Path) -> Result<()> {
let node_dir = node_dir.join(node_name);

let mut cmd = self.path().display().to_string();
Expand All @@ -133,25 +126,6 @@ impl<'a> NodeCmd<'a> {
extra_args.push("--log-dir");
extra_args.push(node_dir);

if let Some(genesis_key_str) = genesis_key {
trace!("Network's genesis key: {}", genesis_key_str);
extra_args.push("--genesis-key");
extra_args.push(genesis_key_str);
}

if !contacts.is_empty() {
extra_args.push("--hard-coded-contacts");
extra_args.push(
serde_json::to_string(
&contacts
.iter()
.map(|contact| contact.to_string())
.collect::<Vec<_>>(),
)
.wrap_err("Failed to generate genesis contacts list parameter")?,
);
}

let mut the_cmd = Command::new(cmd.clone());
let additonal_flame_args = vec![
"flamegraph",
Expand Down
77 changes: 5 additions & 72 deletions src/lib.rs
Expand Up @@ -12,10 +12,8 @@ mod cmd;
use eyre::{eyre, Result, WrapErr};
use std::{
borrow::Cow,
collections::HashSet,
env,
fs::{self, File},
io::BufReader,
fs::{self},
net::SocketAddr,
ops::RangeInclusive,
path::PathBuf,
Expand All @@ -33,9 +31,6 @@ const SN_NODE_EXECUTABLE: &str = "sn_node";
#[cfg(target_os = "windows")]
const SN_NODE_EXECUTABLE: &str = "sn_node.exe";

// Relative path from $HOME where to read the genesis node connection information from
const GENESIS_CONN_INFO_FILEPATH: &str = ".safe/node/node_connection_info.config";

const DEFAULT_RUST_LOG: &str = "safe_network=debug";

/// Tool to launch Safe nodes to form a local single-section network
Expand Down Expand Up @@ -113,9 +108,6 @@ impl Launch {
debug!("Genesis wait over...");
}

// Fetch node_conn_info from $HOME/.safe/node/node_connection_info.config.
let (genesis_contact_info, genesis_key) = read_genesis_conn_info()?;

debug!(
"Common node args for launching the network: {:?}",
node_cmd.args()
Expand All @@ -126,7 +118,7 @@ impl Launch {
info!("Launching nodes {:?}", node_ids);

for i in node_ids {
self.run_node(&node_cmd, i, &genesis_contact_info, genesis_key.as_ref())?;
self.run_node(&node_cmd, i)?;
thread::sleep(interval);
}
}
Expand All @@ -142,29 +134,18 @@ impl Launch {

// Let's launch genesis node now
debug!("Launching genesis node (#1)...");
genesis_cmd.run("sn-node-genesis", &self.nodes_dir, &[], None)?;
genesis_cmd.run("sn-node-genesis", &self.nodes_dir)?;

Ok(())
}

fn run_node(
&self,
node_cmd: &NodeCmd,
node_idx: usize,
contacts: &[SocketAddr],
genesis_key_str: &str,
) -> Result<()> {
fn run_node(&self, node_cmd: &NodeCmd, node_idx: usize) -> Result<()> {
if self.add_nodes_to_existing_network {
debug!("Adding node #{}...", node_idx)
} else {
debug!("Launching node #{}...", node_idx)
};
node_cmd.run(
&format!("sn-node-{}", node_idx),
&self.nodes_dir,
contacts,
Some(genesis_key_str),
)?;
node_cmd.run(&format!("sn-node-{}", node_idx), &self.nodes_dir)?;

Ok(())
}
Expand Down Expand Up @@ -206,14 +187,6 @@ pub struct Join {
#[structopt(short, long)]
max_capacity: Option<u64>,

/// List of node addresses to bootstrap to for joining
#[structopt(short = "h", long)]
hard_coded_contacts: Vec<SocketAddr>,

/// Genesis key of the network to join
#[structopt(short = "g", long)]
genesis_key: String,

/// Local network address for the node, eg 192.168.1.100:12000
#[structopt(long)]
local_addr: Option<SocketAddr>,
Expand Down Expand Up @@ -263,22 +236,10 @@ impl Join {
node_cmd.push_arg("--clear-data");
}

if self.hard_coded_contacts.is_empty() {
debug!("Failed to start a node. No contacts nodes provided.");
return Ok(());
}

debug!(
"Node to be started with contact(s): {:?}",
self.hard_coded_contacts
);

debug!("Launching node...");
node_cmd.run(
"", // no name passed
&self.nodes_dir,
&self.hard_coded_contacts,
Some(&self.genesis_key),
)?;

debug!(
Expand Down Expand Up @@ -372,31 +333,3 @@ impl CommonArgs {
}
}
}

fn read_genesis_conn_info() -> Result<(Vec<SocketAddr>, String)> {
let home_dir = dirs_next::home_dir().ok_or_else(|| eyre!("Home directory not found"))?;
let conn_info_path = home_dir.join(GENESIS_CONN_INFO_FILEPATH);

let file = File::open(&conn_info_path).wrap_err_with(|| {
format!(
"Failed to open node connection information file at '{}'",
conn_info_path.display()
)
})?;
let reader = BufReader::new(file);
let (genesis_key_str, hard_coded_contacts): (String, HashSet<SocketAddr>) =
serde_json::from_reader(reader).wrap_err_with(|| {
format!(
"Failed to parse content of node connection information file at '{}'",
conn_info_path.display()
)
})?;

let contacts: Vec<SocketAddr> = hard_coded_contacts.into_iter().collect();

debug!("Connection info directory: {}", conn_info_path.display());
debug!("Genesis node contact info: {:?}", contacts);
debug!("Network's genesis key: {}", genesis_key_str);

Ok((contacts, genesis_key_str))
}

0 comments on commit bb1868b

Please sign in to comment.