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

136 lines
4.1 KiB
Rust
Raw Normal View History

2019-06-07 23:44:27 +00:00
use clap::ArgMatches;
2019-03-21 01:45:23 +00:00
use libp2p::gossipsub::{GossipsubConfig, GossipsubConfigBuilder};
2019-06-07 23:44:27 +00:00
use serde_derive::{Deserialize, Serialize};
use types::multiaddr::{Error as MultiaddrError, Multiaddr};
2019-04-03 01:25:05 +00:00
//use std::time::Duration;
2019-03-21 01:45:23 +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 {
/// IP address to listen on.
2019-06-07 23:44:27 +00:00
listen_addresses: Vec<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,
/// Configuration parameters for node identification protocol.
2019-06-07 23:44:27 +00:00
#[serde(skip)]
2019-03-21 01:45:23 +00:00
pub identify_config: IdentifyConfig,
/// List of nodes to initially connect to.
2019-06-07 23:44:27 +00:00
boot_nodes: Vec<String>,
2019-03-21 01:45:23 +00:00
/// Client version
pub client_version: String,
/// List of extra topics to initially subscribe to as strings.
2019-03-21 01:45:23 +00:00
pub topics: Vec<String>,
/// Shard pubsub topic prefix.
pub shard_prefix: String,
/// The main beacon chain topic to subscribe to.
pub beacon_chain_topic: String,
2019-03-21 01:45:23 +00:00
}
impl Default for Config {
/// Generate a default network configuration.
fn default() -> Self {
Config {
2019-06-07 23:44:27 +00:00
listen_addresses: vec!["/ip4/127.0.0.1/tcp/9000".to_string()],
2019-03-26 04:01:05 +00:00
gs_config: GossipsubConfigBuilder::new()
.max_gossip_size(4_000_000)
2019-04-03 01:25:05 +00:00
// .inactivity_timeout(Duration::from_secs(90))
2019-03-26 04:01:05 +00:00
.build(),
2019-03-21 02:28:34 +00:00
identify_config: IdentifyConfig::default(),
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(),
beacon_chain_topic: String::from("beacon_chain"),
shard_prefix: String::from("attestations"), // single topic for all attestation for the moment.
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
pub fn listen_addresses(&self) -> Result<Vec<Multiaddr>, MultiaddrError> {
self.listen_addresses.iter().map(|s| s.parse()).collect()
}
pub fn boot_nodes(&self) -> Result<Vec<Multiaddr>, MultiaddrError> {
self.boot_nodes.iter().map(|s| s.parse()).collect()
}
pub fn apply_cli_args(&mut self, args: &ArgMatches) -> Result<(), &'static str> {
if let Some(listen_address_str) = args.value_of("listen-address") {
let listen_addresses = listen_address_str.split(',').map(Into::into).collect();
2019-06-07 23:44:27 +00:00
self.listen_addresses = listen_addresses;
}
if let Some(boot_addresses_str) = args.value_of("boot-nodes") {
let boot_addresses = boot_addresses_str.split(',').map(Into::into).collect();
2019-06-07 23:44:27 +00:00
self.boot_nodes = boot_addresses;
}
Ok(())
}
2019-03-21 01:45:23 +00:00
}
/// The configuration parameters for the Identify protocol
#[derive(Debug, Clone)]
pub struct IdentifyConfig {
/// The protocol version to listen on.
pub version: String,
/// The client's name and version for identification.
pub user_agent: String,
}
impl Default for IdentifyConfig {
fn default() -> Self {
Self {
version: "/eth/serenity/1.0".to_string(),
2019-03-21 02:28:34 +00:00
user_agent: version::version(),
2019-03-21 01:45:23 +00:00
}
}
}
/// Creates a standard network config from a chain_id.
///
/// This creates specified network parameters for each chain type.
impl From<ChainType> for Config {
fn from(chain_type: ChainType) -> Self {
match chain_type {
ChainType::Foundation => Config::default(),
ChainType::LighthouseTestnet => {
let boot_nodes = vec!["/ip4/127.0.0.1/tcp/9000"
.parse()
.expect("correct multiaddr")];
Self {
boot_nodes,
..Config::default()
}
}
ChainType::Other => Config::default(),
}
}
}
pub enum ChainType {
Foundation,
LighthouseTestnet,
Other,
}
/// Maps a chain id to a ChainType.
impl From<u8> for ChainType {
fn from(chain_id: u8) -> Self {
match chain_id {
1 => ChainType::Foundation,
2 => ChainType::LighthouseTestnet,
_ => ChainType::Other,
}
}
}