Skip to content

Commit 9be952b

Browse files
authored
PBS: Reload the config file when it changes (#409)
1 parent a0e1b7c commit 9be952b

23 files changed

Lines changed: 390 additions & 58 deletions

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ lazy_static = "1.5.0"
5151
lh_eth2 = { package = "eth2", git = "https://github.com/sigp/lighthouse", tag = "v8.0.0-rc.0" }
5252
lh_eth2_keystore = { package = "eth2_keystore", git = "https://github.com/sigp/lighthouse", tag = "v8.0.0-rc.0" }
5353
lh_types = { package = "types", git = "https://github.com/sigp/lighthouse", tag = "v8.0.0-rc.0" }
54+
notify = "8.2.0"
5455
parking_lot = "0.12.3"
5556
pbkdf2 = "0.12.2"
5657
prometheus = "0.14.0"

bin/pbs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ async fn main() -> Result<()> {
2727

2828
let _args = cb_cli::PbsArgs::parse();
2929

30-
let pbs_config = load_pbs_config().await?;
30+
let (pbs_config, config_path) = load_pbs_config(None).await?;
3131

3232
PbsService::init_metrics(pbs_config.chain)?;
33-
let state = PbsState::new(pbs_config);
33+
let state = PbsState::new(pbs_config, config_path);
3434
let server = PbsService::run::<_, DefaultBuilderApi>(state);
3535

3636
tokio::select! {

crates/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ lazy_static.workspace = true
3030
lh_eth2.workspace = true
3131
lh_eth2_keystore.workspace = true
3232
lh_types.workspace = true
33+
notify.workspace = true
3334
pbkdf2.workspace = true
3435
rand.workspace = true
3536
rayon.workspace = true

crates/common/src/config/log.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct LogsSettings {
1616

1717
impl LogsSettings {
1818
pub fn from_env_config() -> Result<Self> {
19-
let mut config = CommitBoostConfig::from_env_path()?;
19+
let (mut config, _) = CommitBoostConfig::from_env_path()?;
2020

2121
// Override log dir path if env var is set
2222
if let Some(log_dir) = load_optional_env_var(LOGS_DIR_ENV) {

crates/common/src/config/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ impl CommitBoostConfig {
5656
}
5757

5858
pub fn from_file(path: &PathBuf) -> Result<Self> {
59-
let config: Self = load_from_file(path)?;
59+
let (config, _): (Self, _) = load_from_file(path)?;
6060
Ok(config)
6161
}
6262

6363
// When loading the config from the environment, it's important that every path
6464
// is replaced with the correct value if the config is loaded inside a container
65-
pub fn from_env_path() -> Result<Self> {
66-
let helper_config: HelperConfig = load_file_from_env(CONFIG_ENV)?;
65+
pub fn from_env_path() -> Result<(Self, PathBuf)> {
66+
let (helper_config, config_path): (HelperConfig, PathBuf) = load_file_from_env(CONFIG_ENV)?;
6767

6868
let chain = match helper_config.chain {
6969
ChainLoader::Path { path, genesis_time_secs } => {
@@ -109,13 +109,13 @@ impl CommitBoostConfig {
109109
logs: helper_config.logs,
110110
};
111111

112-
Ok(config)
112+
Ok((config, config_path))
113113
}
114114

115115
/// Returns the path to the chain spec file if any
116116
pub fn chain_spec_file(path: &PathBuf) -> Option<PathBuf> {
117117
match load_from_file::<_, ChainConfig>(path) {
118-
Ok(config) => {
118+
Ok((config, _)) => {
119119
if let ChainLoader::Path { path, genesis_time_secs: _ } = config.chain {
120120
Some(path)
121121
} else {

crates/common/src/config/module.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn load_commit_module_config<T: DeserializeOwned>() -> Result<StartCommitMod
8282
}
8383

8484
// load module config including the extra data (if any)
85-
let cb_config: StubConfig<T> = load_file_from_env(CONFIG_ENV)?;
85+
let (cb_config, _): (StubConfig<T>, _) = load_file_from_env(CONFIG_ENV)?;
8686

8787
// find all matching modules config
8888
let matches: Vec<ThisModuleConfig<T>> = cb_config
@@ -148,7 +148,7 @@ pub fn load_builder_module_config<T: DeserializeOwned>() -> eyre::Result<StartBu
148148
}
149149

150150
// load module config including the extra data (if any)
151-
let cb_config: StubConfig<T> = load_file_from_env(CONFIG_ENV)?;
151+
let (cb_config, _): (StubConfig<T>, _) = load_file_from_env(CONFIG_ENV)?;
152152

153153
// find all matching modules config
154154
let matches: Vec<ThisModuleConfig<T>> = cb_config

crates/common/src/config/pbs.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::{
44
collections::HashMap,
55
net::{Ipv4Addr, SocketAddr},
6+
path::PathBuf,
67
sync::Arc,
78
};
89

@@ -242,8 +243,11 @@ fn default_pbs() -> String {
242243
}
243244

244245
/// Loads the default pbs config, i.e. with no signer client or custom data
245-
pub async fn load_pbs_config() -> Result<PbsModuleConfig> {
246-
let config = CommitBoostConfig::from_env_path()?;
246+
pub async fn load_pbs_config(config_path: Option<PathBuf>) -> Result<(PbsModuleConfig, PathBuf)> {
247+
let (config, config_path) = match config_path {
248+
Some(path) => (CommitBoostConfig::from_file(&path)?, path),
249+
None => CommitBoostConfig::from_env_path()?,
250+
};
247251
config.validate().await?;
248252

249253
// Make sure relays isn't empty - since the config is still technically valid if
@@ -295,16 +299,19 @@ pub async fn load_pbs_config() -> Result<PbsModuleConfig> {
295299

296300
let all_relays = all_relays.into_values().collect();
297301

298-
Ok(PbsModuleConfig {
299-
chain: config.chain,
300-
endpoint,
301-
pbs_config: Arc::new(config.pbs.pbs_config),
302-
relays: relay_clients,
303-
all_relays,
304-
signer_client: None,
305-
registry_muxes,
306-
mux_lookup,
307-
})
302+
Ok((
303+
PbsModuleConfig {
304+
chain: config.chain,
305+
endpoint,
306+
pbs_config: Arc::new(config.pbs.pbs_config),
307+
relays: relay_clients,
308+
all_relays,
309+
signer_client: None,
310+
registry_muxes,
311+
mux_lookup,
312+
},
313+
config_path,
314+
))
308315
}
309316

310317
/// Loads a custom pbs config, i.e. with signer client and/or custom data
@@ -326,7 +333,7 @@ pub async fn load_pbs_custom_config<T: DeserializeOwned>() -> Result<(PbsModuleC
326333
}
327334

328335
// load module config including the extra data (if any)
329-
let cb_config: StubConfig<T> = load_file_from_env(CONFIG_ENV)?;
336+
let (cb_config, _): (StubConfig<T>, _) = load_file_from_env(CONFIG_ENV)?;
330337
cb_config.pbs.static_config.pbs_config.validate(cb_config.chain).await?;
331338

332339
// use endpoint from env if set, otherwise use default host and port

crates/common/src/config/signer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub struct StartSignerConfig {
142142

143143
impl StartSignerConfig {
144144
pub fn load_from_env() -> Result<Self> {
145-
let config = CommitBoostConfig::from_env_path()?;
145+
let (config, _) = CommitBoostConfig::from_env_path()?;
146146

147147
let jwts = load_jwt_secrets()?;
148148

crates/common/src/config/utils.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::{collections::HashMap, path::Path};
1+
use std::{
2+
collections::HashMap,
3+
path::{Path, PathBuf},
4+
};
25

36
use eyre::{Context, Result, bail};
47
use serde::de::DeserializeOwned;
@@ -17,13 +20,18 @@ pub fn load_optional_env_var(env: &str) -> Option<String> {
1720
std::env::var(env).ok()
1821
}
1922

20-
pub fn load_from_file<P: AsRef<Path> + std::fmt::Debug, T: DeserializeOwned>(path: P) -> Result<T> {
23+
pub fn load_from_file<P: AsRef<Path> + std::fmt::Debug, T: DeserializeOwned>(
24+
path: P,
25+
) -> Result<(T, PathBuf)> {
2126
let config_file = std::fs::read_to_string(path.as_ref())
2227
.wrap_err(format!("Unable to find config file: {path:?}"))?;
23-
toml::from_str(&config_file).wrap_err("could not deserialize toml from string")
28+
match toml::from_str(&config_file).wrap_err("could not deserialize toml from string") {
29+
Ok(config) => Ok((config, path.as_ref().to_path_buf())),
30+
Err(e) => Err(e),
31+
}
2432
}
2533

26-
pub fn load_file_from_env<T: DeserializeOwned>(env: &str) -> Result<T> {
34+
pub fn load_file_from_env<T: DeserializeOwned>(env: &str) -> Result<(T, PathBuf)> {
2735
let path = std::env::var(env).wrap_err(format!("{env} is not set"))?;
2836
load_from_file(&path)
2937
}

0 commit comments

Comments
 (0)