Skip to content

Commit

Permalink
add ProtocolId to discv5
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomrsantos committed Jan 14, 2025
1 parent 5885332 commit 7bd1908
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions anchor/network/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::time::Instant;
use discv5::enr::{CombinedKey, NodeId};
use discv5::libp2p_identity::{Keypair, PeerId};
use discv5::multiaddr::Multiaddr;
use discv5::{Discv5, Enr};
use discv5::{Discv5, Enr, ProtocolIdentity};
use futures::stream::FuturesUnordered;
use futures::FutureExt;
use futures::{StreamExt, TryFutureExt};
Expand Down Expand Up @@ -111,12 +111,19 @@ enum EventStream {
InActive,
}

pub struct ProtocolId {}

impl ProtocolIdentity for ProtocolId {
const PROTOCOL_ID_BYTES: [u8; 6] = *b"ssvdv5";
const PROTOCOL_VERSION_BYTES: [u8; 2] = 0x0001_u16.to_be_bytes();
}

pub struct Discovery {
/// The handle for the underlying discv5 Server.
///
/// This is behind a Reference counter to allow for futures to be spawned and polled with a
/// static lifetime.
discv5: Discv5,
discv5: Discv5<ProtocolId>,

/// Indicates if we are actively searching for peers. We only allow a single FindPeers query at
/// a time, regardless of the query concurrency.
Expand Down Expand Up @@ -152,7 +159,7 @@ impl Discovery {
let enr_key: CombinedKey = CombinedKey::from_libp2p(local_keypair)?;

let enr = build_enr(&enr_key, network_config).unwrap();
let mut discv5 = Discv5::new(enr, enr_key, discv5_config)
let mut discv5 = Discv5::<ProtocolId>::new(enr, enr_key, discv5_config)
.map_err(|e| format!("Discv5 service failed. Error: {:?}", e))?;

// Add bootnodes to routing table
Expand Down Expand Up @@ -315,18 +322,12 @@ impl Discovery {
// }
// };

// predicate for finding ssv nodes with a valid tcp port
let ssv_node_predicate = move |enr: &Enr| {
if let Some(Ok(is_ssv)) = enr.get_decodable("ssv") {
is_ssv && enr.tcp4().is_some() || enr.tcp6().is_some()
} else {
false
}
};
// predicate for finding nodes with a valid tcp port
let tcp_predicate = move |enr: &Enr| enr.tcp4().is_some() || enr.tcp6().is_some();

// General predicate
let predicate: Box<dyn Fn(&Enr) -> bool + Send> =
Box::new(move |enr: &Enr| ssv_node_predicate(enr) && additional_predicate(enr));
Box::new(move |enr: &Enr| tcp_predicate(enr) && additional_predicate(enr));

// Build the future
let query_future = self
Expand Down Expand Up @@ -355,7 +356,6 @@ impl Discovery {
debug!("Discovery query yielded no results.");
}
Ok(r) => {
debug!(peers_found = r.len(), "Discovery query completed");
let results = r
.into_iter()
.map(|enr| {
Expand All @@ -364,6 +364,7 @@ impl Discovery {
(enr, None)
})
.collect();
debug!(peers = ?results, "Discovery query completed");
return Some(results);
}
Err(e) => {
Expand All @@ -383,8 +384,8 @@ impl Discovery {
// })
}
Ok(r) => {
debug!(peers_found = r.len(), subnets_searched_for = ?subnets_searched_for, "Peer grouped subnet discovery request completed");
let results = r.into_iter().map(|enr| (enr, None)).collect();
debug!(peers = ?results, subnets_searched_for = ?subnets_searched_for, "Peer grouped subnet discovery request completed");

return Some(results);
}
Expand Down

0 comments on commit 7bd1908

Please sign in to comment.