Skip to content

Commit

Permalink
chore(starknet_committer_and_os_cli): separate block-hash tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amosStarkware committed Feb 2, 2025
1 parent 8a4eee2 commit 3ef0653
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 57 deletions.
1 change: 1 addition & 0 deletions crates/starknet_committer_and_os_cli/src/block_hash_cli.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod run_block_hash_cli;
pub mod tests;
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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::<BlockHashPythonTestRunner>(python_test_arg).await;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod objects;
pub mod python_tests;
Original file line number Diff line number Diff line change
@@ -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<String> for BlockHashPythonTestRunner {
type Error = BlockHashPythonTestError;

fn try_from(value: String) -> Result<Self, Self::Error> {
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<String, BlockHashPythonTestError> {
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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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};
Expand All @@ -68,9 +57,6 @@ pub enum CommitterPythonTestRunner {
StorageNode,
FilledForestOutput,
TreeHeightComparison,
ParseTxOutput,
ParseStateDiff,
ParseTxData,
SerializeForRustCommitterFlowTest,
ComputeHashSingleTree,
MaybePanic,
Expand Down Expand Up @@ -107,9 +93,6 @@ impl TryFrom<String> 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),
Expand Down Expand Up @@ -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<String, String> =
Expand Down Expand Up @@ -251,29 +219,6 @@ pub(crate) fn example_test(test_args: HashMap<String, String>) -> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod objects;
pub mod parse_from_python;
pub mod random_structs;

0 comments on commit 3ef0653

Please sign in to comment.