From f8a85231f1d8d5fee2d03974a51d9500c65c51b5 Mon Sep 17 00:00:00 2001 From: Arkadiy Paronyan Date: Tue, 24 Jul 2018 15:54:34 +0200 Subject: [PATCH] Revert chain command (#393) * Revert chain command * BLOCKS -> NUM * Fixed warning --- polkadot/cli/src/cli.yml | 17 +++++++++++++++++ polkadot/cli/src/lib.rs | 25 ++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/polkadot/cli/src/cli.yml b/polkadot/cli/src/cli.yml index be0d0e501b..d8e2797970 100644 --- a/polkadot/cli/src/cli.yml +++ b/polkadot/cli/src/cli.yml @@ -178,3 +178,20 @@ subcommands: long: max-heap-pages value_name: COUNT help: The maximum number of 64KB pages to ever allocate for Wasm execution. Don't alter this unless you know what you're doing. + - revert: + about: Revert chain to the previous state + args: + - NUM: + index: 1 + help: Number of blocks to revert. Default is 256. + - chain: + long: chain + value_name: CHAIN_SPEC + help: Specify the chain specification. + takes_value: true + - base-path: + long: base-path + short: d + value_name: PATH + help: Specify custom base path. + takes_value: true diff --git a/polkadot/cli/src/lib.rs b/polkadot/cli/src/lib.rs index 9e84660c3f..de8872472f 100644 --- a/polkadot/cli/src/lib.rs +++ b/polkadot/cli/src/lib.rs @@ -223,6 +223,10 @@ pub fn run(args: I, worker: W) -> error::Result<()> where return import_blocks(matches, worker.exit_only()); } + if let Some(matches) = matches.subcommand_matches("revert") { + return revert_chain(matches); + } + let (spec, is_global) = load_spec(&matches)?; let mut config = service::Configuration::default_with_spec(spec); @@ -248,7 +252,7 @@ pub fn run(args: I, worker: W) -> error::Result<()> where config.pruning = match matches.value_of("pruning") { Some("archive") => PruningMode::ArchiveAll, - None => PruningMode::keep_blocks(256), + None => PruningMode::default(), Some(s) => PruningMode::keep_blocks(s.parse() .map_err(|_| error::ErrorKind::Input("Invalid pruning mode specified".to_owned()))?), }; @@ -495,6 +499,25 @@ fn import_blocks(matches: &clap::ArgMatches, exit: E) -> error::Result<()> Ok(()) } +fn revert_chain(matches: &clap::ArgMatches) -> error::Result<()> { + let (spec, _) = load_spec(&matches)?; + let base_path = base_path(matches); + let mut config = service::Configuration::default_with_spec(spec); + config.database_path = db_path(&base_path, config.chain_spec.id()).to_string_lossy().into(); + + let client = service::new_client(config)?; + + let blocks = match matches.value_of("NUM") { + Some(v) => v.parse().map_err(|_| "Invalid block count specified")?, + None => 256, + }; + + let reverted = client.revert(blocks)?; + let info = client.info()?.chain; + info!("Reverted {} blocks. Best: #{} ({})", reverted, info.best_number, info.best_hash); + Ok(()) +} + fn run_until_exit( runtime: &mut Runtime, service: service::Service,