2020-12-23 07:53:36 +00:00
|
|
|
use crate::types::GossipKind;
|
2020-09-22 01:12:36 +00:00
|
|
|
use crate::{Enr, PeerIdSerialized};
|
2020-09-29 00:02:44 +00:00
|
|
|
use directory::{
|
2020-12-08 05:41:10 +00:00
|
|
|
DEFAULT_BEACON_NODE_DIR, DEFAULT_HARDCODED_NETWORK, DEFAULT_NETWORK_DIR, DEFAULT_ROOT_DIR,
|
2020-09-29 00:02:44 +00:00
|
|
|
};
|
2020-05-17 11:16:48 +00:00
|
|
|
use discv5::{Discv5Config, Discv5ConfigBuilder};
|
2020-07-29 03:40:22 +00:00
|
|
|
use libp2p::gossipsub::{
|
2020-12-23 07:53:36 +00:00
|
|
|
FastMessageId, GossipsubConfig, GossipsubConfigBuilder, GossipsubMessage, MessageId,
|
|
|
|
RawGossipsubMessage, ValidationMode,
|
2020-07-29 03:40:22 +00:00
|
|
|
};
|
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;
|
2021-08-04 01:44:57 +00:00
|
|
|
use std::sync::Arc;
|
2019-04-03 06:16:32 +00:00
|
|
|
use std::time::Duration;
|
2021-08-04 01:44:57 +00:00
|
|
|
use types::{ForkContext, ForkName};
|
2019-03-21 01:45:23 +00:00
|
|
|
|
2021-07-13 05:37:52 +00:00
|
|
|
/// The maximum transmit size of gossip messages in bytes.
|
2020-04-05 08:29:14 +00:00
|
|
|
pub const GOSSIP_MAX_SIZE: usize = 1_048_576;
|
2021-07-13 05:37:52 +00:00
|
|
|
/// This is a constant to be used in discovery. The lower bound of the gossipsub mesh.
|
|
|
|
pub const MESH_N_LOW: usize = 6;
|
2020-12-23 07:53:36 +00:00
|
|
|
|
|
|
|
// We treat uncompressed messages as invalid and never use the INVALID_SNAPPY_DOMAIN as in the
|
|
|
|
// specification. We leave it here for posterity.
|
|
|
|
// const MESSAGE_DOMAIN_INVALID_SNAPPY: [u8; 4] = [0, 0, 0, 0];
|
2020-10-14 06:51:58 +00:00
|
|
|
const MESSAGE_DOMAIN_VALID_SNAPPY: [u8; 4] = [1, 0, 0, 0];
|
|
|
|
|
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>,
|
|
|
|
|
2020-09-22 01:12:36 +00:00
|
|
|
/// List of trusted libp2p nodes which are not scored.
|
|
|
|
pub trusted_peers: Vec<PeerIdSerialized>,
|
|
|
|
|
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,
|
|
|
|
|
2020-10-02 08:47:00 +00:00
|
|
|
/// Attempt to construct external port mappings with UPnP.
|
|
|
|
pub upnp_enabled: bool,
|
|
|
|
|
2020-11-13 06:06:33 +00:00
|
|
|
/// Subscribe to all subnets for the duration of the runtime.
|
|
|
|
pub subscribe_all_subnets: bool,
|
|
|
|
|
2020-11-22 23:58:25 +00:00
|
|
|
/// Import/aggregate all attestations recieved on subscribed subnets for the duration of the
|
|
|
|
/// runtime.
|
|
|
|
pub import_all_attestations: bool,
|
|
|
|
|
2020-11-30 22:55:08 +00:00
|
|
|
/// Indicates if the user has set the network to be in private mode. Currently this
|
|
|
|
/// prevents sending client identifying information over identify.
|
|
|
|
pub private: 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 {
|
2020-11-13 06:06:33 +00:00
|
|
|
// WARNING: this directory default should be always overwritten with parameters
|
2020-09-29 00:02:44 +00:00
|
|
|
// from cli for specific networks.
|
|
|
|
let network_dir = dirs::home_dir()
|
|
|
|
.unwrap_or_else(|| PathBuf::from("."))
|
|
|
|
.join(DEFAULT_ROOT_DIR)
|
2020-12-08 05:41:10 +00:00
|
|
|
.join(DEFAULT_HARDCODED_NETWORK)
|
2020-09-29 00:02:44 +00:00
|
|
|
.join(DEFAULT_BEACON_NODE_DIR)
|
|
|
|
.join(DEFAULT_NETWORK_DIR);
|
2019-12-20 05:26:30 +00:00
|
|
|
|
2021-08-04 01:44:57 +00:00
|
|
|
// Note: Using the default config here. Use `gossipsub_config` function for getting
|
|
|
|
// Lighthouse specific configuration for gossipsub.
|
2020-12-23 07:53:36 +00:00
|
|
|
let gs_config = GossipsubConfigBuilder::default()
|
2020-08-30 13:06:50 +00:00
|
|
|
.build()
|
|
|
|
.expect("valid gossipsub configuration");
|
2020-03-19 04:11:08 +00:00
|
|
|
|
2021-07-07 08:18:44 +00:00
|
|
|
// Discv5 Unsolicited Packet Rate Limiter
|
|
|
|
let filter_rate_limiter = Some(
|
|
|
|
discv5::RateLimiterBuilder::new()
|
|
|
|
.total_n_every(10, Duration::from_secs(1)) // Allow bursts, average 10 per second
|
|
|
|
.ip_n_every(9, Duration::from_secs(1)) // Allow bursts, average 9 per second
|
|
|
|
.node_n_every(8, Duration::from_secs(1)) // Allow bursts, average 8 per second
|
|
|
|
.build()
|
|
|
|
.expect("The total rate limit has been specified"),
|
|
|
|
);
|
|
|
|
|
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()
|
2021-06-15 04:40:43 +00:00
|
|
|
.session_cache_capacity(5000)
|
2020-11-06 04:14:14 +00:00
|
|
|
.request_timeout(Duration::from_secs(1))
|
|
|
|
.query_peer_timeout(Duration::from_secs(2))
|
|
|
|
.query_timeout(Duration::from_secs(30))
|
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-10-05 07:45:54 +00:00
|
|
|
.disable_report_discovered_peers()
|
2020-06-19 04:13:23 +00:00
|
|
|
.ip_limit() // limits /24 IP's in buckets.
|
2021-06-03 01:11:33 +00:00
|
|
|
.incoming_bucket_limit(8) // half the bucket size
|
2021-07-07 08:18:44 +00:00
|
|
|
.filter_rate_limiter(filter_rate_limiter)
|
|
|
|
.filter_max_bans_per_ip(Some(5))
|
|
|
|
.filter_max_nodes_per_ip(Some(10))
|
|
|
|
.ban_duration(Some(Duration::from_secs(3600)))
|
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-09-22 01:12:36 +00:00
|
|
|
trusted_peers: 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,
|
2020-10-02 08:47:00 +00:00
|
|
|
upnp_enabled: true,
|
2020-11-30 22:55:08 +00:00
|
|
|
private: false,
|
2020-11-13 06:06:33 +00:00
|
|
|
subscribe_all_subnets: false,
|
2020-11-22 23:58:25 +00:00
|
|
|
import_all_attestations: false,
|
2020-09-23 03:26:33 +00:00
|
|
|
topics: Vec::new(),
|
2019-03-21 01:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-04 01:44:57 +00:00
|
|
|
|
|
|
|
/// Return a Lighthouse specific `GossipsubConfig` where the `message_id_fn` depends on the current fork.
|
|
|
|
pub fn gossipsub_config(fork_context: Arc<ForkContext>) -> GossipsubConfig {
|
|
|
|
// The function used to generate a gossipsub message id
|
|
|
|
// We use the first 8 bytes of SHA256(data) for content addressing
|
|
|
|
let fast_gossip_message_id =
|
|
|
|
|message: &RawGossipsubMessage| FastMessageId::from(&Sha256::digest(&message.data)[..8]);
|
|
|
|
fn prefix(
|
|
|
|
prefix: [u8; 4],
|
|
|
|
message: &GossipsubMessage,
|
|
|
|
fork_context: Arc<ForkContext>,
|
|
|
|
) -> Vec<u8> {
|
|
|
|
let topic_bytes = message.topic.as_str().as_bytes();
|
|
|
|
match fork_context.current_fork() {
|
|
|
|
ForkName::Altair => {
|
|
|
|
let topic_len_bytes = topic_bytes.len().to_le_bytes();
|
|
|
|
let mut vec = Vec::with_capacity(
|
|
|
|
prefix.len() + topic_len_bytes.len() + topic_bytes.len() + message.data.len(),
|
|
|
|
);
|
|
|
|
vec.extend_from_slice(&prefix);
|
|
|
|
vec.extend_from_slice(&topic_len_bytes);
|
|
|
|
vec.extend_from_slice(topic_bytes);
|
|
|
|
vec.extend_from_slice(&message.data);
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
ForkName::Base => {
|
|
|
|
let mut vec = Vec::with_capacity(prefix.len() + message.data.len());
|
|
|
|
vec.extend_from_slice(&prefix);
|
|
|
|
vec.extend_from_slice(&message.data);
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let gossip_message_id = move |message: &GossipsubMessage| {
|
|
|
|
MessageId::from(
|
|
|
|
&Sha256::digest(
|
|
|
|
prefix(MESSAGE_DOMAIN_VALID_SNAPPY, message, fork_context.clone()).as_slice(),
|
|
|
|
)[..20],
|
|
|
|
)
|
|
|
|
};
|
|
|
|
GossipsubConfigBuilder::default()
|
|
|
|
.max_transmit_size(GOSSIP_MAX_SIZE)
|
|
|
|
.heartbeat_interval(Duration::from_millis(700))
|
|
|
|
.mesh_n(8)
|
|
|
|
.mesh_n_low(MESH_N_LOW)
|
|
|
|
.mesh_n_high(12)
|
|
|
|
.gossip_lazy(6)
|
|
|
|
.fanout_ttl(Duration::from_secs(60))
|
|
|
|
.history_length(12)
|
|
|
|
.max_messages_per_rpc(Some(500)) // Responses to IWANT can be quite large
|
|
|
|
.history_gossip(3)
|
|
|
|
.validate_messages() // require validation before propagation
|
|
|
|
.validation_mode(ValidationMode::Anonymous)
|
|
|
|
// prevent duplicates for 550 heartbeats(700millis * 550) = 385 secs
|
|
|
|
.duplicate_cache_time(Duration::from_secs(385))
|
|
|
|
.message_id_fn(gossip_message_id)
|
|
|
|
.fast_message_id_fn(fast_gossip_message_id)
|
|
|
|
.allow_self_origin(true)
|
|
|
|
.build()
|
|
|
|
.expect("valid gossipsub configuration")
|
|
|
|
}
|