Skip to content

Commit

Permalink
Feature gate test framework (#4642)
Browse files Browse the repository at this point in the history
* Only enable test framework on feature

* Bump shlex to 1.3.0

* Update glob pattern in setup test?

* Update ci.yml

* Update ci.yml

* Update send_message_condition.wasm

* Update CI to use rust-script instead of unmaintained cargo script
  • Loading branch information
Leo-Besancon authored Feb 14, 2024
1 parent 9a34d7a commit 9ce47f7
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 159 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion massa-bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}
2 changes: 1 addition & 1 deletion massa-consensus-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion massa-execution-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Binary file not shown.
2 changes: 1 addition & 1 deletion massa-protocol-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
5 changes: 4 additions & 1 deletion massa-test-framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
151 changes: 151 additions & 0 deletions massa-test-framework/src/framework.rs
Original file line number Diff line number Diff line change
@@ -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<SecureShareOperation>,
endorsements: Vec<SecureShareEndorsement>,
denunciations: Vec<Denunciation>,
) -> SecureShareBlock {
let op_ids = operations.iter().map(|op| op.id).collect::<Vec<_>>();
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<WaitPointInner>);

struct WaitPointInner {
mutex: Mutex<bool>,
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();
}
}
154 changes: 4 additions & 150 deletions massa-test-framework/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<SecureShareOperation>,
endorsements: Vec<SecureShareEndorsement>,
denunciations: Vec<Denunciation>,
) -> SecureShareBlock {
let op_ids = operations.iter().map(|op| op.id).collect::<Vec<_>>();
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<WaitPointInner>);

struct WaitPointInner {
mutex: Mutex<bool>,
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};
2 changes: 1 addition & 1 deletion tools/setup_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn download_src() -> Result<String, Error> {
archive.unpack(extract_folder.clone())?;

Ok(format!(
"{}/massa_unit_tests/*.wasm",
"{}/build/massa/*.wasm",
extract_folder.clone()
))
}
Expand Down

0 comments on commit 9ce47f7

Please sign in to comment.