Skip to content

Commit

Permalink
refactor: deserializer for expected facts (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware authored Jul 9, 2024
1 parent d1fd771 commit 1a814b9
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions crates/committer_cli/src/tests/benchmark_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@ const SINGLE_TREE_FLOW_INPUT: &str = include_str!("../../benches/tree_flow_input
const FLOW_TEST_INPUT: &str = include_str!("../../benches/committer_flow_inputs.json");
const OUTPUT_PATH: &str = "benchmark_output.txt";

// TODO(Aner, 8/7/2024): use deserializer to extract the facts to mapping directly.
struct FactMap(Map<String, Value>);

impl<'de> Deserialize<'de> for FactMap {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Ok(Self(
serde_json::from_str(&String::deserialize(deserializer)?).unwrap(),
))
}
}

#[derive(Deserialize)]
struct CommitterRegressionInput {
committer_input: String,
contract_states_root: String,
contract_classes_root: String,
expected_facts: String,
expected_facts: FactMap,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -106,35 +118,34 @@ pub async fn test_benchmark_single_tree() {
#[ignore = "To avoid running the benchmark test in Coverage or without the --release flag."]
#[tokio::test(flavor = "multi_thread")]
pub async fn test_benchmark_committer_flow() {
let regression_input: CommitterRegressionInput = serde_json::from_str(FLOW_TEST_INPUT).unwrap();
let CommitterRegressionInput {
committer_input,
contract_states_root: expected_contract_states_root,
contract_classes_root: expected_contract_classes_root,
expected_facts,
} = serde_json::from_str(FLOW_TEST_INPUT).unwrap();

let start = std::time::Instant::now();
// Benchmark the committer flow test.
commit(&regression_input.committer_input, OUTPUT_PATH.to_owned()).await;
// TODO(Aner, 9/7/2024): refactor committer_input to be a struct.
commit(&committer_input, OUTPUT_PATH.to_owned()).await;
let execution_time = std::time::Instant::now() - start;

// Assert the output of the committer flow test.
let committer_output: CommitterRegressionOutput =
serde_json::from_str(&std::fs::read_to_string(OUTPUT_PATH).unwrap()).unwrap();

assert_eq!(
committer_output.contract_storage_root_hash,
regression_input.contract_states_root
);
assert_eq!(
committer_output.compiled_class_root_hash,
regression_input.contract_classes_root
);
// Assert the storage changes.
let Value::Object(storage_changes) = committer_output.storage.storage else {
// Assert correctness of the output of the committer flow test.
let CommitterRegressionOutput {
contract_storage_root_hash,
compiled_class_root_hash,
storage: StorageObject {
storage: Value::Object(storage_changes),
},
} = serde_json::from_str(&std::fs::read_to_string(OUTPUT_PATH).unwrap()).unwrap()
else {
panic!("Expected the storage to be an object.");
};

// TODO(Aner, 8/7/2024): fix to deserialize automatically, using deserializer.
let expected_storage_changes: Map<String, Value> =
serde_json::from_str(&regression_input.expected_facts).unwrap();

assert_eq!(storage_changes, expected_storage_changes);
assert_eq!(contract_storage_root_hash, expected_contract_states_root);
assert_eq!(compiled_class_root_hash, expected_contract_classes_root);
assert_eq!(storage_changes, expected_facts.0);

// Assert the execution time does not exceed the threshold.
assert!(execution_time.as_secs_f64() < MAX_TIME_FOR_COMMITTER_FLOW_BECHMARK_TEST);
Expand Down

0 comments on commit 1a814b9

Please sign in to comment.