Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock Final State/Improve tests execution #4512

Merged
merged 14 commits into from
Nov 10, 2023
Prev Previous commit
Next Next commit
Use more mocks in execution tests
  • Loading branch information
AurelienFT committed Nov 3, 2023

Verified

This commit was signed with the committer’s verified signature. The key has expired.
vespian Paweł Rozlach
commit 7904d8b4d74d3ec67be45e859d5b66966178d6ca
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.

11 changes: 11 additions & 0 deletions massa-execution-worker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -15,6 +15,8 @@ gas_calibration = [
"massa_execution_exports/gas_calibration",
"massa_final_state/test-exports",
"massa_pos_worker",
"massa_db_worker",
"tempfile"
]
test-exports = [
"massa_execution_exports/test-exports",
@@ -26,11 +28,15 @@ test-exports = [
"massa_pos_worker",
"massa_metrics/test-exports",
"massa_metrics/test-exports",
"massa_db_worker",
"tempfile"
]
benchmarking = [
"massa-sc-runtime/gas_calibration",
"criterion",
"massa_pos_worker",
"massa_db_worker",
"tempfile"
]
metrics = []

@@ -68,6 +74,8 @@ massa_pos_exports = { workspace = true }
massa_final_state = { workspace = true }
massa_versioning = { workspace = true }
massa_db_exports = { workspace = true }
massa_db_worker = { workspace = true, optional = true }
tempfile = { workspace = true, optional = true }
massa_wallet = { workspace = true }
massa-proto-rs = { workspace = true }

@@ -81,6 +89,9 @@ massa_pos_worker = { workspace = true }
massa-sc-runtime = { workspace = true, features = ["testing"] }
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}
tokio = { workspace = true, features = ["sync"] }
hex-literal = { workspace = true }
mockall = { workspace = true }
19 changes: 17 additions & 2 deletions massa-execution-worker/src/interface_impl.rs
Original file line number Diff line number Diff line change
@@ -88,14 +88,17 @@ impl InterfaceImpl {
sender_addr: Address,
operation_datastore: Option<Datastore>,
) -> InterfaceImpl {
use massa_db_exports::{MassaDBConfig, MassaDBController};
use massa_db_worker::MassaDB;
use massa_final_state::test_exports::get_sample_state;
use massa_ledger_exports::{LedgerEntry, SetUpdateOrDelete};
use massa_models::config::MIP_STORE_STATS_BLOCK_CONSIDERED;
use massa_models::config::{MIP_STORE_STATS_BLOCK_CONSIDERED, THREAD_COUNT};
use massa_module_cache::{config::ModuleCacheConfig, controller::ModuleCache};
use massa_pos_exports::SelectorConfig;
use massa_pos_worker::start_selector_worker;
use massa_versioning::versioning::{MipStatsConfig, MipStore};
use parking_lot::RwLock;
use tempfile::TempDir;

let config = ExecutionConfig::default();
let mip_stats_config = MipStatsConfig {
@@ -105,8 +108,20 @@ impl InterfaceImpl {
let mip_store = MipStore::try_from(([], mip_stats_config)).unwrap();
let (_, selector_controller) = start_selector_worker(SelectorConfig::default())
.expect("could not start selector controller");
let disk_ledger = TempDir::new().expect("cannot create temp directory");
let db_config = MassaDBConfig {
path: disk_ledger.path().to_path_buf(),
max_history_length: 10,
max_final_state_elements_size: 100_000,
max_versioning_elements_size: 100_000,
thread_count: THREAD_COUNT,
};

let db = Arc::new(RwLock::new(
Box::new(MassaDB::new(db_config)) as Box<(dyn MassaDBController + 'static)>
));
let (final_state, _tempfile, _tempdir) =
get_sample_state(config.last_start_period, selector_controller, mip_store).unwrap();
get_sample_state(config.last_start_period, selector_controller, mip_store, db).unwrap();
let module_cache = Arc::new(RwLock::new(ModuleCache::new(ModuleCacheConfig {
hd_cache_path: config.hd_cache_path.clone(),
gas_costs: config.gas_costs.clone(),
150 changes: 145 additions & 5 deletions massa-execution-worker/src/tests/scenarios_mandatories.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright (c) 2022 MASSA LABS <[email protected]>

use massa_async_pool::{AsyncPool, AsyncPoolConfig};
use massa_db_exports::DBBatch;
use massa_executed_ops::{ExecutedOps, ExecutedOpsConfig};
use massa_execution_exports::{
ExecutionBlockMetadata, ExecutionConfig, ExecutionQueryRequest, ExecutionQueryRequestItem,
ReadOnlyExecutionRequest, ReadOnlyExecutionTarget,
};
use massa_final_state::test_exports::get_initials;
use massa_hash::Hash;
use massa_models::config::{
ENDORSEMENT_COUNT, LEDGER_ENTRY_BASE_COST, LEDGER_ENTRY_DATASTORE_BASE_SIZE,
};
@@ -18,11 +22,12 @@ use massa_models::{
operation::{Operation, OperationSerializer, OperationType},
secure_share::SecureShareContent,
};
use massa_pos_exports::{MockSelectorControllerWrapper, Selection};
use massa_pos_exports::{MockSelectorControllerWrapper, PoSConfig, PoSFinalState, Selection};
use massa_signature::KeyPair;
use massa_storage::Storage;
use massa_test_framework::TestUniverse;
use massa_time::MassaTime;
use mockall::Sequence;
use num::rational::Ratio;
use std::{
cmp::Reverse, collections::BTreeMap, collections::HashMap, str::FromStr, time::Duration,
@@ -64,12 +69,29 @@ fn selector_boilerplate(
fn test_execution_shutdown() {
let block_producer = KeyPair::generate(0).unwrap();
let mut foreign_controllers = ExecutionForeignControllers::new_with_mocks();
foreign_controllers
.final_state
.write()
.expect_get_slot()
.returning(move || Slot::new(0, 0));
foreign_controllers
.final_state
.write()
.expect_get_execution_trail_hash()
.returning(|| Hash::compute_from("Genesis".as_bytes()));
foreign_controllers
.final_state
.write()
.expect_get_async_pool()
.return_const(AsyncPool::new(
AsyncPoolConfig::default(),
foreign_controllers.db.clone(),
));
selector_boilerplate(
&mut foreign_controllers.selector_controller,
&block_producer,
);
ExecutionTestUniverse::new(foreign_controllers, ExecutionConfig::default());
std::thread::sleep(Duration::from_millis(100));
}

#[test]
@@ -80,13 +102,30 @@ fn test_sending_command() {
&mut foreign_controllers.selector_controller,
&block_producer,
);
foreign_controllers
.final_state
.write()
.expect_get_slot()
.returning(move || Slot::new(0, 0));
foreign_controllers
.final_state
.write()
.expect_get_execution_trail_hash()
.returning(|| Hash::compute_from("Genesis".as_bytes()));
foreign_controllers
.final_state
.write()
.expect_get_async_pool()
.return_const(AsyncPool::new(
AsyncPoolConfig::default(),
foreign_controllers.db.clone(),
));
let universe = ExecutionTestUniverse::new(foreign_controllers, ExecutionConfig::default());
universe.module_controller.update_blockclique_status(
Default::default(),
Default::default(),
Default::default(),
);
std::thread::sleep(Duration::from_millis(100));
}

#[test]
@@ -103,6 +142,39 @@ fn test_readonly_execution() {
&mut foreign_controllers.selector_controller,
&block_producer,
);
let (rolls_path, _) = get_initials();
foreign_controllers
.final_state
.write()
.expect_get_slot()
.returning(move || Slot::new(0, 0));
foreign_controllers
.final_state
.write()
.expect_get_execution_trail_hash()
.returning(|| Hash::compute_from("Genesis".as_bytes()));
foreign_controllers
.final_state
.write()
.expect_get_pos_state()
.return_const(
PoSFinalState::new(
PoSConfig::default(),
"",
&rolls_path.into_temp_path().to_path_buf(),
foreign_controllers.selector_controller.clone(),
foreign_controllers.db.clone(),
)
.unwrap(),
);
foreign_controllers
.final_state
.write()
.expect_get_async_pool()
.return_const(AsyncPool::new(
AsyncPoolConfig::default(),
foreign_controllers.db.clone(),
));
let universe = ExecutionTestUniverse::new(foreign_controllers, exec_cfg);

let mut res = universe
@@ -119,7 +191,7 @@ fn test_readonly_execution() {
})
.expect("readonly execution failed");

assert_eq!(res.out.slot, Slot::new(1, 0));
assert_eq!(res.out.slot, Slot::new(0, 1));
assert!(res.gas_cost > 0);
assert_eq!(res.out.events.take().len(), 1, "wrong number of events");
}
@@ -137,7 +209,8 @@ fn test_nested_call_gas_usage() {
// setup the period duration
let exec_cfg = ExecutionConfig {
t0: MassaTime::from_millis(100),
cursor_delay: MassaTime::from_millis(0),
cursor_delay: MassaTime::from_millis(20),
thread_count: 2,
..ExecutionConfig::default()
};
let block_producer = KeyPair::generate(0).unwrap();
@@ -146,6 +219,73 @@ fn test_nested_call_gas_usage() {
&mut foreign_controllers.selector_controller,
&block_producer,
);
let (rolls_path, _) = get_initials();
let mut seq = Sequence::new();
for period in 0..2 {
for thread in 0..2 {
foreign_controllers
.final_state
.write()
.expect_get_slot()
.times(1)
.in_sequence(&mut seq)
.returning(move || Slot::new(period, thread));
}
}
foreign_controllers
.final_state
.write()
.expect_get_execution_trail_hash()
.returning(|| Hash::compute_from("Genesis".as_bytes()));
foreign_controllers
.final_state
.write()
.expect_get_pos_state()
.return_const(
PoSFinalState::new(
PoSConfig::default(),
"",
&rolls_path.into_temp_path().to_path_buf(),
foreign_controllers.selector_controller.clone(),
foreign_controllers.db.clone(),
)
.unwrap(),
);
foreign_controllers
.final_state
.write()
.expect_get_async_pool()
.return_const(AsyncPool::new(
AsyncPoolConfig::default(),
foreign_controllers.db.clone(),
));
foreign_controllers
.final_state
.write()
.expect_get_executed_ops()
.return_const(ExecutedOps::new(
ExecutedOpsConfig {
thread_count: 2,
keep_executed_history_extra_periods: 10,
},
foreign_controllers.db.clone(),
));
foreign_controllers
.ledger_controller
.set_expectations(|ledger_controller| {
ledger_controller
.expect_get_balance()
.returning(move |_| Some(Amount::from_str("100").unwrap()));

ledger_controller
.expect_entry_exists()
.returning(move |_| true);
});
foreign_controllers
.final_state
.write()
.expect_get_ledger()
.return_const(Box::new(foreign_controllers.ledger_controller.clone()));
let mut universe = ExecutionTestUniverse::new(foreign_controllers, exec_cfg);

// get random keypair
Loading