Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions substrate/cli/src/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,17 @@ subcommands:
value_name: PATH
help: Specify custom base path.
takes_value: true
- purge-chain:
about: Remove the whole chain data.
args:
- 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
31 changes: 31 additions & 0 deletions substrate/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use network::NonReservedPeerMode;

use std::io::{Write, Read, stdin, stdout};
use std::iter;
use std::fs;
use std::fs::File;
use std::net::{Ipv4Addr, SocketAddr};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -214,6 +215,12 @@ where
return Ok(Action::ExecutedInternally);
}

if let Some(matches) = matches.subcommand_matches("purge-chain") {
let spec = load_spec(&matches, spec_factory)?;
purge_chain::<F>(matches, spec)?;
return Ok(Action::ExecutedInternally);
}

let spec = load_spec(&matches, spec_factory)?;
let mut config = service::Configuration::default_with_spec(spec);

Expand Down Expand Up @@ -418,6 +425,30 @@ fn revert_chain<F>(matches: &clap::ArgMatches, spec: ChainSpec<FactoryGenesis<F>
Ok(service::chain_ops::revert_chain::<F>(config, As::sa(blocks))?)
}

fn purge_chain<F>(matches: &clap::ArgMatches, spec: ChainSpec<FactoryGenesis<F>>) -> error::Result<()>
where F: ServiceFactory,
{
let base_path = base_path(matches);
let database_path = db_path(&base_path, spec.id());

print!("Are you sure to remove {:?}? (y/n)", &database_path);
stdout().flush().expect("failed to flush stdout");

let mut input = String::new();
stdin().read_line(&mut input)?;
let input = input.trim();

match input.chars().nth(0) {
Some('y') | Some('Y') => {
fs::remove_dir_all(&database_path)?;
println!("{:?} removed.", &database_path);
},
_ => println!("Aborted"),
}

Ok(())
}

fn parse_address(default: &str, port_param: &str, matches: &clap::ArgMatches) -> Result<SocketAddr, String> {
let mut address: SocketAddr = default.parse().ok().ok_or_else(|| format!("Invalid address specified for --{}.", port_param))?;
if let Some(port) = matches.value_of(port_param) {
Expand Down