2020-04-01 06:20:32 +00:00
|
|
|
use crate::types::GossipKind;
|
2020-03-19 04:11:08 +00:00
|
|
|
use crate::Enr;
|
2020-05-17 11:16:48 +00:00
|
|
|
use discv5::{Discv5Config, Discv5ConfigBuilder};
|
2020-07-29 03:40:22 +00:00
|
|
|
use libp2p::gossipsub::{
|
|
|
|
GossipsubConfig, GossipsubConfigBuilder, GossipsubMessage, MessageId, ValidationMode,
|
|
|
|
};
|
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-12-20 05:26:30 +00:00
|
|
|
use sha2::{Digest, Sha256};
|
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
|
|
|
|
2020-04-05 08:29:14 +00:00
|
|
|
pub const GOSSIP_MAX_SIZE: usize = 1_048_576;
|
|
|
|
|
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,
|
|
|
|
|
2020-03-19 04:11:08 +00:00
|
|
|
/// UDP port that discovery listens on.
|
|
|
|
pub discovery_port: u16,
|
|
|
|
|
2020-01-23 06:31:08 +00:00
|
|
|
/// The address to broadcast to peers about which address we are listening on. None indicates
|
|
|
|
/// that no discovery address has been set in the CLI args.
|
2020-03-19 04:11:08 +00:00
|
|
|
pub enr_address: Option<std::net::IpAddr>,
|
2019-06-25 04:51:45 +00:00
|
|
|
|
2020-03-19 04:11:08 +00:00
|
|
|
/// The udp port to broadcast to peers in order to reach back for discovery.
|
|
|
|
pub enr_udp_port: Option<u16>,
|
|
|
|
|
|
|
|
/// The tcp port to broadcast to peers in order to reach back for libp2p services.
|
|
|
|
pub enr_tcp_port: Option<u16>,
|
2019-06-25 04:51:45 +00:00
|
|
|
|
|
|
|
/// Target number of connected peers.
|
2020-07-23 03:55:36 +00:00
|
|
|
pub target_peers: usize,
|
2019-06-25 04:51:45 +00:00
|
|
|
|
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
|
|
|
|
2020-03-19 04:11:08 +00:00
|
|
|
/// Discv5 configuration parameters.
|
|
|
|
#[serde(skip)]
|
|
|
|
pub discv5_config: Discv5Config,
|
|
|
|
|
2019-03-21 01:45:23 +00:00
|
|
|
/// List of nodes to initially connect to.
|
2020-08-17 02:13:26 +00:00
|
|
|
pub boot_nodes_enr: Vec<Enr>,
|
|
|
|
|
|
|
|
/// List of nodes to initially connect to, on Multiaddr format.
|
|
|
|
pub boot_nodes_multiaddr: Vec<Multiaddr>,
|
2019-06-25 04:51:45 +00:00
|
|
|
|
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
|
|
|
|
2020-06-23 03:45:40 +00:00
|
|
|
/// Disables the discovery protocol from starting.
|
|
|
|
pub disable_discovery: bool,
|
|
|
|
|
2019-04-03 05:00:09 +00:00
|
|
|
/// List of extra topics to initially subscribe to as strings.
|
2020-04-01 06:20:32 +00:00
|
|
|
pub topics: Vec<GossipKind>,
|
2019-03-21 01:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-20 05:26:30 +00:00
|
|
|
|
|
|
|
// The default topics that we will initially subscribe to
|
|
|
|
let topics = vec![
|
2020-04-01 06:20:32 +00:00
|
|
|
GossipKind::BeaconBlock,
|
|
|
|
GossipKind::BeaconAggregateAndProof,
|
|
|
|
GossipKind::VoluntaryExit,
|
|
|
|
GossipKind::ProposerSlashing,
|
|
|
|
GossipKind::AttesterSlashing,
|
2019-12-20 05:26:30 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// The function used to generate a gossipsub message id
|
2020-09-18 02:05:34 +00:00
|
|
|
// We use the first 8 bytes of SHA256(data) for content addressing
|
|
|
|
let gossip_message_id =
|
|
|
|
|message: &GossipsubMessage| MessageId::from(&Sha256::digest(&message.data)[..8]);
|
2019-12-20 05:26:30 +00:00
|
|
|
|
2020-03-19 04:11:08 +00:00
|
|
|
// gossipsub configuration
|
|
|
|
// Note: The topics by default are sent as plain strings. Hashes are an optional
|
|
|
|
// parameter.
|
|
|
|
let gs_config = GossipsubConfigBuilder::new()
|
2020-04-05 08:29:14 +00:00
|
|
|
.max_transmit_size(GOSSIP_MAX_SIZE)
|
2020-08-06 00:04:33 +00:00
|
|
|
.heartbeat_interval(Duration::from_millis(700))
|
2020-08-30 13:06:50 +00:00
|
|
|
.mesh_n(8)
|
|
|
|
.mesh_n_low(6)
|
2020-08-06 00:04:33 +00:00
|
|
|
.mesh_n_high(12)
|
|
|
|
.gossip_lazy(6)
|
|
|
|
.fanout_ttl(Duration::from_secs(60))
|
|
|
|
.history_length(6)
|
|
|
|
.history_gossip(3)
|
2020-07-29 03:40:22 +00:00
|
|
|
.validate_messages() // require validation before propagation
|
2020-09-18 02:05:36 +00:00
|
|
|
.validation_mode(ValidationMode::Anonymous)
|
2020-08-06 00:04:33 +00:00
|
|
|
// prevent duplicates for 550 heartbeats(700millis * 550) = 385 secs
|
|
|
|
.duplicate_cache_time(Duration::from_secs(385))
|
2020-03-19 04:11:08 +00:00
|
|
|
.message_id_fn(gossip_message_id)
|
2020-08-30 13:06:50 +00:00
|
|
|
.build()
|
|
|
|
.expect("valid gossipsub configuration");
|
2020-03-19 04:11:08 +00:00
|
|
|
|
|
|
|
// discv5 configuration
|
|
|
|
let discv5_config = Discv5ConfigBuilder::new()
|
2020-06-19 04:13:23 +00:00
|
|
|
.enable_packet_filter()
|
2020-06-19 05:36:03 +00:00
|
|
|
.session_cache_capacity(1000)
|
2020-03-19 04:11:08 +00:00
|
|
|
.request_timeout(Duration::from_secs(4))
|
2020-07-21 04:19:55 +00:00
|
|
|
.request_retries(1)
|
|
|
|
.enr_peer_update_min(10)
|
2020-03-19 04:11:08 +00:00
|
|
|
.query_parallelism(5)
|
2020-06-23 03:45:40 +00:00
|
|
|
.query_timeout(Duration::from_secs(30))
|
2020-04-19 10:45:25 +00:00
|
|
|
.query_peer_timeout(Duration::from_secs(2))
|
2020-06-19 04:13:23 +00:00
|
|
|
.ip_limit() // limits /24 IP's in buckets.
|
2020-03-19 04:11:08 +00:00
|
|
|
.ping_interval(Duration::from_secs(300))
|
|
|
|
.build();
|
|
|
|
|
2020-04-14 05:29:29 +00:00
|
|
|
// NOTE: Some of these get overridden by the corresponding CLI default values.
|
2019-03-21 01:45:23 +00:00
|
|
|
Config {
|
2019-07-01 06:38:42 +00:00
|
|
|
network_dir,
|
2020-04-14 05:29:29 +00:00
|
|
|
listen_address: "0.0.0.0".parse().expect("valid ip address"),
|
2019-06-25 08:02:11 +00:00
|
|
|
libp2p_port: 9000,
|
2019-06-25 04:51:45 +00:00
|
|
|
discovery_port: 9000,
|
2020-03-19 04:11:08 +00:00
|
|
|
enr_address: None,
|
|
|
|
enr_udp_port: None,
|
|
|
|
enr_tcp_port: None,
|
2020-07-23 03:55:36 +00:00
|
|
|
target_peers: 50,
|
2020-03-19 04:11:08 +00:00
|
|
|
gs_config,
|
|
|
|
discv5_config,
|
2020-08-17 02:13:26 +00:00
|
|
|
boot_nodes_enr: vec![],
|
|
|
|
boot_nodes_multiaddr: vec![],
|
2019-08-10 01:44:17 +00:00
|
|
|
libp2p_nodes: vec![],
|
2020-08-04 07:44:53 +00:00
|
|
|
client_version: lighthouse_version::version_with_platform(),
|
2020-06-23 03:45:40 +00:00
|
|
|
disable_discovery: false,
|
2019-12-20 05:26:30 +00:00
|
|
|
topics,
|
2019-03-21 01:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|