Merge pull request #204 from ethcore/gav

Argument parsing from CLI
This commit is contained in:
Arkadiy Paronyan 2016-01-24 19:15:18 +01:00
commit 0e296e78bd
8 changed files with 72 additions and 18 deletions

View File

@ -20,6 +20,7 @@ time = "0.1"
evmjit = { path = "rust-evmjit", optional = true } evmjit = { path = "rust-evmjit", optional = true }
ethash = { path = "ethash" } ethash = { path = "ethash" }
num_cpus = "0.2" num_cpus = "0.2"
docopt = "0.6"
ctrlc = "1.0" ctrlc = "1.0"
clippy = "0.0.37" clippy = "0.0.37"

View File

@ -26,6 +26,11 @@
"gasLimit": "0x1388", "gasLimit": "0x1388",
"stateRoot": "0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544" "stateRoot": "0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544"
}, },
"nodes": [
"enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303",
"enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f73104b9fdeb090af9583d3391b7f10cb2ba9e26865dd5fca4fcdc0fb1e3b723c786@54.94.239.50:30303",
"enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082@52.74.57.123:30303"
],
"accounts": { "accounts": {
"0000000000000000000000000000000000000001": { "builtin": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } }, "0000000000000000000000000000000000000001": { "builtin": { "name": "ecrecover", "linear": { "base": 3000, "word": 0 } } },
"0000000000000000000000000000000000000002": { "builtin": { "name": "sha256", "linear": { "base": 60, "word": 12 } } }, "0000000000000000000000000000000000000002": { "builtin": { "name": "sha256", "linear": { "base": 60, "word": 12 } } },

View File

@ -1,6 +1,11 @@
#![feature(plugin)]
// TODO: uncomment once this can be made to work.
//#![plugin(docopt_macros)]
extern crate docopt;
extern crate rustc_serialize;
extern crate ethcore_util as util; extern crate ethcore_util as util;
extern crate ethcore; extern crate ethcore;
extern crate rustc_serialize;
extern crate log; extern crate log;
extern crate env_logger; extern crate env_logger;
extern crate ctrlc; extern crate ctrlc;
@ -15,8 +20,27 @@ use ethcore::service::{ClientService, NetSyncMessage};
use ethcore::ethereum; use ethcore::ethereum;
use ethcore::blockchain::CacheSize; use ethcore::blockchain::CacheSize;
use ethcore::sync::EthSync; use ethcore::sync::EthSync;
use docopt::Docopt;
fn setup_log() { const USAGE: &'static str = "
Parity. Ethereum Client.
Usage:
parity [options]
parity [options] <enode>...
Options:
-l --logging LOGGING Specify the logging level
-h --help Show this screen.
";
#[derive(Debug, RustcDecodable)]
struct Args {
arg_enode: Option<Vec<String>>,
flag_logging: Option<String>,
}
fn setup_log(init: &Option<String>) {
let mut builder = LogBuilder::new(); let mut builder = LogBuilder::new();
builder.filter(None, LogLevelFilter::Info); builder.filter(None, LogLevelFilter::Info);
@ -24,13 +48,26 @@ fn setup_log() {
builder.parse(&env::var("RUST_LOG").unwrap()); builder.parse(&env::var("RUST_LOG").unwrap());
} }
if let &Some(ref x) = init {
builder.parse(x);
}
builder.init().unwrap(); builder.init().unwrap();
} }
fn main() { fn main() {
setup_log(); let args: Args = Docopt::new(USAGE).and_then(|d| d.decode()).unwrap_or_else(|e| e.exit());
setup_log(&args.flag_logging);
let spec = ethereum::new_frontier(); let spec = ethereum::new_frontier();
let mut service = ClientService::start(spec).unwrap();
let init_nodes = match &args.arg_enode {
&None => spec.nodes().clone(),
&Some(ref enodes) => enodes.clone(),
};
let mut service = ClientService::start(spec, &init_nodes).unwrap();
let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default(), sync: service.sync() }); let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default(), sync: service.sync() });
service.io().register_handler(io_handler).expect("Error registering IO handler"); service.io().register_handler(io_handler).expect("Error registering IO handler");

View File

@ -91,6 +91,8 @@ extern crate evmjit;
#[macro_use] #[macro_use]
extern crate ethcore_util as util; extern crate ethcore_util as util;
// NOTE: Add doc parser exception for these pub declarations.
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub mod common; pub mod common;
/// TODO [Tomusdrw] Please document me /// TODO [Tomusdrw] Please document me

View File

