-
Notifications
You must be signed in to change notification settings - Fork 789
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d04036
commit 7b58820
Showing
3 changed files
with
98 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
beacon_node/lighthouse_network/src/peer_manager/network_globals_wrapper.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::{NetworkGlobals, PeerId}; | ||
use std::sync::Arc; | ||
use std::time::Instant; | ||
use types::EthSpec; | ||
|
||
pub trait NetworkGlobalsWrapper { | ||
/// Returns the number of libp2p connected peers with outbound-only connections. | ||
fn connected_outbound_only_peers(&self) -> usize; | ||
|
||
/// Returns the number of libp2p peers that are either connected or being dialed. | ||
fn connected_or_dialing_peers(&self) -> usize; | ||
|
||
/// Update min ttl of a peer. | ||
fn update_min_ttl(&self, peer_id: &PeerId, min_ttl: Instant); | ||
|
||
/// 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; | ||
} | ||
|
||
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> { | ||
/// Returns the number of libp2p connected peers with outbound-only connections. | ||
fn connected_outbound_only_peers(&self) -> usize { | ||
self.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() | ||
} | ||
|
||
/// 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) | ||
} | ||
|
||
/// 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) | ||
} | ||
} |