Skip to content

Commit 17ba908

Browse files
dknopikAgeManning
andauthored
Turn ProtocolIdentity into a struct (#285)
Co-authored-by: Age Manning <[email protected]>
1 parent 6ef4928 commit 17ba908

File tree

13 files changed

+240
-188
lines changed

13 files changed

+240
-188
lines changed

src/config.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A set of configuration parameters to tune the discovery protocol.
22
use crate::{
3-
kbucket::MAX_NODES_PER_BUCKET, socket::ListenConfig, Enr, Executor, PermitBanList, RateLimiter,
4-
RateLimiterBuilder,
3+
kbucket::MAX_NODES_PER_BUCKET, socket::ListenConfig, Enr, Executor, PermitBanList,
4+
ProtocolIdentity, RateLimiter, RateLimiterBuilder,
55
};
66
use std::time::Duration;
77

@@ -110,6 +110,9 @@ pub struct Config {
110110

111111
/// Configuration for the sockets to listen on.
112112
pub listen_config: ListenConfig,
113+
114+
/// The protocol identity to use in network messages.
115+
pub protocol_identity: ProtocolIdentity,
113116
}
114117

115118
#[derive(Debug)]
@@ -156,6 +159,7 @@ impl ConfigBuilder {
156159
auto_nat_listen_duration: Some(Duration::from_secs(300)), // 5 minutes
157160
executor: None,
158161
listen_config,
162+
protocol_identity: ProtocolIdentity::default(),
159163
};
160164

161165
ConfigBuilder { config }
@@ -333,6 +337,13 @@ impl ConfigBuilder {
333337
self
334338
}
335339

340+
/// Configures use of a custom protocol identifier to use in network messages. Any packets not
341+
/// matching the configured protocol identity will be ignored.
342+
pub fn protocol_identity(&mut self, protocol_identity: ProtocolIdentity) -> &mut Self {
343+
self.config.protocol_identity = protocol_identity;
344+
self
345+
}
346+
336347
pub fn build(&mut self) -> Config {
337348
// If an executor is not provided, assume a current tokio runtime is running.
338349
if self.config.executor.is_none() {

src/discv5.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ use crate::{
1919
NodeStatus, UpdateResult,
2020
},
2121
node_info::NodeContact,
22-
packet::ProtocolIdentity,
2322
service::{QueryKind, Service, ServiceRequest, TalkRequest},
24-
Config, DefaultProtocolId, Enr, IpMode,
23+
Config, Enr, IpMode,
2524
};
2625
use enr::{CombinedKey, EnrKey, Error as EnrError, NodeId};
2726
use parking_lot::RwLock;
2827
use std::{
2928
future::Future,
30-
marker::PhantomData,
3129
net::SocketAddr,
3230
sync::Arc,
3331
time::{Duration, Instant},
@@ -82,10 +80,7 @@ pub enum Event {
8280

8381
/// The main Discv5 Service struct. This provides the user-level API for performing queries and
8482
/// interacting with the underlying service.
85-
pub struct Discv5<P = DefaultProtocolId>
86-
where
87-
P: ProtocolIdentity,
88-
{
83+
pub struct Discv5 {
8984
config: Config,
9085
/// The channel to make requests from the main service.
9186
service_channel: Option<mpsc::Sender<ServiceRequest>>,
@@ -99,11 +94,9 @@ where
9994
enr_key: Arc<RwLock<CombinedKey>>,
10095
// Type of socket we are using
10196
ip_mode: IpMode,
102-
/// Phantom for the protocol id.
103-
_phantom: PhantomData<P>,
10497
}
10598

106-
impl<P: ProtocolIdentity> Discv5<P> {
99+
impl Discv5 {
107100
pub fn new(
108101
local_enr: Enr,
109102
enr_key: CombinedKey,
@@ -154,7 +147,6 @@ impl<P: ProtocolIdentity> Discv5<P> {
154147
local_enr,
155148
enr_key,
156149
ip_mode,
157-
_phantom: Default::default(),
158150
})
159151
}
160152

@@ -166,7 +158,7 @@ impl<P: ProtocolIdentity> Discv5<P> {
166158
}
167159

168160
// create the main service
169-
let (service_exit, service_channel) = Service::spawn::<P>(
161+
let (service_exit, service_channel) = Service::spawn(
170162
self.local_enr.clone(),
171163
self.enr_key.clone(),
172164
self.kbuckets.clone(),
@@ -739,7 +731,7 @@ impl<P: ProtocolIdentity> Discv5<P> {
739731
}
740732
}
741733

742-
impl<P: ProtocolIdentity> Drop for Discv5<P> {
734+
impl Drop for Discv5 {
743735
fn drop(&mut self) {
744736
self.shutdown();
745737
}

src/handler/crypto/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,8 @@ pub(crate) fn encrypt_message(
220220

221221
#[cfg(test)]
222222
mod tests {
223-
use crate::packet::DefaultProtocolId;
224-
225223
use super::*;
224+
use crate::ProtocolIdentity;
226225
use enr::{CombinedKey, Enr, EnrKey};
227226
use std::convert::TryInto;
228227

@@ -392,9 +391,12 @@ mod tests {
392391
fn decrypt_ref_test_ping() {
393392
let dst_id: NodeId = node_key_2().public().into();
394393
let encoded_ref_packet = hex::decode("00000000000000000000000000000000088b3d4342774649325f313964a39e55ea96c005ad52be8c7560413a7008f16c9e6d2f43bbea8814a546b7409ce783d34c4f53245d08dab84102ed931f66d1492acb308fa1c6715b9d139b81acbdcc").unwrap();
395-
let (_packet, auth_data) =
396-
crate::packet::Packet::decode::<DefaultProtocolId>(&dst_id, &encoded_ref_packet)
397-
.unwrap();
394+
let (_packet, auth_data) = crate::packet::Packet::decode(
395+
&dst_id,
396+
ProtocolIdentity::default(),
397+
&encoded_ref_packet,
398+
)
399+
.unwrap();
398400

399401
let ciphertext = hex::decode("b84102ed931f66d1492acb308fa1c6715b9d139b81acbdcc").unwrap();
400402
let read_key = hex::decode("00000000000000000000000000000000").unwrap();

0 commit comments

Comments
 (0)