Skip to content

Commit

Permalink
simplify to NetworkGlobalsProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Jan 24, 2025
1 parent 7b58820 commit e44528f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 57 deletions.
24 changes: 11 additions & 13 deletions beacon_node/lighthouse_network/src/peer_manager/connectivity.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::peer_manager::network_globals_wrapper::NetworkGlobalsWrapper;
use crate::peer_manager::network_globals_wrapper::NetworkGlobalsProvider;
use crate::EnrExt;
use discv5::Enr;
use std::collections::HashMap;
use std::time::Instant;

pub struct Connectivity<N: NetworkGlobalsWrapper> {
pub struct Connectivity<N: NetworkGlobalsProvider> {
target_peers: usize,
peer_excess_factor: f32,
priority_peer_excess: f32,
Expand All @@ -14,18 +14,18 @@ pub struct Connectivity<N: NetworkGlobalsWrapper> {
discovery_enabled: bool,
/// Peers queued to be dialed.
peers_to_dial: Vec<Enr>,
network_globals_wrapper: N,
network_globals_provider: N,
}

impl<N: NetworkGlobalsWrapper> Connectivity<N> {
impl<N: NetworkGlobalsProvider> Connectivity<N> {
pub fn new(
target_peers: usize,
peer_excess_factor: f32,
priority_peer_excess: f32,
min_outbound_only_factor: f32,
target_outbound_only_factor: f32,
discovery_enabled: bool,
network_globals_connectivity: N,
network_globals_provider: N,
) -> Self {
Self {
target_peers,
Expand All @@ -35,14 +35,14 @@ impl<N: NetworkGlobalsWrapper> Connectivity<N> {
target_outbound_only_factor,
discovery_enabled,
peers_to_dial: Default::default(),
network_globals_wrapper: network_globals_connectivity,
network_globals_provider,
}
}

/// A peer is being dialed.
/// Returns true, if this peer will be dialed.
pub fn dial_peer(&mut self, peer: Enr) -> bool {
if self.network_globals_wrapper.should_dial(&peer.peer_id()) {
if self.network_globals_provider.should_dial(&peer.peer_id()) {
self.peers_to_dial.push(peer);
true
} else {
Expand All @@ -57,7 +57,7 @@ impl<N: NetworkGlobalsWrapper> Connectivity<N> {
pub fn peers_discovered(&mut self, results: &HashMap<Enr, Option<Instant>>) -> usize {
let mut to_dial_peers = 0;
let results_count = results.len();
let connected_or_dialing = self.network_globals_wrapper.connected_or_dialing_peers();
let connected_or_dialing = self.network_globals_provider.connected_or_dialing_peers();
for (enr, min_ttl) in results {
// There are two conditions in deciding whether to dial this peer.
// 1. If we are less than our max connections. Discovery queries are executed to reach
Expand All @@ -76,7 +76,7 @@ impl<N: NetworkGlobalsWrapper> Connectivity<N> {
// dialed
let peer_id = enr.peer_id();
if let Some(min_ttl) = min_ttl {
self.network_globals_wrapper
self.network_globals_provider
.update_min_ttl(&peer_id, *min_ttl);
}
if self.dial_peer(enr.clone()) {
Expand Down Expand Up @@ -111,14 +111,12 @@ impl<N: NetworkGlobalsWrapper> Connectivity<N> {
/// This function checks the status of our current peers and optionally requests a discovery
/// query if we need to find more peers to maintain the current number of peers
pub fn maintain_peer_count(&mut self, dialing_peers: usize) -> usize
where
N: NetworkGlobalsWrapper,
{
// Check if we need to do a discovery lookup
if self.discovery_enabled {
let peer_count = self.network_globals_wrapper.connected_or_dialing_peers();
let peer_count = self.network_globals_provider.connected_or_dialing_peers();
let outbound_only_peer_count =
self.network_globals_wrapper.connected_outbound_only_peers();
self.network_globals_provider.connected_outbound_only_peers();
// return wanted number of peers
if peer_count < self.target_peers.saturating_sub(dialing_peers) {
// We need more peers in general.
Expand Down
5 changes: 2 additions & 3 deletions beacon_node/lighthouse_network/src/peer_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub use libp2p::identity::Keypair;
pub mod peerdb;

use crate::peer_manager::connectivity::Connectivity;
use crate::peer_manager::network_globals_wrapper::LHNetworkGlobalsWrapper;
use crate::peer_manager::peerdb::client::ClientKind;
use libp2p::multiaddr;
pub use peerdb::peer_info::{
Expand Down Expand Up @@ -119,7 +118,7 @@ pub struct PeerManager<E: EthSpec> {
/// The logger associated with the `PeerManager`.
log: slog::Logger,

connectivity: Connectivity<LHNetworkGlobalsWrapper<E>>,
connectivity: Connectivity<Arc<NetworkGlobals<E>>>,
}

/// The events that the `PeerManager` outputs (requests).
Expand Down Expand Up @@ -206,7 +205,7 @@ impl<E: EthSpec> PeerManager<E> {
MIN_OUTBOUND_ONLY_FACTOR,
TARGET_OUTBOUND_ONLY_FACTOR,
discovery_enabled,
LHNetworkGlobalsWrapper::new(network_globals),
network_globals,
),
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use std::time::Instant;
use types::EthSpec;

pub trait NetworkGlobalsWrapper {
pub trait NetworkGlobalsProvider {
/// Returns the number of libp2p connected peers with outbound-only connections.
fn connected_outbound_only_peers(&self) -> usize;

Expand All @@ -18,60 +18,25 @@ pub trait NetworkGlobalsWrapper {
fn should_dial(&self, peer_id: &PeerId) -> bool;
}

pub struct LHNetworkGlobalsWrapper<E: EthSpec> {
/// Storage of network globals to access the `PeerDB`.
network_globals: Arc<NetworkGlobals<E>>,
}

impl<E: EthSpec> LHNetworkGlobalsWrapper<E> {
pub fn new(network_globals: Arc<NetworkGlobals<E>>) -> Self {
Self { network_globals }
}

/// Returns the number of libp2p connected peers with outbound-only connections.
fn connected_outbound_only_peers(&self) -> usize {
self.network_globals.connected_outbound_only_peers()
}

/// Returns the number of libp2p peers that are either connected or being dialed.
fn connected_or_dialing_peers(&self) -> usize {
self.network_globals.connected_or_dialing_peers()
}

/// Update min ttl of a peer.
fn update_min_ttl(&self, peer_id: &PeerId, min_ttl: Instant) {
self.network_globals
.peers
.write()
.update_min_ttl(peer_id, min_ttl);
}

/// Returns true if the peer should be dialed. This checks the connection state and the
/// score state and determines if the peer manager should dial this peer.
fn should_dial(&self, peer_id: &PeerId) -> bool {
self.network_globals.peers.read().should_dial(peer_id)
}
}

impl<E: EthSpec> NetworkGlobalsWrapper for LHNetworkGlobalsWrapper<E> {
impl<E: EthSpec> NetworkGlobalsProvider for Arc<NetworkGlobals<E>> {
/// Returns the number of libp2p connected peers with outbound-only connections.
fn connected_outbound_only_peers(&self) -> usize {
self.connected_outbound_only_peers()
self.as_ref().connected_outbound_only_peers()
}

/// Returns the number of libp2p peers that are either connected or being dialed.
fn connected_or_dialing_peers(&self) -> usize {
self.connected_or_dialing_peers()
self.as_ref().connected_or_dialing_peers()
}

/// Update min ttl of a peer.
fn update_min_ttl(&self, peer_id: &PeerId, min_ttl: Instant) {
self.update_min_ttl(peer_id, min_ttl)
self.as_ref().peers.write().update_min_ttl(peer_id, min_ttl)
}

/// Returns true if the peer should be dialed. This checks the connection state and the
/// score state and determines if the peer manager should dial this peer.
fn should_dial(&self, peer_id: &PeerId) -> bool {
self.should_dial(peer_id)
self.as_ref().peers.read().should_dial(peer_id)
}
}

0 comments on commit e44528f

Please sign in to comment.