2019-06-07 23:44:27 +00:00
|
|
|
use clap::ArgMatches;
|
2019-06-25 04:51:45 +00:00
|
|
|
use enr::Enr;
|
2019-06-25 08:02:11 +00:00
|
|
|
use libp2p::gossipsub::{GossipsubConfig, GossipsubConfigBuilder};
|
2019-08-10 01:44:17 +00:00
|
|
|
use libp2p::Multiaddr;
|
2019-06-07 23:44:27 +00:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-07-01 06:38:42 +00:00
|
|
|
use std::path::PathBuf;
|
2019-04-03 06:16:32 +00:00
|
|
|
use std::time::Duration;
|
2019-03-21 01:45:23 +00:00
|
|
|
|
2019-08-29 11:23:28 +00:00
|
|
|
/// The gossipsub topic names.
|
|
|
|
// These constants form a topic name of the form /TOPIC_PREFIX/TOPIC/ENCODING_POSTFIX
|
|
|
|
// For example /eth2/beacon_block/ssz
|
|
|
|
pub const TOPIC_PREFIX: &str = "eth2";
|
|
|
|
pub const TOPIC_ENCODING_POSTFIX: &str = "ssz";
|
2019-08-10 01:44:17 +00:00
|
|
|
pub const BEACON_BLOCK_TOPIC: &str = "beacon_block";
|
2019-06-25 04:51:45 +00:00
|
|
|
pub const BEACON_ATTESTATION_TOPIC: &str = "beacon_attestation";
|
2019-08-29 11:23:28 +00:00
|
|
|
pub const VOLUNTARY_EXIT_TOPIC: &str = "voluntary_exit";
|
|
|
|
pub const PROPOSER_SLASHING_TOPIC: &str = "proposer_slashing";
|
|
|
|
pub const ATTESTER_SLASHING_TOPIC: &str = "attester_slashing";
|
2019-06-25 04:51:45 +00:00
|
|
|
pub const SHARD_TOPIC_PREFIX: &str = "shard";
|
2019-04-03 05:33:12 +00:00
|
|
|
|
2019-06-07 23:44:27 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(default)]
|
2019-03-21 01:45:23 +00:00
|
|
|
/// Network configuration for lighthouse.
|
|
|
|
pub struct Config {
|
2019-07-01 06:38:42 +00:00
|
|
|
/// Data directory where node's keyfile is stored
|
|
|
|
pub network_dir: PathBuf,
|
|
|
|
|
2019-03-21 01:45:23 +00:00
|
|
|
/// IP address to listen on.
|
2019-06-25 08:02:11 +00:00
|
|
|
pub listen_address: std::net::IpAddr,
|
2019-06-25 04:51:45 +00:00
|
|
|
|
2019-06-25 08:02:11 +00:00
|
|
|
/// The TCP port that libp2p listens on.
|
|
|
|
pub libp2p_port: u16,
|
|
|
|
|
|
|
|
/// The address to broadcast to peers about which address we are listening on.
|
2019-06-25 04:51:45 +00:00
|
|
|
pub discovery_address: std::net::IpAddr,
|
|
|
|
|
|
|
|
/// UDP port that discovery listens on.
|
|
|
|
pub discovery_port: u16,
|
|
|
|
|
|
|
|
/// Target number of connected peers.
|
|
|
|
pub max_peers: usize,
|
|
|
|
|
2019-09-10 16:13:54 +00:00
|
|
|
/// A secp256k1 secret key, as bytes in ASCII-encoded hex.
|
|
|
|
///
|
|
|
|
/// With or without `0x` prefix.
|
|
|
|
#[serde(skip)]
|
|
|
|
pub secret_key_hex: Option<String>,
|
|
|
|
|
2019-03-21 01:45:23 +00:00
|
|
|
/// Gossipsub configuration parameters.
|
2019-06-07 23:44:27 +00:00
|
|
|
#[serde(skip)]
|
2019-03-21 01:45:23 +00:00
|
|
|
pub gs_config: GossipsubConfig,
|
2019-06-25 04:51:45 +00:00
|
|
|
|
2019-03-21 01:45:23 +00:00
|
|
|
/// List of nodes to initially connect to.
|
2019-06-25 04:51:45 +00:00
|
|
|
pub boot_nodes: Vec<Enr>,
|
|
|
|
|
2019-08-10 01:44:17 +00:00
|
|
|
/// List of libp2p nodes to initially connect to.
|
|
|
|
pub libp2p_nodes: Vec<Multiaddr>,
|
|
|
|
|
2019-03-21 01:45:23 +00:00
|
|
|
/// Client version
|
|
|
|
pub client_version: String,
|
2019-06-25 04:51:45 +00:00
|
|
|
|
2019-04-03 05:00:09 +00:00
|
|
|
/// List of extra topics to initially subscribe to as strings.
|
2019-03-21 01:45:23 +00:00
|
|
|
pub topics: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
/// Generate a default network configuration.
|
|
|
|
fn default() -> Self {
|
2019-07-01 06:38:42 +00:00
|
|
|
let mut network_dir = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
|
|
|
|
network_dir.push(".lighthouse");
|
|
|
|
network_dir.push("network");
|
2019-03-21 01:45:23 +00:00
|
|
|
Config {
|
2019-07-01 06:38:42 +00:00
|
|
|
network_dir,
|
2019-07-26 19:26:06 +00:00
|
|
|
listen_address: "127.0.0.1".parse().expect("valid ip address"),
|
2019-06-25 08:02:11 +00:00
|
|
|
libp2p_port: 9000,
|
|
|
|
discovery_address: "127.0.0.1".parse().expect("valid ip address"),
|
2019-06-25 04:51:45 +00:00
|
|
|
discovery_port: 9000,
|
|
|
|
max_peers: 10,
|
2019-09-10 16:13:54 +00:00
|
|
|
secret_key_hex: None,
|
2019-08-29 11:23:28 +00:00
|
|
|
// Note: The topics by default are sent as plain strings. Hashes are an optional
|
|
|
|
// parameter.
|
2019-03-26 04:01:05 +00:00
|
|
|
gs_config: GossipsubConfigBuilder::new()
|
2019-08-29 11:23:28 +00:00
|
|
|
.max_transmit_size(1_048_576)
|
2019-09-04 16:06:39 +00:00
|
|
|
.heartbeat_interval(Duration::from_secs(20)) // TODO: Reduce for mainnet
|
2019-10-30 01:22:18 +00:00
|
|
|
.manual_propagation(true) // require validation before propagation
|
2019-03-26 04:01:05 +00:00
|
|
|
.build(),
|
2019-06-07 23:44:27 +00:00
|
|
|
boot_nodes: vec![],
|
2019-08-10 01:44:17 +00:00
|
|
|
libp2p_nodes: vec![],
|
2019-03-21 01:45:23 +00:00
|
|
|
client_version: version::version(),
|
2019-04-03 01:25:05 +00:00
|
|
|
topics: Vec::new(),
|
2019-03-21 01:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 05:00:09 +00:00
|
|
|
/// Generates a default Config.
|
2019-03-21 01:45:23 +00:00
|
|
|
impl Config {
|
2019-04-03 05:00:09 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Config::default()
|
2019-03-21 01:45:23 +00:00
|
|
|
}
|
2019-06-07 23:44:27 +00:00
|
|
|
|
2019-06-25 04:51:45 +00:00
|
|
|
pub fn apply_cli_args(&mut self, args: &ArgMatches) -> Result<(), String> {
|
2019-07-29 03:45:45 +00:00
|
|
|
// If a `datadir` has been specified, set the network dir to be inside it.
|
2019-07-01 06:38:42 +00:00
|
|
|
if let Some(dir) = args.value_of("datadir") {
|
|
|
|
self.network_dir = PathBuf::from(dir).join("network");
|
|
|
|
};
|
|
|
|
|
2019-07-29 03:45:45 +00:00
|
|
|
// If a network dir has been specified, override the `datadir` definition.
|
|
|
|
if let Some(dir) = args.value_of("network-dir") {
|
|
|
|
self.network_dir = PathBuf::from(dir);
|
|
|
|
};
|
|
|
|
|
2019-06-07 23:44:27 +00:00
|
|
|
if let Some(listen_address_str) = args.value_of("listen-address") {
|
2019-06-25 08:02:11 +00:00
|
|
|
let listen_address = listen_address_str
|
|
|
|
.parse()
|
|
|
|
.map_err(|_| format!("Invalid listen address: {:?}", listen_address_str))?;
|
|
|
|
self.listen_address = listen_address;
|
|
|
|
self.discovery_address = listen_address;
|
2019-06-07 23:44:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 04:51:45 +00:00
|
|
|
if let Some(max_peers_str) = args.value_of("maxpeers") {
|
|
|
|
self.max_peers = max_peers_str
|
|
|
|
.parse::<usize>()
|
|
|
|
.map_err(|_| format!("Invalid number of max peers: {}", max_peers_str))?;
|
2019-06-07 23:44:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 08:02:11 +00:00
|
|
|
if let Some(port_str) = args.value_of("port") {
|
|
|
|
let port = port_str
|
|
|
|
.parse::<u16>()
|
|
|
|
.map_err(|_| format!("Invalid port: {}", port_str))?;
|
|
|
|
self.libp2p_port = port;
|
|
|
|
self.discovery_port = port;
|
2019-03-21 01:45:23 +00:00
|
|
|
}
|
2019-04-03 05:00:09 +00:00
|
|
|
|
2019-06-25 04:51:45 +00:00
|
|
|
if let Some(boot_enr_str) = args.value_of("boot-nodes") {
|
|
|
|
self.boot_nodes = boot_enr_str
|
|
|
|
.split(',')
|
|
|
|
.map(|enr| enr.parse().map_err(|_| format!("Invalid ENR: {}", enr)))
|
|
|
|
.collect::<Result<Vec<Enr>, _>>()?;
|
2019-04-03 05:00:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 01:44:17 +00:00
|
|
|
if let Some(libp2p_addresses_str) = args.value_of("libp2p-addresses") {
|
|
|
|
self.libp2p_nodes = libp2p_addresses_str
|
|
|
|
.split(',')
|
|
|
|
.map(|multiaddr| {
|
|
|
|
multiaddr
|
|
|
|
.parse()
|
|
|
|
.map_err(|_| format!("Invalid Multiaddr: {}", multiaddr))
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<Multiaddr>, _>>()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(topics_str) = args.value_of("topics") {
|
|
|
|
self.topics = topics_str.split(',').map(|s| s.into()).collect();
|
|
|
|
}
|
|
|
|
|
2019-06-25 08:02:11 +00:00
|
|
|
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))?
|
|
|
|
}
|
|
|
|
|
2019-06-25 04:51:45 +00:00
|
|
|
if let Some(disc_port_str) = args.value_of("disc-port") {
|
|
|
|
self.discovery_port = disc_port_str
|
|
|
|
.parse::<u16>()
|
|
|
|
.map_err(|_| format!("Invalid discovery port: {}", disc_port_str))?;
|
2019-04-03 05:00:09 +00:00
|
|
|
}
|
2019-06-25 04:51:45 +00:00
|
|
|
|
2019-09-10 16:13:54 +00:00
|
|
|
if let Some(p2p_priv_key) = args.value_of("p2p-priv-key") {
|
|
|
|
self.secret_key_hex = Some(p2p_priv_key.to_string());
|
|
|
|
}
|
|
|
|
|
2019-06-25 04:51:45 +00:00
|
|
|
Ok(())
|
2019-04-03 05:00:09 +00:00
|
|
|
}
|
|
|
|
}
|