From 3ef0653eac60af7a59525169d40b584d81ea1924 Mon Sep 17 00:00:00 2001 From: amos rothberg Date: Sun, 2 Feb 2025 13:11:18 +0200 Subject: [PATCH] chore(starknet_committer_and_os_cli): separate block-hash tests --- .../src/block_hash_cli.rs | 1 + .../src/block_hash_cli/run_block_hash_cli.rs | 7 +- .../src/block_hash_cli/tests.rs | 2 + .../utils => block_hash_cli/tests}/objects.rs | 0 .../src/block_hash_cli/tests/python_tests.rs | 83 +++++++++++++++++++ .../src/committer_cli/tests/python_tests.rs | 55 ------------ .../src/committer_cli/tests/utils.rs | 1 - 7 files changed, 92 insertions(+), 57 deletions(-) create mode 100644 crates/starknet_committer_and_os_cli/src/block_hash_cli/tests.rs rename crates/starknet_committer_and_os_cli/src/{committer_cli/tests/utils => block_hash_cli/tests}/objects.rs (100%) create mode 100644 crates/starknet_committer_and_os_cli/src/block_hash_cli/tests/python_tests.rs diff --git a/crates/starknet_committer_and_os_cli/src/block_hash_cli.rs b/crates/starknet_committer_and_os_cli/src/block_hash_cli.rs index 7d0636f22d..ea4948a04a 100644 --- a/crates/starknet_committer_and_os_cli/src/block_hash_cli.rs +++ b/crates/starknet_committer_and_os_cli/src/block_hash_cli.rs @@ -1 +1,2 @@ pub mod run_block_hash_cli; +pub mod tests; diff --git a/crates/starknet_committer_and_os_cli/src/block_hash_cli/run_block_hash_cli.rs b/crates/starknet_committer_and_os_cli/src/block_hash_cli/run_block_hash_cli.rs index 2adf9eb6df..a742c11136 100644 --- a/crates/starknet_committer_and_os_cli/src/block_hash_cli/run_block_hash_cli.rs +++ b/crates/starknet_committer_and_os_cli/src/block_hash_cli/run_block_hash_cli.rs @@ -5,9 +5,10 @@ use starknet_api::block_hash::block_hash_calculator::{ }; use tracing::info; +use crate::block_hash_cli::tests::python_tests::BlockHashPythonTestRunner; use crate::committer_cli::block_hash::{BlockCommitmentsInput, BlockHashInput}; use crate::shared_utils::read::{load_input, write_to_file}; -use crate::shared_utils::types::IoArgs; +use crate::shared_utils::types::{run_python_test, IoArgs, PythonTestArg}; #[derive(Parser, Debug)] pub struct BlockHashCliCommand { @@ -27,6 +28,7 @@ enum Command { #[clap(flatten)] io_args: IoArgs, }, + PythonTest(PythonTestArg), } pub async fn run_block_hash_cli(block_hash_cli_command: BlockHashCliCommand) { @@ -54,5 +56,8 @@ pub async fn run_block_hash_cli(block_hash_cli_command: BlockHashCliCommand) { write_to_file(&output_path, &commitments); info!("Successfully computed block hash commitment: \n{:?}", commitments); } + Command::PythonTest(python_test_arg) => { + run_python_test::(python_test_arg).await; + } } } diff --git a/crates/starknet_committer_and_os_cli/src/block_hash_cli/tests.rs b/crates/starknet_committer_and_os_cli/src/block_hash_cli/tests.rs new file mode 100644 index 0000000000..8147ce1469 --- /dev/null +++ b/crates/starknet_committer_and_os_cli/src/block_hash_cli/tests.rs @@ -0,0 +1,2 @@ +pub mod objects; +pub mod python_tests; diff --git a/crates/starknet_committer_and_os_cli/src/committer_cli/tests/utils/objects.rs b/crates/starknet_committer_and_os_cli/src/block_hash_cli/tests/objects.rs similarity index 100% rename from crates/starknet_committer_and_os_cli/src/committer_cli/tests/utils/objects.rs rename to crates/starknet_committer_and_os_cli/src/block_hash_cli/tests/objects.rs diff --git a/crates/starknet_committer_and_os_cli/src/block_hash_cli/tests/python_tests.rs b/crates/starknet_committer_and_os_cli/src/block_hash_cli/tests/python_tests.rs new file mode 100644 index 0000000000..c055b0ac1a --- /dev/null +++ b/crates/starknet_committer_and_os_cli/src/block_hash_cli/tests/python_tests.rs @@ -0,0 +1,83 @@ +use starknet_api::block_hash::block_hash_calculator::{ + TransactionHashingData, + TransactionOutputForHash, +}; +use starknet_api::state::ThinStateDiff; +use starknet_api::transaction::TransactionExecutionStatus; + +use crate::block_hash_cli::tests::objects::{ + get_thin_state_diff, + get_transaction_output_for_hash, + get_tx_data, +}; +use crate::shared_utils::types::{PythonTestError, PythonTestRunner}; + +pub type BlockHashPythonTestError = PythonTestError<()>; + +// Enum representing different Python tests. +pub enum BlockHashPythonTestRunner { + ParseTxOutput, + ParseStateDiff, + ParseTxData, +} + +/// Implements conversion from a string to the test runner. +impl TryFrom for BlockHashPythonTestRunner { + type Error = BlockHashPythonTestError; + + fn try_from(value: String) -> Result { + match value.as_str() { + "parse_tx_output_test" => Ok(Self::ParseTxOutput), + "parse_state_diff_test" => Ok(Self::ParseStateDiff), + "parse_tx_data_test" => Ok(Self::ParseTxData), + _ => Err(PythonTestError::UnknownTestName(value)), + } + } +} + +impl PythonTestRunner for BlockHashPythonTestRunner { + type SpecificError = (); + /// Runs the test with the given arguments. + async fn run(&self, input: Option<&str>) -> Result { + match self { + Self::ParseTxOutput => { + let tx_output: TransactionOutputForHash = + serde_json::from_str(Self::non_optional_input(input)?)?; + Ok(parse_tx_output_test(tx_output)) + } + Self::ParseStateDiff => { + let tx_output: ThinStateDiff = + serde_json::from_str(Self::non_optional_input(input)?)?; + Ok(parse_state_diff_test(tx_output)) + } + Self::ParseTxData => { + let tx_data: TransactionHashingData = + serde_json::from_str(Self::non_optional_input(input)?)?; + Ok(parse_tx_data_test(tx_data)) + } + } + } +} + +pub(crate) fn parse_tx_output_test(tx_execution_info: TransactionOutputForHash) -> String { + let expected_object = get_transaction_output_for_hash(&tx_execution_info.execution_status); + is_success_string(expected_object == tx_execution_info) +} + +pub(crate) fn parse_state_diff_test(state_diff: ThinStateDiff) -> String { + let expected_object = get_thin_state_diff(); + is_success_string(expected_object == state_diff) +} + +pub(crate) fn parse_tx_data_test(tx_data: TransactionHashingData) -> String { + let expected_object = get_tx_data(&TransactionExecutionStatus::Succeeded); + is_success_string(expected_object == tx_data) +} + +fn is_success_string(is_success: bool) -> String { + match is_success { + true => "Success", + false => "Failure", + } + .to_owned() +} diff --git a/crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs b/crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs index 9493f573aa..e932e924ea 100644 --- a/crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs +++ b/crates/starknet_committer_and_os_cli/src/committer_cli/tests/python_tests.rs @@ -3,12 +3,6 @@ use std::fmt::Debug; use ethnum::U256; use serde_json::json; -use starknet_api::block_hash::block_hash_calculator::{ - TransactionHashingData, - TransactionOutputForHash, -}; -use starknet_api::state::ThinStateDiff; -use starknet_api::transaction::TransactionExecutionStatus; use starknet_committer::block_committer::input::{ ContractAddress, StarknetStorageKey, @@ -44,11 +38,6 @@ use super::utils::parse_from_python::TreeFlowInput; use crate::committer_cli::filled_tree_output::filled_forest::SerializedForest; use crate::committer_cli::parse_input::cast::InputImpl; use crate::committer_cli::parse_input::read::parse_input; -use crate::committer_cli::tests::utils::objects::{ - get_thin_state_diff, - get_transaction_output_for_hash, - get_tx_data, -}; use crate::committer_cli::tests::utils::parse_from_python::parse_input_single_storage_tree_flow_test; use crate::committer_cli::tests::utils::random_structs::DummyRandomValue; use crate::shared_utils::types::{PythonTestError, PythonTestRunner}; @@ -68,9 +57,6 @@ pub enum CommitterPythonTestRunner { StorageNode, FilledForestOutput, TreeHeightComparison, - ParseTxOutput, - ParseStateDiff, - ParseTxData, SerializeForRustCommitterFlowTest, ComputeHashSingleTree, MaybePanic, @@ -107,9 +93,6 @@ impl TryFrom for CommitterPythonTestRunner { "storage_node_test" => Ok(Self::StorageNode), "filled_forest_output" => Ok(Self::FilledForestOutput), "compare_tree_height" => Ok(Self::TreeHeightComparison), - "parse_tx_output_test" => Ok(Self::ParseTxOutput), - "parse_state_diff_test" => Ok(Self::ParseStateDiff), - "parse_tx_data_test" => Ok(Self::ParseTxData), "serialize_to_rust_committer_flow_test" => Ok(Self::SerializeForRustCommitterFlowTest), "tree_test" => Ok(Self::ComputeHashSingleTree), "maybe_panic" => Ok(Self::MaybePanic), @@ -160,21 +143,6 @@ impl PythonTestRunner for CommitterPythonTestRunner { } Self::FilledForestOutput => filled_forest_output_test(), Self::TreeHeightComparison => Ok(get_actual_tree_height()), - Self::ParseTxOutput => { - let tx_output: TransactionOutputForHash = - serde_json::from_str(Self::non_optional_input(input)?)?; - Ok(parse_tx_output_test(tx_output)) - } - Self::ParseStateDiff => { - let tx_output: ThinStateDiff = - serde_json::from_str(Self::non_optional_input(input)?)?; - Ok(parse_state_diff_test(tx_output)) - } - Self::ParseTxData => { - let tx_data: TransactionHashingData = - serde_json::from_str(Self::non_optional_input(input)?)?; - Ok(parse_tx_data_test(tx_data)) - } Self::SerializeForRustCommitterFlowTest => { // TODO(Aner, 8/7/2024): refactor using structs for deserialization. let input: HashMap = @@ -251,29 +219,6 @@ pub(crate) fn example_test(test_args: HashMap) -> String { format!("Calling example test with args: x: {}, y: {}", x, y) } -pub(crate) fn parse_tx_output_test(tx_execution_info: TransactionOutputForHash) -> String { - let expected_object = get_transaction_output_for_hash(&tx_execution_info.execution_status); - is_success_string(expected_object == tx_execution_info) -} - -pub(crate) fn parse_state_diff_test(state_diff: ThinStateDiff) -> String { - let expected_object = get_thin_state_diff(); - is_success_string(expected_object == state_diff) -} - -pub(crate) fn parse_tx_data_test(tx_data: TransactionHashingData) -> String { - let expected_object = get_tx_data(&TransactionExecutionStatus::Succeeded); - is_success_string(expected_object == tx_data) -} - -fn is_success_string(is_success: bool) -> String { - match is_success { - true => "Success", - false => "Failure", - } - .to_owned() -} - /// Serializes a Felt into a string. pub(crate) fn felt_serialize_test(felt: u128) -> String { let bytes = Felt::from(felt).to_bytes_be().to_vec(); diff --git a/crates/starknet_committer_and_os_cli/src/committer_cli/tests/utils.rs b/crates/starknet_committer_and_os_cli/src/committer_cli/tests/utils.rs index 2b502e0ae2..dc1e5b73ed 100644 --- a/crates/starknet_committer_and_os_cli/src/committer_cli/tests/utils.rs +++ b/crates/starknet_committer_and_os_cli/src/committer_cli/tests/utils.rs @@ -1,3 +1,2 @@ -pub mod objects; pub mod parse_from_python; pub mod random_structs;