Initial integration of discovery v5

This commit is contained in:
Age Manning 2019-06-25 18:02:11 +10:00
parent 81f0b6c238
commit a64a6c7d3a
No known key found for this signature in database
GPG Key ID: 05EED64B79E06A93
7 changed files with 172 additions and 104 deletions

View File

@ -0,0 +1,67 @@
use clap::ArgMatches;
use http_server::HttpServerConfig;
use network::NetworkConfig;
use serde_derive::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
/// The core configuration of a Lighthouse beacon node.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub data_dir: PathBuf,
pub db_type: String,
db_name: String,
pub network: network::NetworkConfig,
pub rpc: rpc::RPCConfig,
pub http: HttpServerConfig,
}
impl Default for Config {
fn default() -> Self {
Self {
data_dir: PathBuf::from(".lighthouse"),
db_type: "disk".to_string(),
db_name: "chain_db".to_string(),
// Note: there are no default bootnodes specified.
// Once bootnodes are established, add them here.
network: NetworkConfig::new(),
rpc: rpc::RPCConfig::default(),
http: HttpServerConfig::default(),
}
}
}
impl Config {
/// Returns the path to which the client may initialize an on-disk database.
pub fn db_path(&self) -> Option<PathBuf> {
self.data_dir()
.and_then(|path| Some(path.join(&self.db_name)))
}
/// Returns the core path for the client.
pub fn data_dir(&self) -> Option<PathBuf> {
let path = dirs::home_dir()?.join(&self.data_dir);
fs::create_dir_all(&path).ok()?;
Some(path)
}
/// Apply the following arguments to `self`, replacing values if they are specified in `args`.
///
/// Returns an error if arguments are obviously invalid. May succeed even if some values are
/// invalid.
pub fn apply_cli_args(&mut self, args: &ArgMatches) -> Result<(), String> {
if let Some(dir) = args.value_of("datadir") {
self.data_dir = PathBuf::from(dir);
};
if let Some(dir) = args.value_of("db") {
self.db_type = dir.to_string();
}
self.network.apply_cli_args(args)?;
self.rpc.apply_cli_args(args)?;
self.http.apply_cli_args(args)?;
Ok(())
}
}

View File

@ -8,8 +8,8 @@ edition = "2018"
beacon_chain = { path = "../beacon_chain" } beacon_chain = { path = "../beacon_chain" }
clap = "2.32.0" clap = "2.32.0"
# SigP repository # SigP repository
libp2p = { git = "https://github.com/SigP/rust-libp2p", rev = "f018f5c443ed5a93de890048dbc6755393373e72" } libp2p = { git = "https://github.com/SigP/rust-libp2p", rev = "1c2aa97a338fc9dfd8e804cc47497fb9b8e7ad04" }
enr = { git = "https://github.com/SigP/rust-libp2p/", rev = "f018f5c443ed5a93de890048dbc6755393373e72", features = ["serde"] } enr = { git = "https://github.com/SigP/rust-libp2p/", rev = "1c2aa97a338fc9dfd8e804cc47497fb9b8e7ad04", features = ["serde"] }
types = { path = "../../eth2/types" } types = { path = "../../eth2/types" }
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"

View File

