2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-07-25 16:09:47 +02:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum 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-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum 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-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
use cache::CacheConfig;
|
2018-04-13 21:14:53 +02:00
|
|
|
use db::migrate;
|
2017-03-22 06:23:40 +01:00
|
|
|
use dir::{helpers::replace_home, DatabaseDirectories};
|
|
|
|
use ethcore::{
|
|
|
|
client::{BlockId, ClientConfig, DatabaseCompactionProfile, Mode, VMType, VerifierType},
|
|
|
|
miner::{Penalization, PendingSet},
|
2020-08-05 06:08:03 +02:00
|
|
|
};
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::{clean_0x, Address, U256};
|
2018-06-22 15:09:15 +02:00
|
|
|
use ethkey::Password;
|
2017-10-17 06:41:05 +02:00
|
|
|
use journaldb::Algorithm;
|
2018-04-13 17:34:27 +02:00
|
|
|
use miner::pool::PrioritizationStrategy;
|
2016-11-12 10:06:40 +01:00
|
|
|
use std::{
|
2019-01-28 11:26:11 +01:00
|
|
|
collections::HashSet,
|
2020-08-05 06:08:03 +02:00
|
|
|
fs::File,
|
|
|
|
io,
|
2016-11-12 10:06:40 +01:00
|
|
|
io::{BufRead, BufReader, Write},
|
2016-07-25 16:09:47 +02:00
|
|
|
time::Duration,
|
2020-08-05 06:08:03 +02:00
|
|
|
};
|
2018-04-10 12:13:49 +02:00
|
|
|
use sync::{self, validate_node_url};
|
2016-12-12 16:51:07 +01:00
|
|
|
use upgrade::{upgrade, upgrade_data_paths};
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
pub fn to_duration(s: &str) -> Result<Duration, String> {
|
|
|
|
to_seconds(s).map(Duration::from_secs)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_seconds(s: &str) -> Result<u64, String> {
|
|
|
|
let bad = |_| {
|
|
|
|
format!(
|
2020-09-22 14:53:52 +02:00
|
|
|
"{}: Invalid duration given. See openethereum --help for more information.",
|
2016-07-25 16:09:47 +02:00
|
|
|
s
|
|
|
|
)
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
match s {
|
|
|
|
"twice-daily" => Ok(12 * 60 * 60),
|
|
|
|
"half-hourly" => Ok(30 * 60),
|
|
|
|
"1second" | "1 second" | "second" => Ok(1),
|
|
|
|
"1minute" | "1 minute" | "minute" => Ok(60),
|
|
|
|
"hourly" | "1hour" | "1 hour" | "hour" => Ok(60 * 60),
|
|
|
|
"daily" | "1day" | "1 day" | "day" => Ok(24 * 60 * 60),
|
2018-05-24 19:43:18 +02:00
|
|
|
x if x.ends_with("seconds") => x[0..x.len() - 7].trim().parse().map_err(bad),
|
|
|
|
x if x.ends_with("minutes") => x[0..x.len() - 7]
|
|
|
|
.trim()
|
|
|
|
.parse::<u64>()
|
|
|
|
.map_err(bad)
|
|
|
|
.map(|x| x * 60),
|
|
|
|
x if x.ends_with("hours") => x[0..x.len() - 5]
|
|
|
|
.trim()
|
|
|
|
.parse::<u64>()
|
|
|
|
.map_err(bad)
|
|
|
|
.map(|x| x * 60 * 60),
|
|
|
|
x if x.ends_with("days") => x[0..x.len() - 4]
|
|
|
|
.trim()
|
|
|
|
.parse::<u64>()
|
|
|
|
.map_err(bad)
|
|
|
|
.map(|x| x * 24 * 60 * 60),
|
|
|
|
x => x.trim().parse().map_err(bad),
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_mode(s: &str, timeout: u64, alarm: u64) -> Result<Mode, String> {
|
|
|
|
match s {
|
|
|
|
"active" => Ok(Mode::Active),
|
|
|
|
"passive" => Ok(Mode::Passive(
|
|
|
|
Duration::from_secs(timeout),
|
|
|
|
Duration::from_secs(alarm),
|
|
|
|
)),
|
|
|
|
"dark" => Ok(Mode::Dark(Duration::from_secs(timeout))),
|
2016-11-05 10:38:00 +01:00
|
|
|
"offline" => Ok(Mode::Off),
|
|
|
|
_ => Err(format!(
|
|
|
|
"{}: Invalid value for --mode. Must be one of active, passive, dark or offline.",
|
|
|
|
s
|
|
|
|
)),
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:01:43 +01:00
|
|
|
pub fn to_block_id(s: &str) -> Result<BlockId, String> {
|
2016-07-25 16:09:47 +02:00
|
|
|
if s == "latest" {
|
2016-12-09 23:01:43 +01:00
|
|
|
Ok(BlockId::Latest)
|
2016-07-25 16:09:47 +02:00
|
|
|
} else if let Ok(num) = s.parse() {
|
2016-12-09 23:01:43 +01:00
|
|
|
Ok(BlockId::Number(num))
|
2016-07-25 16:09:47 +02:00
|
|
|
} else if let Ok(hash) = s.parse() {
|
2016-12-09 23:01:43 +01:00
|
|
|
Ok(BlockId::Hash(hash))
|
2016-07-25 16:09:47 +02:00
|
|
|
} else {
|
|
|
|
Err("Invalid block.".into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_u256(s: &str) -> Result<U256, String> {
|
|
|
|
if let Ok(decimal) = U256::from_dec_str(s) {
|
|
|
|
Ok(decimal)
|
|
|
|
} else if let Ok(hex) = clean_0x(s).parse() {
|
|
|
|
Ok(hex)
|
|
|
|
} else {
|
|
|
|
Err(format!("Invalid numeric value: {}", s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_pending_set(s: &str) -> Result<PendingSet, String> {
|
|
|
|
match s {
|
|
|
|
"cheap" => Ok(PendingSet::AlwaysQueue),
|
|
|
|
"strict" => Ok(PendingSet::AlwaysSealing),
|
|
|
|
"lenient" => Ok(PendingSet::SealingOrElseQueue),
|
|
|
|
other => Err(format!("Invalid pending set value: {:?}", other)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 14:46:33 +02:00
|
|
|
pub fn to_queue_strategy(s: &str) -> Result<PrioritizationStrategy, String> {
|
|
|
|
match s {
|
|
|
|
"gas_price" => Ok(PrioritizationStrategy::GasPriceOnly),
|
|
|
|
other => Err(format!("Invalid queue strategy: {}", other)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 17:34:27 +02:00
|
|
|
pub fn to_queue_penalization(time: Option<u64>) -> Result<Penalization, String> {
|
|
|
|
Ok(match time {
|
|
|
|
Some(threshold_ms) => Penalization::Enabled {
|
|
|
|
offend_threshold: Duration::from_millis(threshold_ms),
|
|
|
|
},
|
|
|
|
None => Penalization::Disabled,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
pub fn to_address(s: Option<String>) -> Result<Address, String> {
|
|
|
|
match s {
|
|
|
|
Some(ref a) => clean_0x(a)
|
|
|
|
.parse()
|
|
|
|
.map_err(|_| format!("Invalid address: {:?}", a)),
|
|
|
|
None => Ok(Address::default()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_addresses(s: &Option<String>) -> Result<Vec<Address>, String> {
|
|
|
|
match *s {
|
2016-08-02 15:12:33 +02:00
|
|
|
Some(ref adds) if !adds.is_empty() => adds
|
|
|
|
.split(',')
|
2016-07-25 16:09:47 +02:00
|
|
|
.map(|a| {
|
|
|
|
clean_0x(a)
|
|
|
|
.parse()
|
|
|
|
.map_err(|_| format!("Invalid address: {:?}", a))
|
2020-08-05 06:08:03 +02:00
|
|
|
})
|
2016-07-25 16:09:47 +02:00
|
|
|
.collect(),
|
|
|
|
_ => Ok(Vec::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tries to parse string as a price.
|
|
|
|
pub fn to_price(s: &str) -> Result<f32, String> {
|
2019-11-11 21:57:38 +01:00
|
|
|
s.parse::<f32>().map_err(|_| {
|
|
|
|
format!(
|
|
|
|
"Invalid transaction price {:?} given. Must be a decimal number.",
|
|
|
|
s
|
|
|
|
)
|
2020-08-05 06:08:03 +02:00
|
|
|
})
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
2019-01-28 11:26:11 +01:00
|
|
|
pub fn join_set(set: Option<&HashSet<String>>) -> Option<String> {
|
|
|
|
match set {
|
|
|
|
Some(s) => Some(
|
|
|
|
s.iter()
|
|
|
|
.map(|s| s.as_str())
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.join(","),
|
2020-08-05 06:08:03 +02:00
|
|
|
),
|
2019-01-28 11:26:11 +01:00
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
/// Flush output buffer.
|
|
|
|
pub fn flush_stdout() {
|
|
|
|
io::stdout().flush().expect("stdout is flushable; qed");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Formats and returns parity ipc path.
|
2017-05-23 12:24:32 +02:00
|
|
|
pub fn parity_ipc_path(base: &str, path: &str, shift: u16) -> String {
|
|
|
|
let mut path = path.to_owned();
|
|
|
|
if shift != 0 {
|
|
|
|
path = path.replace("jsonrpc.ipc", &format!("jsonrpc-{}.ipc", shift));
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
2017-05-23 12:24:32 +02:00
|
|
|
replace_home(base, &path)
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Validates and formats bootnodes option.
|
|
|
|
pub fn to_bootnodes(bootnodes: &Option<String>) -> Result<Vec<String>, String> {
|
|
|
|
match *bootnodes {
|
|
|
|
Some(ref x) if !x.is_empty() => x
|
|
|
|
.split(',')
|
2017-11-13 14:37:08 +01:00
|
|
|
.map(|s| match validate_node_url(s).map(Into::into) {
|
2017-10-31 05:09:31 +01:00
|
|
|
None => Ok(s.to_owned()),
|
2018-04-10 12:13:49 +02:00
|
|
|
Some(sync::ErrorKind::AddressResolve(_)) => {
|
|
|
|
Err(format!("Failed to resolve hostname of a boot node: {}", s))
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2017-10-31 05:09:31 +01:00
|
|
|
Some(_) => Err(format!(
|
|
|
|
"Invalid node address format given for a boot node: {}",
|
|
|
|
s
|
|
|
|
)),
|
2016-07-25 16:09:47 +02:00
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
Some(_) => Ok(vec![]),
|
|
|
|
None => Ok(vec![]),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2018-04-10 12:13:49 +02:00
|
|
|
pub fn default_network_config() -> ::sync::NetworkConfiguration {
|
2017-07-28 19:06:39 +02:00
|
|
|
use super::network::IpFilter;
|
2018-04-10 12:13:49 +02:00
|
|
|
use sync::NetworkConfiguration;
|
2016-07-25 16:09:47 +02:00
|
|
|
NetworkConfiguration {
|
2016-12-15 21:56:45 +01:00
|
|
|
config_path: Some(replace_home(&::dir::default_data_path(), "$BASE/network")),
|
2016-10-11 18:42:20 +02:00
|
|
|
net_config_path: None,
|
2016-08-05 10:32:04 +02:00
|
|
|
listen_address: Some("0.0.0.0:30303".into()),
|
2016-07-25 16:09:47 +02:00
|
|
|
public_address: None,
|
|
|
|
udp_port: None,
|
|
|
|
nat_enabled: true,
|
|
|
|
discovery_enabled: true,
|
|
|
|
boot_nodes: Vec::new(),
|
|
|
|
use_secret: None,
|
2016-07-29 17:30:02 +02:00
|
|
|
max_peers: 50,
|
|
|
|
min_peers: 25,
|
2016-10-24 18:25:27 +02:00
|
|
|
snapshot_peers: 0,
|
|
|
|
max_pending_peers: 64,
|
2017-07-28 19:06:39 +02:00
|
|
|
ip_filter: IpFilter::default(),
|
2016-07-25 16:09:47 +02:00
|
|
|
reserved_nodes: Vec::new(),
|
2016-08-05 10:32:04 +02:00
|
|
|
allow_non_reserved: true,
|
2017-12-22 14:37:39 +01:00
|
|
|
client_version: ::parity_version::version(),
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_client_config(
|
2018-10-26 13:21:36 +02:00
|
|
|
cache_config: &CacheConfig,
|
|
|
|
spec_name: String,
|
|
|
|
mode: Mode,
|
|
|
|
tracing: bool,
|
|
|
|
fat_db: bool,
|
|
|
|
compaction: DatabaseCompactionProfile,
|
|
|
|
vm_type: VMType,
|
|
|
|
name: String,
|
|
|
|
pruning: Algorithm,
|
|
|
|
pruning_history: u64,
|
|
|
|
pruning_memory: usize,
|
|
|
|
check_seal: bool,
|
|
|
|
max_round_blocks_to_import: usize,
|
|
|
|
) -> ClientConfig {
|
2016-07-25 16:09:47 +02:00
|
|
|
let mut client_config = ClientConfig::default();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
let mb = 1024 * 1024;
|
|
|
|
// in bytes
|
|
|
|
client_config.blockchain.max_cache_size = cache_config.blockchain() as usize * mb;
|
|
|
|
// in bytes
|
|
|
|
client_config.blockchain.pref_cache_size = cache_config.blockchain() as usize * 3 / 4 * mb;
|
2018-01-03 11:00:37 +01:00
|
|
|
// db cache size, in megabytes
|
|
|
|
client_config.db_cache_size = Some(cache_config.db_cache_size() as usize);
|
2016-07-25 16:09:47 +02:00
|
|
|
// db queue cache size, in bytes
|
|
|
|
client_config.queue.max_mem_use = cache_config.queue() as usize * mb;
|
2016-07-31 00:19:27 +02:00
|
|
|
// in bytes
|
|
|
|
client_config.tracing.max_cache_size = cache_config.traces() as usize * mb;
|
|
|
|
// in bytes
|
|
|
|
client_config.tracing.pref_cache_size = cache_config.traces() as usize * 3 / 4 * mb;
|
2016-10-07 12:18:42 +02:00
|
|
|
// in bytes
|
|
|
|
client_config.state_cache_size = cache_config.state() as usize * mb;
|
|
|
|
// in bytes
|
|
|
|
client_config.jump_table_size = cache_config.jump_tables() as usize * mb;
|
2017-01-20 13:25:53 +01:00
|
|
|
// in bytes
|
|
|
|
client_config.history_mem = pruning_memory * mb;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
client_config.mode = mode;
|
|
|
|
client_config.tracing.enabled = tracing;
|
2016-10-03 11:13:10 +02:00
|
|
|
client_config.fat_db = fat_db;
|
2016-09-26 19:21:25 +02:00
|
|
|
client_config.pruning = pruning;
|
2016-10-14 14:44:56 +02:00
|
|
|
client_config.history = pruning_history;
|
2016-07-25 16:09:47 +02:00
|
|
|
client_config.db_compaction = compaction;
|
|
|
|
client_config.vm_type = vm_type;
|
|
|
|
client_config.name = name;
|
2016-10-24 15:09:13 +02:00
|
|
|
client_config.verifier_type = if check_seal {
|
|
|
|
VerifierType::Canon
|
|
|
|
} else {
|
|
|
|
VerifierType::CanonNoSeal
|
|
|
|
};
|
2017-03-13 12:10:53 +01:00
|
|
|
client_config.spec_name = spec_name;
|
2018-10-26 13:21:36 +02:00
|
|
|
client_config.max_round_blocks_to_import = max_round_blocks_to_import;
|
2016-07-25 16:09:47 +02:00
|
|
|
client_config
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:29:58 +02:00
|
|
|
pub fn execute_upgrades(
|
2016-12-15 21:56:45 +01:00
|
|
|
base_path: &str,
|
2016-09-26 19:21:25 +02:00
|
|
|
dirs: &DatabaseDirectories,
|
2016-07-28 20:29:58 +02:00
|
|
|
pruning: Algorithm,
|
2018-04-13 21:14:53 +02:00
|
|
|
compaction_profile: &DatabaseCompactionProfile,
|
2016-07-28 20:29:58 +02:00
|
|
|
) -> Result<(), String> {
|
2016-12-15 21:56:45 +01:00
|
|
|
upgrade_data_paths(base_path, dirs, pruning);
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2018-08-03 09:58:59 +02:00
|
|
|
match upgrade(&dirs.path) {
|
2016-07-25 16:09:47 +02:00
|
|
|
Ok(upgrades_applied) if upgrades_applied > 0 => {
|
|
|
|
debug!("Executed {} upgrade scripts - ok", upgrades_applied);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2020-09-22 14:53:52 +02:00
|
|
|
return Err(format!("Error upgrading OpenEthereum data: {:?}", e));
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-12-12 16:51:07 +01:00
|
|
|
let client_path = dirs.db_path(pruning);
|
2018-02-22 14:53:10 +01:00
|
|
|
migrate(&client_path, compaction_profile).map_err(|e| format!("{}", e))
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Prompts user asking for password.
|
2018-06-22 15:09:15 +02:00
|
|
|
pub fn password_prompt() -> Result<Password, String> {
|
2016-07-25 16:09:47 +02:00
|
|
|
use rpassword::read_password;
|
2016-11-11 16:53:51 +01:00
|
|
|
const STDIN_ERROR: &'static str = "Unable to ask for password on non-interactive terminal.";
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
println!("Please note that password is NOT RECOVERABLE.");
|
|
|
|
print!("Type password: ");
|
|
|
|
flush_stdout();
|
|
|
|
|
2018-06-22 15:09:15 +02:00
|
|
|
let password = read_password().map_err(|_| STDIN_ERROR.to_owned())?.into();
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
print!("Repeat password: ");
|
|
|
|
flush_stdout();
|
|
|
|
|
2018-06-22 15:09:15 +02:00
|
|
|
let password_repeat = read_password().map_err(|_| STDIN_ERROR.to_owned())?.into();
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
if password != password_repeat {
|
|
|
|
return Err("Passwords do not match!".into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(password)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read a password from password file.
|
2018-06-22 15:09:15 +02:00
|
|
|
pub fn password_from_file(path: String) -> Result<Password, String> {
|
2016-12-27 12:53:56 +01:00
|
|
|
let passwords = passwords_from_files(&[path])?;
|
2016-11-12 10:06:40 +01:00
|
|
|
// use only first password from the file
|
2018-06-22 15:09:15 +02:00
|
|
|
passwords
|
|
|
|
.get(0)
|
|
|
|
.map(Password::clone)
|
2016-11-12 10:06:40 +01:00
|
|
|
.ok_or_else(|| "Password file seems to be empty.".to_owned())
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads passwords from files. Treats each line as a separate password.
|
2018-06-22 15:09:15 +02:00
|
|
|
pub fn passwords_from_files(files: &[String]) -> Result<Vec<Password>, String> {
|
2016-07-25 16:09:47 +02:00
|
|
|
let passwords = files.iter().map(|filename| {
|
2016-12-27 12:53:56 +01:00
|
|
|
let file = File::open(filename).map_err(|_| format!("{} Unable to read password file. Ensure it exists and permissions are correct.", filename))?;
|
2016-07-25 16:09:47 +02:00
|
|
|
let reader = BufReader::new(&file);
|
|
|
|
let lines = reader.lines()
|
2016-11-11 16:53:51 +01:00
|
|
|
.filter_map(|l| l.ok())
|
2018-06-22 15:09:15 +02:00
|
|
|
.map(|pwd| pwd.trim().to_owned().into())
|
|
|
|
.collect::<Vec<Password>>();
|
2016-07-25 16:09:47 +02:00
|
|
|
Ok(lines)
|
2018-06-22 15:09:15 +02:00
|
|
|
}).collect::<Result<Vec<Vec<Password>>, String>>();
|
2016-12-27 12:53:56 +01:00
|
|
|
Ok(passwords?.into_iter().flat_map(|x| x).collect())
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-01-28 11:26:11 +01:00
|
|
|
use super::{
|
2020-08-07 19:36:32 +02:00
|
|
|
join_set, password_from_file, to_address, to_addresses, to_block_id, to_bootnodes,
|
|
|
|
to_duration, to_mode, to_pending_set, to_price, to_u256,
|
2020-08-05 06:08:03 +02:00
|
|
|
};
|
2016-12-09 23:01:43 +01:00
|
|
|
use ethcore::{
|
2019-01-28 11:26:11 +01:00
|
|
|
client::{BlockId, Mode},
|
2018-04-13 17:34:27 +02:00
|
|
|
miner::PendingSet,
|
2020-08-05 06:08:03 +02:00
|
|
|
};
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::U256;
|
2018-06-22 15:09:15 +02:00
|
|
|
use ethkey::Password;
|
2019-01-28 11:26:11 +01:00
|
|
|
use std::{collections::HashSet, fs::File, io::Write, time::Duration};
|
2018-01-19 17:32:53 +01:00
|
|
|
use tempdir::TempDir;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[test]
|
|
|
|
fn test_to_duration() {
|
|
|
|
assert_eq!(
|
|
|
|
to_duration("twice-daily").unwrap(),
|
|
|
|
Duration::from_secs(12 * 60 * 60)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_duration("half-hourly").unwrap(),
|
|
|
|
Duration::from_secs(30 * 60)
|
|
|
|
);
|
|
|
|
assert_eq!(to_duration("1second").unwrap(), Duration::from_secs(1));
|
|
|
|
assert_eq!(to_duration("2seconds").unwrap(), Duration::from_secs(2));
|
|
|
|
assert_eq!(to_duration("15seconds").unwrap(), Duration::from_secs(15));
|
|
|
|
assert_eq!(to_duration("1minute").unwrap(), Duration::from_secs(1 * 60));
|
|
|
|
assert_eq!(
|
|
|
|
to_duration("2minutes").unwrap(),
|
|
|
|
Duration::from_secs(2 * 60)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_duration("15minutes").unwrap(),
|
|
|
|
Duration::from_secs(15 * 60)
|
|
|
|
);
|
|
|
|
assert_eq!(to_duration("hourly").unwrap(), Duration::from_secs(60 * 60));
|
|
|
|
assert_eq!(
|
|
|
|
to_duration("daily").unwrap(),
|
|
|
|
Duration::from_secs(24 * 60 * 60)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_duration("1hour").unwrap(),
|
|
|
|
Duration::from_secs(1 * 60 * 60)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_duration("2hours").unwrap(),
|
|
|
|
Duration::from_secs(2 * 60 * 60)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_duration("15hours").unwrap(),
|
|
|
|
Duration::from_secs(15 * 60 * 60)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_duration("1day").unwrap(),
|
|
|
|
Duration::from_secs(1 * 24 * 60 * 60)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_duration("2days").unwrap(),
|
|
|
|
Duration::from_secs(2 * 24 * 60 * 60)
|
2018-05-24 19:43:18 +02:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_duration("15days").unwrap(),
|
|
|
|
Duration::from_secs(15 * 24 * 60 * 60)
|
2016-07-25 16:09:47 +02:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_duration("15 days").unwrap(),
|
|
|
|
Duration::from_secs(15 * 24 * 60 * 60)
|
2020-08-05 06:08:03 +02:00
|
|
|
);
|
2016-07-25 16:09:47 +02:00
|
|
|
assert_eq!(to_duration("2 seconds").unwrap(), Duration::from_secs(2));
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-07-25 16:09:47 +02:00
|
|
|
fn test_to_mode() {
|
|
|
|
assert_eq!(to_mode("active", 0, 0).unwrap(), Mode::Active);
|
|
|
|
assert_eq!(
|
|
|
|
to_mode("passive", 10, 20).unwrap(),
|
2016-12-09 23:01:43 +01:00
|
|
|
Mode::Passive(Duration::from_secs(10), Duration::from_secs(20))
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_mode("dark", 20, 30).unwrap(),
|
|
|
|
Mode::Dark(Duration::from_secs(20))
|
2020-08-05 06:08:03 +02:00
|
|
|
);
|
2016-12-09 23:01:43 +01:00
|
|
|
assert!(to_mode("other", 20, 30).is_err());
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-07-25 16:09:47 +02:00
|
|
|
fn test_to_block_id() {
|
2016-12-09 23:01:43 +01:00
|
|
|
assert_eq!(to_block_id("latest").unwrap(), BlockId::Latest);
|
|
|
|
assert_eq!(to_block_id("0").unwrap(), BlockId::Number(0));
|
2016-07-25 16:09:47 +02:00
|
|
|
assert_eq!(to_block_id("2").unwrap(), BlockId::Number(2));
|
|
|
|
assert_eq!(to_block_id("15").unwrap(), BlockId::Number(15));
|
|
|
|
assert_eq!(
|
|
|
|
to_block_id("9fc84d84f6a785dc1bd5abacfcf9cbdd3b6afb80c0f799bfb2fd42c44a0c224e")
|
2016-12-09 23:01:43 +01:00
|
|
|
.unwrap(),
|
2016-07-25 16:09:47 +02:00
|
|
|
BlockId::Hash(
|
2016-12-09 23:01:43 +01:00
|
|
|
"9fc84d84f6a785dc1bd5abacfcf9cbdd3b6afb80c0f799bfb2fd42c44a0c224e"
|
2016-07-25 16:09:47 +02:00
|
|
|
.parse()
|
|
|
|
.unwrap()
|
2020-08-05 06:08:03 +02:00
|
|
|
)
|
2016-07-25 16:09:47 +02:00
|
|
|
);
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-07-25 16:09:47 +02:00
|
|
|
fn test_to_u256() {
|
|
|
|
assert_eq!(to_u256("0").unwrap(), U256::from(0));
|
2018-06-22 15:09:15 +02:00
|
|
|
assert_eq!(to_u256("11").unwrap(), U256::from(11));
|
2016-07-25 16:09:47 +02:00
|
|
|
assert_eq!(to_u256("0x11").unwrap(), U256::from(17));
|
|
|
|
assert!(to_u256("u").is_err())
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[test]
|
|
|
|
fn test_pending_set() {
|
|
|
|
assert_eq!(to_pending_set("cheap").unwrap(), PendingSet::AlwaysQueue);
|
|
|
|
assert_eq!(to_pending_set("strict").unwrap(), PendingSet::AlwaysSealing);
|
|
|
|
assert_eq!(
|
|
|
|
to_pending_set("lenient").unwrap(),
|
|
|
|
PendingSet::SealingOrElseQueue
|
|
|
|
);
|
|
|
|
assert!(to_pending_set("othe").is_err());
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[test]
|
|
|
|
fn test_to_address() {
|
|
|
|
assert_eq!(
|
|
|
|
to_address(Some("0xD9A111feda3f362f55Ef1744347CDC8Dd9964a41".into())).unwrap(),
|
|
|
|
"D9A111feda3f362f55Ef1744347CDC8Dd9964a41".parse().unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_address(Some("D9A111feda3f362f55Ef1744347CDC8Dd9964a41".into())).unwrap(),
|
|
|
|
"D9A111feda3f362f55Ef1744347CDC8Dd9964a41".parse().unwrap()
|
|
|
|
);
|
|
|
|
assert_eq!(to_address(None).unwrap(), Default::default());
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-08-02 15:12:33 +02:00
|
|
|
#[test]
|
|
|
|
fn test_to_addresses() {
|
|
|
|
let addresses = to_addresses(&Some(
|
|
|
|
"0xD9A111feda3f362f55Ef1744347CDC8Dd9964a41,D9A111feda3f362f55Ef1744347CDC8Dd9964a42"
|
|
|
|
.into(),
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
addresses,
|
|
|
|
vec![
|
|
|
|
"D9A111feda3f362f55Ef1744347CDC8Dd9964a41".parse().unwrap(),
|
|
|
|
"D9A111feda3f362f55Ef1744347CDC8Dd9964a42".parse().unwrap(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-10-07 09:39:16 +02:00
|
|
|
#[test]
|
|
|
|
fn test_password() {
|
2018-01-19 17:32:53 +01:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let path = tempdir.path().join("file");
|
|
|
|
let mut file = File::create(&path).unwrap();
|
2016-10-07 09:39:16 +02:00
|
|
|
file.write_all(b"a bc ").unwrap();
|
2018-01-19 17:32:53 +01:00
|
|
|
assert_eq!(
|
|
|
|
password_from_file(path.to_str().unwrap().into())
|
|
|
|
.unwrap()
|
|
|
|
.as_bytes(),
|
|
|
|
b"a bc"
|
|
|
|
);
|
2016-11-12 10:06:40 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-11-12 10:06:40 +01:00
|
|
|
#[test]
|
|
|
|
fn test_password_multiline() {
|
2018-01-19 17:32:53 +01:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let path = tempdir.path().join("file");
|
2016-11-12 10:06:40 +01:00
|
|
|
let mut file = File::create(path.as_path()).unwrap();
|
|
|
|
file.write_all(
|
|
|
|
br#" password with trailing whitespace
|
|
|
|
those passwords should be
|
|
|
|
ignored
|
|
|
|
but the first password is trimmed
|
|
|
|
|
|
|
|
"#,
|
2020-08-05 06:08:03 +02:00
|
|
|
)
|
2016-11-12 10:06:40 +01:00
|
|
|
.unwrap();
|
2018-06-22 15:09:15 +02:00
|
|
|
assert_eq!(
|
|
|
|
password_from_file(path.to_str().unwrap().into()).unwrap(),
|
|
|
|
Password::from("password with trailing whitespace")
|
|
|
|
);
|
2016-10-07 09:39:16 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[test]
|
|
|
|
fn test_to_price() {
|
|
|
|
assert_eq!(to_price("1").unwrap(), 1.0);
|
|
|
|
assert_eq!(to_price("2.3").unwrap(), 2.3);
|
|
|
|
assert_eq!(to_price("2.33").unwrap(), 2.33);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[test]
|
|
|
|
fn test_to_bootnodes() {
|
|
|
|
let one_bootnode = "enode://e731347db0521f3476e6bbbb83375dcd7133a1601425ebd15fd10f3835fd4c304fba6282087ca5a0deeafadf0aa0d4fd56c3323331901c1f38bd181c283e3e35@128.199.55.137:30303";
|
|
|
|
let two_bootnodes = "enode://e731347db0521f3476e6bbbb83375dcd7133a1601425ebd15fd10f3835fd4c304fba6282087ca5a0deeafadf0aa0d4fd56c3323331901c1f38bd181c283e3e35@128.199.55.137:30303,enode://e731347db0521f3476e6bbbb83375dcd7133a1601425ebd15fd10f3835fd4c304fba6282087ca5a0deeafadf0aa0d4fd56c3323331901c1f38bd181c283e3e35@128.199.55.137:30303";
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
assert_eq!(to_bootnodes(&Some("".into())), Ok(vec![]));
|
|
|
|
assert_eq!(to_bootnodes(&None), Ok(vec![]));
|
|
|
|
assert_eq!(
|
|
|
|
to_bootnodes(&Some(one_bootnode.into())),
|
|
|
|
Ok(vec![one_bootnode.into()])
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
to_bootnodes(&Some(two_bootnodes.into())),
|
|
|
|
Ok(vec![one_bootnode.into(), one_bootnode.into()])
|
2020-08-05 06:08:03 +02:00
|
|
|
);
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-01-28 11:26:11 +01:00
|
|
|
#[test]
|
|
|
|
fn test_join_set() {
|
|
|
|
let mut test_set = HashSet::new();
|
|
|
|
test_set.insert("0x1111111111111111111111111111111111111111".to_string());
|
|
|
|
test_set.insert("0x0000000000000000000000000000000000000000".to_string());
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-01-30 13:57:38 +01:00
|
|
|
let res = join_set(Some(&test_set)).unwrap();
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-01-30 13:57:38 +01:00
|
|
|
assert!(
|
|
|
|
res == "0x1111111111111111111111111111111111111111,0x0000000000000000000000000000000000000000"
|
|
|
|
||
|
|
|
|
res == "0x0000000000000000000000000000000000000000,0x1111111111111111111111111111111111111111"
|
|
|
|
);
|
2019-01-28 11:26:11 +01:00
|
|
|
}
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|