@ -26,8 +26,8 @@ pub struct ClientService {
impl ClientService { impl ClientService {
/// Start the service in a separate thread. /// Start the service in a separate thread.
pub fn start(spec: Spec) -> Result<ClientService, Error> { pub fn start(spec: Spec, init_nodes: &[String]) -> Result<ClientService, Error> {
let mut net_service = try!(NetworkService::start()); let mut net_service = try!(NetworkService::start(init_nodes));
info!("Starting {}", net_service.host_info()); info!("Starting {}", net_service.host_info());
info!("Configured for {} using {} engine", spec.name, spec.engine_name); info!("Configured for {} using {} engine", spec.name, spec.engine_name);
let mut dir = env::home_dir().unwrap(); let mut dir = env::home_dir().unwrap();
@ -47,6 +47,11 @@ impl ClientService {
}) })
} }
/// Get the network service.
pub fn add_node(&mut self, _enode: &str) {
unimplemented!();
}
/// TODO [arkpar] Please document me /// TODO [arkpar] Please document me
pub fn io(&mut self) -> &mut IoService<NetSyncMessage> { pub fn io(&mut self) -> &mut IoService<NetSyncMessage> {
self.net_service.io() self.net_service.io()

View File

@ -73,6 +73,9 @@ pub struct Spec {
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub engine_name: String, pub engine_name: String,
/// Known nodes on the network in enode format.
pub nodes: Vec<String>,
// Parameters concerning operation of the specific engine we're using. // Parameters concerning operation of the specific engine we're using.
// Name -> RLP-encoded value // Name -> RLP-encoded value
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
@ -128,6 +131,9 @@ impl Spec {
self.state_root_memo.read().unwrap().as_ref().unwrap().clone() self.state_root_memo.read().unwrap().as_ref().unwrap().clone()
} }
/// Get the known knodes of the network in enode format.
pub fn nodes(&self) -> &Vec<String> { &self.nodes }
/// TODO [Gav Wood] Please document me /// TODO [Gav Wood] Please document me
pub fn genesis_header(&self) -> Header { pub fn genesis_header(&self) -> Header {
Header { Header {
@ -197,6 +203,10 @@ impl FromJson for Spec {
} }
} }
let nodes = if let Some(&Json::Array(ref ns)) = json.find("nodes") {
ns.iter().filter_map(|n| if let Json::String(ref s) = *n { Some(s.clone()) } else {None}).collect()
} else { Vec::new() };
let genesis = &json["genesis"];//.as_object().expect("No genesis object in JSON"); let genesis = &json["genesis"];//.as_object().expect("No genesis object in JSON");
let (seal_fields, seal_rlp) = { let (seal_fields, seal_rlp) = {
@ -213,12 +223,12 @@ impl FromJson for Spec {
) )
} }
}; };
Spec { Spec {
name: json.find("name").map_or("unknown", |j| j.as_string().unwrap()).to_owned(), name: json.find("name").map_or("unknown", |j| j.as_string().unwrap()).to_owned(),
engine_name: json["engineName"].as_string().unwrap().to_owned(), engine_name: json["engineName"].as_string().unwrap().to_owned(),
engine_params: json_to_rlp_map(&json["params"]), engine_params: json_to_rlp_map(&json["params"]),
nodes: nodes,
builtins: builtins, builtins: builtins,
parent_hash: H256::from_str(&genesis["parentHash"].as_string().unwrap()[2..]).unwrap(), parent_hash: H256::from_str(&genesis["parentHash"].as_string().unwrap()[2..]).unwrap(),
author: Address::from_str(&genesis["author"].as_string().unwrap()[2..]).unwrap(), author: Address::from_str(&genesis["author"].as_string().unwrap()[2..]).unwrap(),

View File

@ -252,7 +252,7 @@ impl<Message> Host<Message> where Message: Send + Sync + Clone {
// Setup the server socket // Setup the server socket
let tcp_listener = TcpListener::bind(&addr).unwrap(); let tcp_listener = TcpListener::bind(&addr).unwrap();
let udp_socket = UdpSocket::bound(&addr).unwrap(); let udp_socket = UdpSocket::bound(&addr).unwrap();
let mut host = Host::<Message> { let host = Host::<Message> {
info: RwLock::new(HostInfo { info: RwLock::new(HostInfo {
keys: KeyPair::create().unwrap(), keys: KeyPair::create().unwrap(),
config: config, config: config,
@ -278,14 +278,6 @@ impl<Message> Host<Message> where Message: Send + Sync + Clone {
Some(iface) => config.public_address = iface.addr.unwrap(), Some(iface) => config.public_address = iface.addr.unwrap(),
None => warn!("No public network interface"), None => warn!("No public network interface"),
*/ */
// self.add_node("enode://a9a921de2ff09a9a4d38b623c67b2d6b477a8e654ae95d874750cbbcb31b33296496a7b4421934e2629269e180823e52c15c2b19fc59592ec51ffe4f2de76ed7@127.0.0.1:30303");
// GO bootnodes
host.add_node("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303"); // IE
host.add_node("enode://de471bccee3d042261d52e9bff31458daecc406142b401d4cd848f677479f73104b9fdeb090af9583d3391b7f10cb2ba9e26865dd5fca4fcdc0fb1e3b723c786@54.94.239.50:30303"); // BR
host.add_node("enode://1118980bf48b0a3640bdba04e0fe78b1add18e1cd99bf22d53daac1fd9972ad650df52176e7c7d89d1114cfef2bc23a2959aa54998a46afcf7d91809f0855082@52.74.57.123:30303"); // SG
// ETH/DEV cpp-ethereum (poc-9.ethdev.com)
host.add_node("enode://979b7fa28feeb35a4741660a16076f1943202cb72b6af70d327f053e248bab9ba81760f39d0701ef1d8f89cc1fbd2cacba0710a12cd5314d5e0c9021aa3637f9@5.1.83.226:30303");
host host
} }

View File

@ -14,9 +14,11 @@ pub struct NetworkService<Message> where Message: Send + Sync + Clone + 'static
impl<Message> NetworkService<Message> where Message: Send + Sync + Clone + 'static { impl<Message> NetworkService<Message> where Message: Send + Sync + Clone + 'static {
/// Starts IO event loop /// Starts IO event loop
pub fn start() -> Result<NetworkService<Message>, UtilError> { pub fn start(init_nodes: &[String]) -> Result<NetworkService<Message>, UtilError> {
let mut io_service = try!(IoService::<NetworkIoMessage<Message>>::start()); let mut io_service = try!(IoService::<NetworkIoMessage<Message>>::start());
let host = Arc::new(Host::new()); let mut host = Host::new();
for n in init_nodes { host.add_node(&n); }
let host = Arc::new(host);
let host_info = host.client_version(); let host_info = host.client_version();
info!("NetworkService::start(): id={:?}", host.client_id()); info!("NetworkService::start(): id={:?}", host.client_id());
try!(io_service.register_handler(host)); try!(io_service.register_handler(host));