From 1a814b99d23517b21b3dcf49459be1bf4ea26a0a Mon Sep 17 00:00:00 2001 From: aner-starkware <147302140+aner-starkware@users.noreply.github.com> Date: Tue, 9 Jul 2024 19:02:56 +0300 Subject: [PATCH] refactor: deserializer for expected facts (#294) --- .../src/tests/benchmark_tests.rs | 57 +++++++++++-------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/crates/committer_cli/src/tests/benchmark_tests.rs b/crates/committer_cli/src/tests/benchmark_tests.rs index d2fb9d84..510015c1 100644 --- a/crates/committer_cli/src/tests/benchmark_tests.rs +++ b/crates/committer_cli/src/tests/benchmark_tests.rs @@ -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); + +impl<'de> Deserialize<'de> for FactMap { + fn deserialize(deserializer: D) -> Result + 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)] @@ -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(®ression_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 = - serde_json::from_str(®ression_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);