Files
lighthouse/boot_node/src/config.rs
blacktemplar 2bc9115a94 reuse beacon_node methods for initializing network configs in boot_node (#1520)
## Issue Addressed

#1378

## Proposed Changes

Boot node reuses code from beacon_node to initialize network config. This also enables using the network directory to store/load the enr and the private key.

## Additional Info

Note that before this PR the port cli arguments were off (the argument was named `enr-port` but used as `boot-node-enr-port`).
Therefore as port always the cli port argument was used (for both enr and listening). Now the enr-port argument can be used to overwrite the listening port as the public port others should connect to.

Last but not least note, that this restructuring reuses `ethlibp2p::NetworkConfig` that has many more options than the ones used in the boot node. For example the network config has an own `discv5_config` field that gets never used in the boot node and instead another `Discv5Config` gets created later in the boot node process.

Co-authored-by: Age Manning <Age@AgeManning.com>
2020-08-21 12:00:01 +00:00

68 lines
2.1 KiB
Rust

use beacon_node::{get_data_dir, set_network_config};
use clap::ArgMatches;
use discv5::{enr::CombinedKey, Enr};
use eth2_libp2p::{
discovery::{create_enr_builder_from_config, use_or_load_enr},
load_private_key, CombinedKeyExt, NetworkConfig,
};
use std::convert::TryFrom;
use std::net::SocketAddr;
/// A set of configuration parameters for the bootnode, established from CLI arguments.
pub struct BootNodeConfig {
pub listen_socket: SocketAddr,
// TODO: Generalise to multiaddr
pub boot_nodes: Vec<Enr>,
pub local_enr: Enr,
pub local_key: CombinedKey,
pub auto_update: bool,
}
impl TryFrom<&ArgMatches<'_>> for BootNodeConfig {
type Error = String;
fn try_from(matches: &ArgMatches<'_>) -> Result<Self, Self::Error> {
let data_dir = get_data_dir(matches);
let mut network_config = NetworkConfig::default();
let logger = slog_scope::logger();
set_network_config(&mut network_config, matches, &data_dir, &logger, true)?;
let private_key = load_private_key(&network_config, &logger);
let local_key = CombinedKey::from_libp2p(&private_key)?;
let mut local_enr = create_enr_builder_from_config(&network_config)
.build(&local_key)
.map_err(|e| format!("Failed to build ENR: {:?}", e))?;
use_or_load_enr(&local_key, &mut local_enr, &network_config, &logger)?;
let boot_nodes = {
if let Some(boot_nodes) = matches.value_of("boot-nodes") {
boot_nodes
.split(',')
.map(|enr| enr.parse().map_err(|_| format!("Invalid ENR: {}", enr)))
.collect::<Result<Vec<Enr>, _>>()?
} else {
Vec::new()
}
};
let auto_update = matches.is_present("enable-enr_auto_update");
// the address to listen on
let listen_socket =
SocketAddr::new(network_config.listen_address, network_config.discovery_port);
Ok(BootNodeConfig {
listen_socket,
boot_nodes,
local_enr,
local_key,
auto_update,
})
}
}