Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cuprated: config & args #304

Merged
merged 33 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a280221
init config
Boog900 Oct 6, 2024
5236ede
split sections
Boog900 Oct 17, 2024
8d0c26c
Merge branch 'main' into cuprated-config
Boog900 Oct 17, 2024
5906e7d
finish initial config.
Boog900 Oct 17, 2024
0d29b1c
Merge branch 'main' into cuprated-config
Boog900 Oct 26, 2024
3dc0049
fix clap
Boog900 Oct 26, 2024
d1f3eb4
misc changes
Boog900 Oct 26, 2024
d7d7541
fix doc
Boog900 Oct 26, 2024
cf34a8d
fix test & clippy
Boog900 Oct 26, 2024
65223dc
fix test 2
Boog900 Oct 26, 2024
2751831
try fix windows
Boog900 Oct 26, 2024
54f0bde
testing
Boog900 Oct 27, 2024
52305ff
testing 2
Boog900 Oct 27, 2024
fe28c70
fix windows test
Boog900 Oct 27, 2024
06b7429
fix windows: the remix.
Boog900 Oct 27, 2024
26653d4
review comments
Boog900 Nov 5, 2024
352dac6
fix imports
Boog900 Nov 5, 2024
5a5f4d1
Merge branch 'main' into cuprated-config
Boog900 Nov 5, 2024
3664eb1
rename & fix default config file
Boog900 Nov 5, 2024
623ba49
fix cargo hack
Boog900 Nov 5, 2024
21f6287
enable serde on `cuprate-helper`
Boog900 Nov 5, 2024
e652c18
changes from matrix chats
Boog900 Nov 10, 2024
7c5fa6d
fix ci
Boog900 Nov 10, 2024
6680954
fix doc
Boog900 Nov 10, 2024
318cf94
fix doc test
Boog900 Nov 10, 2024
8e941f5
move Cuprated.toml
Boog900 Dec 2, 2024
509d5cb
remove default.rs
Boog900 Dec 2, 2024
c947cf6
`size` -> `bytes`
Boog900 Dec 2, 2024
bd00c92
`addressbook_path` -> `address_book_path`
Boog900 Dec 2, 2024
1dd1ed1
fix config output
Boog900 Dec 2, 2024
22aa7a9
Merge branch 'main' into cuprated-config
Boog900 Dec 2, 2024
0d88244
fix ci
Boog900 Dec 2, 2024
b9e26bb
Update binaries/cuprated/src/config/args.rs
Boog900 Dec 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 8 additions & 13 deletions binaries/cuprated/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ cuprate-fast-sync = { workspace = true }
cuprate-consensus-context = { workspace = true }
cuprate-consensus-rules = { workspace = true }
cuprate-cryptonight = { workspace = true }
cuprate-helper = { workspace = true }
cuprate-helper = { workspace = true, features = ["serde"] }
cuprate-epee-encoding = { workspace = true }
cuprate-fixed-bytes = { workspace = true }
cuprate-levin = { workspace = true }
cuprate-wire = { workspace = true }
cuprate-p2p = { workspace = true }
cuprate-p2p-core = { workspace = true }
cuprate-p2p = { workspace = true, features = ["serde"] }
cuprate-p2p-core = { workspace = true, features = ["serde"] }
cuprate-dandelion-tower = { workspace = true }
cuprate-async-buffer = { workspace = true }
cuprate-address-book = { workspace = true }
cuprate-address-book = { workspace = true, features = ["serde_config"] }
cuprate-blockchain = { workspace = true, features = ["service"] }
cuprate-database-service = { workspace = true }
cuprate-database-service = { workspace = true, features = ["serde"] }
cuprate-txpool = { workspace = true }
cuprate-database = { workspace = true }
cuprate-database = { workspace = true, features = ["serde"] }
cuprate-pruning = { workspace = true }
cuprate-test-utils = { workspace = true }
cuprate-types = { workspace = true }
Expand Down Expand Up @@ -72,13 +72,8 @@ tokio-stream = { workspace = true }
tokio = { workspace = true }
tower = { workspace = true }
tracing-subscriber = { workspace = true, features = ["std", "fmt", "default"] }
tracing = { workspace = true }
tracing = { workspace = true, features = ["default"] }
toml = "0.8"

