Skip to content

Commit 2bc9115

Browse files
reuse beacon_node methods for initializing network configs in boot_node (#1520)
## Issue Addressed #1378 ## Proposed Changes Boot node reuses code from beacon_node to initialize network config. This also enables using the network directory to store/load the enr and the private key. ## Additional Info Note that before this PR the port cli arguments were off (the argument was named `enr-port` but used as `boot-node-enr-port`). Therefore as port always the cli port argument was used (for both enr and listening). Now the enr-port argument can be used to overwrite the listening port as the public port others should connect to. Last but not least note, that this restructuring reuses `ethlibp2p::NetworkConfig` that has many more options than the ones used in the boot node. For example the network config has an own `discv5_config` field that gets never used in the boot node and instead another `Discv5Config` gets created later in the boot node process. Co-authored-by: Age Manning <[email protected]>
1 parent 3cfd70d commit 2bc9115

File tree

10 files changed

+274
-252
lines changed

10 files changed

+274
-252
lines changed

Cargo.lock

Lines changed: 37 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

beacon_node/eth2_libp2p/src/discovery/enr.rs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::enr_ext::CombinedKeyExt;
66
use super::ENR_FILENAME;
77
use crate::types::{Enr, EnrBitfield};
88
use crate::NetworkConfig;
9+
use discv5::enr::EnrKey;
910
use libp2p::core::identity::Keypair;
1011
use slog::{debug, warn};
1112
use 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
}

beacon_node/eth2_libp2p/src/discovery/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub(crate) mod enr;
33
pub mod enr_ext;
44

55
// Allow external use of the lighthouse ENR builder
6-
pub use enr::{build_enr, CombinedKey, Eth2Enr};
6+
pub use enr::{build_enr, create_enr_builder_from_config, use_or_load_enr, CombinedKey, Eth2Enr};
77
pub use enr_ext::{CombinedKeyExt, EnrExt};
88
pub use libp2p::core::identity::Keypair;
99

beacon_node/eth2_libp2p/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ pub use metrics::scrape_discovery_metrics;
2626
pub use peer_manager::{
2727
client::Client, score::PeerAction, PeerDB, PeerInfo, PeerSyncStatus, SyncInfo,
2828
};
29-
pub use service::{Libp2pEvent, Service, NETWORK_KEY_FILENAME};
29+
pub use service::{load_private_key, Libp2pEvent, Service, NETWORK_KEY_FILENAME};

beacon_node/eth2_libp2p/src/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ fn keypair_from_bytes(mut bytes: Vec<u8>) -> error::Result<Keypair> {
357357
/// generated and is then saved to disk.
358358
///
359359
/// Currently only secp256k1 keys are allowed, as these are the only keys supported by discv5.
360-
fn load_private_key(config: &NetworkConfig, log: &slog::Logger) -> Keypair {
360+
pub fn load_private_key(config: &NetworkConfig, log: &slog::Logger) -> Keypair {
361361
// check for key from disk
362362
let network_key_f = config.network_dir.join(NETWORK_KEY_FILENAME);
363363
if let Ok(mut network_key_file) = File::open(network_key_f.clone()) {

0 commit comments

Comments
 (0)