From f19b0122217535cbd45ce3c1901a57511680ab90 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 22 Mar 2025 02:35:17 +0530 Subject: [PATCH 01/10] Update mod.rs --- beacon_node/lighthouse_network/src/service/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon_node/lighthouse_network/src/service/mod.rs b/beacon_node/lighthouse_network/src/service/mod.rs index 3f0b5b96ef2..33ff95ffbee 100644 --- a/beacon_node/lighthouse_network/src/service/mod.rs +++ b/beacon_node/lighthouse_network/src/service/mod.rs @@ -178,7 +178,7 @@ impl Network { let config = ctx.config.clone(); trace!("Libp2p Service starting"); // initialise the node's ID - let local_keypair = utils::load_private_key(&config); + 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 From 5cbca3d6917b6c6805d5422d4a59c9ba3d0cdbb0 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 22 Mar 2025 02:36:15 +0530 Subject: [PATCH 02/10] Update config.rs --- boot_node/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boot_node/src/config.rs b/boot_node/src/config.rs index c43a8b397b1..5be8d4fa3f0 100644 --- a/boot_node/src/config.rs +++ b/boot_node/src/config.rs @@ -83,7 +83,7 @@ impl BootNodeConfig { network_config.discv5_config.enr_update = false; } - let private_key = load_private_key(&network_config); + let private_key = load_private_key(&network_config, &cli_args); let local_key = CombinedKey::from_libp2p(private_key)?; let local_enr = if let Some(dir) = matches.get_one::("network-dir") { From 7cbc48b07eff3b62ef7660eea202e4eaa6df26f6 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Sat, 22 Mar 2025 02:38:12 +0530 Subject: [PATCH 03/10] Update utils.rs --- .../lighthouse_network/src/service/utils.rs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/beacon_node/lighthouse_network/src/service/utils.rs b/beacon_node/lighthouse_network/src/service/utils.rs index 01929bcb01c..df7e53d9575 100644 --- a/beacon_node/lighthouse_network/src/service/utils.rs +++ b/beacon_node/lighthouse_network/src/service/utils.rs @@ -107,7 +107,27 @@ 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 { +pub fn load_private_key(config: &NetworkConfig, cli_args: &ArgsMatches) -> 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); + } + } + } // 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()) { From c8cefbf5f9c20ecf25b56b158f175255c228395f Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 25 Mar 2025 14:28:57 +0530 Subject: [PATCH 04/10] Update mod.rs --- beacon_node/lighthouse_network/src/service/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/beacon_node/lighthouse_network/src/service/mod.rs b/beacon_node/lighthouse_network/src/service/mod.rs index 33ff95ffbee..42490d7a852 100644 --- a/beacon_node/lighthouse_network/src/service/mod.rs +++ b/beacon_node/lighthouse_network/src/service/mod.rs @@ -44,6 +44,8 @@ use types::{ }; use types::{ChainSpec, ForkName}; use utils::{build_transport, strip_peer_id, Context as ServiceContext}; +use clap::ArgMatches; +use crate::utils; pub mod api_types; mod gossip_cache; @@ -178,8 +180,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, &cli_args); - + 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 From a7796731d5e89903e566188b6ce7045acaaad59a Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 25 Mar 2025 14:29:32 +0530 Subject: [PATCH 05/10] Update utils.rs --- beacon_node/lighthouse_network/src/service/utils.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/beacon_node/lighthouse_network/src/service/utils.rs b/beacon_node/lighthouse_network/src/service/utils.rs index df7e53d9575..5c5c95393d6 100644 --- a/beacon_node/lighthouse_network/src/service/utils.rs +++ b/beacon_node/lighthouse_network/src/service/utils.rs @@ -20,6 +20,7 @@ use tracing::{debug, warn}; use types::{ ChainSpec, DataColumnSubnetId, EnrForkId, EthSpec, ForkContext, SubnetId, SyncSubnetId, }; +use clap::ArgMatches; pub const NETWORK_KEY_FILENAME: &str = "key"; /// The filename to store our local metadata. From 7bffa2d8002ca1be13b566d0639659e3aec77a68 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 25 Mar 2025 14:30:22 +0530 Subject: [PATCH 06/10] Update config.rs --- boot_node/src/config.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/boot_node/src/config.rs b/boot_node/src/config.rs index 5be8d4fa3f0..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, &cli_args); + 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") { From cd9df13ba7ab6f61e0b50298f56bf84761bdbe5e Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 25 Mar 2025 14:31:45 +0530 Subject: [PATCH 07/10] Update main.rs --- lighthouse/src/main.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lighthouse/src/main.rs b/lighthouse/src/main.rs index a2432e282df..98e559426b6 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( From 7d26b4e9d49236e87dba23678299b79e6e311a87 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 25 Mar 2025 20:59:34 +0530 Subject: [PATCH 08/10] Update Cargo.toml --- beacon_node/lighthouse_network/Cargo.toml | 1 + 1 file changed, 1 insertion(+) 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" From 8e5168a66cfbcb04b99e298c305b9b9d5dbaf6e4 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 25 Mar 2025 21:00:24 +0530 Subject: [PATCH 09/10] Update mod.rs --- beacon_node/lighthouse_network/src/service/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/beacon_node/lighthouse_network/src/service/mod.rs b/beacon_node/lighthouse_network/src/service/mod.rs index 42490d7a852..3cb8cf3c34c 100644 --- a/beacon_node/lighthouse_network/src/service/mod.rs +++ b/beacon_node/lighthouse_network/src/service/mod.rs @@ -45,7 +45,8 @@ use types::{ use types::{ChainSpec, ForkName}; use utils::{build_transport, strip_peer_id, Context as ServiceContext}; use clap::ArgMatches; -use crate::utils; +use super::utils; +use crate::NetworkConfig; pub mod api_types; mod gossip_cache; From 6f6c619e5d5797e4cd39fd4d179c7f1d700ff0f9 Mon Sep 17 00:00:00 2001 From: Ayush Dubey <61616662+Ayushdubey86@users.noreply.github.com> Date: Tue, 25 Mar 2025 21:09:52 +0530 Subject: [PATCH 10/10] Update utils.rs --- .../lighthouse_network/src/service/utils.rs | 60 +++++-------------- 1 file changed, 14 insertions(+), 46 deletions(-) diff --git a/beacon_node/lighthouse_network/src/service/utils.rs b/beacon_node/lighthouse_network/src/service/utils.rs index 5c5c95393d6..e7397c3c90b 100644 --- a/beacon_node/lighthouse_network/src/service/utils.rs +++ b/beacon_node/lighthouse_network/src/service/utils.rs @@ -21,6 +21,9 @@ 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. @@ -108,63 +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, cli_args: &ArgsMatches) -> Keypair { +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); - } + 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); } } } - // 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"); - } - } - } - } - // 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