-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1123 from dusk-network/embed-recovery
Embed rusk-recovery binary
- Loading branch information
Showing
9 changed files
with
218 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
use clap::builder::BoolishValueParser; | ||
use clap::Subcommand; | ||
use rusk_recovery_tools::Theme; | ||
use std::io; | ||
use tracing::info; | ||
|
||
#[allow(clippy::large_enum_variant)] | ||
#[derive(PartialEq, Eq, Hash, Clone, Subcommand, Debug)] | ||
pub enum Command { | ||
#[cfg(feature = "recovery-keys")] | ||
RecoveryKeys { | ||
/// Keeps untracked keys | ||
#[clap(short, long, value_parser = BoolishValueParser::new(), env = "RUSK_KEEP_KEYS")] | ||
keep: bool, | ||
}, | ||
|
||
#[cfg(feature = "recovery-state")] | ||
RecoveryState { | ||
/// Forces a build/download even if the state is in the profile path. | ||
#[clap(short = 'f', value_parser = BoolishValueParser::new(), long, env = "RUSK_FORCE_STATE")] | ||
force: bool, | ||
|
||
/// Create a state applying the init config specified in this file. | ||
#[clap(short, long, value_parser, env = "RUSK_RECOVERY_INPUT")] | ||
init: Option<super::PathBuf>, | ||
|
||
/// If specified, the generated state is written on this file instead | ||
/// of save the state in the profile path. | ||
#[clap(short, long, value_parser, num_args(1))] | ||
output: Option<super::PathBuf>, | ||
}, | ||
} | ||
|
||
impl Command { | ||
fn display_env(theme: &Theme) -> io::Result<()> { | ||
let profile_dir = rusk_profile::get_rusk_profile_dir()?; | ||
let circuits_dir = rusk_profile::get_rusk_circuits_dir()?; | ||
let keys_dir = rusk_profile::get_rusk_keys_dir()?; | ||
let state_dir = rusk_profile::get_rusk_state_dir()?; | ||
|
||
info!("{} {}", theme.info("PROFILE"), profile_dir.display()); | ||
info!("{} {}", theme.info("CIRCUITS"), circuits_dir.display()); | ||
info!("{} {}", theme.info("KEYS"), keys_dir.display()); | ||
info!("{} {}", theme.info("STATE"), state_dir.display()); | ||
Ok(()) | ||
} | ||
|
||
pub fn run(self) -> Result<(), Box<dyn std::error::Error>> { | ||
let theme = Theme::default(); | ||
|
||
Self::display_env(&theme)?; | ||
|
||
let result = match self { | ||
#[cfg(feature = "recovery-state")] | ||
Self::RecoveryState { | ||
force, | ||
init, | ||
output, | ||
} => super::state::recovery_state(init, force, output), | ||
#[cfg(feature = "recovery-keys")] | ||
Self::RecoveryKeys { keep } => { | ||
rusk_recovery_tools::keys::exec(keep) | ||
} | ||
}; | ||
|
||
if let Err(e) = &result { | ||
tracing::error!("{} {e}", theme.error("Error")); | ||
} | ||
|
||
result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
use super::*; | ||
|
||
use std::{env, fs, io}; | ||
|
||
use rusk_recovery_tools::state::{deploy, restore_state, tar}; | ||
use rusk_recovery_tools::Theme; | ||
use tracing::info; | ||
|
||
pub fn recovery_state( | ||
init: Option<PathBuf>, | ||
force: bool, | ||
output_file: Option<PathBuf>, | ||
) -> Result<(), Box<dyn std::error::Error>> { | ||
let config = match &init { | ||
Some(path) => fs::read_to_string(path) | ||
.map_err(|_| format!("file {path:?} not found"))?, | ||
None => rusk_recovery_tools::state::DEFAULT_SNAPSHOT.into(), | ||
}; | ||
let init = toml::from_str(&config)?; | ||
|
||
let theme = Theme::default(); | ||
info!("{} Network state", theme.action("Checking")); | ||
|
||
let _tmpdir = match output_file.clone() { | ||
Some(output) if output.exists() => Err("Output already exists")?, | ||
Some(_) => { | ||
let tmp_dir = tempfile::tempdir()?; | ||
env::set_var("RUSK_STATE_PATH", tmp_dir.path()); | ||
Some(tmp_dir) | ||
} | ||
None => None, | ||
}; | ||
|
||
if force { | ||
clean_state()?; | ||
} | ||
|
||
let state_dir = rusk_profile::get_rusk_state_dir()?; | ||
let state_id_path = rusk_profile::to_rusk_state_id_path(&state_dir); | ||
|
||
let _ = rusk_abi::new_vm(&state_dir)?; | ||
|
||
// if the state already exists in the expected path, stop early. | ||
if state_dir.exists() && state_id_path.exists() { | ||
info!("{} existing state", theme.info("Found")); | ||
|
||
let (_, commit_id) = restore_state(state_dir)?; | ||
info!( | ||
"{} state id at {}", | ||
theme.success("Checked"), | ||
state_id_path.display() | ||
); | ||
info!("{} {}", theme.action("Root"), hex::encode(commit_id)); | ||
|
||
return Ok(()); | ||
} | ||
|
||
info!("{} new state", theme.info("Building")); | ||
|
||
let (_, commit_id) = deploy(&state_dir, &init)?; | ||
|
||
info!("{} {}", theme.action("Final Root"), hex::encode(commit_id)); | ||
|
||
info!( | ||
"{} network state at {}", | ||
theme.success("Stored"), | ||
state_dir.display() | ||
); | ||
info!( | ||
"{} persisted id at {}", | ||
theme.success("Stored"), | ||
state_id_path.display() | ||
); | ||
|
||
if let Some(output) = output_file { | ||
let state_folder = rusk_profile::get_rusk_state_dir()?; | ||
info!( | ||
"{} state into {}", | ||
theme.info("Compressing"), | ||
output.display() | ||
); | ||
tar::archive(&state_folder, &output)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn clean_state() -> Result<(), io::Error> { | ||
let state_path = rusk_profile::get_rusk_state_dir()?; | ||
|
||
fs::remove_dir_all(state_path).or_else(|e| { | ||
if e.kind() == io::ErrorKind::NotFound { | ||
Ok(()) | ||
} else { | ||
Err(e) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters