Skip to content

Commit

Permalink
Merge #4487
Browse files Browse the repository at this point in the history
4487: Add tests factory r=AurelienFT a=AurelienFT

Massa factory now has 73% coverage

Co-authored-by: AurelienFT <[email protected]>
  • Loading branch information
bors[bot] and AurelienFT authored Oct 19, 2023
2 parents 516b329 + 16f6a79 commit 630d35d
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 101 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions massa-factory-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ massa_versioning = {workspace = true}

[dev-dependencies]
num = {workspace = true}
massa_hash = {workspace = true}
massa_protocol_exports = {workspace = true, "features" = ["testing"]}
massa_consensus_exports = {workspace = true, "features" = ["testing"]}
massa_factory_exports = {workspace = true, "features" = ["testing"]}
massa_wallet = {workspace = true, "features" = ["testing"]}
massa_pos_exports = {workspace = true, "features" = ["testing"]}
massa_pool_exports = {workspace = true, "features" = ["testing"]}
serial_test = { workspace = true }
184 changes: 184 additions & 0 deletions massa-factory-worker/src/tests/block_scenarios.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
use std::{str::FromStr, sync::Arc};

use super::BlockTestFactory;
use massa_consensus_exports::MockConsensusController;
use massa_hash::Hash;
use massa_models::{
address::Address,
amount::Amount,
block_id::BlockId,
config::THREAD_COUNT,
operation::{Operation, OperationSerializer, OperationType},
secure_share::SecureShareContent,
slot::Slot,
};
use massa_pool_exports::MockPoolController;
use massa_pos_exports::MockSelectorController;
use massa_signature::KeyPair;
use massa_storage::Storage;
use parking_lot::{Condvar, Mutex};
use serial_test::serial;

/// Creates a basic empty block with the factory.
#[test]
#[serial]
fn basic_creation() {
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
default_panic(info);
std::process::exit(1);
}));
let keypair = KeyPair::generate(0).unwrap();
let storage = Storage::create_root();
let staking_address = Address::from_public_key(&keypair.get_public_key());
let parent = BlockId::generate_from_hash(Hash::compute_from("test".as_bytes()));
let mut parents = Vec::new();
for i in 0..THREAD_COUNT as u64 {
parents.push((parent, i));
}
let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = pair.clone();
let mut consensus_controller = Box::new(MockConsensusController::new());
consensus_controller
.expect_get_best_parents()
.times(1)
.return_once(move || parents);
consensus_controller
.expect_register_block()
.times(1)
.return_once(move |_, _, storage, created| {
assert!(created);
let block = storage.get_block_refs();
assert_eq!(block.len(), 1);
let (lock, cvar) = &*pair2;
let mut started = lock.lock();
*started = true;
cvar.notify_one();
});
let mut selector_controller = Box::new(MockSelectorController::new());
selector_controller
.expect_get_producer()
.times(1)
.return_once(move |_| Ok(staking_address));
let mut pool_controller = Box::new(MockPoolController::new());
pool_controller
.expect_get_block_denunciations()
.returning(|slot| {
assert_eq!(*slot, Slot::new(1, 0));
vec![]
});
pool_controller
.expect_get_block_operations()
.returning(|slot| {
assert_eq!(*slot, Slot::new(1, 0));
(vec![], Storage::create_root())
});
pool_controller
.expect_get_block_endorsements()
.returning(|_, slot| {
assert_eq!(*slot, Slot::new(1, 0));
(vec![], Storage::create_root())
});
let mut test_factory = BlockTestFactory::new(
&keypair,
storage,
consensus_controller,
selector_controller,
pool_controller,
);
let (ref lock, ref cvar) = *pair;
let mut started = lock.lock();
if !*started {
cvar.wait(&mut started);
}
test_factory.stop();
}

