mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-03 00:31:50 +00:00
Add regression tests for boot_node (#2749)
## Issue Addressed
Resolves #2602
## Proposed Changes
*Note: For a review it might help to look at the individual commits.*
### `boot_node`
Add support for the flags `dump-config` and `immediate-shutdown`. For `immediate-shutdown` the actual behavior could be described as `dump-config-and-exit`.
Both flags are handled in `boot_node::main`, which appears to be the simplest approach.
### `boot_node` regression tests
Added in `lighthouse/tests/boot_node.rs`.
### `CommandLineTestExec`
Factors out boilerplate related to CLI tests. It's used in the regression tests for `boot_node`, `beacon_node` and `validator_client`.
## Open TODO
Add tests for `boot_node` flags `enable-enr-auto-update` and `disable-packet-filter`. They end up in [`Discv5Config`](9ed2cba6bc/boot_node/src/config.rs (L29)), which doesn't support serde (de)serialization.
I haven't found a workaround - guidance would be appreciated.
This commit is contained in:
@@ -5,6 +5,7 @@ use lighthouse_network::{
|
||||
discovery::{create_enr_builder_from_config, load_enr_from_disk, use_or_load_enr},
|
||||
load_private_key, CombinedKeyExt, NetworkConfig,
|
||||
};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use ssz::Encode;
|
||||
use std::convert::TryFrom;
|
||||
use std::net::SocketAddr;
|
||||
@@ -130,3 +131,35 @@ impl<T: EthSpec> TryFrom<&ArgMatches<'_>> for BootNodeConfig<T> {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// The set of configuration parameters that can safely be (de)serialized.
|
||||
///
|
||||
/// Its fields are a subset of the fields of `BootNodeConfig`.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct BootNodeConfigSerialization {
|
||||
pub listen_socket: SocketAddr,
|
||||
// TODO: Generalise to multiaddr
|
||||
pub boot_nodes: Vec<Enr>,
|
||||
pub local_enr: Enr,
|
||||
}
|
||||
|
||||
impl BootNodeConfigSerialization {
|
||||
/// Returns a `BootNodeConfigSerialization` obtained from copying resp. cloning the
|
||||
/// relevant fields of `config`
|
||||
pub fn from_config_ref<T: EthSpec>(config: &BootNodeConfig<T>) -> Self {
|
||||
let BootNodeConfig {
|
||||
listen_socket,
|
||||
boot_nodes,
|
||||
local_enr,
|
||||
local_key: _,
|
||||
discv5_config: _,
|
||||
phantom: _,
|
||||
} = config;
|
||||
|
||||
BootNodeConfigSerialization {
|
||||
listen_socket: *listen_socket,
|
||||
boot_nodes: boot_nodes.clone(),
|
||||
local_enr: local_enr.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,17 +3,24 @@ use clap::ArgMatches;
|
||||
use slog::{o, Drain, Level, Logger};
|
||||
|
||||
use std::convert::TryFrom;
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
mod cli;
|
||||
mod config;
|
||||
pub mod config;
|
||||
mod server;
|
||||
pub use cli::cli_app;
|
||||
use config::BootNodeConfig;
|
||||
use config::{BootNodeConfig, BootNodeConfigSerialization};
|
||||
use types::{EthSpec, EthSpecId};
|
||||
|
||||
const LOG_CHANNEL_SIZE: usize = 2048;
|
||||
|
||||
/// Run the bootnode given the CLI configuration.
|
||||
pub fn run(matches: &ArgMatches<'_>, eth_spec_id: EthSpecId, debug_level: String) {
|
||||
pub fn run(
|
||||
lh_matches: &ArgMatches<'_>,
|
||||
bn_matches: &ArgMatches<'_>,
|
||||
eth_spec_id: EthSpecId,
|
||||
debug_level: String,
|
||||
) {
|
||||
let debug_level = match debug_level.as_str() {
|
||||
"trace" => log::Level::Trace,
|
||||
"debug" => log::Level::Debug,
|
||||
@@ -49,24 +56,40 @@ pub fn run(matches: &ArgMatches<'_>, eth_spec_id: EthSpecId, debug_level: String
|
||||
let log = slog_scope::logger();
|
||||
// Run the main function emitting any errors
|
||||
if let Err(e) = match eth_spec_id {
|
||||
EthSpecId::Minimal => main::<types::MinimalEthSpec>(matches, log),
|
||||
EthSpecId::Mainnet => main::<types::MainnetEthSpec>(matches, log),
|
||||
EthSpecId::Minimal => main::<types::MinimalEthSpec>(lh_matches, bn_matches, log),
|
||||
EthSpecId::Mainnet => main::<types::MainnetEthSpec>(lh_matches, bn_matches, log),
|
||||
} {
|
||||
slog::crit!(slog_scope::logger(), "{}", e);
|
||||
}
|
||||
}
|
||||
|
||||
fn main<T: EthSpec>(matches: &ArgMatches<'_>, log: slog::Logger) -> Result<(), String> {
|
||||
fn main<T: EthSpec>(
|
||||
lh_matches: &ArgMatches<'_>,
|
||||
bn_matches: &ArgMatches<'_>,
|
||||
log: slog::Logger,
|
||||
) -> Result<(), String> {
|
||||
// Builds a custom executor for the bootnode
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.map_err(|e| format!("Failed to build runtime: {}", e))?;
|
||||
|
||||
// parse the CLI args into a useable config
|
||||
let config: BootNodeConfig<T> = BootNodeConfig::try_from(matches)?;
|
||||
// Parse the CLI args into a useable config
|
||||
let config: BootNodeConfig<T> = BootNodeConfig::try_from(bn_matches)?;
|
||||
|
||||
// Dump config if `dump-config` flag is set
|
||||
let dump_config = clap_utils::parse_optional::<PathBuf>(lh_matches, "dump-config")?;
|
||||
if let Some(dump_path) = dump_config {
|
||||
let config_sz = BootNodeConfigSerialization::from_config_ref(&config);
|
||||
let mut file = File::create(dump_path)
|
||||
.map_err(|e| format!("Failed to create dumped config: {:?}", e))?;
|
||||
serde_json::to_writer(&mut file, &config_sz)
|
||||
.map_err(|e| format!("Error serializing config: {:?}", e))?;
|
||||
}
|
||||
|
||||
// Run the boot node
|
||||
runtime.block_on(server::run(config, log));
|
||||
if !lh_matches.is_present("immediate-shutdown") {
|
||||
runtime.block_on(server::run(config, log));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user