diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6a8eb714ebc..9333a56d391 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -302,4 +302,4 @@ jobs: - name: setup_tests shell: bash run: | - cargo install cargo-script && cargo script tools/setup_test.rs && git diff --no-ext-diff --quiet + cargo install rust-script && rust-script tools/setup_test.rs && git diff --no-ext-diff --quiet diff --git a/Cargo.lock b/Cargo.lock index 1aa5d2c2b56..722c14159a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5073,9 +5073,9 @@ checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" [[package]] name = "shlex" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "signal-hook-registry" diff --git a/massa-bootstrap/Cargo.toml b/massa-bootstrap/Cargo.toml index c4bf20c137d..a3aaeb7974c 100644 --- a/massa-bootstrap/Cargo.toml +++ b/massa-bootstrap/Cargo.toml @@ -55,4 +55,4 @@ massa_executed_ops = {workspace = true} massa_pos_exports = {workspace = true, "features" = ["test-exports"]} massa_consensus_exports = {workspace = true, "features" = ["test-exports"]} massa_db_worker = {workspace = true, "features" = ["test-exports"]} -massa_test_framework = {workspace = true} +massa_test_framework = {workspace = true, "features" = ["test-exports"]} diff --git a/massa-consensus-worker/Cargo.toml b/massa-consensus-worker/Cargo.toml index 944dbf25564..d09a1dc1144 100644 --- a/massa-consensus-worker/Cargo.toml +++ b/massa-consensus-worker/Cargo.toml @@ -37,7 +37,7 @@ massa_pos_exports = {workspace = true, features = ["test-exports"]} massa_protocol_exports = {workspace = true, features = ["test-exports"]} massa_execution_exports = {workspace = true, features = ["test-exports"]} massa_consensus_exports = {workspace = true, features = ["test-exports"]} -massa_test_framework = {workspace = true} +massa_test_framework = {workspace = true, "features" = ["test-exports"]} mockall = {workspace = true} rand = {workspace = true} itertools = {workspace = true} diff --git a/massa-execution-worker/Cargo.toml b/massa-execution-worker/Cargo.toml index 71fdd47a50e..f942cbbe152 100644 --- a/massa-execution-worker/Cargo.toml +++ b/massa-execution-worker/Cargo.toml @@ -91,7 +91,7 @@ massa_wallet = { workspace = true, features = ["test-exports"] } massa_metrics = { workspace = true, features = ["test-exports"] } massa_db_worker = { workspace = true } tempfile = { workspace = true } -massa_test_framework = {workspace = true} +massa_test_framework = {workspace = true, "features" = ["test-exports"]} tokio = { workspace = true, features = ["sync"] } hex-literal = { workspace = true } mockall = { workspace = true } diff --git a/massa-execution-worker/src/tests/wasm/send_message_condition.wasm b/massa-execution-worker/src/tests/wasm/send_message_condition.wasm index 7d7a4b68c77..54cf830ff1c 100644 Binary files a/massa-execution-worker/src/tests/wasm/send_message_condition.wasm and b/massa-execution-worker/src/tests/wasm/send_message_condition.wasm differ diff --git a/massa-protocol-worker/Cargo.toml b/massa-protocol-worker/Cargo.toml index 3722c41e20d..2f9db883e37 100644 --- a/massa-protocol-worker/Cargo.toml +++ b/massa-protocol-worker/Cargo.toml @@ -39,7 +39,7 @@ static_assertions = {workspace = true} [dev-dependencies] tempfile = {workspace = true} # BOM UPGRADE Revert to "3.3" if problem serial_test = {workspace = true} # BOM UPGRADE Revert to "2.0.0" if problem -massa_test_framework = {workspace = true} +massa_test_framework = {workspace = true, "features" = ["test-exports"]} mockall = {workspace = true} mockall_wrap = {workspace = true} num = {workspace = true} diff --git a/massa-test-framework/Cargo.toml b/massa-test-framework/Cargo.toml index b1f23d80b37..7253f9d43ea 100644 --- a/massa-test-framework/Cargo.toml +++ b/massa-test-framework/Cargo.toml @@ -6,6 +6,9 @@ edition = "2021" [dependencies] massa_hash = { path = "../massa-hash" } -massa_models = { path = "../massa-models", features = ["test-exports"]} +massa_models = { path = "../massa-models"} massa_signature = { path = "../massa-signature" } tracing-subscriber = { workspace = true, features = ["env-filter"]} + +[features] +test-exports = ["massa_models/test-exports"] diff --git a/massa-test-framework/src/framework.rs b/massa-test-framework/src/framework.rs new file mode 100644 index 00000000000..c42b2d5fed6 --- /dev/null +++ b/massa-test-framework/src/framework.rs @@ -0,0 +1,151 @@ +use std::sync::{Arc, Condvar, Mutex}; + +use massa_hash::Hash; +use massa_models::{ + address::Address, + amount::Amount, + block::{Block, BlockSerializer, SecureShareBlock}, + block_header::{BlockHeader, BlockHeaderSerializer}, + block_id::BlockId, + denunciation::Denunciation, + endorsement::{Endorsement, EndorsementSerializer, SecureShareEndorsement}, + operation::{ + compute_operations_hash, Operation, OperationIdSerializer, OperationSerializer, + OperationType, SecureShareOperation, + }, + secure_share::SecureShareContent, + slot::Slot, +}; +use massa_signature::KeyPair; + +pub trait TestUniverse { + type ForeignControllers; + type Config: Default; + + fn new(controllers: Self::ForeignControllers, config: Self::Config) -> Self; + + fn initialize(&self) { + //TODO: unusable now when launching multiple tests. need a fix + // let default_panic = std::panic::take_hook(); + // std::panic::set_hook(Box::new(move |info| { + // default_panic(info); + // std::process::exit(1); + // })); + use tracing_subscriber::{prelude::*, EnvFilter}; + let tracing_layer = + tracing_subscriber::fmt::layer().with_filter(EnvFilter::from_default_env()); + let _ = tracing_subscriber::registry() + .with(tracing_layer) + .try_init(); + } + + // TODO: Create a block builder + fn create_block( + keypair: &KeyPair, + slot: Slot, + operations: Vec, + endorsements: Vec, + denunciations: Vec, + ) -> SecureShareBlock { + let op_ids = operations.iter().map(|op| op.id).collect::>(); + let operation_merkle_root = compute_operations_hash(&op_ids, &OperationIdSerializer::new()); + let header = BlockHeader::new_verifiable( + BlockHeader { + current_version: 0, + announced_version: None, + slot, + parents: vec![ + BlockId::generate_from_hash(Hash::compute_from("Genesis 0".as_bytes())), + BlockId::generate_from_hash(Hash::compute_from("Genesis 1".as_bytes())), + ], + operation_merkle_root, + endorsements, + denunciations, + }, + BlockHeaderSerializer::new(), + keypair, + 0, + ) + .unwrap(); + + Block::new_verifiable( + Block { + header, + operations: op_ids, + }, + BlockSerializer::new(), + keypair, + 0, + ) + .unwrap() + } + + fn create_operation( + keypair: &KeyPair, + expire_period: u64, + chain_id: u64, + ) -> SecureShareOperation { + let recv_keypair = KeyPair::generate(0).unwrap(); + + let op = OperationType::Transaction { + recipient_address: Address::from_public_key(&recv_keypair.get_public_key()), + amount: Amount::default(), + }; + let content = Operation { + fee: Amount::default(), + op, + expire_period, + }; + Operation::new_verifiable(content, OperationSerializer::new(), keypair, chain_id).unwrap() + } + + fn create_endorsement(creator: &KeyPair, slot: Slot) -> SecureShareEndorsement { + let content = Endorsement { + slot, + index: 0, + endorsed_block: BlockId::generate_from_hash(Hash::compute_from("Genesis 1".as_bytes())), + }; + Endorsement::new_verifiable(content, EndorsementSerializer::new(), creator, 0).unwrap() + } +} + +pub struct WaitPoint(Arc); + +struct WaitPointInner { + mutex: Mutex, + condvar: Condvar, +} + +impl Default for WaitPoint { + fn default() -> Self { + Self::new() + } +} + +impl WaitPoint { + pub fn new() -> Self { + Self(Arc::new(WaitPointInner { + mutex: Mutex::new(false), + condvar: Condvar::new(), + })) + } + + pub fn get_trigger_handle(&self) -> WaitPoint { + WaitPoint(self.0.clone()) + } + + pub fn wait(&self) { + let mut started = self.0.mutex.lock().unwrap(); + *started = false; + while !*started { + started = self.0.condvar.wait(started).unwrap(); + } + } + + pub fn trigger(&self) { + let mut started = self.0.mutex.lock().unwrap(); + *started = true; + // We notify the condvar that the value has changed. + self.0.condvar.notify_one(); + } +} diff --git a/massa-test-framework/src/lib.rs b/massa-test-framework/src/lib.rs index c42b2d5fed6..bba11cd3d59 100644 --- a/massa-test-framework/src/lib.rs +++ b/massa-test-framework/src/lib.rs @@ -1,151 +1,5 @@ -use std::sync::{Arc, Condvar, Mutex}; +#[cfg(feature = "test-exports")] +mod framework; -use massa_hash::Hash; -use massa_models::{ - address::Address, - amount::Amount, - block::{Block, BlockSerializer, SecureShareBlock}, - block_header::{BlockHeader, BlockHeaderSerializer}, - block_id::BlockId, - denunciation::Denunciation, - endorsement::{Endorsement, EndorsementSerializer, SecureShareEndorsement}, - operation::{ - compute_operations_hash, Operation, OperationIdSerializer, OperationSerializer, - OperationType, SecureShareOperation, - }, - secure_share::SecureShareContent, - slot::Slot, -}; -use massa_signature::KeyPair; - -pub trait TestUniverse { - type ForeignControllers; - type Config: Default; - - fn new(controllers: Self::ForeignControllers, config: Self::Config) -> Self; - - fn initialize(&self) { - //TODO: unusable now when launching multiple tests. need a fix - // let default_panic = std::panic::take_hook(); - // std::panic::set_hook(Box::new(move |info| { - // default_panic(info); - // std::process::exit(1); - // })); - use tracing_subscriber::{prelude::*, EnvFilter}; - let tracing_layer = - tracing_subscriber::fmt::layer().with_filter(EnvFilter::from_default_env()); - let _ = tracing_subscriber::registry() - .with(tracing_layer) - .try_init(); - } - - // TODO: Create a block builder - fn create_block( - keypair: &KeyPair, - slot: Slot, - operations: Vec, - endorsements: Vec, - denunciations: Vec, - ) -> SecureShareBlock { - let op_ids = operations.iter().map(|op| op.id).collect::>(); - let operation_merkle_root = compute_operations_hash(&op_ids, &OperationIdSerializer::new()); - let header = BlockHeader::new_verifiable( - BlockHeader { - current_version: 0, - announced_version: None, - slot, - parents: vec![ - BlockId::generate_from_hash(Hash::compute_from("Genesis 0".as_bytes())), - BlockId::generate_from_hash(Hash::compute_from("Genesis 1".as_bytes())), - ], - operation_merkle_root, - endorsements, - denunciations, - }, - BlockHeaderSerializer::new(), - keypair, - 0, - ) - .unwrap(); - - Block::new_verifiable( - Block { - header, - operations: op_ids, - }, - BlockSerializer::new(), - keypair, - 0, - ) - .unwrap() - } - - fn create_operation( - keypair: &KeyPair, - expire_period: u64, - chain_id: u64, - ) -> SecureShareOperation { - let recv_keypair = KeyPair::generate(0).unwrap(); - - let op = OperationType::Transaction { - recipient_address: Address::from_public_key(&recv_keypair.get_public_key()), - amount: Amount::default(), - }; - let content = Operation { - fee: Amount::default(), - op, - expire_period, - }; - Operation::new_verifiable(content, OperationSerializer::new(), keypair, chain_id).unwrap() - } - - fn create_endorsement(creator: &KeyPair, slot: Slot) -> SecureShareEndorsement { - let content = Endorsement { - slot, - index: 0, - endorsed_block: BlockId::generate_from_hash(Hash::compute_from("Genesis 1".as_bytes())), - }; - Endorsement::new_verifiable(content, EndorsementSerializer::new(), creator, 0).unwrap() - } -} - -pub struct WaitPoint(Arc); - -struct WaitPointInner { - mutex: Mutex, - condvar: Condvar, -} - -impl Default for WaitPoint { - fn default() -> Self { - Self::new() - } -} - -impl WaitPoint { - pub fn new() -> Self { - Self(Arc::new(WaitPointInner { - mutex: Mutex::new(false), - condvar: Condvar::new(), - })) - } - - pub fn get_trigger_handle(&self) -> WaitPoint { - WaitPoint(self.0.clone()) - } - - pub fn wait(&self) { - let mut started = self.0.mutex.lock().unwrap(); - *started = false; - while !*started { - started = self.0.condvar.wait(started).unwrap(); - } - } - - pub fn trigger(&self) { - let mut started = self.0.mutex.lock().unwrap(); - *started = true; - // We notify the condvar that the value has changed. - self.0.condvar.notify_one(); - } -} +#[cfg(feature = "test-exports")] +pub use framework::{TestUniverse, WaitPoint}; diff --git a/tools/setup_test.rs b/tools/setup_test.rs index d58877e5c33..3bf11fefd7e 100644 --- a/tools/setup_test.rs +++ b/tools/setup_test.rs @@ -102,7 +102,7 @@ fn download_src() -> Result { archive.unpack(extract_folder.clone())?; Ok(format!( - "{}/massa_unit_tests/*.wasm", + "{}/build/massa/*.wasm", extract_folder.clone() )) }