Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Refactor] simplify and unify the storage setup for tests #2590

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ parameters:
default: xlarge
twoxlarge:
type: string
default: aleonet/2xlarge
default: 2xlarge

orbs:
windows: circleci/[email protected]
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion algorithms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion console/collections/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion fields/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 7 additions & 2 deletions ledger/benches/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<N> = ledger_store::helpers::memory::ConsensusMemory<N>;
#[cfg(feature = "rocks")]
type LedgerType<N> = ledger_store::helpers::rocksdb::ConsensusDB<N>;

fn initialize_vm<R: Rng + CryptoRng>(
private_key: &PrivateKey<MainnetV0>,
rng: &mut R,
) -> (VM<MainnetV0, ConsensusMemory<MainnetV0>>, Vec<Record<MainnetV0, Plaintext<MainnetV0>>>) {
) -> (VM<MainnetV0, LedgerType<MainnetV0>>, Vec<Record<MainnetV0, Plaintext<MainnetV0>>>) {
// Initialize the VM.
let vm = VM::from(ConsensusStore::open(None).unwrap()).unwrap();

Expand Down
3 changes: 2 additions & 1 deletion ledger/puzzle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion ledger/puzzle/epoch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 0 additions & 22 deletions ledger/src/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,25 +292,3 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
.collect::<Result<_>>()
}
}

#[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);
}
}
5 changes: 0 additions & 5 deletions ledger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -430,10 +429,6 @@ pub(crate) mod test_helpers {
TestEnv { ledger, private_key, view_key, address }
}

pub(crate) fn sample_genesis_block() -> Block<CurrentNetwork> {
Block::<CurrentNetwork>::from_bytes_le(CurrentNetwork::genesis_bytes()).unwrap()
}