[lints]
workspace = true

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"
Boog900 marked this conversation as resolved.
Show resolved Hide resolved
144 changes: 144 additions & 0 deletions binaries/cuprated/src/config.rs
Original file line number Diff line number Diff line change
@@ -1 +1,145 @@
//! cuprated config
use std::{
fs::{read_to_string, File},
io,
path::Path,
time::Duration,
};

use clap::Parser;
use serde::{Deserialize, Serialize};

use cuprate_consensus::ContextConfig;
use cuprate_helper::{fs::CUPRATE_CONFIG_DIR, network::Network};
use cuprate_p2p::block_downloader::BlockDownloaderConfig;
use cuprate_p2p_core::ClearNet;

mod args;
mod default;
mod p2p;
mod storage;
mod tracing_config;

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";
Boog900 marked this conversation as resolved.
Show resolved Hide resolved

/// Reads the args & config file, returning a [`Config`].
pub fn read_config_and_args() -> Config {
hinto-janai marked this conversation as resolved.
Show resolved Hide resolved
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.
match Config::read_from_file(config_file) {
Ok(config) => config,
Err(e) => {
tracing::error!("Failed to read config from file: {}", e);
Boog900 marked this conversation as resolved.
Show resolved Hide resolved
std::process::exit(1);
}
}
} else {
// First attempt to read the config file from the current directory.
std::env::current_dir()
.map_err(Into::into)
.and_then(Config::read_from_file)
.inspect_err(|e| tracing::debug!("Failed to read config from current dir: {e}"))
// otherwise try the main config directory.
.or_else(|_| {
let file = CUPRATE_CONFIG_DIR.join(DEFAULT_CONFIG_FILE_NAME);
Config::read_from_file(file)
})
.inspect_err(|e| {
tracing::debug!("Failed to read config from config dir: {e}");
tracing::warn!("Failed to find/read config file, using default config.");
})
.unwrap_or_default()
};

args.apply_args(config)
}

/// The config for all of Cuprate.
#[derive(Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
Boog900 marked this conversation as resolved.
Show resolved Hide resolved
/// The network we should run on.
network: Network,

/// [`tracing`] config.
tracing: TracingConfig,

/// The P2P network config.
p2p: P2PConfig,

/// The Storage config
Boog900 marked this conversation as resolved.
Show resolved Hide resolved
Boog900 marked this conversation as resolved.
Show resolved Hide resolved
storage: StorageConfig,
}

impl Config {
/// Attempts to read a config file in [`toml`] format from the given [`Path`.
Boog900 marked this conversation as resolved.
Show resolved Hide resolved
///
/// # 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> {
Boog900 marked this conversation as resolved.
Show resolved Hide resolved
let file_text = read_to_string(file.as_ref())?;

Ok(toml::from_str(&file_text).inspect_err(|_| {
tracing::warn!(
"Failed to parse config file at: {}",
file.as_ref().to_string_lossy()
);
})?)
Boog900 marked this conversation as resolved.
Show resolved Hide resolved
}

/// Returns the current [`Network`] we are running on.
pub const fn network(&self) -> Network {
self.network
}

/// The [`ClearNet`], [`cuprate_p2p::P2PConfig`].
pub fn clearnet_p2p_config(&self) -> cuprate_p2p::P2PConfig<ClearNet> {
cuprate_p2p::P2PConfig {
network: self.network,
seeds: p2p::clear_net_seed_nodes(self.network),
outbound_connections: self.p2p.clear_net.general.outbound_connections,
extra_outbound_connections: self.p2p.clear_net.general.extra_outbound_connections,
max_inbound_connections: self.p2p.clear_net.general.max_inbound_connections,
gray_peers_percent: self.p2p.clear_net.general.gray_peers_percent,
server_config: Some(self.p2p.clear_net.server.clone()),
p2p_port: self.p2p.clear_net.general.p2p_port,
// TODO: set this if a public RPC server is set.
rpc_port: 0,
address_book_config: self.p2p.clear_net.general.address_book_config.clone(),
}
}

