Tidy up p2p code somewhat
This commit is contained in:
parent
68af2011b5
commit
c1d93d073d
27
src/main.rs
27
src/main.rs
@ -10,14 +10,11 @@ pub mod pubkeystore;
|
|||||||
pub mod state;
|
pub mod state;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
use p2p::keys;
|
|
||||||
use p2p::floodsub;
|
|
||||||
use slog::Drain;
|
use slog::Drain;
|
||||||
use clap::{ App, SubCommand};
|
use clap::{ App, SubCommand};
|
||||||
use std::sync::Arc;
|
use p2p::config::NetworkConfig;
|
||||||
use std::time::Duration;
|
use p2p::floodsub;
|
||||||
use libp2p_peerstore::{ PeerAccess, Peerstore };
|
use p2p::state::NetworkState;
|
||||||
use libp2p_peerstore::memory_peerstore::MemoryPeerstore;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let decorator = slog_term::TermDecorator::new().build();
|
let decorator = slog_term::TermDecorator::new().build();
|
||||||
@ -33,22 +30,12 @@ fn main() {
|
|||||||
.about("Generates a new set of random keys for p2p dev.")
|
.about("Generates a new set of random keys for p2p dev.")
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
|
let config = NetworkConfig::default();
|
||||||
if let Some(_) = matches.subcommand_matches("generate-keys") {
|
if let Some(_) = matches.subcommand_matches("generate-keys") {
|
||||||
keys::generate_keys(&log).expect("Failed to generate keys");
|
// keys::generate_keys(&log).expect("Failed to generate keys");
|
||||||
} else {
|
} else {
|
||||||
let (s256k1_public, _s256k1_secret) = keys::load_local_keys(&log);
|
let state = NetworkState::new(&config).expect("setup failed");
|
||||||
let peer_id = keys::peer_id_from_pub_key(&s256k1_public);
|
floodsub::listen(state, &log);
|
||||||
let bootstrap_peer_id =
|
|
||||||
keys::peer_id_from_pub_key(&keys::load_bootstrap_pk(&log));
|
|
||||||
|
|
||||||
let peer_store = Arc::new(MemoryPeerstore::empty());
|
|
||||||
|
|
||||||
peer_store.peer_or_create(&bootstrap_peer_id).add_addr(
|
|
||||||
"/ip4/127.0.0.1/tcp/10101/ws".parse().unwrap(),
|
|
||||||
Duration::from_secs(3600 * 24 * 356)
|
|
||||||
);
|
|
||||||
|
|
||||||
floodsub::listen(peer_id, peer_store, &log);
|
|
||||||
}
|
}
|
||||||
info!(log, "Exiting.");
|
info!(log, "Exiting.");
|
||||||
}
|
}
|
||||||
|
17
src/p2p/config.rs
Normal file
17
src/p2p/config.rs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
use super::libp2p_core::Multiaddr;
|
||||||
|
|
||||||
|
pub struct NetworkConfig {
|
||||||
|
pub config_dir: String,
|
||||||
|
pub listen_multiaddr: Multiaddr,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NetworkConfig {
|
||||||
|
pub fn default() -> Self{
|
||||||
|
Self {
|
||||||
|
config_dir: ".lighthouse".to_string(),
|
||||||
|
listen_multiaddr: "/ip4/0.0.0.0/tcp/0"
|
||||||
|
.parse::<Multiaddr>().unwrap()
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
extern crate bigint;
|
extern crate bigint;
|
||||||
extern crate bytes;
|
extern crate bytes;
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
|
extern crate hex;
|
||||||
extern crate libp2p_peerstore;
|
extern crate libp2p_peerstore;
|
||||||
extern crate libp2p_identify;
|
extern crate libp2p_identify;
|
||||||
extern crate libp2p_core;
|
extern crate libp2p_core;
|
||||||
@ -14,9 +15,9 @@ extern crate tokio_io;
|
|||||||
extern crate tokio_timer;
|
extern crate tokio_timer;
|
||||||
extern crate tokio_stdin;
|
extern crate tokio_stdin;
|
||||||
|
|
||||||
|
use super::state::NetworkState;
|
||||||
use self::bigint::U512;
|
use self::bigint::U512;
|
||||||
use self::futures::{ Future, Stream };
|
use self::futures::{ Future, Stream };
|
||||||
use self::libp2p_peerstore::PeerId;
|
|
||||||
use self::libp2p_core::{ AddrComponent, Endpoint, Multiaddr, Transport, ConnectionUpgrade };
|
use self::libp2p_core::{ AddrComponent, Endpoint, Multiaddr, Transport, ConnectionUpgrade };
|
||||||
use self::libp2p_floodsub::{ FloodSubUpgrade, FloodSubFuture };
|
use self::libp2p_floodsub::{ FloodSubUpgrade, FloodSubFuture };
|
||||||
use self::libp2p_kad::{ KademliaUpgrade, KademliaProcessingFuture};
|
use self::libp2p_kad::{ KademliaUpgrade, KademliaProcessingFuture};
|
||||||
@ -26,16 +27,14 @@ use std::sync::{ Arc, RwLock };
|
|||||||
use std::time::{ Duration, Instant };
|
use std::time::{ Duration, Instant };
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::io::Error as IoError;
|
use std::io::Error as IoError;
|
||||||
use libp2p_peerstore::memory_peerstore::MemoryPeerstore;
|
|
||||||
use self::tokio_io::{ AsyncRead, AsyncWrite };
|
use self::tokio_io::{ AsyncRead, AsyncWrite };
|
||||||
use self::bytes::Bytes;
|
use self::bytes::Bytes;
|
||||||
|
|
||||||
pub fn listen(peer_id: PeerId,
|
pub fn listen(state: NetworkState, log: &Logger)
|
||||||
peer_store: Arc<MemoryPeerstore>,
|
|
||||||
log: &Logger)
|
|
||||||
{
|
{
|
||||||
let listen_multiaddr: Multiaddr = "/ip4/0.0.0.0/tcp/0"
|
let peer_store = state.peer_store;
|
||||||
.parse::<Multiaddr>().expect("Failed to parse listen multiaddr.");
|
let peer_id = state.peer_id;
|
||||||
|
let listen_multiaddr = state.listen_multiaddr;
|
||||||
|
|
||||||
info!(log, "Local PeerId: {:?}", peer_id);
|
info!(log, "Local PeerId: {:?}", peer_id);
|
||||||
|
|
||||||
|
@ -8,6 +8,9 @@ use std::io::prelude::*;
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use slog::Logger;
|
use slog::Logger;
|
||||||
|
|
||||||
|
use std::io::Error as IoError;
|
||||||
|
use std::path::Path;
|
||||||
|
use super::config::NetworkConfig;
|
||||||
use self::secp256k1::key::{ SecretKey, PublicKey };
|
use self::secp256k1::key::{ SecretKey, PublicKey };
|
||||||
use self::libp2p_peerstore::PeerId;
|
use self::libp2p_peerstore::PeerId;
|
||||||
|
|
||||||
@ -15,16 +18,13 @@ const LOCAL_PK_FILE: &str = "local.pk";
|
|||||||
const LOCAL_SK_FILE: &str = "local.sk";
|
const LOCAL_SK_FILE: &str = "local.sk";
|
||||||
const BOOTSTRAP_PK_FILE: &str = "bootstrap.pk";
|
const BOOTSTRAP_PK_FILE: &str = "bootstrap.pk";
|
||||||
|
|
||||||
pub fn peer_id_from_pub_key(pk: &PublicKey) -> PeerId {
|
|
||||||
let curve = get_curve();
|
|
||||||
PeerId::from_public_key(&pk.serialize_vec(&curve, false))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_curve() -> secp256k1::Secp256k1 { secp256k1::Secp256k1::new() }
|
fn get_curve() -> secp256k1::Secp256k1 { secp256k1::Secp256k1::new() }
|
||||||
|
|
||||||
/// Generates a new public and secret key pair and writes them to
|
/// Generates a new public and secret key pair and writes them to
|
||||||
/// individual files.
|
/// individual files.
|
||||||
pub fn generate_keys(log: &Logger) -> std::io::Result<()> {
|
pub fn generate_keys(config: NetworkConfig, log: &Logger)
|
||||||
|
-> Result<(), IoError>
|
||||||
|
{
|
||||||
info!(log, "Generating keys...");
|
info!(log, "Generating keys...");
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let curve = get_curve();
|
let curve = get_curve();
|
||||||
@ -42,32 +42,3 @@ pub fn generate_keys(log: &Logger) -> std::io::Result<()> {
|
|||||||
s_file.write(s_string.as_bytes())?;
|
s_file.write(s_string.as_bytes())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_bootstrap_pk(log: &Logger) -> PublicKey {
|
|
||||||
info!(log, "Loading boostrap public key from filesystem...");
|
|
||||||
load_pk_from_file(BOOTSTRAP_PK_FILE)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load_local_keys(log: &Logger) -> (PublicKey, SecretKey) {
|
|
||||||
info!(log, "Loading local keys from filesystem...");
|
|
||||||
(load_pk_from_file(LOCAL_PK_FILE), load_sk_from_file(LOCAL_SK_FILE))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn load_sk_from_file(file: &str) -> SecretKey {
|
|
||||||
let vec = load_vec_from_hex_file(file);
|
|
||||||
let curve = get_curve();
|
|
||||||
SecretKey::from_slice(&curve, &vec).expect("secret key invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn load_pk_from_file(file: &str) -> PublicKey {
|
|
||||||
let vec = load_vec_from_hex_file(file);
|
|
||||||
let curve = get_curve();
|
|
||||||
PublicKey::from_slice(&curve, &vec).expect("public key invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn load_vec_from_hex_file(file: &str) -> Vec<u8> {
|
|
||||||
let mut contents = String::new();
|
|
||||||
let mut file = File::open(file).expect("key not found");
|
|
||||||
file.read_to_string(&mut contents).expect("error reading from file");
|
|
||||||
hex::decode(contents).expect("public key corrupt")
|
|
||||||
}
|
|
||||||
|
@ -1,2 +1,9 @@
|
|||||||
|
extern crate hex;
|
||||||
|
extern crate libp2p_core;
|
||||||
|
extern crate libp2p_peerstore;
|
||||||
|
extern crate secp256k1;
|
||||||
|
|
||||||
pub mod floodsub;
|
pub mod floodsub;
|
||||||
pub mod keys;
|
pub mod state;
|
||||||
|
// pub mod keys;
|
||||||
|
pub mod config;
|
||||||
|
55
src/p2p/state.rs
Normal file
55
src/p2p/state.rs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
use std::io::Read;
|
||||||
|
use std::error::Error;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use super::hex;
|
||||||
|
use super::secp256k1::Secp256k1;
|
||||||
|
use super::secp256k1::key::{ SecretKey, PublicKey };
|
||||||
|
use super::libp2p_core::Multiaddr;
|
||||||
|
use super::libp2p_peerstore::PeerId;
|
||||||
|
use super::libp2p_peerstore::json_peerstore::JsonPeerstore;
|
||||||
|
use super::config::NetworkConfig;
|
||||||
|
|
||||||
|
|
||||||
|
const PEERS_FILE: &str = "peerstore.json";
|
||||||
|
const LOCAL_SK_FILE: &str = "local.sk";
|
||||||
|
|
||||||
|
pub struct NetworkState {
|
||||||
|
pub pubkey: PublicKey,
|
||||||
|
pub seckey: SecretKey,
|
||||||
|
pub peer_id: PeerId,
|
||||||
|
pub listen_multiaddr: Multiaddr,
|
||||||
|
pub peer_store: Arc<JsonPeerstore>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NetworkState {
|
||||||
|
pub fn new(config: &NetworkConfig) -> Result <Self, Box<Error>> {
|
||||||
|
let curve = Secp256k1::new();
|
||||||
|
let seckey = {
|
||||||
|
let path = Path::new(&config.config_dir).join(LOCAL_SK_FILE);
|
||||||
|
let mut contents = String::new();
|
||||||
|
let mut file = File::open(path)?;
|
||||||
|
file.read_to_string(&mut contents)?;
|
||||||
|
let vec = hex::decode(contents)?;
|
||||||
|
SecretKey::from_slice(&curve, &vec)?
|
||||||
|
};
|
||||||
|
let pubkey = PublicKey::from_secret_key(&curve, &seckey)?;
|
||||||
|
let peer_id = PeerId::from_public_key(
|
||||||
|
&pubkey.serialize_vec(&curve, false));
|
||||||
|
let peer_store = {
|
||||||
|
let path = Path::new(&config.config_dir).join(PEERS_FILE);
|
||||||
|
let base = JsonPeerstore::new(path)?;
|
||||||
|
Arc::new(base)
|
||||||
|
};
|
||||||
|
let listen_multiaddr = "/ip4/0.0.0.0/tcp/0".parse::<Multiaddr>()?;
|
||||||
|
Ok(Self {
|
||||||
|
seckey,
|
||||||
|
pubkey,
|
||||||
|
peer_id,
|
||||||
|
listen_multiaddr,
|
||||||
|
peer_store,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user