diff --git a/Cargo.lock b/Cargo.lock index 339858e..9272584 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,10 +1,10 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "aleo-std" -version = "0.1.24" +version = "0.1.25" dependencies = [ "aleo-std-cpu", "aleo-std-profiler", @@ -28,9 +28,10 @@ dependencies = [ [[package]] name = "aleo-std-storage" -version = "0.1.7" +version = "0.2.0" dependencies = [ "dirs", + "tempfile", ] [[package]] @@ -98,7 +99,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" dependencies = [ "lazy_static", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -131,6 +132,22 @@ dependencies = [ "indexmap", ] +[[package]] +name = "errno" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" +dependencies = [ + "libc", + "windows-sys 0.59.0", +] + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + [[package]] name = "fsio" version = "0.1.3" @@ -148,9 +165,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.12" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" dependencies = [ "cfg-if", "libc", @@ -181,9 +198,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.152" +version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" [[package]] name = "libredox" @@ -196,12 +213,24 @@ dependencies = [ "redox_syscall", ] +[[package]] +name = "linux-raw-sys" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" + [[package]] name = "nias" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab250442c86f1850815b5d268639dff018c0627022bc1940eb2d642ca1ce12f0" +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + [[package]] name = "proc-macro2" version = "1.0.76" @@ -240,6 +269,19 @@ dependencies = [ "thiserror", ] +[[package]] +name = "rustix" +version = "0.38.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a78891ee6bf2340288408954ac787aa063d8e8817e9f53abb37c695c6d834ef6" +dependencies = [ + "bitflags 2.4.2", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.59.0", +] + [[package]] name = "rusty-hook" version = "0.11.2" @@ -294,6 +336,20 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tempfile" +version = "3.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8a559c81686f576e8cd0290cd2a24a2a9ad80c98b3478856500fcbd7acd704" +dependencies = [ + "cfg-if", + "fastrand", + "getrandom", + "once_cell", + "rustix", + "windows-sys 0.59.0", +] + [[package]] name = "thiserror" version = "1.0.56" @@ -369,7 +425,16 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", ] [[package]] @@ -378,13 +443,29 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -393,38 +474,86 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + [[package]] name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + [[package]] name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index f3d62b1..e931480 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aleo-std" -version = "0.1.24" +version = "0.1.25" authors = [ "The Aleo Team " ] description = "A standard library for Aleo repositories" exclude = ["**/*.md"] @@ -13,7 +13,7 @@ members = [ "cpu", "profiler", "storage", "time", "timed", "timer" ] [dependencies] aleo-std-cpu = { path = "./cpu", version = "0.1.4", default-features = false, optional = true } aleo-std-profiler = { path = "./profiler", version = "0.1.15", default-features = false } -aleo-std-storage = { path = "./storage", version = "0.1.7", default-features = false, optional = true } +aleo-std-storage = { path = "./storage", version = "0.2.0", default-features = false, optional = true } aleo-std-time = { path = "./time", version = "0.1.2", default-features = false } aleo-std-timed = { path = "./timed", version = "0.1.2", default-features = false } aleo-std-timer = { path = "./timer", version = "0.1.2", default-features = false } diff --git a/storage/Cargo.toml b/storage/Cargo.toml index fa21d41..fd88a8b 100644 --- a/storage/Cargo.toml +++ b/storage/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aleo-std-storage" -version = "0.1.7" +version = "0.2.0" authors = [ "The Aleo Team " ] description = "Convenience methods for accessing resources in Aleo storage" license = "GPL-3.0" @@ -8,3 +8,6 @@ edition = "2018" [dependencies.dirs] version = "4.0" + +[dependencies.tempfile] +version = "3.15" diff --git a/storage/src/lib.rs b/storage/src/lib.rs index 8aee2b4..d91c2e0 100644 --- a/storage/src/lib.rs +++ b/storage/src/lib.rs @@ -15,13 +15,14 @@ // along with the aleo-std library. If not, see . use dirs::home_dir; -use std::path::PathBuf; +use std::{path::PathBuf, sync::Arc}; +use tempfile::TempDir; /// The directory name for Aleo-related resources. const ALEO_DIRECTORY: &str = ".aleo"; /// An enum to define the operating mode of the Aleo node. -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug)] pub enum StorageMode { /// The production mode is used for running a node on the Aleo mainnet. Production, @@ -29,16 +30,21 @@ pub enum StorageMode { Development(u16), /// The custom mode is used for running a node on custom configurations. Custom(PathBuf), + /// Test-only ephemeral storage which self-destructs afterwards. + Test(Option>), } -impl From> for StorageMode { - fn from(dev: Option) -> Self { - match dev { - Some(id) => StorageMode::Development(id), - None => StorageMode::Production, +impl PartialEq for StorageMode { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Self::Test(Some(tempdir1)), Self::Test(Some(tempdir2))) => { + tempdir1.path() == tempdir2.path() + }, + (a, b) => a == b, } } } +impl Eq for StorageMode {} impl From for StorageMode { fn from(id: u16) -> Self { @@ -52,13 +58,28 @@ impl From for StorageMode { } } +impl From> for StorageMode { + fn from(tempdir: Arc) -> Self { + StorageMode::Test(Some(tempdir)) + } +} + +impl From>> for StorageMode { + fn from(tempdir: Option>) -> Self { + if tempdir.is_some() { + Self::Test(tempdir) + } else { + Self::Test(Some(Arc::new(tempfile::TempDir::with_prefix("aleo_storage_").unwrap()))) + } + } +} + impl StorageMode { /// Returns the development ID if the mode is development. pub const fn dev(&self) -> Option { match self { - StorageMode::Production => None, - StorageMode::Development(id) => Some(*id), - StorageMode::Custom(_) => None, + Self::Development(id) => Some(*id), + Self::Production | Self::Custom(_) | Self::Test(_) => None, } } } @@ -86,7 +107,7 @@ pub fn aleo_dir() -> PathBuf { /// In development mode, the expected directory path is `/path/to/repo/.ledger-{network}-{id}`. /// In custom mode, the expected directory path is `/path/to/custom`. /// -pub fn aleo_ledger_dir(network: u16, mode: StorageMode) -> PathBuf { +pub fn aleo_ledger_dir(network: u16, mode: &StorageMode) -> PathBuf { // Construct the path to the ledger in storage. match mode { // In production mode, the ledger is stored in the `~/.aleo/` directory. @@ -106,7 +127,15 @@ pub fn aleo_ledger_dir(network: u16, mode: StorageMode) -> PathBuf { path } // In custom mode, the ledger files are stored in the given directory path. - StorageMode::Custom(path) => path, + StorageMode::Custom(path) => path.to_owned(), + StorageMode::Test(tempdir) => { + if let Some(tempdir) = tempdir { + tempdir.path().to_owned() + } else { + // aleo_ledger_dir is only ever called with persistent storage, where TempDir must be present. + panic!("StorageMode::Test was created in a persistent storage context without a TempDir"); + } + }, } } @@ -123,8 +152,8 @@ mod tests { fn test_aleo_ledger_dir() { println!( "{:?} exists: {:?}", - aleo_ledger_dir(2, StorageMode::Production), - aleo_ledger_dir(2, StorageMode::Production).exists() + aleo_ledger_dir(2, &StorageMode::Production), + aleo_ledger_dir(2, &StorageMode::Production).exists() ); } }