/// Creates a block with a roll buy operation in it.
#[test]
#[serial]
fn basic_creation_with_operation() {
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
default_panic(info);
std::process::exit(1);
}));
let keypair = KeyPair::generate(0).unwrap();
let staking_address = Address::from_public_key(&keypair.get_public_key());
let parent = BlockId::generate_from_hash(Hash::compute_from("test".as_bytes()));
let mut parents = Vec::new();
for i in 0..THREAD_COUNT as u64 {
parents.push((parent, i));
}
let mut selector_controller = Box::new(MockSelectorController::new());
selector_controller
.expect_get_producer()
.times(1)
.return_once(move |_| Ok(staking_address));
let mut pool_controller = Box::new(MockPoolController::new());
pool_controller
.expect_get_block_denunciations()
.returning(|slot| {
assert_eq!(*slot, Slot::new(1, 0));
vec![]
});
let storage = Storage::create_root();
let keypair_clone = keypair.clone();
let mut pool_storage = storage.clone_without_refs();
pool_controller
.expect_get_block_operations()
.returning(move |slot| {
assert_eq!(*slot, Slot::new(1, 0));
let content = Operation {
fee: Amount::from_str("0.01").unwrap(),
expire_period: 2,
op: OperationType::RollBuy { roll_count: 1 },
};
let operation =
Operation::new_verifiable(content, OperationSerializer::new(), &keypair_clone)
.unwrap();
pool_storage.store_operations(vec![operation.clone()]);
(vec![operation.id], pool_storage.clone())
});
let pool_storage_2 = storage.clone_without_refs();
pool_controller
.expect_get_block_endorsements()
.returning(move |_, slot| {
assert_eq!(*slot, Slot::new(1, 0));
(vec![], pool_storage_2.clone())
});
let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = pair.clone();
let mut consensus_controller = Box::new(MockConsensusController::new());
consensus_controller
.expect_get_best_parents()
.times(1)
.return_once(move || parents);
consensus_controller
.expect_register_block()
.times(1)
.return_once(move |_, _, storage, created| {
assert!(created);
let block = storage.get_block_refs();
assert_eq!(block.len(), 1);
let ops = storage.get_op_refs();
assert_eq!(ops.len(), 1);
let (lock, cvar) = &*pair2;
let mut started = lock.lock();
*started = true;
cvar.notify_one();
});
let mut test_factory = BlockTestFactory::new(
&keypair,
storage,
consensus_controller,
selector_controller,
pool_controller,
);
let (lock, cvar) = &*pair;
let mut started = lock.lock();
if !*started {
cvar.wait(&mut started);
}
test_factory.stop();
}
96 changes: 96 additions & 0 deletions massa-factory-worker/src/tests/endorsement_scenarios.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use std::sync::Arc;

use super::EndorsementTestFactory;
use massa_consensus_exports::MockConsensusController;
use massa_hash::Hash;
use massa_models::{
address::Address,
block_id::BlockId,
config::{ENDORSEMENT_COUNT, THREAD_COUNT},
slot::Slot,
};
use massa_pool_exports::MockPoolController;
use massa_pos_exports::{MockSelectorController, Selection};
use massa_protocol_exports::MockProtocolController;
use massa_signature::KeyPair;
use massa_storage::Storage;
use parking_lot::{Condvar, Mutex};
use serial_test::serial;

/// Creates a basic empty block with the factory.
#[test]
#[serial]
fn basic_creation() {
let default_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
default_panic(info);
std::process::exit(1);
}));
let keypair = KeyPair::generate(0).unwrap();
let storage = Storage::create_root();
let staking_address = Address::from_public_key(&keypair.get_public_key());
let parent = BlockId::generate_from_hash(Hash::compute_from("test".as_bytes()));
let mut parents = Vec::new();
for i in 0..THREAD_COUNT as u64 {
parents.push((parent, i));
}
let pair = Arc::new((Mutex::new(false), Condvar::new()));
let pair2 = pair.clone();
let mut consensus_controller = Box::new(MockConsensusController::new());
consensus_controller
.expect_get_latest_blockclique_block_at_slot()
.times(1)
.returning(move |slot| {
assert_eq!(slot, Slot::new(1, 0));
parent
});
let mut selector_controller = Box::new(MockSelectorController::new());
selector_controller
.expect_get_selection()
.times(1)
.returning(move |slot| {
assert_eq!(slot, Slot::new(1, 0));
Ok(Selection {
producer: staking_address,
endorsements: vec![staking_address; ENDORSEMENT_COUNT as usize],
})
});
let mut pool_controller = Box::new(MockPoolController::new());
pool_controller
.expect_add_endorsements()
.times(1)
.returning(|_| {});
let mut protocol_controller = Box::new(MockProtocolController::new());
protocol_controller
.expect_propagate_endorsements()
.times(1)
.returning(move |storage| {
let endorsement_ids = storage.get_endorsement_refs();
assert_eq!(endorsement_ids.len(), ENDORSEMENT_COUNT as usize);
let first_endorsement = storage
.read_endorsements()
.get(endorsement_ids.iter().next().unwrap())
.unwrap()
.clone();
assert_eq!(first_endorsement.content.slot, Slot::new(1, 0));
let (lock, cvar) = &*pair2;
let mut started = lock.lock();
*started = true;
cvar.notify_one();
Ok(())
});
let mut test_factory = EndorsementTestFactory::new(
&keypair,
storage,
consensus_controller,
selector_controller,
pool_controller,
protocol_controller,
);
let (lock, cvar) = &*pair;
let mut started = lock.lock();
if !*started {
cvar.wait(&mut started);
}
test_factory.stop();
}
3 changes: 2 additions & 1 deletion massa-factory-worker/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod scenarios;
mod block_scenarios;
mod endorsement_scenarios;
mod tools;

pub use tools::*;
56 changes: 0 additions & 56 deletions massa-factory-worker/src/tests/scenarios.rs

This file was deleted.

Loading

0 comments on commit 630d35d

Please sign in to comment.