From 0064efc4027dd8f80f2d6e43d4a74215e74e3381 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Tue, 7 Aug 2018 10:08:39 +1000 Subject: [PATCH] Restructure heavily --- .gitignore | 2 +- Cargo.toml | 1 + network-libp2p/Cargo.toml | 24 +++++++++++++ src/p2p/mod.rs => network-libp2p/src/lib.rs | 1 + {src/p2p => network-libp2p/src}/service.rs | 0 {src/p2p => network-libp2p/src}/state.rs | 39 +++++++++++++-------- src/config/mod.rs | 4 +-- src/main.rs | 36 ++++++++++++------- src/sync/mod.rs | 2 +- 9 files changed, 78 insertions(+), 31 deletions(-) create mode 100644 network-libp2p/Cargo.toml rename src/p2p/mod.rs => network-libp2p/src/lib.rs (92%) rename {src/p2p => network-libp2p/src}/service.rs (100%) rename {src/p2p => network-libp2p/src}/state.rs (73%) diff --git a/.gitignore b/.gitignore index 01154ae902..9050bdab9d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -/target +target/ **/*.rs.bk Cargo.lock *.pk diff --git a/Cargo.toml b/Cargo.toml index a15c84fe07..233819db9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ libp2p-tcp-transport = { git = "https://github.com/tomaka/libp2p-rs", branch ="z libp2p-floodsub = { git = "https://github.com/tomaka/libp2p-rs", branch ="zksummit" } libp2p-identify = { git = "https://github.com/tomaka/libp2p-rs", branch ="zksummit" } libp2p-kad = { git = "https://github.com/tomaka/libp2p-rs", branch ="zksummit" } +network-libp2p = { path = "network-libp2p" } # TODO: Bring pem module internal; too risky to have as external dep. pem = "0.5.0" rand = "0.3" diff --git a/network-libp2p/Cargo.toml b/network-libp2p/Cargo.toml new file mode 100644 index 0000000000..50f5d26b88 --- /dev/null +++ b/network-libp2p/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "network-libp2p" +version = "0.1.0" +authors = ["Paul Hauner "] + +[dependencies] +bigint = "4.2" +bytes = "" +eth-secp256k1 = { git = "https://github.com/paritytech/rust-secp256k1" } +futures = "0.1.23" +libp2p-peerstore = { git = "https://github.com/tomaka/libp2p-rs", branch ="zksummit" } +libp2p-core = { git = "https://github.com/tomaka/libp2p-rs", branch ="zksummit" } +libp2p-mplex = { git = "https://github.com/tomaka/libp2p-rs", branch ="zksummit" } +libp2p-tcp-transport = { git = "https://github.com/tomaka/libp2p-rs", branch ="zksummit" } +libp2p-floodsub = { git = "https://github.com/tomaka/libp2p-rs", branch ="zksummit" } +libp2p-identify = { git = "https://github.com/tomaka/libp2p-rs", branch ="zksummit" } +libp2p-kad = { git = "https://github.com/tomaka/libp2p-rs", branch ="zksummit" } +pem = "0.5.0" +rand = "0.3" +slog = "^2.2.3" +tokio-core = "0.1" +tokio-io = "0.1" +tokio-stdin = "0.1" +tokio-timer = "0.1" diff --git a/src/p2p/mod.rs b/network-libp2p/src/lib.rs similarity index 92% rename from src/p2p/mod.rs rename to network-libp2p/src/lib.rs index a3aa609ce1..f7a50f5207 100644 --- a/src/p2p/mod.rs +++ b/network-libp2p/src/lib.rs @@ -2,6 +2,7 @@ extern crate libp2p_core; extern crate libp2p_peerstore; extern crate pem; extern crate secp256k1; +#[macro_use] extern crate slog; pub mod service; diff --git a/src/p2p/service.rs b/network-libp2p/src/service.rs similarity index 100% rename from src/p2p/service.rs rename to network-libp2p/src/service.rs diff --git a/src/p2p/state.rs b/network-libp2p/src/state.rs similarity index 73% rename from src/p2p/state.rs rename to network-libp2p/src/state.rs index 18bf7433b7..713def77ce 100644 --- a/src/p2p/state.rs +++ b/network-libp2p/src/state.rs @@ -3,10 +3,10 @@ extern crate rand; use std::io::{ Read, Write }; use std::error::Error; use std::fs::File; +use std::path::{ Path, PathBuf }; use std::sync::Arc; use std::time::Duration; -use super::super::config::LighthouseConfig; use super::libp2p_core::Multiaddr; use super::libp2p_peerstore::{ Peerstore, PeerAccess, PeerId }; use super::libp2p_peerstore::json_peerstore::JsonPeerstore; @@ -15,12 +15,14 @@ use super::secp256k1::Secp256k1; use super::secp256k1::key::{ SecretKey, PublicKey }; use super::slog::Logger; - +/// Location of the libp2p peerstore inside the Network base dir. const PEERS_FILE: &str = "peerstore.json"; +/// Location of the libp2p local peer secret key inside the Network base dir. const LOCAL_PEM_FILE: &str = "local_peer_id.pem"; +/// Represents the present state of a libp2p network. pub struct NetworkState { - pub config: LighthouseConfig, + pub base_dir: PathBuf, pub pubkey: PublicKey, pub seckey: SecretKey, pub peer_id: PeerId, @@ -29,29 +31,36 @@ pub struct NetworkState { } impl NetworkState { - pub fn new(config: LighthouseConfig, log: &Logger) -> Result > { + /// Create a new libp2p network state. Used to initialize + /// network service. + pub fn new( + // config: LighthouseConfig, + base_dir: &Path, + listen_port: &u16, + log: &Logger) + -> Result > + { let curve = Secp256k1::new(); let seckey = match - NetworkState::load_secret_key_from_pem_file(&config, &curve) + NetworkState::load_secret_key_from_pem_file(base_dir, &curve) { Ok(k) => k, - _ => NetworkState::generate_new_secret_key(&config, &curve)? + _ => NetworkState::generate_new_secret_key(base_dir, &curve)? }; let pubkey = PublicKey::from_secret_key(&curve, &seckey)?; let peer_id = PeerId::from_public_key( &pubkey.serialize_vec(&curve, false)); info!(log, "Loaded keys"; "peer_id" => &peer_id.to_base58()); let peer_store = { - let path = config.data_dir.join(PEERS_FILE); + let path = base_dir.join(PEERS_FILE); let base = JsonPeerstore::new(path)?; Arc::new(base) }; info!(log, "Loaded peerstore"; "peer_count" => &peer_store.peers().count()); - // let listen_multiaddr = config.listen_multiaddr.clone(); let listen_multiaddr = - NetworkState::multiaddr_on_port(&config.p2p_listen_port); + NetworkState::multiaddr_on_port(&listen_port.to_string()); Ok(Self { - config: config, + base_dir: PathBuf::from(base_dir), seckey, pubkey, peer_id, @@ -75,10 +84,12 @@ impl NetworkState { } /// Instantiate a SecretKey from a .pem file on disk. - pub fn load_secret_key_from_pem_file(config: &LighthouseConfig, curve: &Secp256k1) + pub fn load_secret_key_from_pem_file( + base_dir: &Path, + curve: &Secp256k1) -> Result> { - let path = config.data_dir.join(LOCAL_PEM_FILE); + let path = base_dir.join(LOCAL_PEM_FILE); let mut contents = String::new(); let mut file = File::open(path)?; file.read_to_string(&mut contents)?; @@ -89,7 +100,7 @@ impl NetworkState { /// Generate a new SecretKey and store it on disk as a .pem file. pub fn generate_new_secret_key( - config: &LighthouseConfig, + base_dir: &Path, curve: &Secp256k1) -> Result> { @@ -100,7 +111,7 @@ impl NetworkState { contents: sk[..].to_vec() }; let s_string = pem::encode(&pem_key); - let path = config.data_dir.join(LOCAL_PEM_FILE); + let path = base_dir.join(LOCAL_PEM_FILE); let mut s_file = File::create(path)?; s_file.write(s_string.as_bytes())?; Ok(sk) diff --git a/src/config/mod.rs b/src/config/mod.rs index af7af84a93..31f77e2a71 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -4,7 +4,7 @@ use std::path::PathBuf; #[derive(Clone)] pub struct LighthouseConfig { pub data_dir: PathBuf, - pub p2p_listen_port: String, + pub p2p_listen_port: u16, } const DEFAULT_LIGHTHOUSE_DIR: &str = ".lighthouse"; @@ -17,7 +17,7 @@ impl LighthouseConfig { .expect("Unable to determine home dir."); home.join(DEFAULT_LIGHTHOUSE_DIR) }; - let p2p_listen_port = "0".to_string(); + let p2p_listen_port = 0; Self { data_dir, p2p_listen_port, diff --git a/src/main.rs b/src/main.rs index 7644d10992..1bd48684a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,8 @@ extern crate slog_term; extern crate slog_async; extern crate clap; extern crate libp2p_peerstore; +extern crate network_libp2p; -pub mod p2p; pub mod pubkeystore; pub mod state; pub mod sync; @@ -17,8 +17,8 @@ use std::path::PathBuf; use slog::Drain; use clap::{ Arg, App }; use config::LighthouseConfig; -use p2p::service::NetworkService; -use p2p::state::NetworkState; +use network_libp2p::service::NetworkService; +use network_libp2p::state::NetworkState; use sync::sync_start; fn main() { @@ -51,17 +51,27 @@ fn main() { } // Custom p2p listen port - if let Some(port) = matches.value_of("port") { - config.p2p_listen_port = port.to_string(); + if let Some(port_str) = matches.value_of("port") { + if let Ok(port) = port_str.parse::() { + config.p2p_listen_port = port; + } else { + error!(log, "Invalid port"; "port" => port_str); + return; + } } + + // Log configuration + info!(log, ""; + "data_dir" => &config.data_dir.to_str(), + "port" => &config.p2p_listen_port); + + let state = NetworkState::new( + &config.data_dir, + &config.p2p_listen_port, + &log) + .expect("setup failed"); + let (service, net_rx) = NetworkService::new(state, log.new(o!())); + sync_start(service, net_rx, log.new(o!())); - info!(log, ""; "data_dir" => &config.data_dir.to_str()); - if let Some(_) = matches.subcommand_matches("generate-keys") { - // keys::generate_keys(&log).expect("Failed to generate keys"); - } else { - let mut state = NetworkState::new(config, &log).expect("setup failed"); - let (service, net_rx) = NetworkService::new(state, log.new(o!())); - sync_start(service, net_rx, log.new(o!())); - } info!(log, "Exiting."); } diff --git a/src/sync/mod.rs b/src/sync/mod.rs index 6ccceeb671..35b8e7cfe7 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -2,7 +2,7 @@ extern crate futures; extern crate slog; extern crate tokio; -use super::p2p::service::NetworkService; +use super::network_libp2p::service::NetworkService; use self::futures::sync::mpsc::UnboundedReceiver; use self::futures::Stream; use slog::Logger;