feat: write pruning config if --full is present (#7938)

This commit is contained in:
Darshan Kathiriya
2024-05-02 05:32:46 -04:00
committed by GitHub
parent 4f002f6ef1
commit bb7f1135d0
5 changed files with 95 additions and 5 deletions

View File

@ -54,3 +54,6 @@ eyre.workspace = true
fdlimit.workspace = true
confy.workspace = true
rayon.workspace = true
[dev-dependencies]
tempfile.workspace = true

View File

@ -22,7 +22,7 @@ use reth_prune::PrunerBuilder;
use reth_rpc::JwtSecret;
use reth_static_file::StaticFileProducer;
use reth_tasks::TaskExecutor;
use reth_tracing::tracing::{error, info};
use reth_tracing::tracing::{error, info, warn};
/// Reusable setup for launching a node.
///
@ -66,6 +66,8 @@ impl LaunchContext {
let mut toml_config = confy::load_path::<reth_config::Config>(&config_path)
.wrap_err_with(|| format!("Could not load config file {config_path:?}"))?;
Self::save_pruning_config_if_full_node(&mut toml_config, config, &config_path)?;
info!(target: "reth::cli", path = ?config_path, "Configuration loaded");
// Update the config with the command line arguments
@ -81,6 +83,24 @@ impl LaunchContext {
Ok(toml_config)
}
/// Save prune config to the toml file if node is a full node.
fn save_pruning_config_if_full_node(
reth_config: &mut reth_config::Config,
config: &NodeConfig,
config_path: impl AsRef<std::path::Path>,
) -> eyre::Result<()> {
if reth_config.prune.is_none() {
if let Some(prune_config) = config.prune_config() {
reth_config.update_prune_confing(prune_config);
info!(target: "reth::cli", "Saving prune config to toml file");
reth_config.save(config_path.as_ref())?;
}
} else if config.prune_config().is_none() {
warn!(target: "reth::cli", "Prune configs present in config file but --full not provided. Running as a Full node");
}
Ok(())
}
/// Convenience function to [Self::configure_globals]
pub fn with_configured_globals(self) -> Self {
self.configure_globals();
@ -456,3 +476,42 @@ pub struct WithConfigs {
/// The loaded reth.toml config.
pub toml_config: reth_config::Config,
}
#[cfg(test)]
mod tests {
use super::{LaunchContext, NodeConfig};
use reth_config::Config;
use reth_node_core::args::PruningArgs;
const EXTENSION: &str = "toml";
fn with_tempdir(filename: &str, proc: fn(&std::path::Path)) {
let temp_dir = tempfile::tempdir().unwrap();
let config_path = temp_dir.path().join(filename).with_extension(EXTENSION);
proc(&config_path);
temp_dir.close().unwrap()
}
#[test]
fn test_save_prune_config() {
with_tempdir("prune-store-test", |config_path| {
let mut reth_config = Config::default();
let node_config =
NodeConfig { pruning: PruningArgs { full: true }, ..NodeConfig::test() };
LaunchContext::save_pruning_config_if_full_node(
&mut reth_config,
&node_config,
config_path,
)
.unwrap();
assert_eq!(
reth_config.prune.as_ref().map(|p| p.block_interval),
node_config.prune_config().map(|p| p.block_interval)
);
let loaded_config: Config = confy::load_path(config_path).unwrap();
assert_eq!(reth_config, loaded_config);
})
}
}