Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
chore(JSON-RPC,execution): consolidate repeating test code
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware committed Sep 3, 2023
1 parent 93be831 commit f26af4b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 70 deletions.
10 changes: 5 additions & 5 deletions crates/papyrus_execution/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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<FunctionInvocation>,
Expand Down Expand Up @@ -72,7 +72,7 @@ impl From<TransactionExecutionInfo> 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<FunctionInvocation>,
Expand All @@ -94,7 +94,7 @@ impl From<TransactionExecutionInfo> 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<FunctionInvocation>,
Expand Down Expand Up @@ -125,7 +125,7 @@ impl From<TransactionExecutionInfo> 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,
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_rpc/src/v0_4_0/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
81 changes: 17 additions & 64 deletions crates/papyrus_rpc/src/v0_4_0/execution_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<JsonRpcServerV0_4Impl>();

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::<JsonRpcServerV0_4Impl>();

Expand Down Expand Up @@ -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<TransactionTraceWithHash>>(
"starknet_V0_4_traceBlockTransactions",
Expand All @@ -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);
}

Expand Down

0 comments on commit f26af4b

Please sign in to comment.