@@ -6,6 +6,7 @@ use super::enr_ext::CombinedKeyExt;
66use super :: ENR_FILENAME ;
77use crate :: types:: { Enr , EnrBitfield } ;
88use crate :: NetworkConfig ;
9+ use discv5:: enr:: EnrKey ;
910use libp2p:: core:: identity:: Keypair ;
1011use slog:: { debug, warn} ;
1112use ssz:: { Decode , Encode } ;
@@ -48,23 +49,16 @@ impl Eth2Enr for Enr {
4849 }
4950}
5051
51- /// Loads an ENR from file if it exists and matches the current NodeId and sequence number. If none
52- /// exists, generates a new one.
53- ///
52+ /// Either use the given ENR or load an ENR from file if it exists and matches the current NodeId
53+ /// and sequence number.
5454/// If an ENR exists, with the same NodeId, this function checks to see if the loaded ENR from
55- /// disk is suitable to use, otherwise we increment our newly generated ENR's sequence number.
56- pub fn build_or_load_enr < T : EthSpec > (
57- local_key : Keypair ,
55+ /// disk is suitable to use, otherwise we increment the given ENR's sequence number.
56+ pub fn use_or_load_enr (
57+ enr_key : & CombinedKey ,
58+ local_enr : & mut Enr ,
5859 config : & NetworkConfig ,
59- enr_fork_id : EnrForkId ,
6060 log : & slog:: Logger ,
61- ) -> Result < Enr , String > {
62- // Build the local ENR.
63- // Note: Discovery should update the ENR record's IP to the external IP as seen by the
64- // majority of our peers, if the CLI doesn't expressly forbid it.
65- let enr_key = CombinedKey :: from_libp2p ( & local_key) ?;
66- let mut local_enr = build_enr :: < T > ( & enr_key, config, enr_fork_id) ?;
67-
61+ ) -> Result < ( ) , String > {
6862 let enr_f = config. network_dir . join ( ENR_FILENAME ) ;
6963 if let Ok ( mut enr_file) = File :: open ( enr_f. clone ( ) ) {
7064 let mut enr_string = String :: new ( ) ;
@@ -78,14 +72,15 @@ pub fn build_or_load_enr<T: EthSpec>(
7872 if compare_enr ( & local_enr, & disk_enr) {
7973 debug ! ( log, "ENR loaded from disk" ; "file" => format!( "{:?}" , enr_f) ) ;
8074 // the stored ENR has the same configuration, use it
81- return Ok ( disk_enr) ;
75+ * local_enr = disk_enr;
76+ return Ok ( ( ) ) ;
8277 }
8378
8479 // same node id, different configuration - update the sequence number
8580 // Note: local_enr is generated with default(0) attnets value,
8681 // so a non default value in persisted enr will also update sequence number.
8782 let new_seq_no = disk_enr. seq ( ) . checked_add ( 1 ) . ok_or_else ( || "ENR sequence number on file is too large. Remove it to generate a new NodeId" ) ?;
88- local_enr. set_seq ( new_seq_no, & enr_key) . map_err ( |e| {
83+ local_enr. set_seq ( new_seq_no, enr_key) . map_err ( |e| {
8984 format ! ( "Could not update ENR sequence number: {:?}" , e)
9085 } ) ?;
9186 debug ! ( log, "ENR sequence number increased" ; "seq" => new_seq_no) ;
@@ -101,15 +96,31 @@ pub fn build_or_load_enr<T: EthSpec>(
10196
10297 save_enr_to_disk ( & config. network_dir , & local_enr, log) ;
10398
104- Ok ( local_enr )
99+ Ok ( ( ) )
105100}
106101
107- /// Builds a lighthouse ENR given a `NetworkConfig`.
108- pub fn build_enr < T : EthSpec > (
109- enr_key : & CombinedKey ,
102+ /// Loads an ENR from file if it exists and matches the current NodeId and sequence number. If none
103+ /// exists, generates a new one.
104+ ///
105+ /// If an ENR exists, with the same NodeId, this function checks to see if the loaded ENR from
106+ /// disk is suitable to use, otherwise we increment our newly generated ENR's sequence number.
107+ pub fn build_or_load_enr < T : EthSpec > (
108+ local_key : Keypair ,
110109 config : & NetworkConfig ,
111110 enr_fork_id : EnrForkId ,
111+ log : & slog:: Logger ,
112112) -> Result < Enr , String > {
113+ // Build the local ENR.
114+ // Note: Discovery should update the ENR record's IP to the external IP as seen by the
115+ // majority of our peers, if the CLI doesn't expressly forbid it.
116+ let enr_key = CombinedKey :: from_libp2p ( & local_key) ?;
117+ let mut local_enr = build_enr :: < T > ( & enr_key, config, enr_fork_id) ?;
118+
119+ use_or_load_enr ( & enr_key, & mut local_enr, config, log) ?;
120+ Ok ( local_enr)
121+ }
122+
123+ pub fn create_enr_builder_from_config < T : EnrKey > ( config : & NetworkConfig ) -> EnrBuilder < T > {
113124 let mut builder = EnrBuilder :: new ( "v4" ) ;
114125 if let Some ( enr_address) = config. enr_address {
115126 builder. ip ( enr_address) ;
@@ -120,7 +131,17 @@ pub fn build_enr<T: EthSpec>(
120131 // we always give it our listening tcp port
121132 // TODO: Add uPnP support to map udp and tcp ports
122133 let tcp_port = config. enr_tcp_port . unwrap_or_else ( || config. libp2p_port ) ;
123- builder. tcp ( tcp_port) ;
134+ builder. tcp ( tcp_port) . tcp ( config. libp2p_port ) ;
135+ builder
136+ }
137+
138+ /// Builds a lighthouse ENR given a `NetworkConfig`.
139+ pub fn build_enr < T : EthSpec > (
140+ enr_key : & CombinedKey ,
141+ config : & NetworkConfig ,
142+ enr_fork_id : EnrForkId ,
143+ ) -> Result < Enr , String > {
144+ let mut builder = create_enr_builder_from_config ( config) ;
124145
125146 // set the `eth2` field on our ENR
126147 builder. add_value ( ETH2_ENR_KEY . into ( ) , enr_fork_id. as_ssz_bytes ( ) ) ;
@@ -131,7 +152,6 @@ pub fn build_enr<T: EthSpec>(
131152 builder. add_value ( BITFIELD_ENR_KEY . into ( ) , bitfield. as_ssz_bytes ( ) ) ;
132153
133154 builder
134- . tcp ( config. libp2p_port )
135155 . build ( enr_key)
136156 . map_err ( |e| format ! ( "Could not build Local ENR: {:?}" , e) )
137157}
0 commit comments