Skip to content

Commit

Permalink
test: use VM::genesis_beacon instead of test-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
vvp committed Mar 21, 2024
1 parent 4d926b4 commit f818a3e
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 51 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 18 additions & 12 deletions node/bft/events/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
[package]
name = "snarkos-node-bft-events"
version = "2.2.7"
authors = [ "The Aleo Team <[email protected]>" ]
authors = ["The Aleo Team <[email protected]>"]
description = "Events for the gateway in a decentralized operating system"
homepage = "https://aleo.org"
repository = "https://github.com/AleoHQ/snarkOS"
keywords = [
"aleo",
"cryptography",
"blockchain",
"decentralized",
"zero-knowledge"
"aleo",
"cryptography",
"blockchain",
"decentralized",
"zero-knowledge"
]
categories = [ "cryptography", "operating-systems" ]
categories = ["cryptography", "operating-systems"]
license = "Apache-2.0"
edition = "2021"

[features]
default = [ ]
default = []
metrics = ["dep:metrics", "snarkvm/metrics"]

[dependencies.anyhow]
Expand All @@ -28,7 +28,7 @@ version = "1"

[dependencies.indexmap]
version = "2.1"
features = [ "serde", "rayon" ]
features = ["serde", "rayon"]

[dependencies.metrics]
package = "snarkos-node-metrics"
Expand All @@ -51,7 +51,7 @@ workspace = true

[dependencies.tokio-util]
version = "0.7"
features = [ "codec" ]
features = ["codec"]

[dependencies.tracing]
version = "0.1"
Expand All @@ -61,7 +61,7 @@ version = "1.4.0"

[dev-dependencies.snarkvm]
workspace = true
features = [ "test-helpers" ]
features = ["test-helpers"]

[dev-dependencies.test-strategy]
version = "0.3.1"
Expand All @@ -71,4 +71,10 @@ version = "0.3"

[dev-dependencies.snarkos-node-sync-locators]
path = "../../sync/locators"
features = [ "test" ]
features = ["test"]

[dev-dependencies.tempfile]
version = "3.10.1"

