From 3bed7336147ac0f91d71ed33131658a3df322173 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Wed, 20 Sep 2023 15:50:28 +0200 Subject: [PATCH] compare internal calls --- rpc_state_reader/src/utils.rs | 42 +++++++++++++++++++++++++++++ rpc_state_reader/tests/sir_tests.rs | 31 ++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/rpc_state_reader/src/utils.rs b/rpc_state_reader/src/utils.rs index 1a5ecffb4..3cf1ca5ff 100644 --- a/rpc_state_reader/src/utils.rs +++ b/rpc_state_reader/src/utils.rs @@ -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::{ @@ -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 { @@ -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() + } +} diff --git a/rpc_state_reader/tests/sir_tests.rs b/rpc_state_reader/tests/sir_tests.rs index 2cf08f75d..25a3b15c7 100644 --- a/rpc_state_reader/tests/sir_tests.rs +++ b/rpc_state_reader/tests/sir_tests.rs @@ -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}, @@ -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); @@ -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, @@ -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 = rpc_internal_calls + .iter() + .map(rpc_call_info_to_call_info) + .collect(); + + let stripped_internal_calls: Vec = 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;