2020-01-17 14:27:28 +01:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
2020-03-05 12:19:39 +01:00
|
|
|
// This file is part of Open Ethereum.
|
2016-07-25 16:09:47 +02:00
|
|
|
|
2020-03-05 12:19:39 +01:00
|
|
|
// Open 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.
|
|
|
|
|
2020-03-05 12:19:39 +01:00
|
|
|
// Open 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
|
2020-03-05 12:19:39 +01:00
|
|
|
// along with Open Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-07-25 16:09:47 +02:00
|
|
|
|
2019-09-09 13:46:05 +02:00
|
|
|
use std::str::from_utf8;
|
2016-07-25 16:09:47 +02:00
|
|
|
use std::{io, fs};
|
|
|
|
use std::io::{BufReader, BufRead};
|
2016-09-15 16:56:10 +02:00
|
|
|
use std::time::{Instant, Duration};
|
2016-07-25 16:09:47 +02:00
|
|
|
use std::thread::sleep;
|
|
|
|
use std::sync::Arc;
|
2019-07-18 12:27:08 +02:00
|
|
|
|
2017-07-06 11:36:15 +02:00
|
|
|
use rustc_hex::FromHex;
|
2017-08-31 11:35:41 +02:00
|
|
|
use hash::{keccak, KECCAK_NULL_RLP};
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{U256, H256, Address};
|
2017-09-06 20:47:45 +02:00
|
|
|
use bytes::ToPretty;
|
2016-09-01 14:55:07 +02:00
|
|
|
use rlp::PayloadInfo;
|
2019-10-03 16:52:29 +02:00
|
|
|
use client_traits::{BlockChainReset, Nonce, Balance, BlockChainClient, ImportExportBlocks};
|
2019-08-15 17:59:22 +02:00
|
|
|
use ethcore::{
|
2019-11-20 04:11:36 +01:00
|
|
|
client::{DatabaseCompactionProfile},
|
2019-08-15 17:59:22 +02:00
|
|
|
miner::Miner,
|
2019-02-07 14:34:24 +01:00
|
|
|
};
|
2018-03-13 11:49:57 +01:00
|
|
|
use ethcore_service::ClientService;
|
2016-07-25 16:09:47 +02:00
|
|
|
use cache::CacheConfig;
|
2019-10-28 15:03:28 +01:00
|
|
|
use informant::{Informant, FullNodeInformantData};
|
2016-10-03 11:13:10 +02:00
|
|
|
use params::{SpecType, Pruning, Switch, tracing_switch_to_bool, fatdb_switch_to_bool};
|
2018-04-13 21:14:53 +02:00
|
|
|
use helpers::{to_client_config, execute_upgrades};
|
2016-07-25 16:09:47 +02:00
|
|
|
use dir::Directories;
|
2016-09-26 19:21:25 +02:00
|
|
|
use user_defaults::UserDefaults;
|
2018-04-09 16:14:33 +02:00
|
|
|
use ethcore_private_tx;
|
2018-04-13 21:14:53 +02:00
|
|
|
use db;
|
2019-01-16 16:37:26 +01:00
|
|
|
use ansi_term::Colour;
|
2019-08-13 12:33:34 +02:00
|
|
|
use types::{
|
|
|
|
ids::BlockId,
|
2019-08-15 17:59:22 +02:00
|
|
|
errors::{ImportError, EthcoreError},
|
2019-10-03 16:52:29 +02:00
|
|
|
client_types::{Mode, StateResult},
|
2019-08-13 12:33:34 +02:00
|
|
|
};
|
2019-09-09 13:46:05 +02:00
|
|
|
use types::data_format::DataFormat;
|
2019-08-28 10:09:42 +02:00
|
|
|
use verification::queue::VerifierSettings;
|
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-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,
|
2017-07-27 13:50:12 +02:00
|
|
|
pub light: bool,
|
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),
|
2017-07-27 13:50:12 +02:00
|
|
|
BlockchainCmd::Import(import_cmd) => {
|
|
|
|
if import_cmd.light {
|
|
|
|
execute_import_light(import_cmd)
|
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 13:50:12 +02:00
|
|
|
fn execute_import_light(cmd: ImportBlockchain) -> Result<(), String> {
|
|
|
|
use light::client::{Service as LightClientService, Config as LightClientConfig};
|
|
|
|
use light::cache::Cache as LightDataCache;
|
2017-09-02 20:09:13 +02:00
|
|
|
use parking_lot::Mutex;
|
2017-07-27 13:50:12 +02:00
|
|
|
|
|
|
|
let timer = Instant::now();
|
|
|
|
|
|
|
|
// load spec file
|
|
|
|
let spec = cmd.spec.spec(&cmd.dirs.cache)?;
|
|
|
|
|
|
|
|
// load genesis hash
|
|
|
|
let genesis_hash = spec.genesis_header().hash();
|
|
|
|
|
|
|
|
// database paths
|
|
|
|
let db_dirs = cmd.dirs.database(genesis_hash, None, spec.data_dir.clone());
|
|
|
|
|
|
|
|
// user defaults path
|
|
|
|
let user_defaults_path = db_dirs.user_defaults_path();
|
|
|
|
|
|
|
|
// load user defaults
|
|
|
|
let user_defaults = UserDefaults::load(&user_defaults_path)?;
|
|
|
|
|
|
|
|
// select pruning algorithm
|
|
|
|
let algorithm = cmd.pruning.to_algorithm(&user_defaults);
|
|
|
|
|
|
|
|
// prepare client and snapshot paths.
|
|
|
|
let client_path = db_dirs.client_path(algorithm);
|
|
|
|
|
|
|
|
// execute upgrades
|
2018-04-13 21:14:53 +02:00
|
|
|
execute_upgrades(&cmd.dirs.base, &db_dirs, algorithm, &cmd.compaction)?;
|
2017-07-27 13:50:12 +02:00
|
|
|
|
|
|
|
// create dirs used by parity
|
2018-07-11 12:19:54 +02:00
|
|
|
cmd.dirs.create_dirs(false, false)?;
|
2017-07-27 13:50:12 +02:00
|
|
|
|
2017-09-02 20:09:13 +02:00
|
|
|
let cache = Arc::new(Mutex::new(
|
2018-03-14 12:29:52 +01:00
|
|
|
LightDataCache::new(Default::default(), Duration::new(0, 0))
|
2017-07-27 13:50:12 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
let mut config = LightClientConfig {
|
|
|
|
queue: Default::default(),
|
2019-01-04 14:05:46 +01:00
|
|
|
chain_column: ethcore_db::COL_LIGHT_CHAIN,
|
2017-07-27 13:50:12 +02:00
|
|
|
verify_full: true,
|
|
|
|
check_seal: cmd.check_seal,
|
2018-03-27 13:56:59 +02:00
|
|
|
no_hardcoded_sync: true,
|
2017-07-27 13:50:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
config.queue.max_mem_use = cmd.cache_config.queue() as usize * 1024 * 1024;
|
|
|
|
config.queue.verifier_settings = cmd.verifier_settings;
|
|
|
|
|
2018-03-01 19:53:15 +01:00
|
|
|
// initialize database.
|
2019-12-03 16:59:11 +01:00
|
|
|
let db = db::open_db_light(
|
|
|
|
&client_path.to_str().expect("DB path could not be converted to string."),
|
|
|
|
&cmd.cache_config,
|
|
|
|
&cmd.compaction,
|
|
|
|
).map_err(|e| format!("Failed to open database: {:?}", e))?;
|
2018-03-01 19:53:15 +01:00
|
|
|
|
2019-08-28 10:09:42 +02:00
|
|
|
// TODO: could epoch signals be available at the end of the file?
|
2017-09-05 17:54:05 +02:00
|
|
|
let fetch = ::light::client::fetch::unavailable();
|
2018-03-01 19:53:15 +01:00
|
|
|
let service = LightClientService::start(config, &spec, fetch, db, cache)
|
2017-07-27 13:50:12 +02:00
|
|
|
.map_err(|e| format!("Failed to start client: {}", e))?;
|
|
|
|
|
|
|
|
// free up the spec in memory.
|
|
|
|
drop(spec);
|
|
|
|
|
|
|
|
let client = service.client();
|
|
|
|
|
2019-08-27 17:29:33 +02:00
|
|
|
let mut instream: Box<dyn io::Read> = match cmd.file_path {
|
2017-07-27 13:50:12 +02:00
|
|
|
Some(f) => Box::new(fs::File::open(&f).map_err(|_| format!("Cannot open given file: {}", f))?),
|
|
|
|
None => Box::new(io::stdin()),
|
|
|
|
};
|
|
|
|
|
|
|
|
const READAHEAD_BYTES: usize = 8;
|
|
|
|
|
|
|
|
let mut first_bytes: Vec<u8> = vec![0; READAHEAD_BYTES];
|
|
|
|
let mut first_read = 0;
|
|
|
|
|
|
|
|
let format = match cmd.format {
|
|
|
|
Some(format) => format,
|
|
|
|
None => {
|
|
|
|
first_read = instream.read(&mut first_bytes).map_err(|_| "Error reading from the file/stream.")?;
|
|
|
|
match first_bytes[0] {
|
|
|
|
0xf9 => DataFormat::Binary,
|
|
|
|
_ => DataFormat::Hex,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let do_import = |bytes: Vec<u8>| {
|
|
|
|
while client.queue_info().is_full() { sleep(Duration::from_secs(1)); }
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
let header: ::types::header::Header = ::rlp::Rlp::new(&bytes).val_at(0)
|
2017-07-27 13:50:12 +02:00
|
|
|
.map_err(|e| format!("Bad block: {}", e))?;
|
|
|
|
|
|
|
|
if client.best_block_header().number() >= header.number() { return Ok(()) }
|
|
|
|
|
|
|
|
if header.number() % 10000 == 0 {
|
|
|
|
info!("#{}", header.number());
|
|
|
|
}
|
|
|
|
|
|
|
|
match client.import_header(header) {
|
2019-05-06 15:06:20 +02:00
|
|
|
Err(EthcoreError::Import(ImportError::AlreadyInChain)) => {
|
2017-07-27 13:50:12 +02:00
|
|
|
trace!("Skipping block already in chain.");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
return Err(format!("Cannot import block: {:?}", e));
|
|
|
|
},
|
|
|
|
Ok(_) => {},
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
match format {
|
|
|
|
DataFormat::Binary => {
|
|
|
|
loop {
|
|
|
|
let mut bytes = if first_read > 0 {first_bytes.clone()} else {vec![0; READAHEAD_BYTES]};
|
|
|
|
let n = if first_read > 0 {
|
|
|
|
first_read
|
|
|
|
} else {
|
|
|
|
instream.read(&mut bytes).map_err(|_| "Error reading from the file/stream.")?
|
|
|
|
};
|
|
|
|
if n == 0 { break; }
|
|
|
|
first_read = 0;
|
|
|
|
let s = PayloadInfo::from(&bytes).map_err(|e| format!("Invalid RLP in the file/stream: {:?}", e))?.total();
|
|
|
|
bytes.resize(s, 0);
|
|
|
|
instream.read_exact(&mut bytes[n..]).map_err(|_| "Error reading from the file/stream.")?;
|
|
|
|
do_import(bytes)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DataFormat::Hex => {
|
|
|
|
for line in BufReader::new(instream).lines() {
|
|
|
|
let s = line.map_err(|_| "Error reading from the file/stream.")?;
|
|
|
|
let s = if first_read > 0 {from_utf8(&first_bytes).unwrap().to_owned() + &(s[..])} else {s};
|
|
|
|
first_read = 0;
|
|
|
|
let bytes = s.from_hex().map_err(|_| "Invalid hex in file/stream.")?;
|
|
|
|
do_import(bytes)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client.flush_queue();
|
|
|
|
|
2019-10-28 15:03:28 +01:00
|
|
|
let elapsed = timer.elapsed();
|
2017-07-27 13:50:12 +02:00
|
|
|
let report = client.report();
|
|
|
|
|
|
|
|
info!("Import completed in {} seconds, {} headers, {} hdr/s",
|
2019-10-28 15:03:28 +01:00
|
|
|
elapsed.as_secs(),
|
2017-07-27 13:50:12 +02:00
|
|
|
report.blocks_imported,
|
2019-10-28 15:03:28 +01:00
|
|
|
(report.blocks_imported as u128 * 1000) / elapsed.as_millis(),
|
2017-07-27 13:50:12 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
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)?;
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
// load genesis hash
|
|
|
|
let genesis_hash = spec.genesis_header().hash();
|
|
|
|
|
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());
|
2016-09-26 19:21:25 +02:00
|
|
|
|
|
|
|
// user defaults path
|
|
|
|
let user_defaults_path = db_dirs.user_defaults_path();
|
|
|
|
|
|
|
|
// load user defaults
|
2016-12-27 12:53:56 +01:00
|
|
|
let mut user_defaults = UserDefaults::load(&user_defaults_path)?;
|
2016-09-26 19:21:25 +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);
|
2016-07-25 16:09:47 +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)?;
|
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)?;
|
2016-10-03 11:13:10 +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();
|
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)?;
|
2016-07-25 16:09:47 +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)?;
|
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,
|
|
|
|
"".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
|
|
|
);
|
|
|
|
|
|
|
|
client_config.queue.verifier_settings = cmd.verifier_settings;
|
2016-07-25 16:09:47 +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))?;
|
2018-04-09 14:21:37 +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),
|
2018-04-13 17:34:27 +02:00
|
|
|
Default::default(),
|
2019-02-07 12:39:04 +01:00
|
|
|
Default::default(),
|
2016-12-27 12:53:56 +01:00
|
|
|
).map_err(|e| format!("Client service error: {:?}", e))?;
|
2016-07-25 16:09:47 +02:00
|
|
|
|
2016-11-15 19:07:23 +01:00
|
|
|
// free up the spec in memory.
|
|
|
|
drop(spec);
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
let client = service.client();
|
|
|
|
|
2019-09-09 13:46:05 +02: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))?),
|
2016-07-25 16:09:47 +02:00
|
|
|
None => Box::new(io::stdin()),
|
|
|
|
};
|
|
|
|
|
2017-07-10 13:21:11 +02:00
|
|
|
let informant = Arc::new(Informant::new(
|
|
|
|
FullNodeInformantData {
|
|
|
|
client: client.clone(),
|
|
|
|
sync: None,
|
|
|
|
net: None,
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
cmd.with_color,
|
|
|
|
));
|
|
|
|
|
2016-12-10 23:58:39 +01:00
|
|
|
service.register_io_handler(informant).map_err(|_| "Unable to register informant handler".to_owned())?;
|
2016-09-11 14:04:56 +02:00
|
|
|
|
2019-09-09 13:46:05 +02:00
|
|
|
client.import_blocks(instream, cmd.format)?;
|
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)?;
|
2016-09-26 19:21:25 +02:00
|
|
|
|
2016-09-15 16:56:10 +02:00
|
|
|
let report = client.report();
|
2019-10-28 15:03:28 +01:00
|
|
|
let elapsed = timer.elapsed();
|
|
|
|
let ms = timer.elapsed().as_millis();
|
2016-12-16 11:00:17 +01:00
|
|
|
info!("Import completed in {} seconds, {} blocks, {} blk/s, {} transactions, {} tx/s, {} Mgas, {} Mgas/s",
|
2019-10-28 15:03:28 +01:00
|
|
|
elapsed.as_secs(),
|
2016-09-15 16:56:10 +02:00
|
|
|
report.blocks_imported,
|
2019-10-28 15:03:28 +01:00
|
|
|
(report.blocks_imported as u128 * 1000) / ms,
|
2016-09-15 16:56:10 +02:00
|
|
|
report.transactions_applied,
|
2019-10-28 15:03:28 +01:00
|
|
|
(report.transactions_applied as u128 * 1000) / ms,
|
2018-09-04 20:13:51 +02:00
|
|
|
report.gas_processed / 1_000_000,
|
2019-10-28 15:03:28 +01:00
|
|
|
report.gas_processed / (ms * 1000),
|
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)?;
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
// load genesis hash
|
|
|
|
let genesis_hash = spec.genesis_header().hash();
|
|
|
|
|
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());
|
2016-09-26 19:21:25 +02:00
|
|
|
|
|
|
|
// user defaults path
|
|
|
|
let user_defaults_path = db_dirs.user_defaults_path();
|
|
|
|
|
|
|
|
// load user defaults
|
2016-12-27 12:53:56 +01:00
|
|
|
let user_defaults = UserDefaults::load(&user_defaults_path)?;
|
2016-09-26 19:21:25 +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);
|
2016-07-25 16:09:47 +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)?;
|
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());
|
|
|
|
}
|
2016-10-03 11:13:10 +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();
|
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)?;
|
2016-07-25 16:09:47 +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)?;
|
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,
|
|
|
|
"".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
|
|
|
);
|
2016-07-25 16:09:47 +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))?;
|
2018-04-09 14:21:37 +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),
|
2018-04-13 17:34:27 +02:00
|
|
|
Default::default(),
|
2019-02-07 12:39:04 +01:00
|
|
|
Default::default(),
|
2016-12-27 12:53:56 +01:00
|
|
|
).map_err(|e| format!("Client service error: {:?}", e))?;
|
2016-07-25 16:09:47 +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();
|
|
|
|
|
2019-09-09 13:46:05 +02: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()),
|
|
|
|
};
|
|
|
|
|
2019-09-09 13:46:05 +02:00
|
|
|
client.export_blocks(out, cmd.from_block, cmd.to_block, cmd.format)?;
|
2016-07-25 16:09:47 +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
|
|
|
)?;
|
|
|
|
|
2016-11-27 11:11:56 +01:00
|
|
|
let client = service.client();
|
|
|
|
|
2019-08-27 17:29:33 +02: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()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut last: Option<Address> = None;
|
|
|
|
let at = cmd.at;
|
|
|
|
let mut i = 0usize;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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");
|
2019-10-03 13:21:26 +02:00
|
|
|
let code = match client.code(&account, at.into()) {
|
|
|
|
StateResult::Missing => Vec::new(),
|
|
|
|
StateResult::Some(t) => t.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");
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2019-10-04 14:38:57 +02:00
|
|
|
let keys = client.list_storage(at, &account, last_storage.as_ref(), Some(1000)).ok_or("Specified block not found")?;
|
2016-11-27 11:57:05 +01:00
|
|
|
if keys.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out.write(b"\n}").expect("Write error");
|
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);
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let client = service.client();
|
|
|
|
client.reset(cmd.num)?;
|
|
|
|
info!("{}", Colour::Green.bold().paint("Successfully reset db!"));
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
#[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());
|
|
|
|
}
|
|
|
|
}
|