lighthouse/beacon_node/eth2-libp2p/src/config.rs

144 lines
4.9 KiB
Rust
Raw Normal View History

use crate::types::GossipKind;
use crate::Enr;
use libp2p::discv5::{Discv5Config, Discv5ConfigBuilder};
use libp2p::gossipsub::{GossipsubConfig, GossipsubConfigBuilder, GossipsubMessage, MessageId};
use libp2p::Multiaddr;
2019-06-07 23:44:27 +00:00
use serde_derive::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
2019-07-01 06:38:42 +00:00
use std::path::PathBuf;
use std::time::Duration;
2019-03-21 01:45:23 +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,
/// UDP port that discovery listens on.
pub discovery_port: u16,
/// 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.
pub enr_address: Option<std::net::IpAddr>,
2019-06-25 04:51:45 +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.
pub max_peers: usize,
/// 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
/// Discv5 configuration parameters.
#[serde(skip)]
pub discv5_config: Discv5Config,
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>,
/// 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
/// List of extra topics to initially subscribe to as strings.
pub topics: Vec<GossipKind>,
Testnet compatible network upgrade (#587) * Create libp2p instance * Change logger to stdlog * test_connection initial commit * Add gossipsub test * Delete tests in network crate * Add test module * Clean tests * Remove dependency on discovery * Working publish between 2 nodes TODO: Publish should be called just once * Working 2 peer gossipsub test with additional events * Cleanup test * Add rpc test * Star topology discovery WIP * build_nodes builds and connects n nodes. Increase nodes in gossipsub test * Add unsubscribe method and expose reference to gossipsub object for gossipsub tests * Add gossipsub message forwarding test * Fix gossipsub forward test * Test improvements * Remove discovery tests * Simplify gossipsub forward test topology * Add helper functions for topology building * Clean up tests * Update naming to new network spec * Correct ssz encoding of protocol names * Further additions to network upgrade * Initial network spec update WIP * Temp commit * Builds one side of the streamed RPC responses * Temporary commit * Propagates streaming changes up into message handler * Intermediate network update * Partial update in upgrading to the new network spec * Update dependencies, remove redundant deps * Correct sync manager for block stream handling * Re-write of RPC handler, improves efficiency and corrects bugs * Stream termination update * Completed refactor of rpc handler * Remove crates * Correct compile issues associated with test merge * Build basic tests and testing structure for eth2-libp2p * Enhance RPC tests and add logging * Complete RPC testing framework and STATUS test * Decoding bug fixes, log improvements, stream test * Clean up RPC handler logging * Decoder bug fix, empty block stream test * Add BlocksByRoot RPC test * Add Goodbye RPC test * Syncing and stream handling bug fixes and performance improvements * Applies discv5 bug fixes * Adds DHT IP filtering for lighthouse - currently disabled * Adds randomized network propagation as a CLI arg * Add disconnect functionality * Adds attestation handling and parent lookup * Adds RPC error handling for the sync manager * Allow parent's blocks to be already processed * Update github workflow * Adds reviewer suggestions
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");
// The default topics that we will initially subscribe to
let topics = vec![
GossipKind::BeaconBlock,
GossipKind::BeaconAggregateAndProof,
GossipKind::VoluntaryExit,
GossipKind::ProposerSlashing,
GossipKind::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,
))
};
// gossipsub configuration
// Note: The topics by default are sent as plain strings. Hashes are an optional
// parameter.
let gs_config = GossipsubConfigBuilder::new()
.max_transmit_size(GOSSIP_MAX_SIZE)
.heartbeat_interval(Duration::from_secs(1))
.manual_propagation() // require validation before propagation
.no_source_id()
.message_id_fn(gossip_message_id)
.build();
// discv5 configuration
let discv5_config = Discv5ConfigBuilder::new()
.request_timeout(Duration::from_secs(4))
.request_retries(2)
.enr_update(true) // update IP based on PONG responses
.enr_peer_update_min(2) // prevents NAT's should be raised for mainnet
.query_parallelism(5)
.query_timeout(Duration::from_secs(60))
.query_peer_timeout(Duration::from_secs(2))
.ip_limit(false) // limits /24 IP's in buckets. Enable for mainnet
.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,
enr_address: None,
enr_udp_port: None,
enr_tcp_port: None,
2020-04-14 05:29:29 +00:00
max_peers: 50,
secret_key_hex: None,
gs_config,
discv5_config,
2019-06-07 23:44:27 +00:00
boot_nodes: vec![],
libp2p_nodes: vec![],
2019-03-21 01:45:23 +00:00
client_version: version::version(),
topics,
Testnet compatible network upgrade (#587) * Create libp2p instance * Change logger to stdlog * test_connection initial commit * Add gossipsub test * Delete tests in network crate * Add test module * Clean tests * Remove dependency on discovery * Working publish between 2 nodes TODO: Publish should be called just once * Working 2 peer gossipsub test with additional events * Cleanup test * Add rpc test * Star topology discovery WIP * build_nodes builds and connects n nodes. Increase nodes in gossipsub test * Add unsubscribe method and expose reference to gossipsub object for gossipsub tests * Add gossipsub message forwarding test * Fix gossipsub forward test * Test improvements * Remove discovery tests * Simplify gossipsub forward test topology * Add helper functions for topology building * Clean up tests * Update naming to new network spec * Correct ssz encoding of protocol names * Further additions to network upgrade * Initial network spec update WIP * Temp commit * Builds one side of the streamed RPC responses * Temporary commit * Propagates streaming changes up into message handler * Intermediate network update * Partial update in upgrading to the new network spec * Update dependencies, remove redundant deps * Correct sync manager for block stream handling * Re-write of RPC handler, improves efficiency and corrects bugs * Stream termination update * Completed refactor of rpc handler * Remove crates * Correct compile issues associated with test merge * Build basic tests and testing structure for eth2-libp2p * Enhance RPC tests and add logging * Complete RPC testing framework and STATUS test * Decoding bug fixes, log improvements, stream test * Clean up RPC handler logging * Decoder bug fix, empty block stream test * Add BlocksByRoot RPC test * Add Goodbye RPC test * Syncing and stream handling bug fixes and performance improvements * Applies discv5 bug fixes * Adds DHT IP filtering for lighthouse - currently disabled * Adds randomized network propagation as a CLI arg * Add disconnect functionality * Adds attestation handling and parent lookup * Adds RPC error handling for the sync manager * Allow parent's blocks to be already processed * Update github workflow * Adds reviewer suggestions
2019-11-27 01:47:46 +00:00
propagation_percentage: None,
2019-03-21 01:45:23 +00:00
}
}
}