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

compare internal calls in tests using rpc state reader #1044

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
42 changes: 42 additions & 0 deletions rpc_state_reader/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use cairo_lang_starknet::contract_class::ContractEntryPoints;
use cairo_lang_utils::bigint::BigUintAsHex;
use cairo_vm::{felt::Felt252, vm::runners::cairo_runner::ExecutionResources};
use serde::Deserialize;
use starknet::core::types::{LegacyContractEntryPoint, LegacyEntryPointsByType};
use starknet_api::{
Expand All @@ -13,6 +14,9 @@ use starknet_api::{
hash::{StarkFelt, StarkHash},
transaction::{DeclareTransaction, InvokeTransaction, Transaction},
};
use starknet_in_rust::execution::CallInfo;

use crate::rpc_state::RpcCallInfo;

#[derive(Debug, Deserialize)]
pub struct MiddleSierraContractClass {
Expand Down Expand Up @@ -105,3 +109,41 @@ pub fn deserialize_transaction_json(
))),
}
}

/// Converts a StarkFelt to a Felt252
pub fn starkfelt_to_felt252(data: &StarkFelt) -> Felt252 {
Felt252::from_bytes_be(data.bytes())
}

pub fn rpc_call_info_to_call_info(rpc_call_info: &RpcCallInfo) -> CallInfo {
CallInfo {
calldata: rpc_call_info
.calldata
.as_ref()
.unwrap_or(&vec![])
.iter()
.map(starkfelt_to_felt252)
.collect(),
execution_resources: ExecutionResources {
n_steps: rpc_call_info.execution_resources.n_steps,
n_memory_holes: rpc_call_info.execution_resources.n_memory_holes,
builtin_instance_counter: rpc_call_info
.execution_resources
.builtin_instance_counter
.clone(),
},
retdata: rpc_call_info
.retdata
.as_ref()
.unwrap_or(&vec![])
.iter()
.map(starkfelt_to_felt252)
.collect(),
internal_calls: rpc_call_info
.internal_calls
.iter()
.map(rpc_call_info_to_call_info)
.collect(),
..Default::default()
}
}
31 changes: 30 additions & 1 deletion rpc_state_reader/tests/sir_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::sync::Arc;

use cairo_vm::felt::{felt_str, Felt252};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use pretty_assertions_sorted::{assert_eq, assert_eq_sorted};
use rpc_state_reader::utils::rpc_call_info_to_call_info;
use starknet_api::{
block::BlockNumber,
core::{ClassHash as SNClassHash, ContractAddress, PatriciaKey},
Expand Down Expand Up @@ -34,7 +36,7 @@ use starknet_in_rust::{

use test_case::test_case;

use rpc_state_reader::rpc_state::*;
use rpc_state_reader::{rpc_state::*, utils::starkfelt_to_felt252};

#[derive(Debug)]
pub struct RpcStateReader(RpcState);
Expand Down Expand Up @@ -296,6 +298,21 @@ fn test_get_gas_price() {
assert_eq!(price, 22804578690);
}

/// Removes data from the call info that the RpcCallInfo struct doesn't have, so we can compare them properly
fn strip_call_info_to_compare(call_info: &CallInfo) -> CallInfo {
CallInfo {
calldata: call_info.calldata.clone(),
execution_resources: call_info.execution_resources.clone(),
retdata: call_info.retdata.clone(),
internal_calls: call_info
.internal_calls
.iter()
.map(strip_call_info_to_compare)
.collect(),
..Default::default()
}
}

#[test_case(
"0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5",
90006,
Expand Down Expand Up @@ -435,6 +452,18 @@ fn starknet_in_rust_test_case_tx(hash: &str, block_number: u64, chain: RpcChain)
"internal calls length mismatch"
);

let rpc_internal_calls = &trace.function_invocation.as_ref().unwrap().internal_calls;
let rpc_internal_calls: Vec<CallInfo> = rpc_internal_calls
.iter()
.map(rpc_call_info_to_call_info)
.collect();

let stripped_internal_calls: Vec<CallInfo> = internal_calls
.iter()
.map(strip_call_info_to_compare)
.collect();
assert_eq_sorted!(stripped_internal_calls, rpc_internal_calls);

// check actual fee calculation
if receipt.actual_fee != actual_fee {
let diff = 100 * receipt.actual_fee.abs_diff(actual_fee) / receipt.actual_fee;
Expand Down
Loading