Skip to content

Commit

Permalink
fix: change reward balance to optional
Browse files Browse the repository at this point in the history
Due to it being possible to run the node manager status command before any services have been
started, the reward balance should be an optional value, since the node's wallet has not been
created before it starts.
  • Loading branch information
jacderida committed May 1, 2024
1 parent 50321d4 commit 3716890
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
3 changes: 1 addition & 2 deletions sn_node_manager/src/add_services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use sn_service_management::{
control::ServiceControl, DaemonServiceData, FaucetServiceData, NodeRegistry, NodeServiceData,
ServiceStatus,
};
use sn_transfers::NanoTokens;
use std::{
ffi::OsString,
net::{IpAddr, Ipv4Addr, SocketAddr},
Expand Down Expand Up @@ -177,7 +176,7 @@ pub async fn add_node(
local: options.local,
log_dir_path: service_log_dir_path.clone(),
number: node_number,
reward_balance: NanoTokens::zero(),
reward_balance: None,
rpc_socket_addr,
peer_id: None,
pid: None,
Expand Down
4 changes: 2 additions & 2 deletions sn_node_manager/src/add_services/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ async fn add_genesis_node_should_return_an_error_if_there_is_already_a_genesis_n
number: 1,
pid: None,
peer_id: None,
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
status: ServiceStatus::Added,
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
Expand Down Expand Up @@ -787,7 +787,7 @@ async fn add_new_node_should_add_another_service() -> Result<()> {
number: 1,
pid: None,
peer_id: None,
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
service_name: "safenode1".to_string(),
Expand Down
50 changes: 29 additions & 21 deletions sn_node_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ pub async fn status_report(
.as_ref()
.map_or("-".to_string(), |p| p.len().to_string())
);
println!("Reward balance: {}", node.reward_balance);
println!(
"Reward balance: {}",
node.reward_balance
.map_or("-".to_string(), |b| b.to_string())
);
println!();
}

Expand Down Expand Up @@ -423,8 +427,12 @@ pub async fn refresh_node_registry(
}

for node in &mut node_registry.nodes {
let wallet = HotWallet::load_from(&node.data_dir_path)?;
node.reward_balance = wallet.balance();
// The `status` command can run before a node is started and therefore before its wallet
// exists.
match HotWallet::load_from(&node.data_dir_path) {
Ok(wallet) => node.reward_balance = Some(wallet.balance()),
Err(_) => node.reward_balance = None,
}

let rpc_client = RpcClient::from_socket_addr(node.rpc_socket_addr);
if let ServiceStatus::Running = node.status {
Expand Down Expand Up @@ -606,7 +614,7 @@ mod tests {
number: 1,
peer_id: None,
pid: None,
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -693,7 +701,7 @@ mod tests {
"12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR",
)?),
pid: None,
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -749,7 +757,7 @@ mod tests {
"12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR",
)?),
pid: Some(1000),
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -841,7 +849,7 @@ mod tests {
"12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR",
)?),
pid: Some(1000),
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -910,7 +918,7 @@ mod tests {
number: 1,
peer_id: None,
pid: None,
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -962,7 +970,7 @@ mod tests {
"12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR",
)?),
pid: Some(1000),
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -1001,7 +1009,7 @@ mod tests {
number: 1,
peer_id: None,
pid: None,
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -1042,7 +1050,7 @@ mod tests {
"12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR",
)?),
pid: None,
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -1082,7 +1090,7 @@ mod tests {
number: 1,
peer_id: None,
pid: None,
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -1199,7 +1207,7 @@ mod tests {
"12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR",
)?),
pid: Some(1000),
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: current_node_bin.to_path_buf(),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -1278,7 +1286,7 @@ mod tests {
"12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR",
)?),
pid: Some(1000),
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: current_node_bin.to_path_buf(),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -1399,7 +1407,7 @@ mod tests {
"12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR",
)?),
pid: Some(1000),
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: current_node_bin.to_path_buf(),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -1533,7 +1541,7 @@ mod tests {
"12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR",
)?),
pid: Some(1000),
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: current_node_bin.to_path_buf(),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -1663,7 +1671,7 @@ mod tests {
"12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR",
)?),
pid: Some(1000),
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: current_node_bin.to_path_buf(),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -1731,7 +1739,7 @@ mod tests {
number: 1,
pid: None,
peer_id: None,
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: safenode_bin.to_path_buf(),
status: ServiceStatus::Stopped,
Expand Down Expand Up @@ -1780,7 +1788,7 @@ mod tests {
peer_id: Some(PeerId::from_str(
"12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR",
)?),
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -1835,7 +1843,7 @@ mod tests {
peer_id: Some(PeerId::from_str(
"12D3KooWS2tpXGGTmg2AHFiDh57yPQnat49YHnyqoggzXZWpqkCR",
)?),
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: PathBuf::from("/var/safenode-manager/services/safenode1/safenode"),
service_name: "safenode1".to_string(),
Expand Down Expand Up @@ -1890,7 +1898,7 @@ mod tests {
number: 1,
pid: None,
peer_id: None,
reward_balance: NanoTokens::zero(),
reward_balance: Some(NanoTokens::zero()),
rpc_socket_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8081),
safenode_path: safenode_bin.to_path_buf(),
service_name: "safenode1".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions sn_node_manager/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use sn_service_management::{
rpc::{RpcActions, RpcClient},
FaucetServiceData, NodeRegistry, NodeServiceData, ServiceStatus,
};
use sn_transfers::{get_faucet_data_dir, NanoTokens};
use sn_transfers::get_faucet_data_dir;
use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
path::PathBuf,
Expand Down Expand Up @@ -347,7 +347,7 @@ pub async fn run_node(
number: run_options.number,
peer_id: Some(peer_id),
pid: Some(node_info.pid),
reward_balance: NanoTokens::zero(),
reward_balance: None,
rpc_socket_addr: run_options.rpc_socket_addr,
safenode_path: launcher.get_safenode_path(),
status: ServiceStatus::Running,
Expand Down
2 changes: 1 addition & 1 deletion sn_service_management/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub struct NodeServiceData {
)]
pub peer_id: Option<PeerId>,
pub pid: Option<u32>,
pub reward_balance: NanoTokens,
pub reward_balance: Option<NanoTokens>,
pub rpc_socket_addr: SocketAddr,
pub safenode_path: PathBuf,
pub service_name: String,
Expand Down

0 comments on commit 3716890

Please sign in to comment.