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

1
Cargo.lock generated
View File

@ -7192,6 +7192,7 @@ dependencies = [
"reth-tasks",
"reth-tracing",
"reth-transaction-pool",
"tempfile",
"tokio",
]

View File

@ -24,7 +24,9 @@ humantime-serde.workspace = true
# crypto
secp256k1 = { workspace = true, features = ["global-context", "rand-std", "recovery"] }
[dev-dependencies]
# toml
confy.workspace = true
[dev-dependencies]
tempfile.workspace = true
toml.workspace = true

View File

@ -6,10 +6,13 @@ use reth_primitives::PruneModes;
use secp256k1::SecretKey;
use serde::{Deserialize, Deserializer, Serialize};
use std::{
ffi::OsStr,
path::{Path, PathBuf},
time::Duration,
};
const EXTENSION: &str = "toml";
/// Configuration for the reth node.
#[derive(Debug, Clone, Default, Deserialize, PartialEq, Eq, Serialize)]
#[serde(default)]
@ -47,6 +50,22 @@ impl Config {
.peer_config(peer_config)
.discovery(discv4)
}
/// Save the configuration to toml file.
pub fn save(&self, path: &Path) -> Result<(), std::io::Error> {
if path.extension() != Some(OsStr::new(EXTENSION)) {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!("reth config file extension must be '{EXTENSION}'"),
));
}
confy::store_path(path, self).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
}
/// Sets the pruning configuration.
pub fn update_prune_confing(&mut self, prune_config: PruneConfig) {
self.prune = Some(prune_config);
}
}
/// Configuration for each stage in the pipeline.
@ -325,11 +344,9 @@ where
#[cfg(test)]
mod tests {
use super::Config;
use super::{Config, EXTENSION};
use std::time::Duration;
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);
@ -347,6 +364,14 @@ mod tests {
})
}
#[test]
fn test_store_config_method() {
with_tempdir("config-store-test-method", |config_path| {
let config = Config::default();
config.save(config_path).expect("Failed to store config");
})
}
#[test]
fn test_load_config() {
with_tempdir("config-load-test", |config_path| {

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);
})
}
}