diff --git a/.circleci/config.yml b/.circleci/config.yml index 4aad608529..e099cc6929 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -15,7 +15,7 @@ parameters: default: xlarge twoxlarge: type: string - default: aleonet/2xlarge + default: 2xlarge orbs: windows: circleci/windows@5.0 @@ -945,8 +945,7 @@ workflows: - curves - fields - ledger -# TODO (howardwu) - Implement `open_testing` on all storage, update to `CurrentConsensusStore::open_testing`, then re-enable. -# - ledger-with-rocksdb + - ledger-with-rocksdb - ledger-with-valid-solutions - ledger-authority - ledger-block diff --git a/Cargo.toml b/Cargo.toml index d722810219..8b380687ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -272,7 +272,7 @@ version = "1.3" version = "0.11.2" [dev-dependencies.tempfile] -version = "3.8" +version = "3.15" [build-dependencies.walkdir] version = "2" diff --git a/algorithms/Cargo.toml b/algorithms/Cargo.toml index 3603cc1a4a..fb822b29cf 100644 --- a/algorithms/Cargo.toml +++ b/algorithms/Cargo.toml @@ -71,7 +71,8 @@ version = "=1.1.0" optional = true [dependencies.aleo-std] -version = "0.1.24" +git = "https://github.com/ljedrz/aleo-std" +branch = "feat/test_storage_mode" default-features = false [dependencies.anyhow] diff --git a/console/collections/Cargo.toml b/console/collections/Cargo.toml index 036d17bd69..7c69ebe102 100644 --- a/console/collections/Cargo.toml +++ b/console/collections/Cargo.toml @@ -29,7 +29,8 @@ default-features = false features = [ "field", "integers" ] [dependencies.aleo-std] -version = "0.1.24" +git = "https://github.com/ljedrz/aleo-std" +branch = "feat/test_storage_mode" default-features = false [dependencies.rayon] diff --git a/fields/Cargo.toml b/fields/Cargo.toml index d7935f7845..9cbd237a3a 100644 --- a/fields/Cargo.toml +++ b/fields/Cargo.toml @@ -34,7 +34,8 @@ version = "=1.1.0" default-features = false [dependencies.aleo-std] -version = "0.1.24" +git = "https://github.com/ljedrz/aleo-std" +branch = "feat/test_storage_mode" default-features = false [dependencies.anyhow] diff --git a/ledger/Cargo.toml b/ledger/Cargo.toml index 0b6502b0ce..35faf500c9 100644 --- a/ledger/Cargo.toml +++ b/ledger/Cargo.toml @@ -112,7 +112,8 @@ path = "../synthesizer" version = "=1.1.0" [dependencies.aleo-std] -version = "0.1.24" +git = "https://github.com/ljedrz/aleo-std" +branch = "feat/test_storage_mode" default-features = false [dependencies.anyhow] diff --git a/ledger/benches/transaction.rs b/ledger/benches/transaction.rs index 933b0918c0..bce9abdc3b 100644 --- a/ledger/benches/transaction.rs +++ b/ledger/benches/transaction.rs @@ -24,16 +24,21 @@ use console::{ program::{Plaintext, Record, Value}, }; use ledger_block::Transition; -use ledger_store::{ConsensusStore, helpers::memory::ConsensusMemory}; +use ledger_store::ConsensusStore; use synthesizer::{VM, program::Program}; use criterion::Criterion; use indexmap::IndexMap; +#[cfg(not(feature = "rocks"))] +type LedgerType = ledger_store::helpers::memory::ConsensusMemory; +#[cfg(feature = "rocks")] +type LedgerType = ledger_store::helpers::rocksdb::ConsensusDB; + fn initialize_vm( private_key: &PrivateKey, rng: &mut R, -) -> (VM>, Vec>>) { +) -> (VM>, Vec>>) { // Initialize the VM. let vm = VM::from(ConsensusStore::open(None).unwrap()).unwrap(); diff --git a/ledger/puzzle/Cargo.toml b/ledger/puzzle/Cargo.toml index a7b15e7b17..6d403d76ca 100644 --- a/ledger/puzzle/Cargo.toml +++ b/ledger/puzzle/Cargo.toml @@ -47,7 +47,8 @@ path = "../../algorithms" version = "=1.1.0" [dependencies.aleo-std] -version = "0.1.24" +git = "https://github.com/ljedrz/aleo-std" +branch = "feat/test_storage_mode" default-features = false [dependencies.anyhow] diff --git a/ledger/puzzle/epoch/Cargo.toml b/ledger/puzzle/epoch/Cargo.toml index 94ba127cf4..795ade3106 100644 --- a/ledger/puzzle/epoch/Cargo.toml +++ b/ledger/puzzle/epoch/Cargo.toml @@ -64,7 +64,8 @@ version = "=1.1.0" optional = true [dependencies.aleo-std] -version = "0.1.24" +git = "https://github.com/ljedrz/aleo-std" +branch = "feat/test_storage_mode" default-features = false optional = true diff --git a/ledger/src/get.rs b/ledger/src/get.rs index 162dd2c247..73620b3401 100644 --- a/ledger/src/get.rs +++ b/ledger/src/get.rs @@ -292,25 +292,3 @@ impl> Ledger { .collect::>() } } - -#[cfg(test)] -mod tests { - use super::*; - use crate::test_helpers::CurrentLedger; - use console::network::MainnetV0; - - type CurrentNetwork = MainnetV0; - - #[test] - fn test_get_block() { - // Load the genesis block. - let genesis = Block::from_bytes_le(CurrentNetwork::genesis_bytes()).unwrap(); - - // Initialize a new ledger. - let ledger = CurrentLedger::load(genesis.clone(), StorageMode::Production).unwrap(); - // Retrieve the genesis block. - let candidate = ledger.get_block(0).unwrap(); - // Ensure the genesis block matches. - assert_eq!(genesis, candidate); - } -} diff --git a/ledger/src/lib.rs b/ledger/src/lib.rs index 4d7465c744..b7121c91f5 100644 --- a/ledger/src/lib.rs +++ b/ledger/src/lib.rs @@ -390,7 +390,6 @@ pub(crate) mod test_helpers { network::MainnetV0, prelude::*, }; - use ledger_block::Block; use ledger_store::ConsensusStore; use snarkvm_circuit::network::AleoV0; use synthesizer::vm::VM; @@ -430,10 +429,6 @@ pub(crate) mod test_helpers { TestEnv { ledger, private_key, view_key, address } } - pub(crate) fn sample_genesis_block() -> Block { - Block::::from_bytes_le(CurrentNetwork::genesis_bytes()).unwrap() - } - pub(crate) fn sample_ledger( private_key: PrivateKey, rng: &mut (impl Rng + CryptoRng), diff --git a/ledger/src/tests.rs b/ledger/src/tests.rs index cc5989c0e5..11819e3c72 100644 --- a/ledger/src/tests.rs +++ b/ledger/src/tests.rs @@ -30,7 +30,7 @@ use ledger_authority::Authority; use ledger_block::{Block, ConfirmedTransaction, Execution, Ratify, Rejected, Transaction}; use ledger_committee::{Committee, MIN_VALIDATOR_STAKE}; use ledger_narwhal::{BatchCertificate, BatchHeader, Data, Subdag, Transmission, TransmissionID}; -use ledger_store::{ConsensusStore, helpers::memory::ConsensusMemory}; +use ledger_store::ConsensusStore; use snarkvm_utilities::try_vm_runtime; use synthesizer::{Stack, program::Program, vm::VM}; @@ -39,9 +39,14 @@ use rand::seq::SliceRandom; use std::collections::{BTreeMap, HashMap}; use time::OffsetDateTime; +#[cfg(not(feature = "rocks"))] +type LedgerType = ledger_store::helpers::memory::ConsensusMemory; +#[cfg(feature = "rocks")] +type LedgerType = ledger_store::helpers::rocksdb::ConsensusDB; + /// Initializes a sample VM. -fn sample_vm() -> VM> { - VM::from(ConsensusStore::>::open(None).unwrap()).unwrap() +fn sample_vm() -> VM> { + VM::from(ConsensusStore::>::open(None).unwrap()).unwrap() } /// Extract the transmissions from a block. @@ -71,8 +76,7 @@ fn construct_quorum_blocks( ) -> Vec> { // Initialize the ledger with the genesis block. let ledger = - Ledger::>::load(genesis.clone(), StorageMode::Production) - .unwrap(); + Ledger::>::load(genesis.clone(), StorageMode::Production).unwrap(); // Initialize the round parameters. assert!(num_blocks > 0); @@ -121,7 +125,7 @@ fn construct_quorum_blocks( // Helper function to create a quorum block. fn create_next_quorum_block( - ledger: &Ledger>, + ledger: &Ledger>, round: u64, leader_certificate: &BatchCertificate, previous_leader_certificate: Option<&BatchCertificate>, @@ -182,7 +186,7 @@ fn test_load() { // Sample the genesis private key. let private_key = PrivateKey::::new(rng).unwrap(); // Initialize the store. - let store = ConsensusStore::<_, ConsensusMemory<_>>::open(None).unwrap(); + let store = ConsensusStore::<_, LedgerType<_>>::open(None).unwrap(); // Create a genesis block. let genesis = VM::from(store).unwrap().genesis_beacon(&private_key, rng).unwrap(); @@ -196,8 +200,14 @@ fn test_load() { #[test] fn test_load_unchecked() { - // Load the genesis block. - let genesis = crate::test_helpers::sample_genesis_block(); + let rng = &mut TestRng::default(); + + // Sample the genesis private key. + let private_key = PrivateKey::::new(rng).unwrap(); + // Initialize the store. + let store = ConsensusStore::<_, LedgerType<_>>::open(None).unwrap(); + // Create a genesis block. + let genesis = VM::from(store).unwrap().genesis_beacon(&private_key, rng).unwrap(); // Initialize the ledger without checks. let ledger = CurrentLedger::load_unchecked(genesis.clone(), StorageMode::Production).unwrap(); @@ -214,6 +224,25 @@ fn test_load_unchecked() { assert_eq!(ledger.latest_block(), genesis); } +#[test] +fn test_get_block() { + let rng = &mut TestRng::default(); + + // Sample the genesis private key. + let private_key = PrivateKey::::new(rng).unwrap(); + // Initialize the store. + let store = ConsensusStore::<_, LedgerType<_>>::open(None).unwrap(); + // Create a genesis block. + let genesis = VM::from(store).unwrap().genesis_beacon(&private_key, rng).unwrap(); + + // Initialize a new ledger. + let ledger = CurrentLedger::load(genesis.clone(), StorageMode::Production).unwrap(); + // Retrieve the genesis block. + let candidate = ledger.get_block(0).unwrap(); + // Ensure the genesis block matches. + assert_eq!(genesis, candidate); +} + #[test] fn test_state_path() { let rng = &mut TestRng::default(); @@ -1990,8 +2019,7 @@ fn test_max_committee_limit_with_bonds() { // Initialize a Ledger from the genesis block. let ledger = - Ledger::>::load(genesis_block, StorageMode::Production) - .unwrap(); + Ledger::>::load(genesis_block, StorageMode::Production).unwrap(); // Bond the first validator. let bond_first_transaction = ledger @@ -3050,7 +3078,7 @@ fn test_forged_block_subdags() { // Sample the genesis private key. let private_key = PrivateKey::::new(rng).unwrap(); // Initialize the store. - let store = ConsensusStore::<_, ConsensusMemory<_>>::open(None).unwrap(); + let store = ConsensusStore::<_, LedgerType<_>>::open(None).unwrap(); // Create a genesis block with a seeded RNG to reproduce the same genesis private keys. let seed: u64 = rng.gen(); let genesis_rng = &mut TestRng::from_seed(seed); @@ -3074,8 +3102,7 @@ fn test_forged_block_subdags() { let block_3 = quorum_blocks.remove(0); // Construct the ledger. - let ledger = - Ledger::>::load(genesis, StorageMode::Production).unwrap(); + let ledger = Ledger::>::load(genesis, StorageMode::Production).unwrap(); ledger.advance_to_next_block(&block_1).unwrap(); ledger.check_next_block(&block_2, rng).unwrap(); diff --git a/ledger/store/Cargo.toml b/ledger/store/Cargo.toml index 0b9d22a7a3..7d7200aee9 100644 --- a/ledger/store/Cargo.toml +++ b/ledger/store/Cargo.toml @@ -80,7 +80,8 @@ path = "../../synthesizer/snark" version = "=1.1.0" [dependencies.aleo-std-storage] -version = "0.1.7" +git = "https://github.com/ljedrz/aleo-std" +branch = "feat/test_storage_mode" default-features = false [dependencies.anyhow] @@ -136,11 +137,8 @@ features = [ "test-helpers" ] package = "snarkvm-ledger-test-helpers" path = "../../ledger/test-helpers" -[dev-dependencies.serial_test] -version = "2" - [dev-dependencies.tempfile] -version = "3.8" +version = "3.15" [dev-dependencies.tracing-test] version = "0.2" diff --git a/ledger/store/src/block/mod.rs b/ledger/store/src/block/mod.rs index 88ed88b169..5b1a9cf6e3 100644 --- a/ledger/store/src/block/mod.rs +++ b/ledger/store/src/block/mod.rs @@ -146,7 +146,7 @@ pub trait BlockStorage: 'static + Clone + Send + Sync { type TransitionStorage: TransitionStorage; /// Initializes the block storage. - fn open>(storage: S) -> Result; + fn open>(storage: S) -> Result; /// Returns the state root map. fn state_root_map(&self) -> &Self::StateRootMap; @@ -1003,7 +1003,7 @@ pub struct BlockStore> { impl> BlockStore { /// Initializes the block store. - pub fn open>(storage: S) -> Result { + pub fn open>(storage: S) -> Result { // Initialize the block storage. let storage = B::open(storage)?; diff --git a/ledger/store/src/consensus/mod.rs b/ledger/store/src/consensus/mod.rs index 4f46f46fb8..83de0acacc 100644 --- a/ledger/store/src/consensus/mod.rs +++ b/ledger/store/src/consensus/mod.rs @@ -41,7 +41,7 @@ pub trait ConsensusStorage: 'static + Clone + Send + Sync { type TransitionStorage: TransitionStorage; /// Initializes the consensus storage. - fn open>(storage: S) -> Result; + fn open>(storage: S) -> Result; /// Returns the finalize storage. fn finalize_store(&self) -> &FinalizeStore; @@ -115,9 +115,9 @@ pub struct ConsensusStore> { impl> ConsensusStore { /// Initializes the consensus store. - pub fn open>(storage: S) -> Result { + pub fn open>(storage: S) -> Result { // Initialize the consensus storage. - let storage = C::open(storage.clone())?; + let storage = C::open(storage.into())?; // Return the consensus store. Ok(Self { storage, _phantom: PhantomData }) } diff --git a/ledger/store/src/helpers/memory/block.rs b/ledger/store/src/helpers/memory/block.rs index 04ee35c308..8c818b34cb 100644 --- a/ledger/store/src/helpers/memory/block.rs +++ b/ledger/store/src/helpers/memory/block.rs @@ -94,7 +94,7 @@ impl BlockStorage for BlockMemory { type TransitionStorage = TransitionMemory; /// Initializes the block storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { // Initialize the transition store. let transition_store = TransitionStore::>::open(storage)?; // Initialize the transaction store. diff --git a/ledger/store/src/helpers/memory/consensus.rs b/ledger/store/src/helpers/memory/consensus.rs index b29a1cad6e..0e580eed10 100644 --- a/ledger/store/src/helpers/memory/consensus.rs +++ b/ledger/store/src/helpers/memory/consensus.rs @@ -40,7 +40,8 @@ impl ConsensusStorage for ConsensusMemory { type TransitionStorage = TransitionMemory; /// Initializes the consensus storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { + let storage = storage.into(); // Initialize the finalize store. let finalize_store = FinalizeStore::>::open(storage.clone())?; // Initialize the block store. diff --git a/ledger/store/src/helpers/memory/program.rs b/ledger/store/src/helpers/memory/program.rs index b48edf62c0..8c2ca38a47 100644 --- a/ledger/store/src/helpers/memory/program.rs +++ b/ledger/store/src/helpers/memory/program.rs @@ -50,7 +50,8 @@ impl FinalizeStorage for FinalizeMemory { type KeyValueMap = NestedMemoryMap<(ProgramID, Identifier), Plaintext, Value>; /// Initializes the finalize storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { + let storage = storage.into(); // Initialize the committee store. let committee_store = CommitteeStore::>::open(storage.clone())?; // Return the finalize store. @@ -58,16 +59,10 @@ impl FinalizeStorage for FinalizeMemory { committee_store, program_id_map: MemoryMap::default(), key_value_map: NestedMemoryMap::default(), - storage_mode: storage.into(), + storage_mode: storage, }) } - /// Initializes the test-variant of the storage. - #[cfg(any(test, feature = "test"))] - fn open_testing(_: std::path::PathBuf, dev: Option) -> Result { - Self::open(dev) - } - /// Returns the committee store. fn committee_store(&self) -> &CommitteeStore { &self.committee_store @@ -109,7 +104,7 @@ impl CommitteeStorage for CommitteeMemory { type CommitteeMap = MemoryMap>; /// Initializes the committee storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { Ok(Self { current_round_map: MemoryMap::default(), round_to_height_map: MemoryMap::default(), @@ -118,12 +113,6 @@ impl CommitteeStorage for CommitteeMemory { }) } - /// Initializes the test-variant of the storage. - #[cfg(any(test, feature = "test"))] - fn open_testing(_: std::path::PathBuf, dev: Option) -> Result { - Self::open(dev) - } - /// Returns the current round map. fn current_round_map(&self) -> &Self::CurrentRoundMap { &self.current_round_map diff --git a/ledger/store/src/helpers/memory/transition.rs b/ledger/store/src/helpers/memory/transition.rs index 1b1b56be27..2f997029f2 100644 --- a/ledger/store/src/helpers/memory/transition.rs +++ b/ledger/store/src/helpers/memory/transition.rs @@ -55,7 +55,8 @@ impl TransitionStorage for TransitionMemory { type SCMMap = MemoryMap>; /// Initializes the transition storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { + let storage = storage.into(); Ok(Self { locator_map: MemoryMap::default(), input_store: InputStore::open(storage.clone())?, @@ -144,7 +145,7 @@ impl InputStorage for InputMemory { type ExternalRecordMap = MemoryMap, ()>; /// Initializes the transition input storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { Ok(Self { id_map: MemoryMap::default(), reverse_id_map: MemoryMap::default(), @@ -243,7 +244,7 @@ impl OutputStorage for OutputMemory { type FutureMap = MemoryMap, Option>>; /// Initializes the transition output storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { Ok(Self { id_map: Default::default(), reverse_id_map: Default::default(), diff --git a/ledger/store/src/helpers/rocksdb/block.rs b/ledger/store/src/helpers/rocksdb/block.rs index 97a86cc3d1..d0b486bf11 100644 --- a/ledger/store/src/helpers/rocksdb/block.rs +++ b/ledger/store/src/helpers/rocksdb/block.rs @@ -100,7 +100,8 @@ impl BlockStorage for BlockDB { type TransitionStorage = TransitionDB; /// Initializes the block storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { + let storage = storage.into(); // Initialize the transition store. let transition_store = TransitionStore::>::open(storage.clone())?; // Initialize the transaction store. diff --git a/ledger/store/src/helpers/rocksdb/consensus.rs b/ledger/store/src/helpers/rocksdb/consensus.rs index 0ff5df0aff..8cac0a4b81 100644 --- a/ledger/store/src/helpers/rocksdb/consensus.rs +++ b/ledger/store/src/helpers/rocksdb/consensus.rs @@ -40,7 +40,8 @@ impl ConsensusStorage for ConsensusDB { type TransitionStorage = TransitionDB; /// Initializes the consensus storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { + let storage = storage.into(); // Initialize the finalize store. let finalize_store = FinalizeStore::>::open(storage.clone())?; // Initialize the block store. diff --git a/ledger/store/src/helpers/rocksdb/internal/map.rs b/ledger/store/src/helpers/rocksdb/internal/map.rs index c4c6c0a02b..ee7c32c31a 100644 --- a/ledger/store/src/helpers/rocksdb/internal/map.rs +++ b/ledger/store/src/helpers/rocksdb/internal/map.rs @@ -558,15 +558,14 @@ mod tests { FinalizeMode, atomic_batch_scope, atomic_finalize, - helpers::rocksdb::{MapID, TestMap, internal::tests::temp_dir}, + helpers::rocksdb::{MapID, TestMap}, }; use console::{ account::{Address, FromStr}, network::MainnetV0, }; - use anyhow::anyhow; - use serial_test::serial; + use anyhow::{anyhow, bail}; use tracing_test::traced_test; type CurrentNetwork = MainnetV0; @@ -600,7 +599,7 @@ mod tests { impl TestStorage { fn open() -> Self { // Initialize a database. - let database = RocksDB::open_testing(temp_dir(), None).expect("Failed to open a test database"); + let database = RocksDB::open(0, None).expect("Failed to open a test database"); Self { own_map: open_map_testing_from_db(database.clone(), MapID::Test(TestMap::Test)), @@ -733,7 +732,6 @@ mod tests { } #[test] - #[serial] fn test_contains_key_sanity_check() { // Initialize an address. let address = @@ -742,73 +740,67 @@ mod tests { // Initialize a map. let map: DataMap, ()> = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); map.insert(address, ()).expect("Failed to insert into data map"); assert!(map.contains_key_confirmed(&address).unwrap()); } #[test] - #[serial] #[traced_test] fn test_insert_and_get_speculative() { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::map::check_insert_and_get_speculative(map); } #[test] - #[serial] #[traced_test] fn test_remove_and_get_speculative() { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::map::check_remove_and_get_speculative(map); } #[test] - #[serial] #[traced_test] fn test_contains_key() { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::map::check_contains_key(map); } #[test] - #[serial] #[traced_test] fn test_check_iterators_match() { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::map::check_iterators_match(map); } #[test] - #[serial] #[traced_test] fn test_atomic_writes_are_batched() { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::map::check_atomic_writes_are_batched(map); } #[test] - #[serial] #[traced_test] fn test_atomic_writes_can_be_aborted() { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::map::check_atomic_writes_can_be_aborted(map); } @@ -820,7 +812,7 @@ mod tests { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -894,7 +886,7 @@ mod tests { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -956,7 +948,7 @@ mod tests { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1010,7 +1002,7 @@ mod tests { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1089,7 +1081,7 @@ mod tests { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1167,7 +1159,7 @@ mod tests { fn test_atomic_finalize_fails_to_start() { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1200,7 +1192,7 @@ mod tests { fn test_atomic_checkpoint_truncation() { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1258,7 +1250,7 @@ mod tests { fn test_atomic_finalize_with_nested_batch_scope() -> Result<()> { // Initialize a map. let map: DataMap = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); + RocksDB::open_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1376,7 +1368,6 @@ mod tests { } #[test] - #[serial] #[traced_test] fn test_nested_atomic_write_batch_success() -> Result<()> { // Initialize a multi-layer test storage. @@ -1451,7 +1442,6 @@ mod tests { } #[test] - #[serial] #[traced_test] fn test_nested_atomic_write_batch_failure() { // We'll want to execute the atomic write batch in its own function, in order to be able to diff --git a/ledger/store/src/helpers/rocksdb/internal/mod.rs b/ledger/store/src/helpers/rocksdb/internal/mod.rs index a5c5d3c7e4..1e065155e3 100644 --- a/ledger/store/src/helpers/rocksdb/internal/mod.rs +++ b/ledger/store/src/helpers/rocksdb/internal/mod.rs @@ -27,14 +27,16 @@ mod tests; use aleo_std_storage::StorageMode; use anyhow::{Result, bail, ensure}; -use once_cell::sync::OnceCell; +use once_cell::sync::Lazy; use parking_lot::Mutex; use serde::{Serialize, de::DeserializeOwned}; use std::{ borrow::Borrow, + collections::HashMap, marker::PhantomData, mem, ops::Deref, + path::PathBuf, sync::{ Arc, atomic::{AtomicBool, AtomicUsize, Ordering}, @@ -43,19 +45,19 @@ use std::{ pub const PREFIX_LEN: usize = 4; // N::ID (u16) + DataID (u16) +// A static map of database paths to their objects; it's needed in order to facilitate concurrent +// tests involving persistent storage, but it only ever has a single member outside of them. +// TODO: remove the static in favor of improved `open` methods. +static DATABASES: Mutex>> = Mutex::new(Lazy::new(HashMap::new)); + pub trait Database { /// Opens the database. - fn open>(network_id: u16, storage: S) -> Result + fn open>(network_id: u16, storage: S) -> Result where Self: Sized; /// Opens the map with the given `network_id`, `storage mode`, and `map_id` from storage. - fn open_map< - S: Clone + Into, - K: Serialize + DeserializeOwned, - V: Serialize + DeserializeOwned, - T: Into, - >( + fn open_map, K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned, T: Into>( network_id: u16, storage: S, map_id: T, @@ -63,7 +65,7 @@ pub trait Database { /// Opens the nested map with the given `network_id`, `storage mode`, and `map_id` from storage. fn open_nested_map< - S: Clone + Into, + S: Into, M: Serialize + DeserializeOwned, K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned, @@ -122,12 +124,16 @@ impl Database for RocksDB { /// /// In production mode, the database opens directory `~/.aleo/storage/ledger-{network}`. /// In development mode, the database opens directory `/path/to/repo/.ledger-{network}-{id}`. - fn open>(network_id: u16, storage: S) -> Result { - static DB: OnceCell = OnceCell::new(); + /// In tests, the database opens an ephemeral directory in the OS temporary folder. + fn open>(network_id: u16, storage: S) -> Result { + let storage = storage.into(); // Retrieve the database. - let database = DB - .get_or_try_init(|| { + let db_path = aleo_std_storage::aleo_ledger_dir(network_id, &storage); + let database = DATABASES + .lock() + .entry(db_path.clone()) + .or_insert_with(|| { // Customize database options. let mut options = rocksdb::Options::default(); options.set_compression_type(rocksdb::DBCompressionType::Lz4); @@ -136,30 +142,29 @@ impl Database for RocksDB { let prefix_extractor = rocksdb::SliceTransform::create_fixed_prefix(PREFIX_LEN); options.set_prefix_extractor(prefix_extractor); - let primary = aleo_std_storage::aleo_ledger_dir(network_id, storage.clone().into()); let rocksdb = { options.increase_parallelism(2); options.set_max_background_jobs(4); options.create_if_missing(true); options.set_max_open_files(8192); - Arc::new(rocksdb::DB::open(&options, primary)?) + Arc::new(rocksdb::DB::open(&options, db_path).expect("Couldn't open the database")) }; - Ok::<_, anyhow::Error>(RocksDB { + RocksDB { rocksdb, network_id, - storage_mode: storage.clone().into(), + storage_mode: storage.clone(), atomic_batch: Default::default(), atomic_depth: Default::default(), atomic_writes_paused: Default::default(), default_readopts: Default::default(), - }) - })? + } + }) .clone(); // Ensure the database network ID and storage mode match. - match database.network_id == network_id && database.storage_mode == storage.into() { + match database.network_id == network_id && database.storage_mode == storage { true => Ok(database), false => bail!("Mismatching network ID or storage mode in the database"), } @@ -167,7 +172,7 @@ impl Database for RocksDB { /// Opens the map with the given `network_id`, `storage mode`, and `map_id` from storage. fn open_map< - S: Clone + Into, + S: Into, K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned, T: Into, @@ -195,7 +200,7 @@ impl Database for RocksDB { /// Opens the nested map with the given `network_id`, `storage mode`, and `map_id` from storage. fn open_nested_map< - S: Clone + Into, + S: Into, M: Serialize + DeserializeOwned, K: Serialize + DeserializeOwned, V: Serialize + DeserializeOwned, @@ -273,123 +278,6 @@ impl RocksDB { fn are_atomic_writes_paused(&self) -> bool { self.atomic_writes_paused.load(Ordering::SeqCst) } - - /// Opens the test database. - #[cfg(any(test, feature = "test"))] - pub fn open_testing(temp_dir: std::path::PathBuf, dev: Option) -> Result { - use console::prelude::{Rng, TestRng}; - - // Ensure the `temp_dir` is unique. - let temp_dir = temp_dir.join(Rng::gen::(&mut TestRng::default()).to_string()); - - // Construct the directory for the test database. - let primary = match dev { - Some(dev) => temp_dir.join(dev.to_string()), - None => temp_dir, - }; - - // Prepare the storage mode. - let storage_mode = StorageMode::from(primary.clone()); - - let database = { - // Customize database options. - let mut options = rocksdb::Options::default(); - options.set_compression_type(rocksdb::DBCompressionType::Lz4); - - // Register the prefix length. - let prefix_extractor = rocksdb::SliceTransform::create_fixed_prefix(PREFIX_LEN); - options.set_prefix_extractor(prefix_extractor); - - let rocksdb = { - options.increase_parallelism(2); - options.set_max_background_jobs(4); - options.create_if_missing(true); - - // Keep these around as options for configuration testing. - - // options.set_max_subcompactions(4); - // options.set_use_direct_io_for_flush_and_compaction(true); - // options.set_bytes_per_sync(1 << 28); - // options.set_compaction_readahead_size(1 << 28); - // options.set_max_write_buffer_number(16); - // options.set_min_write_buffer_number_to_merge(8); - // options.set_compression_type(rocksdb::DBCompressionType::None); - // options.set_bottommost_compression_type(rocksdb::DBCompressionType::None); - // options.set_write_buffer_size(1 << 28); - - Arc::new(rocksdb::DB::open(&options, primary)?) - }; - - Ok::<_, anyhow::Error>(RocksDB { - rocksdb, - network_id: u16::MAX, - storage_mode: storage_mode.clone(), - atomic_batch: Default::default(), - atomic_depth: Default::default(), - atomic_writes_paused: Default::default(), - default_readopts: Default::default(), - }) - }?; - - // Ensure the database storage mode match. - match database.storage_mode == storage_mode { - true => Ok(database), - false => bail!("Mismatching storage mode in the test database"), - } - } - - /// Opens the test map. - #[cfg(any(test, feature = "test"))] - pub fn open_map_testing>( - temp_dir: std::path::PathBuf, - dev: Option, - map_id: T, - ) -> Result> { - // Open the RocksDB test database. - let database = Self::open_testing(temp_dir, dev)?; - - // Combine contexts to create a new scope. - let mut context = database.network_id.to_le_bytes().to_vec(); - context.extend_from_slice(&(map_id.into()).to_le_bytes()); - - // Return the DataMap. - Ok(DataMap(Arc::new(InnerDataMap { - database, - context, - batch_in_progress: Default::default(), - atomic_batch: Default::default(), - checkpoints: Default::default(), - }))) - } - - /// Opens the test nested map. - #[cfg(any(test, feature = "test"))] - pub fn open_nested_map_testing< - M: Serialize + DeserializeOwned, - K: Serialize + DeserializeOwned, - V: Serialize + DeserializeOwned, - T: Into, - >( - temp_dir: std::path::PathBuf, - dev: Option, - map_id: T, - ) -> Result> { - // Open the RocksDB test database. - let database = Self::open_testing(temp_dir, dev)?; - - // Combine contexts to create a new scope. - let mut context = database.network_id.to_le_bytes().to_vec(); - context.extend_from_slice(&(map_id.into()).to_le_bytes()); - - // Return the DataMap. - Ok(NestedDataMap { - database, - context, - batch_in_progress: Default::default(), - atomic_batch: Default::default(), - checkpoints: Default::default(), - }) - } } // impl RocksDB { diff --git a/ledger/store/src/helpers/rocksdb/internal/nested_map.rs b/ledger/store/src/helpers/rocksdb/internal/nested_map.rs index 245b50d913..e0e106082d 100644 --- a/ledger/store/src/helpers/rocksdb/internal/nested_map.rs +++ b/ledger/store/src/helpers/rocksdb/internal/nested_map.rs @@ -766,7 +766,7 @@ mod tests { atomic_batch_scope, atomic_finalize, helpers::{ - rocksdb::{MapID, TestMap, internal::tests::temp_dir}, + rocksdb::{MapID, TestMap}, traits::Map, }, }; @@ -775,8 +775,7 @@ mod tests { network::MainnetV0, }; - use anyhow::anyhow; - use serial_test::serial; + use anyhow::{anyhow, bail}; use tracing_test::traced_test; type CurrentNetwork = MainnetV0; @@ -837,7 +836,7 @@ mod tests { impl TestStorage { fn open() -> Self { // Initialize a database. - let database = RocksDB::open_testing(temp_dir(), None).expect("Failed to open a test database"); + let database = RocksDB::open(0, None).expect("Failed to open a test database"); Self { own_map: open_map_testing_from_db(database.clone(), MapID::Test(TestMap::Test)), @@ -979,7 +978,6 @@ mod tests { } #[test] - #[serial] fn test_contains_key_sanity_check() { // Initialize 'm'. let m = 0usize; @@ -990,78 +988,66 @@ mod tests { // Initialize a map. let map: NestedDataMap, ()> = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); map.insert(m, address, ()).expect("Failed to insert into data map"); assert!(map.contains_key_confirmed(&m, &address).unwrap()); } #[test] - #[serial] #[traced_test] fn test_insert_and_get_value_speculative() { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::nested_map::check_insert_and_get_value_speculative(map); } #[test] - #[serial] #[traced_test] fn test_remove_key_and_get_value_speculative() { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::nested_map::check_remove_and_get_value_speculative(map); } #[test] - #[serial] #[traced_test] fn test_contains_key() { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::nested_map::check_contains_key(map); } #[test] - #[serial] #[traced_test] fn test_get_map() { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::nested_map::check_get_map(map); } #[test] - #[serial] #[traced_test] fn test_check_iterators_match() { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::nested_map::check_iterators_match(map); } #[test] - #[serial] #[traced_test] fn test_iter_from_nested_to_non_nested() { // Open a storage with a DataMap right after a NestedDataMap. - let database = RocksDB::open_testing(temp_dir(), None).expect("Failed to open a test database"); + let database = RocksDB::open(0, None).expect("Failed to open a test database"); let test_storage = TestStorage3::open(database); // Insert 5 (confirmed) records into a nested map 77. @@ -1080,25 +1066,21 @@ mod tests { } #[test] - #[serial] #[traced_test] fn test_atomic_writes_are_batched() { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::nested_map::check_atomic_writes_are_batched(map); } #[test] - #[serial] #[traced_test] fn test_atomic_writes_can_be_aborted() { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); crate::helpers::test_helpers::nested_map::check_atomic_writes_can_be_aborted(map); } @@ -1110,8 +1092,7 @@ mod tests { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1185,8 +1166,7 @@ mod tests { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1248,8 +1228,7 @@ mod tests { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1303,8 +1282,7 @@ mod tests { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1383,8 +1361,7 @@ mod tests { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1462,8 +1439,7 @@ mod tests { fn test_atomic_finalize_fails_to_start() { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1496,8 +1472,7 @@ mod tests { fn test_atomic_checkpoint_truncation() { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1558,8 +1533,7 @@ mod tests { fn test_atomic_finalize_with_nested_batch_scope() -> Result<()> { // Initialize a map. let map: NestedDataMap = - RocksDB::open_nested_map_testing(temp_dir(), None, MapID::Test(TestMap::Test)) - .expect("Failed to open data map"); + RocksDB::open_nested_map(0, None, MapID::Test(TestMap::Test)).expect("Failed to open data map"); // Sanity check. assert!(map.iter_confirmed().next().is_none()); // Make sure the checkpoint index is None. @@ -1677,7 +1651,6 @@ mod tests { } #[test] - #[serial] #[traced_test] fn test_nested_atomic_write_batch_success() -> Result<()> { // Initialize a multi-layer test storage. @@ -1748,7 +1721,6 @@ mod tests { } #[test] - #[serial] #[traced_test] fn test_nested_atomic_write_batch_failure() { // We'll want to execute the atomic write batch in its own function, in order to be able to diff --git a/ledger/store/src/helpers/rocksdb/internal/tests.rs b/ledger/store/src/helpers/rocksdb/internal/tests.rs index cbae47a770..c3bb50e331 100644 --- a/ledger/store/src/helpers/rocksdb/internal/tests.rs +++ b/ledger/store/src/helpers/rocksdb/internal/tests.rs @@ -16,7 +16,7 @@ use crate::helpers::{ Map, MapRead, - rocksdb::{MapID, RocksDB, TestMap as TestMapID}, + rocksdb::{Database, MapID, RocksDB, TestMap as TestMapID}, }; use console::{ network::{MainnetV0, Network}, @@ -24,34 +24,24 @@ use console::{ types::Scalar, }; -use serial_test::serial; - -pub(crate) fn temp_dir() -> std::path::PathBuf { - tempfile::tempdir().expect("Failed to open temporary directory").into_path() -} - // pub(crate) fn temp_file() -> std::path::PathBuf { // tempfile::NamedTempFile::new().expect("Failed to open temporary file").path().to_owned() // } #[test] -#[serial] fn test_open() { - let _storage = RocksDB::open_testing(temp_dir(), None).expect("Failed to open storage"); + let _storage = RocksDB::open(0, None).expect("Failed to open storage"); } #[test] -#[serial] fn test_open_map() { - let _map = RocksDB::open_map_testing::(temp_dir(), None, MapID::Test(TestMapID::Test)) - .expect("Failed to open data map"); + let _map = + RocksDB::open_map::<_, u32, String, _>(0, None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); } #[test] -#[serial] fn test_insert_and_contains_key() { - let map = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); + let map = RocksDB::open_map(0, None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); map.insert(123456789, "123456789".to_string()).expect("Failed to insert"); assert!(map.contains_key_confirmed(&123456789).expect("Failed to call contains key")); @@ -59,10 +49,8 @@ fn test_insert_and_contains_key() { } #[test] -#[serial] fn test_insert_and_get() { - let map = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); + let map = RocksDB::open_map(0, None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); map.insert(123456789, "123456789".to_string()).expect("Failed to insert"); assert_eq!( @@ -74,10 +62,8 @@ fn test_insert_and_get() { } #[test] -#[serial] fn test_insert_and_remove() { - let map = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); + let map = RocksDB::open_map(0, None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); map.insert(123456789, "123456789".to_string()).expect("Failed to insert"); assert_eq!( @@ -90,10 +76,8 @@ fn test_insert_and_remove() { } #[test] -#[serial] fn test_insert_and_iter() { - let map = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); + let map = RocksDB::open_map(0, None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); map.insert(123456789, "123456789".to_string()).expect("Failed to insert"); @@ -103,10 +87,8 @@ fn test_insert_and_iter() { } #[test] -#[serial] fn test_insert_and_keys() { - let map = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); + let map = RocksDB::open_map(0, None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); map.insert(123456789, "123456789".to_string()).expect("Failed to insert"); @@ -116,10 +98,8 @@ fn test_insert_and_keys() { } #[test] -#[serial] fn test_insert_and_values() { - let map = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); + let map = RocksDB::open_map(0, None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); map.insert(123456789, "123456789".to_string()).expect("Failed to insert"); @@ -129,11 +109,10 @@ fn test_insert_and_values() { } // #[test] -// #[serial] // fn test_export_import() { // let file = temp_file(); // { -// let mut map = RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); +// let mut map = RocksDB::open_map(0, None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); // // for i in 0..100 { // map.insert(i, i.to_string()).expect("Failed to insert"); @@ -153,7 +132,6 @@ fn test_insert_and_values() { // } #[test] -#[serial] fn test_scalar_mul() { type CurrentNetwork = MainnetV0; @@ -161,8 +139,7 @@ fn test_scalar_mul() { const ITERATIONS: u32 = 1_000_000u32; - let map = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); + let map = RocksDB::open_map(0, None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); // Sample `ITERATION` random field elements to store. for i in 0..ITERATIONS { @@ -182,10 +159,8 @@ fn test_scalar_mul() { } #[test] -#[serial] fn test_iterator_ordering() { - let map = - RocksDB::open_map_testing(temp_dir(), None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); + let map = RocksDB::open_map(0, None, MapID::Test(TestMapID::Test)).expect("Failed to open data map"); // Insert values into the map. map.insert(5, "d".to_string()).expect("Failed to insert"); diff --git a/ledger/store/src/helpers/rocksdb/program.rs b/ledger/store/src/helpers/rocksdb/program.rs index d57a6fb286..f32b7c7b5b 100644 --- a/ledger/store/src/helpers/rocksdb/program.rs +++ b/ledger/store/src/helpers/rocksdb/program.rs @@ -50,7 +50,8 @@ impl FinalizeStorage for FinalizeDB { type KeyValueMap = NestedDataMap<(ProgramID, Identifier), Plaintext, Value>; /// Initializes the finalize storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { + let storage = storage.into(); // Initialize the committee store. let committee_store = CommitteeStore::>::open(storage.clone())?; // Return the finalize storage. @@ -58,21 +59,7 @@ impl FinalizeStorage for FinalizeDB { committee_store, program_id_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Program(ProgramMap::ProgramID))?, key_value_map: rocksdb::RocksDB::open_nested_map(N::ID, storage.clone(), MapID::Program(ProgramMap::KeyValueID))?, - storage_mode: storage.into(), - }) - } - - /// Initializes the test-variant of the storage. - #[cfg(any(test, feature = "test"))] - fn open_testing(temp_dir: std::path::PathBuf, dev: Option) -> Result { - // Initialize the committee store. - let committee_store = CommitteeStore::>::open_testing(temp_dir.clone(), dev)?; - // Return the finalize storage. - Ok(Self { - committee_store, - program_id_map: rocksdb::RocksDB::open_map_testing(temp_dir.clone(), dev, MapID::Program(ProgramMap::ProgramID))?, - key_value_map: rocksdb::RocksDB::open_nested_map_testing(temp_dir, dev, MapID::Program(ProgramMap::KeyValueID))?, - storage_mode: dev.into(), + storage_mode: storage, }) } @@ -117,23 +104,13 @@ impl CommitteeStorage for CommitteeDB { type CommitteeMap = DataMap>; /// Initializes the committee storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { + let storage = storage.into(); Ok(Self { current_round_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Committee(CommitteeMap::CurrentRound))?, round_to_height_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Committee(CommitteeMap::RoundToHeight))?, committee_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Committee(CommitteeMap::Committee))?, - storage_mode: storage.into(), - }) - } - - /// Initializes the test-variant of the storage. - #[cfg(any(test, feature = "test"))] - fn open_testing(temp_dir: std::path::PathBuf, dev: Option) -> Result { - Ok(Self { - current_round_map: rocksdb::RocksDB::open_map_testing(temp_dir.clone(), dev, MapID::Committee(CommitteeMap::CurrentRound))?, - round_to_height_map: rocksdb::RocksDB::open_map_testing(temp_dir.clone(), dev, MapID::Committee(CommitteeMap::RoundToHeight))?, - committee_map: rocksdb::RocksDB::open_map_testing(temp_dir, dev, MapID::Committee(CommitteeMap::Committee))?, - storage_mode: dev.into(), + storage_mode: storage, }) } diff --git a/ledger/store/src/helpers/rocksdb/transition.rs b/ledger/store/src/helpers/rocksdb/transition.rs index 0b6e801c71..d363709801 100644 --- a/ledger/store/src/helpers/rocksdb/transition.rs +++ b/ledger/store/src/helpers/rocksdb/transition.rs @@ -62,7 +62,8 @@ impl TransitionStorage for TransitionDB { type SCMMap = DataMap>; /// Initializes the transition storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { + let storage = storage.into(); Ok(Self { locator_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Transition(TransitionMap::Locator))?, input_store: InputStore::open(storage.clone())?, @@ -71,7 +72,7 @@ impl TransitionStorage for TransitionDB { reverse_tpk_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Transition(TransitionMap::ReverseTPK))?, tcm_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Transition(TransitionMap::TCM))?, reverse_tcm_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Transition(TransitionMap::ReverseTCM))?, - scm_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::Transition(TransitionMap::SCM))?, + scm_map: rocksdb::RocksDB::open_map(N::ID, storage, MapID::Transition(TransitionMap::SCM))?, }) } @@ -151,7 +152,8 @@ impl InputStorage for InputDB { type ExternalRecordMap = DataMap, ()>; /// Initializes the transition input storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { + let storage = storage.into(); Ok(Self { id_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::TransitionInput(TransitionInputMap::ID))?, reverse_id_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::TransitionInput(TransitionInputMap::ReverseID))?, @@ -161,7 +163,7 @@ impl InputStorage for InputDB { record: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::TransitionInput(TransitionInputMap::Record))?, record_tag: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::TransitionInput(TransitionInputMap::RecordTag))?, external_record: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::TransitionInput(TransitionInputMap::ExternalRecord))?, - storage_mode: storage.into(), + storage_mode: storage, }) } @@ -250,7 +252,8 @@ impl OutputStorage for OutputDB { type FutureMap = DataMap, Option>>; /// Initializes the transition output storage. - fn open>(storage: S) -> Result { + fn open>(storage: S) -> Result { + let storage = storage.into(); Ok(Self { id_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::TransitionOutput(TransitionOutputMap::ID))?, reverse_id_map: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::TransitionOutput(TransitionOutputMap::ReverseID))?, @@ -261,7 +264,7 @@ impl OutputStorage for OutputDB { record_nonce: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::TransitionOutput(TransitionOutputMap::RecordNonce))?, external_record: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::TransitionOutput(TransitionOutputMap::ExternalRecord))?, future: rocksdb::RocksDB::open_map(N::ID, storage.clone(), MapID::TransitionOutput(TransitionOutputMap::Future))?, - storage_mode: storage.into(), + storage_mode: storage, }) } diff --git a/ledger/store/src/program/committee.rs b/ledger/store/src/program/committee.rs index a3261e99d1..f9e95fb284 100644 --- a/ledger/store/src/program/committee.rs +++ b/ledger/store/src/program/committee.rs @@ -38,11 +38,7 @@ pub trait CommitteeStorage: 'static + Clone + Send + Sync { type CommitteeMap: for<'a> Map<'a, u32, Committee>; /// Initializes the committee storage. - fn open>(storage: S) -> Result; - - /// Initializes the test-variant of the storage. - #[cfg(any(test, feature = "test"))] - fn open_testing(temp_dir: std::path::PathBuf, dev: Option) -> Result; + fn open>(storage: S) -> Result; /// Returns the current round map. fn current_round_map(&self) -> &Self::CurrentRoundMap; @@ -301,18 +297,9 @@ pub struct CommitteeStore> { impl> CommitteeStore { /// Initializes the committee store. - pub fn open>(storage: S) -> Result { - // Initialize the committee storage. - let storage = C::open(storage.clone())?; - // Return the committee store. - Ok(Self { storage, _phantom: PhantomData }) - } - - /// Initializes the test-variant of the storage. - #[cfg(any(test, feature = "test"))] - pub fn open_testing(temp_dir: std::path::PathBuf, dev: Option) -> Result { + pub fn open>(storage: S) -> Result { // Initialize the committee storage. - let storage = C::open_testing(temp_dir, dev)?; + let storage = C::open(storage)?; // Return the committee store. Ok(Self { storage, _phantom: PhantomData }) } diff --git a/ledger/store/src/program/finalize.rs b/ledger/store/src/program/finalize.rs index f0662a6feb..4b465d0e24 100644 --- a/ledger/store/src/program/finalize.rs +++ b/ledger/store/src/program/finalize.rs @@ -80,11 +80,7 @@ pub trait FinalizeStorage: 'static + Clone + Send + Sync { type KeyValueMap: for<'a> NestedMap<'a, (ProgramID, Identifier), Plaintext, Value>; /// Initializes the program state storage. - fn open>(storage: S) -> Result; - - /// Initializes the test-variant of the storage. - #[cfg(any(test, feature = "test"))] - fn open_testing(temp_dir: std::path::PathBuf, dev: Option) -> Result; + fn open>(storage: S) -> Result; /// Returns the committee storage. fn committee_store(&self) -> &CommitteeStore; @@ -535,16 +531,10 @@ pub struct FinalizeStore> { impl> FinalizeStore { /// Initializes the finalize store. - pub fn open>(storage: S) -> Result { + pub fn open>(storage: S) -> Result { Self::from(P::open(storage)?) } - /// Initializes the test-variant of the storage. - #[cfg(any(test, feature = "test"))] - pub fn open_testing(temp_dir: std::path::PathBuf, dev: Option) -> Result { - Self::from(P::open_testing(temp_dir, dev)?) - } - /// Initializes a finalize store from storage. pub fn from(storage: P) -> Result { // Return the finalize store. @@ -1301,8 +1291,8 @@ mod tests { // Initialize a new finalize store. #[cfg(feature = "rocks")] let finalize_store = { - let temp_dir = tempfile::tempdir().expect("Failed to open temporary directory").into_path(); - let program_rocksdb = crate::helpers::rocksdb::FinalizeDB::open_testing(temp_dir, None).unwrap(); + let temp_dir = std::sync::Arc::new(tempfile::tempdir().expect("Failed to open temporary directory")); + let program_rocksdb = crate::helpers::rocksdb::FinalizeDB::open(temp_dir).unwrap(); FinalizeStore::from(program_rocksdb).unwrap() }; diff --git a/ledger/store/src/transition/input.rs b/ledger/store/src/transition/input.rs index 8889d344a4..25b9737823 100644 --- a/ledger/store/src/transition/input.rs +++ b/ledger/store/src/transition/input.rs @@ -48,7 +48,7 @@ pub trait InputStorage: Clone + Send + Sync { type ExternalRecordMap: for<'a> Map<'a, Field, ()>; /// Initializes the transition input storage. - fn open>(storage: S) -> Result; + fn open>(storage: S) -> Result; /// Returns the ID map. fn id_map(&self) -> &Self::IDMap; @@ -307,7 +307,7 @@ pub struct InputStore> { impl> InputStore { /// Initializes the transition input store. - pub fn open>(storage: S) -> Result { + pub fn open>(storage: S) -> Result { // Initialize a new transition input storage. let storage = I::open(storage)?; // Return the transition input store. diff --git a/ledger/store/src/transition/mod.rs b/ledger/store/src/transition/mod.rs index bb0eff8629..c3d4988600 100644 --- a/ledger/store/src/transition/mod.rs +++ b/ledger/store/src/transition/mod.rs @@ -56,7 +56,7 @@ pub trait TransitionStorage: Clone + Send + Sync { type SCMMap: for<'a> Map<'a, N::TransitionID, Field>; /// Initializes the transition storage. - fn open>(storage: S) -> Result; + fn open>(storage: S) -> Result; /// Returns the transition program IDs and function names. fn locator_map(&self) -> &Self::LocatorMap; @@ -292,7 +292,7 @@ pub struct TransitionStore> { impl> TransitionStore { /// Initializes the transition store. - pub fn open>(storage: S) -> Result { + pub fn open>(storage: S) -> Result { // Initialize the transition storage. let storage = T::open(storage)?; // Return the transition store. diff --git a/ledger/store/src/transition/output.rs b/ledger/store/src/transition/output.rs index 30d53bcc4b..c7e67a252d 100644 --- a/ledger/store/src/transition/output.rs +++ b/ledger/store/src/transition/output.rs @@ -50,7 +50,7 @@ pub trait OutputStorage: Clone + Send + Sync { type FutureMap: for<'a> Map<'a, Field, Option>>; /// Initializes the transition output storage. - fn open>(storage: S) -> Result; + fn open>(storage: S) -> Result; /// Returns the ID map. fn id_map(&self) -> &Self::IDMap; @@ -328,7 +328,7 @@ pub struct OutputStore> { impl> OutputStore { /// Initializes the transition output store. - pub fn open>(storage: S) -> Result { + pub fn open>(storage: S) -> Result { // Initialize a new transition output storage. let storage = O::open(storage)?; // Return the transition output store. diff --git a/parameters/Cargo.toml b/parameters/Cargo.toml index c4e6f93496..fdf404ad3d 100644 --- a/parameters/Cargo.toml +++ b/parameters/Cargo.toml @@ -27,6 +27,7 @@ edition = "2021" default = [ "colored", "remote" ] no_std_out = [ ] remote = [ "curl" ] +rocks = [ "snarkvm-ledger-store/rocks" ] wasm = [ "encoding", "js-sys", "web-sys" ] [dependencies.snarkvm-curves] @@ -39,7 +40,8 @@ path = "../utilities" version = "=1.1.0" [dependencies.aleo-std] -version = "0.1.24" +git = "https://github.com/ljedrz/aleo-std" +branch = "feat/test_storage_mode" default-features = false features = [ "storage" ] diff --git a/parameters/examples/inclusion.rs b/parameters/examples/inclusion.rs index dc241cbd95..6f3f7c8c96 100644 --- a/parameters/examples/inclusion.rs +++ b/parameters/examples/inclusion.rs @@ -22,7 +22,13 @@ use snarkvm_console::{ program::{Plaintext, Record, StatePath}, types::Field, }; -use snarkvm_ledger_store::{ConsensusStore, helpers::memory::ConsensusMemory}; +use snarkvm_ledger_store::ConsensusStore; + +#[cfg(not(feature = "rocks"))] +type LedgerType = snarkvm_ledger_store::helpers::memory::ConsensusMemory; +#[cfg(feature = "rocks")] +type LedgerType = snarkvm_ledger_store::helpers::rocksdb::ConsensusDB; + use snarkvm_synthesizer::{VM, process::InclusionAssignment, snark::UniversalSRS}; use anyhow::{Result, anyhow}; @@ -70,7 +76,7 @@ fn write_metadata(filename: &str, metadata: &Value) -> Result<()> { #[allow(clippy::type_complexity)] pub fn sample_assignment>() -> Result<(Assignment, StatePath, Field)> { // Initialize the consensus store. - let store = ConsensusStore::>::open(None)?; + let store = ConsensusStore::>::open(None)?; // Initialize a new VM. let vm = VM::from(store)?; diff --git a/synthesizer/Cargo.toml b/synthesizer/Cargo.toml index d100b6d540..a6c507bc2e 100644 --- a/synthesizer/Cargo.toml +++ b/synthesizer/Cargo.toml @@ -143,7 +143,8 @@ path = "../utilities" version = "=1.1.0" [dependencies.aleo-std] -version = "0.1.24" +git = "https://github.com/ljedrz/aleo-std" +branch = "feat/test_storage_mode" default-features = false [dependencies.anyhow] diff --git a/synthesizer/process/Cargo.toml b/synthesizer/process/Cargo.toml index 56c8de65b6..70d50b93c9 100644 --- a/synthesizer/process/Cargo.toml +++ b/synthesizer/process/Cargo.toml @@ -94,7 +94,8 @@ path = "../../utilities" version = "=1.1.0" [dependencies.aleo-std] -version = "0.1.24" +git = "https://github.com/ljedrz/aleo-std" +branch = "feat/test_storage_mode" default-features = false [dependencies.colored] diff --git a/synthesizer/process/src/tests/test_credits.rs b/synthesizer/process/src/tests/test_credits.rs index 9de634854f..2369a7cb9f 100644 --- a/synthesizer/process/src/tests/test_credits.rs +++ b/synthesizer/process/src/tests/test_credits.rs @@ -45,20 +45,14 @@ const TEST_COMMISSION: u8 = 5; macro_rules! sample_finalize_store { () => {{ #[cfg(feature = "rocks")] - let temp_dir = tempfile::tempdir().expect("Failed to open temporary directory"); - #[cfg(not(feature = "rocks"))] - let temp_dir = (); - - #[cfg(feature = "rocks")] - let store = FinalizeStore::>::open_testing( - temp_dir.path().to_owned(), - None, + let store = FinalizeStore::>::open( + std::sync::Arc::new(tempfile::tempdir().expect("Failed to open temporary directory")), ) .unwrap(); #[cfg(not(feature = "rocks"))] let store = FinalizeStore::>::open(None).unwrap(); - (store, temp_dir) + store }}; } @@ -464,7 +458,7 @@ fn test_bond_validator_simple() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators. let (validators, _) = initialize_stakers(&store, 1, 0, rng).unwrap(); @@ -519,7 +513,7 @@ fn test_bond_public_with_minimum_bond() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validator and delegator keys let validator_private_key = PrivateKey::::new(rng).unwrap(); @@ -613,7 +607,7 @@ fn test_bond_validator_below_min_stake_fails() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators. let (validators, _) = initialize_stakers(&store, 1, 0, rng).unwrap(); @@ -649,7 +643,7 @@ fn test_bond_validator_same_withdrawal_address_fails() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators. let (validators, _) = initialize_stakers(&store, 1, 0, rng).unwrap(); @@ -685,7 +679,7 @@ fn test_bond_validator_with_insufficient_funds_fails() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators. let (validators, _) = initialize_stakers(&store, 1, 0, rng).unwrap(); @@ -722,7 +716,7 @@ fn test_bond_validator_different_commission_fails() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators. let (validators, _) = initialize_stakers(&store, 1, 0, rng).unwrap(); @@ -790,7 +784,7 @@ fn test_bond_validator_multiple_bonds() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators. let (validators, _) = initialize_stakers(&store, 1, 0, rng).unwrap(); @@ -850,7 +844,7 @@ fn test_bond_validator_to_other_validator_fails() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators. let (validators, _) = initialize_stakers(&store, 2, 0, rng).unwrap(); @@ -936,7 +930,7 @@ fn test_bond_delegator_simple() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, delegators) = initialize_stakers(&store, 1, 1, rng).unwrap(); @@ -998,7 +992,7 @@ fn test_bond_delegator_below_min_stake_fails() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, delegators) = initialize_stakers(&store, 1, 1, rng).unwrap(); @@ -1065,7 +1059,7 @@ fn test_bond_delegator_with_insufficient_funds_fails() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, delegators) = initialize_stakers(&store, 1, 1, rng).unwrap(); @@ -1132,7 +1126,7 @@ fn test_bond_delegator_multiple_bonds() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, delegators) = initialize_stakers(&store, 1, 1, rng).unwrap(); @@ -1359,7 +1353,7 @@ fn test_bond_delegator_to_multiple_validators_fails() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, delegators) = initialize_stakers(&store, 2, 1, rng).unwrap(); @@ -1502,7 +1496,7 @@ fn test_unbond_validator() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, _) = initialize_stakers(&store, 1, 0, rng).unwrap(); @@ -1646,7 +1640,7 @@ fn test_bond_validator_fails_if_unbonding_state() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, _) = initialize_stakers(&store, 1, 0, rng).unwrap(); @@ -1733,7 +1727,7 @@ fn test_unbond_validator_fails_if_unbonding_beyond_their_stake() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, delegators) = initialize_stakers(&store, 1, 1, rng).unwrap(); @@ -1831,7 +1825,7 @@ fn test_unbond_validator_continues_if_there_is_a_delegator() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, delegators) = initialize_stakers(&store, 1, 1, rng).unwrap(); @@ -1912,7 +1906,7 @@ fn test_unbond_delegator() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, delegators) = initialize_stakers(&store, 1, 1, rng).unwrap(); @@ -2070,7 +2064,7 @@ fn test_unbond_delegator_without_validator() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, delegators) = initialize_stakers(&store, 1, 1, rng).unwrap(); @@ -2114,7 +2108,7 @@ fn test_unbond_delegator_removes_validator_with_insufficient_stake() { // Construct the process. let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, delegators) = initialize_stakers(&store, 1, 1, rng).unwrap(); @@ -2568,7 +2562,7 @@ fn test_claim_unbond_public_to_withdrawal_address() { let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, delegators) = initialize_stakers(&store, 1, 1, rng).unwrap(); @@ -2655,7 +2649,7 @@ fn test_bonding_multiple_stakers_to_same_withdrawal_address() { let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, delegators) = initialize_stakers(&store, 1, 1, rng).unwrap(); @@ -2686,7 +2680,7 @@ fn test_claim_unbond_public_removes_withdraw_mapping() { let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, _) = initialize_stakers(&store, 1, 0, rng).unwrap(); @@ -2744,7 +2738,7 @@ fn test_bond_validator_to_different_withdraw_address_fails() { let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, _) = initialize_stakers(&store, 1, 0, rng).unwrap(); @@ -2782,7 +2776,7 @@ fn test_bond_validator_with_different_commission_fails() { let process = Process::::load().unwrap(); // Initialize a new finalize store. - let (store, _temp_dir) = sample_finalize_store!(); + let store = sample_finalize_store!(); // Initialize the validators and delegators. let (validators, _) = initialize_stakers(&store, 1, 0, rng).unwrap(); diff --git a/synthesizer/src/vm/execute.rs b/synthesizer/src/vm/execute.rs index 97ab70d379..dc68e43ccd 100644 --- a/synthesizer/src/vm/execute.rs +++ b/synthesizer/src/vm/execute.rs @@ -220,18 +220,21 @@ mod tests { types::Field, }; use ledger_block::Transition; - use ledger_store::helpers::memory::ConsensusMemory; use synthesizer_process::{ConsensusFeeVersion, cost_per_command, execution_cost_v2}; use synthesizer_program::StackProgram; use indexmap::IndexMap; type CurrentNetwork = MainnetV0; + #[cfg(not(feature = "rocks"))] + type LedgerType = ledger_store::helpers::memory::ConsensusMemory; + #[cfg(feature = "rocks")] + type LedgerType = ledger_store::helpers::rocksdb::ConsensusDB; fn prepare_vm( rng: &mut TestRng, ) -> Result<( - VM>, + VM, IndexMap, Record>>, )> { // Initialize the genesis block. diff --git a/synthesizer/src/vm/finalize.rs b/synthesizer/src/vm/finalize.rs index 199a571060..c90319269e 100644 --- a/synthesizer/src/vm/finalize.rs +++ b/synthesizer/src/vm/finalize.rs @@ -1390,16 +1390,19 @@ mod tests { }; use ledger_block::{Block, Header, Metadata, Transaction, Transition}; use ledger_committee::{MAX_DELEGATORS, MIN_VALIDATOR_STAKE}; - use ledger_store::helpers::memory::ConsensusMemory; use synthesizer_program::Program; use rand::distributions::DistString; type CurrentNetwork = test_helpers::CurrentNetwork; + #[cfg(not(feature = "rocks"))] + type LedgerType = ledger_store::helpers::memory::ConsensusMemory; + #[cfg(feature = "rocks")] + type LedgerType = ledger_store::helpers::rocksdb::ConsensusDB; /// Sample a new program and deploy it to the VM. Returns the program name. fn new_program_deployment( - vm: &VM>, + vm: &VM, private_key: &PrivateKey, previous_block: &Block, unspent_records: &mut Vec>>, @@ -1467,7 +1470,7 @@ finalize transfer_public: /// Construct a new block based on the given transactions. fn sample_next_block( - vm: &VM>, + vm: &VM, private_key: &PrivateKey, transactions: &[Transaction], previous_block: &Block, @@ -1537,7 +1540,7 @@ finalize transfer_public: /// Generate split transactions for the unspent records. fn generate_splits( - vm: &VM>, + vm: &VM, private_key: &PrivateKey, previous_block: &Block, unspent_records: &mut Vec>>, @@ -1576,7 +1579,7 @@ finalize transfer_public: /// Create an execution transaction. fn create_execution( - vm: &VM>, + vm: &VM, caller_private_key: PrivateKey, program_id: &str, function_name: &str, @@ -1603,7 +1606,7 @@ finalize transfer_public: /// Sample a public mint transaction. fn sample_mint_public( - vm: &VM>, + vm: &VM, caller_private_key: PrivateKey, program_id: &str, recipient: Address, @@ -1621,7 +1624,7 @@ finalize transfer_public: /// Sample a public transfer transaction. fn sample_transfer_public( - vm: &VM>, + vm: &VM, caller_private_key: PrivateKey, program_id: &str, recipient: Address, @@ -2388,11 +2391,11 @@ finalize compute: let mut transactions = Vec::new(); let mut excess_transaction_ids = Vec::new(); - for _ in 0..VM::>::MAXIMUM_CONFIRMED_TRANSACTIONS + 1 { + for _ in 0..VM::::MAXIMUM_CONFIRMED_TRANSACTIONS + 1 { let transaction = sample_mint_public(&vm, caller_private_key, &program_id, caller_address, 10, &mut unspent_records, rng); // Abort the transaction if the block is full. - if transactions.len() >= VM::>::MAXIMUM_CONFIRMED_TRANSACTIONS { + if transactions.len() >= VM::::MAXIMUM_CONFIRMED_TRANSACTIONS { excess_transaction_ids.push(transaction.id()); } @@ -2406,10 +2409,7 @@ finalize compute: // Ensure that the excess transactions were aborted. assert_eq!(next_block.aborted_transaction_ids(), &excess_transaction_ids); - assert_eq!( - next_block.transactions().len(), - VM::>::MAXIMUM_CONFIRMED_TRANSACTIONS - ); + assert_eq!(next_block.transactions().len(), VM::::MAXIMUM_CONFIRMED_TRANSACTIONS); } #[test] diff --git a/synthesizer/src/vm/helpers/history.rs b/synthesizer/src/vm/helpers/history.rs index 568d93f624..710c69bb47 100644 --- a/synthesizer/src/vm/helpers/history.rs +++ b/synthesizer/src/vm/helpers/history.rs @@ -32,10 +32,11 @@ pub fn history_directory_path(network: u16, storage_mode: &StorageMode) -> PathB let directory_name = match &storage_mode { StorageMode::Development(id) => format!(".{HISTORY_DIRECTORY_NAME}-{network}-{id}"), StorageMode::Production | StorageMode::Custom(_) => format!("{HISTORY_DIRECTORY_NAME}-{network}"), + StorageMode::Test(_) => unimplemented!(), }; // Obtain the path to the ledger. - let mut path = aleo_ledger_dir(network, storage_mode.clone()); + let mut path = aleo_ledger_dir(network, &storage_mode); // Go to the folder right above the ledger. path.pop(); // Append the history directory's name. diff --git a/synthesizer/src/vm/mod.rs b/synthesizer/src/vm/mod.rs index 72b7166b70..0ea6e0595b 100644 --- a/synthesizer/src/vm/mod.rs +++ b/synthesizer/src/vm/mod.rs @@ -471,36 +471,29 @@ pub(crate) mod test_helpers { types::Field, }; use ledger_block::{Block, Header, Metadata, Transition}; - use ledger_store::helpers::memory::ConsensusMemory; - #[cfg(feature = "rocks")] - use ledger_store::helpers::rocksdb::ConsensusDB; use ledger_test_helpers::{large_transaction_program, small_transaction_program}; use synthesizer_program::Program; use indexmap::IndexMap; use once_cell::sync::OnceCell; - #[cfg(feature = "rocks")] - use std::path::Path; use synthesizer_snark::VerifyingKey; pub(crate) type CurrentNetwork = MainnetV0; + #[cfg(not(feature = "rocks"))] + type LedgerType = ledger_store::helpers::memory::ConsensusMemory; + #[cfg(feature = "rocks")] + type LedgerType = ledger_store::helpers::rocksdb::ConsensusDB; /// Samples a new finalize state. pub(crate) fn sample_finalize_state(block_height: u32) -> FinalizeGlobalState { FinalizeGlobalState::from(block_height as u64, block_height, [0u8; 32]) } - pub(crate) fn sample_vm() -> VM> { + pub(crate) fn sample_vm() -> VM { // Initialize a new VM. VM::from(ConsensusStore::open(None).unwrap()).unwrap() } - #[cfg(feature = "rocks")] - pub(crate) fn sample_vm_rocks(path: &Path) -> VM> { - // Initialize a new VM. - VM::from(ConsensusStore::open(path.to_owned()).unwrap()).unwrap() - } - pub(crate) fn sample_genesis_private_key(rng: &mut TestRng) -> PrivateKey { static INSTANCE: OnceCell> = OnceCell::new(); *INSTANCE.get_or_init(|| { @@ -523,9 +516,7 @@ pub(crate) mod test_helpers { .clone() } - pub(crate) fn sample_vm_with_genesis_block( - rng: &mut TestRng, - ) -> VM> { + pub(crate) fn sample_vm_with_genesis_block(rng: &mut TestRng) -> VM { // Initialize the VM. let vm = crate::vm::test_helpers::sample_vm(); // Initialize the genesis block. @@ -784,7 +775,7 @@ function compute: } pub fn sample_next_block( - vm: &VM>, + vm: &VM, private_key: &PrivateKey, transactions: &[Transaction], rng: &mut R, @@ -2563,8 +2554,7 @@ finalize transfer_public_to_private: let block2 = sample_next_block(&vm, &genesis_private_key, &[], rng).unwrap(); // Create a new, rocks-based VM shadowing the 1st one. - let tempdir = tempfile::tempdir().unwrap(); - let vm = sample_vm_rocks(tempdir.path()); + let vm = sample_vm(); vm.add_next_block(&genesis).unwrap(); // This time, however, try to insert the 2nd block first, which fails due to height. assert!(vm.add_next_block(&block2).is_err()); diff --git a/synthesizer/tests/test_vm_execute_and_finalize.rs b/synthesizer/tests/test_vm_execute_and_finalize.rs index c15e42c4db..7d597e9385 100644 --- a/synthesizer/tests/test_vm_execute_and_finalize.rs +++ b/synthesizer/tests/test_vm_execute_and_finalize.rs @@ -31,7 +31,7 @@ use ledger_block::{ Transactions, Transition, }; -use ledger_store::{ConsensusStorage, ConsensusStore, helpers::memory::ConsensusMemory}; +use ledger_store::{ConsensusStorage, ConsensusStore}; use snarkvm_synthesizer::{VM, program::FinalizeOperation}; use synthesizer_program::FinalizeGlobalState; @@ -41,6 +41,11 @@ use indexmap::IndexMap; use rayon::prelude::*; use utilities::*; +#[cfg(not(feature = "rocks"))] +type LedgerType = ledger_store::helpers::memory::ConsensusMemory; +#[cfg(feature = "rocks")] +type LedgerType = ledger_store::helpers::rocksdb::ConsensusDB; + #[test] fn test_vm_execute_and_finalize() { // Load the tests. @@ -365,10 +370,9 @@ fn run_test(test: &ProgramTest) -> serde_yaml::Mapping { fn initialize_vm( private_key: &PrivateKey, rng: &mut R, -) -> (VM>, Vec>>) { +) -> (VM>, Vec>>) { // Initialize a VM. - let vm: VM> = - VM::from(ConsensusStore::open(None).unwrap()).unwrap(); + let vm: VM> = VM::from(ConsensusStore::open(None).unwrap()).unwrap(); // Initialize the genesis block. let genesis = vm.genesis_beacon(private_key, rng).unwrap(); diff --git a/utilities/Cargo.toml b/utilities/Cargo.toml index 8d6ca632a4..aca0751d9c 100644 --- a/utilities/Cargo.toml +++ b/utilities/Cargo.toml @@ -29,7 +29,8 @@ version = "=1.1.0" optional = true [dependencies.aleo-std] -version = "0.1.24" +git = "https://github.com/ljedrz/aleo-std" +branch = "feat/test_storage_mode" optional = true default-features = false