From df468ced340b73031164c839eb4cae4563bddd08 Mon Sep 17 00:00:00 2001 From: Jonas Bostoen Date: Thu, 29 Feb 2024 10:44:46 +0100 Subject: [PATCH 1/3] feat(beacon_node): add explicit peers cli + config --- beacon_node/lighthouse_network/src/config.rs | 4 ++++ beacon_node/src/cli.rs | 8 ++++++++ beacon_node/src/config.rs | 14 ++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/beacon_node/lighthouse_network/src/config.rs b/beacon_node/lighthouse_network/src/config.rs index 5b13730f971..3d3e0a53120 100644 --- a/beacon_node/lighthouse_network/src/config.rs +++ b/beacon_node/lighthouse_network/src/config.rs @@ -104,6 +104,9 @@ pub struct Config { /// List of trusted libp2p nodes which are not scored. pub trusted_peers: Vec, + /// List of explicit peers which are not scored. https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md#explicit-peering-agreements + pub explicit_peers: Vec, + /// Disables peer scoring altogether. pub disable_peer_scoring: bool, @@ -357,6 +360,7 @@ impl Default for Config { boot_nodes_multiaddr: vec![], libp2p_nodes: vec![], trusted_peers: vec![], + explicit_peers: vec![], disable_peer_scoring: false, client_version: lighthouse_version::version_with_platform(), disable_discovery: false, diff --git a/beacon_node/src/cli.rs b/beacon_node/src/cli.rs index 1a8e0194f6e..d8229f3e41b 100644 --- a/beacon_node/src/cli.rs +++ b/beacon_node/src/cli.rs @@ -292,6 +292,14 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> { .help("One or more comma-delimited trusted peer ids which always have the highest score according to the peer scoring system.") .takes_value(true), ) + .arg( + Arg::with_name("explicit-peers") + .long("explicit-peers") + .value_name("EXPLICIT_PEERS") + .help("One or more comma-delimited explicit peer ids which are exempt from any peer scoring and other defensive measures. \ + Explicit peering agreements are established out of band and reciprocal.") + .takes_value(true), + ) .arg( Arg::with_name("genesis-backfill") .long("genesis-backfill") diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index ba8430aceae..707b0105926 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -1191,6 +1191,20 @@ pub fn set_network_config( } } + if let Some(explicit_peers_str) = cli_args.value_of("explicit-peers") { + config.explicit_peers = explicit_peers_str + .split(',') + .map(|peer_id| { + peer_id + .parse() + .map_err(|_| format!("Invalid explicit peer id: {}", peer_id)) + }) + .collect::, _>>()?; + if config.explicit_peers.len() >= config.target_peers { + slog::warn!(log, "More explicit peers than the target peer limit. This will prevent efficient peer selection criteria."; "target_peers" => config.target_peers, "explicit_peers" => config.explicit_peers.len()); + } + } + if let Some(enr_udp_port_str) = cli_args.value_of("enr-udp-port") { config.enr_udp4_port = Some( enr_udp_port_str From a2b5de40423ff9cba7c64e395d0d45fcc92103e4 Mon Sep 17 00:00:00 2001 From: Jonas Bostoen Date: Thu, 29 Feb 2024 11:12:10 +0100 Subject: [PATCH 2/3] feat(beacon_node): add explicit peers to GossipSub, mark as trusted --- .../lighthouse_network/src/service/mod.rs | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/beacon_node/lighthouse_network/src/service/mod.rs b/beacon_node/lighthouse_network/src/service/mod.rs index 3717c497386..f88944a8f3e 100644 --- a/beacon_node/lighthouse_network/src/service/mod.rs +++ b/beacon_node/lighthouse_network/src/service/mod.rs @@ -144,6 +144,21 @@ impl Network { // initialise the node's ID let local_keypair = utils::load_private_key(&config, &log); + let mut trusted_peers: Vec = config + .trusted_peers + .iter() + .map(|x| PeerId::from(x.clone())) + .collect(); + + // Explicit peers should also be marked as trusted since they are exempt from peer scoring. + // Cfr. https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md#explicit-peering-agreements + trusted_peers.extend( + config + .explicit_peers + .iter() + .map(|x| PeerId::from(x.clone())), + ); + // set up a collection of variables accessible outside of the network crate let network_globals = { // Create an ENR or load from disk if appropriate @@ -158,11 +173,7 @@ impl Network { let globals = NetworkGlobals::new( enr, meta_data, - config - .trusted_peers - .iter() - .map(|x| PeerId::from(x.clone())) - .collect(), + trusted_peers, config.disable_peer_scoring, &log, ); @@ -275,6 +286,11 @@ impl Network { .with_peer_score(params, thresholds) .expect("Valid score params and thresholds"); + // Add explicit peers to GossipSub + for explicit_peer in config.explicit_peers.iter() { + gossipsub.add_explicit_peer(&PeerId::from(explicit_peer.clone())); + } + (gossipsub, update_gossipsub_scores) }; From 5f36d7ffa2b0fa303ba31b111092f904a305de4c Mon Sep 17 00:00:00 2001 From: Jonas Bostoen Date: Tue, 5 Mar 2024 09:04:23 +0100 Subject: [PATCH 3/3] refactor: remove explicit-peers flag, mark trusted peers as explicit instead --- beacon_node/lighthouse_network/src/config.rs | 6 +----- .../lighthouse_network/src/service/mod.rs | 17 +++++------------ beacon_node/src/cli.rs | 8 -------- beacon_node/src/config.rs | 14 -------------- 4 files changed, 6 insertions(+), 39 deletions(-) diff --git a/beacon_node/lighthouse_network/src/config.rs b/beacon_node/lighthouse_network/src/config.rs index 3d3e0a53120..02134580e0f 100644 --- a/beacon_node/lighthouse_network/src/config.rs +++ b/beacon_node/lighthouse_network/src/config.rs @@ -101,12 +101,9 @@ pub struct Config { /// List of libp2p nodes to initially connect to. pub libp2p_nodes: Vec, - /// List of trusted libp2p nodes which are not scored. + /// List of trusted libp2p nodes which are not scored and marked as explicit. pub trusted_peers: Vec, - /// List of explicit peers which are not scored. https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md#explicit-peering-agreements - pub explicit_peers: Vec, - /// Disables peer scoring altogether. pub disable_peer_scoring: bool, @@ -360,7 +357,6 @@ impl Default for Config { boot_nodes_multiaddr: vec![], libp2p_nodes: vec![], trusted_peers: vec![], - explicit_peers: vec![], disable_peer_scoring: false, client_version: lighthouse_version::version_with_platform(), disable_discovery: false, diff --git a/beacon_node/lighthouse_network/src/service/mod.rs b/beacon_node/lighthouse_network/src/service/mod.rs index f88944a8f3e..62633e2ad13 100644 --- a/beacon_node/lighthouse_network/src/service/mod.rs +++ b/beacon_node/lighthouse_network/src/service/mod.rs @@ -144,21 +144,14 @@ impl Network { // initialise the node's ID let local_keypair = utils::load_private_key(&config, &log); - let mut trusted_peers: Vec = config + // 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 .trusted_peers .iter() .map(|x| PeerId::from(x.clone())) .collect(); - // Explicit peers should also be marked as trusted since they are exempt from peer scoring. - // Cfr. https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md#explicit-peering-agreements - trusted_peers.extend( - config - .explicit_peers - .iter() - .map(|x| PeerId::from(x.clone())), - ); - // set up a collection of variables accessible outside of the network crate let network_globals = { // Create an ENR or load from disk if appropriate @@ -286,8 +279,8 @@ impl Network { .with_peer_score(params, thresholds) .expect("Valid score params and thresholds"); - // Add explicit peers to GossipSub - for explicit_peer in config.explicit_peers.iter() { + // Mark trusted peers as explicit. + for explicit_peer in config.trusted_peers.iter() { gossipsub.add_explicit_peer(&PeerId::from(explicit_peer.clone())); } diff --git a/beacon_node/src/cli.rs b/beacon_node/src/cli.rs index d8229f3e41b..1a8e0194f6e 100644 --- a/beacon_node/src/cli.rs +++ b/beacon_node/src/cli.rs @@ -292,14 +292,6 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> { .help("One or more comma-delimited trusted peer ids which always have the highest score according to the peer scoring system.") .takes_value(true), ) - .arg( - Arg::with_name("explicit-peers") - .long("explicit-peers") - .value_name("EXPLICIT_PEERS") - .help("One or more comma-delimited explicit peer ids which are exempt from any peer scoring and other defensive measures. \ - Explicit peering agreements are established out of band and reciprocal.") - .takes_value(true), - ) .arg( Arg::with_name("genesis-backfill") .long("genesis-backfill") diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index 707b0105926..ba8430aceae 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -1191,20 +1191,6 @@ pub fn set_network_config( } } - if let Some(explicit_peers_str) = cli_args.value_of("explicit-peers") { - config.explicit_peers = explicit_peers_str - .split(',') - .map(|peer_id| { - peer_id - .parse() - .map_err(|_| format!("Invalid explicit peer id: {}", peer_id)) - }) - .collect::, _>>()?; - if config.explicit_peers.len() >= config.target_peers { - slog::warn!(log, "More explicit peers than the target peer limit. This will prevent efficient peer selection criteria."; "target_peers" => config.target_peers, "explicit_peers" => config.explicit_peers.len()); - } - } - if let Some(enr_udp_port_str) = cli_args.value_of("enr-udp-port") { config.enr_udp4_port = Some( enr_udp_port_str