Skip to content

Commit

Permalink
move pilot functions to own module
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed May 8, 2021
1 parent c9ef7b3 commit 6ac915f
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 266 deletions.
212 changes: 5 additions & 207 deletions ol/cli/src/commands/pilot_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@
#![allow(clippy::never_loop)]
use super::OlCliCmd;
use crate::node::states::*;
use crate::{
pilot,
entrypoint,
mgmt::{
self,
management::NodeMode::*,
},
node::client,
node::node::Node,
prelude::app_config,
};
use abscissa_core::{status_err, status_info, status_ok, status_warn, Command, Options, Runnable};
use libra_types::waypoint::Waypoint;
use std::{thread, time::Duration};
use abscissa_core::{Command, Options, Runnable};

/// `version` subcommand
/// `pilot` subcommand
#[derive(Command, Debug, Default, Options)]
pub struct PilotCmd {}

Expand All @@ -36,206 +30,10 @@ impl Runnable for PilotCmd {
cfg.workspace.node_home = tp;
}
let mut node = Node::new(client, cfg.clone());
pilot_db(&mut node, verbose);
pilot::maybe_restore_db(&mut node, verbose);

loop {
pilot_once(&mut node, wp, verbose);
pilot::run_once(&mut node, wp, verbose);
}
}
}

pub fn pilot_db(mut node: &mut Node, verbose: bool) -> &mut Node {
let cfg = node.conf.to_owned();
// let wp = node.client.waypoint().unwrap().to_owned();
// Abort if the database is not set correctly.
node.vitals.host_state.onboard_state = OnboardState::EmptyBox;

if node.db_files_exist() {
node.vitals.host_state.onboard_state = OnboardState::DbFilesOk;

if verbose {
status_ok!("DB", "db files exist");
}
// is DB bootstrapped
if node.db_bootstrapped() {
node.vitals.host_state.onboard_state = OnboardState::DbBootstrapOk;
if verbose {
status_ok!("DB", "db bootstrapped");
}
} else {
if verbose {
status_err!("libraDB is not bootstrapped. Database needs a valid set of transactions to boot. Attempting `ol restore` to fetch backups from archive.");
}
mgmt::restore::fast_forward_db(true).unwrap();
node.vitals.host_state.onboard_state = OnboardState::DbBootstrapOk;
}
// return
} else {
if verbose {
status_err!(
"NO db files found {:?}. Attempting `ol restore` to fetch backups from archive.",
&cfg.workspace.node_home
);
}
mgmt::restore::fast_forward_db(true).unwrap();
node.vitals.host_state.onboard_state = OnboardState::DbBootstrapOk;
}
node
}

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;
}

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")
}
}
}

// is node started?
if Node::node_running() {
if verbose {
status_ok!("Node", "node is running");
}
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);
}

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");
}
if !Node::node_running() {
if verbose {
status_err!("Node not running. Cannot start miner if node is not running");
}
}
// did the node finish sync?
let sync_tup = Node::cold_start_is_synced(&cfg, wp);
if sync_tup.0 {
if verbose {
status_ok!("Sync", "node is synced");
}

// does the account exist on chain? otherwise sending mining txs will fail
if node.accounts_exist_on_chain() {
if verbose {
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?")
}
}
} else {
if verbose {
status_warn!("node is NOT Synced");
}
}
}
thread::sleep(Duration::from_millis(10_000));

node
}

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 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 NodeState::ValidatorMode;
}

// INCORRECT CASE 1: Need to change mode from Fullnode to Validator mode
if !running_in_val_mode && is_in_val_set {
status_warn!("Mode: running the INCORRECT mode, switching to VALIDATOR mode");
node.stop_node();
node.start_node(Validator).expect("could not start node");

return NodeState::ValidatorMode;
}

// INCORRECT CASE 2: Need to change mode from Validator to Fullnode mode
if running_in_val_mode && !is_in_val_set {
status_warn!("Mode: running the INCORRECT mode, switching to FULLNODE mode");
node.stop_node();
node.start_node(Validator).expect("could not start node");

return NodeState::FullnodeMode;
}

NodeState::Stopped
}
3 changes: 1 addition & 2 deletions ol/cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
//! `config` module for all 0L settings
pub use ol_types
::config::AppCfg;
pub use ol_types::config::AppCfg;
3 changes: 2 additions & 1 deletion ol/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ pub mod cache;
pub mod check;
pub mod explorer;
pub mod server;
pub mod migrate;
pub mod migrate;
pub mod pilot;
56 changes: 0 additions & 56 deletions ol/cli/src/mgmt/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,6 @@ pub struct HostProcess {
pids: HashSet<u32>,
}

/// Check if we are in prod mode
// pub static IS_PROD: Lazy<bool> = Lazy::new(|| {
// match env::var("NODE_ENV") {
// Ok(val) => {
// match val.as_str() {
// "prod" => true,
// // if anything else is set by user is false
// _ => false,
// }
// }
// // default to prod if nothig is set
// _ => true,
// }
// });

// TODO: do we need to kill zombies this way?

/// create log files
pub fn create_log_file(file_name: &str) -> File {
let conf = app_config();
Expand Down Expand Up @@ -79,45 +62,6 @@ fn spawn_process(
.expect(expect_msg)
}

// /// start validator wizard
// pub fn run_validator_wizard() -> bool {
// println!("Running validator wizard");
// let entry_arg = entrypoint::get_args();

// let mut child = if *IS_PROD {
// Command::new("miner")
// .arg("val-wizard")
// .spawn()
// .expect(&format!("failed to find 'miner', is it installed?"))
// } else if let Some(path) = entry_arg.swarm_path {
// // we are testing with swarm
// let swarm_arg = path.to_str().unwrap();
// let swarm_persona = entry_arg.swarm_persona.unwrap();

// Command::new("cargo")
// .args(&["r", "-p", "miner", "--"])
// .arg("--swarm-path")
// .arg(swarm_arg)
// .arg("--swarm-persona")
// .arg(swarm_persona)
// .arg("val-wizard")
// .spawn()
// .expect(&format!("failed to run cargo r -p miner"))
// } else {
// // we are testing on devnet
// Command::new("cargo")
// .args(&["r", "-p", "miner", "--"])
// .arg("val-wizard")
// .spawn()
// .expect(&format!("failed to run cargo r -p miner"))
// };

// let exit_code = child.wait().expect("failed to wait on miner");
// assert!(exit_code.success());

// true
// }

impl Node {
/// Start Node, as fullnode
pub fn start_node(&mut self, config_type: NodeMode) -> Result<(), Error> {
Expand Down
Loading

0 comments on commit 6ac915f

Please sign in to comment.