2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-07-25 16:09:47 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// 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
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
use std::{io, env};
|
2016-11-12 10:06:40 +01:00
|
|
|
use std::io::{Write, BufReader, BufRead};
|
2016-07-25 16:09:47 +02:00
|
|
|
use std::time::Duration;
|
|
|
|
use std::fs::File;
|
2017-09-04 16:36:49 +02:00
|
|
|
use bigint::prelude::U256;
|
|
|
|
use bigint::hash::clean_0x;
|
2017-10-10 20:01:27 +02:00
|
|
|
use util::Address;
|
2017-10-12 15:36:27 +02:00
|
|
|
use kvdb_rocksdb::CompactionProfile;
|
2017-10-17 06:41:05 +02:00
|
|
|
use journaldb::Algorithm;
|
2016-12-09 23:01:43 +01:00
|
|
|
use ethcore::client::{Mode, BlockId, VMType, DatabaseCompactionProfile, ClientConfig, VerifierType};
|
2016-10-15 14:46:33 +02:00
|
|
|
use ethcore::miner::{PendingSet, GasLimit, PrioritizationStrategy};
|
2016-07-25 16:09:47 +02:00
|
|
|
use cache::CacheConfig;
|
2016-09-26 19:21:25 +02:00
|
|
|
use dir::DatabaseDirectories;
|
2016-12-12 16:51:07 +01:00
|
|
|
use upgrade::{upgrade, upgrade_data_paths};
|
2016-07-25 16:09:47 +02:00
|
|
|
use migration::migrate;
|
2016-08-05 10:32:04 +02:00
|
|
|
use ethsync::is_valid_node_url;
|
2017-03-22 06:23:40 +01:00
|
|
|
use path;
|
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!("{}: Invalid duration given. See parity --help for more information.", s)
|
|
|
|
};
|
|
|
|
|
|
|
|
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),
|
|
|
|
x if x.ends_with("seconds") => x[0..x.len() - 7].parse().map_err(bad),
|
2016-11-05 10:38:00 +01:00
|
|
|
x if x.ends_with("minutes") => x[0..x.len() - 7].parse::<u64>().map_err(bad).map(|x| x * 60),
|
2016-07-25 16:09:47 +02:00
|
|
|
x if x.ends_with("hours") => x[0..x.len() - 5].parse::<u64>().map_err(bad).map(|x| x * 60 * 60),
|
|
|
|
x if x.ends_with("days") => x[0..x.len() - 4].parse::<u64>().map_err(bad).map(|x| x * 24 * 60 * 60),
|
|
|
|
x => x.parse().map_err(bad),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10 23:04:43 +02:00
|
|
|
pub fn to_gas_limit(s: &str) -> Result<GasLimit, String> {
|
|
|
|
match s {
|
|
|
|
"auto" => Ok(GasLimit::Auto),
|
|
|
|
"off" => Ok(GasLimit::None),
|
2016-12-27 12:53:56 +01:00
|
|
|
other => Ok(GasLimit::Fixed(to_u256(other)?)),
|
2016-10-10 23:04:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 14:46:33 +02:00
|
|
|
pub fn to_queue_strategy(s: &str) -> Result<PrioritizationStrategy, String> {
|
|
|
|
match s {
|
|
|
|
"gas" => Ok(PrioritizationStrategy::GasAndGasPrice),
|
|
|
|
"gas_price" => Ok(PrioritizationStrategy::GasPriceOnly),
|
|
|
|
"gas_factor" => Ok(PrioritizationStrategy::GasFactorAndGasPrice),
|
|
|
|
other => Err(format!("Invalid queue strategy: {}", other)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)))
|
|
|
|
.collect(),
|
|
|
|
_ => Ok(Vec::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tries to parse string as a price.
|
|
|
|
pub fn to_price(s: &str) -> Result<f32, String> {
|
|
|
|
s.parse::<f32>().map_err(|_| format!("Invalid transaciton price 's' given. Must be a decimal number."))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Replaces `$HOME` str with home directory path.
|
2016-12-12 16:51:07 +01:00
|
|
|
pub fn replace_home(base: &str, arg: &str) -> String {
|
2016-07-25 16:09:47 +02:00
|
|
|
// the $HOME directory on mac os should be `~/Library` or `~/Library/Application Support`
|
|
|
|
let r = arg.replace("$HOME", env::home_dir().unwrap().to_str().unwrap());
|
2017-01-05 14:12:54 +01:00
|
|
|
let r = r.replace("$BASE", base);
|
|
|
|
r.replace("/", &::std::path::MAIN_SEPARATOR.to_string())
|
|
|
|
}
|
|
|
|
|
2017-07-10 12:57:40 +02:00
|
|
|
pub fn replace_home_and_local(base: &str, local: &str, arg: &str) -> String {
|
2017-01-05 14:12:54 +01:00
|
|
|
let r = replace_home(base, arg);
|
|
|
|
r.replace("$LOCAL", local)
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Flush output buffer.
|
|
|
|
pub fn flush_stdout() {
|
|
|
|
io::stdout().flush().expect("stdout is flushable; qed");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns default geth ipc path.
|
|
|
|
pub fn geth_ipc_path(testnet: bool) -> String {
|
|
|
|
// Windows path should not be hardcoded here.
|
|
|
|
// Instead it should be a part of path::ethereum
|
|
|
|
if cfg!(windows) {
|
|
|
|
return r"\\.\pipe\geth.ipc".to_owned();
|
|
|
|
}
|
|
|
|
|
|
|
|
if testnet {
|
|
|
|
path::ethereum::with_testnet("geth.ipc").to_str().unwrap().to_owned()
|
|
|
|
} else {
|
|
|
|
path::ethereum::with_default("geth.ipc").to_str().unwrap().to_owned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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(',').map(|s| {
|
|
|
|
if is_valid_node_url(s) {
|
|
|
|
Ok(s.to_owned())
|
|
|
|
} else {
|
|
|
|
Err(format!("Invalid node address format given for a boot node: {}", s))
|
|
|
|
}
|
|
|
|
}).collect(),
|
|
|
|
Some(_) => Ok(vec![]),
|
|
|
|
None => Ok(vec![])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2016-08-05 10:32:04 +02:00
|
|
|
pub fn default_network_config() -> ::ethsync::NetworkConfiguration {
|
2017-07-28 19:06:39 +02:00
|
|
|
use ethsync::{NetworkConfiguration};
|
|
|
|
use super::network::IpFilter;
|
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,
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 20:31:25 +02:00
|
|
|
#[cfg_attr(feature = "dev", allow(too_many_arguments))]
|
2016-07-25 16:09:47 +02:00
|
|
|
pub fn to_client_config(
|
|
|
|
cache_config: &CacheConfig,
|
2017-03-13 12:10:53 +01:00
|
|
|
spec_name: String,
|
2016-07-25 16:09:47 +02:00
|
|
|
mode: Mode,
|
2016-09-26 19:21:25 +02:00
|
|
|
tracing: bool,
|
2016-10-03 11:13:10 +02:00
|
|
|
fat_db: bool,
|
2016-07-25 16:09:47 +02:00
|
|
|
compaction: DatabaseCompactionProfile,
|
2016-07-29 15:36:00 +02:00
|
|
|
wal: bool,
|
2016-07-25 16:09:47 +02:00
|
|
|
vm_type: VMType,
|
|
|
|
name: String,
|
2016-09-26 19:21:25 +02:00
|
|
|
pruning: Algorithm,
|
2016-10-14 14:44:56 +02:00
|
|
|
pruning_history: u64,
|
2017-01-20 13:25:53 +01:00
|
|
|
pruning_memory: usize,
|
2016-10-24 15:09:13 +02:00
|
|
|
check_seal: bool,
|
2016-07-25 16:09:47 +02:00
|
|
|
) -> ClientConfig {
|
|
|
|
let mut client_config = ClientConfig::default();
|
|
|
|
|
|
|
|
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;
|
|
|
|
// db blockchain cache size, in megabytes
|
|
|
|
client_config.blockchain.db_cache_size = Some(cache_config.db_blockchain_cache_size() as usize);
|
|
|
|
// db state cache size, in megabytes
|
|
|
|
client_config.db_cache_size = Some(cache_config.db_state_cache_size() as usize);
|
|
|
|
// 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;
|
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;
|
2016-07-29 15:36:00 +02:00
|
|
|
client_config.db_wal = wal;
|
2016-07-25 16:09:47 +02:00
|
|
|
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;
|
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,
|
|
|
|
compaction_profile: CompactionProfile
|
|
|
|
) -> Result<(), String> {
|
|
|
|
|
2016-12-15 21:56:45 +01:00
|
|
|
upgrade_data_paths(base_path, dirs, pruning);
|
2016-12-12 16:51:07 +01:00
|
|
|
|
2016-09-26 19:21:25 +02:00
|
|
|
match upgrade(Some(&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) => {
|
|
|
|
return Err(format!("Error upgrading parity data: {:?}", e));
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
|
2016-12-12 16:51:07 +01:00
|
|
|
let client_path = dirs.db_path(pruning);
|
2016-07-28 20:29:58 +02:00
|
|
|
migrate(&client_path, pruning, compaction_profile).map_err(|e| format!("{}", e))
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Prompts user asking for password.
|
|
|
|
pub fn password_prompt() -> Result<String, String> {
|
|
|
|
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();
|
|
|
|
|
2016-12-27 12:53:56 +01:00
|
|
|
let password = read_password().map_err(|_| STDIN_ERROR.to_owned())?;
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
print!("Repeat password: ");
|
|
|
|
flush_stdout();
|
|
|
|
|
2016-12-27 12:53:56 +01:00
|
|
|
let password_repeat = read_password().map_err(|_| STDIN_ERROR.to_owned())?;
|
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.
|
2016-11-12 10:06:40 +01:00
|
|
|
pub fn password_from_file(path: String) -> Result<String, 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
|
|
|
|
passwords.get(0).map(String::to_owned)
|
|
|
|
.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.
|
2016-12-05 20:23:03 +01:00
|
|
|
pub fn passwords_from_files(files: &[String]) -> Result<Vec<String>, 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())
|
2016-11-12 10:06:40 +01:00
|
|
|
.map(|pwd| pwd.trim().to_owned())
|
2016-07-25 16:09:47 +02:00
|
|
|
.collect::<Vec<String>>();
|
|
|
|
Ok(lines)
|
2016-11-12 10:06:40 +01:00
|
|
|
}).collect::<Result<Vec<Vec<String>>, 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 {
|
|
|
|
use std::time::Duration;
|
2016-10-07 09:39:16 +02:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
use devtools::RandomTempPath;
|
2017-09-04 16:36:49 +02:00
|
|
|
use bigint::prelude::U256;
|
2016-12-09 23:01:43 +01:00
|
|
|
use ethcore::client::{Mode, BlockId};
|
2016-07-25 16:09:47 +02:00
|
|
|
use ethcore::miner::PendingSet;
|
2016-10-07 09:39:16 +02:00
|
|
|
use super::{to_duration, to_mode, to_block_id, to_u256, to_pending_set, to_address, to_addresses, to_price, geth_ipc_path, to_bootnodes, password_from_file};
|
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));
|
|
|
|
assert_eq!(to_duration("15days").unwrap(), Duration::from_secs(15 * 24 * 60 * 60));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_mode() {
|
|
|
|
assert_eq!(to_mode("active", 0, 0).unwrap(), Mode::Active);
|
|
|
|
assert_eq!(to_mode("passive", 10, 20).unwrap(), Mode::Passive(Duration::from_secs(10), Duration::from_secs(20)));
|
|
|
|
assert_eq!(to_mode("dark", 20, 30).unwrap(), Mode::Dark(Duration::from_secs(20)));
|
|
|
|
assert!(to_mode("other", 20, 30).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
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));
|
|
|
|
assert_eq!(to_block_id("2").unwrap(), BlockId::Number(2));
|
|
|
|
assert_eq!(to_block_id("15").unwrap(), BlockId::Number(15));
|
2016-07-25 16:09:47 +02:00
|
|
|
assert_eq!(
|
|
|
|
to_block_id("9fc84d84f6a785dc1bd5abacfcf9cbdd3b6afb80c0f799bfb2fd42c44a0c224e").unwrap(),
|
2016-12-09 23:01:43 +01:00
|
|
|
BlockId::Hash("9fc84d84f6a785dc1bd5abacfcf9cbdd3b6afb80c0f799bfb2fd42c44a0c224e".parse().unwrap())
|
2016-07-25 16:09:47 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_u256() {
|
|
|
|
assert_eq!(to_u256("0").unwrap(), U256::from(0));
|
|
|
|
assert_eq!(to_u256("11").unwrap(), U256::from(11));
|
|
|
|
assert_eq!(to_u256("0x11").unwrap(), U256::from(17));
|
|
|
|
assert!(to_u256("u").is_err())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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());
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-10-07 09:39:16 +02:00
|
|
|
#[test]
|
|
|
|
fn test_password() {
|
|
|
|
let path = RandomTempPath::new();
|
|
|
|
let mut file = File::create(path.as_path()).unwrap();
|
|
|
|
file.write_all(b"a bc ").unwrap();
|
2016-11-12 10:06:40 +01:00
|
|
|
assert_eq!(password_from_file(path.as_str().into()).unwrap().as_bytes(), b"a bc");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_password_multiline() {
|
|
|
|
let path = RandomTempPath::new();
|
|
|
|
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
|
|
|
|
|
|
|
|
"#).unwrap();
|
|
|
|
assert_eq!(&password_from_file(path.as_str().into()).unwrap(), "password with trailing whitespace");
|
2016-10-07 09:39:16 +02:00
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[test]
|
2016-07-26 20:31:25 +02:00
|
|
|
#[cfg_attr(feature = "dev", allow(float_cmp))]
|
2016-07-25 16:09:47 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn test_geth_ipc_path() {
|
|
|
|
assert_eq!(geth_ipc_path(true), r"\\.\pipe\geth.ipc".to_owned());
|
|
|
|
assert_eq!(geth_ipc_path(false), r"\\.\pipe\geth.ipc".to_owned());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn test_geth_ipc_path() {
|
2017-03-22 06:23:40 +01:00
|
|
|
use path;
|
2016-07-25 16:09:47 +02:00
|
|
|
assert_eq!(geth_ipc_path(true), path::ethereum::with_testnet("geth.ipc").to_str().unwrap().to_owned());
|
|
|
|
assert_eq!(geth_ipc_path(false), path::ethereum::with_default("geth.ipc").to_str().unwrap().to_owned());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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";
|
|
|
|
|
|
|
|
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()]));
|
|
|
|
}
|
|
|
|
}
|