/// The [`ContextConfig`].
pub const fn context_config(&self) -> ContextConfig {
match self.network {
Network::Mainnet => ContextConfig::main_net(),
Network::Stagenet => ContextConfig::stage_net(),
Network::Testnet => ContextConfig::test_net(),
}
}

/// The [`cuprate_blockchain`] config.
pub fn blockchain_config(&self) -> cuprate_blockchain::config::Config {
let blockchain = &self.storage.blockchain;

// We don't set reader threads as we manually make the reader threadpool.
cuprate_blockchain::config::ConfigBuilder::default()
.network(self.network)
.db_directory(blockchain.shared.path.clone())
.sync_mode(blockchain.shared.sync_mode)
.build()
}

/// The [`BlockDownloaderConfig`].
pub const fn block_downloader_config(&self) -> BlockDownloaderConfig {
self.p2p.block_downloader
}
}
75 changes: 75 additions & 0 deletions binaries/cuprated/src/config/Cuprate.toml
Boog900 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# ____ _
hinto-janai marked this conversation as resolved.
Show resolved Hide resolved
# / ___| _ _ __ _ __ __ _| |_ ___
# | | | | | | '_ \| '__/ _` | __/ _ \
# | |__| |_| | |_) | | | (_| | || __/
# \____\__,_| .__/|_| \__,_|\__\___|
# |_|
#

## The network to run on, valid values: "Mainnet", "Testnet", "Stagenet".
hinto-janai marked this conversation as resolved.
Show resolved Hide resolved
network = "Mainnet"

## Tracing config.
[tracing]
## The minimum level for log events to be displayed.
level = "info"

## Clear-net config.
[p2p.clear_net]
## The number of outbound connections we should make and maintain.
outbound_connections = 64
## The number of extra connections we should make under load from the rest of Cuprate, i.e. when syncing.
extra_outbound_connections = 8
## The maximum number of incoming we should allow.
max_inbound_connections = 128
## The percent of outbound connections that should be to nodes we have not connected to before.
gray_peers_percent = 0.7
## The port to accept connections on, if left `0` no connections will be accepted.
p2p_port = 0
## The IP address to listen to connections on.
server.ip = "0.0.0.0"

## The Clear-net addressbook config.
[p2p.clear_net.address_book_config]
## The size of the white peer list, which contains peers we have made a connection to before.
max_white_list_length = 1_000
## The size of the gray peer list, which contains peers we have not made a connection to before.
max_gray_list_length = 5_000
## The folder to store the address book.
peer_store_folder = "{cache}"
## The amount of time between address book saves.
peer_save_period = {{ secs = 90, nanos = 0 }}
hinto-janai marked this conversation as resolved.
Show resolved Hide resolved

## The block downloader config.
[p2p.block_downloader]
## The size of the buffer of sequential blocks waiting to be verified and added to the chain (bytes).
buffer_size = 50_000_000
## The size of the queue of blocks which are waiting for a parent block to be downloaded (bytes).
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).
Boog900 marked this conversation as resolved.
Show resolved Hide resolved
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 }}

## Storage config
[storage]
## The amount of reader threads to spawn.
reader_threads = "OnePerThread"

## Txpool storage config.
[storage.txpool]
## The txpool storage location.
path = "{txpool}"
## The database sync mode for the txpool.
sync_mode = "Async"
## The maximum size of all the txs in the pool (bytes).
max_txpool_size = 100_000_000

## Blockchain storage config.
[storage.blockchain]
## The blockchain storage location.
path = "{blockchain}"
## The database sync mode for the blockchain.
sync_mode = "Async"
Loading
Loading