use crate::error;
use tc_service::{PruningMode, Role, KeepBlocks};
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
pub struct PruningParams {
#[structopt(long = "pruning", value_name = "PRUNING_MODE")]
pub pruning: Option<String>,
#[structopt(long, value_name = "COUNT")]
pub keep_blocks: Option<u32>,
}
impl PruningParams {
pub fn state_pruning(&self, unsafe_pruning: bool, role: &Role) -> error::Result<PruningMode> {
Ok(match &self.pruning {
Some(ref s) if s == "archive" => PruningMode::ArchiveAll,
None if role.is_network_authority() => PruningMode::ArchiveAll,
None => PruningMode::default(),
Some(s) => {
if role.is_network_authority() && !unsafe_pruning {
return Err(error::Error::Input(
"Validators should run with state pruning disabled (i.e. archive). \
You can ignore this check with `--unsafe-pruning`."
.to_string(),
));
}
PruningMode::keep_blocks(s.parse().map_err(|_| {
error::Error::Input("Invalid pruning mode specified".to_string())
})?)
}
})
}
pub fn keep_blocks(&self) -> error::Result<KeepBlocks> {
Ok(match self.keep_blocks {
Some(n) => KeepBlocks::Some(n),
None => KeepBlocks::All,
})
}
}