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

Commit a4c3b7c

Browse files
committed
compare internal calls
1 parent 500fec7 commit a4c3b7c

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

rpc_state_reader/src/utils.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55

66
use cairo_lang_starknet::contract_class::ContractEntryPoints;
77
use cairo_lang_utils::bigint::BigUintAsHex;
8+
use cairo_vm::{felt::Felt252, vm::runners::cairo_runner::ExecutionResources};
89
use serde::Deserialize;
910
use starknet::core::types::{LegacyContractEntryPoint, LegacyEntryPointsByType};
1011
use starknet_api::{
@@ -13,6 +14,9 @@ use starknet_api::{
1314
hash::{StarkFelt, StarkHash},
1415
transaction::{InvokeTransaction, Transaction},
1516
};
17+
use starknet_in_rust::execution::CallInfo;
18+
19+
use crate::rpc_state::RpcCallInfo;
1620

1721
#[derive(Debug, Deserialize)]
1822
pub struct MiddleSierraContractClass {
@@ -87,3 +91,41 @@ pub fn deserialize_transaction_json(
8791
))),
8892
}
8993
}
94+
95+
/// Converts a StarkFelt to a Felt252
96+
pub fn starkfelt_to_felt252(data: &StarkFelt) -> Felt252 {
97+
Felt252::from_bytes_be(data.bytes())
98+
}
99+
100+
pub fn rpc_call_info_to_call_info(rpc_call_info: &RpcCallInfo) -> CallInfo {
101+
CallInfo {
102+
calldata: rpc_call_info
103+
.calldata
104+
.as_ref()
105+
.unwrap_or(&vec![])
106+
.iter()
107+
.map(starkfelt_to_felt252)
108+
.collect(),
109+
execution_resources: ExecutionResources {
110+
n_steps: rpc_call_info.execution_resources.n_steps,
111+
n_memory_holes: rpc_call_info.execution_resources.n_memory_holes,
112+
builtin_instance_counter: rpc_call_info
113+
.execution_resources
114+
.builtin_instance_counter
115+
.clone(),
116+
},
117+
retdata: rpc_call_info
118+
.retdata
119+
.as_ref()
120+
.unwrap_or(&vec![])
121+
.iter()
122+
.map(starkfelt_to_felt252)
123+
.collect(),
124+
internal_calls: rpc_call_info
125+
.internal_calls
126+
.iter()
127+
.map(rpc_call_info_to_call_info)
128+
.collect(),
129+
..Default::default()
130+
}
131+
}

rpc_state_reader/tests/sir_tests.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::sync::Arc;
22

33
use cairo_vm::felt::{felt_str, Felt252};
4+
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
45
use pretty_assertions_sorted::{assert_eq, assert_eq_sorted};
6+
use rpc_state_reader::utils::rpc_call_info_to_call_info;
57
use starknet_api::{
68
block::BlockNumber,
79
core::{ClassHash as SNClassHash, ContractAddress, PatriciaKey},
@@ -34,7 +36,7 @@ use starknet_in_rust::{
3436

3537
use test_case::test_case;
3638

37-
use rpc_state_reader::rpc_state::*;
39+
use rpc_state_reader::{rpc_state::*, utils::starkfelt_to_felt252};
3840

3941
pub struct RpcStateReader(RpcState);
4042

@@ -195,6 +197,21 @@ fn test_get_gas_price() {
195197
assert_eq!(price, 22804578690);
196198
}
197199

200+
/// Removes data from the call info that the RpcCallInfo struct doesn't have, so we can compare them properly
201+
fn strip_call_info_to_compare(call_info: &CallInfo) -> CallInfo {
202+
CallInfo {
203+
calldata: call_info.calldata.clone(),
204+
execution_resources: call_info.execution_resources.clone(),
205+
retdata: call_info.retdata.clone(),
206+
internal_calls: call_info
207+
.internal_calls
208+
.iter()
209+
.map(strip_call_info_to_compare)
210+
.collect(),
211+
..Default::default()
212+
}
213+
}
214+
198215
#[test_case(
199216
"0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5",
200217
90006,
@@ -283,6 +300,18 @@ fn starknet_in_rust_test_case_tx(hash: &str, block_number: u64, chain: RpcChain)
283300
"internal calls length mismatch"
284301
);
285302

303+
let rpc_internal_calls = &trace.function_invocation.as_ref().unwrap().internal_calls;
304+
let rpc_internal_calls: Vec<CallInfo> = rpc_internal_calls
305+
.iter()
306+
.map(rpc_call_info_to_call_info)
307+
.collect();
308+
309+
let stripped_internal_calls: Vec<CallInfo> = internal_calls
310+
.iter()
311+
.map(strip_call_info_to_compare)
312+
.collect();
313+
assert_eq_sorted!(stripped_internal_calls, rpc_internal_calls);
314+
286315
// check actual fee calculation
287316
if receipt.actual_fee != actual_fee {
288317
let diff = 100 * receipt.actual_fee.abs_diff(actual_fee) / receipt.actual_fee;

0 commit comments

Comments
 (0)