Restructure heavily
This commit is contained in:
parent
819527038e
commit
0064efc402
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,4 @@
|
|||||||
/target
|
target/
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
*.pk
|
*.pk
|
||||||
|
@ -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-floodsub = { git = "https://github.com/tomaka/libp2p-rs", branch ="zksummit" }
|
||||||
libp2p-identify = { 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" }
|
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.
|
# TODO: Bring pem module internal; too risky to have as external dep.
|
||||||
pem = "0.5.0"
|
pem = "0.5.0"
|
||||||
rand = "0.3"
|
rand = "0.3"
|
||||||
|
24
network-libp2p/Cargo.toml
Normal file
24
network-libp2p/Cargo.toml
Normal 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"
|
@ -2,6 +2,7 @@ extern crate libp2p_core;
|
|||||||
extern crate libp2p_peerstore;
|
extern crate libp2p_peerstore;
|
||||||
extern crate pem;
|
extern crate pem;
|
||||||
extern crate secp256k1;
|
extern crate secp256k1;
|
||||||
|
#[macro_use]
|
||||||
extern crate slog;
|
extern crate slog;
|
||||||
|
|
||||||
pub mod service;
|
pub mod service;
|
@ -3,10 +3,10 @@ extern crate rand;
|
|||||||
use std::io::{ Read, Write };
|
use std::io::{ Read, Write };
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::path::{ Path, PathBuf };
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use super::super::config::LighthouseConfig;
|
|
||||||
use super::libp2p_core::Multiaddr;
|
use super::libp2p_core::Multiaddr;
|
||||||
use super::libp2p_peerstore::{ Peerstore, PeerAccess, PeerId };
|
use super::libp2p_peerstore::{ Peerstore, PeerAccess, PeerId };
|
||||||
use super::libp2p_peerstore::json_peerstore::JsonPeerstore;
|
use super::libp2p_peerstore::json_peerstore::JsonPeerstore;
|
||||||
@ -15,12 +15,14 @@ use super::secp256k1::Secp256k1;
|
|||||||
use super::secp256k1::key::{ SecretKey, PublicKey };
|
use super::secp256k1::key::{ SecretKey, PublicKey };
|
||||||
use super::slog::Logger;
|
use super::slog::Logger;
|
||||||
|
|
||||||
|
/// Location of the libp2p peerstore inside the Network base dir.
|
||||||
const PEERS_FILE: &str = "peerstore.json";
|
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";
|
const LOCAL_PEM_FILE: &str = "local_peer_id.pem";
|
||||||
|
|
||||||
|
/// Represents the present state of a libp2p network.
|
||||||
pub struct NetworkState {
|
pub struct NetworkState {
|
||||||
pub config: LighthouseConfig,
|
pub base_dir: PathBuf,
|
||||||
pub pubkey: PublicKey,
|
pub pubkey: PublicKey,
|
||||||
pub seckey: SecretKey,
|
pub seckey: SecretKey,
|
||||||
pub peer_id: PeerId,
|
pub peer_id: PeerId,
|
||||||
@ -29,29 +31,36 @@ pub struct NetworkState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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 curve = Secp256k1::new();
|
||||||
let seckey = match
|
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,
|
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 pubkey = PublicKey::from_secret_key(&curve, &seckey)?;
|
||||||
let peer_id = PeerId::from_public_key(
|
let peer_id = PeerId::from_public_key(
|
||||||
&pubkey.serialize_vec(&curve, false));
|
&pubkey.serialize_vec(&curve, false));
|
||||||
info!(log, "Loaded keys"; "peer_id" => &peer_id.to_base58());
|
info!(log, "Loaded keys"; "peer_id" => &peer_id.to_base58());
|
||||||
let peer_store = {
|
let peer_store = {
|
||||||
let path = config.data_dir.join(PEERS_FILE);
|
let path = base_dir.join(PEERS_FILE);
|
||||||
let base = JsonPeerstore::new(path)?;
|
let base = JsonPeerstore::new(path)?;
|
||||||
Arc::new(base)
|
Arc::new(base)
|
||||||
};
|
};
|
||||||
info!(log, "Loaded peerstore"; "peer_count" => &peer_store.peers().count());
|
info!(log, "Loaded peerstore"; "peer_count" => &peer_store.peers().count());
|
||||||
// let listen_multiaddr = config.listen_multiaddr.clone();
|
|
||||||
let listen_multiaddr =
|
let listen_multiaddr =
|
||||||
NetworkState::multiaddr_on_port(&config.p2p_listen_port);
|
NetworkState::multiaddr_on_port(&listen_port.to_string());
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
config: config,
|
base_dir: PathBuf::from(base_dir),
|
||||||
seckey,
|
seckey,
|
||||||
pubkey,
|
pubkey,
|
||||||
peer_id,
|
peer_id,
|
||||||
@ -75,10 +84,12 @@ impl NetworkState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Instantiate a SecretKey from a .pem file on disk.
|
/// 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>>
|
-> 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 contents = String::new();
|
||||||
let mut file = File::open(path)?;
|
let mut file = File::open(path)?;
|
||||||
file.read_to_string(&mut contents)?;
|
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.
|
/// Generate a new SecretKey and store it on disk as a .pem file.
|
||||||
pub fn generate_new_secret_key(
|
pub fn generate_new_secret_key(
|
||||||
config: &LighthouseConfig,
|
base_dir: &Path,
|
||||||
curve: &Secp256k1)
|
curve: &Secp256k1)
|
||||||
-> Result<SecretKey, Box<Error>>
|
-> Result<SecretKey, Box<Error>>
|
||||||
{
|
{
|
||||||
@ -100,7 +111,7 @@ impl NetworkState {
|
|||||||
contents: sk[..].to_vec()
|
contents: sk[..].to_vec()
|
||||||
};
|
};
|
||||||
let s_string = pem::encode(&pem_key);
|
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)?;
|
let mut s_file = File::create(path)?;
|
||||||
s_file.write(s_string.as_bytes())?;
|
s_file.write(s_string.as_bytes())?;
|
||||||
Ok(sk)
|
Ok(sk)
|
@ -4,7 +4,7 @@ use std::path::PathBuf;
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct LighthouseConfig {
|
pub struct LighthouseConfig {
|
||||||
pub data_dir: PathBuf,
|
pub data_dir: PathBuf,
|
||||||
pub p2p_listen_port: String,
|
pub p2p_listen_port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_LIGHTHOUSE_DIR: &str = ".lighthouse";
|
const DEFAULT_LIGHTHOUSE_DIR: &str = ".lighthouse";
|
||||||
@ -17,7 +17,7 @@ impl LighthouseConfig {
|
|||||||
.expect("Unable to determine home dir.");
|
.expect("Unable to determine home dir.");
|
||||||
home.join(DEFAULT_LIGHTHOUSE_DIR)
|
home.join(DEFAULT_LIGHTHOUSE_DIR)
|
||||||
};
|
};
|
||||||
let p2p_listen_port = "0".to_string();
|
let p2p_listen_port = 0;
|
||||||
Self {
|
Self {
|
||||||
data_dir,
|
data_dir,
|
||||||
p2p_listen_port,
|
p2p_listen_port,
|
||||||
|
36
src/main.rs
36
src/main.rs
@ -4,8 +4,8 @@ extern crate slog_term;
|
|||||||
extern crate slog_async;
|
extern crate slog_async;
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
extern crate libp2p_peerstore;
|
extern crate libp2p_peerstore;
|
||||||
|
extern crate network_libp2p;
|
||||||
|
|
||||||
pub mod p2p;
|
|
||||||
pub mod pubkeystore;
|
pub mod pubkeystore;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
pub mod sync;
|
pub mod sync;
|
||||||
@ -17,8 +17,8 @@ use std::path::PathBuf;
|
|||||||
use slog::Drain;
|
use slog::Drain;
|
||||||
use clap::{ Arg, App };
|
use clap::{ Arg, App };
|
||||||
use config::LighthouseConfig;
|
use config::LighthouseConfig;
|
||||||
use p2p::service::NetworkService;
|
use network_libp2p::service::NetworkService;
|
||||||
use p2p::state::NetworkState;
|
use network_libp2p::state::NetworkState;
|
||||||
use sync::sync_start;
|
use sync::sync_start;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -51,17 +51,27 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Custom p2p listen port
|
// Custom p2p listen port
|
||||||
if let Some(port) = matches.value_of("port") {
|
if let Some(port_str) = matches.value_of("port") {
|
||||||
config.p2p_listen_port = port.to_string();
|
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.");
|
info!(log, "Exiting.");
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ extern crate futures;
|
|||||||
extern crate slog;
|
extern crate slog;
|
||||||
extern crate tokio;
|
extern crate tokio;
|
||||||
|
|
||||||
use super::p2p::service::NetworkService;
|
use super::network_libp2p::service::NetworkService;
|
||||||
use self::futures::sync::mpsc::UnboundedReceiver;
|
use self::futures::sync::mpsc::UnboundedReceiver;
|
||||||
use self::futures::Stream;
|
use self::futures::Stream;
|
||||||
use slog::Logger;
|
use slog::Logger;
|
||||||
|
Loading…
Reference in New Issue
Block a user