Restructure heavily

This commit is contained in:
Paul Hauner 2018-08-07 10:08:39 +10:00
parent 819527038e
commit 0064efc402
9 changed files with 78 additions and 31 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
/target
target/
**/*.rs.bk
Cargo.lock
*.pk

View File

@ -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"

24
network-libp2p/Cargo.toml Normal file
View File

@ -0,0 +1,24 @@
[package]
name = "network-libp2p"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
[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"

View File

@ -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;

View File

@ -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 <Self, Box<Error>> {
/// 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 <Self, Box<Error>>
{
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<SecretKey, Box<Error>>
{
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<SecretKey, Box<Error>>
{
@ -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)

View File

@ -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,

View File

@ -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::<u16>() {
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.");
}

View File

@ -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;