lighthouse/beacon_node/eth2-libp2p/src/config.rs

119 lines
3.9 KiB
Rust
Raw Normal View History

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-06-07 23:44:27 +00:00
use serde_derive::{Deserialize, Serialize};
use std::time::Duration;
2019-03-21 01:45:23 +00:00
/// The beacon node topic string to subscribe to.
2019-06-25 04:51:45 +00:00
pub const BEACON_PUBSUB_TOPIC: &str = "beacon_block";
pub const BEACON_ATTESTATION_TOPIC: &str = "beacon_attestation";
//TODO: Implement shard subnets
pub const SHARD_TOPIC_PREFIX: &str = "shard";
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 {
/// 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-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-03-21 01:45:23 +00:00
/// Client version
pub client_version: String,
2019-06-25 04:51:45 +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 {
Config {
2019-06-25 08:02:11 +00:00
listen_address: "127.0.0.1".parse().expect("vaild ip address"),
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,
//TODO: Set realistic values for production
2019-03-26 04:01:05 +00:00
gs_config: GossipsubConfigBuilder::new()
.max_gossip_size(4_000_000)
.inactivity_timeout(Duration::from_secs(90))
.heartbeat_interval(Duration::from_secs(20))
2019-03-26 04:01:05 +00:00
.build(),
2019-06-07 23:44:27 +00:00
boot_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
}
}
}
/// Generates a default Config.
2019-03-21 01:45:23 +00:00
impl Config {
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-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-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-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-06-25 04:51:45 +00:00
Ok(())
}
}