Skip to content

Commit

Permalink
refactor pilot functions
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed May 8, 2021
1 parent 0b9e43e commit c9ef7b3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
52 changes: 40 additions & 12 deletions ol/cli/src/commands/pilot_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
entrypoint,
mgmt::{
self,
management::NodeMode::{self, *},
management::NodeMode::*,
},
node::client,
node::node::Node,
Expand Down Expand Up @@ -83,33 +83,40 @@ 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 {
println!("==========================================");
}
// 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")
}
Expand All @@ -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");
Expand All @@ -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?")
Expand All @@ -172,23 +198,25 @@ 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<NodeMode> {
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);

let running_in_val_mode = running_mode == Validator;
// 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
Expand All @@ -197,7 +225,7 @@ fn maybe_switch_mode(node: &mut Node, is_in_val_set: bool) -> Option<NodeMode> {
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
Expand All @@ -206,8 +234,8 @@ fn maybe_switch_mode(node: &mut Node, is_in_val_set: bool) -> Option<NodeMode> {
node.stop_node();
node.start_node(Validator).expect("could not start node");

return Some(Fullnode);
return NodeState::FullnodeMode;
}

None
NodeState::Stopped
}
2 changes: 1 addition & 1 deletion ol/cli/src/mgmt/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 24 additions & 2 deletions ol/cli/src/node/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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,
}
}
}

0 comments on commit c9ef7b3

Please sign in to comment.