diff --git a/Cargo.lock b/Cargo.lock index 2b724e9d99..246a667d79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3075,12 +3075,14 @@ dependencies = [ "anyhow", "bytes", "indexmap 2.2.5", + "once_cell", "proptest", "rayon", "serde", "snarkos-node-metrics", "snarkos-node-sync-locators", "snarkvm", + "tempfile", "test-strategy", "time", "tokio-util", @@ -3233,12 +3235,14 @@ dependencies = [ "anyhow", "bytes", "indexmap 2.2.5", + "once_cell", "proptest", "rayon", "serde", "snarkos-node-bft-events", "snarkos-node-sync-locators", "snarkvm", + "tempfile", "test-strategy", "tokio", "tokio-util", diff --git a/node/bft/events/Cargo.toml b/node/bft/events/Cargo.toml index 1dc1de94cb..54cc5798bc 100644 --- a/node/bft/events/Cargo.toml +++ b/node/bft/events/Cargo.toml @@ -1,23 +1,23 @@ [package] name = "snarkos-node-bft-events" version = "2.2.7" -authors = [ "The Aleo Team " ] +authors = ["The Aleo Team "] 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] @@ -28,7 +28,7 @@ version = "1" [dependencies.indexmap] version = "2.1" -features = [ "serde", "rayon" ] +features = ["serde", "rayon"] [dependencies.metrics] package = "snarkos-node-metrics" @@ -51,7 +51,7 @@ workspace = true [dependencies.tokio-util] version = "0.7" -features = [ "codec" ] +features = ["codec"] [dependencies.tracing] version = "0.1" @@ -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" @@ -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" diff --git a/node/bft/events/src/block_response.rs b/node/bft/events/src/block_response.rs index 1ca7ab86ef..c838abe1b2 100644 --- a/node/bft/events/src/block_response.rs +++ b/node/bft/events/src/block_response.rs @@ -136,22 +136,52 @@ impl FromBytes for DataBlocks { #[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 { + // TODO refactor me to a single location in codebase + static INSTANCE: OnceCell> = OnceCell::new(); + INSTANCE + .get_or_init(|| { + // Sample the genesis private key. + let private_key = PrivateKey::::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> { - any::().prop_map(|seed| sample_genesis_block(&mut TestRng::fixed(seed))).boxed() + any::().prop_map(|seed| sample_genesis_block(&mut TestRng::from_seed(seed))).boxed() } pub fn any_data_blocks() -> BoxedStrategy> { diff --git a/node/router/messages/Cargo.toml b/node/router/messages/Cargo.toml index 22999d4294..db283390f2 100644 --- a/node/router/messages/Cargo.toml +++ b/node/router/messages/Cargo.toml @@ -1,24 +1,24 @@ [package] name = "snarkos-node-router-messages" version = "2.2.7" -authors = [ "The Aleo Team " ] +authors = ["The Aleo Team "] 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" @@ -28,7 +28,7 @@ version = "1" [dependencies.indexmap] version = "2.1" -features = [ "serde", "rayon" ] +features = ["serde", "rayon"] [dependencies.rayon] version = "1" @@ -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" \ No newline at end of file diff --git a/node/router/messages/src/block_response.rs b/node/router/messages/src/block_response.rs index 75a4135dd6..52dfd14dd8 100644 --- a/node/router/messages/src/block_response.rs +++ b/node/router/messages/src/block_response.rs @@ -61,23 +61,55 @@ impl FromBytes for BlockResponse { #[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 { + // TODO refactor me to a single location in codebase + static INSTANCE: OnceCell> = OnceCell::new(); + INSTANCE + .get_or_init(|| { + // Sample the genesis private key. + let private_key = PrivateKey::::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> { - any::().prop_map(|seed| sample_genesis_block(&mut TestRng::fixed(seed))).boxed() + any::().prop_map(|seed| sample_genesis_block(&mut TestRng::from_seed(seed))).boxed() } pub fn any_data_blocks() -> BoxedStrategy> { diff --git a/node/router/messages/src/challenge_response.rs b/node/router/messages/src/challenge_response.rs index 190001837e..73339a734b 100644 --- a/node/router/messages/src/challenge_response.rs +++ b/node/router/messages/src/challenge_response.rs @@ -59,11 +59,12 @@ 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; @@ -71,9 +72,9 @@ pub mod prop_tests { type CurrentNetwork = snarkvm::prelude::MainnetV0; pub fn any_signature() -> BoxedStrategy> { - (0..64) - .prop_map(|message_size| { - let rng = &mut TestRng::default(); + (any::(), 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() @@ -82,7 +83,7 @@ pub mod prop_tests { } pub fn any_genesis_header() -> BoxedStrategy> { - any::().prop_map(|seed| *sample_genesis_block(&mut TestRng::fixed(seed)).header()).boxed() + any::().prop_map(|seed| *prop_tests::sample_genesis_block(&mut TestRng::from_seed(seed)).header()).boxed() } pub fn any_challenge_response() -> BoxedStrategy> { diff --git a/node/router/messages/src/unconfirmed_transaction.rs b/node/router/messages/src/unconfirmed_transaction.rs index ae24a9ff21..711204ba61 100644 --- a/node/router/messages/src/unconfirmed_transaction.rs +++ b/node/router/messages/src/unconfirmed_transaction.rs @@ -60,21 +60,27 @@ impl FromBytes for UnconfirmedTransaction { 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> { - any::() - .prop_map(|seed| { - let mut rng = TestRng::fixed(seed); - sample_fee_public_transaction(&mut rng) + (any_block(), any::()) + .prop_map(|(block, selector)| { + let tx: Transaction = + selector.select(block.transactions().clone().into_iter().map(|tx| tx.into_transaction())); + tx }) .boxed() }