Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Revert chain command (#393)
Browse files Browse the repository at this point in the history
* Revert chain command

* BLOCKS -> NUM

* Fixed warning
  • Loading branch information
arkpar authored and gavofyork committed Jul 24, 2018
1 parent 0f710a1 commit d4696b9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
17 changes: 17 additions & 0 deletions cli/src/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 24 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ pub fn run<I, T, W>(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);

Expand All @@ -248,7 +252,7 @@ pub fn run<I, T, W>(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()))?),
};
Expand Down Expand Up @@ -495,6 +499,25 @@ fn import_blocks<E>(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<C, W>(
runtime: &mut Runtime,
service: service::Service<C>,
Expand Down

0 comments on commit d4696b9

Please sign in to comment.