From a280221cd9604525945fd1dea2b2798bdccfe378 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sun, 6 Oct 2024 01:07:47 +0100 Subject: [PATCH 01/22] init config --- Cargo.lock | 4 +++ binaries/cuprated/Cargo.toml | 4 +-- binaries/cuprated/src/config.rs | 11 ++++++ binaries/cuprated/src/config/sections.rs | 44 ++++++++++++++++++++++++ helper/Cargo.toml | 2 ++ helper/src/network.rs | 2 ++ p2p/address-book/Cargo.toml | 7 +++- p2p/address-book/src/lib.rs | 23 +++++++++---- p2p/address-book/src/store.rs | 7 ++-- p2p/p2p-core/Cargo.toml | 1 + p2p/p2p-core/src/network_zones/clear.rs | 10 ++++++ 11 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 binaries/cuprated/src/config/sections.rs diff --git a/Cargo.lock b/Cargo.lock index 611f9026..4d651bd6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -519,12 +519,14 @@ version = "0.1.0" dependencies = [ "borsh", "cuprate-constants", + "cuprate-helper", "cuprate-p2p-core", "cuprate-pruning", "cuprate-test-utils", "futures", "indexmap", "rand", + "serde", "thiserror", "tokio", "tokio-util", @@ -733,6 +735,7 @@ dependencies = [ "libc", "monero-serai", "rayon", + "serde", "tokio", "windows", ] @@ -812,6 +815,7 @@ dependencies = [ "futures", "hex", "hex-literal", + "serde", "thiserror", "tokio", "tokio-stream", diff --git a/binaries/cuprated/Cargo.toml b/binaries/cuprated/Cargo.toml index c8ccd5a6..a2ded303 100644 --- a/binaries/cuprated/Cargo.toml +++ b/binaries/cuprated/Cargo.toml @@ -19,10 +19,10 @@ cuprate-fixed-bytes = { path = "../../net/fixed-bytes" } cuprate-levin = { path = "../../net/levin" } cuprate-wire = { path = "../../net/wire" } cuprate-p2p = { path = "../../p2p/p2p" } -cuprate-p2p-core = { path = "../../p2p/p2p-core" } +cuprate-p2p-core = { path = "../../p2p/p2p-core", features = ["serde"] } cuprate-dandelion-tower = { path = "../../p2p/dandelion-tower" } cuprate-async-buffer = { path = "../../p2p/async-buffer" } -cuprate-address-book = { path = "../../p2p/address-book" } +cuprate-address-book = { path = "../../p2p/address-book", features = ["serde_config"] } cuprate-blockchain = { path = "../../storage/blockchain" } cuprate-database-service = { path = "../../storage/service" } cuprate-txpool = { path = "../../storage/txpool" } diff --git a/binaries/cuprated/src/config.rs b/binaries/cuprated/src/config.rs index d613c1fc..898db414 100644 --- a/binaries/cuprated/src/config.rs +++ b/binaries/cuprated/src/config.rs @@ -1 +1,12 @@ //! cuprated config +use serde::{Deserialize, Serialize}; + +mod sections; + +use sections::P2PConfig; + +#[derive(Default, Deserialize, Serialize)] +#[serde(deny_unknown_fields, default)] +pub struct Config { + p2p: P2PConfig, +} diff --git a/binaries/cuprated/src/config/sections.rs b/binaries/cuprated/src/config/sections.rs new file mode 100644 index 00000000..c345d229 --- /dev/null +++ b/binaries/cuprated/src/config/sections.rs @@ -0,0 +1,44 @@ +use cuprate_address_book::AddressBookConfig; +use cuprate_p2p_core::ClearNetServerCfg; +use serde::{Deserialize, Serialize}; + +#[derive(Default, Deserialize, Serialize)] +#[serde(deny_unknown_fields, default)] +pub struct P2PConfig { + clear_net: ClearNetConfig, +} + +#[derive(Default, Deserialize, Serialize)] +#[serde(deny_unknown_fields, default)] +pub struct ClearNetConfig { + server: ClearNetServerCfg, + #[serde(flatten)] + flattened: SharedNetConfig, +} + +#[derive(Deserialize, Serialize)] +#[serde(deny_unknown_fields, default)] +pub struct SharedNetConfig { + /// The number of outbound connections to make and try keep. + pub outbound_connections: usize, + /// The amount of extra connections we can make if we are under load from the rest of Cuprate. + pub extra_outbound_connections: usize, + /// The maximum amount of inbound connections + pub max_inbound_connections: usize, + /// port to use to accept p2p connections. + pub p2p_port: u16, + /// The address book config. + pub address_book_config: AddressBookConfig, +} + +impl Default for SharedNetConfig { + fn default() -> Self { + Self { + outbound_connections: 32, + extra_outbound_connections: 8, + max_inbound_connections: 128, + p2p_port: 18080, + address_book_config: AddressBookConfig::default(), + } + } +} diff --git a/helper/Cargo.toml b/helper/Cargo.toml index 111c6f02..71d12c0e 100644 --- a/helper/Cargo.toml +++ b/helper/Cargo.toml @@ -33,6 +33,8 @@ futures = { workspace = true, optional = true, features = ["std"] } monero-serai = { workspace = true, optional = true } rayon = { workspace = true, optional = true } +serde = { workspace = true, optional = true, features = ["derive"] } + # This is kinda a stupid work around. # [thread] needs to activate one of these libs (windows|libc) # although it depends on what target we're building for. diff --git a/helper/src/network.rs b/helper/src/network.rs index f3224b33..02574752 100644 --- a/helper/src/network.rs +++ b/helper/src/network.rs @@ -5,6 +5,7 @@ //! into it's own crate. //! //! `#[no_std]` compatible. +// TODO: move to types crate. const MAINNET_NETWORK_ID: [u8; 16] = [ 0x12, 0x30, 0xF1, 0x71, 0x61, 0x04, 0x41, 0x61, 0x17, 0x31, 0x00, 0x82, 0x16, 0xA1, 0xA1, 0x10, @@ -18,6 +19,7 @@ const STAGENET_NETWORK_ID: [u8; 16] = [ /// An enum representing every Monero network. #[derive(Debug, Clone, Copy, Default)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub enum Network { /// Mainnet #[default] diff --git a/p2p/address-book/Cargo.toml b/p2p/address-book/Cargo.toml index 9afc2552..79fe9211 100644 --- a/p2p/address-book/Cargo.toml +++ b/p2p/address-book/Cargo.toml @@ -5,9 +5,13 @@ edition = "2021" license = "MIT" authors = ["Boog900"] +[features] +defualt = [] +serde_config = ["dep:serde", "dep:cuprate-helper"] [dependencies] cuprate-constants = { path = "../../constants" } +cuprate-helper = { path = "../../helper", features = ["std", "fs"], optional = true } cuprate-pruning = { path = "../../pruning" } cuprate-p2p-core = { path = "../p2p-core" } @@ -23,7 +27,8 @@ indexmap = { workspace = true, features = ["std"] } rand = { workspace = true, features = ["std", "std_rng"] } -borsh = { workspace = true, features = ["derive", "std"]} +borsh = { workspace = true, features = ["derive", "std"] } +serde = { workspace = true, features = ["derive", "std"], optional = true } [dev-dependencies] cuprate-test-utils = {path = "../../test-utils"} diff --git a/p2p/address-book/src/lib.rs b/p2p/address-book/src/lib.rs index c0903485..2d7b3d63 100644 --- a/p2p/address-book/src/lib.rs +++ b/p2p/address-book/src/lib.rs @@ -20,6 +20,8 @@ mod store; /// The address book config. #[derive(Debug, Clone)] +#[cfg_attr(feature = "serde_config", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "serde_config", serde(deny_unknown_fields, default))] pub struct AddressBookConfig { /// The maximum number of white peers in the peer list. /// @@ -29,12 +31,24 @@ 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 address book. - pub peer_store_file: PathBuf, + /// The location to store the peer store file. + pub peer_store_folder: PathBuf, /// The amount of time between saving the address book to disk. pub peer_save_period: Duration, } +#[cfg(feature = "serde_config")] +impl Default for AddressBookConfig { + fn default() -> Self { + Self { + max_white_list_length: 1000, + max_gray_list_length: 5000, + peer_store_folder: cuprate_helper::fs::CUPRATE_CACHE_DIR.clone(), + peer_save_period: Duration::from_secs(90), + } + } +} + /// Possible errors when dealing with the address book. /// This is boxed when returning an error in the [`tower::Service`]. #[derive(Debug, thiserror::Error, Eq, PartialEq)] @@ -63,11 +77,6 @@ pub enum AddressBookError { pub async fn init_address_book( cfg: AddressBookConfig, ) -> Result, std::io::Error> { - tracing::info!( - "Loading peers from file: {} ", - cfg.peer_store_file.display() - ); - let (white_list, gray_list) = match store::read_peers_from_disk::(&cfg).await { Ok(res) => res, Err(e) if e.kind() == ErrorKind::NotFound => (vec![], vec![]), diff --git a/p2p/address-book/src/store.rs b/p2p/address-book/src/store.rs index 07c117e1..91309182 100644 --- a/p2p/address-book/src/store.rs +++ b/p2p/address-book/src/store.rs @@ -39,7 +39,7 @@ pub(crate) fn save_peers_to_disk( }) .unwrap(); - let file = cfg.peer_store_file.clone(); + let file = cfg.peer_store_folder.join(format!("{}_p2p_state", Z::NAME)); spawn_blocking(move || fs::write(&file, &data)) } @@ -52,7 +52,10 @@ pub(crate) async fn read_peers_from_disk( ), std::io::Error, > { - let file = cfg.peer_store_file.clone(); + let file = cfg.peer_store_folder.join(format!("{}_p2p_state", Z::NAME)); + + tracing::info!("Loading peers from file: {} ", file.display()); + let data = spawn_blocking(move || fs::read(file)).await.unwrap()?; let de_ser: DeserPeerDataV1 = from_slice(&data)?; diff --git a/p2p/p2p-core/Cargo.toml b/p2p/p2p-core/Cargo.toml index a30590fa..0a4b781f 100644 --- a/p2p/p2p-core/Cargo.toml +++ b/p2p/p2p-core/Cargo.toml @@ -27,6 +27,7 @@ tracing = { workspace = true, features = ["std", "attributes"] } hex-literal = { workspace = true } borsh = { workspace = true, features = ["derive", "std"], optional = true } +serde = { workspace = true, features = ["derive", "std"], optional = true } [dev-dependencies] cuprate-test-utils = { path = "../../test-utils" } diff --git a/p2p/p2p-core/src/network_zones/clear.rs b/p2p/p2p-core/src/network_zones/clear.rs index 261d5ad3..3c50f315 100644 --- a/p2p/p2p-core/src/network_zones/clear.rs +++ b/p2p/p2p-core/src/network_zones/clear.rs @@ -38,10 +38,20 @@ impl NetZoneAddress for SocketAddr { } #[derive(Debug, Clone)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] +#[cfg_attr(feature = "serde", serde(deny_unknown_fields))] pub struct ClearNetServerCfg { pub ip: IpAddr, } +impl Default for ClearNetServerCfg { + fn default() -> Self { + Self { + ip: IpAddr::V4(Ipv4Addr::UNSPECIFIED), + } + } +} + #[derive(Clone, Copy)] pub enum ClearNet {} From 5236ede85bc09a4675221f9efd101eeb53773c68 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Thu, 17 Oct 2024 16:50:14 +0100 Subject: [PATCH 02/22] split sections --- binaries/cuprated/src/config.rs | 62 ++++++++++++++++++- .../src/config/{sections.rs => p2p.rs} | 10 +-- binaries/cuprated/src/config/storage.rs | 8 +++ p2p/p2p/src/lib.rs | 2 +- 4 files changed, 75 insertions(+), 7 deletions(-) rename binaries/cuprated/src/config/{sections.rs => p2p.rs} (85%) create mode 100644 binaries/cuprated/src/config/storage.rs diff --git a/binaries/cuprated/src/config.rs b/binaries/cuprated/src/config.rs index 898db414..2a55bebb 100644 --- a/binaries/cuprated/src/config.rs +++ b/binaries/cuprated/src/config.rs @@ -1,12 +1,70 @@ //! cuprated config + +use cuprate_consensus::ContextConfig; +use cuprate_helper::network::Network; +use cuprate_p2p::block_downloader::BlockDownloaderConfig; +use cuprate_p2p_core::ClearNet; use serde::{Deserialize, Serialize}; +use std::time::Duration; -mod sections; +mod p2p; +mod storage; -use sections::P2PConfig; +use p2p::P2PConfig; +use storage::StorageConfig; + +pub fn config() -> Config { + Config::default() +} #[derive(Default, Deserialize, Serialize)] #[serde(deny_unknown_fields, default)] pub struct Config { + network: Network, + p2p: P2PConfig, + + storage: StorageConfig, +} + +impl Config { + pub fn network(&self) -> Network { + self.network + } + + pub fn clearnet_p2p_config(&self) -> cuprate_p2p::P2PConfig { + cuprate_p2p::P2PConfig { + network: 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, + rpc_port: 0, + address_book_config: self.p2p.clear_net.general.address_book_config.clone(), + } + } + + pub fn context_config(&self) -> ContextConfig { + match self.network { + Network::Mainnet => ContextConfig::main_net(), + Network::Stagenet => ContextConfig::stage_net(), + Network::Testnet => ContextConfig::test_net(), + } + } + + pub fn blockchain_config(&self) -> cuprate_blockchain::config::Config { + self.storage.blockchain.clone() + } + + pub fn block_downloader_config(&self) -> BlockDownloaderConfig { + BlockDownloaderConfig { + buffer_size: 50_000_000, + in_progress_queue_size: 50_000_000, + check_client_pool_interval: Duration::from_secs(30), + target_batch_size: 5_000_000, + initial_batch_size: 1, + } + } } diff --git a/binaries/cuprated/src/config/sections.rs b/binaries/cuprated/src/config/p2p.rs similarity index 85% rename from binaries/cuprated/src/config/sections.rs rename to binaries/cuprated/src/config/p2p.rs index c345d229..f89b2460 100644 --- a/binaries/cuprated/src/config/sections.rs +++ b/binaries/cuprated/src/config/p2p.rs @@ -5,15 +5,15 @@ use serde::{Deserialize, Serialize}; #[derive(Default, Deserialize, Serialize)] #[serde(deny_unknown_fields, default)] pub struct P2PConfig { - clear_net: ClearNetConfig, + pub clear_net: ClearNetConfig, } #[derive(Default, Deserialize, Serialize)] #[serde(deny_unknown_fields, default)] pub struct ClearNetConfig { - server: ClearNetServerCfg, + pub server: ClearNetServerCfg, #[serde(flatten)] - flattened: SharedNetConfig, + pub general: SharedNetConfig, } #[derive(Deserialize, Serialize)] @@ -25,6 +25,7 @@ pub struct SharedNetConfig { pub extra_outbound_connections: usize, /// The maximum amount of inbound connections pub max_inbound_connections: usize, + pub gray_peers_percent: f64, /// port to use to accept p2p connections. pub p2p_port: u16, /// The address book config. @@ -34,9 +35,10 @@ pub struct SharedNetConfig { impl Default for SharedNetConfig { fn default() -> Self { Self { - outbound_connections: 32, + outbound_connections: 64, extra_outbound_connections: 8, max_inbound_connections: 128, + gray_peers_percent: 0.7, p2p_port: 18080, address_book_config: AddressBookConfig::default(), } diff --git a/binaries/cuprated/src/config/storage.rs b/binaries/cuprated/src/config/storage.rs new file mode 100644 index 00000000..de913949 --- /dev/null +++ b/binaries/cuprated/src/config/storage.rs @@ -0,0 +1,8 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Default, Deserialize, Serialize)] +#[serde(deny_unknown_fields, default)] +pub struct StorageConfig { + pub blockchain: cuprate_blockchain::config::Config, + pub txpool: cuprate_txpool::Config, +} diff --git a/p2p/p2p/src/lib.rs b/p2p/p2p/src/lib.rs index 24311589..5d78e85d 100644 --- a/p2p/p2p/src/lib.rs +++ b/p2p/p2p/src/lib.rs @@ -17,7 +17,7 @@ use cuprate_p2p_core::{ CoreSyncSvc, NetworkZone, ProtocolRequestHandlerMaker, }; -mod block_downloader; +pub mod block_downloader; mod broadcast; mod client_pool; pub mod config; From 5906e7d4b7e58797a173b94ce84bbf93e25efe9e Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Thu, 17 Oct 2024 20:49:55 +0100 Subject: [PATCH 03/22] finish initial config. --- Cargo.lock | 28 +++++ binaries/cuprated/Cargo.toml | 11 +- binaries/cuprated/src/config.rs | 107 +++++++++++++++--- binaries/cuprated/src/config/Cuprate.toml | 75 ++++++++++++ binaries/cuprated/src/config/args.rs | 45 ++++++++ binaries/cuprated/src/config/default.rs | 69 +++++++++++ binaries/cuprated/src/config/p2p.rs | 57 +++++++++- binaries/cuprated/src/config/storage.rs | 66 ++++++++++- .../cuprated/src/config/tracing_config.rs | 42 +++++++ binaries/cuprated/src/main.rs | 2 + helper/src/fs.rs | 6 + helper/src/network.rs | 31 ++++- net/wire/src/p2p/common.rs | 2 + p2p/p2p-core/src/network_zones/clear.rs | 2 +- p2p/p2p/Cargo.toml | 2 + p2p/p2p/src/block_downloader.rs | 18 ++- p2p/p2p/src/block_downloader/tests.rs | 2 +- storage/blockchain/Cargo.toml | 1 + storage/blockchain/src/config.rs | 39 +++++-- storage/txpool/Cargo.toml | 2 +- storage/txpool/src/config.rs | 47 +++++--- 21 files changed, 595 insertions(+), 59 deletions(-) create mode 100644 binaries/cuprated/src/config/Cuprate.toml create mode 100644 binaries/cuprated/src/config/args.rs create mode 100644 binaries/cuprated/src/config/default.rs create mode 100644 binaries/cuprated/src/config/tracing_config.rs diff --git a/Cargo.lock b/Cargo.lock index 39305f62..b17a322a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -798,6 +798,7 @@ dependencies = [ "rand", "rand_distr", "rayon", + "serde", "thiserror", "tokio", "tokio-stream", @@ -1018,6 +1019,7 @@ dependencies = [ "tokio", "tokio-stream", "tokio-util", + "toml", "tower 0.5.1", "tracing", "tracing-subscriber", @@ -2552,6 +2554,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -2911,11 +2922,26 @@ dependencies = [ "tracing", ] +[[package]] +name = "toml" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + [[package]] name = "toml_datetime" version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" +dependencies = [ + "serde", +] [[package]] name = "toml_edit" @@ -2924,6 +2950,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b072cee73c449a636ffd6f32bd8de3a9f7119139aff882f44943ce2986dc5cf" dependencies = [ "indexmap", + "serde", + "serde_spanned", "toml_datetime", "winnow", ] diff --git a/binaries/cuprated/Cargo.toml b/binaries/cuprated/Cargo.toml index c458302b..4fe3a650 100644 --- a/binaries/cuprated/Cargo.toml +++ b/binaries/cuprated/Cargo.toml @@ -13,20 +13,20 @@ cuprate-consensus = { path = "../../consensus" } cuprate-fast-sync = { path = "../../consensus/fast-sync" } cuprate-consensus-rules = { path = "../../consensus/rules" } cuprate-cryptonight = { path = "../../cryptonight" } -cuprate-helper = { path = "../../helper" } +cuprate-helper = { path = "../../helper", features = ["serde"] } cuprate-epee-encoding = { path = "../../net/epee-encoding" } cuprate-fixed-bytes = { path = "../../net/fixed-bytes" } cuprate-levin = { path = "../../net/levin" } cuprate-wire = { path = "../../net/wire" } -cuprate-p2p = { path = "../../p2p/p2p" } +cuprate-p2p = { path = "../../p2p/p2p", features = ["serde"] } cuprate-p2p-core = { path = "../../p2p/p2p-core", features = ["serde"] } cuprate-dandelion-tower = { path = "../../p2p/dandelion-tower" } cuprate-async-buffer = { path = "../../p2p/async-buffer" } cuprate-address-book = { path = "../../p2p/address-book", features = ["serde_config"] } cuprate-blockchain = { path = "../../storage/blockchain", features = ["service"] } -cuprate-database-service = { path = "../../storage/service" } +cuprate-database-service = { path = "../../storage/service", features = ["serde"] } cuprate-txpool = { path = "../../storage/txpool" } -cuprate-database = { path = "../../storage/database" } +cuprate-database = { path = "../../storage/database", features = ["serde"] } cuprate-pruning = { path = "../../pruning" } cuprate-test-utils = { path = "../../test-utils" } cuprate-types = { path = "../../types" } @@ -71,7 +71,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 diff --git a/binaries/cuprated/src/config.rs b/binaries/cuprated/src/config.rs index 2a55bebb..290f5c27 100644 --- a/binaries/cuprated/src/config.rs +++ b/binaries/cuprated/src/config.rs @@ -1,52 +1,124 @@ //! 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::network::Network; +use cuprate_helper::{fs::CUPRATE_CONFIG_DIR, network::Network}; use cuprate_p2p::block_downloader::BlockDownloaderConfig; use cuprate_p2p_core::ClearNet; -use serde::{Deserialize, Serialize}; -use std::time::Duration; +mod args; +mod default; mod p2p; mod storage; +mod tracing_config; use p2p::P2PConfig; use storage::StorageConfig; +use tracing_config::TracingConfig; -pub fn config() -> Config { - Config::default() +/// 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. + match Config::read_from_file(config_file) { + Ok(config) => config, + Err(e) => { + tracing::error!("Failed to read config from file: {}", e); + 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 { + /// The network we should run on. network: Network, + /// [`tracing`] config. + tracing: TracingConfig, + + /// The P2P network config. p2p: P2PConfig, + /// The Storage config storage: StorageConfig, } impl Config { - pub fn network(&self) -> Network { + /// 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) -> Result { + 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() + ); + })?) + } + + /// 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 { 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(), } } - pub fn context_config(&self) -> ContextConfig { + /// The [`ContextConfig`]. + pub const fn context_config(&self) -> ContextConfig { match self.network { Network::Mainnet => ContextConfig::main_net(), Network::Stagenet => ContextConfig::stage_net(), @@ -54,17 +126,20 @@ impl Config { } } + /// The [`cuprate_blockchain`] config. pub fn blockchain_config(&self) -> cuprate_blockchain::config::Config { - self.storage.blockchain.clone() + 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() } - pub fn block_downloader_config(&self) -> BlockDownloaderConfig { - BlockDownloaderConfig { - buffer_size: 50_000_000, - in_progress_queue_size: 50_000_000, - check_client_pool_interval: Duration::from_secs(30), - target_batch_size: 5_000_000, - initial_batch_size: 1, - } + /// The [`BlockDownloaderConfig`]. + pub const fn block_downloader_config(&self) -> BlockDownloaderConfig { + self.p2p.block_downloader } } diff --git a/binaries/cuprated/src/config/Cuprate.toml b/binaries/cuprated/src/config/Cuprate.toml new file mode 100644 index 00000000..aa8031f8 --- /dev/null +++ b/binaries/cuprated/src/config/Cuprate.toml @@ -0,0 +1,75 @@ +# ____ _ +# / ___| _ _ __ _ __ __ _| |_ ___ +# | | | | | | '_ \| '__/ _` | __/ _ \ +# | |__| |_| | |_) | | | (_| | || __/ +# \____\__,_| .__/|_| \__,_|\__\___| +# |_| +# + +## The network to run on, valid values: "Mainnet", "Testnet", "Stagenet". +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 }} + +## 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). +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" diff --git a/binaries/cuprated/src/config/args.rs b/binaries/cuprated/src/config/args.rs new file mode 100644 index 00000000..c0b89694 --- /dev/null +++ b/binaries/cuprated/src/config/args.rs @@ -0,0 +1,45 @@ +use std::{io::Write, path::PathBuf}; + +use clap::builder::TypedValueParser; + +use cuprate_helper::network::Network; + +use crate::config::{default::create_default_config_file, Config, DEFAULT_CONFIG_FILE_NAME}; + +#[derive(clap::Parser, Debug)] +pub struct Args { + /// The network we should run on. + #[arg( + long, + default_value_t = Network::Mainnet, + value_parser = clap::builder::PossibleValuesParser::new(["mainnet", "testnet", "stagenet"]) + .map(|s| s.parse::().unwrap()), + )] + pub network: Network, + /// The amount of outbound clear-net connections to maintain. + pub outbound_connections: Option, + /// The location of the Cuprate config file. + pub config_file: Option, + /// Generate a config file and place it in the given folder. + pub generate_config: Option, +} + +impl Args { + /// Apply the [`Args`] to the given [`Config`]. + /// + /// This may exit the program if a config value was set that requires an early exit. + pub fn apply_args(&self, mut config: Config) -> Config { + if let Some(config_folder) = self.generate_config.as_ref() { + // This will create the config file and exit. + create_default_config_file(config_folder) + }; + + config.network = self.network; + + if let Some(outbound_connections) = self.outbound_connections { + config.p2p.clear_net.general.outbound_connections = outbound_connections; + } + + config + } +} diff --git a/binaries/cuprated/src/config/default.rs b/binaries/cuprated/src/config/default.rs new file mode 100644 index 00000000..d1438c89 --- /dev/null +++ b/binaries/cuprated/src/config/default.rs @@ -0,0 +1,69 @@ +use std::{ + io::Write, + path::{Path, PathBuf}, +}; + +use cuprate_helper::fs::{CUPRATE_BLOCKCHAIN_DIR, CUPRATE_CACHE_DIR, CUPRATE_TXPOOL_DIR}; + +use crate::config::DEFAULT_CONFIG_FILE_NAME; + +/// Creates a config file which will be named [`DEFAULT_CONFIG_FILE_NAME`] in the directory given in [`Path`]. +/// +/// This will always terminate the program, on success and failure. +pub fn create_default_config_file(path: &Path) -> ! { + let config_file = path.join(DEFAULT_CONFIG_FILE_NAME); + + tracing::info!("Attempting to create new config file here: {config_file:?}"); + + let mut file = match std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&config_file) + { + Ok(file) => file, + Err(e) => { + tracing::error!("Failed to create config file, got error: {e}"); + std::process::exit(1); + } + }; + + let config = generate_config_text(); + file.write_all(config.as_bytes()).unwrap(); + + std::process::exit(0); +} + +/// Generates the text of the default config file. +fn generate_config_text() -> String { + format!( + include_str!("Cuprate.toml"), + cache = CUPRATE_CACHE_DIR.to_string_lossy(), + txpool = CUPRATE_TXPOOL_DIR.to_string_lossy(), + blockchain = CUPRATE_BLOCKCHAIN_DIR.to_string_lossy() + ) +} + +#[cfg(test)] +mod tests { + use crate::config::{default::generate_config_text, Config}; + + #[test] + fn generate_config_text_covers_all_values() { + let text = generate_config_text(); + + let table: toml::Table = toml::from_str(&text).unwrap(); + + let full_config = Config::default(); + let full_config_table: toml::Table = + toml::from_str(&toml::to_string(&full_config).unwrap()).unwrap(); + + assert_eq!(full_config_table, table); + } + + #[test] + fn generate_config_text_is_valid() { + let text = generate_config_text(); + + let config: Config = toml::from_str(&text).unwrap(); + } +} diff --git a/binaries/cuprated/src/config/p2p.rs b/binaries/cuprated/src/config/p2p.rs index f89b2460..64b5c353 100644 --- a/binaries/cuprated/src/config/p2p.rs +++ b/binaries/cuprated/src/config/p2p.rs @@ -1,21 +1,33 @@ -use cuprate_address_book::AddressBookConfig; -use cuprate_p2p_core::ClearNetServerCfg; +use std::net::SocketAddr; + use serde::{Deserialize, Serialize}; +use cuprate_address_book::AddressBookConfig; +use cuprate_helper::network::Network; +use cuprate_p2p::block_downloader::BlockDownloaderConfig; +use cuprate_p2p_core::ClearNetServerCfg; + +/// P2P config. #[derive(Default, Deserialize, Serialize)] #[serde(deny_unknown_fields, default)] pub struct P2PConfig { + /// Clear-net config. pub clear_net: ClearNetConfig, + /// Block downloader config. + pub block_downloader: BlockDownloaderConfig, } +/// The config values for P2P clear-net. #[derive(Default, Deserialize, Serialize)] #[serde(deny_unknown_fields, default)] pub struct ClearNetConfig { + /// The server config. pub server: ClearNetServerCfg, #[serde(flatten)] pub general: SharedNetConfig, } +/// Network config values shared between all network zones. #[derive(Deserialize, Serialize)] #[serde(deny_unknown_fields, default)] pub struct SharedNetConfig { @@ -25,6 +37,7 @@ pub struct SharedNetConfig { pub extra_outbound_connections: usize, /// The maximum amount of inbound connections pub max_inbound_connections: usize, + /// The percent of connections that should be to peers we haven't connected to before. pub gray_peers_percent: f64, /// port to use to accept p2p connections. pub p2p_port: u16, @@ -39,8 +52,46 @@ impl Default for SharedNetConfig { extra_outbound_connections: 8, max_inbound_connections: 128, gray_peers_percent: 0.7, - p2p_port: 18080, + p2p_port: 0, address_book_config: AddressBookConfig::default(), } } } + +/// Seed nodes for [`ClearNet`](cuprate_p2p_core::ClearNet). +pub fn clear_net_seed_nodes(network: Network) -> Vec { + let seeds = match network { + Network::Mainnet => [ + "176.9.0.187:18080", + "88.198.163.90:18080", + "66.85.74.134:18080", + "51.79.173.165:18080", + "192.99.8.110:18080", + "37.187.74.171:18080", + "77.172.183.193:18080", + ] + .as_slice(), + Network::Stagenet => [ + "176.9.0.187:38080", + "51.79.173.165:38080", + "192.99.8.110:38080", + "37.187.74.171:38080", + "77.172.183.193:38080", + ] + .as_slice(), + Network::Testnet => [ + "176.9.0.187:28080", + "51.79.173.165:28080", + "192.99.8.110:28080", + "37.187.74.171:28080", + "77.172.183.193:28080", + ] + .as_slice(), + }; + + seeds + .iter() + .map(|&s| str::parse(s)) + .collect::>() + .unwrap() +} diff --git a/binaries/cuprated/src/config/storage.rs b/binaries/cuprated/src/config/storage.rs index de913949..ba86c48e 100644 --- a/binaries/cuprated/src/config/storage.rs +++ b/binaries/cuprated/src/config/storage.rs @@ -1,8 +1,70 @@ use serde::{Deserialize, Serialize}; +use cuprate_database::config::SyncMode; +use cuprate_database_service::ReaderThreads; +use cuprate_helper::fs::{CUPRATE_BLOCKCHAIN_DIR, CUPRATE_TXPOOL_DIR}; + +/// The storage config. #[derive(Default, Deserialize, Serialize)] #[serde(deny_unknown_fields, default)] pub struct StorageConfig { - pub blockchain: cuprate_blockchain::config::Config, - pub txpool: cuprate_txpool::Config, + /// The amount of reader threads to spawn between the tx-pool and blockchain. + pub reader_threads: ReaderThreads, + /// The tx-pool config. + pub txpool: TxpoolConfig, + /// The blockchain config. + pub blockchain: BlockchainConfig, +} + +/// The blockchain config. +#[derive(Deserialize, Serialize)] +#[serde(deny_unknown_fields, default)] +pub struct BlockchainConfig { + #[serde(flatten)] + pub shared: SharedStorageConfig, +} + +impl Default for BlockchainConfig { + fn default() -> Self { + Self { + shared: SharedStorageConfig { + path: CUPRATE_BLOCKCHAIN_DIR.to_path_buf(), + sync_mode: SyncMode::Async, + }, + } + } +} + +/// The tx-pool config. +#[derive(Deserialize, Serialize)] +#[serde(deny_unknown_fields, default)] +pub struct TxpoolConfig { + #[serde(flatten)] + pub shared: SharedStorageConfig, + + /// The maximum size of the tx-pool (bytes). + pub max_txpool_size: usize, +} + +impl Default for TxpoolConfig { + fn default() -> Self { + Self { + shared: SharedStorageConfig { + path: CUPRATE_TXPOOL_DIR.to_path_buf(), + sync_mode: SyncMode::Async, + }, + max_txpool_size: 100_000_000, + } + } +} + +/// Config values shared between the tx-pool and blockchain. +#[derive(Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct SharedStorageConfig { + /// The path to the database storage. + pub path: std::path::PathBuf, + /// The [`SyncMode`] of the database. + #[serde(default)] + pub sync_mode: SyncMode, } diff --git a/binaries/cuprated/src/config/tracing_config.rs b/binaries/cuprated/src/config/tracing_config.rs new file mode 100644 index 00000000..859d516a --- /dev/null +++ b/binaries/cuprated/src/config/tracing_config.rs @@ -0,0 +1,42 @@ +use serde::{Deserialize, Serialize}; +use tracing::level_filters::LevelFilter; + +/// [`tracing`] config. +#[derive(Deserialize, Serialize)] +#[serde(deny_unknown_fields, default)] +pub struct TracingConfig { + /// The default minimum log level. + #[serde(with = "level_filter_serde")] + level: LevelFilter, +} + +impl Default for TracingConfig { + fn default() -> Self { + Self { + level: LevelFilter::INFO, + } + } +} + +mod level_filter_serde { + use std::str::FromStr; + + use serde::{Deserialize, Deserializer, Serializer}; + use tracing::level_filters::LevelFilter; + + #[expect(clippy::trivially_copy_pass_by_ref, reason = "serde")] + pub fn serialize(level_filter: &LevelFilter, s: S) -> Result + where + S: Serializer, + { + s.serialize_str(&level_filter.to_string()) + } + + pub fn deserialize<'de, D>(d: D) -> Result + where + D: Deserializer<'de>, + { + let s = String::deserialize(d)?; + LevelFilter::from_str(&s).map_err(serde::de::Error::custom) + } +} diff --git a/binaries/cuprated/src/main.rs b/binaries/cuprated/src/main.rs index d3fe1f56..51d3bce6 100644 --- a/binaries/cuprated/src/main.rs +++ b/binaries/cuprated/src/main.rs @@ -25,6 +25,8 @@ fn main() { // Initialize global static `LazyLock` data. statics::init_lazylock_statics(); + let _config = config::read_config_and_args(); + // TODO: everything else. todo!() } diff --git a/helper/src/fs.rs b/helper/src/fs.rs index 5d62a644..a145ea0e 100644 --- a/helper/src/fs.rs +++ b/helper/src/fs.rs @@ -28,6 +28,8 @@ //! - //---------------------------------------------------------------------------------------------------- Use +use crate::network::Network; +use std::path::Path; use std::{path::PathBuf, sync::LazyLock}; //---------------------------------------------------------------------------------------------------- Const @@ -178,6 +180,10 @@ impl_path_lazylock! { "txpool", } +pub fn path_with_network(path: &Path, network: Network) -> PathBuf { + path.join(network.to_string()) +} + //---------------------------------------------------------------------------------------------------- Tests #[cfg(test)] mod test { diff --git a/helper/src/network.rs b/helper/src/network.rs index 02574752..6d7740b0 100644 --- a/helper/src/network.rs +++ b/helper/src/network.rs @@ -7,6 +7,9 @@ //! `#[no_std]` compatible. // TODO: move to types crate. +use std::fmt::{Display, Formatter}; +use std::str::FromStr; + const MAINNET_NETWORK_ID: [u8; 16] = [ 0x12, 0x30, 0xF1, 0x71, 0x61, 0x04, 0x41, 0x61, 0x17, 0x31, 0x00, 0x82, 0x16, 0xA1, 0xA1, 0x10, ]; @@ -18,7 +21,7 @@ const STAGENET_NETWORK_ID: [u8; 16] = [ ]; /// An enum representing every Monero network. -#[derive(Debug, Clone, Copy, Default)] +#[derive(Debug, Clone, Copy, Default, Ord, PartialOrd, Eq, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub enum Network { /// Mainnet @@ -40,3 +43,29 @@ impl Network { } } } + +#[derive(Debug, PartialEq, Eq)] +pub struct ParseNetworkError; + +impl FromStr for Network { + type Err = ParseNetworkError; + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "mainnet" => Ok(Self::Mainnet), + "testnet" => Ok(Self::Testnet), + "stagenet" => Ok(Self::Stagenet), + _ => Err(ParseNetworkError), + } + } +} + +impl Display for Network { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Self::Mainnet => "mainnet", + Self::Testnet => "testnet", + Self::Stagenet => "stagenet", + }) + } +} diff --git a/net/wire/src/p2p/common.rs b/net/wire/src/p2p/common.rs index d95a6203..5beb26bd 100644 --- a/net/wire/src/p2p/common.rs +++ b/net/wire/src/p2p/common.rs @@ -51,6 +51,8 @@ impl<'a> From<&'a PeerSupportFlags> for &'a u32 { } } +//15515542498767257178 + /// Basic Node Data, information on the connected peer #[derive(Debug, Clone, PartialEq, Eq)] pub struct BasicNodeData { diff --git a/p2p/p2p-core/src/network_zones/clear.rs b/p2p/p2p-core/src/network_zones/clear.rs index ecc6c0ef..bff7b1ed 100644 --- a/p2p/p2p-core/src/network_zones/clear.rs +++ b/p2p/p2p-core/src/network_zones/clear.rs @@ -1,5 +1,5 @@ use std::{ - net::{IpAddr, SocketAddr}, + net::{IpAddr, Ipv4Addr, SocketAddr}, pin::Pin, task::{Context, Poll}, }; diff --git a/p2p/p2p/Cargo.toml b/p2p/p2p/Cargo.toml index 3444b5ef..41dda2e2 100644 --- a/p2p/p2p/Cargo.toml +++ b/p2p/p2p/Cargo.toml @@ -34,6 +34,8 @@ rand_distr = { workspace = true, features = ["std"] } tracing = { workspace = true, features = ["std", "attributes"] } borsh = { workspace = true, features = ["derive", "std"] } +serde = { workspace = true, features = ["std", "derive"], optional = true } + [dev-dependencies] cuprate-test-utils = { path = "../../test-utils" } indexmap = { workspace = true } diff --git a/p2p/p2p/src/block_downloader.rs b/p2p/p2p/src/block_downloader.rs index fcc9eb65..d5d7fcce 100644 --- a/p2p/p2p/src/block_downloader.rs +++ b/p2p/p2p/src/block_downloader.rs @@ -59,6 +59,8 @@ pub struct BlockBatch { /// The block downloader config. #[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(deny_unknown_fields, default))] pub struct BlockDownloaderConfig { /// The size in bytes of the buffer between the block downloader and the place which /// is consuming the downloaded blocks. @@ -70,7 +72,19 @@ pub struct BlockDownloaderConfig { /// The target size of a single batch of blocks (in bytes). pub target_batch_size: usize, /// The initial amount of blocks to request (in number of blocks) - pub initial_batch_size: usize, + pub initial_batch_len: usize, +} + +impl Default for BlockDownloaderConfig { + fn default() -> Self { + Self { + buffer_size: 50_000_000, + in_progress_queue_size: 50_000_000, + check_client_pool_interval: Duration::from_secs(30), + target_batch_size: 5_000_000, + initial_batch_len: 1, + } + } } /// An error that occurred in the [`BlockDownloader`]. @@ -243,7 +257,7 @@ where Self { client_pool, our_chain_svc, - amount_of_blocks_to_request: config.initial_batch_size, + amount_of_blocks_to_request: config.initial_batch_len, amount_of_blocks_to_request_updated_at: 0, amount_of_empty_chain_entries: 0, block_download_tasks: JoinSet::new(), diff --git a/p2p/p2p/src/block_downloader/tests.rs b/p2p/p2p/src/block_downloader/tests.rs index 83dd417c..dd07cce9 100644 --- a/p2p/p2p/src/block_downloader/tests.rs +++ b/p2p/p2p/src/block_downloader/tests.rs @@ -69,7 +69,7 @@ proptest! { in_progress_queue_size: 10_000, check_client_pool_interval: Duration::from_secs(5), target_batch_size: 5_000, - initial_batch_size: 1, + initial_batch_len: 1, }); let blocks = stream.map(|blocks| blocks.blocks).concat().await; diff --git a/storage/blockchain/Cargo.toml b/storage/blockchain/Cargo.toml index 00579110..e7ff955b 100644 --- a/storage/blockchain/Cargo.toml +++ b/storage/blockchain/Cargo.toml @@ -16,6 +16,7 @@ heed = ["cuprate-database/heed"] redb = ["cuprate-database/redb"] redb-memory = ["cuprate-database/redb-memory"] service = ["dep:thread_local", "dep:rayon", "cuprate-helper/thread"] +serde = ["dep:serde", "cuprate-helper/serde", "cuprate-database-service/serde", "cuprate-database/serde"] [dependencies] cuprate-database = { path = "../database" } diff --git a/storage/blockchain/src/config.rs b/storage/blockchain/src/config.rs index e4b76068..e4b2f4ca 100644 --- a/storage/blockchain/src/config.rs +++ b/storage/blockchain/src/config.rs @@ -41,16 +41,17 @@ //! ``` //---------------------------------------------------------------------------------------------------- Import -use std::{borrow::Cow, path::Path}; +use std::{borrow::Cow, path::PathBuf}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use cuprate_database::{config::SyncMode, resize::ResizeAlgorithm}; -use cuprate_helper::fs::CUPRATE_BLOCKCHAIN_DIR; +use cuprate_helper::fs::{path_with_network, CUPRATE_BLOCKCHAIN_DIR}; // re-exports pub use cuprate_database_service::ReaderThreads; +use cuprate_helper::network::Network; //---------------------------------------------------------------------------------------------------- ConfigBuilder /// Builder for [`Config`]. @@ -59,8 +60,10 @@ pub use cuprate_database_service::ReaderThreads; #[derive(Debug, Clone, PartialEq, PartialOrd)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ConfigBuilder { + network: Network, + /// [`Config::db_directory`]. - db_directory: Option>, + db_directory: Option, /// [`Config::cuprate_database_config`]. db_config: cuprate_database::config::ConfigBuilder, @@ -76,10 +79,12 @@ impl ConfigBuilder { /// after this function to use default values. pub fn new() -> Self { Self { + network: Network::default(), db_directory: None, - db_config: cuprate_database::config::ConfigBuilder::new(Cow::Borrowed( - &*CUPRATE_BLOCKCHAIN_DIR, - )), + db_config: cuprate_database::config::ConfigBuilder::new(Cow::Owned(path_with_network( + &CUPRATE_BLOCKCHAIN_DIR, + Network::default(), + ))), reader_threads: None, } } @@ -96,12 +101,12 @@ impl ConfigBuilder { // in `helper::fs`. No need to do them here. let db_directory = self .db_directory - .unwrap_or_else(|| Cow::Borrowed(&*CUPRATE_BLOCKCHAIN_DIR)); + .unwrap_or_else(|| CUPRATE_BLOCKCHAIN_DIR.to_path_buf()); let reader_threads = self.reader_threads.unwrap_or_default(); let db_config = self .db_config - .db_directory(db_directory) + .db_directory(Cow::Owned(path_with_network(&db_directory, self.network))) .reader_threads(reader_threads.as_threads()) .build(); @@ -111,9 +116,16 @@ impl ConfigBuilder { } } + /// Change the network this blockchain database is for. + #[must_use] + pub const fn network(mut self, network: Network) -> Self { + self.network = network; + self + } + /// Set a custom database directory (and file) [`Path`]. #[must_use] - pub fn db_directory(mut self, db_directory: Cow<'static, Path>) -> Self { + pub fn db_directory(mut self, db_directory: PathBuf) -> Self { self.db_directory = Some(db_directory); self } @@ -170,10 +182,13 @@ impl ConfigBuilder { impl Default for ConfigBuilder { fn default() -> Self { - let db_directory = Cow::Borrowed(&**CUPRATE_BLOCKCHAIN_DIR); Self { - db_directory: Some(db_directory.clone()), - db_config: cuprate_database::config::ConfigBuilder::new(db_directory), + network: Network::default(), + db_directory: Some(CUPRATE_BLOCKCHAIN_DIR.to_path_buf()), + db_config: cuprate_database::config::ConfigBuilder::new(Cow::Owned(path_with_network( + &CUPRATE_BLOCKCHAIN_DIR, + Network::default(), + ))), reader_threads: Some(ReaderThreads::default()), } } diff --git a/storage/txpool/Cargo.toml b/storage/txpool/Cargo.toml index 70211d9e..21ac915d 100644 --- a/storage/txpool/Cargo.toml +++ b/storage/txpool/Cargo.toml @@ -16,7 +16,7 @@ heed = ["cuprate-database/heed"] redb = ["cuprate-database/redb"] redb-memory = ["cuprate-database/redb-memory"] service = ["dep:tower", "dep:rayon", "dep:cuprate-database-service"] -serde = ["dep:serde", "cuprate-database/serde", "cuprate-database-service/serde"] +serde = ["dep:serde", "cuprate-helper/serde", "cuprate-database-service/serde", "cuprate-database/serde"] [dependencies] cuprate-database = { path = "../database", features = ["heed"] } diff --git a/storage/txpool/src/config.rs b/storage/txpool/src/config.rs index 1ef0d734..5aa7334d 100644 --- a/storage/txpool/src/config.rs +++ b/storage/txpool/src/config.rs @@ -1,15 +1,18 @@ //! The transaction pool [`Config`]. -use std::{borrow::Cow, path::Path}; +use std::{borrow::Cow, path::PathBuf}; + +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; use cuprate_database::{ config::{Config as DbConfig, SyncMode}, resize::ResizeAlgorithm, }; use cuprate_database_service::ReaderThreads; -use cuprate_helper::fs::CUPRATE_TXPOOL_DIR; - -#[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; +use cuprate_helper::{ + fs::{path_with_network, CUPRATE_TXPOOL_DIR}, + network::Network, +}; /// The default transaction pool weight limit. const DEFAULT_TXPOOL_WEIGHT_LIMIT: usize = 600 * 1024 * 1024; @@ -21,8 +24,10 @@ const DEFAULT_TXPOOL_WEIGHT_LIMIT: usize = 600 * 1024 * 1024; #[derive(Debug, Clone, PartialEq, PartialOrd)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ConfigBuilder { + network: Network, + /// [`Config::db_directory`]. - db_directory: Option>, + db_directory: Option, /// [`Config::cuprate_database_config`]. db_config: cuprate_database::config::ConfigBuilder, @@ -41,10 +46,12 @@ impl ConfigBuilder { /// after this function to use default values. pub fn new() -> Self { Self { + network: Network::default(), db_directory: None, - db_config: cuprate_database::config::ConfigBuilder::new(Cow::Borrowed( - &*CUPRATE_TXPOOL_DIR, - )), + db_config: cuprate_database::config::ConfigBuilder::new(Cow::Owned(path_with_network( + &CUPRATE_TXPOOL_DIR, + Network::default(), + ))), reader_threads: None, max_txpool_weight: None, } @@ -62,7 +69,7 @@ impl ConfigBuilder { // in `helper::fs`. No need to do them here. let db_directory = self .db_directory - .unwrap_or_else(|| Cow::Borrowed(&*CUPRATE_TXPOOL_DIR)); + .unwrap_or_else(|| CUPRATE_TXPOOL_DIR.to_path_buf()); let reader_threads = self.reader_threads.unwrap_or_default(); @@ -72,7 +79,7 @@ impl ConfigBuilder { let db_config = self .db_config - .db_directory(db_directory) + .db_directory(Cow::Owned(path_with_network(&db_directory, self.network))) .reader_threads(reader_threads.as_threads()) .build(); @@ -83,6 +90,13 @@ impl ConfigBuilder { } } + /// Change the network this blockchain database is for. + #[must_use] + pub const fn network(mut self, network: Network) -> Self { + self.network = network; + self + } + /// Sets a new maximum weight for the transaction pool. #[must_use] pub const fn max_txpool_weight(mut self, max_txpool_weight: usize) -> Self { @@ -92,7 +106,7 @@ impl ConfigBuilder { /// Set a custom database directory (and file) [`Path`]. #[must_use] - pub fn db_directory(mut self, db_directory: Cow<'static, Path>) -> Self { + pub fn db_directory(mut self, db_directory: PathBuf) -> Self { self.db_directory = Some(db_directory); self } @@ -149,10 +163,13 @@ impl ConfigBuilder { impl Default for ConfigBuilder { fn default() -> Self { - let db_directory = Cow::Borrowed(CUPRATE_TXPOOL_DIR.as_path()); Self { - db_directory: Some(db_directory.clone()), - db_config: cuprate_database::config::ConfigBuilder::new(db_directory), + network: Network::default(), + db_directory: Some(CUPRATE_TXPOOL_DIR.to_path_buf()), + db_config: cuprate_database::config::ConfigBuilder::new(Cow::Owned(path_with_network( + &CUPRATE_TXPOOL_DIR, + Network::default(), + ))), reader_threads: Some(ReaderThreads::default()), max_txpool_weight: Some(DEFAULT_TXPOOL_WEIGHT_LIMIT), } From 3dc00494814c0f097e2c8a022f0dee0bb9a00480 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sat, 26 Oct 2024 20:46:45 +0100 Subject: [PATCH 04/22] fix clap --- Cargo.lock | 11 +++++++++++ binaries/cuprated/Cargo.toml | 4 ++-- binaries/cuprated/src/config/args.rs | 5 +++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9ee48eb1..20ee1fa3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -363,6 +363,7 @@ checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73" dependencies = [ "anstyle", "clap_lex", + "terminal_size", ] [[package]] @@ -2812,6 +2813,16 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "terminal_size" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" +dependencies = [ + "rustix", + "windows-sys 0.48.0", +] + [[package]] name = "thiserror" version = "1.0.63" diff --git a/binaries/cuprated/Cargo.toml b/binaries/cuprated/Cargo.toml index eaaba402..0521b1fd 100644 --- a/binaries/cuprated/Cargo.toml +++ b/binaries/cuprated/Cargo.toml @@ -2,7 +2,7 @@ name = "cuprated" version = "0.0.1" edition = "2021" -description = "The Cuprate Monero Rust node." +description = "The Cuprate Rust Monero node." license = "AGPL-3.0-only" authors = ["Boog900", "hinto-janai", "SyntheticBird45"] repository = "https://github.com/Cuprate/cuprate/tree/main/binaries/cuprated" @@ -43,7 +43,7 @@ borsh = { workspace = true } bytemuck = { workspace = true } bytes = { workspace = true } cfg-if = { workspace = true } -clap = { workspace = true, features = ["cargo"] } +clap = { workspace = true, features = ["cargo", "help", "wrap_help"] } chrono = { workspace = true } crypto-bigint = { workspace = true } crossbeam = { workspace = true } diff --git a/binaries/cuprated/src/config/args.rs b/binaries/cuprated/src/config/args.rs index c0b89694..0e397417 100644 --- a/binaries/cuprated/src/config/args.rs +++ b/binaries/cuprated/src/config/args.rs @@ -6,7 +6,9 @@ use cuprate_helper::network::Network; use crate::config::{default::create_default_config_file, Config, DEFAULT_CONFIG_FILE_NAME}; +/// Cuprate Args. #[derive(clap::Parser, Debug)] +#[command(version, about)] pub struct Args { /// The network we should run on. #[arg( @@ -17,10 +19,13 @@ pub struct Args { )] pub network: Network, /// The amount of outbound clear-net connections to maintain. + #[arg(long)] pub outbound_connections: Option, /// The location of the Cuprate config file. + #[arg(long)] pub config_file: Option, /// Generate a config file and place it in the given folder. + #[arg(long)] pub generate_config: Option, } From d1f3eb40bf7b50eb31f9b49736b1bf282900ddbc Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sat, 26 Oct 2024 21:21:01 +0100 Subject: [PATCH 05/22] misc changes --- Cargo.toml | 2 ++ binaries/cuprated/Cargo.toml | 2 +- binaries/cuprated/src/config.rs | 2 +- binaries/cuprated/src/config/p2p.rs | 15 ++++++++++++++- helper/src/fs.rs | 8 ++++++-- helper/src/network.rs | 7 ++++--- net/wire/src/p2p/common.rs | 2 -- storage/blockchain/src/config.rs | 6 ++++-- 8 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 38658632..9865e168 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ members = [ ] [profile.release] +panic = "abort" lto = true # Build with LTO strip = "none" # Keep panic stack traces codegen-units = 1 # Optimize for binary speed over compile times @@ -115,6 +116,7 @@ tokio-util = { version = "0.7.12", default-features = false } tokio-stream = { version = "0.1.16", default-features = false } tokio = { version = "1.40.0", default-features = false } tower = { git = "https://github.com/Cuprate/tower.git", rev = "6c7faf0", default-features = false } # +toml = { version = "0.8", default-features = false } tracing-subscriber = { version = "0.3.18", default-features = false } tracing = { version = "0.1.40", default-features = false } diff --git a/binaries/cuprated/Cargo.toml b/binaries/cuprated/Cargo.toml index 0521b1fd..3de2f151 100644 --- a/binaries/cuprated/Cargo.toml +++ b/binaries/cuprated/Cargo.toml @@ -70,10 +70,10 @@ thread_local = { workspace = true } tokio-util = { workspace = true } tokio-stream = { workspace = true } tokio = { workspace = true } +toml = { workspace = true, features = ["parse"]} tower = { workspace = true } tracing-subscriber = { workspace = true, features = ["std", "fmt", "default"] } tracing = { workspace = true, features = ["default"] } -toml = "0.8" [lints] workspace = true diff --git a/binaries/cuprated/src/config.rs b/binaries/cuprated/src/config.rs index 290f5c27..b553c078 100644 --- a/binaries/cuprated/src/config.rs +++ b/binaries/cuprated/src/config.rs @@ -113,7 +113,7 @@ impl Config { 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(), + address_book_config: self.p2p.clear_net.general.address_book_config(self.network), } } diff --git a/binaries/cuprated/src/config/p2p.rs b/binaries/cuprated/src/config/p2p.rs index 64b5c353..81e5b5fa 100644 --- a/binaries/cuprated/src/config/p2p.rs +++ b/binaries/cuprated/src/config/p2p.rs @@ -42,7 +42,20 @@ pub struct SharedNetConfig { /// port to use to accept p2p connections. pub p2p_port: u16, /// The address book config. - pub address_book_config: AddressBookConfig, + address_book_config: AddressBookConfig, +} + +impl SharedNetConfig { + /// Returns the [`AddressBookConfig`]. + pub fn address_book_config(&self, network: Network) -> AddressBookConfig { + // 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 + .push(network.to_string()); + + address_book_config + } } impl Default for SharedNetConfig { diff --git a/helper/src/fs.rs b/helper/src/fs.rs index a145ea0e..7abde893 100644 --- a/helper/src/fs.rs +++ b/helper/src/fs.rs @@ -28,9 +28,12 @@ //! - //---------------------------------------------------------------------------------------------------- Use +use std::{ + path::{Path, PathBuf}, + sync::LazyLock, +}; + use crate::network::Network; -use std::path::Path; -use std::{path::PathBuf, sync::LazyLock}; //---------------------------------------------------------------------------------------------------- Const /// Cuprate's main directory. @@ -180,6 +183,7 @@ impl_path_lazylock! { "txpool", } +/// Joins the [`Path`] with a folder for the given [`Network`]. pub fn path_with_network(path: &Path, network: Network) -> PathBuf { path.join(network.to_string()) } diff --git a/helper/src/network.rs b/helper/src/network.rs index 6d7740b0..2836781d 100644 --- a/helper/src/network.rs +++ b/helper/src/network.rs @@ -6,9 +6,10 @@ //! //! `#[no_std]` compatible. // TODO: move to types crate. - -use std::fmt::{Display, Formatter}; -use std::str::FromStr; +use std::{ + fmt::{Display, Formatter}, + str::FromStr, +}; const MAINNET_NETWORK_ID: [u8; 16] = [ 0x12, 0x30, 0xF1, 0x71, 0x61, 0x04, 0x41, 0x61, 0x17, 0x31, 0x00, 0x82, 0x16, 0xA1, 0xA1, 0x10, diff --git a/net/wire/src/p2p/common.rs b/net/wire/src/p2p/common.rs index 5beb26bd..d95a6203 100644 --- a/net/wire/src/p2p/common.rs +++ b/net/wire/src/p2p/common.rs @@ -51,8 +51,6 @@ impl<'a> From<&'a PeerSupportFlags> for &'a u32 { } } -//15515542498767257178 - /// Basic Node Data, information on the connected peer #[derive(Debug, Clone, PartialEq, Eq)] pub struct BasicNodeData { diff --git a/storage/blockchain/src/config.rs b/storage/blockchain/src/config.rs index e4b2f4ca..2bd35e0e 100644 --- a/storage/blockchain/src/config.rs +++ b/storage/blockchain/src/config.rs @@ -47,11 +47,13 @@ use std::{borrow::Cow, path::PathBuf}; use serde::{Deserialize, Serialize}; use cuprate_database::{config::SyncMode, resize::ResizeAlgorithm}; -use cuprate_helper::fs::{path_with_network, CUPRATE_BLOCKCHAIN_DIR}; +use cuprate_helper::{ + fs::{path_with_network, CUPRATE_BLOCKCHAIN_DIR}, + network::Network, +}; // re-exports pub use cuprate_database_service::ReaderThreads; -use cuprate_helper::network::Network; //---------------------------------------------------------------------------------------------------- ConfigBuilder /// Builder for [`Config`]. From d7d7541dae0af7763b309874e56b715774bc42df Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sat, 26 Oct 2024 21:25:38 +0100 Subject: [PATCH 06/22] fix doc --- storage/blockchain/src/config.rs | 2 +- storage/txpool/src/config.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/storage/blockchain/src/config.rs b/storage/blockchain/src/config.rs index 2bd35e0e..68736219 100644 --- a/storage/blockchain/src/config.rs +++ b/storage/blockchain/src/config.rs @@ -125,7 +125,7 @@ impl ConfigBuilder { self } - /// Set a custom database directory (and file) [`Path`]. + /// Set a custom database directory (and file) [`PathBuf`]. #[must_use] pub fn db_directory(mut self, db_directory: PathBuf) -> Self { self.db_directory = Some(db_directory); diff --git a/storage/txpool/src/config.rs b/storage/txpool/src/config.rs index 5aa7334d..e45a2be4 100644 --- a/storage/txpool/src/config.rs +++ b/storage/txpool/src/config.rs @@ -104,7 +104,7 @@ impl ConfigBuilder { self } - /// Set a custom database directory (and file) [`Path`]. + /// Set a custom database directory (and file) [`PathBuf`]. #[must_use] pub fn db_directory(mut self, db_directory: PathBuf) -> Self { self.db_directory = Some(db_directory); From cf34a8d2be2d1eabca62b8f0eee93954d20e00ce Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sat, 26 Oct 2024 21:32:47 +0100 Subject: [PATCH 07/22] fix test & clippy --- binaries/cuprated/Cargo.toml | 2 +- p2p/address-book/src/book/tests.rs | 2 +- storage/blockchain/src/service/tests.rs | 3 +-- storage/blockchain/src/tests.rs | 4 ++-- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/binaries/cuprated/Cargo.toml b/binaries/cuprated/Cargo.toml index 3de2f151..f5eff338 100644 --- a/binaries/cuprated/Cargo.toml +++ b/binaries/cuprated/Cargo.toml @@ -70,7 +70,7 @@ thread_local = { workspace = true } tokio-util = { workspace = true } tokio-stream = { workspace = true } tokio = { workspace = true } -toml = { workspace = true, features = ["parse"]} +toml = { workspace = true, features = ["parse", "display"]} tower = { workspace = true } tracing-subscriber = { workspace = true, features = ["std", "fmt", "default"] } tracing = { workspace = true, features = ["default"] } diff --git a/p2p/address-book/src/book/tests.rs b/p2p/address-book/src/book/tests.rs index 216fcfac..33f881ee 100644 --- a/p2p/address-book/src/book/tests.rs +++ b/p2p/address-book/src/book/tests.rs @@ -15,7 +15,7 @@ fn test_cfg() -> AddressBookConfig { AddressBookConfig { max_white_list_length: 100, max_gray_list_length: 500, - peer_store_file: PathBuf::new(), + peer_store_folder: PathBuf::new(), peer_save_period: Duration::from_secs(60), } } diff --git a/storage/blockchain/src/service/tests.rs b/storage/blockchain/src/service/tests.rs index 719f3613..e8a18426 100644 --- a/storage/blockchain/src/service/tests.rs +++ b/storage/blockchain/src/service/tests.rs @@ -7,7 +7,6 @@ //---------------------------------------------------------------------------------------------------- Use use std::{ - borrow::Cow, collections::{HashMap, HashSet}, sync::Arc, }; @@ -46,7 +45,7 @@ fn init_service() -> ( ) { let tempdir = tempfile::tempdir().unwrap(); let config = ConfigBuilder::new() - .db_directory(Cow::Owned(tempdir.path().into())) + .db_directory(tempdir.path().into()) .low_power() .build(); let (reader, writer, env) = init(config).unwrap(); diff --git a/storage/blockchain/src/tests.rs b/storage/blockchain/src/tests.rs index 1fe20637..271a82cc 100644 --- a/storage/blockchain/src/tests.rs +++ b/storage/blockchain/src/tests.rs @@ -5,7 +5,7 @@ //! - only used internally //---------------------------------------------------------------------------------------------------- Import -use std::{borrow::Cow, fmt::Debug}; +use std::fmt::Debug; use pretty_assertions::assert_eq; @@ -74,7 +74,7 @@ impl AssertTableLen { pub(crate) fn tmp_concrete_env() -> (impl Env, tempfile::TempDir) { let tempdir = tempfile::tempdir().unwrap(); let config = ConfigBuilder::new() - .db_directory(Cow::Owned(tempdir.path().into())) + .db_directory(tempdir.path().into()) .low_power() .build(); let env = crate::open(config).unwrap(); From 65223dc1753ab4226c73b02cee8ac20ef4340c45 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sat, 26 Oct 2024 22:30:41 +0100 Subject: [PATCH 08/22] fix test 2 --- storage/blockchain/src/config.rs | 20 +++++++++++--------- storage/txpool/src/config.rs | 12 ++++-------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/storage/blockchain/src/config.rs b/storage/blockchain/src/config.rs index 68736219..2ae94c70 100644 --- a/storage/blockchain/src/config.rs +++ b/storage/blockchain/src/config.rs @@ -159,9 +159,10 @@ impl ConfigBuilder { /// Good default for testing, and resource-available machines. #[must_use] pub fn fast(mut self) -> Self { - self.db_config = - cuprate_database::config::ConfigBuilder::new(Cow::Borrowed(&*CUPRATE_BLOCKCHAIN_DIR)) - .fast(); + self.db_config = cuprate_database::config::ConfigBuilder::new(Cow::Owned( + path_with_network(&CUPRATE_BLOCKCHAIN_DIR, self.network), + )) + .fast(); self.reader_threads = Some(ReaderThreads::OnePerThread); self @@ -173,9 +174,10 @@ impl ConfigBuilder { /// Good default for resource-limited machines, e.g. a cheap VPS. #[must_use] pub fn low_power(mut self) -> Self { - self.db_config = - cuprate_database::config::ConfigBuilder::new(Cow::Borrowed(&*CUPRATE_BLOCKCHAIN_DIR)) - .low_power(); + self.db_config = cuprate_database::config::ConfigBuilder::new(Cow::Owned( + path_with_network(&CUPRATE_BLOCKCHAIN_DIR, self.network), + )) + .low_power(); self.reader_threads = Some(ReaderThreads::One); self @@ -218,7 +220,7 @@ impl Config { /// Create a new [`Config`] with sane default settings. /// /// The [`cuprate_database::config::Config::db_directory`] - /// will be set to [`CUPRATE_BLOCKCHAIN_DIR`]. + /// will be set to [`CUPRATE_BLOCKCHAIN_DIR`] joined with [`Network::Mainnet`]. /// /// All other values will be [`Default::default`]. /// @@ -230,13 +232,13 @@ impl Config { /// resize::ResizeAlgorithm, /// DATABASE_DATA_FILENAME, /// }; - /// use cuprate_helper::fs::*; + /// use cuprate_helper::{fs::*, network::Network}; /// /// use cuprate_blockchain::config::*; /// /// let config = Config::new(); /// - /// assert_eq!(config.db_config.db_directory(), &*CUPRATE_BLOCKCHAIN_DIR); + /// assert_eq!(config.db_config.db_directory().as_ref(), path_with_network(&CUPRATE_BLOCKCHAIN_DIR, Network::Mainnet).as_path()); /// assert!(config.db_config.db_file().starts_with(&*CUPRATE_BLOCKCHAIN_DIR)); /// assert!(config.db_config.db_file().ends_with(DATABASE_DATA_FILENAME)); /// assert_eq!(config.db_config.sync_mode, SyncMode::default()); diff --git a/storage/txpool/src/config.rs b/storage/txpool/src/config.rs index e45a2be4..6e838df6 100644 --- a/storage/txpool/src/config.rs +++ b/storage/txpool/src/config.rs @@ -201,7 +201,7 @@ impl Config { /// Create a new [`Config`] with sane default settings. /// /// The [`DbConfig::db_directory`] - /// will be set to [`CUPRATE_TXPOOL_DIR`]. + /// will be set to [`CUPRATE_TXPOOL_DIR`] joined with [`Network::Mainnet`]. /// /// All other values will be [`Default::default`]. /// @@ -214,13 +214,13 @@ impl Config { /// DATABASE_DATA_FILENAME, /// }; /// use cuprate_database_service::ReaderThreads; - /// use cuprate_helper::fs::*; + /// use cuprate_helper::{fs::*, network::Network}; /// /// use cuprate_txpool::Config; /// /// let config = Config::new(); /// - /// assert_eq!(config.db_config.db_directory(), &*CUPRATE_TXPOOL_DIR); + /// assert_eq!(config.db_config.db_directory(), path_with_network(&CUPRATE_TXPOOL_DIR, Network::Mainnet).as_path()); /// assert!(config.db_config.db_file().starts_with(&*CUPRATE_TXPOOL_DIR)); /// assert!(config.db_config.db_file().ends_with(DATABASE_DATA_FILENAME)); /// assert_eq!(config.db_config.sync_mode, SyncMode::default()); @@ -228,11 +228,7 @@ impl Config { /// assert_eq!(config.reader_threads, ReaderThreads::default()); /// ``` pub fn new() -> Self { - Self { - db_config: DbConfig::new(Cow::Borrowed(&*CUPRATE_TXPOOL_DIR)), - reader_threads: ReaderThreads::default(), - max_txpool_weight: 0, - } + ConfigBuilder::new().build() } } From 2751831a755323ba722c4fedea0ba269a82b6718 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sat, 26 Oct 2024 23:35:11 +0100 Subject: [PATCH 09/22] try fix windows --- binaries/cuprated/src/config/Cuprate.toml | 3 +++ binaries/cuprated/src/config/default.rs | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/binaries/cuprated/src/config/Cuprate.toml b/binaries/cuprated/src/config/Cuprate.toml index aa8031f8..66cc43c2 100644 --- a/binaries/cuprated/src/config/Cuprate.toml +++ b/binaries/cuprated/src/config/Cuprate.toml @@ -1,3 +1,4 @@ +r#" # ____ _ # / ___| _ _ __ _ __ __ _| |_ ___ # | | | | | | '_ \| '__/ _` | __/ _ \ @@ -73,3 +74,5 @@ max_txpool_size = 100_000_000 path = "{blockchain}" ## The database sync mode for the blockchain. sync_mode = "Async" + +"# \ No newline at end of file diff --git a/binaries/cuprated/src/config/default.rs b/binaries/cuprated/src/config/default.rs index d1438c89..87e78bd9 100644 --- a/binaries/cuprated/src/config/default.rs +++ b/binaries/cuprated/src/config/default.rs @@ -1,6 +1,7 @@ use std::{ io::Write, path::{Path, PathBuf}, + str::from_utf8, }; use cuprate_helper::fs::{CUPRATE_BLOCKCHAIN_DIR, CUPRATE_CACHE_DIR, CUPRATE_TXPOOL_DIR}; @@ -36,7 +37,7 @@ pub fn create_default_config_file(path: &Path) -> ! { /// Generates the text of the default config file. fn generate_config_text() -> String { format!( - include_str!("Cuprate.toml"), + include!("Cuprate.toml"), cache = CUPRATE_CACHE_DIR.to_string_lossy(), txpool = CUPRATE_TXPOOL_DIR.to_string_lossy(), blockchain = CUPRATE_BLOCKCHAIN_DIR.to_string_lossy() From 54f0bdee86b14097d5a0a0b88cb95f9f64e4415d Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sun, 27 Oct 2024 01:46:50 +0100 Subject: [PATCH 10/22] testing --- binaries/cuprated/src/config/Cuprate.toml | 3 --- binaries/cuprated/src/config/default.rs | 7 +++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/binaries/cuprated/src/config/Cuprate.toml b/binaries/cuprated/src/config/Cuprate.toml index 66cc43c2..aa8031f8 100644 --- a/binaries/cuprated/src/config/Cuprate.toml +++ b/binaries/cuprated/src/config/Cuprate.toml @@ -1,4 +1,3 @@ -r#" # ____ _ # / ___| _ _ __ _ __ __ _| |_ ___ # | | | | | | '_ \| '__/ _` | __/ _ \ @@ -74,5 +73,3 @@ max_txpool_size = 100_000_000 path = "{blockchain}" ## The database sync mode for the blockchain. sync_mode = "Async" - -"# \ No newline at end of file diff --git a/binaries/cuprated/src/config/default.rs b/binaries/cuprated/src/config/default.rs index 87e78bd9..9afcf051 100644 --- a/binaries/cuprated/src/config/default.rs +++ b/binaries/cuprated/src/config/default.rs @@ -52,6 +52,13 @@ mod tests { fn generate_config_text_covers_all_values() { let text = generate_config_text(); + #[cfg!(target_os = "windows")] + { + let full_config = Config::default(); + panic!(toml::to_string_pretty(&full_config).unwrap()); + } + + let table: toml::Table = toml::from_str(&text).unwrap(); let full_config = Config::default(); From 52305ffa2b85adac46885e3ff135fa47ca0f77b9 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sun, 27 Oct 2024 01:47:57 +0100 Subject: [PATCH 11/22] testing 2 --- binaries/cuprated/src/config/default.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/binaries/cuprated/src/config/default.rs b/binaries/cuprated/src/config/default.rs index 9afcf051..46e357e2 100644 --- a/binaries/cuprated/src/config/default.rs +++ b/binaries/cuprated/src/config/default.rs @@ -37,7 +37,7 @@ pub fn create_default_config_file(path: &Path) -> ! { /// Generates the text of the default config file. fn generate_config_text() -> String { format!( - include!("Cuprate.toml"), + include_str!("Cuprate.toml"), cache = CUPRATE_CACHE_DIR.to_string_lossy(), txpool = CUPRATE_TXPOOL_DIR.to_string_lossy(), blockchain = CUPRATE_BLOCKCHAIN_DIR.to_string_lossy() @@ -52,12 +52,11 @@ mod tests { fn generate_config_text_covers_all_values() { let text = generate_config_text(); - #[cfg!(target_os = "windows")] + #[cfg(target_os = "windows")] { let full_config = Config::default(); panic!(toml::to_string_pretty(&full_config).unwrap()); } - let table: toml::Table = toml::from_str(&text).unwrap(); From fe28c70ad12a63560f250a91bf733feebc0f795b Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sun, 27 Oct 2024 01:01:05 +0000 Subject: [PATCH 12/22] fix windows test --- binaries/cuprated/src/config/default.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/binaries/cuprated/src/config/default.rs b/binaries/cuprated/src/config/default.rs index 46e357e2..33d9b845 100644 --- a/binaries/cuprated/src/config/default.rs +++ b/binaries/cuprated/src/config/default.rs @@ -55,7 +55,7 @@ mod tests { #[cfg(target_os = "windows")] { let full_config = Config::default(); - panic!(toml::to_string_pretty(&full_config).unwrap()); + panic!("{}", toml::to_string_pretty(&full_config).unwrap()); } let table: toml::Table = toml::from_str(&text).unwrap(); From 06b7429f9e2cf71b404b27bb403d7edc3180df5d Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sun, 27 Oct 2024 13:10:30 +0000 Subject: [PATCH 13/22] fix windows: the remix. --- binaries/cuprated/src/config/Cuprate.toml | 6 +++--- binaries/cuprated/src/config/default.rs | 21 +++++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/binaries/cuprated/src/config/Cuprate.toml b/binaries/cuprated/src/config/Cuprate.toml index aa8031f8..0a92a178 100644 --- a/binaries/cuprated/src/config/Cuprate.toml +++ b/binaries/cuprated/src/config/Cuprate.toml @@ -36,7 +36,7 @@ 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}" +peer_store_folder = {cache} ## The amount of time between address book saves. peer_save_period = {{ secs = 90, nanos = 0 }} @@ -61,7 +61,7 @@ reader_threads = "OnePerThread" ## Txpool storage config. [storage.txpool] ## The txpool storage location. -path = "{txpool}" +path = {txpool} ## The database sync mode for the txpool. sync_mode = "Async" ## The maximum size of all the txs in the pool (bytes). @@ -70,6 +70,6 @@ max_txpool_size = 100_000_000 ## Blockchain storage config. [storage.blockchain] ## The blockchain storage location. -path = "{blockchain}" +path = {blockchain} ## The database sync mode for the blockchain. sync_mode = "Async" diff --git a/binaries/cuprated/src/config/default.rs b/binaries/cuprated/src/config/default.rs index 33d9b845..09022ca8 100644 --- a/binaries/cuprated/src/config/default.rs +++ b/binaries/cuprated/src/config/default.rs @@ -36,11 +36,19 @@ pub fn create_default_config_file(path: &Path) -> ! { /// Generates the text of the default config file. fn generate_config_text() -> String { + let toml_value_str = |t: &PathBuf| { + let mut value = String::new(); + + serde::Serialize::serialize(t, toml::ser::ValueSerializer::new(&mut value)).unwrap(); + + value + }; + format!( include_str!("Cuprate.toml"), - cache = CUPRATE_CACHE_DIR.to_string_lossy(), - txpool = CUPRATE_TXPOOL_DIR.to_string_lossy(), - blockchain = CUPRATE_BLOCKCHAIN_DIR.to_string_lossy() + cache = toml_value_str(&CUPRATE_CACHE_DIR), + txpool = toml_value_str(&CUPRATE_TXPOOL_DIR), + blockchain = toml_value_str(&CUPRATE_BLOCKCHAIN_DIR) ) } @@ -51,13 +59,6 @@ mod tests { #[test] fn generate_config_text_covers_all_values() { let text = generate_config_text(); - - #[cfg(target_os = "windows")] - { - let full_config = Config::default(); - panic!("{}", toml::to_string_pretty(&full_config).unwrap()); - } - let table: toml::Table = toml::from_str(&text).unwrap(); let full_config = Config::default(); From 26653d428d5b0995c647e80f750ea6ba4c52fbaa Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Tue, 5 Nov 2024 01:14:36 +0000 Subject: [PATCH 14/22] review comments --- binaries/cuprated/src/config.rs | 22 ++++++++++++---------- binaries/cuprated/src/config/Cuprate.toml | 2 +- binaries/cuprated/src/config/args.rs | 6 +++--- binaries/cuprated/src/config/p2p.rs | 4 ++-- binaries/cuprated/src/config/storage.rs | 4 ++-- helper/src/fs.rs | 3 +++ p2p/address-book/src/book/tests.rs | 2 +- p2p/address-book/src/lib.rs | 6 +++--- p2p/address-book/src/store.rs | 8 ++++++-- storage/txpool/src/config.rs | 2 +- 10 files changed, 34 insertions(+), 25 deletions(-) diff --git a/binaries/cuprated/src/config.rs b/binaries/cuprated/src/config.rs index b553c078..08d38e4c 100644 --- a/binaries/cuprated/src/config.rs +++ b/binaries/cuprated/src/config.rs @@ -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) -> Result { + fn read_from_path(file: impl AsRef) -> Result { 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() diff --git a/binaries/cuprated/src/config/Cuprate.toml b/binaries/cuprated/src/config/Cuprate.toml index 0a92a178..ecc53790 100644 --- a/binaries/cuprated/src/config/Cuprate.toml +++ b/binaries/cuprated/src/config/Cuprate.toml @@ -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 }} diff --git a/binaries/cuprated/src/config/args.rs b/binaries/cuprated/src/config/args.rs index 0e397417..9abb3f13 100644 --- a/binaries/cuprated/src/config/args.rs +++ b/binaries/cuprated/src/config/args.rs @@ -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, - /// The location of the Cuprate config file. + /// The PATH of the `cuprated` config file. #[arg(long)] pub config_file: Option, - /// 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, } diff --git a/binaries/cuprated/src/config/p2p.rs b/binaries/cuprated/src/config/p2p.rs index 81e5b5fa..abd667d3 100644 --- a/binaries/cuprated/src/config/p2p.rs +++ b/binaries/cuprated/src/config/p2p.rs @@ -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 { seeds .iter() - .map(|&s| str::parse(s)) + .map(|s| s.parse()) .collect::>() .unwrap() } diff --git a/binaries/cuprated/src/config/storage.rs b/binaries/cuprated/src/config/storage.rs index ba86c48e..80de2656 100644 --- a/binaries/cuprated/src/config/storage.rs +++ b/binaries/cuprated/src/config/storage.rs @@ -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 { diff --git a/helper/src/fs.rs b/helper/src/fs.rs index 7abde893..cebd0cc4 100644 --- a/helper/src/fs.rs +++ b/helper/src/fs.rs @@ -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. /// diff --git a/p2p/address-book/src/book/tests.rs b/p2p/address-book/src/book/tests.rs index 33f881ee..b2c4c493 100644 --- a/p2p/address-book/src/book/tests.rs +++ b/p2p/address-book/src/book/tests.rs @@ -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), } } diff --git a/p2p/address-book/src/lib.rs b/p2p/address-book/src/lib.rs index 2d7b3d63..04229677 100644 --- a/p2p/address-book/src/lib.rs +++ b/p2p/address-book/src/lib.rs @@ -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), } } diff --git a/p2p/address-book/src/store.rs b/p2p/address-book/src/store.rs index 31277f9e..47994ae5 100644 --- a/p2p/address-book/src/store.rs +++ b/p2p/address-book/src/store.rs @@ -39,7 +39,9 @@ pub(crate) fn save_peers_to_disk( }) .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( ), 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()); diff --git a/storage/txpool/src/config.rs b/storage/txpool/src/config.rs index 6e838df6..69b73aab 100644 --- a/storage/txpool/src/config.rs +++ b/storage/txpool/src/config.rs @@ -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; From 352dac694fd2095592cfc5777d996de437338c01 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Tue, 5 Nov 2024 01:17:32 +0000 Subject: [PATCH 15/22] fix imports --- binaries/cuprated/src/config.rs | 6 +++--- binaries/cuprated/src/config/args.rs | 2 +- binaries/cuprated/src/config/default.rs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/binaries/cuprated/src/config.rs b/binaries/cuprated/src/config.rs index 08d38e4c..794b10bf 100644 --- a/binaries/cuprated/src/config.rs +++ b/binaries/cuprated/src/config.rs @@ -33,7 +33,7 @@ pub fn read_config_and_args() -> Config { let config: Config = if let Some(config_file) = &args.config_file { // 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) { + match Config::read_from_path(config_file) { Ok(config) => config, Err(e) => { tracing::error!("Failed to read config from file: {e}"); @@ -44,12 +44,12 @@ pub fn read_config_and_args() -> Config { // 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) + .and_then(Config::read_from_path) .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) + Config::read_from_path(file) }) .inspect_err(|e| { tracing::debug!("Failed to read config from config dir: {e}"); diff --git a/binaries/cuprated/src/config/args.rs b/binaries/cuprated/src/config/args.rs index 9abb3f13..c2cd6233 100644 --- a/binaries/cuprated/src/config/args.rs +++ b/binaries/cuprated/src/config/args.rs @@ -4,7 +4,7 @@ use clap::builder::TypedValueParser; use cuprate_helper::network::Network; -use crate::config::{default::create_default_config_file, Config, DEFAULT_CONFIG_FILE_NAME}; +use crate::config::{default::create_default_config_file, Config}; /// Cuprate Args. #[derive(clap::Parser, Debug)] diff --git a/binaries/cuprated/src/config/default.rs b/binaries/cuprated/src/config/default.rs index 09022ca8..23ca77e9 100644 --- a/binaries/cuprated/src/config/default.rs +++ b/binaries/cuprated/src/config/default.rs @@ -4,9 +4,9 @@ use std::{ str::from_utf8, }; -use cuprate_helper::fs::{CUPRATE_BLOCKCHAIN_DIR, CUPRATE_CACHE_DIR, CUPRATE_TXPOOL_DIR}; - -use crate::config::DEFAULT_CONFIG_FILE_NAME; +use cuprate_helper::fs::{ + CUPRATE_BLOCKCHAIN_DIR, CUPRATE_CACHE_DIR, CUPRATE_TXPOOL_DIR, DEFAULT_CONFIG_FILE_NAME, +}; /// Creates a config file which will be named [`DEFAULT_CONFIG_FILE_NAME`] in the directory given in [`Path`]. /// From 3664eb1b08fad51be6ae00cc32ecf2dc17976836 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Tue, 5 Nov 2024 01:37:51 +0000 Subject: [PATCH 16/22] rename & fix default config file --- Cargo.lock | 7 +++---- binaries/cuprated/Cargo.toml | 2 +- .../cuprated/src/config/{Cuprate.toml => Cuprated.toml} | 4 ++-- binaries/cuprated/src/config/default.rs | 2 +- binaries/cuprated/src/config/storage.rs | 2 +- 5 files changed, 8 insertions(+), 9 deletions(-) rename binaries/cuprated/src/config/{Cuprate.toml => Cuprated.toml} (97%) diff --git a/Cargo.lock b/Cargo.lock index a7d3d374..ed598a40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1041,7 +1041,6 @@ dependencies = [ "cuprate-consensus", "cuprate-consensus-context", "cuprate-consensus-rules", - "cuprate-constants", "cuprate-cryptonight", "cuprate-dandelion-tower", "cuprate-database", @@ -2851,12 +2850,12 @@ dependencies = [ [[package]] name = "terminal_size" -version = "0.3.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" +checksum = "4f599bd7ca042cfdf8f4512b277c02ba102247820f9d9d4a9f521f496751a6ef" dependencies = [ "rustix", - "windows-sys 0.48.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/binaries/cuprated/Cargo.toml b/binaries/cuprated/Cargo.toml index 5b4f6e62..cc288b41 100644 --- a/binaries/cuprated/Cargo.toml +++ b/binaries/cuprated/Cargo.toml @@ -24,7 +24,7 @@ cuprate-p2p-core = { workspace = true, features = ["serde"] } cuprate-dandelion-tower = { workspace = true, features = ["txpool"] } cuprate-async-buffer = { workspace = true } cuprate-address-book = { workspace = true, features = ["serde_config"] } -cuprate-blockchain = { workspace = true, features = ["service"] } +cuprate-blockchain = { workspace = true } cuprate-database-service = { workspace = true, features = ["serde"] } cuprate-txpool = { workspace = true } cuprate-database = { workspace = true, features = ["serde"] } diff --git a/binaries/cuprated/src/config/Cuprate.toml b/binaries/cuprated/src/config/Cuprated.toml similarity index 97% rename from binaries/cuprated/src/config/Cuprate.toml rename to binaries/cuprated/src/config/Cuprated.toml index ecc53790..7a626834 100644 --- a/binaries/cuprated/src/config/Cuprate.toml +++ b/binaries/cuprated/src/config/Cuprated.toml @@ -36,7 +36,7 @@ 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} +peer_store_directory = {cache} ## The amount of time between address book saves. peer_save_period = {{ secs = 90, nanos = 0 }} @@ -65,7 +65,7 @@ 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 +max_txpool_byte_size = 100_000_000 ## Blockchain storage config. [storage.blockchain] diff --git a/binaries/cuprated/src/config/default.rs b/binaries/cuprated/src/config/default.rs index 23ca77e9..6b8bb57f 100644 --- a/binaries/cuprated/src/config/default.rs +++ b/binaries/cuprated/src/config/default.rs @@ -45,7 +45,7 @@ fn generate_config_text() -> String { }; format!( - include_str!("Cuprate.toml"), + include_str!("Cuprated.toml"), cache = toml_value_str(&CUPRATE_CACHE_DIR), txpool = toml_value_str(&CUPRATE_TXPOOL_DIR), blockchain = toml_value_str(&CUPRATE_BLOCKCHAIN_DIR) diff --git a/binaries/cuprated/src/config/storage.rs b/binaries/cuprated/src/config/storage.rs index 80de2656..2023139b 100644 --- a/binaries/cuprated/src/config/storage.rs +++ b/binaries/cuprated/src/config/storage.rs @@ -53,7 +53,7 @@ impl Default for TxpoolConfig { path: CUPRATE_TXPOOL_DIR.to_path_buf(), sync_mode: SyncMode::Async, }, - max_txpool_size: 100_000_000, + max_txpool_byte_size: 100_000_000, } } } From 623ba49bb119e61b0fbd0389709c2b8b8ef69bca Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Tue, 5 Nov 2024 02:21:38 +0000 Subject: [PATCH 17/22] fix cargo hack --- helper/src/network.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/helper/src/network.rs b/helper/src/network.rs index 2836781d..d2961170 100644 --- a/helper/src/network.rs +++ b/helper/src/network.rs @@ -6,7 +6,8 @@ //! //! `#[no_std]` compatible. // TODO: move to types crate. -use std::{ + +use core::{ fmt::{Display, Formatter}, str::FromStr, }; @@ -52,17 +53,16 @@ impl FromStr for Network { type Err = ParseNetworkError; fn from_str(s: &str) -> Result { - match s.to_lowercase().as_str() { - "mainnet" => Ok(Self::Mainnet), - "testnet" => Ok(Self::Testnet), - "stagenet" => Ok(Self::Stagenet), + match s { + "mainnet" | "Mainnet" => Ok(Self::Mainnet), + "testnet" | "Testnet" => Ok(Self::Testnet), + "stagenet" | "Stagenet" => Ok(Self::Stagenet), _ => Err(ParseNetworkError), } } } - impl Display for Network { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { f.write_str(match self { Self::Mainnet => "mainnet", Self::Testnet => "testnet", From 21f62872888d2d94d4636940dee4f89daeb4c8d8 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Tue, 5 Nov 2024 16:53:59 +0000 Subject: [PATCH 18/22] enable serde on `cuprate-helper` --- storage/blockchain/Cargo.toml | 2 +- storage/txpool/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/storage/blockchain/Cargo.toml b/storage/blockchain/Cargo.toml index 6fd973cd..92b4374b 100644 --- a/storage/blockchain/Cargo.toml +++ b/storage/blockchain/Cargo.toml @@ -15,7 +15,7 @@ default = ["heed"] heed = ["cuprate-database/heed"] redb = ["cuprate-database/redb"] redb-memory = ["cuprate-database/redb-memory"] -serde = ["dep:serde", "cuprate-database/serde", "cuprate-database-service/serde"] +serde = ["dep:serde", "cuprate-database/serde", "cuprate-database-service/serde", "cuprate-helper/serde"] [dependencies] cuprate-database = { workspace = true } diff --git a/storage/txpool/Cargo.toml b/storage/txpool/Cargo.toml index c9082655..0fb43b24 100644 --- a/storage/txpool/Cargo.toml +++ b/storage/txpool/Cargo.toml @@ -15,7 +15,7 @@ default = ["heed"] heed = ["cuprate-database/heed"] redb = ["cuprate-database/redb"] redb-memory = ["cuprate-database/redb-memory"] -serde = ["dep:serde", "cuprate-database/serde", "cuprate-database-service/serde"] +serde = ["dep:serde", "cuprate-database/serde", "cuprate-database-service/serde", "cuprate-helper/serde"] [dependencies] cuprate-database = { workspace = true, features = ["heed"] } From e652c189e5ba9c69177cb89136ec5eefe0bfeca1 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sun, 10 Nov 2024 20:56:43 +0000 Subject: [PATCH 19/22] changes from matrix chats --- Cargo.lock | 2 - .../src/config/Cuprated.toml => Cuprated.toml | 14 +-- binaries/cuprated/Cargo.toml | 4 +- binaries/cuprated/src/config.rs | 42 +++++--- binaries/cuprated/src/config/default.rs | 45 ++------- binaries/cuprated/src/config/fs.rs | 21 ++++ binaries/cuprated/src/config/p2p.rs | 99 +++++++++++++++---- binaries/cuprated/src/config/storage.rs | 12 +-- binaries/cuprated/src/constants.rs | 2 + helper/src/fs.rs | 90 ++++++++++------- p2p/address-book/Cargo.toml | 6 +- p2p/address-book/src/lib.rs | 14 --- p2p/p2p/Cargo.toml | 2 - p2p/p2p/src/block_downloader.rs | 14 --- storage/blockchain/README.md | 2 +- storage/blockchain/src/config.rs | 47 ++++----- storage/blockchain/src/ops/mod.rs | 2 +- storage/blockchain/src/service/mod.rs | 2 +- storage/blockchain/src/service/tests.rs | 2 +- storage/blockchain/src/tests.rs | 2 +- storage/txpool/src/config.rs | 47 ++++----- 21 files changed, 247 insertions(+), 224 deletions(-) rename binaries/cuprated/src/config/Cuprated.toml => Cuprated.toml (85%) create mode 100644 binaries/cuprated/src/config/fs.rs diff --git a/Cargo.lock b/Cargo.lock index ed598a40..c4279630 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -558,7 +558,6 @@ dependencies = [ "futures", "indexmap", "rand", - "serde", "thiserror", "tokio", "tokio-util", @@ -850,7 +849,6 @@ dependencies = [ "rand", "rand_distr", "rayon", - "serde", "thiserror", "tokio", "tokio-stream", diff --git a/binaries/cuprated/src/config/Cuprated.toml b/Cuprated.toml similarity index 85% rename from binaries/cuprated/src/config/Cuprated.toml rename to Cuprated.toml index 7a626834..a0446251 100644 --- a/binaries/cuprated/src/config/Cuprated.toml +++ b/Cuprated.toml @@ -27,7 +27,7 @@ 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" +listen_on = "0.0.0.0" ## The Clear-net addressbook config. [p2p.clear_net.address_book_config] @@ -35,10 +35,8 @@ server.ip = "0.0.0.0" 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_directory = {cache} ## The amount of time between address book saves. -peer_save_period = {{ secs = 90, nanos = 0 }} +peer_save_period = { secs = 90, nanos = 0 } ## The block downloader config. [p2p.block_downloader] @@ -48,10 +46,8 @@ 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 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 }} +check_client_pool_interval = { secs = 30, nanos = 0 } ## Storage config [storage] @@ -60,8 +56,6 @@ 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). @@ -69,7 +63,5 @@ max_txpool_byte_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" diff --git a/binaries/cuprated/Cargo.toml b/binaries/cuprated/Cargo.toml index cc288b41..af50acc8 100644 --- a/binaries/cuprated/Cargo.toml +++ b/binaries/cuprated/Cargo.toml @@ -19,11 +19,11 @@ cuprate-epee-encoding = { workspace = true } cuprate-fixed-bytes = { workspace = true } cuprate-levin = { workspace = true } cuprate-wire = { workspace = true } -cuprate-p2p = { workspace = true, features = ["serde"] } +cuprate-p2p = { workspace = true } cuprate-p2p-core = { workspace = true, features = ["serde"] } cuprate-dandelion-tower = { workspace = true, features = ["txpool"] } cuprate-async-buffer = { workspace = true } -cuprate-address-book = { workspace = true, features = ["serde_config"] } +cuprate-address-book = { workspace = true } cuprate-blockchain = { workspace = true } cuprate-database-service = { workspace = true, features = ["serde"] } cuprate-txpool = { workspace = true } diff --git a/binaries/cuprated/src/config.rs b/binaries/cuprated/src/config.rs index 794b10bf..6b1229f8 100644 --- a/binaries/cuprated/src/config.rs +++ b/binaries/cuprated/src/config.rs @@ -15,14 +15,16 @@ use cuprate_helper::{ network::Network, }; use cuprate_p2p::block_downloader::BlockDownloaderConfig; -use cuprate_p2p_core::ClearNet; +use cuprate_p2p_core::{ClearNet, ClearNetServerCfg}; mod args; mod default; +mod fs; mod p2p; mod storage; mod tracing_config; +use crate::config::fs::FileSystemConfig; use p2p::P2PConfig; use storage::StorageConfig; use tracing_config::TracingConfig; @@ -43,6 +45,7 @@ pub fn read_config_and_args() -> Config { } else { // First attempt to read the config file from the current directory. std::env::current_dir() + .map(|path| path.join(DEFAULT_CONFIG_FILE_NAME)) .map_err(Into::into) .and_then(Config::read_from_path) .inspect_err(|e| tracing::debug!("Failed to read config from current dir: {e}")) @@ -53,7 +56,7 @@ pub fn read_config_and_args() -> Config { }) .inspect_err(|e| { tracing::debug!("Failed to read config from config dir: {e}"); - tracing::warn!("Failed to find/read config file, using default config."); + println!("Failed to find/read config file, using default config."); }) .unwrap_or_default() }; @@ -76,6 +79,8 @@ pub struct Config { /// The storage config. storage: StorageConfig, + + fs: FileSystemConfig, } impl Config { @@ -87,14 +92,15 @@ impl Config { fn read_from_path(file: impl AsRef) -> Result { let file_text = read_to_string(file.as_ref())?; - 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() - ); - })?) + Ok(toml::from_str(&file_text) + .inspect(|_| println!("Using config at: {}", file.as_ref().to_string_lossy())) + .inspect_err(|e| { + println!("{e}"); + println!( + "Failed to parse config file at: {}", + file.as_ref().to_string_lossy() + ); + })?) } /// Returns the current [`Network`] we are running on. @@ -111,11 +117,17 @@ impl Config { 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()), + server_config: Some(ClearNetServerCfg { + ip: self.p2p.clear_net.listen_on, + }), 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(self.network), + address_book_config: self + .p2p + .clear_net + .general + .address_book_config(&self.fs.cache_directory, self.network), } } @@ -135,13 +147,13 @@ impl Config { // 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()) + .data_directory(self.fs.data_directory.clone()) .sync_mode(blockchain.shared.sync_mode) .build() } /// The [`BlockDownloaderConfig`]. - pub const fn block_downloader_config(&self) -> BlockDownloaderConfig { - self.p2p.block_downloader + pub fn block_downloader_config(&self) -> BlockDownloaderConfig { + self.p2p.block_downloader.clone().into() } } diff --git a/binaries/cuprated/src/config/default.rs b/binaries/cuprated/src/config/default.rs index 6b8bb57f..8c9642b8 100644 --- a/binaries/cuprated/src/config/default.rs +++ b/binaries/cuprated/src/config/default.rs @@ -4,9 +4,9 @@ use std::{ str::from_utf8, }; -use cuprate_helper::fs::{ - CUPRATE_BLOCKCHAIN_DIR, CUPRATE_CACHE_DIR, CUPRATE_TXPOOL_DIR, DEFAULT_CONFIG_FILE_NAME, -}; +use cuprate_helper::fs::{CUPRATE_CACHE_DIR, DEFAULT_CONFIG_FILE_NAME}; + +use crate::constants::EXAMPLE_CONFIG; /// Creates a config file which will be named [`DEFAULT_CONFIG_FILE_NAME`] in the directory given in [`Path`]. /// @@ -28,50 +28,17 @@ pub fn create_default_config_file(path: &Path) -> ! { } }; - let config = generate_config_text(); + let config = EXAMPLE_CONFIG; file.write_all(config.as_bytes()).unwrap(); std::process::exit(0); } -/// Generates the text of the default config file. -fn generate_config_text() -> String { - let toml_value_str = |t: &PathBuf| { - let mut value = String::new(); - - serde::Serialize::serialize(t, toml::ser::ValueSerializer::new(&mut value)).unwrap(); - - value - }; - - format!( - include_str!("Cuprated.toml"), - cache = toml_value_str(&CUPRATE_CACHE_DIR), - txpool = toml_value_str(&CUPRATE_TXPOOL_DIR), - blockchain = toml_value_str(&CUPRATE_BLOCKCHAIN_DIR) - ) -} - #[cfg(test)] mod tests { - use crate::config::{default::generate_config_text, Config}; - - #[test] - fn generate_config_text_covers_all_values() { - let text = generate_config_text(); - let table: toml::Table = toml::from_str(&text).unwrap(); - - let full_config = Config::default(); - let full_config_table: toml::Table = - toml::from_str(&toml::to_string(&full_config).unwrap()).unwrap(); - - assert_eq!(full_config_table, table); - } - + use crate::{config::Config, constants::EXAMPLE_CONFIG}; #[test] fn generate_config_text_is_valid() { - let text = generate_config_text(); - - let config: Config = toml::from_str(&text).unwrap(); + let config: Config = toml::from_str(EXAMPLE_CONFIG).unwrap(); } } diff --git a/binaries/cuprated/src/config/fs.rs b/binaries/cuprated/src/config/fs.rs new file mode 100644 index 00000000..f8f61307 --- /dev/null +++ b/binaries/cuprated/src/config/fs.rs @@ -0,0 +1,21 @@ +use std::path::PathBuf; + +use serde::{Deserialize, Serialize}; + +use cuprate_helper::fs::{CUPRATE_CACHE_DIR, CUPRATE_DATA_DIR}; + +#[derive(Deserialize, Serialize)] +#[serde(deny_unknown_fields, default)] +pub struct FileSystemConfig { + pub data_directory: PathBuf, + pub cache_directory: PathBuf, +} + +impl Default for FileSystemConfig { + fn default() -> Self { + Self { + data_directory: CUPRATE_DATA_DIR.to_path_buf(), + cache_directory: CUPRATE_CACHE_DIR.to_path_buf(), + } + } +} diff --git a/binaries/cuprated/src/config/p2p.rs b/binaries/cuprated/src/config/p2p.rs index abd667d3..749355bd 100644 --- a/binaries/cuprated/src/config/p2p.rs +++ b/binaries/cuprated/src/config/p2p.rs @@ -1,11 +1,9 @@ -use std::net::SocketAddr; - -use serde::{Deserialize, Serialize}; - -use cuprate_address_book::AddressBookConfig; +use cuprate_helper::fs::addressbook_path; use cuprate_helper::network::Network; -use cuprate_p2p::block_downloader::BlockDownloaderConfig; -use cuprate_p2p_core::ClearNetServerCfg; +use serde::{Deserialize, Serialize}; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; +use std::path::Path; +use std::time::Duration; /// P2P config. #[derive(Default, Deserialize, Serialize)] @@ -17,16 +15,62 @@ pub struct P2PConfig { pub block_downloader: BlockDownloaderConfig, } +#[derive(Clone, Deserialize, Serialize)] +#[serde(deny_unknown_fields, default)] +pub struct BlockDownloaderConfig { + /// The size in bytes of the buffer between the block downloader and the place which + /// is consuming the downloaded blocks. + pub buffer_size: usize, + /// The size of the in progress queue (in bytes) at which we stop requesting more blocks. + pub in_progress_queue_size: usize, + /// The [`Duration`] between checking the client pool for free peers. + pub check_client_pool_interval: Duration, + /// The target size of a single batch of blocks (in bytes). + pub target_batch_size: usize, +} + +impl From for cuprate_p2p::block_downloader::BlockDownloaderConfig { + fn from(value: BlockDownloaderConfig) -> Self { + Self { + buffer_size: value.buffer_size, + in_progress_queue_size: value.in_progress_queue_size, + check_client_pool_interval: value.check_client_pool_interval, + target_batch_size: value.target_batch_size, + initial_batch_len: 1, + } + } +} + +impl Default for BlockDownloaderConfig { + fn default() -> Self { + Self { + buffer_size: 50_000_000, + in_progress_queue_size: 50_000_000, + check_client_pool_interval: Duration::from_secs(30), + target_batch_size: 5_000_000, + } + } +} + /// The config values for P2P clear-net. -#[derive(Default, Deserialize, Serialize)] +#[derive(Deserialize, Serialize)] #[serde(deny_unknown_fields, default)] pub struct ClearNetConfig { /// The server config. - pub server: ClearNetServerCfg, + pub listen_on: IpAddr, #[serde(flatten)] pub general: SharedNetConfig, } +impl Default for ClearNetConfig { + fn default() -> Self { + Self { + listen_on: IpAddr::V4(Ipv4Addr::UNSPECIFIED), + general: Default::default(), + } + } +} + /// Network config values shared between all network zones. #[derive(Deserialize, Serialize)] #[serde(deny_unknown_fields, default)] @@ -47,14 +91,17 @@ pub struct SharedNetConfig { impl SharedNetConfig { /// Returns the [`AddressBookConfig`]. - pub fn address_book_config(&self, network: Network) -> AddressBookConfig { - // 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_directory - .push(network.to_string()); - - address_book_config + pub fn address_book_config( + &self, + cache_dir: &Path, + network: Network, + ) -> cuprate_address_book::AddressBookConfig { + cuprate_address_book::AddressBookConfig { + max_white_list_length: self.address_book_config.max_white_list_length, + max_gray_list_length: self.address_book_config.max_gray_list_length, + peer_store_directory: addressbook_path(cache_dir, network), + peer_save_period: self.address_book_config.peer_save_period, + } } } @@ -71,6 +118,24 @@ impl Default for SharedNetConfig { } } +#[derive(Deserialize, Serialize)] +#[serde(deny_unknown_fields, default)] +pub struct AddressBookConfig { + max_white_list_length: usize, + max_gray_list_length: usize, + peer_save_period: Duration, +} + +impl Default for AddressBookConfig { + fn default() -> Self { + Self { + max_white_list_length: 1_000, + max_gray_list_length: 5_000, + peer_save_period: Duration::from_secs(30), + } + } +} + /// Seed nodes for [`ClearNet`](cuprate_p2p_core::ClearNet). pub fn clear_net_seed_nodes(network: Network) -> Vec { let seeds = match network { diff --git a/binaries/cuprated/src/config/storage.rs b/binaries/cuprated/src/config/storage.rs index 2023139b..99b8e202 100644 --- a/binaries/cuprated/src/config/storage.rs +++ b/binaries/cuprated/src/config/storage.rs @@ -1,8 +1,9 @@ use serde::{Deserialize, Serialize}; +use std::path::PathBuf; use cuprate_database::config::SyncMode; use cuprate_database_service::ReaderThreads; -use cuprate_helper::fs::{CUPRATE_BLOCKCHAIN_DIR, CUPRATE_TXPOOL_DIR}; +use cuprate_helper::fs::CUPRATE_DATA_DIR; /// The storage config. #[derive(Default, Deserialize, Serialize)] @@ -28,7 +29,6 @@ impl Default for BlockchainConfig { fn default() -> Self { Self { shared: SharedStorageConfig { - path: CUPRATE_BLOCKCHAIN_DIR.to_path_buf(), sync_mode: SyncMode::Async, }, } @@ -50,7 +50,6 @@ impl Default for TxpoolConfig { fn default() -> Self { Self { shared: SharedStorageConfig { - path: CUPRATE_TXPOOL_DIR.to_path_buf(), sync_mode: SyncMode::Async, }, max_txpool_byte_size: 100_000_000, @@ -59,12 +58,9 @@ impl Default for TxpoolConfig { } /// Config values shared between the tx-pool and blockchain. -#[derive(Deserialize, Serialize)] -#[serde(deny_unknown_fields)] +#[derive(Default, Deserialize, Serialize)] +#[serde(deny_unknown_fields, default)] pub struct SharedStorageConfig { - /// The path to the database storage. - pub path: std::path::PathBuf, /// The [`SyncMode`] of the database. - #[serde(default)] pub sync_mode: SyncMode, } diff --git a/binaries/cuprated/src/constants.rs b/binaries/cuprated/src/constants.rs index 2f3c7bb6..a42248b6 100644 --- a/binaries/cuprated/src/constants.rs +++ b/binaries/cuprated/src/constants.rs @@ -18,6 +18,8 @@ pub const VERSION_BUILD: &str = if cfg!(debug_assertions) { pub const PANIC_CRITICAL_SERVICE_ERROR: &str = "A service critical to Cuprate's function returned an unexpected error."; +pub const EXAMPLE_CONFIG: &str = include_str!("../../../Cuprated.toml"); + #[cfg(test)] mod test { use super::*; diff --git a/helper/src/fs.rs b/helper/src/fs.rs index cebd0cc4..60d61a37 100644 --- a/helper/src/fs.rs +++ b/helper/src/fs.rs @@ -158,37 +158,61 @@ impl_path_lazylock! { CUPRATE_DATA_DIR, data_dir, "", - - /// Cuprate's blockchain directory. - /// - /// This is the PATH used for any Cuprate blockchain files. - /// - /// | OS | PATH | - /// |---------|----------------------------------------------------------------| - /// | Windows | `C:\Users\Alice\AppData\Roaming\Cuprate\blockchain\` | - /// | macOS | `/Users/Alice/Library/Application Support/Cuprate/blockchain/` | - /// | Linux | `/home/alice/.local/share/cuprate/blockchain/` | - CUPRATE_BLOCKCHAIN_DIR, - data_dir, - "blockchain", - - /// Cuprate's transaction pool directory. - /// - /// This is the PATH used for any Cuprate txpool files. - /// - /// | OS | PATH | - /// |---------|------------------------------------------------------------| - /// | Windows | `C:\Users\Alice\AppData\Roaming\Cuprate\txpool\` | - /// | macOS | `/Users/Alice/Library/Application Support/Cuprate/txpool/` | - /// | Linux | `/home/alice/.local/share/cuprate/txpool/` | - CUPRATE_TXPOOL_DIR, - data_dir, - "txpool", } -/// Joins the [`Path`] with a folder for the given [`Network`]. -pub fn path_with_network(path: &Path, network: Network) -> PathBuf { - path.join(network.to_string()) +/// Joins the [`Network`] to the [`Path`]. +/// +/// This will keep the path the same for [`Network::Mainnet`]. +fn path_with_network(path: &Path, network: Network) -> PathBuf { + match network { + Network::Mainnet => path.to_path_buf(), + network => path.join(network.to_string()), + } +} + +/// Cuprate's blockchain directory. +/// +/// This is the PATH used for any Cuprate blockchain files. +/// +/// ```rust +/// use cuprate_helper::{network::Network, fs::{CUPRATE_DATA_DIR, blockchain_path}}; +/// +/// assert_eq!(blockchain_path(&**CUPRATE_DATA_DIR, Network::Mainnet).as_path(), CUPRATE_DATA_DIR.join("blockchain")); +/// assert_eq!(blockchain_path(&**CUPRATE_DATA_DIR, Network::Stagenet).as_path(), CUPRATE_DATA_DIR.join(Network::Stagenet.to_string()).join("blockchain")); +/// assert_eq!(blockchain_path(&**CUPRATE_DATA_DIR, Network::Testnet).as_path(), CUPRATE_DATA_DIR.join(Network::Testnet.to_string()).join("blockchain")); +/// ``` +pub fn blockchain_path(data_dir: &Path, network: Network) -> PathBuf { + path_with_network(data_dir, network).join("blockchain") +} + +/// Cuprate's txpool directory. +/// +/// This is the PATH used for any Cuprate txpool files. +/// +/// ```rust +/// use cuprate_helper::{network::Network, fs::{CUPRATE_DATA_DIR, txpool_path}}; +/// +/// assert_eq!(txpool_path(&**CUPRATE_DATA_DIR, Network::Mainnet).as_path(), CUPRATE_DATA_DIR.join("txpool")); +/// assert_eq!(txpool_path(&**CUPRATE_DATA_DIR, Network::Stagenet).as_path(), CUPRATE_DATA_DIR.join(Network::Stagenet.to_string()).join("txpool")); +/// assert_eq!(txpool_path(&**CUPRATE_DATA_DIR, Network::Testnet).as_path(), CUPRATE_DATA_DIR.join(Network::Testnet.to_string()).join("txpool")); +/// ``` +pub fn txpool_path(data_dir: &Path, network: Network) -> PathBuf { + path_with_network(data_dir, network).join("txpool") +} + +/// Cuprate's address-book directory. +/// +/// This is the PATH used for any Cuprate address-book files. +/// +/// ```rust +/// use cuprate_helper::{network::Network, fs::{CUPRATE_CACHE_DIR, addressbook_path}}; +/// +/// assert_eq!(addressbook_path(&**CUPRATE_CACHE_DIR, Network::Mainnet).as_path(), CUPRATE_CACHE_DIR.join("addressbook")); +/// assert_eq!(addressbook_path(&**CUPRATE_CACHE_DIR, Network::Stagenet).as_path(), CUPRATE_CACHE_DIR.join(Network::Stagenet.to_string()).join("addressbook")); +/// assert_eq!(addressbook_path(&**CUPRATE_CACHE_DIR, Network::Testnet).as_path(), CUPRATE_CACHE_DIR.join(Network::Testnet.to_string()).join("addressbook")); +/// ``` +pub fn addressbook_path(cache_dir: &Path, network: Network) -> PathBuf { + path_with_network(cache_dir, network).join("addressbook") } //---------------------------------------------------------------------------------------------------- Tests @@ -210,29 +234,21 @@ mod test { (&*CUPRATE_CACHE_DIR, ""), (&*CUPRATE_CONFIG_DIR, ""), (&*CUPRATE_DATA_DIR, ""), - (&*CUPRATE_BLOCKCHAIN_DIR, ""), - (&*CUPRATE_TXPOOL_DIR, ""), ]; if cfg!(target_os = "windows") { array[0].1 = r"AppData\Local\Cuprate"; array[1].1 = r"AppData\Roaming\Cuprate"; array[2].1 = r"AppData\Roaming\Cuprate"; - array[3].1 = r"AppData\Roaming\Cuprate\blockchain"; - array[4].1 = r"AppData\Roaming\Cuprate\txpool"; } else if cfg!(target_os = "macos") { array[0].1 = "Library/Caches/Cuprate"; array[1].1 = "Library/Application Support/Cuprate"; array[2].1 = "Library/Application Support/Cuprate"; - array[3].1 = "Library/Application Support/Cuprate/blockchain"; - array[4].1 = "Library/Application Support/Cuprate/txpool"; } else { // Assumes Linux. array[0].1 = ".cache/cuprate"; array[1].1 = ".config/cuprate"; array[2].1 = ".local/share/cuprate"; - array[3].1 = ".local/share/cuprate/blockchain"; - array[4].1 = ".local/share/cuprate/txpool"; }; for (path, expected) in array { diff --git a/p2p/address-book/Cargo.toml b/p2p/address-book/Cargo.toml index 18416e22..aa7e3c57 100644 --- a/p2p/address-book/Cargo.toml +++ b/p2p/address-book/Cargo.toml @@ -5,13 +5,10 @@ edition = "2021" license = "MIT" authors = ["Boog900"] -[features] -defualt = [] -serde_config = ["dep:serde", "dep:cuprate-helper"] [dependencies] cuprate-constants = { workspace = true } -cuprate-helper = { workspace = true, features = ["std", "fs"], optional = true } +cuprate-helper = { workspace = true, features = ["std"], optional = true } cuprate-pruning = { workspace = true } cuprate-p2p-core = { workspace = true, features = ["borsh"] } @@ -28,7 +25,6 @@ indexmap = { workspace = true, features = ["std"] } rand = { workspace = true, features = ["std", "std_rng"] } borsh = { workspace = true, features = ["derive", "std"] } -serde = { workspace = true, features = ["derive", "std"], optional = true } [dev-dependencies] cuprate-test-utils = { workspace = true } diff --git a/p2p/address-book/src/lib.rs b/p2p/address-book/src/lib.rs index 04229677..054be462 100644 --- a/p2p/address-book/src/lib.rs +++ b/p2p/address-book/src/lib.rs @@ -20,8 +20,6 @@ mod store; /// The address book config. #[derive(Debug, Clone)] -#[cfg_attr(feature = "serde_config", derive(serde::Deserialize, serde::Serialize))] -#[cfg_attr(feature = "serde_config", serde(deny_unknown_fields, default))] pub struct AddressBookConfig { /// The maximum number of white peers in the peer list. /// @@ -37,18 +35,6 @@ pub struct AddressBookConfig { pub peer_save_period: Duration, } -#[cfg(feature = "serde_config")] -impl Default for AddressBookConfig { - fn default() -> Self { - Self { - max_white_list_length: 1000, - max_gray_list_length: 5000, - peer_store_directory: cuprate_helper::fs::CUPRATE_CACHE_DIR.clone(), - peer_save_period: Duration::from_secs(90), - } - } -} - /// Possible errors when dealing with the address book. /// This is boxed when returning an error in the [`tower::Service`]. #[derive(Debug, thiserror::Error, Eq, PartialEq)] diff --git a/p2p/p2p/Cargo.toml b/p2p/p2p/Cargo.toml index feb4b3f7..866fb918 100644 --- a/p2p/p2p/Cargo.toml +++ b/p2p/p2p/Cargo.toml @@ -34,8 +34,6 @@ rand_distr = { workspace = true, features = ["std"] } tracing = { workspace = true, features = ["std", "attributes"] } borsh = { workspace = true, features = ["derive", "std"] } -serde = { workspace = true, features = ["std", "derive"], optional = true } - [dev-dependencies] cuprate-test-utils = { workspace = true } indexmap = { workspace = true } diff --git a/p2p/p2p/src/block_downloader.rs b/p2p/p2p/src/block_downloader.rs index d5d7fcce..5ebcedc0 100644 --- a/p2p/p2p/src/block_downloader.rs +++ b/p2p/p2p/src/block_downloader.rs @@ -59,8 +59,6 @@ pub struct BlockBatch { /// The block downloader config. #[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(feature = "serde", serde(deny_unknown_fields, default))] pub struct BlockDownloaderConfig { /// The size in bytes of the buffer between the block downloader and the place which /// is consuming the downloaded blocks. @@ -75,18 +73,6 @@ pub struct BlockDownloaderConfig { pub initial_batch_len: usize, } -impl Default for BlockDownloaderConfig { - fn default() -> Self { - Self { - buffer_size: 50_000_000, - in_progress_queue_size: 50_000_000, - check_client_pool_interval: Duration::from_secs(30), - target_batch_size: 5_000_000, - initial_batch_len: 1, - } - } -} - /// An error that occurred in the [`BlockDownloader`]. #[derive(Debug, thiserror::Error)] pub(crate) enum BlockDownloadError { diff --git a/storage/blockchain/README.md b/storage/blockchain/README.md index 3f97a3d6..8ab8b432 100644 --- a/storage/blockchain/README.md +++ b/storage/blockchain/README.md @@ -76,7 +76,7 @@ use cuprate_blockchain::{ let tmp_dir = tempfile::tempdir()?; let db_dir = tmp_dir.path().to_owned(); let config = ConfigBuilder::new() - .db_directory(db_dir.into()) + .data_directory(db_dir.into()) .build(); // Initialize the database environment. diff --git a/storage/blockchain/src/config.rs b/storage/blockchain/src/config.rs index 2ae94c70..5578db8f 100644 --- a/storage/blockchain/src/config.rs +++ b/storage/blockchain/src/config.rs @@ -25,7 +25,7 @@ //! //! let config = ConfigBuilder::new() //! // Use a custom database directory. -//! .db_directory(db_dir.into()) +//! .data_directory(db_dir.into()) //! // Use as many reader threads as possible (when using `service`). //! .reader_threads(ReaderThreads::OnePerThread) //! // Use the fastest sync mode. @@ -48,7 +48,7 @@ use serde::{Deserialize, Serialize}; use cuprate_database::{config::SyncMode, resize::ResizeAlgorithm}; use cuprate_helper::{ - fs::{path_with_network, CUPRATE_BLOCKCHAIN_DIR}, + fs::{blockchain_path, CUPRATE_DATA_DIR}, network::Network, }; @@ -64,8 +64,7 @@ pub use cuprate_database_service::ReaderThreads; pub struct ConfigBuilder { network: Network, - /// [`Config::db_directory`]. - db_directory: Option, + data_dir: Option, /// [`Config::cuprate_database_config`]. db_config: cuprate_database::config::ConfigBuilder, @@ -82,10 +81,10 @@ impl ConfigBuilder { pub fn new() -> Self { Self { network: Network::default(), - db_directory: None, - db_config: cuprate_database::config::ConfigBuilder::new(Cow::Owned(path_with_network( - &CUPRATE_BLOCKCHAIN_DIR, - Network::default(), + data_dir: None, + db_config: cuprate_database::config::ConfigBuilder::new(Cow::Owned(blockchain_path( + &CUPRATE_DATA_DIR, + Network::Mainnet, ))), reader_threads: None, } @@ -101,14 +100,14 @@ impl ConfigBuilder { pub fn build(self) -> Config { // INVARIANT: all PATH safety checks are done // in `helper::fs`. No need to do them here. - let db_directory = self - .db_directory - .unwrap_or_else(|| CUPRATE_BLOCKCHAIN_DIR.to_path_buf()); + let data_dir = self + .data_dir + .unwrap_or_else(|| CUPRATE_DATA_DIR.to_path_buf()); let reader_threads = self.reader_threads.unwrap_or_default(); let db_config = self .db_config - .db_directory(Cow::Owned(path_with_network(&db_directory, self.network))) + .db_directory(Cow::Owned(blockchain_path(&data_dir, self.network))) .reader_threads(reader_threads.as_threads()) .build(); @@ -127,8 +126,8 @@ impl ConfigBuilder { /// Set a custom database directory (and file) [`PathBuf`]. #[must_use] - pub fn db_directory(mut self, db_directory: PathBuf) -> Self { - self.db_directory = Some(db_directory); + pub fn data_directory(mut self, db_directory: PathBuf) -> Self { + self.data_dir = Some(db_directory); self } @@ -159,10 +158,7 @@ impl ConfigBuilder { /// Good default for testing, and resource-available machines. #[must_use] pub fn fast(mut self) -> Self { - self.db_config = cuprate_database::config::ConfigBuilder::new(Cow::Owned( - path_with_network(&CUPRATE_BLOCKCHAIN_DIR, self.network), - )) - .fast(); + self.db_config = self.db_config.fast(); self.reader_threads = Some(ReaderThreads::OnePerThread); self @@ -174,10 +170,7 @@ impl ConfigBuilder { /// Good default for resource-limited machines, e.g. a cheap VPS. #[must_use] pub fn low_power(mut self) -> Self { - self.db_config = cuprate_database::config::ConfigBuilder::new(Cow::Owned( - path_with_network(&CUPRATE_BLOCKCHAIN_DIR, self.network), - )) - .low_power(); + self.db_config = self.db_config.low_power(); self.reader_threads = Some(ReaderThreads::One); self @@ -188,9 +181,9 @@ impl Default for ConfigBuilder { fn default() -> Self { Self { network: Network::default(), - db_directory: Some(CUPRATE_BLOCKCHAIN_DIR.to_path_buf()), - db_config: cuprate_database::config::ConfigBuilder::new(Cow::Owned(path_with_network( - &CUPRATE_BLOCKCHAIN_DIR, + data_dir: Some(CUPRATE_DATA_DIR.to_path_buf()), + db_config: cuprate_database::config::ConfigBuilder::new(Cow::Owned(blockchain_path( + &CUPRATE_DATA_DIR, Network::default(), ))), reader_threads: Some(ReaderThreads::default()), @@ -238,8 +231,8 @@ impl Config { /// /// let config = Config::new(); /// - /// assert_eq!(config.db_config.db_directory().as_ref(), path_with_network(&CUPRATE_BLOCKCHAIN_DIR, Network::Mainnet).as_path()); - /// assert!(config.db_config.db_file().starts_with(&*CUPRATE_BLOCKCHAIN_DIR)); + /// assert_eq!(config.db_config.db_directory().as_ref(), blockchain_path(&CUPRATE_DATA_DIR, Network::Mainnet).as_path()); + /// assert!(config.db_config.db_file().starts_with(&*CUPRATE_DATA_DIR)); /// assert!(config.db_config.db_file().ends_with(DATABASE_DATA_FILENAME)); /// assert_eq!(config.db_config.sync_mode, SyncMode::default()); /// assert_eq!(config.db_config.resize_algorithm, ResizeAlgorithm::default()); diff --git a/storage/blockchain/src/ops/mod.rs b/storage/blockchain/src/ops/mod.rs index 285aa244..96b25aff 100644 --- a/storage/blockchain/src/ops/mod.rs +++ b/storage/blockchain/src/ops/mod.rs @@ -71,7 +71,7 @@ //! let tmp_dir = tempfile::tempdir()?; //! let db_dir = tmp_dir.path().to_owned(); //! let config = ConfigBuilder::new() -//! .db_directory(db_dir.into()) +//! .data_directory(db_dir.into()) //! .build(); //! //! // Initialize the database environment. diff --git a/storage/blockchain/src/service/mod.rs b/storage/blockchain/src/service/mod.rs index c5eb80c2..d6a811bd 100644 --- a/storage/blockchain/src/service/mod.rs +++ b/storage/blockchain/src/service/mod.rs @@ -77,7 +77,7 @@ //! let tmp_dir = tempfile::tempdir()?; //! let db_dir = tmp_dir.path().to_owned(); //! let config = ConfigBuilder::new() -//! .db_directory(db_dir.into()) +//! .data_directory(db_dir.into()) //! .build(); //! //! // Initialize the database thread-pool. diff --git a/storage/blockchain/src/service/tests.rs b/storage/blockchain/src/service/tests.rs index e8a18426..38db6652 100644 --- a/storage/blockchain/src/service/tests.rs +++ b/storage/blockchain/src/service/tests.rs @@ -45,7 +45,7 @@ fn init_service() -> ( ) { let tempdir = tempfile::tempdir().unwrap(); let config = ConfigBuilder::new() - .db_directory(tempdir.path().into()) + .data_directory(tempdir.path().into()) .low_power() .build(); let (reader, writer, env) = init(config).unwrap(); diff --git a/storage/blockchain/src/tests.rs b/storage/blockchain/src/tests.rs index 271a82cc..4192f812 100644 --- a/storage/blockchain/src/tests.rs +++ b/storage/blockchain/src/tests.rs @@ -74,7 +74,7 @@ impl AssertTableLen { pub(crate) fn tmp_concrete_env() -> (impl Env, tempfile::TempDir) { let tempdir = tempfile::tempdir().unwrap(); let config = ConfigBuilder::new() - .db_directory(tempdir.path().into()) + .data_directory(tempdir.path().into()) .low_power() .build(); let env = crate::open(config).unwrap(); diff --git a/storage/txpool/src/config.rs b/storage/txpool/src/config.rs index 69b73aab..66864b1d 100644 --- a/storage/txpool/src/config.rs +++ b/storage/txpool/src/config.rs @@ -10,7 +10,7 @@ use cuprate_database::{ }; use cuprate_database_service::ReaderThreads; use cuprate_helper::{ - fs::{path_with_network, CUPRATE_TXPOOL_DIR}, + fs::{txpool_path, CUPRATE_DATA_DIR}, network::Network, }; @@ -26,8 +26,7 @@ const DEFAULT_TXPOOL_WEIGHT_LIMIT: usize = 600 * 1024 * 1024; pub struct ConfigBuilder { network: Network, - /// [`Config::db_directory`]. - db_directory: Option, + data_dir: Option, /// [`Config::cuprate_database_config`]. db_config: cuprate_database::config::ConfigBuilder, @@ -47,10 +46,10 @@ impl ConfigBuilder { pub fn new() -> Self { Self { network: Network::default(), - db_directory: None, - db_config: cuprate_database::config::ConfigBuilder::new(Cow::Owned(path_with_network( - &CUPRATE_TXPOOL_DIR, - Network::default(), + data_dir: None, + db_config: cuprate_database::config::ConfigBuilder::new(Cow::Owned(txpool_path( + &CUPRATE_DATA_DIR, + Network::Mainnet, ))), reader_threads: None, max_txpool_weight: None, @@ -67,9 +66,9 @@ impl ConfigBuilder { pub fn build(self) -> Config { // INVARIANT: all PATH safety checks are done // in `helper::fs`. No need to do them here. - let db_directory = self - .db_directory - .unwrap_or_else(|| CUPRATE_TXPOOL_DIR.to_path_buf()); + let data_dir = self + .data_dir + .unwrap_or_else(|| CUPRATE_DATA_DIR.to_path_buf()); let reader_threads = self.reader_threads.unwrap_or_default(); @@ -79,7 +78,7 @@ impl ConfigBuilder { let db_config = self .db_config - .db_directory(Cow::Owned(path_with_network(&db_directory, self.network))) + .db_directory(Cow::Owned(txpool_path(&data_dir, self.network))) .reader_threads(reader_threads.as_threads()) .build(); @@ -104,10 +103,10 @@ impl ConfigBuilder { self } - /// Set a custom database directory (and file) [`PathBuf`]. + /// Set a custom data directory [`PathBuf`]. #[must_use] - pub fn db_directory(mut self, db_directory: PathBuf) -> Self { - self.db_directory = Some(db_directory); + pub fn data_directory(mut self, db_directory: PathBuf) -> Self { + self.data_dir = Some(db_directory); self } @@ -138,9 +137,7 @@ impl ConfigBuilder { /// Good default for testing, and resource-available machines. #[must_use] pub fn fast(mut self) -> Self { - self.db_config = - cuprate_database::config::ConfigBuilder::new(Cow::Borrowed(&*CUPRATE_TXPOOL_DIR)) - .fast(); + self.db_config = self.db_config.fast(); self.reader_threads = Some(ReaderThreads::OnePerThread); self @@ -152,9 +149,7 @@ impl ConfigBuilder { /// Good default for resource-limited machines, e.g. a cheap VPS. #[must_use] pub fn low_power(mut self) -> Self { - self.db_config = - cuprate_database::config::ConfigBuilder::new(Cow::Borrowed(&*CUPRATE_TXPOOL_DIR)) - .low_power(); + self.db_config = self.db_config.low_power(); self.reader_threads = Some(ReaderThreads::One); self @@ -165,10 +160,10 @@ impl Default for ConfigBuilder { fn default() -> Self { Self { network: Network::default(), - db_directory: Some(CUPRATE_TXPOOL_DIR.to_path_buf()), - db_config: cuprate_database::config::ConfigBuilder::new(Cow::Owned(path_with_network( - &CUPRATE_TXPOOL_DIR, - Network::default(), + data_dir: Some(CUPRATE_DATA_DIR.to_path_buf()), + db_config: cuprate_database::config::ConfigBuilder::new(Cow::Owned(txpool_path( + &CUPRATE_DATA_DIR, + Network::Mainnet, ))), reader_threads: Some(ReaderThreads::default()), max_txpool_weight: Some(DEFAULT_TXPOOL_WEIGHT_LIMIT), @@ -220,8 +215,8 @@ impl Config { /// /// let config = Config::new(); /// - /// assert_eq!(config.db_config.db_directory(), path_with_network(&CUPRATE_TXPOOL_DIR, Network::Mainnet).as_path()); - /// assert!(config.db_config.db_file().starts_with(&*CUPRATE_TXPOOL_DIR)); + /// assert_eq!(config.db_config.db_directory(), txpool_path(&CUPRATE_DATA_DIR, Network::Mainnet).as_path()); + /// assert!(config.db_config.db_file().starts_with(&*CUPRATE_DATA_DIR)); /// assert!(config.db_config.db_file().ends_with(DATABASE_DATA_FILENAME)); /// assert_eq!(config.db_config.sync_mode, SyncMode::default()); /// assert_eq!(config.db_config.resize_algorithm, ResizeAlgorithm::default()); From 7c5fa6db20d8a2e66a7e601e2a9193b1adde696f Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sun, 10 Nov 2024 21:20:42 +0000 Subject: [PATCH 20/22] fix ci --- Cargo.lock | 2 -- binaries/cuprated/Cargo.toml | 2 +- binaries/cuprated/src/config/p2p.rs | 13 ++++++++----- binaries/cuprated/src/config/storage.rs | 3 ++- p2p/address-book/Cargo.toml | 1 - p2p/p2p-core/Cargo.toml | 1 - p2p/p2p-core/src/network_zones/clear.rs | 12 +----------- 7 files changed, 12 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c4279630..b7eb7a48 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -551,7 +551,6 @@ version = "0.1.0" dependencies = [ "borsh", "cuprate-constants", - "cuprate-helper", "cuprate-p2p-core", "cuprate-pruning", "cuprate-test-utils", @@ -881,7 +880,6 @@ dependencies = [ "futures", "hex", "hex-literal", - "serde", "thiserror", "tokio", "tokio-stream", diff --git a/binaries/cuprated/Cargo.toml b/binaries/cuprated/Cargo.toml index af50acc8..acf8827b 100644 --- a/binaries/cuprated/Cargo.toml +++ b/binaries/cuprated/Cargo.toml @@ -20,7 +20,7 @@ cuprate-fixed-bytes = { workspace = true } cuprate-levin = { workspace = true } cuprate-wire = { workspace = true } cuprate-p2p = { workspace = true } -cuprate-p2p-core = { workspace = true, features = ["serde"] } +cuprate-p2p-core = { workspace = true } cuprate-dandelion-tower = { workspace = true, features = ["txpool"] } cuprate-async-buffer = { workspace = true } cuprate-address-book = { workspace = true } diff --git a/binaries/cuprated/src/config/p2p.rs b/binaries/cuprated/src/config/p2p.rs index 749355bd..aaf7dda7 100644 --- a/binaries/cuprated/src/config/p2p.rs +++ b/binaries/cuprated/src/config/p2p.rs @@ -1,9 +1,12 @@ -use cuprate_helper::fs::addressbook_path; -use cuprate_helper::network::Network; +use std::{ + net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}, + path::Path, + time::Duration, +}; + use serde::{Deserialize, Serialize}; -use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; -use std::path::Path; -use std::time::Duration; + +use cuprate_helper::{fs::addressbook_path, network::Network}; /// P2P config. #[derive(Default, Deserialize, Serialize)] diff --git a/binaries/cuprated/src/config/storage.rs b/binaries/cuprated/src/config/storage.rs index 99b8e202..b3e3c1f4 100644 --- a/binaries/cuprated/src/config/storage.rs +++ b/binaries/cuprated/src/config/storage.rs @@ -1,6 +1,7 @@ -use serde::{Deserialize, Serialize}; use std::path::PathBuf; +use serde::{Deserialize, Serialize}; + use cuprate_database::config::SyncMode; use cuprate_database_service::ReaderThreads; use cuprate_helper::fs::CUPRATE_DATA_DIR; diff --git a/p2p/address-book/Cargo.toml b/p2p/address-book/Cargo.toml index aa7e3c57..d57cfded 100644 --- a/p2p/address-book/Cargo.toml +++ b/p2p/address-book/Cargo.toml @@ -8,7 +8,6 @@ authors = ["Boog900"] [dependencies] cuprate-constants = { workspace = true } -cuprate-helper = { workspace = true, features = ["std"], optional = true } cuprate-pruning = { workspace = true } cuprate-p2p-core = { workspace = true, features = ["borsh"] } diff --git a/p2p/p2p-core/Cargo.toml b/p2p/p2p-core/Cargo.toml index 7fa919c6..bc6c8335 100644 --- a/p2p/p2p-core/Cargo.toml +++ b/p2p/p2p-core/Cargo.toml @@ -28,7 +28,6 @@ tracing = { workspace = true, features = ["std", "attributes"] } hex-literal = { workspace = true } borsh = { workspace = true, features = ["derive", "std"], optional = true } -serde = { workspace = true, features = ["derive", "std"], optional = true } [dev-dependencies] cuprate-test-utils = { workspace = true } diff --git a/p2p/p2p-core/src/network_zones/clear.rs b/p2p/p2p-core/src/network_zones/clear.rs index bff7b1ed..cb3c5592 100644 --- a/p2p/p2p-core/src/network_zones/clear.rs +++ b/p2p/p2p-core/src/network_zones/clear.rs @@ -1,5 +1,5 @@ use std::{ - net::{IpAddr, Ipv4Addr, SocketAddr}, + net::{IpAddr, SocketAddr}, pin::Pin, task::{Context, Poll}, }; @@ -38,20 +38,10 @@ impl NetZoneAddress for SocketAddr { } #[derive(Debug, Clone)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -#[cfg_attr(feature = "serde", serde(deny_unknown_fields))] pub struct ClearNetServerCfg { pub ip: IpAddr, } -impl Default for ClearNetServerCfg { - fn default() -> Self { - Self { - ip: IpAddr::V4(Ipv4Addr::UNSPECIFIED), - } - } -} - #[derive(Clone, Copy)] pub enum ClearNet {} From 668095433d74849e145b667b38ae253d5369d12b Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sun, 10 Nov 2024 21:28:51 +0000 Subject: [PATCH 21/22] fix doc --- storage/blockchain/src/config.rs | 6 +++--- storage/txpool/src/config.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/storage/blockchain/src/config.rs b/storage/blockchain/src/config.rs index 5578db8f..4bef2cb0 100644 --- a/storage/blockchain/src/config.rs +++ b/storage/blockchain/src/config.rs @@ -93,8 +93,8 @@ impl ConfigBuilder { /// Build into a [`Config`]. /// /// # Default values - /// If [`ConfigBuilder::db_directory`] was not called, - /// the default [`CUPRATE_BLOCKCHAIN_DIR`] will be used. + /// If [`ConfigBuilder::data_directory`] was not called, + /// [`blockchain_path`] with [`CUPRATE_DATA_DIR`] [`Network::Mainnet`] will be used. /// /// For all other values, [`Default::default`] is used. pub fn build(self) -> Config { @@ -213,7 +213,7 @@ impl Config { /// Create a new [`Config`] with sane default settings. /// /// The [`cuprate_database::config::Config::db_directory`] - /// will be set to [`CUPRATE_BLOCKCHAIN_DIR`] joined with [`Network::Mainnet`]. + /// will be set to [`blockchain_path`] with [`CUPRATE_DATA_DIR`] [`Network::Mainnet`]. /// /// All other values will be [`Default::default`]. /// diff --git a/storage/txpool/src/config.rs b/storage/txpool/src/config.rs index 66864b1d..724ae21a 100644 --- a/storage/txpool/src/config.rs +++ b/storage/txpool/src/config.rs @@ -59,8 +59,8 @@ impl ConfigBuilder { /// Build into a [`Config`]. /// /// # Default values - /// If [`ConfigBuilder::db_directory`] was not called, - /// the default [`CUPRATE_TXPOOL_DIR`] will be used. + /// If [`ConfigBuilder::data_directory`] was not called, + /// [`txpool_path`] with [`CUPRATE_DATA_DIR`] and [`Network::Mainnet`] will be used. /// /// For all other values, [`Default::default`] is used. pub fn build(self) -> Config { @@ -196,7 +196,7 @@ impl Config { /// Create a new [`Config`] with sane default settings. /// /// The [`DbConfig::db_directory`] - /// will be set to [`CUPRATE_TXPOOL_DIR`] joined with [`Network::Mainnet`]. + /// will be set to [`txpool_path`] with [`CUPRATE_DATA_DIR`] and [`Network::Mainnet`]. /// /// All other values will be [`Default::default`]. /// From 318cf946efa5be7227f69762010ae62b1d59f008 Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Sun, 10 Nov 2024 21:53:14 +0000 Subject: [PATCH 22/22] fix doc test --- storage/txpool/README.md | 2 +- storage/txpool/src/ops.rs | 2 +- storage/txpool/src/service.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/storage/txpool/README.md b/storage/txpool/README.md index d14f445b..ca4f737c 100644 --- a/storage/txpool/README.md +++ b/storage/txpool/README.md @@ -78,7 +78,7 @@ use cuprate_txpool::{ let tmp_dir = tempfile::tempdir()?; let db_dir = tmp_dir.path().to_owned(); let config = ConfigBuilder::new() - .db_directory(db_dir.into()) + .data_directory(db_dir.into()) .build(); // Initialize the database environment. diff --git a/storage/txpool/src/ops.rs b/storage/txpool/src/ops.rs index 289a8bbf..badc4f6d 100644 --- a/storage/txpool/src/ops.rs +++ b/storage/txpool/src/ops.rs @@ -51,7 +51,7 @@ //! let tmp_dir = tempfile::tempdir()?; //! let db_dir = tmp_dir.path().to_owned(); //! let config = ConfigBuilder::new() -//! .db_directory(db_dir.into()) +//! .data_directory(db_dir.into()) //! .build(); //! //! // Initialize the database environment. diff --git a/storage/txpool/src/service.rs b/storage/txpool/src/service.rs index a82de5bf..03ce2f03 100644 --- a/storage/txpool/src/service.rs +++ b/storage/txpool/src/service.rs @@ -83,7 +83,7 @@ //! let tmp_dir = tempfile::tempdir()?; //! let db_dir = tmp_dir.path().to_owned(); //! let config = ConfigBuilder::new() -//! .db_directory(db_dir.into()) +//! .data_directory(db_dir.into()) //! .build(); //! //! // Initialize the database thread-pool.