diff --git a/Cargo.lock b/Cargo.lock index 352b4a2da..3dcc0e0d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,21 +29,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - [[package]] name = "anstyle" version = "1.0.10" @@ -320,12 +305,6 @@ dependencies = [ "syn 2.0.101", ] -[[package]] -name = "bumpalo" -version = "3.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" - [[package]] name = "bytemuck" version = "1.23.0" @@ -384,18 +363,6 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" -[[package]] -name = "chrono" -version = "0.4.41" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d" -dependencies = [ - "android-tzdata", - "iana-time-zone", - "num-traits", - "windows-link", -] - [[package]] name = "clap" version = "4.5.37" @@ -622,6 +589,7 @@ dependencies = [ "cuprate-constants", "cuprate-database", "cuprate-database-service", + "cuprate-fs", "cuprate-helper", "cuprate-pruning", "cuprate-test-utils", @@ -826,6 +794,14 @@ dependencies = [ "thiserror", ] +[[package]] +name = "cuprate-fs" +version = "0.0.1" +dependencies = [ + "cuprate-types", + "dirs", +] + [[package]] name = "cuprate-fuzz" version = "0.0.0" @@ -844,16 +820,12 @@ dependencies = [ name = "cuprate-helper" version = "0.1.0" dependencies = [ - "chrono", - "crossbeam", "cuprate-constants", "curve25519-dalek", - "dirs", "futures", "libc", "monero-serai", "rayon", - "serde", "tokio", "windows", ] @@ -1045,7 +1017,7 @@ dependencies = [ "bytemuck", "cuprate-database", "cuprate-database-service", - "cuprate-helper", + "cuprate-fs", "cuprate-test-utils", "cuprate-types", "hex", @@ -1123,7 +1095,6 @@ dependencies = [ "bytemuck", "bytes", "cfg-if", - "chrono", "clap", "const_format", "crossbeam", @@ -1142,6 +1113,7 @@ dependencies = [ "cuprate-epee-encoding", "cuprate-fast-sync", "cuprate-fixed-bytes", + "cuprate-fs", "cuprate-helper", "cuprate-hex", "cuprate-json-rpc", @@ -1764,30 +1736,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "iana-time-zone" -version = "0.1.63" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0c919e5debc312ad217002b8048a17b7d83f80703865bbfcfebb0458b0b27d8" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "log", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - [[package]] name = "icu_collections" version = "2.0.0" @@ -1933,16 +1881,6 @@ dependencies = [ "libc", ] -[[package]] -name = "js-sys" -version = "0.3.77" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" -dependencies = [ - "once_cell", - "wasm-bindgen", -] - [[package]] name = "keccak" version = "0.1.5" @@ -3611,64 +3549,6 @@ dependencies = [ "wit-bindgen-rt", ] -[[package]] -name = "wasm-bindgen" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" -dependencies = [ - "cfg-if", - "once_cell", - "rustversion", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" -dependencies = [ - "bumpalo", - "log", - "proc-macro2", - "quote", - "syn 2.0.101", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.101", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.100" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" -dependencies = [ - "unicode-ident", -] - [[package]] name = "webpki-roots" version = "0.26.11" diff --git a/Cargo.toml b/Cargo.toml index 32f824872..f11a4bc4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,9 @@ members = [ "consensus/fast-sync", "consensus/rules", + # Filesystem + "fs", + # Net "net/epee-encoding", "net/levin", @@ -51,7 +54,7 @@ members = [ "test-utils", # Fuzz - "fuzz" + "fuzz", ] # Windows is a pain, so instead of making the fuzz tests work on windows, we just don't build the fuzz target on windows. @@ -69,6 +72,9 @@ default-members = [ "consensus/fast-sync", "consensus/rules", + # Filesystem + "fs", + # Net "net/epee-encoding", "net/levin", @@ -137,6 +143,7 @@ cuprate-constants = { path = "constants", default-featur cuprate-consensus = { path = "consensus", default-features = false } cuprate-consensus-context = { path = "consensus/context", default-features = false } cuprate-cryptonight = { path = "cryptonight", default-features = false } +cuprate-fs = { path = "fs", default-features = false } cuprate-helper = { path = "helper", default-features = false } cuprate-epee-encoding = { path = "net/epee-encoding", default-features = false } cuprate-levin = { path = "net/levin", default-features = false } @@ -175,7 +182,6 @@ bytemuck = { version = "1", default-features = false } bytes = { version = "1", default-features = false } cfg-if = { version = "1", default-features = false } clap = { version = "4", default-features = false } -chrono = { version = "0.4", default-features = false } crypto-bigint = { version = "0.5", default-features = false } crossbeam = { version = "0.8", default-features = false } const_format = { version = "0.2", default-features = false } diff --git a/binaries/cuprated/Cargo.toml b/binaries/cuprated/Cargo.toml index cd5f8145b..9931c7810 100644 --- a/binaries/cuprated/Cargo.toml +++ b/binaries/cuprated/Cargo.toml @@ -23,7 +23,8 @@ cuprate-database = { workspace = true, features = ["serde"] } cuprate-epee-encoding = { workspace = true } cuprate-fast-sync = { workspace = true } cuprate-fixed-bytes = { workspace = true } -cuprate-helper = { workspace = true, features = ["std", "serde", "time", "net"] } +cuprate-fs = { workspace = true } +cuprate-helper = { workspace = true, features = ["std", "time", "net"] } cuprate-hex = { workspace = true } cuprate-json-rpc = { workspace = true } cuprate-levin = { workspace = true } @@ -33,7 +34,7 @@ cuprate-pruning = { workspace = true } cuprate-rpc-interface = { workspace = true, features = ["dummy"] } cuprate-rpc-types = { workspace = true, features = ["from"] } cuprate-test-utils = { workspace = true } -cuprate-txpool = { workspace = true } +cuprate-txpool = { workspace = true, features = ["serde"] } cuprate-types = { workspace = true, features = ["json"] } cuprate-wire = { workspace = true } @@ -48,7 +49,6 @@ bytemuck = { workspace = true } bytes = { workspace = true } cfg-if = { workspace = true } clap = { workspace = true, features = ["cargo", "help", "wrap_help", "usage", "error-context", "suggestions"] } -chrono = { workspace = true } crypto-bigint = { workspace = true } crossbeam = { workspace = true } curve25519-dalek = { workspace = true } diff --git a/binaries/cuprated/src/blockchain/fast_sync.rs b/binaries/cuprated/src/blockchain/fast_sync.rs index eeba4be19..cdfcbda72 100644 --- a/binaries/cuprated/src/blockchain/fast_sync.rs +++ b/binaries/cuprated/src/blockchain/fast_sync.rs @@ -1,6 +1,6 @@ use std::slice; -use cuprate_helper::network::Network; +use cuprate_types::network::Network; /// The hashes of the compiled in fast sync file. /// diff --git a/binaries/cuprated/src/blockchain/manager/tests.rs b/binaries/cuprated/src/blockchain/manager/tests.rs index 7efa3137f..659d60604 100644 --- a/binaries/cuprated/src/blockchain/manager/tests.rs +++ b/binaries/cuprated/src/blockchain/manager/tests.rs @@ -9,9 +9,9 @@ use tower::BoxError; use cuprate_consensus_context::{BlockchainContext, ContextConfig}; use cuprate_consensus_rules::{hard_forks::HFInfo, miner_tx::calculate_block_reward, HFsInfo}; -use cuprate_helper::network::Network; use cuprate_p2p::{block_downloader::BlockBatch, BroadcastSvc}; use cuprate_p2p_core::handles::HandleBuilder; +use cuprate_types::network::Network; use cuprate_types::{CachedVerificationState, TransactionVerificationData, TxVersion}; use crate::blockchain::{ diff --git a/binaries/cuprated/src/config.rs b/binaries/cuprated/src/config.rs index d74deebf6..605274626 100644 --- a/binaries/cuprated/src/config.rs +++ b/binaries/cuprated/src/config.rs @@ -12,12 +12,10 @@ use clap::Parser; use serde::{Deserialize, Serialize}; use cuprate_consensus::ContextConfig; -use cuprate_helper::{ - fs::{CUPRATE_CONFIG_DIR, DEFAULT_CONFIG_FILE_NAME}, - network::Network, -}; +use cuprate_fs::{CUPRATE_CONFIG_DIR, DEFAULT_CONFIG_FILE_NAME}; use cuprate_p2p::block_downloader::BlockDownloaderConfig; use cuprate_p2p_core::ClearNet; +use cuprate_types::network::Network; use crate::{ constants::{DEFAULT_CONFIG_STARTUP_DELAY, DEFAULT_CONFIG_WARNING}, diff --git a/binaries/cuprated/src/config/args.rs b/binaries/cuprated/src/config/args.rs index 712ee936f..618fa7e60 100644 --- a/binaries/cuprated/src/config/args.rs +++ b/binaries/cuprated/src/config/args.rs @@ -3,7 +3,7 @@ use std::{io::Write, path::PathBuf, process::exit}; use clap::builder::TypedValueParser; use serde_json::Value; -use cuprate_helper::network::Network; +use cuprate_types::network::Network; use crate::{config::Config, version::CupratedVersionInfo}; diff --git a/binaries/cuprated/src/config/fs.rs b/binaries/cuprated/src/config/fs.rs index fa85f7d5b..743d44f95 100644 --- a/binaries/cuprated/src/config/fs.rs +++ b/binaries/cuprated/src/config/fs.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use serde::{Deserialize, Serialize}; -use cuprate_helper::fs::{CUPRATE_CACHE_DIR, CUPRATE_DATA_DIR}; +use cuprate_fs::{CUPRATE_CACHE_DIR, CUPRATE_DATA_DIR}; use super::macros::config_struct; diff --git a/binaries/cuprated/src/config/p2p.rs b/binaries/cuprated/src/config/p2p.rs index 2fda70896..b09911ec3 100644 --- a/binaries/cuprated/src/config/p2p.rs +++ b/binaries/cuprated/src/config/p2p.rs @@ -7,12 +7,13 @@ use std::{ use serde::{Deserialize, Serialize}; -use cuprate_helper::{fs::address_book_path, network::Network}; +use cuprate_fs::address_book_path; use cuprate_p2p::config::TransportConfig; use cuprate_p2p_core::{ transports::{Tcp, TcpServerConfig}, ClearNet, NetworkZone, Transport, }; +use cuprate_types::network::Network; use cuprate_wire::OnionAddr; use super::macros::config_struct; diff --git a/binaries/cuprated/src/config/storage.rs b/binaries/cuprated/src/config/storage.rs index 775404523..f1f7918b7 100644 --- a/binaries/cuprated/src/config/storage.rs +++ b/binaries/cuprated/src/config/storage.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use cuprate_database::config::SyncMode; use cuprate_database_service::ReaderThreads; -use cuprate_helper::fs::CUPRATE_DATA_DIR; +use cuprate_fs::CUPRATE_DATA_DIR; use super::macros::config_struct; diff --git a/binaries/cuprated/src/logging.rs b/binaries/cuprated/src/logging.rs index 0b9adc3df..910f24994 100644 --- a/binaries/cuprated/src/logging.rs +++ b/binaries/cuprated/src/logging.rs @@ -24,7 +24,7 @@ use tracing_subscriber::{ Layer, Registry, }; -use cuprate_helper::fs::logs_path; +use cuprate_fs::logs_path; use crate::config::Config; diff --git a/books/architecture/src/resources/fs/paths.md b/books/architecture/src/resources/fs/paths.md index 0e5dc3d65..31401f5ff 100644 --- a/books/architecture/src/resources/fs/paths.md +++ b/books/architecture/src/resources/fs/paths.md @@ -1,7 +1,7 @@ # Index of PATHs This is an index of all of the filesystem PATHs Cuprate actively uses. -The [`cuprate_helper::fs`](https://doc.cuprate.org/cuprate_helper/fs/index.html) +The [`cuprate_fs`](https://doc.cuprate.org/cuprate_helper/fs/index.html) module defines the general locations used throughout Cuprate. [`dirs`](https://docs.rs/dirs) is used internally, which follows diff --git a/consensus/context/src/difficulty.rs b/consensus/context/src/difficulty.rs index 3e4186862..e739095b0 100644 --- a/consensus/context/src/difficulty.rs +++ b/consensus/context/src/difficulty.rs @@ -51,7 +51,7 @@ impl DifficultyCacheConfig { self.window - 2 * self.cut } - /// Returns the config needed for [`Mainnet`](cuprate_helper::network::Network::Mainnet). This is also the + /// Returns the config needed for [`Mainnet`](cuprate_types::network::Network::Mainnet). This is also the /// config for all other current networks. pub const fn main_net() -> Self { Self { diff --git a/consensus/fast-sync/proptest-regressions/fast_sync.txt b/consensus/fast-sync/proptest-regressions/fast_sync.txt new file mode 100644 index 000000000..5da8fc40e --- /dev/null +++ b/consensus/fast-sync/proptest-regressions/fast_sync.txt @@ -0,0 +1,8 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 7edc45a2c7ef7e6625839a1e92c0e58ae1ff9be041a646318092748454aa6e2d # shrinks to len = 0 +cc 5047bd1b9440e28dc03ea450984724f470c25e26fbc127725e400eb908a0687f # shrinks to len = 0 diff --git a/consensus/rules/Cargo.toml b/consensus/rules/Cargo.toml index 6e7c8b64e..b5b1e2b73 100644 --- a/consensus/rules/Cargo.toml +++ b/consensus/rules/Cargo.toml @@ -12,7 +12,6 @@ rayon = ["dep:rayon"] [dependencies] cuprate-constants = { workspace = true, default-features = false, features = ["block"] } -cuprate-helper = { workspace = true, default-features = false, features = ["std", "cast"] } cuprate-types = { workspace = true, default-features = false } cuprate-cryptonight = { workspace = true } @@ -33,6 +32,8 @@ thiserror = { workspace = true } rayon = { workspace = true, optional = true } [dev-dependencies] +cuprate-helper = { workspace = true, default-features = false, features = ["std", "cast"] } + proptest = { workspace = true } proptest-derive = { workspace = true } tokio = { version = "1.35.0", features = ["rt-multi-thread", "macros"] } diff --git a/consensus/rules/src/genesis.rs b/consensus/rules/src/genesis.rs index e1cf4f827..e64aff40c 100644 --- a/consensus/rules/src/genesis.rs +++ b/consensus/rules/src/genesis.rs @@ -6,7 +6,7 @@ use monero_serai::{ transaction::Transaction, }; -use cuprate_helper::network::Network; +use cuprate_types::network::Network; const fn genesis_nonce(network: Network) -> u32 { match network { diff --git a/fs/Cargo.toml b/fs/Cargo.toml new file mode 100644 index 000000000..3405178cc --- /dev/null +++ b/fs/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "cuprate-fs" +version = "0.0.1" +edition = "2024" + +[dependencies] +cuprate-types = { workspace = true } + +dirs = { workspace = true } + +[lints] +workspace = true diff --git a/fs/src/constants.rs b/fs/src/constants.rs new file mode 100644 index 000000000..fe44e6d5c --- /dev/null +++ b/fs/src/constants.rs @@ -0,0 +1,29 @@ +/// Cuprate's main directory. +/// +/// This is the head PATH node used for any top-level Cuprate directories. +/// +/// | OS | PATH | +/// |---------|-----------------------------------------------------| +/// | Windows | `C:\Users\Alice\AppData\Roaming\Cuprate\` | +/// | macOS | `/Users/Alice/Library/Application Support/Cuprate/` | +/// | Linux | `/home/alice/.config/cuprate/` | +/// +/// This is shared between all Cuprate programs. +/// +/// # Value +/// This is `Cuprate` on `Windows|macOS` and `cuprate` on everything else. +/// +/// # Monero Equivalent +/// `.bitmonero` +pub const CUPRATE_DIR: &str = { + if cfg!(target_os = "windows") || cfg!(target_os = "macos") { + // The standard for main directories is capitalized. + "Cuprate" + } else { + // Standard on Linux + BSDs is lowercase. + "cuprate" + } +}; + +/// The default name of Cuprate's config file. +pub const DEFAULT_CONFIG_FILE_NAME: &str = "Cuprated.toml"; diff --git a/fs/src/lib.rs b/fs/src/lib.rs new file mode 100644 index 000000000..939bc0e7f --- /dev/null +++ b/fs/src/lib.rs @@ -0,0 +1,37 @@ +//! Cuprate directories and filenames. +//! +//! # Environment variables on Linux +//! Note that this module's functions uses [`dirs`], +//! which adheres to the XDG standard on Linux. +//! +//! This means that the values returned by these statics +//! may change at runtime depending on environment variables, +//! for example: +//! +//! By default the config directory is `~/.config`, however +//! if `$XDG_CONFIG_HOME` is set to something, that will be +//! used instead. +//! +//! ```rust +//! # use cuprate_fs::*; +//! # if cfg!(target_os = "linux") { +//! // SAFETY: test. +//! unsafe { std::env::set_var("XDG_CONFIG_HOME", "/custom/path"); } +//! assert_eq!( +//! CUPRATE_CONFIG_DIR.to_string_lossy(), +//! "/custom/path/cuprate" +//! ); +//! # } +//! ``` +//! +//! Reference: +//! - +//! - + +mod constants; +mod network; +mod paths; + +pub use constants::{CUPRATE_DIR, DEFAULT_CONFIG_FILE_NAME}; +pub use network::{address_book_path, arti_path, blockchain_path, logs_path, txpool_path}; +pub use paths::{CUPRATE_CACHE_DIR, CUPRATE_CONFIG_DIR, CUPRATE_DATA_DIR}; diff --git a/fs/src/network.rs b/fs/src/network.rs new file mode 100644 index 000000000..d0dcaa3dd --- /dev/null +++ b/fs/src/network.rs @@ -0,0 +1,47 @@ +//! Path generation functions prefixed with [`Network`]. + +use std::path::{Path, PathBuf}; + +use cuprate_types::network::Network; + +/// 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()), + } +} + +/// Create functions for creating data paths. +macro_rules! impl_data_path_with_network { + ($( + $(#[$attr:meta])* + $f:ident => $path:literal + ),* $(,)?) => { + $( + $(#[$attr])* + pub fn $f(data_dir: &Path, network: Network) -> PathBuf { + path_with_network(data_dir, network).join($path) + } + )* + }; +} + +impl_data_path_with_network! { + /// Cuprate's blockchain directory. + blockchain_path => "blockchain", + + /// Cuprate's txpool directory. + txpool_path => "txpool", + + /// Cuprate's logs directory. + logs_path => "logs", + + /// Cuprate's address-book directory. + address_book_path => "addressbook", + + /// Cuprate's arti directory. + arti_path => "arti", +} diff --git a/fs/src/paths.rs b/fs/src/paths.rs new file mode 100644 index 000000000..453ceded9 --- /dev/null +++ b/fs/src/paths.rs @@ -0,0 +1,134 @@ +//! Base paths used by Cuprate. + +use std::{path::PathBuf, sync::LazyLock}; + +use crate::CUPRATE_DIR; + +/// Create a `LazyLock` for common PATHs used by Cuprate. +macro_rules! impl_path_lazylock { + ($( + $(#[$attr:meta])* // Documentation and any `derive`'s. + $name:ident, // Name of the corresponding `LazyLock`. + $dirs_fn:ident, // Name of the `dirs` function to use, the PATH prefix. + $sub_dirs:literal // Any sub-directories to add onto the PATH. + ),* $(,)?) => {$( + // Create the `LazyLock` if needed, append + // the Cuprate directory string and return. + $(#[$attr])* + pub static $name: LazyLock = LazyLock::new(|| { + // There's nothing we can do but panic if + // we cannot acquire critical system directories. + // + // Although, this realistically won't panic on + // normal systems for all OS's supported by `dirs`. + let mut path = dirs::$dirs_fn().unwrap(); + + // FIXME: + // Consider a user who does `HOME=/ ./cuprated` + // + // Should we say "that's stupid" and panic here? + // Or should it be respected? + // We really don't want a `rm -rf /` type of situation... + assert!( + path.parent().is_some(), + "SAFETY: returned OS PATH was either root or empty, aborting" + ); + + // Returned OS PATH should be absolute, not relative. + assert!(path.is_absolute(), "SAFETY: returned OS PATH was not absolute"); + + // Unconditionally prefix with the top-level Cuprate directory. + path.push(CUPRATE_DIR); + + // Add any sub directories if specified in the macro. + if !$sub_dirs.is_empty() { + path.push($sub_dirs); + } + + path + }); + )*}; +} + +impl_path_lazylock! { + /// Cuprate's cache directory. + /// + /// This is the PATH used for any Cuprate cache files. + /// + /// | OS | PATH | + /// |---------|-----------------------------------------| + /// | Windows | `C:\Users\Alice\AppData\Local\Cuprate\` | + /// | macOS | `/Users/Alice/Library/Caches/Cuprate/` | + /// | Linux | `/home/alice/.cache/cuprate/` | + CUPRATE_CACHE_DIR, + cache_dir, + "", + + /// Cuprate's config directory. + /// + /// This is the PATH used for any Cuprate configuration files. + /// + /// | OS | PATH | + /// |---------|-----------------------------------------------------| + /// | Windows | `C:\Users\Alice\AppData\Roaming\Cuprate\` | + /// | macOS | `/Users/Alice/Library/Application Support/Cuprate/` | + /// | Linux | `/home/alice/.config/cuprate/` | + CUPRATE_CONFIG_DIR, + config_dir, + "", + + /// Cuprate's data directory. + /// + /// This is the PATH used for any Cuprate data files. + /// + /// | OS | PATH | + /// |---------|-----------------------------------------------------| + /// | Windows | `C:\Users\Alice\AppData\Roaming\Cuprate\` | + /// | macOS | `/Users/Alice/Library/Application Support/Cuprate/` | + /// | Linux | `/home/alice/.local/share/cuprate/` | + CUPRATE_DATA_DIR, + data_dir, + "", +} + +#[cfg(test)] +mod test { + use super::*; + + // Sanity check every PATH defined in this file. + // + // Each new PATH should be added to this test: + // - It must be `is_absolute()` + // - It must `ends_with()` the expected end PATH for the OS + #[test] + fn path_sanity_check() { + // Array of (PATH, expected_path_as_string). + // + // The different OS's will set the expected path below. + let mut array = [ + (&*CUPRATE_CACHE_DIR, ""), + (&*CUPRATE_CONFIG_DIR, ""), + (&*CUPRATE_DATA_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"; + } 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"; + } else { + // Assumes Linux. + array[0].1 = ".cache/cuprate"; + array[1].1 = ".config/cuprate"; + array[2].1 = ".local/share/cuprate"; + } + + for (path, expected) in array { + assert!(path.is_absolute()); + assert!(path.ends_with(expected)); + } + } +} diff --git a/helper/Cargo.toml b/helper/Cargo.toml index 73ef732a6..e262e1401 100644 --- a/helper/Cargo.toml +++ b/helper/Cargo.toml @@ -12,15 +12,13 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/consensus" # All features off by default. default = [] std = [] -atomic = ["dep:crossbeam"] asynch = ["dep:futures", "dep:rayon"] cast = [] constants = [] crypto = ["dep:curve25519-dalek", "dep:monero-serai", "std"] -fs = ["dep:dirs", "std"] num = [] map = ["cast", "dep:monero-serai", "dep:cuprate-constants"] -time = ["dep:chrono", "std"] +time = ["std"] thread = ["std", "dep:target_os_lib"] tx = ["dep:monero-serai"] fmt = ["map", "std"] @@ -29,16 +27,11 @@ net = [] [dependencies] cuprate-constants = { workspace = true, optional = true, features = ["block"] } -chrono = { workspace = true, optional = true, features = ["std", "clock"] } -crossbeam = { workspace = true, optional = true } curve25519-dalek = { workspace = true, optional = true } -dirs = { workspace = true, optional = true } 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/atomic.rs b/helper/src/atomic.rs deleted file mode 100644 index aa66c0c34..000000000 --- a/helper/src/atomic.rs +++ /dev/null @@ -1,84 +0,0 @@ -//! Atomic related -//! -//! `#[no_std]` compatible. - -//---------------------------------------------------------------------------------------------------- Use -use crossbeam::atomic::AtomicCell; - -//---------------------------------------------------------------------------------------------------- Atomic Float -/// Compile-time assertion that our floats are -/// lock-free for the target we're building for. -const _: () = { - assert!( - AtomicCell::::is_lock_free(), - "32-bit atomics are not supported on this build target." - ); - - assert!( - AtomicCell::::is_lock_free(), - "64-bit atomics are not supported on this build target." - ); -}; - -// SOMEDAY: use a custom float that implements `Eq` -// so that `compare_exchange()`, `fetch_*()` work. - -/// An atomic [`f32`]. -/// -/// This is an alias for -/// [`crossbeam::atomic::AtomicCell`](https://docs.rs/crossbeam/latest/crossbeam/atomic/struct.AtomicCell.html). -/// -/// Note that there are no [Ordering] parameters, -/// atomic loads use [Acquire], -/// and atomic stores use [Release]. -/// -/// [Ordering]: std::sync::atomic::Ordering -/// [Acquire]: std::sync::atomic::Ordering::Acquire -/// [Release]: std::sync::atomic::Ordering::Release -pub type AtomicF32 = AtomicCell; - -/// An atomic [`f64`]. -/// -/// This is an alias for -/// [`crossbeam::atomic::AtomicCell`](https://docs.rs/crossbeam/latest/crossbeam/atomic/struct.AtomicCell.html). -/// -/// Note that there are no [Ordering] parameters, -/// atomic loads use [Acquire], -/// and atomic stores use [Release]. -/// -/// [Ordering]: std::sync::atomic::Ordering -/// [Acquire]: std::sync::atomic::Ordering::Acquire -/// [Release]: std::sync::atomic::Ordering::Release -pub type AtomicF64 = AtomicCell; - -//---------------------------------------------------------------------------------------------------- TESTS -#[cfg(test)] -mod tests { - #![allow(clippy::float_cmp)] - - use super::*; - - #[test] - // Tests `AtomicF32`. - fn f32() { - let float = AtomicF32::new(5.0); - - // Loads/Stores - assert_eq!(float.swap(1.0), 5.0); - assert_eq!(float.load(), 1.0); - float.store(2.0); - assert_eq!(float.load(), 2.0); - } - - #[test] - // Tests `AtomicF64`. - fn f64() { - let float = AtomicF64::new(5.0); - - // Loads/Stores - assert_eq!(float.swap(1.0), 5.0); - assert_eq!(float.load(), 1.0); - float.store(2.0); - assert_eq!(float.load(), 2.0); - } -} diff --git a/helper/src/fs.rs b/helper/src/fs.rs deleted file mode 100644 index b9267326d..000000000 --- a/helper/src/fs.rs +++ /dev/null @@ -1,274 +0,0 @@ -//! Cuprate directories and filenames. -//! -//! # Environment variables on Linux -//! Note that this module's functions uses [`dirs`], -//! which adheres to the XDG standard on Linux. -//! -//! This means that the values returned by these statics -//! may change at runtime depending on environment variables, -//! for example: -//! -//! By default the config directory is `~/.config`, however -//! if `$XDG_CONFIG_HOME` is set to something, that will be -//! used instead. -//! -//! ```rust -//! # use cuprate_helper::fs::*; -//! # if cfg!(target_os = "linux") { -//! std::env::set_var("XDG_CONFIG_HOME", "/custom/path"); -//! assert_eq!( -//! CUPRATE_CONFIG_DIR.to_string_lossy(), -//! "/custom/path/cuprate" -//! ); -//! # } -//! ``` -//! -//! Reference: -//! - -//! - - -//---------------------------------------------------------------------------------------------------- Use -use std::{ - path::{Path, PathBuf}, - sync::LazyLock, -}; - -use crate::network::Network; - -//---------------------------------------------------------------------------------------------------- Const -/// Cuprate's main directory. -/// -/// This is the head PATH node used for any top-level Cuprate directories. -/// -/// | OS | PATH | -/// |---------|-----------------------------------------------------| -/// | Windows | `C:\Users\Alice\AppData\Roaming\Cuprate\` | -/// | macOS | `/Users/Alice/Library/Application Support/Cuprate/` | -/// | Linux | `/home/alice/.config/cuprate/` | -/// -/// This is shared between all Cuprate programs. -/// -/// # Value -/// This is `Cuprate` on `Windows|macOS` and `cuprate` on everything else. -/// -/// # Monero Equivalent -/// `.bitmonero` -pub const CUPRATE_DIR: &str = { - if cfg!(target_os = "windows") || cfg!(target_os = "macos") { - // The standard for main directories is capitalized. - "Cuprate" - } else { - // Standard on Linux + BSDs is lowercase. - "cuprate" - } -}; - -/// 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. -/// -/// This currently creates these directories: -/// - [`CUPRATE_CACHE_DIR`] -/// - [`CUPRATE_CONFIG_DIR`] -/// - [`CUPRATE_DATA_DIR`] -/// - [`CUPRATE_BLOCKCHAIN_DIR`] -macro_rules! impl_path_lazylock { - ($( - $(#[$attr:meta])* // Documentation and any `derive`'s. - $name:ident, // Name of the corresponding `LazyLock`. - $dirs_fn:ident, // Name of the `dirs` function to use, the PATH prefix. - $sub_dirs:literal // Any sub-directories to add onto the PATH. - ),* $(,)?) => {$( - // Create the `LazyLock` if needed, append - // the Cuprate directory string and return. - $(#[$attr])* - pub static $name: LazyLock = LazyLock::new(|| { - // There's nothing we can do but panic if - // we cannot acquire critical system directories. - // - // Although, this realistically won't panic on - // normal systems for all OS's supported by `dirs`. - let mut path = dirs::$dirs_fn().unwrap(); - - // FIXME: - // Consider a user who does `HOME=/ ./cuprated` - // - // Should we say "that's stupid" and panic here? - // Or should it be respected? - // We really don't want a `rm -rf /` type of situation... - assert!( - path.parent().is_some(), - "SAFETY: returned OS PATH was either root or empty, aborting" - ); - - // Returned OS PATH should be absolute, not relative. - assert!(path.is_absolute(), "SAFETY: returned OS PATH was not absolute"); - - // Unconditionally prefix with the top-level Cuprate directory. - path.push(CUPRATE_DIR); - - // Add any sub directories if specified in the macro. - if !$sub_dirs.is_empty() { - path.push($sub_dirs); - } - - path - }); - )*}; -} - -impl_path_lazylock! { - /// Cuprate's cache directory. - /// - /// This is the PATH used for any Cuprate cache files. - /// - /// | OS | PATH | - /// |---------|-----------------------------------------| - /// | Windows | `C:\Users\Alice\AppData\Local\Cuprate\` | - /// | macOS | `/Users/Alice/Library/Caches/Cuprate/` | - /// | Linux | `/home/alice/.cache/cuprate/` | - CUPRATE_CACHE_DIR, - cache_dir, - "", - - /// Cuprate's config directory. - /// - /// This is the PATH used for any Cuprate configuration files. - /// - /// | OS | PATH | - /// |---------|-----------------------------------------------------| - /// | Windows | `C:\Users\Alice\AppData\Roaming\Cuprate\` | - /// | macOS | `/Users/Alice/Library/Application Support/Cuprate/` | - /// | Linux | `/home/alice/.config/cuprate/` | - CUPRATE_CONFIG_DIR, - config_dir, - "", - - /// Cuprate's data directory. - /// - /// This is the PATH used for any Cuprate data files. - /// - /// | OS | PATH | - /// |---------|-----------------------------------------------------| - /// | Windows | `C:\Users\Alice\AppData\Roaming\Cuprate\` | - /// | macOS | `/Users/Alice/Library/Application Support/Cuprate/` | - /// | Linux | `/home/alice/.local/share/cuprate/` | - CUPRATE_DATA_DIR, - data_dir, - "", -} - -/// 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 logs directory. -/// -/// This is the PATH used for all Cuprate log files. -/// -/// ```rust -/// use cuprate_helper::{network::Network, fs::{CUPRATE_DATA_DIR, logs_path}}; -/// -/// assert_eq!(logs_path(&**CUPRATE_DATA_DIR, Network::Mainnet).as_path(), CUPRATE_DATA_DIR.join("logs")); -/// assert_eq!(logs_path(&**CUPRATE_DATA_DIR, Network::Stagenet).as_path(), CUPRATE_DATA_DIR.join(Network::Stagenet.to_string()).join("logs")); -/// assert_eq!(logs_path(&**CUPRATE_DATA_DIR, Network::Testnet).as_path(), CUPRATE_DATA_DIR.join(Network::Testnet.to_string()).join("logs")); -/// ``` -pub fn logs_path(data_dir: &Path, network: Network) -> PathBuf { - path_with_network(data_dir, network).join("logs") -} - -/// 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, address_book_path}}; -/// -/// assert_eq!(address_book_path(&**CUPRATE_CACHE_DIR, Network::Mainnet).as_path(), CUPRATE_CACHE_DIR.join("addressbook")); -/// assert_eq!(address_book_path(&**CUPRATE_CACHE_DIR, Network::Stagenet).as_path(), CUPRATE_CACHE_DIR.join(Network::Stagenet.to_string()).join("addressbook")); -/// assert_eq!(address_book_path(&**CUPRATE_CACHE_DIR, Network::Testnet).as_path(), CUPRATE_CACHE_DIR.join(Network::Testnet.to_string()).join("addressbook")); -/// ``` -pub fn address_book_path(cache_dir: &Path, network: Network) -> PathBuf { - path_with_network(cache_dir, network).join("addressbook") -} - -//---------------------------------------------------------------------------------------------------- Tests -#[cfg(test)] -mod test { - use super::*; - - // Sanity check every PATH defined in this file. - // - // Each new PATH should be added to this test: - // - It must be `is_absolute()` - // - It must `ends_with()` the expected end PATH for the OS - #[test] - fn path_sanity_check() { - // Array of (PATH, expected_path_as_string). - // - // The different OS's will set the expected path below. - let mut array = [ - (&*CUPRATE_CACHE_DIR, ""), - (&*CUPRATE_CONFIG_DIR, ""), - (&*CUPRATE_DATA_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"; - } 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"; - } else { - // Assumes Linux. - array[0].1 = ".cache/cuprate"; - array[1].1 = ".config/cuprate"; - array[2].1 = ".local/share/cuprate"; - } - - for (path, expected) in array { - assert!(path.is_absolute()); - assert!(path.ends_with(expected)); - } - } -} diff --git a/helper/src/lib.rs b/helper/src/lib.rs index 31797b9d3..e23356c15 100644 --- a/helper/src/lib.rs +++ b/helper/src/lib.rs @@ -5,17 +5,9 @@ #[cfg(feature = "asynch")] pub mod asynch; // async collides -#[cfg(feature = "atomic")] -pub mod atomic; - #[cfg(feature = "cast")] pub mod cast; -#[cfg(feature = "fs")] -pub mod fs; - -pub mod network; - #[cfg(feature = "num")] pub mod num; diff --git a/helper/src/num.rs b/helper/src/num.rs index 399c38d3b..b6a329397 100644 --- a/helper/src/num.rs +++ b/helper/src/num.rs @@ -3,10 +3,7 @@ //! `#[no_std]` compatible. //---------------------------------------------------------------------------------------------------- Use -use core::{ - cmp::Ordering, - ops::{Add, Div, Mul, Sub}, -}; +use core::ops::{Add, Div, Mul, Sub}; #[cfg(feature = "std")] mod rolling_median; @@ -128,73 +125,6 @@ where } } -#[inline] -/// Compare 2 non-`NaN` floats. -/// -/// ```rust -/// # use cuprate_helper::num::*; -/// # use core::cmp::Ordering; -/// assert_eq!(cmp_float(0.0, 1.0), Ordering::Less); -/// assert_eq!(cmp_float(1.0, 1.0), Ordering::Equal); -/// assert_eq!(cmp_float(2.0, 1.0), Ordering::Greater); -/// -/// assert_eq!(cmp_float(1.0, f32::INFINITY), Ordering::Less); -/// assert_eq!(cmp_float(f32::INFINITY, f32::INFINITY), Ordering::Equal); -/// assert_eq!(cmp_float(f32::INFINITY, 1.0), Ordering::Greater); -/// -/// assert_eq!(cmp_float(f32::NEG_INFINITY, f32::INFINITY), Ordering::Less); -/// assert_eq!(cmp_float(f32::NEG_INFINITY, f32::NEG_INFINITY), Ordering::Equal); -/// assert_eq!(cmp_float(f32::INFINITY, f32::NEG_INFINITY), Ordering::Greater); -/// ``` -/// -/// # Panic -/// This function panics if either floats are NaNs. -/// -/// ```rust,should_panic -/// # use cuprate_helper::num::*; -/// cmp_float(0.0, f32::NAN); -/// ``` -pub fn cmp_float(a: F, b: F) -> Ordering { - match (a <= b, a >= b) { - (false, true) => Ordering::Greater, - (true, false) => Ordering::Less, - (true, true) => Ordering::Equal, - _ => panic!("cmp_float() has failed, input: {a} - {b}"), - } -} - -#[inline] -/// Compare 2 floats, `NaN`'s will always return [`Ordering::Equal`]. -/// -/// ```rust -/// # use cuprate_helper::num::*; -/// # use core::cmp::Ordering; -/// assert_eq!(cmp_float_nan(0.0, 1.0), Ordering::Less); -/// assert_eq!(cmp_float_nan(1.0, 1.0), Ordering::Equal); -/// assert_eq!(cmp_float_nan(2.0, 1.0), Ordering::Greater); -/// -/// assert_eq!(cmp_float_nan(1.0, f32::INFINITY), Ordering::Less); -/// assert_eq!(cmp_float_nan(f32::INFINITY, f32::INFINITY), Ordering::Equal); -/// assert_eq!(cmp_float_nan(f32::INFINITY, 1.0), Ordering::Greater); -/// -/// assert_eq!(cmp_float_nan(f32::NEG_INFINITY, f32::INFINITY), Ordering::Less); -/// assert_eq!(cmp_float_nan(f32::NEG_INFINITY, f32::NEG_INFINITY), Ordering::Equal); -/// assert_eq!(cmp_float_nan(f32::INFINITY, f32::NEG_INFINITY), Ordering::Greater); -/// -/// assert_eq!(cmp_float_nan(f32::NAN, -0.0), Ordering::Equal); -/// assert_eq!(cmp_float_nan(f32::NAN, 0.0), Ordering::Equal); -/// assert_eq!(cmp_float_nan(f32::NAN, f32::NAN), Ordering::Equal); -/// assert_eq!(cmp_float_nan(f32::NAN, f32::INFINITY), Ordering::Equal); -/// assert_eq!(cmp_float_nan(f32::NAN, f32::NEG_INFINITY), Ordering::Equal); -/// ``` -pub fn cmp_float_nan(a: F, b: F) -> Ordering { - match (a <= b, a >= b) { - (false, true) => Ordering::Greater, - (true, false) => Ordering::Less, - _ => Ordering::Equal, - } -} - //---------------------------------------------------------------------------------------------------- Tests #[cfg(test)] mod test {} diff --git a/helper/src/time.rs b/helper/src/time.rs index c7b12c268..6660bb397 100644 --- a/helper/src/time.rs +++ b/helper/src/time.rs @@ -23,32 +23,6 @@ pub fn current_unix_timestamp() -> u64 { .as_secs() } -#[inline] -/// Get the clock time of a UNIX timestamp -/// -/// The input must be a UNIX timestamp. -/// -/// The returned `u64` will represent how many seconds has -/// passed on the day corresponding to that timestamp. -/// -/// The output is guaranteed to be in the range of `0..=86399`. -/// -/// ```rust -/// # use cuprate_helper::time::*; -/// // October 20th 2023 - 10:18:30 PM -/// const TIME: u64 = 1697840310; -/// -/// let seconds = unix_clock(TIME); -/// assert_eq!(seconds, 80310); -/// -/// let (h, m, s) = secs_to_clock(seconds); -/// // 10:18:30 PM. -/// assert_eq!((h, m, s), (22, 18, 30)) -/// ``` -pub const fn unix_clock(seconds_after_unix_epoch: u64) -> u32 { - (seconds_after_unix_epoch % 86400) as _ -} - #[inline] /// Convert seconds to `hours`, `minutes` and `seconds`. /// @@ -133,31 +107,6 @@ pub const fn secs_to_clock(seconds: u32) -> (u8, u8, u8) { (h as u8, m, s) } -#[inline] -/// Get the current system time in the system's timezone -/// -/// The returned value is the total amount of seconds passed in the current day. -/// -/// This is guaranteed to return a value between `0..=86399` -/// -/// This will return `0` if the underlying system call fails. -pub fn time() -> u32 { - use chrono::Timelike; - let now = chrono::offset::Local::now().time(); - (now.hour() * 3600) + (now.minute() * 60) + now.second() -} - -#[inline] -/// Get the current system time in the UTC timezone -/// -/// The returned value is the total amount of seconds passed in the current day. -/// -/// This is guaranteed to return a value between `0..=86399` -pub fn time_utc() -> u32 { - #[expect(clippy::cast_sign_loss, reason = "checked in function calls")] - unix_clock(chrono::offset::Local::now().timestamp() as u64) -} - //---------------------------------------------------------------------------------------------------- Tests #[cfg(test)] mod test {} diff --git a/p2p/p2p-core/src/lib.rs b/p2p/p2p-core/src/lib.rs index 979e5e28e..f6fb7843f 100644 --- a/p2p/p2p-core/src/lib.rs +++ b/p2p/p2p-core/src/lib.rs @@ -91,7 +91,7 @@ pub use network_zones::{ClearNet, Tor}; pub use protocol::*; use services::*; //re-export -pub use cuprate_helper::network::Network; +pub use cuprate_types::network::Network; pub use cuprate_wire::CoreSyncData; /// The direction of a connection. diff --git a/p2p/p2p-core/tests/fragmented_handshake.rs b/p2p/p2p-core/tests/fragmented_handshake.rs index 61798ebb6..2d4705aee 100644 --- a/p2p/p2p-core/tests/fragmented_handshake.rs +++ b/p2p/p2p-core/tests/fragmented_handshake.rs @@ -23,8 +23,8 @@ use tokio_util::{ }; use tower::{Service, ServiceExt}; -use cuprate_helper::network::Network; use cuprate_test_utils::monerod::monerod; +use cuprate_types::network::Network; use cuprate_wire::{ common::PeerSupportFlags, levin::{message::make_fragmented_messages, LevinMessage, Protocol}, diff --git a/p2p/p2p-core/tests/handshake.rs b/p2p/p2p-core/tests/handshake.rs index b06de454b..cf32ba65a 100644 --- a/p2p/p2p-core/tests/handshake.rs +++ b/p2p/p2p-core/tests/handshake.rs @@ -10,11 +10,11 @@ use tokio::{ use tokio_util::codec::{FramedRead, FramedWrite}; use tower::{Service, ServiceExt}; -use cuprate_helper::network::Network; use cuprate_test_utils::{ monerod::monerod, test_netzone::{TestNetZone, TestNetZoneAddr}, }; +use cuprate_types::network::Network; use cuprate_wire::{common::PeerSupportFlags, BasicNodeData, MoneroWireCodec}; use cuprate_p2p_core::{ diff --git a/p2p/p2p-core/tests/sending_receiving.rs b/p2p/p2p-core/tests/sending_receiving.rs index 683e29a38..5eeb7ff87 100644 --- a/p2p/p2p-core/tests/sending_receiving.rs +++ b/p2p/p2p-core/tests/sending_receiving.rs @@ -2,8 +2,8 @@ use tower::{Service, ServiceExt}; -use cuprate_helper::network::Network; use cuprate_test_utils::monerod::monerod; +use cuprate_types::network::Network; use cuprate_wire::{common::PeerSupportFlags, protocol::GetObjectsRequest, BasicNodeData}; use cuprate_p2p_core::{ diff --git a/p2p/p2p/src/config.rs b/p2p/p2p/src/config.rs index 2bbc0ee6d..7dd2f181c 100644 --- a/p2p/p2p/src/config.rs +++ b/p2p/p2p/src/config.rs @@ -1,5 +1,5 @@ -use cuprate_helper::network::Network; use cuprate_p2p_core::{NetworkZone, Transport}; +use cuprate_types::network::Network; use cuprate_wire::{common::PeerSupportFlags, BasicNodeData}; pub use cuprate_address_book::AddressBookConfig; diff --git a/storage/blockchain/Cargo.toml b/storage/blockchain/Cargo.toml index 34324ebc1..3411fab54 100644 --- a/storage/blockchain/Cargo.toml +++ b/storage/blockchain/Cargo.toml @@ -15,12 +15,13 @@ 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", "cuprate-helper/serde"] +serde = ["dep:serde", "cuprate-database/serde", "cuprate-database-service/serde", "cuprate-types/serde"] [dependencies] cuprate-database = { workspace = true } cuprate-database-service = { workspace = true } -cuprate-helper = { workspace = true, features = ["fs", "map", "crypto", "tx", "thread"] } +cuprate-fs = { workspace = true } +cuprate-helper = { workspace = true, features = ["map", "crypto", "tx", "thread"] } cuprate-types = { workspace = true, features = ["blockchain"] } cuprate-pruning = { workspace = true } diff --git a/storage/blockchain/src/config.rs b/storage/blockchain/src/config.rs index 4bef2cb01..c7def328c 100644 --- a/storage/blockchain/src/config.rs +++ b/storage/blockchain/src/config.rs @@ -47,10 +47,8 @@ use std::{borrow::Cow, path::PathBuf}; use serde::{Deserialize, Serialize}; use cuprate_database::{config::SyncMode, resize::ResizeAlgorithm}; -use cuprate_helper::{ - fs::{blockchain_path, CUPRATE_DATA_DIR}, - network::Network, -}; +use cuprate_fs::{blockchain_path, CUPRATE_DATA_DIR}; +use cuprate_types::network::Network; // re-exports pub use cuprate_database_service::ReaderThreads; @@ -225,7 +223,8 @@ impl Config { /// resize::ResizeAlgorithm, /// DATABASE_DATA_FILENAME, /// }; - /// use cuprate_helper::{fs::*, network::Network}; + /// use cuprate_fs::*; + /// use cuprate_types::network::Network; /// /// use cuprate_blockchain::config::*; /// diff --git a/storage/database/src/config/backend.rs b/storage/database/src/config/backend.rs index ea92b35d3..3d51b7690 100644 --- a/storage/database/src/config/backend.rs +++ b/storage/database/src/config/backend.rs @@ -10,7 +10,7 @@ use std::{ #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use cuprate_helper::fs::database_dir; +use cuprate_fs::database_dir; use crate::{ config::{ReaderThreads, SyncMode}, diff --git a/storage/service/Cargo.toml b/storage/service/Cargo.toml index 9e35e28f1..dda9fde71 100644 --- a/storage/service/Cargo.toml +++ b/storage/service/Cargo.toml @@ -16,7 +16,7 @@ redb-memory = ["cuprate-database/redb-memory"] [dependencies] cuprate-database = { workspace = true } -cuprate-helper = { workspace = true, features = ["fs", "thread", "map", "asynch"] } +cuprate-helper = { workspace = true, features = ["thread", "map", "asynch"] } serde = { workspace = true, optional = true } rayon = { workspace = true } diff --git a/storage/txpool/Cargo.toml b/storage/txpool/Cargo.toml index 63d55578e..560743ede 100644 --- a/storage/txpool/Cargo.toml +++ b/storage/txpool/Cargo.toml @@ -15,13 +15,13 @@ 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", "cuprate-helper/serde"] +serde = ["dep:serde", "cuprate-database/serde", "cuprate-database-service/serde", "cuprate-types/serde"] [dependencies] cuprate-database = { workspace = true, features = ["heed"] } cuprate-database-service = { workspace = true } cuprate-types = { workspace = true, features = ["rpc"] } -cuprate-helper = { workspace = true, default-features = false, features = ["constants"] } +cuprate-fs = { workspace = true } monero-serai = { workspace = true, features = ["std"] } bytemuck = { workspace = true, features = ["must_cast", "derive", "min_const_generics", "extern_crate_alloc"] } diff --git a/storage/txpool/src/config.rs b/storage/txpool/src/config.rs index 724ae21a3..e7b51372c 100644 --- a/storage/txpool/src/config.rs +++ b/storage/txpool/src/config.rs @@ -9,10 +9,8 @@ use cuprate_database::{ resize::ResizeAlgorithm, }; use cuprate_database_service::ReaderThreads; -use cuprate_helper::{ - fs::{txpool_path, CUPRATE_DATA_DIR}, - network::Network, -}; +use cuprate_fs::{txpool_path, CUPRATE_DATA_DIR}; +use cuprate_types::network::Network; /// The default transaction pool weight limit. const DEFAULT_TXPOOL_WEIGHT_LIMIT: usize = 600 * 1024 * 1024; @@ -209,7 +207,8 @@ impl Config { /// DATABASE_DATA_FILENAME, /// }; /// use cuprate_database_service::ReaderThreads; - /// use cuprate_helper::{fs::*, network::Network}; + /// use cuprate_fs::*; + /// use cuprate_types::network::Network; /// /// use cuprate_txpool::Config; /// diff --git a/types/types/src/lib.rs b/types/types/src/lib.rs index da76352cc..e2b505569 100644 --- a/types/types/src/lib.rs +++ b/types/types/src/lib.rs @@ -13,6 +13,7 @@ mod address_type; mod block_complete_entry; mod connection_state; mod hard_fork; +pub mod network; mod transaction_verification_data; mod types; diff --git a/helper/src/network.rs b/types/types/src/network.rs similarity index 85% rename from helper/src/network.rs rename to types/types/src/network.rs index d29611705..10da2953f 100644 --- a/helper/src/network.rs +++ b/types/types/src/network.rs @@ -1,11 +1,10 @@ -//! This module contains an enum representing every Monero network: mainnet, testnet, stagenet and functionality -//! related to that. +//! This module contains an enum representing every Monero network: //! -//! This feels out of place for the helper crate but this is needed through out Cuprate and felt too small to split -//! into it's own crate. +//! [`Network::Mainnet`] +//! [`Network::Testnet`] +//! [`Network::Stagenet`] //! -//! `#[no_std]` compatible. -// TODO: move to types crate. +//! and functionality related to that. use core::{ fmt::{Display, Formatter}, @@ -26,12 +25,9 @@ const STAGENET_NETWORK_ID: [u8; 16] = [ #[derive(Debug, Clone, Copy, Default, Ord, PartialOrd, Eq, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub enum Network { - /// Mainnet #[default] Mainnet, - /// Testnet Testnet, - /// Stagenet Stagenet, }