Skip to content

Commit

Permalink
feat(launchpad): add network id arg for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Dec 16, 2024
1 parent 87f2f70 commit 039c8ba
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 55 deletions.
23 changes: 17 additions & 6 deletions node-launchpad/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl App {
peers_args: PeersArgs,
antnode_path: Option<PathBuf>,
app_data_path: Option<PathBuf>,
network_id: Option<u8>,
) -> Result<Self> {
// Configurations
let app_data = AppData::load(app_data_path)?;
Expand Down Expand Up @@ -93,6 +94,7 @@ impl App {
allocated_disk_space: app_data.nodes_to_start,
rewards_address: app_data.discord_username.clone(),
peers_args,
network_id,
antnode_path,
data_dir_path,
connection_mode,
Expand Down Expand Up @@ -356,7 +358,7 @@ mod tests {
let mut output = Cursor::new(Vec::new());

// Create and run the App, capturing its output
let app_result = App::new(60.0, 60.0, peers_args, None, Some(config_path)).await;
let app_result = App::new(60.0, 60.0, peers_args, None, Some(config_path), None).await;

match app_result {
Ok(app) => {
Expand Down Expand Up @@ -417,7 +419,8 @@ mod tests {
let mut output = Cursor::new(Vec::new());

// Create and run the App, capturing its output
let app_result = App::new(60.0, 60.0, peers_args, None, Some(test_app_data_path)).await;
let app_result =
App::new(60.0, 60.0, peers_args, None, Some(test_app_data_path), None).await;

match app_result {
Ok(app) => {
Expand Down Expand Up @@ -472,8 +475,15 @@ mod tests {
let mut output = Cursor::new(Vec::new());

// Create and run the App, capturing its output
let app_result =
App::new(60.0, 60.0, peers_args, None, Some(non_existent_config_path)).await;
let app_result = App::new(
60.0,
60.0,
peers_args,
None,
Some(non_existent_config_path),
None,
)
.await;

match app_result {
Ok(app) => {
Expand Down Expand Up @@ -535,7 +545,7 @@ mod tests {
let peers_args = PeersArgs::default();

// Create and run the App, capturing its output
let app_result = App::new(60.0, 60.0, peers_args, None, Some(config_path)).await;
let app_result = App::new(60.0, 60.0, peers_args, None, Some(config_path), None).await;

// Could be that the mountpoint doesn't exists
// or that the user doesn't have permissions to access it
Expand Down Expand Up @@ -576,7 +586,8 @@ mod tests {
let peers_args = PeersArgs::default();

// Create and run the App
let app_result = App::new(60.0, 60.0, peers_args, None, Some(test_app_data_path)).await;
let app_result =
App::new(60.0, 60.0, peers_args, None, Some(test_app_data_path), None).await;

match app_result {
Ok(app) => {
Expand Down
47 changes: 25 additions & 22 deletions node-launchpad/src/bin/tui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,44 @@ use std::{env, path::PathBuf};
#[derive(Parser, Debug)]
#[command(disable_version_flag = true)]
pub struct Cli {
#[arg(
short,
long,
value_name = "FLOAT",
help = "Tick rate, i.e. number of ticks per second",
default_value_t = 1.0
)]
pub tick_rate: f64,

#[arg(
short,
long,
value_name = "FLOAT",
help = "Frame rate, i.e. number of frames per second",
default_value_t = 60.0
)]
pub frame_rate: f64,

/// Provide a path for the antnode binary to be used by the service.
///
/// Useful for creating the service using a custom built binary.
#[clap(long)]
antnode_path: Option<PathBuf>,

#[command(flatten)]
pub(crate) peers: PeersArgs,

/// Print the crate version.
#[clap(long)]
crate_version: bool,

/// Specify the network ID to use. This will allow you to run the node on a different network.
///
/// By default, the network ID is set to 1, which represents the mainnet.
#[clap(long, verbatim_doc_comment)]
network_id: Option<u8>,

/// Frame rate, i.e. number of frames per second
#[arg(short, long, value_name = "FLOAT", default_value_t = 60.0)]
frame_rate: f64,

/// Provide a path for the antnode binary to be used by the service.
///
/// Useful for creating the service using a custom built binary.
#[clap(long)]
path: Option<PathBuf>,

#[command(flatten)]
peers: PeersArgs,

/// Print the package version.
#[clap(long)]
#[cfg(not(feature = "nightly"))]
package_version: bool,

/// Tick rate, i.e. number of ticks per second
#[arg(short, long, value_name = "FLOAT", default_value_t = 1.0)]
tick_rate: f64,

/// Print the version.
#[clap(long)]
version: bool,
Expand Down Expand Up @@ -129,7 +131,8 @@ async fn main() -> Result<()> {
args.frame_rate,
args.peers,
args.antnode_path,
None,
args.path,
args.network_id,
)
.await?;
app.run().await?;
Expand Down
21 changes: 13 additions & 8 deletions node-launchpad/src/components/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub struct Status<'a> {
// Nodes
node_services: Vec<NodeServiceData>,
items: Option<StatefulTable<NodeItem<'a>>>,
/// To pass into node services.
network_id: Option<u8>,
// Node Management
node_management: NodeManagement,
// Amount of nodes
Expand Down Expand Up @@ -117,13 +119,14 @@ pub enum LockRegistryState {

pub struct StatusConfig {
pub allocated_disk_space: usize,
pub rewards_address: String,
pub peers_args: PeersArgs,
pub antnode_path: Option<PathBuf>,
pub data_dir_path: PathBuf,
pub connection_mode: ConnectionMode,
pub data_dir_path: PathBuf,
pub network_id: Option<u8>,
pub peers_args: PeersArgs,
pub port_from: Option<u32>,
pub port_to: Option<u32>,
pub rewards_address: String,
}

impl Status<'_> {
Expand All @@ -135,6 +138,7 @@ impl Status<'_> {
active: true,
is_nat_status_determined: false,
error_while_running_nat_detection: 0,
network_id: config.network_id,
node_stats: NodeStats::default(),
node_stats_last_update: Instant::now(),
node_services: Default::default(),
Expand Down Expand Up @@ -614,16 +618,17 @@ impl Component for Status<'_> {
let action_sender = self.get_actions_sender()?;

let maintain_nodes_args = MaintainNodesArgs {
action_sender: action_sender.clone(),
antnode_path: self.antnode_path.clone(),
connection_mode: self.connection_mode,
count: self.nodes_to_start as u16,
data_dir_path: Some(self.data_dir_path.clone()),
network_id: self.network_id,
owner: self.rewards_address.clone(),
peers_args: self.peers_args.clone(),
run_nat_detection: self.should_we_run_nat_detection(),
antnode_path: self.antnode_path.clone(),
data_dir_path: Some(self.data_dir_path.clone()),
action_sender: action_sender.clone(),
connection_mode: self.connection_mode,
port_range: Some(port_range),
rewards_address: self.rewards_address.clone(),
run_nat_detection: self.should_we_run_nat_detection(),
};

debug!("Calling maintain_n_running_nodes");
Expand Down
41 changes: 22 additions & 19 deletions node-launchpad/src/node_mgmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,17 @@ async fn stop_nodes(services: Vec<String>, action_sender: UnboundedSender<Action

#[derive(Debug)]
pub struct MaintainNodesArgs {
pub action_sender: UnboundedSender<Action>,
pub antnode_path: Option<PathBuf>,
pub connection_mode: ConnectionMode,
pub count: u16,
pub data_dir_path: Option<PathBuf>,
pub network_id: Option<u8>,
pub owner: String,
pub peers_args: PeersArgs,
pub run_nat_detection: bool,
pub antnode_path: Option<PathBuf>,
pub data_dir_path: Option<PathBuf>,
pub action_sender: UnboundedSender<Action>,
pub connection_mode: ConnectionMode,
pub port_range: Option<PortRange>,
pub rewards_address: String,
pub run_nat_detection: bool,
}

/// Maintain the specified number of nodes
Expand Down Expand Up @@ -289,16 +290,17 @@ async fn load_node_registry(
}

struct NodeConfig {
antnode_path: Option<PathBuf>,
auto_set_nat_flags: bool,
upnp: bool,
home_network: bool,
custom_ports: Option<PortRange>,
owner: Option<String>,
count: u16,
custom_ports: Option<PortRange>,
data_dir_path: Option<PathBuf>,
home_network: bool,
network_id: Option<u8>,
owner: Option<String>,
peers_args: PeersArgs,
antnode_path: Option<PathBuf>,
rewards_address: String,
upnp: bool,
}

/// Run the NAT detection process
Expand Down Expand Up @@ -344,9 +346,10 @@ async fn run_nat_detection(action_sender: &UnboundedSender<Action>) {

fn prepare_node_config(args: &MaintainNodesArgs) -> NodeConfig {
NodeConfig {
antnode_path: args.antnode_path.clone(),
auto_set_nat_flags: args.connection_mode == ConnectionMode::Automatic,
upnp: args.connection_mode == ConnectionMode::UPnP,
home_network: args.connection_mode == ConnectionMode::HomeNetwork,
data_dir_path: args.data_dir_path.clone(),
count: args.count,
custom_ports: if args.connection_mode == ConnectionMode::CustomPorts {
args.port_range.clone()
} else {
Expand All @@ -357,11 +360,11 @@ fn prepare_node_config(args: &MaintainNodesArgs) -> NodeConfig {
} else {
Some(args.owner.clone())
},
count: args.count,
data_dir_path: args.data_dir_path.clone(),
home_network: args.connection_mode == ConnectionMode::HomeNetwork,
network_id: args.network_id,
peers_args: args.peers_args.clone(),
antnode_path: args.antnode_path.clone(),
rewards_address: args.rewards_address.clone(),
upnp: args.connection_mode == ConnectionMode::UPnP,
}
}

Expand All @@ -373,8 +376,8 @@ fn debug_log_config(config: &NodeConfig, args: &MaintainNodesArgs) {
config.count
);
debug!(
" owner: {:?}, peers_args: {:?}, antnode_path: {:?}",
config.owner, config.peers_args, config.antnode_path
" owner: {:?}, peers_args: {:?}, antnode_path: {:?}, network_id: {:?}",
config.owner, config.peers_args, config.antnode_path, args.network_id
);
debug!(
" data_dir_path: {:?}, connection_mode: {:?}",
Expand Down Expand Up @@ -423,7 +426,7 @@ async fn scale_down_nodes(config: &NodeConfig, count: u16) {
None,
None,
None,
None,
config.network_id,
None,
None, // We don't care about the port, as we are scaling down
config.owner.clone(),
Expand Down Expand Up @@ -497,7 +500,7 @@ async fn add_nodes(
None,
None,
None,
None,
config.network_id,
None,
port_range,
config.owner.clone(),
Expand Down

0 comments on commit 039c8ba

Please sign in to comment.