2016-10-03 06:44:35 -07:00
|
|
|
use std::net;
|
2016-10-02 18:01:46 -07:00
|
|
|
use clap;
|
2016-11-25 09:38:21 -08:00
|
|
|
use network::Magic;
|
2016-11-30 07:01:11 -08:00
|
|
|
use p2p::InternetProtocol;
|
2016-11-30 05:37:17 -08:00
|
|
|
use seednodes::{mainnet_seednodes, testnet_seednodes};
|
2016-11-25 03:37:27 -08:00
|
|
|
use {USER_AGENT, REGTEST_USER_AGENT};
|
2016-10-02 18:01:46 -07:00
|
|
|
|
2016-10-03 06:44:35 -07:00
|
|
|
pub struct Config {
|
2016-10-02 18:01:46 -07:00
|
|
|
pub magic: Magic,
|
|
|
|
pub port: u16,
|
2016-11-02 00:25:09 -07:00
|
|
|
pub connect: Option<net::SocketAddr>,
|
2016-11-30 05:37:17 -08:00
|
|
|
pub seednodes: Vec<String>,
|
2016-10-03 06:44:35 -07:00
|
|
|
pub print_to_console: bool,
|
2016-11-16 15:33:35 -08:00
|
|
|
pub inbound_connections: u32,
|
|
|
|
pub outbound_connections: u32,
|
|
|
|
pub p2p_threads: usize,
|
2016-11-21 00:52:27 -08:00
|
|
|
pub db_cache: usize,
|
2016-11-24 22:58:04 -08:00
|
|
|
pub data_dir: Option<String>,
|
2016-11-25 03:37:27 -08:00
|
|
|
pub user_agent: String,
|
2016-11-30 07:01:11 -08:00
|
|
|
pub internet_protocol: InternetProtocol,
|
2016-10-02 18:01:46 -07:00
|
|
|
}
|
|
|
|
|
2016-11-21 00:52:27 -08:00
|
|
|
pub const DEFAULT_DB_CACHE: usize = 512;
|
|
|
|
|
2016-10-03 06:44:35 -07:00
|
|
|
pub fn parse(matches: &clap::ArgMatches) -> Result<Config, String> {
|
2016-12-01 01:40:56 -08:00
|
|
|
let print_to_console = matches.is_present("print-to-console");
|
2016-11-05 07:32:57 -07:00
|
|
|
let magic = match (matches.is_present("testnet"), matches.is_present("regtest")) {
|
|
|
|
(true, false) => Magic::Testnet,
|
|
|
|
(false, true) => Magic::Regtest,
|
|
|
|
(false, false) => Magic::Mainnet,
|
|
|
|
(true, true) => return Err("Only one testnet option can be used".into()),
|
2016-10-02 18:01:46 -07:00
|
|
|
};
|
|
|
|
|
2016-11-16 15:33:35 -08:00
|
|
|
let (in_connections, out_connections) = match magic {
|
2016-11-25 09:38:21 -08:00
|
|
|
Magic::Testnet | Magic::Mainnet | Magic::Other(_) => (10, 10),
|
2016-11-16 15:33:35 -08:00
|
|
|
Magic::Regtest => (1, 0),
|
|
|
|
};
|
|
|
|
|
|
|
|
let p2p_threads = match magic {
|
2016-11-25 09:38:21 -08:00
|
|
|
Magic::Testnet | Magic::Mainnet | Magic::Other(_) => 4,
|
2016-11-16 15:33:35 -08:00
|
|
|
Magic::Regtest => 1,
|
|
|
|
};
|
|
|
|
|
2016-11-25 03:37:27 -08:00
|
|
|
// to skip idiotic 30 seconds delay in test-scripts
|
|
|
|
let user_agent = match magic {
|
2016-11-25 09:38:21 -08:00
|
|
|
Magic::Testnet | Magic::Mainnet | Magic::Other(_) => USER_AGENT,
|
2016-11-25 03:37:27 -08:00
|
|
|
Magic::Regtest => REGTEST_USER_AGENT,
|
|
|
|
};
|
|
|
|
|
2016-10-02 18:01:46 -07:00
|
|
|
let port = match matches.value_of("port") {
|
|
|
|
Some(port) => try!(port.parse().map_err(|_| "Invalid port".to_owned())),
|
2016-10-03 06:44:35 -07:00
|
|
|
None => magic.port(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let connect = match matches.value_of("connect") {
|
2016-11-02 00:25:09 -07:00
|
|
|
Some(s) => Some(try!(match s.parse::<net::SocketAddr>() {
|
|
|
|
Err(_) => s.parse::<net::IpAddr>()
|
|
|
|
.map(|ip| net::SocketAddr::new(ip, magic.port()))
|
|
|
|
.map_err(|_| "Invalid connect".to_owned()),
|
|
|
|
Ok(a) => Ok(a),
|
|
|
|
})),
|
2016-10-03 06:44:35 -07:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2016-11-30 05:37:17 -08:00
|
|
|
let seednodes = match matches.value_of("seednode") {
|
|
|
|
Some(s) => vec![try!(s.parse().map_err(|_| "Invalid seednode".to_owned()))],
|
|
|
|
None => match magic {
|
|
|
|
Magic::Mainnet => mainnet_seednodes().into_iter().map(Into::into).collect(),
|
|
|
|
Magic::Testnet => testnet_seednodes().into_iter().map(Into::into).collect(),
|
|
|
|
Magic::Other(_) | Magic::Regtest => Vec::new(),
|
|
|
|
},
|
2016-10-02 18:01:46 -07:00
|
|
|
};
|
|
|
|
|
2016-11-21 00:52:27 -08:00
|
|
|
let db_cache = match matches.value_of("db-cache") {
|
|
|
|
Some(s) => try!(s.parse().map_err(|_| "Invalid cache size - should be number in MB".to_owned())),
|
|
|
|
None => DEFAULT_DB_CACHE,
|
|
|
|
};
|
|
|
|
|
2016-11-24 22:58:04 -08:00
|
|
|
let data_dir = match matches.value_of("data-dir") {
|
|
|
|
Some(s) => Some(try!(s.parse().map_err(|_| "Invalid data-dir".to_owned()))),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2016-11-30 07:01:11 -08:00
|
|
|
let only_net = match matches.value_of("only-net") {
|
|
|
|
Some(s) => try!(s.parse()),
|
|
|
|
None => InternetProtocol::default(),
|
|
|
|
};
|
|
|
|
|
2016-10-02 18:01:46 -07:00
|
|
|
let config = Config {
|
2016-10-03 06:44:35 -07:00
|
|
|
print_to_console: print_to_console,
|
2016-10-02 18:01:46 -07:00
|
|
|
magic: magic,
|
|
|
|
port: port,
|
2016-10-03 06:44:35 -07:00
|
|
|
connect: connect,
|
2016-11-30 05:37:17 -08:00
|
|
|
seednodes: seednodes,
|
2016-11-16 15:33:35 -08:00
|
|
|
inbound_connections: in_connections,
|
|
|
|
outbound_connections: out_connections,
|
|
|
|
p2p_threads: p2p_threads,
|
2016-11-21 00:52:27 -08:00
|
|
|
db_cache: db_cache,
|
2016-11-24 22:58:04 -08:00
|
|
|
data_dir: data_dir,
|
2016-11-25 03:37:27 -08:00
|
|
|
user_agent: user_agent.to_string(),
|
2016-11-30 07:01:11 -08:00
|
|
|
internet_protocol: only_net,
|
2016-10-02 18:01:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(config)
|
|
|
|
}
|