Skip to content

Commit 513ae79

Browse files
MegaRedHandLeanSerra
authored andcommitted
feat(l1): embed bootnodes for public networks inside binary (#3967)
**Motivation** We want users to be able to run the binary without needing to have a file of known bootnodes for each public network. **Description** This PR embeds the bootnode files inside the binary. Closes #3966
1 parent 21a9fbe commit 513ae79

File tree

2 files changed

+25
-44
lines changed

2 files changed

+25
-44
lines changed

cmd/ethrex/initializers.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
cli::Options,
3-
networks::{self, Network, PublicNetwork},
3+
networks::Network,
44
utils::{
55
get_client_version, parse_socket_addr, read_jwtsecret_file, read_node_config_file,
66
set_datadir,
@@ -225,25 +225,7 @@ pub fn get_network(opts: &Options) -> Network {
225225
pub fn get_bootnodes(opts: &Options, network: &Network, data_dir: &str) -> Vec<Node> {
226226
let mut bootnodes: Vec<Node> = opts.bootnodes.clone();
227227

228-
match network {
229-
Network::PublicNetwork(PublicNetwork::Holesky) => {
230-
info!("Adding holesky preset bootnodes");
231-
bootnodes.extend(networks::HOLESKY_BOOTNODES.clone());
232-
}
233-
Network::PublicNetwork(PublicNetwork::Hoodi) => {
234-
info!("Addig hoodi preset bootnodes");
235-
bootnodes.extend(networks::HOODI_BOOTNODES.clone());
236-
}
237-
Network::PublicNetwork(PublicNetwork::Mainnet) => {
238-
info!("Adding mainnet preset bootnodes");
239-
bootnodes.extend(networks::MAINNET_BOOTNODES.clone());
240-
}
241-
Network::PublicNetwork(PublicNetwork::Sepolia) => {
242-
info!("Adding sepolia preset bootnodes");
243-
bootnodes.extend(networks::SEPOLIA_BOOTNODES.clone());
244-
}
245-
_ => {}
246-
}
228+
bootnodes.extend(network.get_bootnodes());
247229

248230
if bootnodes.is_empty() {
249231
warn!("No bootnodes specified. This node will not be able to connect to the network.");

cmd/ethrex/networks.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,28 @@ use std::{
55

66
use ethrex_common::types::{Genesis, GenesisError};
77
use ethrex_p2p::types::Node;
8-
use lazy_static::lazy_static;
98

109
pub const HOLESKY_GENESIS_PATH: &str = "cmd/ethrex/networks/holesky/genesis.json";
1110
pub const HOLESKY_GENESIS_CONTENTS: &str = include_str!("networks/holesky/genesis.json");
12-
const HOLESKY_BOOTNODES_PATH: &str = "cmd/ethrex/networks/holesky/bootnodes.json";
11+
const HOLESKY_BOOTNODES: &str = include_str!("networks/holesky/bootnodes.json");
1312

1413
pub const SEPOLIA_GENESIS_PATH: &str = "cmd/ethrex/networks/sepolia/genesis.json";
1514
pub const SEPOLIA_GENESIS_CONTENTS: &str = include_str!("networks/sepolia/genesis.json");
16-
const SEPOLIA_BOOTNODES_PATH: &str = "cmd/ethrex/networks/sepolia/bootnodes.json";
15+
const SEPOLIA_BOOTNODES: &str = include_str!("networks/sepolia/bootnodes.json");
1716

1817
pub const HOODI_GENESIS_PATH: &str = "cmd/ethrex/networks/hoodi/genesis.json";
1918
pub const HOODI_GENESIS_CONTENTS: &str = include_str!("networks/hoodi/genesis.json");
20-
const HOODI_BOOTNODES_PATH: &str = "cmd/ethrex/networks/hoodi/bootnodes.json";
19+
const HOODI_BOOTNODES: &str = include_str!("networks/hoodi/bootnodes.json");
2120

2221
pub const MAINNET_GENESIS_PATH: &str = "cmd/ethrex/networks/mainnet/genesis.json";
2322
pub const MAINNET_GENESIS_CONTENTS: &str = include_str!("networks/mainnet/genesis.json");
24-
const MAINNET_BOOTNODES_PATH: &str = "cmd/ethrex/networks/mainnet/bootnodes.json";
23+
const MAINNET_BOOTNODES: &str = include_str!("networks/mainnet/bootnodes.json");
2524

2625
pub const LOCAL_DEVNET_GENESIS_PATH: &str = "../../fixtures/genesis/l1-dev.json";
2726
pub const LOCAL_DEVNETL2_GENESIS_PATH: &str = "../../fixtures/genesis/l2.json";
2827
pub const LOCAL_DEVNET_GENESIS_CONTENTS: &str = include_str!("../../fixtures/genesis/l1-dev.json");
2928
pub const LOCAL_DEVNETL2_GENESIS_CONTENTS: &str = include_str!("../../fixtures/genesis/l2.json");
3029

31-
lazy_static! {
32-
pub static ref HOLESKY_BOOTNODES: Vec<Node> = serde_json::from_reader(
33-
std::fs::File::open(HOLESKY_BOOTNODES_PATH).expect("Failed to open holesky bootnodes file")
34-
)
35-
.expect("Failed to parse holesky bootnodes file");
36-
pub static ref SEPOLIA_BOOTNODES: Vec<Node> = serde_json::from_reader(
37-
std::fs::File::open(SEPOLIA_BOOTNODES_PATH).expect("Failed to open sepolia bootnodes file")
38-
)
39-
.expect("Failed to parse sepolia bootnodes file");
40-
pub static ref HOODI_BOOTNODES: Vec<Node> = serde_json::from_reader(
41-
std::fs::File::open(HOODI_BOOTNODES_PATH).expect("Failed to open hoodi bootnodes file")
42-
)
43-
.expect("Failed to parse hoodi bootnodes file");
44-
pub static ref MAINNET_BOOTNODES: Vec<Node> = serde_json::from_reader(
45-
std::fs::File::open(MAINNET_BOOTNODES_PATH).expect("Failed to open mainnet bootnodes file")
46-
)
47-
.expect("Failed to parse mainnet bootnodes file");
48-
}
49-
5030
#[derive(Debug, Clone)]
5131
pub enum Network {
5232
PublicNetwork(PublicNetwork),
@@ -129,6 +109,17 @@ impl Network {
129109
Network::GenesisPath(s) => Genesis::try_from(s.as_path()),
130110
}
131111
}
112+
113+
pub fn get_bootnodes(&self) -> Vec<Node> {
114+
let bootnodes = match self {
115+
Network::PublicNetwork(PublicNetwork::Holesky) => HOLESKY_BOOTNODES,
116+
Network::PublicNetwork(PublicNetwork::Hoodi) => HOODI_BOOTNODES,
117+
Network::PublicNetwork(PublicNetwork::Mainnet) => MAINNET_BOOTNODES,
118+
Network::PublicNetwork(PublicNetwork::Sepolia) => SEPOLIA_BOOTNODES,
119+
_ => return vec![],
120+
};
121+
serde_json::from_str(bootnodes).expect("bootnodes file should be valid JSON")
122+
}
132123
}
133124

134125
fn get_genesis_contents(network: PublicNetwork) -> &'static str {
@@ -192,4 +183,12 @@ mod tests {
192183
"d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
193184
);
194185
}
186+
187+
#[test]
188+
fn test_get_bootnodes_works_for_public_networks() {
189+
Network::PublicNetwork(PublicNetwork::Holesky).get_bootnodes();
190+
Network::PublicNetwork(PublicNetwork::Hoodi).get_bootnodes();
191+
Network::PublicNetwork(PublicNetwork::Mainnet).get_bootnodes();
192+
Network::PublicNetwork(PublicNetwork::Sepolia).get_bootnodes();
193+
}
195194
}

0 commit comments

Comments
 (0)