review comments

This commit is contained in:
Boog900 2024-11-05 01:14:36 +00:00
parent 06b7429f9e
commit 26653d428d
No known key found for this signature in database
GPG key ID: 42AB1287CB0041C2
10 changed files with 34 additions and 25 deletions

View file

@ -10,7 +10,10 @@ use clap::Parser;
use serde::{Deserialize, Serialize};
use cuprate_consensus::ContextConfig;
use cuprate_helper::{fs::CUPRATE_CONFIG_DIR, network::Network};
use cuprate_helper::{
fs::{CUPRATE_CONFIG_DIR, DEFAULT_CONFIG_FILE_NAME},
network::Network,
};
use cuprate_p2p::block_downloader::BlockDownloaderConfig;
use cuprate_p2p_core::ClearNet;
@ -24,19 +27,16 @@ use p2p::P2PConfig;
use storage::StorageConfig;
use tracing_config::TracingConfig;
/// The default name of Cuprate's config file.
const DEFAULT_CONFIG_FILE_NAME: &str = "Cuprate.toml";
/// Reads the args & config file, returning a [`Config`].
pub fn read_config_and_args() -> Config {
let args = args::Args::parse();
let config: Config = if let Some(config_file) = &args.config_file {
// If a config file was set in the args try read it and exit if we can't.
// If a config file was set in the args try to read it and exit if we can't.
match Config::read_from_file(config_file) {
Ok(config) => config,
Err(e) => {
tracing::error!("Failed to read config from file: {}", e);
tracing::error!("Failed to read config from file: {e}");
std::process::exit(1);
}
}
@ -74,20 +74,22 @@ pub struct Config {
/// The P2P network config.
p2p: P2PConfig,
/// The Storage config
/// The storage config.
storage: StorageConfig,
}
impl Config {
/// Attempts to read a config file in [`toml`] format from the given [`Path`.
/// Attempts to read a config file in [`toml`] format from the given [`Path`].
///
/// # Errors
///
/// Will return an [`Err`] if the file cannot be read or if the file is not a valid [`toml`] config.
fn read_from_file(file: impl AsRef<Path>) -> Result<Self, anyhow::Error> {
fn read_from_path(file: impl AsRef<Path>) -> Result<Self, anyhow::Error> {
let file_text = read_to_string(file.as_ref())?;
Ok(toml::from_str(&file_text).inspect_err(|_| {
Ok(toml::from_str(&file_text).inspect_err(|e| {
tracing::warn!("Error: {e}");
tracing::warn!(
"Failed to parse config file at: {}",
file.as_ref().to_string_lossy()

View file

@ -48,7 +48,7 @@ buffer_size = 50_000_000
in_progress_queue_size = 50_000_000
## The target size of a batch of blocks (bytes), must not exceed 100MB.
target_batch_size = 5_000_000
## The number of blocks in the first bacth (you probably shouldn't change this).
## The number of blocks in the first batch (you probably shouldn't change this).
initial_batch_len = 1
## The amount of time between checking the pool of connected peers for free peers to download blocks.
check_client_pool_interval = {{ secs = 30, nanos = 0 }}

View file

@ -10,7 +10,7 @@ use crate::config::{default::create_default_config_file, Config, DEFAULT_CONFIG_
#[derive(clap::Parser, Debug)]
#[command(version, about)]
pub struct Args {
/// The network we should run on.
/// The network to run on.
#[arg(
long,
default_value_t = Network::Mainnet,
@ -21,10 +21,10 @@ pub struct Args {
/// The amount of outbound clear-net connections to maintain.
#[arg(long)]
pub outbound_connections: Option<usize>,
/// The location of the Cuprate config file.
/// The PATH of the `cuprated` config file.
#[arg(long)]
pub config_file: Option<PathBuf>,
/// Generate a config file and place it in the given folder.
/// Generate a config file and place it in the given PATH.
#[arg(long)]
pub generate_config: Option<PathBuf>,
}

View file

@ -51,7 +51,7 @@ impl SharedNetConfig {
// HACK: we add the network here so we don't need to define another address book config.
let mut address_book_config = self.address_book_config.clone();
address_book_config
.peer_store_folder
.peer_store_directory
.push(network.to_string());
address_book_config
@ -104,7 +104,7 @@ pub fn clear_net_seed_nodes(network: Network) -> Vec<SocketAddr> {
seeds
.iter()
.map(|&s| str::parse(s))
.map(|s| s.parse())
.collect::<Result<_, _>>()
.unwrap()
}

View file

@ -42,8 +42,8 @@ pub struct TxpoolConfig {
#[serde(flatten)]
pub shared: SharedStorageConfig,
/// The maximum size of the tx-pool (bytes).
pub max_txpool_size: usize,
/// The maximum size of the tx-pool.
pub max_txpool_byte_size: usize,
}
impl Default for TxpoolConfig {

View file

@ -63,6 +63,9 @@ pub const CUPRATE_DIR: &str = {
}
};
/// The default name of Cuprate's config file.
pub const DEFAULT_CONFIG_FILE_NAME: &str = "Cuprated.toml";
//---------------------------------------------------------------------------------------------------- Directories
/// Create a `LazyLock` for common PATHs used by Cuprate.
///

View file

@ -15,7 +15,7 @@ fn test_cfg() -> AddressBookConfig {
AddressBookConfig {
max_white_list_length: 100,
max_gray_list_length: 500,
peer_store_folder: PathBuf::new(),
peer_store_directory: PathBuf::new(),
peer_save_period: Duration::from_secs(60),
}
}

View file

@ -31,8 +31,8 @@ pub struct AddressBookConfig {
///
/// Gray peers are peers we are yet to make a connection to.
pub max_gray_list_length: usize,
/// The location to store the peer store file.
pub peer_store_folder: PathBuf,
/// The location to store the peer store files.
pub peer_store_directory: PathBuf,
/// The amount of time between saving the address book to disk.
pub peer_save_period: Duration,
}
@ -43,7 +43,7 @@ impl Default for AddressBookConfig {
Self {
max_white_list_length: 1000,
max_gray_list_length: 5000,
peer_store_folder: cuprate_helper::fs::CUPRATE_CACHE_DIR.clone(),
peer_store_directory: cuprate_helper::fs::CUPRATE_CACHE_DIR.clone(),
peer_save_period: Duration::from_secs(90),
}
}

View file

@ -39,7 +39,9 @@ pub(crate) fn save_peers_to_disk<Z: BorshNetworkZone>(
})
.unwrap();
let file = cfg.peer_store_folder.join(format!("{}_p2p_state", Z::NAME));
let file = cfg
.peer_store_directory
.join(format!("{}_p2p_state", Z::NAME));
spawn_blocking(move || fs::write(&file, &data))
}
@ -52,7 +54,9 @@ pub(crate) async fn read_peers_from_disk<Z: BorshNetworkZone>(
),
std::io::Error,
> {
let file = cfg.peer_store_folder.join(format!("{}_p2p_state", Z::NAME));
let file = cfg
.peer_store_directory
.join(format!("{}_p2p_state", Z::NAME));
tracing::info!("Loading peers from file: {} ", file.display());

View file

@ -90,7 +90,7 @@ impl ConfigBuilder {
}
}
/// Change the network this blockchain database is for.
/// Change the network this database is for.
#[must_use]
pub const fn network(mut self, network: Network) -> Self {
self.network = network;