Skip to content

Commit

Permalink
Rename
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanjay176 committed Jul 29, 2020
1 parent 7bfe0d5 commit da3447d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 48 deletions.
4 changes: 2 additions & 2 deletions beacon_node/eth2_libp2p/src/behaviour/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::peer_manager::{score::PeerAction, PeerManager, PeerManagerEvent, SubnetDiscovery};
use crate::peer_manager::{score::PeerAction, DiscoverySubnet, PeerManager, PeerManagerEvent};
use crate::rpc::*;
use crate::types::{GossipEncoding, GossipKind, GossipTopic};
use crate::Eth2Enr;
Expand Down Expand Up @@ -302,7 +302,7 @@ impl<TSpec: EthSpec> Behaviour<TSpec> {

/// Attempts to discover new peers for a given subnet. The `min_ttl` gives the time at which we
/// would like to retain the peers for.
pub fn discover_subnet_peers(&mut self, subnet_subscriptions: Vec<SubnetDiscovery>) {
pub fn discover_subnet_peers(&mut self, subnet_subscriptions: Vec<DiscoverySubnet>) {
self.peer_manager
.discover_subnet_peers(subnet_subscriptions)
}
Expand Down
57 changes: 26 additions & 31 deletions beacon_node/eth2_libp2p/src/discovery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub(crate) mod enr;
pub mod enr_ext;

// Allow external use of the lighthouse ENR builder
use crate::peer_manager::SubnetDiscovery;
use crate::peer_manager::DiscoverySubnet;
pub use enr::{build_enr, CombinedKey, Eth2Enr};
pub use enr_ext::{CombinedKeyExt, EnrExt};
pub use libp2p::core::identity::Keypair;
Expand Down Expand Up @@ -65,45 +65,38 @@ pub enum DiscoveryEvent {

#[derive(Debug, Clone, PartialEq)]
struct SubnetQuery {
subnet_id: SubnetId,
min_ttl: Option<Instant>,
discovery_subnet: DiscoverySubnet,
retries: usize,
}

#[derive(Debug, Clone, PartialEq)]
enum QueryType {
/// We are searching for subnet peers.
Subnet(SubnetQuery),
/// We are searching for more peers without ENR or time constraints.
FindPeers,
}

#[derive(Debug, Clone, PartialEq)]
enum GroupedQueryType {
/// We are searching for peers on one of a few subnets.
/// We are searching for peers on >=1 subnets.
Subnet(Vec<SubnetQuery>),
/// We are searching for more peers without ENR or time constraints.
FindPeers,
}

impl QueryType {
/// Returns true if this query has expired.
pub fn expired(&self) -> bool {
/// Removes expired queries
pub fn remove_expired(&mut self) {
match self {
Self::FindPeers => false,
Self::Subnet(subnet_query) => {
if let Some(ttl) = subnet_query.min_ttl {
ttl < Instant::now()
} else {
true
}
Self::FindPeers => return,
Self::Subnet(subnet_queries) => {
subnet_queries.iter_mut().filter(|subnet_query| {
if let Some(ttl) = subnet_query.discovery_subnet.min_ttl {
ttl < Instant::now()
} else {
true
}
});
}
}
}
}

/// The result of a query.
struct QueryResult(GroupedQueryType, Result<Vec<Enr>, discv5::QueryError>);
struct QueryResult(QueryType, Result<Vec<Enr>, discv5::QueryError>);

// Awaiting the event stream future
enum EventStream {
Expand Down Expand Up @@ -257,19 +250,21 @@ impl<TSpec: EthSpec> Discovery<TSpec> {
}

/// Processes a request to search for more peers on a subnet.
pub fn discover_subnet_peers(&mut self, subnets_to_discover: Vec<SubnetDiscovery>) {
pub fn discover_subnet_peers(&mut self, subnets_to_discover: Vec<DiscoverySubnet>) {
// If the discv5 service isn't running, ignore queries
if !self.started {
return;
}
for s in subnets_to_discover {
debug!(
self.log,
"Making discovery query for subnet";
"subnet_id" => format!("{:?}", s)
);
self.add_subnet_query(s.subnet_id, s.min_ttl, 0);
}
subnets_to_discover
.into_iter()
.map(|s| SubnetQuery {
subnet_id: s.subnet_id,
min_ttl: s.min_ttl,
retries: 0,
})
.collect();

self.add_subnet_query(subnets_to_discover);
}

/// Add an ENR to the routing table of the discovery mechanism.
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/eth2_libp2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ pub use libp2p::{core::ConnectedPoint, PeerId, Swarm};
pub use libp2p::{multiaddr, Multiaddr};
pub use metrics::scrape_discovery_metrics;
pub use peer_manager::{
client::Client, score::PeerAction, PeerDB, PeerInfo, PeerSyncStatus, SubnetDiscovery, SyncInfo,
client::Client, score::PeerAction, DiscoverySubnet, PeerDB, PeerInfo, PeerSyncStatus, SyncInfo,
};
pub use service::{Libp2pEvent, Service, NETWORK_KEY_FILENAME};
8 changes: 4 additions & 4 deletions beacon_node/eth2_libp2p/src/peer_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ const DURATION_DIFFERENCE: Duration = Duration::from_millis(1);
/// A subnet to discover peers on along with the instant after which it's no longer useful.
// TODO(pawan): rename to something better
#[derive(Debug, Clone)]
pub struct SubnetDiscovery {
pub struct DiscoverySubnet {
pub subnet_id: SubnetId,
pub min_ttl: Option<Instant>,
}

impl PartialEq for SubnetDiscovery {
fn eq(&self, other: &SubnetDiscovery) -> bool {
impl PartialEq for DiscoverySubnet {
fn eq(&self, other: &DiscoverySubnet) -> bool {
self.subnet_id == other.subnet_id
&& match (self.min_ttl, other.min_ttl) {
(Some(min_ttl_instant), Some(other_min_ttl_instant)) => {
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<TSpec: EthSpec> PeerManager<TSpec> {
}

/// A request to find peers on a given subnet.
pub fn discover_subnet_peers(&mut self, subnets_to_discover: Vec<SubnetDiscovery>) {
pub fn discover_subnet_peers(&mut self, subnets_to_discover: Vec<DiscoverySubnet>) {
// Extend the time to maintain peers if required.
for s in subnets_to_discover.iter() {
if let Some(min_ttl) = s.min_ttl {
Expand Down
12 changes: 6 additions & 6 deletions beacon_node/network/src/attestation_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rand::seq::SliceRandom;
use slog::{crit, debug, error, o, trace, warn};

use beacon_chain::{BeaconChain, BeaconChainTypes};
use eth2_libp2p::{types::GossipKind, NetworkGlobals, SubnetDiscovery};
use eth2_libp2p::{types::GossipKind, DiscoverySubnet, NetworkGlobals};
use hashset_delay::HashSetDelay;
use rest_types::ValidatorSubscription;
use slot_clock::SlotClock;
Expand Down Expand Up @@ -49,8 +49,8 @@ pub enum AttServiceMessage {
EnrAdd(SubnetId),
/// Remove the `SubnetId` from the ENR bitfield.
EnrRemove(SubnetId),
/// Discover peers for a list of `SubnetDiscovery`.
DiscoverPeers(Vec<SubnetDiscovery>),
/// Discover peers for a list of `DiscoverySubnet`.
DiscoverPeers(Vec<DiscoverySubnet>),
}

/// A particular subnet at a given slot.
Expand Down Expand Up @@ -259,7 +259,7 @@ impl<T: BeaconChainTypes> AttestationService<T> {
.now()
.ok_or_else(|| "Could not get the current slot")?;

let discovery_subnets: Vec<SubnetDiscovery> = exact_subnets
let discovery_subnets: Vec<DiscoverySubnet> = exact_subnets
.into_iter()
.filter_map(|exact_subnet| {
// check if there is enough time to perform a discovery lookup
Expand All @@ -273,7 +273,7 @@ impl<T: BeaconChainTypes> AttestationService<T> {
.slot_clock
.duration_to_slot(exact_subnet.slot + 1)
.map(|duration| std::time::Instant::now() + duration);
Some(SubnetDiscovery {
Some(DiscoverySubnet {
subnet_id: exact_subnet.subnet_id,
min_ttl,
})
Expand Down Expand Up @@ -450,7 +450,7 @@ impl<T: BeaconChainTypes> AttestationService<T> {
// TODO(pawan): group these queries in a single request.
// might be too hard on discovery?
self.events
.push_back(AttServiceMessage::DiscoverPeers(vec![SubnetDiscovery {
.push_back(AttServiceMessage::DiscoverPeers(vec![DiscoverySubnet {
subnet_id,
min_ttl: None,
}]));
Expand Down
8 changes: 4 additions & 4 deletions beacon_node/network/src/attestation_service/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod tests {
};
use eth2_libp2p::discovery::{build_enr, Keypair};
use eth2_libp2p::{
discovery::CombinedKey, CombinedKeyExt, NetworkConfig, NetworkGlobals, SubnetDiscovery,
discovery::CombinedKey, CombinedKeyExt, DiscoverySubnet, NetworkConfig, NetworkGlobals,
};
use futures::Stream;
use genesis::{generate_deterministic_keypairs, interop_genesis_state};
Expand Down Expand Up @@ -316,7 +316,7 @@ mod tests {
&attestation_service.beacon_chain.spec,
)
.unwrap();
let expected = vec![AttServiceMessage::DiscoverPeers(vec![SubnetDiscovery {
let expected = vec![AttServiceMessage::DiscoverPeers(vec![DiscoverySubnet {
subnet_id,
min_ttl,
}])];
Expand Down Expand Up @@ -378,7 +378,7 @@ mod tests {
)
.unwrap();
let expected = vec![
AttServiceMessage::DiscoverPeers(vec![SubnetDiscovery { subnet_id, min_ttl }]),
AttServiceMessage::DiscoverPeers(vec![DiscoverySubnet { subnet_id, min_ttl }]),
AttServiceMessage::Subscribe(subnet_id),
];

Expand Down Expand Up @@ -485,7 +485,7 @@ mod tests {

// expect discover peers because we will enter TARGET_PEER_DISCOVERY_SLOT_LOOK_AHEAD range
let expected: Vec<AttServiceMessage> =
vec![AttServiceMessage::DiscoverPeers(vec![SubnetDiscovery {
vec![AttServiceMessage::DiscoverPeers(vec![DiscoverySubnet {
subnet_id,
min_ttl,
}])];
Expand Down

0 comments on commit da3447d

Please sign in to comment.