From c9ef7b321ec65f5156748749b71bed64fa57a857 Mon Sep 17 00:00:00 2001 From: lpgeiger <1364012+lpgeiger@users.noreply.github.com> Date: Sat, 8 May 2021 15:59:10 +0000 Subject: [PATCH] refactor pilot functions --- ol/cli/src/commands/pilot_cmd.rs | 52 ++++++++++++++++++++++++-------- ol/cli/src/mgmt/management.rs | 2 +- ol/cli/src/node/states.rs | 26 ++++++++++++++-- 3 files changed, 65 insertions(+), 15 deletions(-) diff --git a/ol/cli/src/commands/pilot_cmd.rs b/ol/cli/src/commands/pilot_cmd.rs index d2297254e3..acea341aaf 100644 --- a/ol/cli/src/commands/pilot_cmd.rs +++ b/ol/cli/src/commands/pilot_cmd.rs @@ -7,7 +7,7 @@ use crate::{ entrypoint, mgmt::{ self, - management::NodeMode::{self, *}, + management::NodeMode::*, }, node::client, node::node::Node, @@ -83,7 +83,7 @@ pub fn pilot_db(mut node: &mut Node, verbose: bool) -> &mut Node { node } -pub fn pilot_once(mut node: &mut Node, wp: Waypoint, verbose: bool) { +pub fn pilot_once(mut node: &mut Node, wp: Waypoint, verbose: bool) -> &mut Node{ let cfg = node.conf.to_owned(); if verbose { @@ -91,25 +91,32 @@ pub fn pilot_once(mut node: &mut Node, wp: Waypoint, verbose: bool) { } // Start the webserver before anything else if Node::is_web_monitor_serving() { + node.vitals.host_state.monitor_state = MonitorState::Serving; + if verbose { status_ok!("Web", "web monitor is serving on 3030"); } } else { + node.vitals.host_state.monitor_state = MonitorState::Stopped; + if verbose { status_warn!("web monitor is NOT serving 3030. Attempting start."); } node.start_monitor(); + node.vitals.host_state.monitor_state = MonitorState::Serving; } - // exit if cannot connect to any client, local or upstream. let is_in_val_set = node.refresh_onchain_state().is_in_validator_set(); match is_in_val_set { true => { + node.vitals.host_state.account_state = AccountState::InSet; if verbose { status_ok!("Node", "account is in validator set") } } false => { + // TODO: we don't know if the account exists from the is_in_validator_set check + node.vitals.host_state.account_state = AccountState::None; if verbose { status_warn!("Node: account is NOT in validator set") } @@ -121,23 +128,40 @@ pub fn pilot_once(mut node: &mut Node, wp: Waypoint, verbose: bool) { if verbose { status_ok!("Node", "node is running"); } - maybe_switch_mode(&mut node, is_in_val_set); + node.vitals.host_state.node_state = maybe_switch_mode(&mut node, is_in_val_set); } else { let start_mode = if is_in_val_set { Validator } else { Fullnode }; if verbose { - status_warn!("node is NOT running, starting in {:?} mode", start_mode); + status_warn!("node is NOT running, starting in {:?} mode", &start_mode); } - node.start_node(start_mode).expect("could not start node"); + node.vitals.host_state.node_state = match node.start_node(start_mode.clone()) { + Ok(_) => { + + match &start_mode { + Validator => NodeState::ValidatorMode, + Fullnode => NodeState::FullnodeMode, + } + + } + Err(_) => { + if verbose { + status_warn!(&format!("could not start node in: {:?}", &start_mode)); + } + NodeState::Stopped + } + } } //////// MINER RULES //////// if Node::miner_running() { + node.vitals.host_state.miner_state = MinerState::Mining; if verbose { status_ok!("Miner", "miner is running") } } else { + node.vitals.host_state.miner_state = MinerState::Stopped; if verbose { status_warn!("miner is NOT running"); status_info!("Miner", "will try to start miner"); @@ -160,6 +184,8 @@ pub fn pilot_once(mut node: &mut Node, wp: Waypoint, verbose: bool) { status_ok!("Account", "owner account found on chain. Starting miner"); } node.start_miner(); + node.vitals.host_state.miner_state = MinerState::Mining; + } else { if verbose { status_warn!("error trying to start miner. Owner account does NOT exist on chain. Was the account creation transaction submitted?") @@ -172,9 +198,11 @@ pub fn pilot_once(mut node: &mut Node, wp: Waypoint, verbose: bool) { } } thread::sleep(Duration::from_millis(10_000)); + + node } -fn maybe_switch_mode(node: &mut Node, is_in_val_set: bool) -> Option { +fn maybe_switch_mode(node: &mut Node, is_in_val_set: bool) -> NodeState { let running_mode = Node::what_node_mode().expect("could not detect node mode"); status_ok!("Mode", "node running in mode: {:?}", running_mode); @@ -182,13 +210,13 @@ fn maybe_switch_mode(node: &mut Node, is_in_val_set: bool) -> Option { // Running correctly as a FULLNODE if !running_in_val_mode && !is_in_val_set { status_ok!("Mode", "running the correct mode {:?}. Noop.", running_mode); - return None; + return NodeState::FullnodeMode; } // Running correctly as a VALIDATOR // Do nothing, the account is in validator set, and we are running as a validator if running_in_val_mode && is_in_val_set { status_ok!("Mode", "running the correct mode {:?}. Noop.", running_mode); - return None; + return NodeState::ValidatorMode; } // INCORRECT CASE 1: Need to change mode from Fullnode to Validator mode @@ -197,7 +225,7 @@ fn maybe_switch_mode(node: &mut Node, is_in_val_set: bool) -> Option { node.stop_node(); node.start_node(Validator).expect("could not start node"); - return Some(Validator); + return NodeState::ValidatorMode; } // INCORRECT CASE 2: Need to change mode from Validator to Fullnode mode @@ -206,8 +234,8 @@ fn maybe_switch_mode(node: &mut Node, is_in_val_set: bool) -> Option { node.stop_node(); node.start_node(Validator).expect("could not start node"); - return Some(Fullnode); + return NodeState::FullnodeMode; } - None + NodeState::Stopped } diff --git a/ol/cli/src/mgmt/management.rs b/ol/cli/src/mgmt/management.rs index c95e121ffb..ca6c10248c 100644 --- a/ol/cli/src/mgmt/management.rs +++ b/ol/cli/src/mgmt/management.rs @@ -16,7 +16,7 @@ use std::{ const BINARY_NODE: &str = "libra-node"; const BINARY_MINER: &str = "miner"; -#[derive(Debug, PartialEq)] +#[derive(Debug, Clone, PartialEq)] /// What kind of node are we starting pub enum NodeMode { /// Validator diff --git a/ol/cli/src/node/states.rs b/ol/cli/src/node/states.rs index 360d2cc007..405454add7 100644 --- a/ol/cli/src/node/states.rs +++ b/ol/cli/src/node/states.rs @@ -10,6 +10,8 @@ pub enum AccountState { None, /// account created on chain ExistsOnChain, + /// account in val set + InSet, } /// Events that can be taken on accounts @@ -83,11 +85,14 @@ pub enum NodeEvents { #[serde(deny_unknown_fields)] /// All states a miner can be in pub enum MinerState { - /// Miner connected to upstream + /// Miner stopped Stopped, - /// Miner connected to upstream + /// Miner running Mining, } + + + #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] #[serde(deny_unknown_fields)] /// Actions that impact the miner @@ -98,6 +103,17 @@ pub enum MinerEvents { Failed, } + +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +#[serde(deny_unknown_fields)] +/// All states the web-monitor can be in +pub enum MonitorState { + /// Monitor stopped + Stopped, + /// Monitor serving + Serving, +} + #[derive(Clone, Debug, Deserialize, Serialize)] /// The Current state of a node pub struct HostState { @@ -107,6 +123,10 @@ pub struct HostState { pub node_state: NodeState, /// state of miner pub miner_state: MinerState, + /// state of monitor + pub monitor_state: MonitorState, + /// state of account + pub account_state: AccountState, } /// methods for host state @@ -117,6 +137,8 @@ impl HostState { onboard_state: OnboardState::EmptyBox, node_state: NodeState::Stopped, miner_state: MinerState::Stopped, + monitor_state: MonitorState::Stopped, + account_state: AccountState::None, } } }