pub(crate) fn sample_ledger(
private_key: PrivateKey<CurrentNetwork>,
rng: &mut (impl Rng + CryptoRng),
Expand Down
55 changes: 41 additions & 14 deletions ledger/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -39,9 +39,14 @@ use rand::seq::SliceRandom;
use std::collections::{BTreeMap, HashMap};
use time::OffsetDateTime;

#[cfg(not(feature = "rocks"))]
type LedgerType<N> = ledger_store::helpers::memory::ConsensusMemory<N>;
#[cfg(feature = "rocks")]
type LedgerType<N> = ledger_store::helpers::rocksdb::ConsensusDB<N>;

/// Initializes a sample VM.
fn sample_vm() -> VM<CurrentNetwork, ConsensusMemory<CurrentNetwork>> {
VM::from(ConsensusStore::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::open(None).unwrap()).unwrap()
fn sample_vm() -> VM<CurrentNetwork, LedgerType<CurrentNetwork>> {
VM::from(ConsensusStore::<CurrentNetwork, LedgerType<CurrentNetwork>>::open(None).unwrap()).unwrap()
}

/// Extract the transmissions from a block.
Expand Down Expand Up @@ -71,8 +76,7 @@ fn construct_quorum_blocks(
) -> Vec<Block<CurrentNetwork>> {
// Initialize the ledger with the genesis block.
let ledger =
Ledger::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::load(genesis.clone(), StorageMode::Production)
.unwrap();
Ledger::<CurrentNetwork, LedgerType<CurrentNetwork>>::load(genesis.clone(), StorageMode::Production).unwrap();

// Initialize the round parameters.
assert!(num_blocks > 0);
Expand Down Expand Up @@ -121,7 +125,7 @@ fn construct_quorum_blocks(

// Helper function to create a quorum block.
fn create_next_quorum_block(
ledger: &Ledger<CurrentNetwork, ConsensusMemory<CurrentNetwork>>,
ledger: &Ledger<CurrentNetwork, LedgerType<CurrentNetwork>>,
round: u64,
leader_certificate: &BatchCertificate<CurrentNetwork>,
previous_leader_certificate: Option<&BatchCertificate<CurrentNetwork>>,
Expand Down Expand Up @@ -182,7 +186,7 @@ fn test_load() {
// Sample the genesis private key.
let private_key = PrivateKey::<CurrentNetwork>::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();

Expand All @@ -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::<CurrentNetwork>::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();
Expand All @@ -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::<CurrentNetwork>::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();
Expand Down Expand Up @@ -1990,8 +2019,7 @@ fn test_max_committee_limit_with_bonds() {

// Initialize a Ledger from the genesis block.
let ledger =
Ledger::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::load(genesis_block, StorageMode::Production)
.unwrap();
Ledger::<CurrentNetwork, LedgerType<CurrentNetwork>>::load(genesis_block, StorageMode::Production).unwrap();

// Bond the first validator.
let bond_first_transaction = ledger
Expand Down Expand Up @@ -3050,7 +3078,7 @@ fn test_forged_block_subdags() {
// Sample the genesis private key.
let private_key = PrivateKey::<CurrentNetwork>::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);
Expand All @@ -3074,8 +3102,7 @@ fn test_forged_block_subdags() {
let block_3 = quorum_blocks.remove(0);

// Construct the ledger.
let ledger =
Ledger::<CurrentNetwork, ConsensusMemory<CurrentNetwork>>::load(genesis, StorageMode::Production).unwrap();
let ledger = Ledger::<CurrentNetwork, LedgerType<CurrentNetwork>>::load(genesis, StorageMode::Production).unwrap();
ledger.advance_to_next_block(&block_1).unwrap();
ledger.check_next_block(&block_2, rng).unwrap();

Expand Down
8 changes: 3 additions & 5 deletions ledger/store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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"
4 changes: 2 additions & 2 deletions ledger/store/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub trait BlockStorage<N: Network>: 'static + Clone + Send + Sync {
type TransitionStorage: TransitionStorage<N>;

/// Initializes the block storage.
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self>;
fn open<S: Into<StorageMode>>(storage: S) -> Result<Self>;

/// Returns the state root map.
fn state_root_map(&self) -> &Self::StateRootMap;
Expand Down Expand Up @@ -1003,7 +1003,7 @@ pub struct BlockStore<N: Network, B: BlockStorage<N>> {

impl<N: Network, B: BlockStorage<N>> BlockStore<N, B> {
/// Initializes the block store.
pub fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
pub fn open<S: Into<StorageMode>>(storage: S) -> Result<Self> {
// Initialize the block storage.
let storage = B::open(storage)?;

Expand Down
6 changes: 3 additions & 3 deletions ledger/store/src/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub trait ConsensusStorage<N: Network>: 'static + Clone + Send + Sync {
type TransitionStorage: TransitionStorage<N>;

/// Initializes the consensus storage.
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self>;
fn open<S: Into<StorageMode>>(storage: S) -> Result<Self>;

/// Returns the finalize storage.
fn finalize_store(&self) -> &FinalizeStore<N, Self::FinalizeStorage>;
Expand Down Expand Up @@ -115,9 +115,9 @@ pub struct ConsensusStore<N: Network, C: ConsensusStorage<N>> {

impl<N: Network, C: ConsensusStorage<N>> ConsensusStore<N, C> {
/// Initializes the consensus store.
pub fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
pub fn open<S: Into<StorageMode>>(storage: S) -> Result<Self> {
// 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 })
}
Expand Down
2 changes: 1 addition & 1 deletion ledger/store/src/helpers/memory/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<N: Network> BlockStorage<N> for BlockMemory<N> {
type TransitionStorage = TransitionMemory<N>;

/// Initializes the block storage.
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
fn open<S: Into<StorageMode>>(storage: S) -> Result<Self> {
// Initialize the transition store.
let transition_store = TransitionStore::<N, TransitionMemory<N>>::open(storage)?;
// Initialize the transaction store.
Expand Down
3 changes: 2 additions & 1 deletion ledger/store/src/helpers/memory/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ impl<N: Network> ConsensusStorage<N> for ConsensusMemory<N> {
type TransitionStorage = TransitionMemory<N>;

/// Initializes the consensus storage.
fn open<S: Clone + Into<StorageMode>>(storage: S) -> Result<Self> {
fn open<S: Into<StorageMode>>(storage: S) -> Result<Self> {
let storage = storage.into();
// Initialize the finalize store.
let finalize_store = FinalizeStore::<N, FinalizeMemory<N>>::open(storage.clone())?;
// Initialize the block store.
Expand Down
Loading