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

refactor: desrialization structs for regression test output #293

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions crates/committer_cli/src/tests/benchmark_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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.
#[derive(Deserialize)]
struct CommitterRegressionInput {
committer_input: String,
Expand All @@ -25,6 +26,24 @@ struct CommitterRegressionInput {
expected_facts: String,
}

#[derive(Deserialize)]
struct TreeRegressionOutput {
root_hash: Value,
storage_changes: Value,
}

#[derive(Deserialize)]
struct StorageObject {
storage: Value,
}

#[derive(Deserialize)]
struct CommitterRegressionOutput {
contract_storage_root_hash: Value,
compiled_class_root_hash: Value,
storage: StorageObject,
}

struct TreeRegressionInput {
tree_flow_input: TreeFlowInput,
expected_hash: String,
Expand Down Expand Up @@ -69,17 +88,16 @@ pub async fn test_benchmark_single_tree() {
let execution_time = std::time::Instant::now() - start;

// Assert correctness of the output of the single tree flow test.
// TODO(Aner, 8/7/2024): use structs for deserialization.
let output_map: HashMap<&str, Value> = serde_json::from_str(&output).unwrap();
let output_hash = output_map.get("root_hash").unwrap();
assert_eq!(output_hash.as_str().unwrap(), expected_hash);

// Assert the storage changes.
let Value::Object(storage_changes) = output_map.get("storage_changes").unwrap() else {
let TreeRegressionOutput {
root_hash,
storage_changes: Value::Object(actual_storage_changes),
} = serde_json::from_str(&output).unwrap()
else {
panic!("Expected storage changes object to be an object.");
};
assert_eq!(root_hash, expected_hash);

assert_eq!(storage_changes, &expected_storage_changes);
assert_eq!(actual_storage_changes, expected_storage_changes);

// 4. Assert the execution time does not exceed the threshold.
assert!(execution_time.as_secs_f64() < MAX_TIME_FOR_SINGLE_TREE_BECHMARK_TEST);
Expand All @@ -96,37 +114,27 @@ pub async fn test_benchmark_committer_flow() {
let execution_time = std::time::Instant::now() - start;

// Assert the output of the committer flow test.
// TODO(Aner, 8/7/2024): use structs for deserialization.
let committer_output: HashMap<String, Value> =
let committer_output: CommitterRegressionOutput =
serde_json::from_str(&std::fs::read_to_string(OUTPUT_PATH).unwrap()).unwrap();

let contract_storage_root_hash = committer_output.get("contract_storage_root_hash").unwrap();
let compiled_class_root_hash = committer_output.get("compiled_class_root_hash").unwrap();

assert_eq!(
contract_storage_root_hash.as_str().unwrap(),
committer_output.contract_storage_root_hash,
regression_input.contract_states_root
);
assert_eq!(
compiled_class_root_hash.as_str().unwrap(),
committer_output.compiled_class_root_hash,
regression_input.contract_classes_root
);
// Assert the storage changes.
// TODO(Aner, 8/7/2024): use structs for deserialization.
let Value::Object(storage_changes) = committer_output
.get("storage")
.unwrap()
.get("storage")
.unwrap()
else {
let Value::Object(storage_changes) = committer_output.storage.storage else {
panic!("Expected the storage to be an object.");
};

// TODO(Aner, 8/7/2024): fix to deserialize automatically.
// 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!(storage_changes, expected_storage_changes);

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