KillDB command

This commit is contained in:
arkpar 2016-12-12 17:19:41 +01:00
parent 6601fde328
commit edb853ca35
4 changed files with 35 additions and 5 deletions

View File

@ -64,11 +64,19 @@ impl FromStr for DataFormat {
#[derive(Debug, PartialEq)]
pub enum BlockchainCmd {
Kill(KillBlockchain),
Import(ImportBlockchain),
Export(ExportBlockchain),
ExportState(ExportState),
}
#[derive(Debug, PartialEq)]
pub struct KillBlockchain {
pub spec: SpecType,
pub dirs: Directories,
pub pruning: Pruning,
}
#[derive(Debug, PartialEq)]
pub struct ImportBlockchain {
pub spec: SpecType,
@ -128,6 +136,7 @@ pub struct ExportState {
pub fn execute(cmd: BlockchainCmd) -> Result<String, String> {
match cmd {
BlockchainCmd::Kill(kill_cmd) => kill_db(kill_cmd),
BlockchainCmd::Import(import_cmd) => execute_import(import_cmd),
BlockchainCmd::Export(export_cmd) => execute_export(export_cmd),
BlockchainCmd::ExportState(export_cmd) => execute_export_state(export_cmd),
@ -473,6 +482,18 @@ fn execute_export_state(cmd: ExportState) -> Result<String, String> {
Ok("Export completed.".into())
}
pub fn kill_db(cmd: KillBlockchain) -> Result<String, String> {
let spec = try!(cmd.spec.spec());
let genesis_hash = spec.genesis_header().hash();
let db_dirs = cmd.dirs.database(genesis_hash, None, spec.data_dir);
let user_defaults_path = db_dirs.user_defaults_path();
let user_defaults = try!(UserDefaults::load(&user_defaults_path));
let algorithm = cmd.pruning.to_algorithm(&user_defaults);
let dir = db_dirs.db_path(algorithm);
try!(fs::remove_dir_all(&dir).map_err(|e| format!("Error removing database: {:?}", e)));
Ok("Database deleted.".to_owned())
}
#[cfg(test)]
mod test {
use super::DataFormat;

View File

@ -36,6 +36,8 @@ usage! {
cmd_ui: bool,
cmd_tools: bool,
cmd_hash: bool,
cmd_kill: bool,
cmd_db: bool,
// Arguments
arg_pid_file: String,
@ -512,6 +514,8 @@ mod tests {
cmd_ui: false,
cmd_tools: false,
cmd_hash: false,
cmd_db: false,
cmd_kill: false,
// Arguments
arg_pid_file: "".into(),

View File

@ -15,6 +15,7 @@ Usage:
parity snapshot <file> [options]
parity restore [ <file> ] [options]
parity tools hash <file>
parity db kill [options]
Operating Options:
--mode MODE Set the operating mode. MODE can be one of:
@ -282,10 +283,8 @@ Import/Export Options:
(default: {flag_format:?} = Import: auto, Export: binary)
--no-seal-check Skip block seal check. (default: {flag_no_seal_check})
--at BLOCK Export state at the given block, which may be an
index, hash, or 'latest'. Note that taking snapshots at
non-recent blocks will only work with --pruning archive
(default: {flag_at})
--no-storage Don't export account storge. (default: {flag_no_storage})
index, hash, or 'latest'. (default: {flag_at})
--no-storage Don't export account storage. (default: {flag_no_storage})
--no-code Don't export account code. (default: {flag_no_code})
--min-balance WEI Don't export accounts with balance less than specified.
(default: {flag_min_balance:?})

View File

@ -38,7 +38,7 @@ use dir::Directories;
use dapps::Configuration as DappsConfiguration;
use signer::{Configuration as SignerConfiguration};
use run::RunCmd;
use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, ExportState, DataFormat};
use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, KillBlockchain, ExportState, DataFormat};
use presale::ImportWallet;
use account::{AccountCmd, NewAccount, ListAccounts, ImportAccounts, ImportFromGethAccounts};
use snapshot::{self, SnapshotCommand};
@ -107,6 +107,12 @@ impl Configuration {
Cmd::SignerToken(signer_conf)
} else if self.args.cmd_tools && self.args.cmd_hash {
Cmd::Hash(self.args.arg_file)
} else if self.args.cmd_db && self.args.cmd_kill {
Cmd::Blockchain(BlockchainCmd::Kill(KillBlockchain {
spec: spec,
dirs: dirs,
pruning: pruning,
}))
} else if self.args.cmd_account {
let account_cmd = if self.args.cmd_new {
let new_acc = NewAccount {