2019-12-20 05:26:30 +00:00
|
|
|
use crate::topics::GossipTopic;
|
2019-06-25 04:51:45 +00:00
|
|
|
use enr::Enr;
|
2019-12-20 05:26:30 +00:00
|
|
|
use libp2p::gossipsub::{GossipsubConfig, GossipsubConfigBuilder, GossipsubMessage, MessageId};
|
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
|
|
|
|
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,
|
|
|
|
|
|
|
|
/// The address to broadcast to peers about which address we are listening on.
|
2019-06-25 04:51:45 +00:00
|
|
|
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,
|
|
|
|
|
2019-09-10 16:13:54 +00:00
|
|
|
/// A secp256k1 secret key, as bytes in ASCII-encoded hex.
|
|
|
|
///
|
|
|
|
/// With or without `0x` prefix.
|
|
|
|
#[serde(skip)]
|
|
|
|
pub secret_key_hex: Option<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,
|
2019-06-25 04:51:45 +00:00
|
|
|
|
2019-03-21 01:45:23 +00:00
|
|
|
/// List of nodes to initially connect to.
|
2019-06-25 04:51:45 +00:00
|
|
|
pub boot_nodes: Vec<Enr>,
|
|
|
|
|
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
|
|
|
|
2019-04-03 05:00:09 +00:00
|
|
|
/// List of extra topics to initially subscribe to as strings.
|
2019-12-20 05:26:30 +00:00
|
|
|
pub topics: Vec<GossipTopic>,
|
2019-11-27 01:47:46 +00:00
|
|
|
|
|
|
|
/// Introduces randomization in network propagation of messages. This should only be set for
|
|
|
|
/// testing purposes and will likely be removed in future versions.
|
|
|
|
// TODO: Remove this functionality for mainnet
|
|
|
|
pub propagation_percentage: Option<u8>,
|
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![
|
|
|
|
GossipTopic::BeaconBlock,
|
|
|
|
GossipTopic::BeaconAttestation,
|
|
|
|
GossipTopic::VoluntaryExit,
|
|
|
|
GossipTopic::ProposerSlashing,
|
|
|
|
GossipTopic::AttesterSlashing,
|
|
|
|
];
|
|
|
|
|
|
|
|
// The function used to generate a gossipsub message id
|
|
|
|
// We use base64(SHA256(data)) for content addressing
|
|
|
|
let gossip_message_id = |message: &GossipsubMessage| {
|
|
|
|
MessageId(base64::encode_config(
|
|
|
|
&Sha256::digest(&message.data),
|
|
|
|
base64::URL_SAFE,
|
|
|
|
))
|
|
|
|
};
|
|
|
|
|
2019-03-21 01:45:23 +00:00
|
|
|
Config {
|
2019-07-01 06:38:42 +00:00
|
|
|
network_dir,
|
2019-07-26 19:26:06 +00:00
|
|
|
listen_address: "127.0.0.1".parse().expect("valid ip address"),
|
2019-06-25 08:02:11 +00:00
|
|
|
libp2p_port: 9000,
|
|
|
|
discovery_address: "127.0.0.1".parse().expect("valid ip address"),
|
2019-06-25 04:51:45 +00:00
|
|
|
discovery_port: 9000,
|
|
|
|
max_peers: 10,
|
2019-09-10 16:13:54 +00:00
|
|
|
secret_key_hex: None,
|
2019-08-29 11:23:28 +00:00
|
|
|
// Note: The topics by default are sent as plain strings. Hashes are an optional
|
|
|
|
// parameter.
|
2019-03-26 04:01:05 +00:00
|
|
|
gs_config: GossipsubConfigBuilder::new()
|
2019-08-29 11:23:28 +00:00
|
|
|
.max_transmit_size(1_048_576)
|
2019-09-04 16:06:39 +00:00
|
|
|
.heartbeat_interval(Duration::from_secs(20)) // TODO: Reduce for mainnet
|
2020-01-03 06:56:03 +00:00
|
|
|
.manual_propagation() // require validation before propagation
|
|
|
|
.no_source_id()
|
2019-12-20 05:26:30 +00:00
|
|
|
.message_id_fn(gossip_message_id)
|
2019-03-26 04:01:05 +00:00
|
|
|
.build(),
|
2019-06-07 23:44:27 +00:00
|
|
|
boot_nodes: vec![],
|
2019-08-10 01:44:17 +00:00
|
|
|
libp2p_nodes: vec![],
|
2019-03-21 01:45:23 +00:00
|
|
|
client_version: version::version(),
|
2019-12-20 05:26:30 +00:00
|
|
|
topics,
|
2019-11-27 01:47:46 +00:00
|
|
|
propagation_percentage: None,
|
2019-03-21 01:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|