Skip to content

Commit

Permalink
fix: retain options on upgrade and prevent dup ports
Browse files Browse the repository at this point in the history
The following options that can be specified when the service is created are now retained on an
upgrade:

* Custom `--node-port` value
* Custom `--metrics-port` value
* The `--home-network` flag
* The `--upnp` flag

Tests were added for all of these, and in addition, a test was added for the retention of custom RPC
ports, though these were already being retained on upgrade.

Although the node port was being tracked as part of the `listen_addr` field in `NodeServiceData`, a
new `node_port` field was explicitly added, because it's possible that a service can be upgraded
before it starts, and the `listen_addr` is not assigned until the service starts.

I also discovered the `add` command did not prevent specifying custom ports that were already in
use. That was fixed too, and some tests were added.
  • Loading branch information
jacderida committed May 16, 2024
1 parent e3caac5 commit 7568040
Show file tree
Hide file tree
Showing 9 changed files with 1,344 additions and 25 deletions.
1 change: 1 addition & 0 deletions node-launchpad/src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ fn add_and_start_nodes(
None,
None,
None,
false,
None,
None,
None,
Expand Down
43 changes: 43 additions & 0 deletions sn_node_manager/src/add_services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ pub async fn add_node(
}
}

if let Some(port_option) = &options.node_port {
check_port_availability(port_option, &node_registry.nodes)?;
}

if let Some(port_option) = &options.metrics_port {
check_port_availability(port_option, &node_registry.nodes)?;
}

if let Some(port_option) = &options.rpc_port {
check_port_availability(port_option, &node_registry.nodes)?;
}

let safenode_file_name = options
.safenode_src_path
.file_name()
Expand Down Expand Up @@ -202,6 +214,8 @@ pub async fn add_node(
listen_addr: None,
local: options.local,
log_dir_path: service_log_dir_path.clone(),
metrics_port,
node_port,
number: node_number,
reward_balance: None,
rpc_socket_addr,
Expand Down Expand Up @@ -415,3 +429,32 @@ fn increment_port_option(port: Option<u16>) -> Option<u16> {
}
None
}

fn check_port_availability(port_option: &PortRange, nodes: &[NodeServiceData]) -> Result<()> {
let mut all_ports = Vec::new();
for node in nodes {
if let Some(port) = node.metrics_port {
all_ports.push(port);
}
if let Some(port) = node.node_port {
all_ports.push(port);
}
all_ports.push(node.rpc_socket_addr.port());
}

match port_option {
PortRange::Single(port) => {
if all_ports.iter().any(|p| *p == *port) {
return Err(eyre!("Port {port} is being used by another service"));
}
}
PortRange::Range(start, end) => {
for i in *start..=*end {
if all_ports.iter().any(|p| *p == i) {
return Err(eyre!("Port {i} is being used by another service"));
}
}
}
}
Ok(())
}
Loading

0 comments on commit 7568040

Please sign in to comment.