@@ -19,13 +19,15 @@ use crate::{
19
19
NodeStatus , UpdateResult ,
20
20
} ,
21
21
node_info:: NodeContact ,
22
+ packet:: ProtocolIdentity ,
22
23
service:: { QueryKind , Service , ServiceRequest , TalkRequest } ,
23
- Discv5Config , Enr ,
24
+ DefaultProtocolId , Discv5Config , Enr ,
24
25
} ;
25
26
use enr:: { CombinedKey , EnrError , EnrKey , NodeId } ;
26
27
use parking_lot:: RwLock ;
27
28
use std:: {
28
29
future:: Future ,
30
+ marker:: PhantomData ,
29
31
net:: SocketAddr ,
30
32
sync:: Arc ,
31
33
time:: { Duration , Instant } ,
@@ -71,7 +73,10 @@ pub enum Discv5Event {
71
73
72
74
/// The main Discv5 Service struct. This provides the user-level API for performing queries and
73
75
/// interacting with the underlying service.
74
- pub struct Discv5 {
76
+ pub struct Discv5 < P = DefaultProtocolId >
77
+ where
78
+ P : ProtocolIdentity ,
79
+ {
75
80
config : Discv5Config ,
76
81
/// The channel to make requests from the main service.
77
82
service_channel : Option < mpsc:: Sender < ServiceRequest > > ,
@@ -83,47 +88,16 @@ pub struct Discv5 {
83
88
local_enr : Arc < RwLock < Enr > > ,
84
89
/// The key associated with the local ENR, required for updating the local ENR.
85
90
enr_key : Arc < RwLock < CombinedKey > > ,
91
+ /// Phantom for the protocol id.
92
+ _phantom : PhantomData < P > ,
86
93
}
87
94
88
- impl Discv5 {
95
+ impl < P : ProtocolIdentity > Discv5 < P > {
89
96
pub fn new (
90
97
local_enr : Enr ,
91
98
enr_key : CombinedKey ,
92
99
mut config : Discv5Config ,
93
100
) -> Result < Self , & ' static str > {
94
- // tests use the default value, so we ignore initializing the protocol.
95
- #[ cfg( not( test) ) ]
96
- {
97
- use crate :: {
98
- config:: { DEFAULT_PROTOCOL_ID , DEFAULT_PROTOCOL_VERSION } ,
99
- packet:: { PROTOCOL_ID , VERSION } ,
100
- } ;
101
- // initialize the protocol id and version
102
- let ( protocol_id_bytes, protocol_version_bytes) = config. protocol ;
103
- PROTOCOL_ID
104
- . set ( protocol_id_bytes)
105
- . map_err ( |_old_val| "PROTOCOL_ID has already been initialized" ) ?;
106
- VERSION
107
- . set ( protocol_version_bytes)
108
- . map_err ( |_old_val| "protocol's VERSION has already been initialized" ) ?;
109
-
110
- if protocol_id_bytes != DEFAULT_PROTOCOL_ID
111
- || protocol_version_bytes != DEFAULT_PROTOCOL_VERSION
112
- {
113
- let protocol_version = u16:: from_be_bytes ( protocol_version_bytes) ;
114
- match std:: str:: from_utf8 ( & protocol_id_bytes) {
115
- Ok ( pretty_protocol_id) => tracing:: info!(
116
- "Discv5 using custom protocol id and version. Id: {} Version: {}" ,
117
- pretty_protocol_id, protocol_version
118
- ) ,
119
- Err ( _) => tracing:: info!(
120
- "Discv5 using custom protocol id and version, with non utf8 protocol id. Id: {:?} Version: {}" ,
121
- protocol_id_bytes, protocol_version
122
- ) ,
123
- }
124
- }
125
- }
126
-
127
101
// ensure the keypair matches the one that signed the enr.
128
102
if local_enr. public_key ( ) != enr_key. public ( ) {
129
103
return Err ( "Provided keypair does not match the provided ENR" ) ;
@@ -166,6 +140,7 @@ impl Discv5 {
166
140
kbuckets,
167
141
local_enr,
168
142
enr_key,
143
+ _phantom : Default :: default ( ) ,
169
144
} )
170
145
}
171
146
@@ -177,7 +152,7 @@ impl Discv5 {
177
152
}
178
153
179
154
// create the main service
180
- let ( service_exit, service_channel) = Service :: spawn (
155
+ let ( service_exit, service_channel) = Service :: spawn :: < P > (
181
156
self . local_enr . clone ( ) ,
182
157
self . enr_key . clone ( ) ,
183
158
self . kbuckets . clone ( ) ,
@@ -638,7 +613,7 @@ impl Discv5 {
638
613
}
639
614
}
640
615
641
- impl Drop for Discv5 {
616
+ impl < P : ProtocolIdentity > Drop for Discv5 < P > {
642
617
fn drop ( & mut self ) {
643
618
self . shutdown ( ) ;
644
619
}
0 commit comments