Skip to content

Commit 0337ce4

Browse files
Fix top call resources (#3426)
Partially solves #3413 (to be verified by someone from snforge team) ## Introduced changes - stop adding resources used by nested calls to top call recursively - they already contain cumulative resources ## Checklist - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - I believe there is a task for that - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md` --------- Co-authored-by: Fiiranek <[email protected]> Co-authored-by: Franciszek Job <[email protected]>
1 parent 94a09af commit 0337ce4

File tree

5 files changed

+351
-15
lines changed

5 files changed

+351
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
#### Fixed
1717

1818
- bug that caused `--trace-verbosity` to panic in fork tests
19+
- fixed a bug in fork tests where resources used in nested calls were counted multiple times, leading to overestimated gas and resource usage
1920

2021
#### Removed
2122

crates/cheatnet/src/runtime_extensions/forge_runtime_extension/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ fn add_sierra_gas_resources(top_call: &Rc<RefCell<CallTrace>>) -> u64 {
705705
let mut gas_consumed = top_call.borrow().gas_consumed;
706706
for nested_call in &top_call.borrow().nested_calls {
707707
if let CallTraceNode::EntryPointCall(nested_call) = nested_call {
708-
gas_consumed += &add_sierra_gas_resources(nested_call);
708+
gas_consumed += &nested_call.borrow().gas_consumed;
709709
}
710710
}
711711
gas_consumed
@@ -717,7 +717,7 @@ fn add_execution_resources(top_call: Rc<RefCell<CallTrace>>) -> ExecutionResourc
717717
for nested_call in &top_call.borrow().nested_calls {
718718
match nested_call {
719719
CallTraceNode::EntryPointCall(nested_call) => {
720-
execution_resources += &add_execution_resources(nested_call.clone());
720+
execution_resources += &nested_call.borrow().used_execution_resources;
721721
}
722722
CallTraceNode::DeployWithoutConstructor => {}
723723
}
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use starknet::ContractAddress;
1+
use starknet::{ContractAddress, SyscallResult};
22

33
#[starknet::interface]
44
trait IGasChecker<TContractState> {
@@ -9,25 +9,37 @@ trait IGasChecker<TContractState> {
99
#[starknet::interface]
1010
trait IGasCheckerProxy<TContractState> {
1111
fn send_l1_message_from_gas_checker(self: @TContractState, address: ContractAddress);
12+
fn call_other_contract(
13+
self: @TContractState,
14+
contract_address: ContractAddress,
15+
entry_point_selector: felt252,
16+
calldata: Array::<felt252>,
17+
) -> SyscallResult<Span<felt252>>;
1218
}
1319

1420
#[starknet::contract]
1521
mod GasCheckerProxy {
16-
use starknet::ContractAddress;
17-
use super::IGasCheckerDispatcherTrait;
18-
use super::IGasCheckerDispatcher;
22+
use starknet::{ContractAddress, SyscallResult};
23+
use starknet::syscalls::call_contract_syscall;
24+
use super::{IGasCheckerDispatcher, IGasCheckerDispatcherTrait};
1925

2026
#[storage]
2127
struct Storage {}
2228

23-
#[external(v0)]
29+
#[abi(embed_v0)]
2430
impl IGasCheckerProxy of super::IGasCheckerProxy<ContractState> {
25-
fn send_l1_message_from_gas_checker(
26-
self: @ContractState,
27-
address: ContractAddress)
28-
{
31+
fn send_l1_message_from_gas_checker(self: @ContractState, address: ContractAddress) {
2932
let gas_checker = IGasCheckerDispatcher { contract_address: address };
3033
gas_checker.send_l1_message()
3134
}
35+
36+
fn call_other_contract(
37+
self: @ContractState,
38+
contract_address: ContractAddress,
39+
entry_point_selector: felt252,
40+
calldata: Array::<felt252>,
41+
) -> SyscallResult<Span<felt252>> {
42+
call_contract_syscall(contract_address, entry_point_selector, calldata.span())
43+
}
3244
}
3345
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#[starknet::interface]
2+
pub trait IHelloStarknet<TContractState> {
3+
fn example_function(ref self: TContractState);
4+
}
5+
6+
#[starknet::contract]
7+
pub mod HelloStarknet {
8+
use starknet::SyscallResultTrait;
9+
use core::sha256::compute_sha256_u32_array;
10+
#[storage]
11+
struct Storage {}
12+
13+
#[abi(embed_v0)]
14+
impl HelloStarknetImpl of super::IHelloStarknet<ContractState> {
15+
fn example_function(ref self: ContractState) {
16+
core::pedersen::pedersen(1, 2);
17+
core::keccak::keccak_u256s_le_inputs(array![1].span());
18+
let _hash = compute_sha256_u32_array(array![0x68656c6c], 0x6f, 1);
19+
starknet::syscalls::get_block_hash_syscall(1).unwrap_syscall();
20+
starknet::syscalls::emit_event_syscall(array![1].span(), array![2].span())
21+
.unwrap_syscall();
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)