From f26af4bf4c17f2010759e9a4e995c53e2c032399 Mon Sep 17 00:00:00 2001 From: Yair Bakalchuk Date: Thu, 24 Aug 2023 12:57:45 +0300 Subject: [PATCH] chore(JSON-RPC,execution): consolidate repeating test code --- crates/papyrus_execution/src/objects.rs | 10 +-- crates/papyrus_rpc/src/v0_4_0/api/mod.rs | 2 +- .../papyrus_rpc/src/v0_4_0/execution_test.rs | 81 ++++--------------- 3 files changed, 23 insertions(+), 70 deletions(-) diff --git a/crates/papyrus_execution/src/objects.rs b/crates/papyrus_execution/src/objects.rs index a1852e0dcd..84e0e55c86 100644 --- a/crates/papyrus_execution/src/objects.rs +++ b/crates/papyrus_execution/src/objects.rs @@ -17,7 +17,7 @@ use starknet_api::transaction::{Calldata, EventContent, MessageToL1}; /// The execution trace of a transaction. #[allow(missing_docs)] -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] #[serde(untagged)] pub enum TransactionTrace { L1Handler(L1HandlerTransactionTrace), @@ -27,7 +27,7 @@ pub enum TransactionTrace { } /// The execution trace of an Invoke transaction. -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct InvokeTransactionTrace { /// The trace of the __validate__ call. pub validate_invocation: Option, @@ -72,7 +72,7 @@ impl From for InvokeTransactionTrace { } /// The execution trace of a Declare transaction. -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct DeclareTransactionTrace { /// The trace of the __validate__ call. pub validate_invocation: Option, @@ -94,7 +94,7 @@ impl From for DeclareTransactionTrace { } /// The execution trace of a DeployAccount transaction. -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct DeployAccountTransactionTrace { /// The trace of the __validate__ call. pub validate_invocation: Option, @@ -125,7 +125,7 @@ impl From for DeployAccountTransactionTrace { } /// The execution trace of an L1Handler transaction. -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] pub struct L1HandlerTransactionTrace { /// The trace of the funcion call. pub function_invocation: FunctionInvocation, diff --git a/crates/papyrus_rpc/src/v0_4_0/api/mod.rs b/crates/papyrus_rpc/src/v0_4_0/api/mod.rs index af6f887b2b..bf90f533aa 100644 --- a/crates/papyrus_rpc/src/v0_4_0/api/mod.rs +++ b/crates/papyrus_rpc/src/v0_4_0/api/mod.rs @@ -474,7 +474,7 @@ pub(crate) fn decompress_program( serde_json::from_reader(decompressed.as_slice()).map_err(internal_server_error) } -#[derive(Debug, Clone, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq)] pub struct TransactionTraceWithHash { pub transaction_hash: TransactionHash, pub trace_root: TransactionTrace, diff --git a/crates/papyrus_rpc/src/v0_4_0/execution_test.rs b/crates/papyrus_rpc/src/v0_4_0/execution_test.rs index 4f8ea5cc52..054861e490 100644 --- a/crates/papyrus_rpc/src/v0_4_0/execution_test.rs +++ b/crates/papyrus_rpc/src/v0_4_0/execution_test.rs @@ -369,68 +369,7 @@ async fn call_simulate_skip_fee_charge() { } #[tokio::test] -async fn call_trace_transaction() { - let (module, storage_writer) = - get_test_rpc_server_and_storage_writer::(); - - let mut writer = prepare_storage_for_execution(storage_writer); - - let tx_hash = TransactionHash(stark_felt!("0x1234")); - writer - .begin_rw_txn() - .unwrap() - .append_header( - BlockNumber(2), - &BlockHeader { - gas_price: *GAS_PRICE, - sequencer: *SEQUENCER_ADDRESS, - timestamp: *BLOCK_TIMESTAMP, - block_hash: BlockHash(stark_felt!("0x2")), - parent_hash: BlockHash(stark_felt!("0x1")), - ..Default::default() - }, - ) - .unwrap() - .append_body( - BlockNumber(2), - BlockBody { - transactions: vec![starknet_api::transaction::Transaction::Invoke( - starknet_api::transaction::InvokeTransaction::V1( - starknet_api::transaction::InvokeTransactionV1 { - max_fee: *MAX_FEE, - sender_address: *ACCOUNT_ADDRESS, - calldata: calldata![ - *DEPRECATED_CONTRACT_ADDRESS.0.key(), // Contract address. - selector_from_name("return_result").0, // EP selector. - stark_felt!(1_u8), // Calldata length. - stark_felt!(2_u8) // Calldata: num. - ], - ..Default::default() - }, - ), - )], - transaction_outputs: vec![starknet_api::transaction::TransactionOutput::Invoke( - starknet_api::transaction::InvokeTransactionOutput::default(), - )], - transaction_hashes: vec![tx_hash], - }, - ) - .unwrap() - .append_state_diff(BlockNumber(2), StateDiff::default(), IndexMap::new()) - .unwrap() - .commit() - .unwrap(); - - let res = module - .call::<_, TransactionTrace>("starknet_V0_4_traceTransaction", [tx_hash]) - .await - .unwrap(); - - assert_matches!(res, TransactionTrace::Invoke(_)); -} - -#[tokio::test] -async fn call_trace_block_transactions() { +async fn trace_block_transactions() { let (module, storage_writer) = get_test_rpc_server_and_storage_writer::(); @@ -502,6 +441,20 @@ async fn call_trace_block_transactions() { .commit() .unwrap(); + let tx_1_trace = module + .call::<_, TransactionTrace>("starknet_V0_4_traceTransaction", [tx_hash1]) + .await + .unwrap(); + + assert_matches!(tx_1_trace, TransactionTrace::Invoke(_)); + + let tx_2_trace = module + .call::<_, TransactionTrace>("starknet_V0_4_traceTransaction", [tx_hash2]) + .await + .unwrap(); + + assert_matches!(tx_2_trace, TransactionTrace::Invoke(_)); + let res = module .call::<_, Vec>( "starknet_V0_4_traceBlockTransactions", @@ -511,9 +464,9 @@ async fn call_trace_block_transactions() { .unwrap(); assert_eq!(res.len(), 2); - assert_matches!(res[0].trace_root, TransactionTrace::Invoke(_)); + assert_eq!(res[0].trace_root, tx_1_trace); assert_eq!(res[0].transaction_hash, tx_hash1); - assert_matches!(res[1].trace_root, TransactionTrace::Invoke(_)); + assert_eq!(res[1].trace_root, tx_2_trace); assert_eq!(res[1].transaction_hash, tx_hash2); }