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

@ -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| {