1
1
//! Helper functions and an extension trait for Ethereum 2 ENRs.
2
2
3
- pub use discv5:: enr:: { CombinedKey , EnrBuilder } ;
3
+ pub use discv5:: enr:: CombinedKey ;
4
4
5
5
use super :: enr_ext:: CombinedKeyExt ;
6
6
use super :: ENR_FILENAME ;
7
7
use crate :: types:: { Enr , EnrAttestationBitfield , EnrSyncCommitteeBitfield } ;
8
8
use crate :: NetworkConfig ;
9
- use discv5:: enr:: EnrKey ;
10
9
use libp2p:: identity:: Keypair ;
11
10
use slog:: { debug, warn} ;
12
11
use ssz:: { Decode , Encode } ;
@@ -142,11 +141,13 @@ pub fn build_or_load_enr<T: EthSpec>(
142
141
Ok ( local_enr)
143
142
}
144
143
145
- pub fn create_enr_builder_from_config < T : EnrKey > (
144
+ /// Builds a lighthouse ENR given a `NetworkConfig`.
145
+ pub fn build_enr < T : EthSpec > (
146
+ enr_key : & CombinedKey ,
146
147
config : & NetworkConfig ,
147
- enable_libp2p : bool ,
148
- ) -> EnrBuilder < T > {
149
- let mut builder = EnrBuilder :: new ( "v4" ) ;
148
+ enr_fork_id : & EnrForkId ,
149
+ ) -> Result < Enr , String > {
150
+ let mut builder = discv5 :: enr :: Enr :: builder ( ) ;
150
151
let ( maybe_ipv4_address, maybe_ipv6_address) = & config. enr_address ;
151
152
152
153
if let Some ( ip) = maybe_ipv4_address {
@@ -165,63 +166,51 @@ pub fn create_enr_builder_from_config<T: EnrKey>(
165
166
builder. udp6 ( udp6_port. get ( ) ) ;
166
167
}
167
168
168
- if enable_libp2p {
169
- // Add QUIC fields to the ENR.
170
- // Since QUIC is used as an alternative transport for the libp2p protocols,
171
- // the related fields should only be added when both QUIC and libp2p are enabled
172
- if !config. disable_quic_support {
173
- // If we are listening on ipv4, add the quic ipv4 port.
174
- if let Some ( quic4_port) = config. enr_quic4_port . or_else ( || {
175
- config
176
- . listen_addrs ( )
177
- . v4 ( )
178
- . and_then ( |v4_addr| v4_addr. quic_port . try_into ( ) . ok ( ) )
179
- } ) {
180
- builder. add_value ( QUIC_ENR_KEY , & quic4_port. get ( ) ) ;
181
- }
182
-
183
- // If we are listening on ipv6, add the quic ipv6 port.
184
- if let Some ( quic6_port) = config. enr_quic6_port . or_else ( || {
185
- config
186
- . listen_addrs ( )
187
- . v6 ( )
188
- . and_then ( |v6_addr| v6_addr. quic_port . try_into ( ) . ok ( ) )
189
- } ) {
190
- builder. add_value ( QUIC6_ENR_KEY , & quic6_port. get ( ) ) ;
191
- }
192
- }
193
-
194
- // If the ENR port is not set, and we are listening over that ip version, use the listening port instead.
195
- let tcp4_port = config. enr_tcp4_port . or_else ( || {
169
+ // Add QUIC fields to the ENR.
170
+ // Since QUIC is used as an alternative transport for the libp2p protocols,
171
+ // the related fields should only be added when both QUIC and libp2p are enabled
172
+ if !config. disable_quic_support {
173
+ // If we are listening on ipv4, add the quic ipv4 port.
174
+ if let Some ( quic4_port) = config. enr_quic4_port . or_else ( || {
196
175
config
197
176
. listen_addrs ( )
198
177
. v4 ( )
199
- . and_then ( |v4_addr| v4_addr. tcp_port . try_into ( ) . ok ( ) )
200
- } ) ;
201
- if let Some ( tcp4_port) = tcp4_port {
202
- builder. tcp4 ( tcp4_port. get ( ) ) ;
178
+ . and_then ( |v4_addr| v4_addr. quic_port . try_into ( ) . ok ( ) )
179
+ } ) {
180
+ builder. add_value ( QUIC_ENR_KEY , & quic4_port. get ( ) ) ;
203
181
}
204
182
205
- let tcp6_port = config. enr_tcp6_port . or_else ( || {
183
+ // If we are listening on ipv6, add the quic ipv6 port.
184
+ if let Some ( quic6_port) = config. enr_quic6_port . or_else ( || {
206
185
config
207
186
. listen_addrs ( )
208
187
. v6 ( )
209
- . and_then ( |v6_addr| v6_addr. tcp_port . try_into ( ) . ok ( ) )
210
- } ) ;
211
- if let Some ( tcp6_port) = tcp6_port {
212
- builder. tcp6 ( tcp6_port. get ( ) ) ;
188
+ . and_then ( |v6_addr| v6_addr. quic_port . try_into ( ) . ok ( ) )
189
+ } ) {
190
+ builder. add_value ( QUIC6_ENR_KEY , & quic6_port. get ( ) ) ;
213
191
}
214
192
}
215
- builder
216
- }
217
193
218
- /// Builds a lighthouse ENR given a `NetworkConfig`.
219
- pub fn build_enr < T : EthSpec > (
220
- enr_key : & CombinedKey ,
221
- config : & NetworkConfig ,
222
- enr_fork_id : & EnrForkId ,
223
- ) -> Result < Enr , String > {
224
- let mut builder = create_enr_builder_from_config ( config, true ) ;
194
+ // If the ENR port is not set, and we are listening over that ip version, use the listening port instead.
195
+ let tcp4_port = config. enr_tcp4_port . or_else ( || {
196
+ config
197
+ . listen_addrs ( )
198
+ . v4 ( )
199
+ . and_then ( |v4_addr| v4_addr. tcp_port . try_into ( ) . ok ( ) )
200
+ } ) ;
201
+ if let Some ( tcp4_port) = tcp4_port {
202
+ builder. tcp4 ( tcp4_port. get ( ) ) ;
203
+ }
204
+
205
+ let tcp6_port = config. enr_tcp6_port . or_else ( || {
206
+ config
207
+ . listen_addrs ( )
208
+ . v6 ( )
209
+ . and_then ( |v6_addr| v6_addr. tcp_port . try_into ( ) . ok ( ) )
210
+ } ) ;
211
+ if let Some ( tcp6_port) = tcp6_port {
212
+ builder. tcp6 ( tcp6_port. get ( ) ) ;
213
+ }
225
214
226
215
// set the `eth2` field on our ENR
227
216
builder. add_value ( ETH2_ENR_KEY , & enr_fork_id. as_ssz_bytes ( ) ) ;
0 commit comments