Adds cli interface to allow reseting chain to a particular block (#9782)
* added BlockChainReset trait, client impl, and cli interface * show block hashes to be deleted and new best block, update best block in db, better cli interface * delete BlockNumber from COL_EXTRA * add TODO comment * add BlockReciepts to imports * refactor block_headers_from_best_block, better cli documentation * exit gracefully if reset arg isn't supplied * fix cli usage macro * removed stray int literals * use Vec::with_capacity Co-Authored-By: seunlanlege <seunlanlege@gmail.com> * cast n to usize * correct imports * make db reset arg required
This commit is contained in:
committed by
Afri Schoedon
parent
1df6361753
commit
cdba22a2cb
@@ -26,7 +26,8 @@ use ethereum_types::{U256, H256, Address};
|
||||
use bytes::ToPretty;
|
||||
use rlp::PayloadInfo;
|
||||
use ethcore::account_provider::AccountProvider;
|
||||
use ethcore::client::{Mode, DatabaseCompactionProfile, VMType, Nonce, Balance, BlockChainClient, BlockId, BlockInfo, ImportBlock};
|
||||
use ethcore::client::{Mode, DatabaseCompactionProfile, VMType, Nonce, Balance, BlockChainClient, BlockId, BlockInfo,
|
||||
ImportBlock, BlockChainReset};
|
||||
use ethcore::error::{ImportErrorKind, ErrorKind as EthcoreErrorKind, Error as EthcoreError};
|
||||
use ethcore::miner::Miner;
|
||||
use ethcore::verification::queue::VerifierSettings;
|
||||
@@ -40,6 +41,7 @@ use dir::Directories;
|
||||
use user_defaults::UserDefaults;
|
||||
use ethcore_private_tx;
|
||||
use db;
|
||||
use ansi_term::Colour;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum DataFormat {
|
||||
@@ -71,6 +73,21 @@ pub enum BlockchainCmd {
|
||||
Import(ImportBlockchain),
|
||||
Export(ExportBlockchain),
|
||||
ExportState(ExportState),
|
||||
Reset(ResetBlockchain)
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct ResetBlockchain {
|
||||
pub dirs: Directories,
|
||||
pub spec: SpecType,
|
||||
pub pruning: Pruning,
|
||||
pub pruning_history: u64,
|
||||
pub pruning_memory: usize,
|
||||
pub tracing: Switch,
|
||||
pub fat_db: Switch,
|
||||
pub compaction: DatabaseCompactionProfile,
|
||||
pub cache_config: CacheConfig,
|
||||
pub num: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -153,6 +170,7 @@ pub fn execute(cmd: BlockchainCmd) -> Result<(), String> {
|
||||
}
|
||||
BlockchainCmd::Export(export_cmd) => execute_export(export_cmd),
|
||||
BlockchainCmd::ExportState(export_cmd) => execute_export_state(export_cmd),
|
||||
BlockchainCmd::Reset(reset_cmd) => execute_reset(reset_cmd),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -709,6 +727,28 @@ fn execute_export_state(cmd: ExportState) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn execute_reset(cmd: ResetBlockchain) -> Result<(), String> {
|
||||
let service = start_client(
|
||||
cmd.dirs,
|
||||
cmd.spec,
|
||||
cmd.pruning,
|
||||
cmd.pruning_history,
|
||||
cmd.pruning_memory,
|
||||
cmd.tracing,
|
||||
cmd.fat_db,
|
||||
cmd.compaction,
|
||||
cmd.cache_config,
|
||||
false,
|
||||
0,
|
||||
)?;
|
||||
|
||||
let client = service.client();
|
||||
client.reset(cmd.num)?;
|
||||
info!("{}", Colour::Green.bold().paint("Successfully reset db!"));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn kill_db(cmd: KillBlockchain) -> Result<(), String> {
|
||||
let spec = cmd.spec.spec(&cmd.dirs.cache)?;
|
||||
let genesis_hash = spec.genesis_header().hash();
|
||||
|
||||
@@ -217,6 +217,15 @@ usage! {
|
||||
CMD cmd_db_kill {
|
||||
"Clean the database of the given --chain (default: mainnet)",
|
||||
}
|
||||
|
||||
CMD cmd_db_reset {
|
||||
"Removes NUM latests blocks from the db",
|
||||
|
||||
ARG arg_db_reset_num: (u32) = 10u32,
|
||||
"<NUM>",
|
||||
"Number of blocks to revert",
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
CMD cmd_export_hardcoded_sync
|
||||
@@ -1612,6 +1621,7 @@ mod tests {
|
||||
cmd_tools_hash: false,
|
||||
cmd_db: false,
|
||||
cmd_db_kill: false,
|
||||
cmd_db_reset: false,
|
||||
cmd_export_hardcoded_sync: false,
|
||||
|
||||
// Arguments
|
||||
@@ -1631,6 +1641,7 @@ mod tests {
|
||||
arg_dapp_path: None,
|
||||
arg_account_import_path: None,
|
||||
arg_wallet_import_path: None,
|
||||
arg_db_reset_num: 10,
|
||||
|
||||
// -- Operating Options
|
||||
arg_mode: "last".into(),
|
||||
|
||||
@@ -48,7 +48,7 @@ use ethcore_private_tx::{ProviderConfig, EncryptorConfig};
|
||||
use secretstore::{NodeSecretKey, Configuration as SecretStoreConfiguration, ContractAddress as SecretStoreContractAddress};
|
||||
use updater::{UpdatePolicy, UpdateFilter, ReleaseTrack};
|
||||
use run::RunCmd;
|
||||
use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, KillBlockchain, ExportState, DataFormat};
|
||||
use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, KillBlockchain, ExportState, DataFormat, ResetBlockchain};
|
||||
use export_hardcoded_sync::ExportHsyncCmd;
|
||||
use presale::ImportWallet;
|
||||
use account::{AccountCmd, NewAccount, ListAccounts, ImportAccounts, ImportFromGethAccounts};
|
||||
@@ -176,6 +176,19 @@ impl Configuration {
|
||||
}
|
||||
} else if self.args.cmd_tools && self.args.cmd_tools_hash {
|
||||
Cmd::Hash(self.args.arg_tools_hash_file)
|
||||
} else if self.args.cmd_db && self.args.cmd_db_reset {
|
||||
Cmd::Blockchain(BlockchainCmd::Reset(ResetBlockchain {
|
||||
dirs,
|
||||
spec,
|
||||
pruning,
|
||||
pruning_history,
|
||||
pruning_memory: self.args.arg_pruning_memory,
|
||||
tracing,
|
||||
fat_db,
|
||||
compaction,
|
||||
cache_config,
|
||||
num: self.args.arg_db_reset_num,
|
||||
}))
|
||||
} else if self.args.cmd_db && self.args.cmd_db_kill {
|
||||
Cmd::Blockchain(BlockchainCmd::Kill(KillBlockchain {
|
||||
spec: spec,
|
||||
|
||||
Reference in New Issue
Block a user