mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-16 12:28:24 +00:00
Integrate discv5 into lighthouse
This commit is contained in:
@@ -1,29 +1,44 @@
|
||||
use clap::ArgMatches;
|
||||
use libp2p::gossipsub::{GossipsubConfig, GossipsubConfigBuilder};
|
||||
use libp2p::multiaddr::{Error as MultiaddrError, Multiaddr};
|
||||
use enr::Enr;
|
||||
use libp2p::{
|
||||
gossipsub::{GossipsubConfig, GossipsubConfigBuilder},
|
||||
multiaddr::Multiaddr,
|
||||
};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::time::Duration;
|
||||
|
||||
/// 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.
|
||||
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";
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
/// Network configuration for lighthouse.
|
||||
pub struct Config {
|
||||
/// IP address to listen on.
|
||||
listen_addresses: Vec<String>,
|
||||
pub listen_addresses: Vec<Multiaddr>,
|
||||
|
||||
/// Specifies the IP address that the discovery protocol will listen on.
|
||||
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,
|
||||
|
||||
/// Gossipsub configuration parameters.
|
||||
#[serde(skip)]
|
||||
pub gs_config: GossipsubConfig,
|
||||
/// Configuration parameters for node identification protocol.
|
||||
#[serde(skip)]
|
||||
pub identify_config: IdentifyConfig,
|
||||
|
||||
/// List of nodes to initially connect to.
|
||||
boot_nodes: Vec<String>,
|
||||
pub boot_nodes: Vec<Enr>,
|
||||
|
||||
/// Client version
|
||||
pub client_version: String,
|
||||
|
||||
/// List of extra topics to initially subscribe to as strings.
|
||||
pub topics: Vec<String>,
|
||||
}
|
||||
@@ -32,13 +47,16 @@ impl Default for Config {
|
||||
/// Generate a default network configuration.
|
||||
fn default() -> Self {
|
||||
Config {
|
||||
listen_addresses: vec!["/ip4/127.0.0.1/tcp/9000".to_string()],
|
||||
listen_addresses: vec!["/ip4/127.0.0.1/tcp/9000".parse().expect("vaild multiaddr")],
|
||||
discovery_address: "0.0.0.0".parse().expect("valid ip address"),
|
||||
discovery_port: 9000,
|
||||
max_peers: 10,
|
||||
//TODO: Set realistic values for production
|
||||
gs_config: GossipsubConfigBuilder::new()
|
||||
.max_gossip_size(4_000_000)
|
||||
.inactivity_timeout(Duration::from_secs(90))
|
||||
.heartbeat_interval(Duration::from_secs(20))
|
||||
.build(),
|
||||
identify_config: IdentifyConfig::default(),
|
||||
boot_nodes: vec![],
|
||||
client_version: version::version(),
|
||||
topics: Vec::new(),
|
||||
@@ -52,83 +70,42 @@ impl Config {
|
||||
Config::default()
|
||||
}
|
||||
|
||||
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> {
|
||||
pub fn apply_cli_args(&mut self, args: &ArgMatches) -> Result<(), String> {
|
||||
if let Some(listen_address_str) = args.value_of("listen-address") {
|
||||
let listen_addresses = listen_address_str.split(',').map(Into::into).collect();
|
||||
self.listen_addresses = listen_addresses;
|
||||
self.listen_addresses = listen_address_str
|
||||
.split(',')
|
||||
.map(|a| {
|
||||
a.parse::<Multiaddr>()
|
||||
.map_err(|_| format!("Invalid Listen address: {:?}", a))
|
||||
})
|
||||
.collect::<Result<Vec<Multiaddr>, _>>()?;
|
||||
}
|
||||
|
||||
if let Some(boot_addresses_str) = args.value_of("boot-nodes") {
|
||||
let boot_addresses = boot_addresses_str.split(',').map(Into::into).collect();
|
||||
self.boot_nodes = boot_addresses;
|
||||
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))?;
|
||||
}
|
||||
|
||||
if let Some(discovery_address_str) = args.value_of("disc-listen-address") {
|
||||
self.discovery_address = discovery_address_str
|
||||
.parse::<std::net::IpAddr>()
|
||||
.map_err(|_| format!("Invalid discovery address: {:?}", discovery_address_str))?;
|
||||
}
|
||||
|
||||
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>, _>>()?;
|
||||
}
|
||||
|
||||
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))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// 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(),
|
||||
user_agent: version::version(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user