@ -1,9 +1,6 @@
use clap::ArgMatches; use clap::ArgMatches;
use enr::Enr; use enr::Enr;
use libp2p::{ use libp2p::gossipsub::{GossipsubConfig, GossipsubConfigBuilder};
gossipsub::{GossipsubConfig, GossipsubConfigBuilder},
multiaddr::Multiaddr,
};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use std::time::Duration; use std::time::Duration;
@ -18,9 +15,12 @@ pub const SHARD_TOPIC_PREFIX: &str = "shard";
/// Network configuration for lighthouse. /// Network configuration for lighthouse.
pub struct Config { pub struct Config {
/// IP address to listen on. /// IP address to listen on.
pub listen_addresses: Vec<Multiaddr>, pub listen_address: std::net::IpAddr,
/// Specifies the IP address that the discovery protocol will listen on. /// The TCP port that libp2p listens on.
pub libp2p_port: u16,
/// The address to broadcast to peers about which address we are listening on.
pub discovery_address: std::net::IpAddr, pub discovery_address: std::net::IpAddr,
/// UDP port that discovery listens on. /// UDP port that discovery listens on.
@ -47,8 +47,9 @@ impl Default for Config {
/// Generate a default network configuration. /// Generate a default network configuration.
fn default() -> Self { fn default() -> Self {
Config { Config {
listen_addresses: vec!["/ip4/127.0.0.1/tcp/9000".parse().expect("vaild multiaddr")], listen_address: "127.0.0.1".parse().expect("vaild ip address"),
discovery_address: "0.0.0.0".parse().expect("valid ip address"), libp2p_port: 9000,
discovery_address: "127.0.0.1".parse().expect("valid ip address"),
discovery_port: 9000, discovery_port: 9000,
max_peers: 10, max_peers: 10,
//TODO: Set realistic values for production //TODO: Set realistic values for production
@ -72,13 +73,11 @@ impl Config {
pub fn apply_cli_args(&mut self, args: &ArgMatches) -> Result<(), String> { pub fn apply_cli_args(&mut self, args: &ArgMatches) -> Result<(), String> {
if let Some(listen_address_str) = args.value_of("listen-address") { if let Some(listen_address_str) = args.value_of("listen-address") {
self.listen_addresses = listen_address_str let listen_address = listen_address_str
.split(',') .parse()
.map(|a| { .map_err(|_| format!("Invalid listen address: {:?}", listen_address_str))?;
a.parse::<Multiaddr>() self.listen_address = listen_address;
.map_err(|_| format!("Invalid Listen address: {:?}", a)) self.discovery_address = listen_address;
})
.collect::<Result<Vec<Multiaddr>, _>>()?;
} }
if let Some(max_peers_str) = args.value_of("maxpeers") { if let Some(max_peers_str) = args.value_of("maxpeers") {
@ -87,10 +86,12 @@ impl Config {
.map_err(|_| format!("Invalid number of max peers: {}", max_peers_str))?; .map_err(|_| format!("Invalid number of max peers: {}", max_peers_str))?;
} }
if let Some(discovery_address_str) = args.value_of("disc-listen-address") { if let Some(port_str) = args.value_of("port") {
self.discovery_address = discovery_address_str let port = port_str
.parse::<std::net::IpAddr>() .parse::<u16>()
.map_err(|_| format!("Invalid discovery address: {:?}", discovery_address_str))?; .map_err(|_| format!("Invalid port: {}", port_str))?;
self.libp2p_port = port;
self.discovery_port = port;
} }
if let Some(boot_enr_str) = args.value_of("boot-nodes") { if let Some(boot_enr_str) = args.value_of("boot-nodes") {
@ -100,6 +101,12 @@ impl Config {
.collect::<Result<Vec<Enr>, _>>()?; .collect::<Result<Vec<Enr>, _>>()?;
} }
if let Some(discovery_address_str) = args.value_of("discovery-address") {
self.discovery_address = discovery_address_str
.parse()
.map_err(|_| format!("Invalid discovery address: {:?}", discovery_address_str))?
}
if let Some(disc_port_str) = args.value_of("disc-port") { if let Some(disc_port_str) = args.value_of("disc-port") {
self.discovery_port = disc_port_str self.discovery_port = disc_port_str
.parse::<u16>() .parse::<u16>()

View File

@ -11,7 +11,7 @@ use libp2p::core::{identity::Keypair, Multiaddr, PeerId, ProtocolsHandler};
use libp2p::discv5::{Discv5, Discv5Event}; use libp2p::discv5::{Discv5, Discv5Event};
use libp2p::enr::{Enr, EnrBuilder, NodeId}; use libp2p::enr::{Enr, EnrBuilder, NodeId};
use libp2p::multiaddr::Protocol; use libp2p::multiaddr::Protocol;
use slog::{debug, error, info, o, warn}; use slog::{debug, info, o, warn};
use std::collections::HashSet; use std::collections::HashSet;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use tokio::io::{AsyncRead, AsyncWrite}; use tokio::io::{AsyncRead, AsyncWrite};
@ -19,6 +19,8 @@ use tokio_timer::Delay;
/// Maximum seconds before searching for extra peers. /// Maximum seconds before searching for extra peers.
const MAX_TIME_BETWEEN_PEER_SEARCHES: u64 = 60; const MAX_TIME_BETWEEN_PEER_SEARCHES: u64 = 60;
/// Initial delay between peer searches.
const INITIAL_SEARCH_DELAY: u64 = 5;
/// Lighthouse discovery behaviour. This provides peer management and discovery using the Discv5 /// Lighthouse discovery behaviour. This provides peer management and discovery using the Discv5
/// libp2p protocol. /// libp2p protocol.
@ -57,50 +59,27 @@ impl<TSubstream> Discovery<TSubstream> {
let log = log.new(o!("Service" => "Libp2p-Discovery")); let log = log.new(o!("Service" => "Libp2p-Discovery"));
// Build the local ENR. // Build the local ENR.
// The first TCP listening address is used for the ENR record. This will inform our peers to
// connect to this TCP port and establish libp2p streams.
// Note: Discovery should update the ENR record's IP to the external IP as seen by the // Note: Discovery should update the ENR record's IP to the external IP as seen by the
// majority of our peers. // majority of our peers.
let tcp_multiaddr = net_conf
.listen_addresses
.iter()
.filter(|a| {
if let Some(Protocol::Tcp(_)) = a.iter().last() {
true
} else {
false
}
})
.next()
.ok_or_else(|| "No valid TCP addresses")?;
let ip: std::net::IpAddr = match tcp_multiaddr.iter().next() {
Some(Protocol::Ip4(ip)) => ip.into(),
Some(Protocol::Ip6(ip)) => ip.into(),
_ => {
error!(log, "Multiaddr has an invalid IP address");
return Err(format!("Invalid IP Address: {}", tcp_multiaddr).into());
}
};
let tcp_port = match tcp_multiaddr.iter().last() {
Some(Protocol::Tcp(tcp)) => tcp,
_ => unreachable!(),
};
let local_enr = EnrBuilder::new() let local_enr = EnrBuilder::new()
.ip(ip.into()) .ip(net_conf.discovery_address.into())
.tcp(tcp_port) .tcp(net_conf.libp2p_port)
.udp(net_conf.discovery_port) .udp(net_conf.discovery_port)
.build(&local_key) .build(&local_key)
.map_err(|e| format!("Could not build Local ENR: {:?}", e))?; .map_err(|e| format!("Could not build Local ENR: {:?}", e))?;
info!(log, "Local ENR: {}", local_enr.to_base64()); info!(log, "Local ENR: {}", local_enr.to_base64());
let mut discovery = Discv5::new(local_enr, local_key.clone(), net_conf.discovery_address) let mut discovery = Discv5::new(local_enr, local_key.clone(), net_conf.listen_address)
.map_err(|e| format!("Discv5 service failed: {:?}", e))?; .map_err(|e| format!("Discv5 service failed: {:?}", e))?;
// Add bootnodes to routing table // Add bootnodes to routing table
for bootnode_enr in net_conf.boot_nodes.clone() { for bootnode_enr in net_conf.boot_nodes.clone() {
debug!(
log,
"Adding node to routing table: {}",
bootnode_enr.node_id()
);
discovery.add_enr(bootnode_enr); discovery.add_enr(bootnode_enr);
} }
@ -108,8 +87,8 @@ impl<TSubstream> Discovery<TSubstream> {
connected_peers: HashSet::new(), connected_peers: HashSet::new(),
max_peers: net_conf.max_peers, max_peers: net_conf.max_peers,
peer_discovery_delay: Delay::new(Instant::now()), peer_discovery_delay: Delay::new(Instant::now()),
past_discovery_delay: 1, past_discovery_delay: INITIAL_SEARCH_DELAY,
tcp_port, tcp_port: net_conf.libp2p_port,
discovery, discovery,
log, log,
}) })
@ -118,7 +97,7 @@ impl<TSubstream> Discovery<TSubstream> {
/// Manually search for peers. This restarts the discovery round, sparking multiple rapid /// Manually search for peers. This restarts the discovery round, sparking multiple rapid
/// queries. /// queries.
pub fn discover_peers(&mut self) { pub fn discover_peers(&mut self) {
self.past_discovery_delay = 1; self.past_discovery_delay = INITIAL_SEARCH_DELAY;
self.find_peers(); self.find_peers();
} }
@ -203,7 +182,9 @@ where
loop { loop {
match self.peer_discovery_delay.poll() { match self.peer_discovery_delay.poll() {
Ok(Async::Ready(_)) => { Ok(Async::Ready(_)) => {
self.find_peers(); if self.connected_peers.len() < self.max_peers {
self.find_peers();
}
} }
Ok(Async::NotReady) => break, Ok(Async::NotReady) => break,
Err(e) => { Err(e) => {
@ -217,16 +198,9 @@ where
match self.discovery.poll(params) { match self.discovery.poll(params) {
Async::Ready(NetworkBehaviourAction::GenerateEvent(event)) => { Async::Ready(NetworkBehaviourAction::GenerateEvent(event)) => {
match event { match event {
Discv5Event::Discovered(enr) => { Discv5Event::Discovered(_enr) => {
debug!(self.log, "Discv5: Peer discovered"; "Peer"=> format!("{:?}", enr.peer_id()), "Addresses" => format!("{:?}", enr.multiaddr())); // not concerned about FINDNODE results, rather the result of an entire
// query.
let peer_id = enr.peer_id();
// if we need more peers, attempt a connection
if self.connected_peers.len() < self.max_peers
&& self.connected_peers.get(&peer_id).is_none()
{
return Async::Ready(NetworkBehaviourAction::DialPeer { peer_id });
}
} }
Discv5Event::SocketUpdated(socket) => { Discv5Event::SocketUpdated(socket) => {
info!(self.log, "Address updated"; "IP" => format!("{}",socket.ip())); info!(self.log, "Address updated"; "IP" => format!("{}",socket.ip()));
@ -241,6 +215,17 @@ where
if closer_peers.is_empty() { if closer_peers.is_empty() {
debug!(self.log, "Discv5 random query yielded empty results"); debug!(self.log, "Discv5 random query yielded empty results");
} }
for peer_id in closer_peers {
// if we need more peers, attempt a connection
if self.connected_peers.len() < self.max_peers
&& self.connected_peers.get(&peer_id).is_none()
{
debug!(self.log, "Discv5: Peer discovered"; "Peer"=> format!("{:?}", peer_id));
return Async::Ready(NetworkBehaviourAction::DialPeer {
peer_id,
});
}
}
} }
_ => {} _ => {}
} }

View File

@ -9,6 +9,7 @@ use futures::prelude::*;
use futures::Stream; use futures::Stream;
use libp2p::core::{ use libp2p::core::{
identity, identity,
multiaddr::Multiaddr,
muxing::StreamMuxerBox, muxing::StreamMuxerBox,
nodes::Substream, nodes::Substream,
transport::boxed::Boxed, transport::boxed::Boxed,
@ -52,17 +53,24 @@ impl Service {
Swarm::new(transport, behaviour, local_peer_id.clone()) Swarm::new(transport, behaviour, local_peer_id.clone())
}; };
// listen on all addresses // listen on the specified address
for address in config.listen_addresses { let listen_multiaddr = {
match Swarm::listen_on(&mut swarm, address.clone()) { let mut m = Multiaddr::from(config.listen_address);
Ok(_) => { m.push(Protocol::Tcp(config.libp2p_port));
let mut log_address = address.clone(); m
log_address.push(Protocol::P2p(local_peer_id.clone().into())); };
info!(log, "Listening on: {}", log_address);
} match Swarm::listen_on(&mut swarm, listen_multiaddr.clone()) {
Err(err) => warn!(log, "Cannot listen on: {} because: {:?}", address, err), Ok(_) => {
}; let mut log_address = listen_multiaddr;
} log_address.push(Protocol::P2p(local_peer_id.clone().into()));
info!(log, "Listening on: {}", log_address);
}
Err(err) => warn!(
log,
"Cannot listen on: {} because: {:?}", listen_multiaddr, err
),
};
// subscribe to default gossipsub topics // subscribe to default gossipsub topics
let mut topics = vec![]; let mut topics = vec![];

View File

@ -33,15 +33,14 @@ fn main() {
.arg( .arg(
Arg::with_name("listen-address") Arg::with_name("listen-address")
.long("listen-address") .long("listen-address")
.value_name("Listen Address") .value_name("Address")
.help("One or more comma-delimited multi-addresses to listen for p2p connections.") .help("The address lighthouse will listen for UDP and TCP connections. (default 127.0.0.1).")
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
Arg::with_name("maxpeers") Arg::with_name("maxpeers")
.long("maxpeers") .long("maxpeers")
.value_name("Max Peers") .help("The maximum number of peers (default 10).")
.help("The maximum number of peers (default 10)")
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
@ -53,17 +52,24 @@ fn main() {
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
Arg::with_name("disc-listen-address") Arg::with_name("port")
.long("disc-listen_address") .long("port")
.value_name("DISCPORT") .value_name("Lighthouse Port")
.help("The IP address that the discovery protocol will listen on. Defaults to 0.0.0.0") .help("The TCP/UDP port to listen on. The UDP port can be modified by the --discovery-port flag.")
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
Arg::with_name("discovery-port") Arg::with_name("discovery-port")
.long("disc-port") .long("disc-port")
.value_name("DISCPORT") .value_name("DiscoveryPort")
.help("Listen UDP port for the discovery process") .help("The discovery UDP port.")
.takes_value(true),
)
.arg(
Arg::with_name("discovery-address")
.long("discovery-address")
.value_name("Address")
.help("The address to broadcast to other peers on how to reach this node.")
.takes_value(true), .takes_value(true),
) )
// rpc related arguments // rpc related arguments
@ -77,14 +83,13 @@ fn main() {
.arg( .arg(
Arg::with_name("rpc-address") Arg::with_name("rpc-address")
.long("rpc-address") .long("rpc-address")
.value_name("RPCADDRESS") .value_name("Address")
.help("Listen address for RPC endpoint.") .help("Listen address for RPC endpoint.")
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
Arg::with_name("rpc-port") Arg::with_name("rpc-port")
.long("rpc-port") .long("rpc-port")
.value_name("RPCPORT")
.help("Listen port for RPC endpoint.") .help("Listen port for RPC endpoint.")
.takes_value(true), .takes_value(true),
) )
@ -92,21 +97,19 @@ fn main() {
.arg( .arg(
Arg::with_name("http") Arg::with_name("http")
.long("http") .long("http")
.value_name("HTTP")
.help("Enable the HTTP server.") .help("Enable the HTTP server.")
.takes_value(false), .takes_value(false),
) )
.arg( .arg(
Arg::with_name("http-address") Arg::with_name("http-address")
.long("http-address") .long("http-address")
.value_name("HTTPADDRESS") .value_name("Address")
.help("Listen address for the HTTP server.") .help("Listen address for the HTTP server.")
.takes_value(true), .takes_value(true),
) )
.arg( .arg(
Arg::with_name("http-port") Arg::with_name("http-port")
.long("http-port") .long("http-port")
.value_name("HTTPPORT")
.help("Listen port for the HTTP server.") .help("Listen port for the HTTP server.")
.takes_value(true), .takes_value(true),
) )

View File

@ -41,6 +41,15 @@ pub fn run_beacon_node(
"This software is EXPERIMENTAL and provides no guarantees or warranties." "This software is EXPERIMENTAL and provides no guarantees or warranties."
); );
info!(
log,
"Starting beacon node";
"p2p_listen_address" => format!("{:?}", &other_client_config.network.listen_address),
"data_dir" => format!("{:?}", other_client_config.data_dir()),
"spec_constants" => &spec_constants,
"db_type" => &other_client_config.db_type,
);
let result = match (db_type.as_str(), spec_constants.as_str()) { let result = match (db_type.as_str(), spec_constants.as_str()) {
("disk", "minimal") => run::<ClientType<DiskStore, MinimalEthSpec>>( ("disk", "minimal") => run::<ClientType<DiskStore, MinimalEthSpec>>(
&db_path, &db_path,
@ -80,17 +89,6 @@ pub fn run_beacon_node(
} }
}; };
if result.is_ok() {
info!(
log,
"Started beacon node";
"p2p_listen_addresses" => format!("{:?}", &other_client_config.network.listen_addresses),
"data_dir" => format!("{:?}", other_client_config.data_dir()),
"spec_constants" => &spec_constants,
"db_type" => &other_client_config.db_type,
);
}
result result
} }