2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-07-25 16:09:47 +02:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-07-25 16:09:47 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-07-25 16:09:47 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-07-25 16:09:47 +02:00
|
|
|
|
2020-08-13 18:25:19 +02:00
|
|
|
use std::{fs, io, sync::Arc, time::Instant};
|
2019-11-11 21:57:38 +01:00
|
|
|
|
2018-01-10 13:35:18 +01:00
|
|
|
use ansi_term::Colour;
|
2017-09-06 20:47:45 +02:00
|
|
|
use bytes::ToPretty;
|
2016-07-25 16:09:47 +02:00
|
|
|
use cache::CacheConfig;
|
2016-10-03 11:13:10 +02:00
|
|
|
use db;
|
2016-07-25 16:09:47 +02:00
|
|
|
use dir::Directories;
|
2016-09-26 19:21:25 +02:00
|
|
|
use ethcore::{
|
2020-08-05 06:08:03 +02:00
|
|
|
client::{
|
2020-07-29 10:57:15 +02:00
|
|
|
Balance, BlockChainClient, BlockChainReset, BlockId, DatabaseCompactionProfile,
|
|
|
|
ImportExportBlocks, Mode, Nonce, VMType,
|
2020-08-05 06:08:03 +02:00
|
|
|
},
|
2016-07-25 16:09:47 +02:00
|
|
|
miner::Miner,
|
2020-07-29 10:57:15 +02:00
|
|
|
verification::queue::VerifierSettings,
|
2016-09-26 19:21:25 +02:00
|
|
|
};
|
2018-04-09 16:14:33 +02:00
|
|
|
use ethcore_private_tx;
|
2018-03-13 11:49:57 +01:00
|
|
|
use ethcore_service::ClientService;
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{Address, H256, U256};
|
2018-04-13 21:14:53 +02:00
|
|
|
use hash::{keccak, KECCAK_NULL_RLP};
|
2019-01-16 16:37:26 +01:00
|
|
|
use helpers::{execute_upgrades, to_client_config};
|
|
|
|
use informant::{FullNodeInformantData, Informant, MillisecondDuration};
|
|
|
|
use params::{fatdb_switch_to_bool, tracing_switch_to_bool, Pruning, SpecType, Switch};
|
2019-11-11 21:57:38 +01:00
|
|
|
use types::data_format::DataFormat;
|
2017-07-27 13:50:12 +02:00
|
|
|
use user_defaults::UserDefaults;
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum BlockchainCmd {
|
2016-12-12 17:19:41 +01:00
|
|
|
Kill(KillBlockchain),
|
2016-07-25 16:09:47 +02:00
|
|
|
Import(ImportBlockchain),
|
|
|
|
Export(ExportBlockchain),
|
2016-11-27 11:11:56 +01:00
|
|
|
ExportState(ExportState),
|
2019-01-16 16:37:26 +01:00
|
|
|
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,
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
2016-12-12 17:19:41 +01:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct KillBlockchain {
|
|
|
|
pub spec: SpecType,
|
|
|
|
pub dirs: Directories,
|
|
|
|
pub pruning: Pruning,
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct ImportBlockchain {
|
|
|
|
pub spec: SpecType,
|
|
|
|
pub cache_config: CacheConfig,
|
|
|
|
pub dirs: Directories,
|
|
|
|
pub file_path: Option<String>,
|
|
|
|
pub format: Option<DataFormat>,
|
|
|
|
pub pruning: Pruning,
|
2016-10-14 14:44:56 +02:00
|
|
|
pub pruning_history: u64,
|
2017-01-20 13:25:53 +01:00
|
|
|
pub pruning_memory: usize,
|
2016-07-25 16:09:47 +02:00
|
|
|
pub compaction: DatabaseCompactionProfile,
|
|
|
|
pub tracing: Switch,
|
2016-10-03 11:13:10 +02:00
|
|
|
pub fat_db: Switch,
|
2016-07-25 16:09:47 +02:00
|
|
|
pub vm_type: VMType,
|
2016-10-24 15:09:13 +02:00
|
|
|
pub check_seal: bool,
|
2016-11-02 19:42:21 +01:00
|
|
|
pub with_color: bool,
|
2016-12-02 18:21:54 +01:00
|
|
|
pub verifier_settings: VerifierSettings,
|
2018-10-26 13:21:36 +02:00
|
|
|
pub max_round_blocks_to_import: usize,
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct ExportBlockchain {
|
|
|
|
pub spec: SpecType,
|
|
|
|
pub cache_config: CacheConfig,
|
|
|
|
pub dirs: Directories,
|
|
|
|
pub file_path: Option<String>,
|
|
|
|
pub format: Option<DataFormat>,
|
|
|
|
pub pruning: Pruning,
|
2016-10-14 14:44:56 +02:00
|
|
|
pub pruning_history: u64,
|
2017-01-20 13:25:53 +01:00
|
|
|
pub pruning_memory: usize,
|
2016-07-25 16:09:47 +02:00
|
|
|
pub compaction: DatabaseCompactionProfile,
|
2016-10-03 11:13:10 +02:00
|
|
|
pub fat_db: Switch,
|
2016-07-25 16:09:47 +02:00
|
|
|
pub tracing: Switch,
|
2016-11-22 10:24:22 +01:00
|
|
|
pub from_block: BlockId,
|
|
|
|
pub to_block: BlockId,
|
2016-10-24 15:09:13 +02:00
|
|
|
pub check_seal: bool,
|
2018-10-26 13:21:36 +02:00
|
|
|
pub max_round_blocks_to_import: usize,
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
2016-11-27 11:11:56 +01:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct ExportState {
|
|
|
|
pub spec: SpecType,
|
|
|
|
pub cache_config: CacheConfig,
|
|
|
|
pub dirs: Directories,
|
|
|
|
pub file_path: Option<String>,
|
|
|
|
pub format: Option<DataFormat>,
|
|
|
|
pub pruning: Pruning,
|
|
|
|
pub pruning_history: u64,
|
2017-01-20 13:25:53 +01:00
|
|
|
pub pruning_memory: usize,
|
2016-11-27 11:11:56 +01:00
|
|
|
pub compaction: DatabaseCompactionProfile,
|
|
|
|
pub fat_db: Switch,
|
|
|
|
pub tracing: Switch,
|
2016-12-04 18:13:23 +01:00
|
|
|
pub at: BlockId,
|
2016-11-27 11:11:56 +01:00
|
|
|
pub storage: bool,
|
|
|
|
pub code: bool,
|
2016-11-27 18:16:43 +01:00
|
|
|
pub min_balance: Option<U256>,
|
|
|
|
pub max_balance: Option<U256>,
|
2018-10-26 13:21:36 +02:00
|
|
|
pub max_round_blocks_to_import: usize,
|
2016-11-27 11:11:56 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 11:00:17 +01:00
|
|
|
pub fn execute(cmd: BlockchainCmd) -> Result<(), String> {
|
2016-07-25 16:09:47 +02:00
|
|
|
match cmd {
|
2016-12-12 17:19:41 +01:00
|
|
|
BlockchainCmd::Kill(kill_cmd) => kill_db(kill_cmd),
|
2020-08-13 18:25:19 +02:00
|
|
|
BlockchainCmd::Import(import_cmd) => execute_import(import_cmd),
|
2016-07-25 16:09:47 +02:00
|
|
|
BlockchainCmd::Export(export_cmd) => execute_export(export_cmd),
|
2016-11-27 11:11:56 +01:00
|
|
|
BlockchainCmd::ExportState(export_cmd) => execute_export_state(export_cmd),
|
2019-01-16 16:37:26 +01:00
|
|
|
BlockchainCmd::Reset(reset_cmd) => execute_reset(reset_cmd),
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 11:00:17 +01:00
|
|
|
fn execute_import(cmd: ImportBlockchain) -> Result<(), String> {
|
2016-09-15 16:56:10 +02:00
|
|
|
let timer = Instant::now();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
// load spec file
|
2017-07-10 12:57:40 +02:00
|
|
|
let spec = cmd.spec.spec(&cmd.dirs.cache)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
// load genesis hash
|
|
|
|
let genesis_hash = spec.genesis_header().hash();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-09-26 19:21:25 +02:00
|
|
|
// database paths
|
2016-12-12 16:51:07 +01:00
|
|
|
let db_dirs = cmd.dirs.database(genesis_hash, None, spec.data_dir.clone());
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-09-26 19:21:25 +02:00
|
|
|
// user defaults path
|
|
|
|
let user_defaults_path = db_dirs.user_defaults_path();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-09-26 19:21:25 +02:00
|
|
|
// load user defaults
|
2016-12-27 12:53:56 +01:00
|
|
|
let mut user_defaults = UserDefaults::load(&user_defaults_path)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
// select pruning algorithm
|
2016-09-26 19:21:25 +02:00
|
|
|
let algorithm = cmd.pruning.to_algorithm(&user_defaults);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-03 11:13:10 +02:00
|
|
|
// check if tracing is on
|
2016-12-27 12:53:56 +01:00
|
|
|
let tracing = tracing_switch_to_bool(cmd.tracing, &user_defaults)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-03 11:13:10 +02:00
|
|
|
// check if fatdb is on
|
2016-12-27 12:53:56 +01:00
|
|
|
let fat_db = fatdb_switch_to_bool(cmd.fat_db, &user_defaults, algorithm)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-09-07 15:27:28 +02:00
|
|
|
// prepare client and snapshot paths.
|
2016-09-26 19:21:25 +02:00
|
|
|
let client_path = db_dirs.client_path(algorithm);
|
|
|
|
let snapshot_path = db_dirs.snapshot_path();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
// execute upgrades
|
2018-04-13 21:14:53 +02:00
|
|
|
execute_upgrades(&cmd.dirs.base, &db_dirs, algorithm, &cmd.compaction)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-13 23:38:29 +01:00
|
|
|
// create dirs used by parity
|
2018-07-11 12:19:54 +02:00
|
|
|
cmd.dirs.create_dirs(false, false)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
// prepare client config
|
2016-12-02 18:21:54 +01:00
|
|
|
let mut client_config = to_client_config(
|
2016-12-28 13:44:51 +01:00
|
|
|
&cmd.cache_config,
|
2017-03-13 12:10:53 +01:00
|
|
|
spec.name.to_lowercase(),
|
2016-12-28 13:44:51 +01:00
|
|
|
Mode::Active,
|
|
|
|
tracing,
|
|
|
|
fat_db,
|
|
|
|
cmd.compaction,
|
|
|
|
cmd.vm_type,
|
|
|
|
"".into(),
|
|
|
|
algorithm,
|
|
|
|
cmd.pruning_history,
|
2017-01-20 13:25:53 +01:00
|
|
|
cmd.pruning_memory,
|
2018-07-02 19:00:06 +02:00
|
|
|
cmd.check_seal,
|
2018-10-26 13:21:36 +02:00
|
|
|
12,
|
2016-12-02 18:21:54 +01:00
|
|
|
);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-02 18:21:54 +01:00
|
|
|
client_config.queue.verifier_settings = cmd.verifier_settings;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-04-13 21:14:53 +02:00
|
|
|
let restoration_db_handler = db::restoration_db_handler(&client_path, &client_config);
|
2018-06-20 15:13:07 +02:00
|
|
|
let client_db = restoration_db_handler
|
|
|
|
.open(&client_path)
|
|
|
|
.map_err(|e| format!("Failed to open database {:?}", e))?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
// build client
|
2016-12-27 12:53:56 +01:00
|
|
|
let service = ClientService::start(
|
2016-07-25 16:09:47 +02:00
|
|
|
client_config,
|
2016-08-05 23:33:55 +02:00
|
|
|
&spec,
|
2018-04-09 14:21:37 +02:00
|
|
|
client_db,
|
2016-09-07 15:27:28 +02:00
|
|
|
&snapshot_path,
|
2018-04-09 14:21:37 +02:00
|
|
|
restoration_db_handler,
|
2016-09-07 15:27:28 +02:00
|
|
|
&cmd.dirs.ipc_path(),
|
2018-04-13 17:34:27 +02:00
|
|
|
// TODO [ToDr] don't use test miner here
|
|
|
|
// (actually don't require miner at all)
|
|
|
|
Arc::new(Miner::new_for_tests(&spec, None)),
|
2019-02-07 14:34:24 +01:00
|
|
|
Arc::new(ethcore_private_tx::DummySigner),
|
2018-04-09 16:14:33 +02:00
|
|
|
Box::new(ethcore_private_tx::NoopEncryptor),
|
2019-02-07 12:39:04 +01:00
|
|
|
Default::default(),
|
2018-04-13 17:34:27 +02:00
|
|
|
Default::default(),
|
2016-12-27 12:53:56 +01:00
|
|
|
)
|
|
|
|
.map_err(|e| format!("Client service error: {:?}", e))?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-15 19:07:23 +01:00
|
|
|
// free up the spec in memory.
|
|
|
|
drop(spec);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
let client = service.client();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-11-11 21:57:38 +01:00
|
|
|
let instream: Box<dyn io::Read> = match cmd.file_path {
|
2016-12-27 12:53:56 +01:00
|
|
|
Some(f) => {
|
|
|
|
Box::new(fs::File::open(&f).map_err(|_| format!("Cannot open given file: {}", f))?)
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2016-07-25 16:09:47 +02:00
|
|
|
None => Box::new(io::stdin()),
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-07-10 13:21:11 +02:00
|
|
|
let informant = Arc::new(Informant::new(
|
|
|
|
FullNodeInformantData {
|
|
|
|
client: client.clone(),
|
|
|
|
sync: None,
|
|
|
|
net: None,
|
|
|
|
},
|
2020-08-05 06:08:03 +02:00
|
|
|
None,
|
2017-07-10 13:21:11 +02:00
|
|
|
None,
|
|
|
|
cmd.with_color,
|
|
|
|
));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-10 23:58:39 +01:00
|
|
|
service
|
|
|
|
.register_io_handler(informant)
|
|
|
|
.map_err(|_| "Unable to register informant handler".to_owned())?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-11-11 21:57:38 +01:00
|
|
|
client.import_blocks(instream, cmd.format)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-09-26 19:21:25 +02:00
|
|
|
// save user defaults
|
|
|
|
user_defaults.pruning = algorithm;
|
|
|
|
user_defaults.tracing = tracing;
|
2016-11-27 12:58:14 +01:00
|
|
|
user_defaults.fat_db = fat_db;
|
2016-12-27 12:53:56 +01:00
|
|
|
user_defaults.save(&user_defaults_path)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-09-15 16:56:10 +02:00
|
|
|
let report = client.report();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-09-15 16:56:10 +02:00
|
|
|
let ms = timer.elapsed().as_milliseconds();
|
2016-12-16 11:00:17 +01:00
|
|
|
info!("Import completed in {} seconds, {} blocks, {} blk/s, {} transactions, {} tx/s, {} Mgas, {} Mgas/s",
|
2016-09-15 16:56:10 +02:00
|
|
|
ms / 1000,
|
|
|
|
report.blocks_imported,
|
|
|
|
(report.blocks_imported * 1000) as u64 / ms,
|
|
|
|
report.transactions_applied,
|
|
|
|
(report.transactions_applied * 1000) as u64 / ms,
|
2018-09-04 20:13:51 +02:00
|
|
|
report.gas_processed / 1_000_000,
|
|
|
|
(report.gas_processed / (ms * 1000)).low_u64(),
|
2016-12-16 11:00:17 +01:00
|
|
|
);
|
|
|
|
Ok(())
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
2016-11-27 11:11:56 +01:00
|
|
|
fn start_client(
|
|
|
|
dirs: Directories,
|
|
|
|
spec: SpecType,
|
|
|
|
pruning: Pruning,
|
|
|
|
pruning_history: u64,
|
2017-01-20 13:25:53 +01:00
|
|
|
pruning_memory: usize,
|
2016-11-27 11:11:56 +01:00
|
|
|
tracing: Switch,
|
|
|
|
fat_db: Switch,
|
|
|
|
compaction: DatabaseCompactionProfile,
|
2017-06-27 22:40:46 +02:00
|
|
|
cache_config: CacheConfig,
|
|
|
|
require_fat_db: bool,
|
2018-10-26 13:21:36 +02:00
|
|
|
max_round_blocks_to_import: usize,
|
2016-12-04 18:01:50 +01:00
|
|
|
) -> Result<ClientService, String> {
|
2016-07-25 16:09:47 +02:00
|
|
|
// load spec file
|
2017-07-10 12:57:40 +02:00
|
|
|
let spec = spec.spec(&dirs.cache)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
// load genesis hash
|
|
|
|
let genesis_hash = spec.genesis_header().hash();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-09-26 19:21:25 +02:00
|
|
|
// database paths
|
2016-12-12 16:51:07 +01:00
|
|
|
let db_dirs = dirs.database(genesis_hash, None, spec.data_dir.clone());
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-09-26 19:21:25 +02:00
|
|
|
// user defaults path
|
|
|
|
let user_defaults_path = db_dirs.user_defaults_path();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-09-26 19:21:25 +02:00
|
|
|
// load user defaults
|
2016-12-27 12:53:56 +01:00
|
|
|
let user_defaults = UserDefaults::load(&user_defaults_path)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
// select pruning algorithm
|
2016-11-27 11:11:56 +01:00
|
|
|
let algorithm = pruning.to_algorithm(&user_defaults);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-03 11:13:10 +02:00
|
|
|
// check if tracing is on
|
2016-12-27 12:53:56 +01:00
|
|
|
let tracing = tracing_switch_to_bool(tracing, &user_defaults)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-03 11:13:10 +02:00
|
|
|
// check if fatdb is on
|
2016-12-27 12:53:56 +01:00
|
|
|
let fat_db = fatdb_switch_to_bool(fat_db, &user_defaults, algorithm)?;
|
2017-06-27 22:40:46 +02:00
|
|
|
if !fat_db && require_fat_db {
|
|
|
|
return Err("This command requires Parity to be synced with --fat-db on.".to_owned());
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-09-07 15:27:28 +02:00
|
|
|
// prepare client and snapshot paths.
|
2016-09-26 19:21:25 +02:00
|
|
|
let client_path = db_dirs.client_path(algorithm);
|
|
|
|
let snapshot_path = db_dirs.snapshot_path();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
// execute upgrades
|
2018-04-13 21:14:53 +02:00
|
|
|
execute_upgrades(&dirs.base, &db_dirs, algorithm, &compaction)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-13 23:38:29 +01:00
|
|
|
// create dirs used by parity
|
2018-07-11 12:19:54 +02:00
|
|
|
dirs.create_dirs(false, false)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
// prepare client config
|
2017-01-20 13:25:53 +01:00
|
|
|
let client_config = to_client_config(
|
|
|
|
&cache_config,
|
2017-03-13 12:10:53 +01:00
|
|
|
spec.name.to_lowercase(),
|
2017-01-20 13:25:53 +01:00
|
|
|
Mode::Active,
|
|
|
|
tracing,
|
|
|
|
fat_db,
|
|
|
|
compaction,
|
|
|
|
VMType::default(),
|
|
|
|
"".into(),
|
|
|
|
algorithm,
|
|
|
|
pruning_history,
|
|
|
|
pruning_memory,
|
|
|
|
true,
|
2018-10-26 13:21:36 +02:00
|
|
|
max_round_blocks_to_import,
|
2017-01-20 13:25:53 +01:00
|
|
|
);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-04-13 21:14:53 +02:00
|
|
|
let restoration_db_handler = db::restoration_db_handler(&client_path, &client_config);
|
2018-06-20 15:13:07 +02:00
|
|
|
let client_db = restoration_db_handler
|
|
|
|
.open(&client_path)
|
|
|
|
.map_err(|e| format!("Failed to open database {:?}", e))?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-27 12:53:56 +01:00
|
|
|
let service = ClientService::start(
|
2016-07-25 16:09:47 +02:00
|
|
|
client_config,
|
2016-08-05 23:33:55 +02:00
|
|
|
&spec,
|
2018-04-09 14:21:37 +02:00
|
|
|
client_db,
|
2016-09-07 15:27:28 +02:00
|
|
|
&snapshot_path,
|
2018-04-09 14:21:37 +02:00
|
|
|
restoration_db_handler,
|
2016-11-27 18:26:23 +01:00
|
|
|
&dirs.ipc_path(),
|
2018-04-13 17:34:27 +02:00
|
|
|
// It's fine to use test version here,
|
|
|
|
// since we don't care about miner parameters at all
|
|
|
|
Arc::new(Miner::new_for_tests(&spec, None)),
|
2019-02-07 14:34:24 +01:00
|
|
|
Arc::new(ethcore_private_tx::DummySigner),
|
2018-04-09 16:14:33 +02:00
|
|
|
Box::new(ethcore_private_tx::NoopEncryptor),
|
2019-02-07 12:39:04 +01:00
|
|
|
Default::default(),
|
2018-04-13 17:34:27 +02:00
|
|
|
Default::default(),
|
2016-12-27 12:53:56 +01:00
|
|
|
)
|
|
|
|
.map_err(|e| format!("Client service error: {:?}", e))?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-15 19:07:23 +01:00
|
|
|
drop(spec);
|
2016-11-27 11:11:56 +01:00
|
|
|
Ok(service)
|
|
|
|
}
|
|
|
|
|
2016-12-16 11:00:17 +01:00
|
|
|
fn execute_export(cmd: ExportBlockchain) -> Result<(), String> {
|
2017-01-20 13:25:53 +01:00
|
|
|
let service = start_client(
|
|
|
|
cmd.dirs,
|
|
|
|
cmd.spec,
|
|
|
|
cmd.pruning,
|
|
|
|
cmd.pruning_history,
|
|
|
|
cmd.pruning_memory,
|
|
|
|
cmd.tracing,
|
|
|
|
cmd.fat_db,
|
|
|
|
cmd.compaction,
|
2017-06-27 22:40:46 +02:00
|
|
|
cmd.cache_config,
|
|
|
|
false,
|
2018-10-26 13:21:36 +02:00
|
|
|
cmd.max_round_blocks_to_import,
|
2017-01-20 13:25:53 +01:00
|
|
|
)?;
|
2016-07-25 16:09:47 +02:00
|
|
|
let client = service.client();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-11-11 21:57:38 +01:00
|
|
|
let out: Box<dyn io::Write> = match cmd.file_path {
|
2016-12-27 12:53:56 +01:00
|
|
|
Some(f) => Box::new(
|
|
|
|
fs::File::create(&f).map_err(|_| format!("Cannot write to file given: {}", f))?,
|
|
|
|
),
|
2016-07-25 16:09:47 +02:00
|
|
|
None => Box::new(io::stdout()),
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-11-11 21:57:38 +01:00
|
|
|
client.export_blocks(out, cmd.from_block, cmd.to_block, cmd.format)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-16 11:00:17 +01:00
|
|
|
info!("Export completed.");
|
|
|
|
Ok(())
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
2016-12-16 11:00:17 +01:00
|
|
|
fn execute_export_state(cmd: ExportState) -> Result<(), String> {
|
2017-01-20 13:25:53 +01:00
|
|
|
let service = start_client(
|
|
|
|
cmd.dirs,
|
|
|
|
cmd.spec,
|
|
|
|
cmd.pruning,
|
|
|
|
cmd.pruning_history,
|
|
|
|
cmd.pruning_memory,
|
|
|
|
cmd.tracing,
|
|
|
|
cmd.fat_db,
|
|
|
|
cmd.compaction,
|
2017-06-27 22:40:46 +02:00
|
|
|
cmd.cache_config,
|
2018-10-26 13:21:36 +02:00
|
|
|
true,
|
|
|
|
cmd.max_round_blocks_to_import,
|
2017-01-20 13:25:53 +01:00
|
|
|
)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-27 11:11:56 +01:00
|
|
|
let client = service.client();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-11-11 21:57:38 +01:00
|
|
|
let mut out: Box<dyn io::Write> = match cmd.file_path {
|
2016-12-27 12:53:56 +01:00
|
|
|
Some(f) => Box::new(
|
|
|
|
fs::File::create(&f).map_err(|_| format!("Cannot write to file given: {}", f))?,
|
|
|
|
),
|
2016-11-27 11:11:56 +01:00
|
|
|
None => Box::new(io::stdout()),
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-27 11:11:56 +01:00
|
|
|
let mut last: Option<Address> = None;
|
|
|
|
let at = cmd.at;
|
|
|
|
let mut i = 0usize;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2017-04-25 19:08:28 +02:00
|
|
|
out.write_fmt(format_args!("{{ \"state\": {{",))
|
|
|
|
.expect("Couldn't write to stream.");
|
2016-11-27 11:11:56 +01:00
|
|
|
loop {
|
2016-12-27 12:53:56 +01:00
|
|
|
let accounts = client
|
|
|
|
.list_accounts(at, last.as_ref(), 1000)
|
|
|
|
.ok_or("Specified block not found")?;
|
2016-11-27 11:11:56 +01:00
|
|
|
if accounts.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-27 11:11:56 +01:00
|
|
|
for account in accounts.into_iter() {
|
2018-03-03 18:42:13 +01:00
|
|
|
let balance = client
|
|
|
|
.balance(&account, at.into())
|
|
|
|
.unwrap_or_else(U256::zero);
|
2016-11-27 18:16:43 +01:00
|
|
|
if cmd.min_balance.map_or(false, |m| balance < m)
|
|
|
|
|| cmd.max_balance.map_or(false, |m| balance > m)
|
|
|
|
{
|
2016-11-28 01:32:40 +01:00
|
|
|
last = Some(account);
|
2016-11-27 18:16:43 +01:00
|
|
|
continue; //filtered out
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-27 11:11:56 +01:00
|
|
|
if i != 0 {
|
|
|
|
out.write(b",").expect("Write error");
|
|
|
|
}
|
2018-02-09 09:32:06 +01:00
|
|
|
out.write_fmt(format_args!(
|
|
|
|
"\n\"0x{:x}\": {{\"balance\": \"{:x}\", \"nonce\": \"{:x}\"",
|
|
|
|
account,
|
|
|
|
balance,
|
|
|
|
client.nonce(&account, at).unwrap_or_else(U256::zero)
|
|
|
|
))
|
|
|
|
.expect("Write error");
|
2018-03-03 18:42:13 +01:00
|
|
|
let code = client
|
|
|
|
.code(&account, at.into())
|
|
|
|
.unwrap_or(None)
|
|
|
|
.unwrap_or_else(Vec::new);
|
2016-11-27 11:11:56 +01:00
|
|
|
if !code.is_empty() {
|
2018-02-09 09:32:06 +01:00
|
|
|
out.write_fmt(format_args!(", \"code_hash\": \"0x{:x}\"", keccak(&code)))
|
|
|
|
.expect("Write error");
|
2016-11-27 11:11:56 +01:00
|
|
|
if cmd.code {
|
|
|
|
out.write_fmt(format_args!(", \"code\": \"{}\"", code.to_hex()))
|
|
|
|
.expect("Write error");
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2017-08-31 11:35:41 +02:00
|
|
|
let storage_root = client.storage_root(&account, at).unwrap_or(KECCAK_NULL_RLP);
|
|
|
|
if storage_root != KECCAK_NULL_RLP {
|
2018-02-09 09:32:06 +01:00
|
|
|
out.write_fmt(format_args!(", \"storage_root\": \"0x{:x}\"", storage_root))
|
|
|
|
.expect("Write error");
|
2016-11-27 11:11:56 +01:00
|
|
|
if cmd.storage {
|
2016-11-27 11:57:05 +01:00
|
|
|
out.write_fmt(format_args!(", \"storage\": {{"))
|
|
|
|
.expect("Write error");
|
|
|
|
let mut last_storage: Option<H256> = None;
|
|
|
|
loop {
|
2016-12-27 12:53:56 +01:00
|
|
|
let keys = client
|
|
|
|
.list_storage(at, &account, last_storage.as_ref(), 1000)
|
|
|
|
.ok_or("Specified block not found")?;
|
2016-11-27 11:57:05 +01:00
|
|
|
if keys.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-27 11:57:05 +01:00
|
|
|
for key in keys.into_iter() {
|
2017-04-25 19:08:28 +02:00
|
|
|
if last_storage.is_some() {
|
2016-11-27 11:57:05 +01:00
|
|
|
out.write(b",").expect("Write error");
|
|
|
|
}
|
2018-03-03 18:42:13 +01:00
|
|
|
out.write_fmt(format_args!(
|
|
|
|
"\n\t\"0x{:x}\": \"0x{:x}\"",
|
|
|
|
key,
|
|
|
|
client
|
|
|
|
.storage_at(&account, &key, at.into())
|
|
|
|
.unwrap_or_else(Default::default)
|
|
|
|
))
|
|
|
|
.expect("Write error");
|
2016-11-27 11:57:05 +01:00
|
|
|
last_storage = Some(key);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2016-11-27 11:57:05 +01:00
|
|
|
out.write(b"\n}").expect("Write error");
|
2016-11-27 11:11:56 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2016-11-27 11:11:56 +01:00
|
|
|
out.write(b"}").expect("Write error");
|
|
|
|
i += 1;
|
|
|
|
if i % 10000 == 0 {
|
2016-11-27 18:16:43 +01:00
|
|
|
info!("Account #{}", i);
|
2016-11-27 11:11:56 +01:00
|
|
|
}
|
|
|
|
last = Some(account);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2017-04-25 19:08:28 +02:00
|
|
|
out.write_fmt(format_args!("\n}}}}")).expect("Write error");
|
2016-12-16 11:00:17 +01:00
|
|
|
info!("Export completed.");
|
|
|
|
Ok(())
|
2016-11-27 11:11:56 +01:00
|
|
|
}
|
|
|
|
|
2019-01-16 16:37:26 +01:00
|
|
|
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,
|
|
|
|
)?;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-01-16 16:37:26 +01:00
|
|
|
let client = service.client();
|
|
|
|
client.reset(cmd.num)?;
|
|
|
|
info!("{}", Colour::Green.bold().paint("Successfully reset db!"));
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-01-16 16:37:26 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-12-16 11:00:17 +01:00
|
|
|
pub fn kill_db(cmd: KillBlockchain) -> Result<(), String> {
|
2017-07-10 12:57:40 +02:00
|
|
|
let spec = cmd.spec.spec(&cmd.dirs.cache)?;
|
2016-12-12 17:19:41 +01:00
|
|
|
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();
|
2017-07-18 15:38:38 +02:00
|
|
|
let mut user_defaults = UserDefaults::load(&user_defaults_path)?;
|
2016-12-12 17:19:41 +01:00
|
|
|
let algorithm = cmd.pruning.to_algorithm(&user_defaults);
|
|
|
|
let dir = db_dirs.db_path(algorithm);
|
2016-12-27 12:53:56 +01:00
|
|
|
fs::remove_dir_all(&dir).map_err(|e| format!("Error removing database: {:?}", e))?;
|
2017-07-18 15:38:38 +02:00
|
|
|
user_defaults.is_first_launch = true;
|
|
|
|
user_defaults.save(&user_defaults_path)?;
|
2016-12-16 11:00:17 +01:00
|
|
|
info!("Database deleted.");
|
|
|
|
Ok(())
|
2016-12-12 17:19:41 +01:00
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::DataFormat;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[test]
|
|
|
|
fn test_data_format_parsing() {
|
|
|
|
assert_eq!(DataFormat::Binary, "binary".parse().unwrap());
|
|
|
|
assert_eq!(DataFormat::Binary, "bin".parse().unwrap());
|
|
|
|
assert_eq!(DataFormat::Hex, "hex".parse().unwrap());
|
|
|
|
}
|
|
|
|
}
|