[dev-dependencies.once_cell]
version = "1.18"
40 changes: 35 additions & 5 deletions node/bft/events/src/block_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,52 @@ impl<N: Network> FromBytes for DataBlocks<N> {
#[cfg(test)]
pub mod prop_tests {
use crate::{block_request::prop_tests::any_block_request, BlockResponse, DataBlocks};
use snarkvm::{
ledger::ledger_test_helpers::sample_genesis_block,
prelude::{block::Block, narwhal::Data, FromBytes, TestRng, ToBytes},
};
use anyhow::Context;
use snarkvm::prelude::{block::Block, narwhal::Data, FromBytes, TestRng, ToBytes};
use std::{env, fs::DirBuilder};

use bytes::{Buf, BufMut, BytesMut};
use once_cell::sync::OnceCell;
use proptest::{
collection::vec,
prelude::{any, BoxedStrategy, Strategy},
};
use snarkvm::{
ledger::store::{helpers::memory::ConsensusMemory, ConsensusStore},
prelude::{PrivateKey, VM},
};
use tempfile::tempdir_in;
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::MainnetV0;

fn sample_genesis_block(rng: &mut TestRng) -> Block<CurrentNetwork> {
// TODO refactor me to a single location in codebase
static INSTANCE: OnceCell<Block<CurrentNetwork>> = OnceCell::new();
INSTANCE
.get_or_init(|| {
// Sample the genesis private key.
let private_key = PrivateKey::<CurrentNetwork>::new(rng).unwrap();

// Initialize the store in temp dir inside aleo-test specific tmp dir.
let aleo_tmp_dir = env::temp_dir().join("aleo_tmp_SAFE_TO_DELETE/");
if aleo_tmp_dir.exists() {
std::fs::remove_dir_all(aleo_tmp_dir.clone())
.with_context(|| format!("Cannot remove {aleo_tmp_dir:?}"))
.unwrap();
};
DirBuilder::new().recursive(true).create(aleo_tmp_dir.clone()).unwrap();
let temp_dir = tempdir_in(aleo_tmp_dir).unwrap();
let store = ConsensusStore::<_, ConsensusMemory<_>>::open(temp_dir.into_path()).unwrap();

// Create a genesis block.
VM::from(store).unwrap().genesis_beacon(&private_key, rng).unwrap()
})
.clone()
}

pub fn any_block() -> BoxedStrategy<Block<CurrentNetwork>> {
any::<u64>().prop_map(|seed| sample_genesis_block(&mut TestRng::fixed(seed))).boxed()
any::<u64>().prop_map(|seed| sample_genesis_block(&mut TestRng::from_seed(seed))).boxed()
}

pub fn any_data_blocks() -> BoxedStrategy<DataBlocks<CurrentNetwork>> {
Expand Down
46 changes: 26 additions & 20 deletions node/router/messages/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
[package]
name = "snarkos-node-router-messages"
version = "2.2.7"
authors = [ "The Aleo Team <[email protected]>" ]
authors = ["The Aleo Team <[email protected]>"]
description = "Node messages for a decentralized operating system"
homepage = "https://aleo.org"
repository = "https://github.com/AleoHQ/snarkOS"
keywords = [
"aleo",
"cryptography",
"blockchain",
"decentralized",
"zero-knowledge"
"aleo",
"cryptography",
"blockchain",
"decentralized",
"zero-knowledge"
]
categories = [ "cryptography", "operating-systems" ]
categories = ["cryptography", "operating-systems"]
license = "Apache-2.0"
edition = "2021"

[features]
default = [ ]
test = [ ]
default = []
test = []

[dependencies.anyhow]
version = "1.0"
Expand All @@ -28,7 +28,7 @@ version = "1"

[dependencies.indexmap]
version = "2.1"
features = [ "serde", "rayon" ]
features = ["serde", "rayon"]

[dependencies.rayon]
version = "1"
Expand All @@ -50,32 +50,38 @@ workspace = true
[dependencies.tokio]
version = "1.28"
features = [
"io-util",
"macros",
"net",
"rt-multi-thread",
"signal",
"sync",
"time"
"io-util",
"macros",
"net",
"rt-multi-thread",
"signal",
"sync",
"time"
]

[dependencies.tokio-util]
version = "0.7"
features = [ "codec" ]
features = ["codec"]

[dependencies.tracing]
version = "0.1"

[dev-dependencies.snarkos-node-sync-locators]
path = "../../sync/locators"
features = [ "test" ]
features = ["test"]

[dev-dependencies.snarkvm]
workspace = true
features = [ "algorithms", "test-helpers" ]
features = ["algorithms", "test-helpers"]

[dev-dependencies.proptest]
version = "1.4.0"

[dev-dependencies.test-strategy]
version = "0.3.1"

[dev-dependencies.tempfile]
version = "3.10.1"

[dev-dependencies.once_cell]
version = "1.18"
36 changes: 34 additions & 2 deletions node/router/messages/src/block_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,55 @@ impl<N: Network> FromBytes for BlockResponse<N> {
#[cfg(test)]
pub mod prop_tests {
use crate::{block_request::prop_tests::any_block_request, BlockResponse, DataBlocks};
use anyhow::Context;
use snarkvm::{
ledger::ledger_test_helpers::sample_genesis_block,
prelude::{block::Block, narwhal::Data},
utilities::{FromBytes, TestRng, ToBytes},
};
use std::{env, fs::DirBuilder};

use bytes::{Buf, BufMut, BytesMut};
use once_cell::sync::OnceCell;
use proptest::{
collection::vec,
prelude::{any, BoxedStrategy, Strategy},
};
use snarkvm::{
ledger::store::{helpers::memory::ConsensusMemory, ConsensusStore},
prelude::{PrivateKey, VM},
};
use tempfile::tempdir_in;
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::MainnetV0;

pub fn sample_genesis_block(rng: &mut TestRng) -> Block<CurrentNetwork> {
// TODO refactor me to a single location in codebase
static INSTANCE: OnceCell<Block<CurrentNetwork>> = OnceCell::new();
INSTANCE
.get_or_init(|| {
// Sample the genesis private key.
let private_key = PrivateKey::<CurrentNetwork>::new(rng).unwrap();

// Initialize the store in temp dir inside aleo-test specific tmp dir.
let aleo_tmp_dir = env::temp_dir().join("aleo_tmp_SAFE_TO_DELETE/");
if aleo_tmp_dir.exists() {
std::fs::remove_dir_all(aleo_tmp_dir.clone())
.with_context(|| format!("Cannot remove {aleo_tmp_dir:?}"))
.unwrap();
};
DirBuilder::new().recursive(true).create(aleo_tmp_dir.clone()).unwrap();
let temp_dir = tempdir_in(aleo_tmp_dir).unwrap();
let store = ConsensusStore::<_, ConsensusMemory<_>>::open(temp_dir.into_path()).unwrap();

// Create a genesis block.
VM::from(store).unwrap().genesis_beacon(&private_key, rng).unwrap()
})
.clone()
}

pub fn any_block() -> BoxedStrategy<Block<CurrentNetwork>> {
any::<u64>().prop_map(|seed| sample_genesis_block(&mut TestRng::fixed(seed))).boxed()
any::<u64>().prop_map(|seed| sample_genesis_block(&mut TestRng::from_seed(seed))).boxed()
}

pub fn any_data_blocks() -> BoxedStrategy<DataBlocks<CurrentNetwork>> {
Expand Down
11 changes: 6 additions & 5 deletions node/router/messages/src/challenge_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,22 @@ pub mod prop_tests {
use crate::ChallengeResponse;
use snarkvm::{
console::prelude::{FromBytes, ToBytes},
ledger::{ledger_test_helpers::sample_genesis_block, narwhal::Data},
ledger::narwhal::Data,
prelude::{block::Header, PrivateKey, Signature},
utilities::rand::{TestRng, Uniform},
};

use crate::block_response::prop_tests;
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{any, BoxedStrategy, Strategy};
use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::MainnetV0;

pub fn any_signature() -> BoxedStrategy<Signature<CurrentNetwork>> {
(0..64)
.prop_map(|message_size| {
let rng = &mut TestRng::default();
(any::<u64>(), 0..64)
.prop_map(|(seed, message_size)| {
let rng = &mut TestRng::from_seed(seed);
let message: Vec<_> = (0..message_size).map(|_| Uniform::rand(rng)).collect();
let private_key = PrivateKey::new(rng).unwrap();
Signature::sign(&private_key, &message, rng).unwrap()
Expand All @@ -82,7 +83,7 @@ pub mod prop_tests {
}

pub fn any_genesis_header() -> BoxedStrategy<Header<CurrentNetwork>> {
any::<u64>().prop_map(|seed| *sample_genesis_block(&mut TestRng::fixed(seed)).header()).boxed()
any::<u64>().prop_map(|seed| *prop_tests::sample_genesis_block(&mut TestRng::from_seed(seed)).header()).boxed()
}

pub fn any_challenge_response() -> BoxedStrategy<ChallengeResponse<CurrentNetwork>> {
Expand Down
20 changes: 13 additions & 7 deletions node/router/messages/src/unconfirmed_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,27 @@ impl<N: Network> FromBytes for UnconfirmedTransaction<N> {
pub mod prop_tests {
use crate::{Transaction, UnconfirmedTransaction};
use snarkvm::{
ledger::{ledger_test_helpers::sample_fee_public_transaction, narwhal::Data},
prelude::{FromBytes, TestRng, ToBytes},
ledger::{narwhal::Data},
prelude::{FromBytes, ToBytes},
};

use crate::block_response::prop_tests::any_block;
use bytes::{Buf, BufMut, BytesMut};
use proptest::prelude::{any, BoxedStrategy, Strategy};
use proptest::{
prelude::{any, BoxedStrategy, Strategy},
sample::Selector,
};

use test_strategy::proptest;

type CurrentNetwork = snarkvm::prelude::MainnetV0;

pub fn any_transaction() -> BoxedStrategy<Transaction<CurrentNetwork>> {
any::<u64>()
.prop_map(|seed| {
let mut rng = TestRng::fixed(seed);
sample_fee_public_transaction(&mut rng)
(any_block(), any::<Selector>())
.prop_map(|(block, selector)| {
let tx: Transaction<CurrentNetwork> =
selector.select(block.transactions().clone().into_iter().map(|tx| tx.into_transaction()));
tx
})
.boxed()
}
Expand Down

0 comments on commit f818a3e

Please sign in to comment.