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};
|
2019-04-03 06:16:32 +00:00
|
|
|
use std::time::Duration;
|
2019-06-07 23:44:27 +00:00
|
|
|
use types::multiaddr::{Error as MultiaddrError, Multiaddr};
|
2019-03-21 01:45:23 +00:00
|
|
|
|
2019-04-03 05:33:12 +00:00
|
|
|
/// The beacon node topic string to subscribe to.
|
|
|
|
pub const BEACON_PUBSUB_TOPIC: &str = "beacon_node";
|
|
|
|
pub const SHARD_TOPIC_PREFIX: &str = "attestations"; // single topic for all attestation for the moment.
|
|
|
|
|
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,
|
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 {
|
|
|
|
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 06:16:32 +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(),
|
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
|
|
|
|
|
|
|
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") {
|
2019-06-10 15:01:25 +00:00
|
|
|
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") {
|
2019-06-10 15:01:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-03 05:00:09 +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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|