diff --git a/beacon_node/lighthouse_network/Cargo.toml b/beacon_node/lighthouse_network/Cargo.toml index 4f1825af201..d1107cfd8cb 100644 --- a/beacon_node/lighthouse_network/Cargo.toml +++ b/beacon_node/lighthouse_network/Cargo.toml @@ -49,6 +49,7 @@ tracing-subscriber = { workspace = true } types = { workspace = true } unsigned-varint = { version = "0.8", features = ["codec"] } unused_port = { workspace = true } +clap = { version = "4", features = ["derive"] } [dependencies.libp2p] version = "0.55" diff --git a/beacon_node/lighthouse_network/src/service/mod.rs b/beacon_node/lighthouse_network/src/service/mod.rs index 3f0b5b96ef2..3cb8cf3c34c 100644 --- a/beacon_node/lighthouse_network/src/service/mod.rs +++ b/beacon_node/lighthouse_network/src/service/mod.rs @@ -44,6 +44,9 @@ use types::{ }; use types::{ChainSpec, ForkName}; use utils::{build_transport, strip_peer_id, Context as ServiceContext}; +use clap::ArgMatches; +use super::utils; +use crate::NetworkConfig; pub mod api_types; mod gossip_cache; @@ -178,8 +181,9 @@ impl Network { let config = ctx.config.clone(); trace!("Libp2p Service starting"); // initialise the node's ID - let local_keypair = utils::load_private_key(&config); - + pub fn initialize_network(config: NetworkConfig, cli_args: &ArgMatches) { + let local_keypair = utils::load_private_key(&config, cli_args); + } // Trusted peers will also be marked as explicit in GossipSub. // Cfr. https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md#explicit-peering-agreements let trusted_peers: Vec = config diff --git a/beacon_node/lighthouse_network/src/service/utils.rs b/beacon_node/lighthouse_network/src/service/utils.rs index 01929bcb01c..e7397c3c90b 100644 --- a/beacon_node/lighthouse_network/src/service/utils.rs +++ b/beacon_node/lighthouse_network/src/service/utils.rs @@ -20,6 +20,10 @@ use tracing::{debug, warn}; use types::{ ChainSpec, DataColumnSubnetId, EnrForkId, EthSpec, ForkContext, SubnetId, SyncSubnetId, }; +use clap::ArgMatches; +use tracing::{error, debug, info} +use std::path::PathBuf; +use std::fs; pub const NETWORK_KEY_FILENAME: &str = "key"; /// The filename to store our local metadata. @@ -107,43 +111,28 @@ fn keypair_from_bytes(mut bytes: Vec) -> Result { /// generated and is then saved to disk. /// /// Currently only secp256k1 keys are allowed, as these are the only keys supported by discv5. -pub fn load_private_key(config: &NetworkConfig) -> Keypair { - // check for key from disk - let network_key_f = config.network_dir.join(NETWORK_KEY_FILENAME); - if let Ok(mut network_key_file) = File::open(network_key_f.clone()) { - let mut key_bytes: Vec = Vec::with_capacity(36); - match network_key_file.read_to_end(&mut key_bytes) { - Err(_) => debug!("Could not read network key file"), - Ok(_) => { - // only accept secp256k1 keys for now - if let Ok(secret_key) = secp256k1::SecretKey::try_from_bytes(&mut key_bytes) { - let kp: secp256k1::Keypair = secret_key.into(); - debug!("Loaded network key from disk."); - return kp.into(); - } else { - debug!("Network key file is not a valid secp256k1 key"); + +pub fn load_private_key(config: &NetworkConfig, cli_args: &ArgMatches) -> Keypair { + if let Some(custom_key_path) = cli_args.get_one::("p2p-priv-key") { + let path = PathBuf::from(custom_key_path); + match fs::read_to_string(&path) { + Ok(key_hex) => match keypair_from_hex(key_hex.trim()) { + Ok(keypair) => { + debug!("Loaded custom p2p key from file: {:?}", path); + return keypair; + } + Err(e) => { + error!("Failed to decode custom p2p key from hex: {}", e); } + }, + Err(e) => { + error!("Failed to read custom p2p key file {:?}: {}", path, e); } } } - // if a key could not be loaded from disk, generate a new one and save it - let local_private_key = secp256k1::Keypair::generate(); - let _ = std::fs::create_dir_all(&config.network_dir); - match File::create(network_key_f.clone()) - .and_then(|mut f| f.write_all(&local_private_key.secret().to_bytes())) - { - Ok(_) => { - debug!("New network key generated and written to disk"); - } - Err(e) => { - warn!( - "Could not write node key to file: {:?}. error: {}", - network_key_f, e - ); - } - } - local_private_key.into() + let key_path = config.network_dir.join("key"); + load_or_create_keypair(key_path) } /// Generate authenticated XX Noise config from identity keys diff --git a/boot_node/src/config.rs b/boot_node/src/config.rs index c43a8b397b1..60646db0c86 100644 --- a/boot_node/src/config.rs +++ b/boot_node/src/config.rs @@ -13,6 +13,8 @@ use std::net::{SocketAddrV4, SocketAddrV6}; use std::time::Duration; use std::{marker::PhantomData, path::PathBuf}; use types::EthSpec; +use clap::ArgMatches; +use crate::utils; /// A set of configuration parameters for the bootnode, established from CLI arguments. pub struct BootNodeConfig { @@ -83,7 +85,10 @@ impl BootNodeConfig { network_config.discv5_config.enr_update = false; } - let private_key = load_private_key(&network_config); + pub fn initialize_network(config: NetworkConfig, cli_args: &ArgMatches) { + let local_keypair = utils::load_private_key(&config, cli_args); + } + let local_key = CombinedKey::from_libp2p(private_key)?; let local_enr = if let Some(dir) = matches.get_one::("network-dir") { diff --git a/lighthouse/src/main.rs b/lighthouse/src/main.rs index 2b7387e0763..03b5aad1334 100644 --- a/lighthouse/src/main.rs +++ b/lighthouse/src/main.rs @@ -503,6 +503,12 @@ fn main() { exit(1) } } + + let cli_args = cli::parse_args(); + let config = NetworkConfig::default(); + + // pass `cli_args` explicitly into initialize_network + network::initialize_network(config, &cli_args) } fn run(