Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Made Enr generic with EnrKey in impl #272

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/discv5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
service::{QueryKind, Service, ServiceRequest, TalkRequest},
Config, DefaultProtocolId, Enr, IpMode,
};
use enr::{CombinedKey, EnrKey, Error as EnrError, NodeId};
use enr::{CombinedKey, CombinedPublicKey ,EnrKey, Error as EnrError, NodeId};
use parking_lot::RwLock;
use std::{
future::Future,
Expand Down Expand Up @@ -82,9 +82,10 @@ pub enum Event {

/// The main Discv5 Service struct. This provides the user-level API for performing queries and
/// interacting with the underlying service.
pub struct Discv5<P = DefaultProtocolId>
pub struct Discv5<P = DefaultProtocolId, K = CombinedKey>
where
P: ProtocolIdentity,
K: EnrKey<PublicKey = CombinedPublicKey>,
{
config: Config,
/// The channel to make requests from the main service.
Expand All @@ -96,17 +97,20 @@ where
/// The local ENR of the server.
local_enr: Arc<RwLock<Enr>>,
/// The key associated with the local ENR, required for updating the local ENR.
enr_key: Arc<RwLock<CombinedKey>>,
enr_key: Arc<RwLock<K>>,
// Type of socket we are using
ip_mode: IpMode,
/// Phantom for the protocol id.
_phantom: PhantomData<P>,
}

impl<P: ProtocolIdentity> Discv5<P> {
impl<P, K> Discv5<P, K>
where
P: ProtocolIdentity,
K: EnrKey<PublicKey = CombinedPublicKey>,
{
pub fn new(
local_enr: Enr,
enr_key: CombinedKey,
enr_key: K,
mut config: Config,
) -> Result<Self, &'static str> {
// ensure the keypair matches the one that signed the enr.
Expand Down Expand Up @@ -737,7 +741,7 @@ impl<P: ProtocolIdentity> Discv5<P> {
}
}

impl<P: ProtocolIdentity> Drop for Discv5<P> {
impl<P: ProtocolIdentity, K:EnrKey<PublicKey = CombinedPublicKey>> Drop for Discv5<P,K> {
fn drop(&mut self) {
self.shutdown();
}
Expand Down
5 changes: 3 additions & 2 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use self::{
ip_vote::IpVote,
query_info::{QueryInfo, QueryType},
};
use enr::EnrKey;
use crate::{
error::{RequestError, ResponseError},
handler::{Handler, HandlerIn, HandlerOut},
Expand Down Expand Up @@ -261,15 +262,15 @@ impl Default for NodesResponse {
}
}

impl Service {
impl<K> Service {
/// Builds the `Service` main struct.
///
/// `local_enr` is the `ENR` representing the local node. This contains node identifying information, such
/// as IP addresses and ports which we wish to broadcast to other nodes via this discovery
/// mechanism.
pub async fn spawn<P: ProtocolIdentity>(
local_enr: Arc<RwLock<Enr>>,
enr_key: Arc<RwLock<CombinedKey>>,
enr_key: Arc<RwLock<K>>,
kbuckets: Arc<RwLock<KBucketsTable<NodeId, Enr>>>,
config: Config,
) -> Result<(oneshot::Sender<()>, mpsc::Sender<ServiceRequest>), std::io::Error> {
Expand Down
Loading