2020-12-08 05:41:10 +00:00
|
|
|
use beacon_node::{get_data_dir, get_eth2_network_config, set_network_config};
|
2020-06-19 06:30:07 +00:00
|
|
|
use clap::ArgMatches;
|
2021-09-16 04:45:05 +00:00
|
|
|
use eth2_libp2p::discv5::{enr::CombinedKey, Discv5Config, Enr};
|
2020-08-21 12:00:01 +00:00
|
|
|
use eth2_libp2p::{
|
2021-03-30 05:17:58 +00:00
|
|
|
discovery::{create_enr_builder_from_config, load_enr_from_disk, use_or_load_enr},
|
2020-08-21 12:00:01 +00:00
|
|
|
load_private_key, CombinedKeyExt, NetworkConfig,
|
|
|
|
};
|
2020-09-29 08:28:29 +00:00
|
|
|
use ssz::Encode;
|
2020-06-19 06:30:07 +00:00
|
|
|
use std::convert::TryFrom;
|
2020-08-21 12:00:01 +00:00
|
|
|
use std::net::SocketAddr;
|
2021-03-30 05:17:58 +00:00
|
|
|
use std::{marker::PhantomData, path::PathBuf};
|
2020-09-29 08:28:29 +00:00
|
|
|
use types::EthSpec;
|
2020-06-19 06:30:07 +00:00
|
|
|
|
|
|
|
/// A set of configuration parameters for the bootnode, established from CLI arguments.
|
2020-09-29 08:28:29 +00:00
|
|
|
pub struct BootNodeConfig<T: EthSpec> {
|
2020-06-19 06:30:07 +00:00
|
|
|
pub listen_socket: SocketAddr,
|
|
|
|
// TODO: Generalise to multiaddr
|
|
|
|
pub boot_nodes: Vec<Enr>,
|
|
|
|
pub local_enr: Enr,
|
|
|
|
pub local_key: CombinedKey,
|
2021-09-16 04:45:05 +00:00
|
|
|
pub discv5_config: Discv5Config,
|
2020-09-29 08:28:29 +00:00
|
|
|
phantom: PhantomData<T>,
|
2020-06-19 06:30:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:28:29 +00:00
|
|
|
impl<T: EthSpec> TryFrom<&ArgMatches<'_>> for BootNodeConfig<T> {
|
2020-06-19 06:30:07 +00:00
|
|
|
type Error = String;
|
|
|
|
|
|
|
|
fn try_from(matches: &ArgMatches<'_>) -> Result<Self, Self::Error> {
|
2020-08-21 12:00:01 +00:00
|
|
|
let data_dir = get_data_dir(matches);
|
2020-06-19 06:30:07 +00:00
|
|
|
|
2020-12-08 05:41:10 +00:00
|
|
|
// Try and grab network config from input CLI params
|
2021-07-30 01:11:47 +00:00
|
|
|
let eth2_network_config = get_eth2_network_config(matches)?;
|
2020-09-29 08:28:29 +00:00
|
|
|
|
|
|
|
// Try and obtain bootnodes
|
|
|
|
|
|
|
|
let boot_nodes = {
|
|
|
|
let mut boot_nodes = Vec::new();
|
|
|
|
|
2020-12-08 05:41:10 +00:00
|
|
|
if let Some(enr) = ð2_network_config.boot_enr {
|
|
|
|
boot_nodes.extend_from_slice(enr);
|
2020-09-29 08:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(nodes) = matches.value_of("boot-nodes") {
|
|
|
|
boot_nodes.extend_from_slice(
|
|
|
|
&nodes
|
|
|
|
.split(',')
|
|
|
|
.map(|enr| enr.parse().map_err(|_| format!("Invalid ENR: {}", enr)))
|
|
|
|
.collect::<Result<Vec<Enr>, _>>()?,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_nodes
|
|
|
|
};
|
|
|
|
|
2020-08-21 12:00:01 +00:00
|
|
|
let mut network_config = NetworkConfig::default();
|
|
|
|
|
|
|
|
let logger = slog_scope::logger();
|
|
|
|
|
|
|
|
set_network_config(&mut network_config, matches, &data_dir, &logger, true)?;
|
2021-09-16 04:45:05 +00:00
|
|
|
|
|
|
|
// Set the enr-udp-port to the default listening port if it was not specified.
|
2020-09-29 08:28:29 +00:00
|
|
|
if !matches.is_present("enr-udp-port") {
|
2021-09-16 04:45:05 +00:00
|
|
|
network_config.enr_udp_port = Some(network_config.discovery_port);
|
2020-09-29 08:28:29 +00:00
|
|
|
}
|
2020-08-21 12:00:01 +00:00
|
|
|
|
2021-09-16 04:45:05 +00:00
|
|
|
// By default this is enabled. If it is not set, revert to false.
|
|
|
|
if !matches.is_present("enable-enr-auto-update") {
|
|
|
|
network_config.discv5_config.enr_update = false;
|
|
|
|
}
|
2020-06-19 06:30:07 +00:00
|
|
|
|
|
|
|
// the address to listen on
|
2020-08-21 12:00:01 +00:00
|
|
|
let listen_socket =
|
|
|
|
SocketAddr::new(network_config.listen_address, network_config.discovery_port);
|
2020-06-19 06:30:07 +00:00
|
|
|
|
2021-03-30 05:17:58 +00:00
|
|
|
let private_key = load_private_key(&network_config, &logger);
|
|
|
|
let local_key = CombinedKey::from_libp2p(&private_key)?;
|
|
|
|
|
|
|
|
let local_enr = if let Some(dir) = matches.value_of("network-dir") {
|
|
|
|
let network_dir: PathBuf = dir.into();
|
|
|
|
load_enr_from_disk(&network_dir)?
|
|
|
|
} else {
|
|
|
|
// build the enr_fork_id and add it to the local_enr if it exists
|
|
|
|
let enr_fork = {
|
2021-07-09 06:15:32 +00:00
|
|
|
let spec = eth2_network_config.chain_spec::<T>()?;
|
2021-03-30 05:17:58 +00:00
|
|
|
|
|
|
|
if eth2_network_config.beacon_state_is_known() {
|
|
|
|
let genesis_state = eth2_network_config.beacon_state::<T>()?;
|
|
|
|
|
|
|
|
slog::info!(logger, "Genesis state found"; "root" => genesis_state.canonical_root().to_string());
|
2021-08-04 01:44:57 +00:00
|
|
|
let enr_fork = spec.enr_fork_id::<T>(
|
2021-03-30 05:17:58 +00:00
|
|
|
types::Slot::from(0u64),
|
2021-07-09 06:15:32 +00:00
|
|
|
genesis_state.genesis_validators_root(),
|
2021-03-30 05:17:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Some(enr_fork.as_ssz_bytes())
|
|
|
|
} else {
|
|
|
|
slog::warn!(
|
|
|
|
logger,
|
|
|
|
"No genesis state provided. No Eth2 field added to the ENR"
|
|
|
|
);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Build the local ENR
|
|
|
|
|
|
|
|
let mut local_enr = {
|
|
|
|
let mut builder = create_enr_builder_from_config(&network_config, false);
|
|
|
|
|
|
|
|
// If we know of the ENR field, add it to the initial construction
|
|
|
|
if let Some(enr_fork_bytes) = enr_fork {
|
2021-08-04 01:44:57 +00:00
|
|
|
builder.add_value("eth2", enr_fork_bytes.as_slice());
|
2021-03-30 05:17:58 +00:00
|
|
|
}
|
|
|
|
builder
|
|
|
|
.build(&local_key)
|
|
|
|
.map_err(|e| format!("Failed to build ENR: {:?}", e))?
|
|
|
|
};
|
|
|
|
|
|
|
|
use_or_load_enr(&local_key, &mut local_enr, &network_config, &logger)?;
|
|
|
|
local_enr
|
|
|
|
};
|
|
|
|
|
2020-06-19 06:30:07 +00:00
|
|
|
Ok(BootNodeConfig {
|
|
|
|
listen_socket,
|
|
|
|
boot_nodes,
|
|
|
|
local_enr,
|
|
|
|
local_key,
|
2021-09-16 04:45:05 +00:00
|
|
|
discv5_config: network_config.discv5_config,
|
2020-09-29 08:28:29 +00:00
|
|
|
phantom: PhantomData,
|
2020-06-19 06:30:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|