diff --git a/crates/blockifier/resources/versioned_constants_0_13_0.json b/crates/blockifier/resources/versioned_constants_0_13_0.json index 5586db71ea..766e53273f 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_0.json +++ b/crates/blockifier/resources/versioned_constants_0_13_0.json @@ -114,13 +114,11 @@ "syscall_gas_costs": { "call_contract": { "syscall_base_gas_cost": 1, - "step_gas_cost": 510, - "entry_point_initial_budget": 1 + "step_gas_cost": 510 }, "deploy": { "syscall_base_gas_cost": 1, - "step_gas_cost": 700, - "entry_point_initial_budget": 1 + "step_gas_cost": 700 }, "get_block_hash": { "syscall_base_gas_cost": 1, @@ -132,8 +130,7 @@ }, "library_call": { "syscall_base_gas_cost": 1, - "step_gas_cost": 510, - "entry_point_initial_budget": 1 + "step_gas_cost": 510 }, "replace_class": { "syscall_base_gas_cost": 1, diff --git a/crates/blockifier/resources/versioned_constants_0_13_1.json b/crates/blockifier/resources/versioned_constants_0_13_1.json index 889e611664..d9e563e858 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_1.json +++ b/crates/blockifier/resources/versioned_constants_0_13_1.json @@ -118,13 +118,11 @@ "syscall_gas_costs": { "call_contract": { "syscall_base_gas_cost": 1, - "step_gas_cost": 510, - "entry_point_initial_budget": 1 + "step_gas_cost": 510 }, "deploy": { "syscall_base_gas_cost": 1, - "step_gas_cost": 700, - "entry_point_initial_budget": 1 + "step_gas_cost": 700 }, "get_block_hash": { "syscall_base_gas_cost": 1, @@ -136,8 +134,7 @@ }, "library_call": { "syscall_base_gas_cost": 1, - "step_gas_cost": 510, - "entry_point_initial_budget": 1 + "step_gas_cost": 510 }, "replace_class": { "syscall_base_gas_cost": 1, diff --git a/crates/blockifier/resources/versioned_constants_0_13_1_1.json b/crates/blockifier/resources/versioned_constants_0_13_1_1.json index d1f7f30ead..f0e7eda10e 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_1_1.json +++ b/crates/blockifier/resources/versioned_constants_0_13_1_1.json @@ -118,13 +118,11 @@ "syscall_gas_costs": { "call_contract": { "syscall_base_gas_cost": 1, - "step_gas_cost": 510, - "entry_point_initial_budget": 1 + "step_gas_cost": 510 }, "deploy": { "syscall_base_gas_cost": 1, - "step_gas_cost": 700, - "entry_point_initial_budget": 1 + "step_gas_cost": 700 }, "get_block_hash": { "syscall_base_gas_cost": 1, @@ -136,8 +134,7 @@ }, "library_call": { "syscall_base_gas_cost": 1, - "step_gas_cost": 510, - "entry_point_initial_budget": 1 + "step_gas_cost": 510 }, "replace_class": { "syscall_base_gas_cost": 1, diff --git a/crates/blockifier/resources/versioned_constants_0_13_2.json b/crates/blockifier/resources/versioned_constants_0_13_2.json index 686cc943eb..7d02e78b39 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_2.json +++ b/crates/blockifier/resources/versioned_constants_0_13_2.json @@ -117,12 +117,10 @@ "validated": "VALID", "syscall_gas_costs": { "call_contract": { - "entry_point_initial_budget": 1, "step_gas_cost": 510, "syscall_base_gas_cost": 1 }, "deploy": { - "entry_point_initial_budget": 1, "step_gas_cost": 700, "syscall_base_gas_cost": 1 }, @@ -143,7 +141,6 @@ }, "keccak_round_cost": 180000, "library_call": { - "entry_point_initial_budget": 1, "step_gas_cost": 510, "syscall_base_gas_cost": 1 }, diff --git a/crates/blockifier/resources/versioned_constants_0_13_2_1.json b/crates/blockifier/resources/versioned_constants_0_13_2_1.json index c5b4314286..22f3d81c4c 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_2_1.json +++ b/crates/blockifier/resources/versioned_constants_0_13_2_1.json @@ -117,12 +117,10 @@ "validated": "VALID", "syscall_gas_costs": { "call_contract": { - "entry_point_initial_budget": 1, "step_gas_cost": 510, "syscall_base_gas_cost": 1 }, "deploy": { - "entry_point_initial_budget": 1, "step_gas_cost": 700, "syscall_base_gas_cost": 1 }, @@ -143,7 +141,6 @@ }, "keccak_round_cost": 180000, "library_call": { - "entry_point_initial_budget": 1, "step_gas_cost": 510, "syscall_base_gas_cost": 1 }, diff --git a/crates/blockifier/resources/versioned_constants_0_13_3.json b/crates/blockifier/resources/versioned_constants_0_13_3.json index c5b4314286..22f3d81c4c 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_3.json +++ b/crates/blockifier/resources/versioned_constants_0_13_3.json @@ -117,12 +117,10 @@ "validated": "VALID", "syscall_gas_costs": { "call_contract": { - "entry_point_initial_budget": 1, "step_gas_cost": 510, "syscall_base_gas_cost": 1 }, "deploy": { - "entry_point_initial_budget": 1, "step_gas_cost": 700, "syscall_base_gas_cost": 1 }, @@ -143,7 +141,6 @@ }, "keccak_round_cost": 180000, "library_call": { - "entry_point_initial_budget": 1, "step_gas_cost": 510, "syscall_base_gas_cost": 1 }, diff --git a/crates/blockifier/resources/versioned_constants_0_13_4.json b/crates/blockifier/resources/versioned_constants_0_13_4.json index adb97a953f..43beef11b2 100644 --- a/crates/blockifier/resources/versioned_constants_0_13_4.json +++ b/crates/blockifier/resources/versioned_constants_0_13_4.json @@ -119,12 +119,10 @@ "validated": "VALID", "syscall_gas_costs": { "call_contract": { - "entry_point_initial_budget": 1, "step_gas_cost": 860, "range_check": 15 }, "deploy": { - "entry_point_initial_budget": 1, "step_gas_cost": 1128, "range_check": 18, "pedersen": 7 @@ -146,7 +144,6 @@ }, "keccak_round_cost": 180000, "library_call": { - "entry_point_initial_budget": 1, "step_gas_cost": 836, "range_check": 15 }, diff --git a/crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscalls_test.rs b/crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscalls_test.rs index fc244c6719..7f1d08ee09 100644 --- a/crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscalls_test.rs +++ b/crates/blockifier/src/execution/deprecated_syscalls/deprecated_syscalls_test.rs @@ -113,12 +113,15 @@ fn test_nested_library_call() { felt!(key), // Calldata: address. felt!(value) // Calldata: value. ]; + let vesrioned_const = VersionedConstants::create_for_testing(); + let initial_gas = vesrioned_const.os_constants.gas_costs.base.default_initial_gas_cost; // Create expected call info tree. let main_entry_point = CallEntryPoint { entry_point_selector: selector_from_name("test_nested_library_call"), calldata: main_entry_point_calldata, class_hash: Some(test_contract.get_class_hash()), + initial_gas, ..trivial_external_entry_point_new(test_contract) }; let nested_storage_entry_point = CallEntryPoint { @@ -127,6 +130,7 @@ fn test_nested_library_call() { class_hash: Some(test_contract.get_class_hash()), code_address: None, call_type: CallType::Delegate, + initial_gas, ..trivial_external_entry_point_new(test_contract) }; let library_entry_point = CallEntryPoint { @@ -141,6 +145,7 @@ fn test_nested_library_call() { class_hash: Some(test_contract.get_class_hash()), code_address: None, call_type: CallType::Delegate, + initial_gas, ..trivial_external_entry_point_new(test_contract) }; let storage_entry_point = CallEntryPoint { @@ -235,6 +240,8 @@ fn test_call_contract() { }; let call_info = entry_point_call.execute_directly(&mut state).unwrap(); + let vesrioned_const = VersionedConstants::create_for_testing(); + let initial_gas = vesrioned_const.os_constants.gas_costs.base.default_initial_gas_cost; let expected_execution = CallExecution { retdata: retdata![value], ..Default::default() }; let expected_inner_call_info = CallInfo { call: CallEntryPoint { @@ -242,6 +249,7 @@ fn test_call_contract() { entry_point_selector: inner_entry_point_selector, calldata: inner_calldata, caller_address: test_address, + initial_gas, ..trivial_external_entry_point }, execution: expected_execution.clone(), @@ -260,6 +268,7 @@ fn test_call_contract() { class_hash: Some(test_contract.get_class_hash()), entry_point_selector: outer_entry_point_selector, calldata, + initial_gas, ..trivial_external_entry_point }, execution: expected_execution, diff --git a/crates/blockifier/src/execution/entry_point_execution.rs b/crates/blockifier/src/execution/entry_point_execution.rs index 0297de89ee..b5a336d239 100644 --- a/crates/blockifier/src/execution/entry_point_execution.rs +++ b/crates/blockifier/src/execution/entry_point_execution.rs @@ -27,7 +27,7 @@ use crate::execution::execution_utils::{ ReadOnlySegments, SEGMENT_ARENA_BUILTIN_SIZE, }; -use crate::execution::syscalls::hint_processor::SyscallHintProcessor; +use crate::execution::syscalls::hint_processor::{SyscallHintProcessor, OUT_OF_GAS_ERROR}; use crate::state::state_api::State; use crate::versioned_constants::GasCosts; @@ -61,6 +61,8 @@ pub fn execute_entry_point_call( ) -> EntryPointExecutionResult { let tracked_resource = *context.tracked_resource_stack.last().expect("Unexpected empty tracked resource."); + let enable_reverts = context.versioned_constants().enable_reverts; + let entry_point_initial_budget = context.gas_costs().base.entry_point_initial_budget; let VmExecutionContext { mut runner, mut syscall_handler, @@ -69,13 +71,31 @@ pub fn execute_entry_point_call( program_extra_data_length, } = initialize_execution_context(call, &compiled_class, state, context)?; - let args = prepare_call_arguments( + let args = match prepare_call_arguments( &syscall_handler.base.call, &mut runner, initial_syscall_ptr, &mut syscall_handler.read_only_segments, &entry_point, - )?; + entry_point_initial_budget, + ) { + Ok(args) => args, + Err(PreExecutionError::OutOfGas) if enable_reverts => { + return Ok(CallInfo { + call: syscall_handler.base.call, + execution: CallExecution { + retdata: Retdata(vec![Felt::from_hex(OUT_OF_GAS_ERROR).unwrap()]), + failed: true, + gas_consumed: 0, + ..CallExecution::default() + }, + tracked_resource, + ..CallInfo::default() + }); + } + Err(err) => return Err(err.into()), + }; + let n_total_args = args.len(); // Execute. @@ -180,6 +200,7 @@ pub fn prepare_call_arguments( initial_syscall_ptr: Relocatable, read_only_segments: &mut ReadOnlySegments, entrypoint: &EntryPointV1, + entry_point_initial_budget: u64, ) -> Result { let mut args: Args = vec![]; @@ -208,8 +229,14 @@ pub fn prepare_call_arguments( } return Err(PreExecutionError::InvalidBuiltin(*builtin_name)); } + // Include the initial entry point budget in the initial gas calculation. + if call.initial_gas < entry_point_initial_budget { + return Err(PreExecutionError::OutOfGas); + } + let initial_gas = i128::from(call.initial_gas) - i128::from(entry_point_initial_budget); + assert!(initial_gas >= 0, "initial_gas must be non-negative"); // Push gas counter. - args.push(CairoArg::Single(MaybeRelocatable::from(Felt::from(call.initial_gas)))); + args.push(CairoArg::Single(MaybeRelocatable::from(Felt::from(initial_gas)))); // Push syscall ptr. args.push(CairoArg::Single(MaybeRelocatable::from(initial_syscall_ptr))); diff --git a/crates/blockifier/src/execution/errors.rs b/crates/blockifier/src/execution/errors.rs index 6dc1b6f9bd..a6f9f752b8 100644 --- a/crates/blockifier/src/execution/errors.rs +++ b/crates/blockifier/src/execution/errors.rs @@ -50,6 +50,8 @@ pub enum PreExecutionError { UninitializedStorageAddress(ContractAddress), #[error("Called builtins: {0:?} are unsupported in a Cairo0 contract")] UnsupportedCairo0Builtin(HashSet), + #[error("Out of gas.")] + OutOfGas, } impl From for PreExecutionError { diff --git a/crates/blockifier/src/execution/native/entry_point_execution.rs b/crates/blockifier/src/execution/native/entry_point_execution.rs index ee713aa263..c572bd5dcd 100644 --- a/crates/blockifier/src/execution/native/entry_point_execution.rs +++ b/crates/blockifier/src/execution/native/entry_point_execution.rs @@ -2,6 +2,7 @@ use cairo_native::execution_result::ContractExecutionResult; use cairo_native::utils::BuiltinCosts; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use stacker; +use starknet_types_core::felt::Felt; use crate::execution::call_info::{CallExecution, CallInfo, ChargedResources, Retdata}; use crate::execution::contract_class::TrackedResource; @@ -14,6 +15,7 @@ use crate::execution::entry_point_execution::gas_consumed_without_inner_calls; use crate::execution::errors::{EntryPointExecutionError, PostExecutionError}; use crate::execution::native::contract_class::NativeCompiledClassV1; use crate::execution::native::syscall_handler::NativeSyscallHandler; +use crate::execution::syscalls::hint_processor::OUT_OF_GAS_ERROR; use crate::state::state_api::State; // todo(rodrigo): add an `entry point not found` test for Native @@ -23,6 +25,8 @@ pub fn execute_entry_point_call( state: &mut dyn State, context: &mut EntryPointExecutionContext, ) -> EntryPointExecutionResult { + let orig_call = call.clone(); + let entry_point = compiled_class.get_entry_point(&call)?; let mut syscall_handler: NativeSyscallHandler<'_> = @@ -56,13 +60,34 @@ pub fn execute_entry_point_call( // TODO(Aviv/Yonatan): add these numbers to overridable VC. let stack_size_red_zone = 160 * 1024 * 1024; let target_stack_size = stack_size_red_zone + 10 * 1024 * 1024; + // Include the initial entry point budget in the initial gas calculation. + let entry_point_initial_budget = + syscall_handler.base.context.gas_costs().base.entry_point_initial_budget; + if syscall_handler.base.call.initial_gas < entry_point_initial_budget { + let current_tracked_resource = compiled_class + .casm() + .tracked_resource(&context.versioned_constants().min_sierra_version_for_sierra_gas); + + return Ok(CallInfo { + call: orig_call, + execution: CallExecution { + retdata: Retdata(vec![Felt::from_hex(OUT_OF_GAS_ERROR).unwrap()]), + failed: true, + gas_consumed: 0, + ..CallExecution::default() + }, + tracked_resource: current_tracked_resource, + ..CallInfo::default() + }); + } + let initial_gas = syscall_handler.base.call.initial_gas - entry_point_initial_budget; // Use `maybe_grow` and not `grow` for performance, since in happy flows, only the main call // should trigger the growth. let execution_result = stacker::maybe_grow(stack_size_red_zone, target_stack_size, || { compiled_class.executor.run( entry_point.selector.0, &syscall_handler.base.call.calldata.0.clone(), - syscall_handler.base.call.initial_gas, + initial_gas, Some(builtin_costs), &mut syscall_handler, ) diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/constants.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/constants.rs index ff3ebd021d..dee7caf83d 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/constants.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/constants.rs @@ -1,4 +1,4 @@ -pub const REQUIRED_GAS_CALL_CONTRACT_TEST: u64 = 120370; -pub const REQUIRED_GAS_STORAGE_READ_WRITE_TEST: u64 = 16990; -pub const REQUIRED_GAS_GET_CLASS_HASH_AT_TEST: u64 = 7830; -pub const REQUIRED_GAS_LIBRARY_CALL_TEST: u64 = 117970; +pub const REQUIRED_GAS_CALL_CONTRACT_TEST: u64 = 130370; +pub const REQUIRED_GAS_STORAGE_READ_WRITE_TEST: u64 = 26990; +pub const REQUIRED_GAS_GET_CLASS_HASH_AT_TEST: u64 = 17830; +pub const REQUIRED_GAS_LIBRARY_CALL_TEST: u64 = 127970; diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/deploy.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/deploy.rs index c2428bfeaf..2b9b659879 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/deploy.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/deploy.rs @@ -124,7 +124,7 @@ fn with_constructor(runnable_version: RunnableCairo1) { let deploy_call = &entry_point_call.execute_directly(&mut state).unwrap(); assert_eq!( deploy_call.execution, - CallExecution { retdata: retdata![], gas_consumed: 164550, ..CallExecution::default() } + CallExecution { retdata: retdata![], gas_consumed: 174550, ..CallExecution::default() } ); let constructor_call = &deploy_call.inner_calls[0]; @@ -136,7 +136,7 @@ fn with_constructor(runnable_version: RunnableCairo1) { // The test contract constructor returns its first argument. retdata: retdata![constructor_calldata[0]], // This reflects the gas cost of storage write syscall. - gas_consumed: 4610, + gas_consumed: 14610, ..CallExecution::default() } ); diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/emit_event.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/emit_event.rs index f45e9aa257..d8fecfd07f 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/emit_event.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/emit_event.rs @@ -41,7 +41,7 @@ fn positive_flow(runnable_version: RunnableCairo1) { call_info.execution, CallExecution { events: vec![OrderedEvent { order: 0, event }], - gas_consumed: 47330, + gas_consumed: 57330, ..Default::default() } ); diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs index e805e7eb0b..8e863e730c 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/get_block_hash.rs @@ -53,7 +53,7 @@ fn positive_flow(runnable_version: RunnableCairo1) { assert_eq!( entry_point_call.clone().execute_directly(&mut state).unwrap().execution, - CallExecution { gas_consumed: 5220, ..CallExecution::from_retdata(retdata![block_hash]) } + CallExecution { gas_consumed: 15220, ..CallExecution::from_retdata(retdata![block_hash]) } ); } diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/keccak.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/keccak.rs index 644db1ed87..1a054803e9 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/keccak.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/keccak.rs @@ -26,6 +26,6 @@ fn test_keccak(runnable_version: RunnableCairo1) { pretty_assertions::assert_eq!( entry_point_call.execute_directly(&mut state).unwrap().execution, - CallExecution { gas_consumed: 254240, ..CallExecution::from_retdata(retdata![]) } + CallExecution { gas_consumed: 264240, ..CallExecution::from_retdata(retdata![]) } ); } diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs index b2d7e38d3f..bfe522355e 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/library_call.rs @@ -84,7 +84,7 @@ fn test_library_call_assert_fails(runnable_version: RunnableCairo1) { // 'ENTRYPOINT_FAILED'. felt!("0x454e545259504f494e545f4641494c4544") ]), - gas_consumed: 100980, + gas_consumed: 110980, failed: true, ..Default::default() } @@ -148,7 +148,7 @@ fn test_nested_library_call(runnable_version: RunnableCairo1) { ..nested_storage_entry_point }; - let storage_entry_point_gas = GasAmount(16990); + let storage_entry_point_gas = GasAmount(26990); // The default VersionedConstants is used in the execute_directly call bellow. let tracked_resource = test_contract.get_runnable_class().tracked_resource( @@ -177,7 +177,7 @@ fn test_nested_library_call(runnable_version: RunnableCairo1) { gas_consumed: REQUIRED_GAS_LIBRARY_CALL_TEST, ..CallExecution::default() }, - charged_resources: ChargedResources::from_gas(GasAmount(117970)), + charged_resources: ChargedResources::from_gas(GasAmount(127970)), inner_calls: vec![nested_storage_call_info], tracked_resource, ..Default::default() @@ -197,7 +197,7 @@ fn test_nested_library_call(runnable_version: RunnableCairo1) { ..Default::default() }; - let main_gas_consumed = 325110; + let main_gas_consumed = 335110; let expected_call_info = CallInfo { call: main_entry_point.clone(), execution: CallExecution { diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/out_of_gas.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/out_of_gas.rs index e93e26c6be..ed32807310 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/out_of_gas.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/out_of_gas.rs @@ -74,7 +74,7 @@ fn test_stack_overflow() { CallExecution { // 'Out of gas' retdata: retdata![felt!["0x4f7574206f6620676173"]], - gas_consumed: MAX_POSSIBLE_SIERRA_GAS - 6590, + gas_consumed: MAX_POSSIBLE_SIERRA_GAS - 5380, failed: true, ..Default::default() } diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/replace_class.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/replace_class.rs index b0932025f9..af10881179 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/replace_class.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/replace_class.rs @@ -74,7 +74,7 @@ fn positive_flow(runnable_version: RunnableCairo1) { }; assert_eq!( entry_point_call.execute_directly(&mut state).unwrap().execution, - CallExecution { gas_consumed: 5220, ..Default::default() } + CallExecution { gas_consumed: 15220, ..Default::default() } ); assert_eq!(state.get_class_hash_at(contract_address).unwrap(), new_class_hash); } diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs index a6ff6d624f..ec187c5263 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs @@ -25,7 +25,7 @@ fn test_secp256k1(runnable_version: RunnableCairo1) { pretty_assertions::assert_eq!( entry_point_call.execute_directly(&mut state).unwrap().execution, - CallExecution { gas_consumed: 17033486, ..Default::default() } + CallExecution { gas_consumed: 17043486, ..Default::default() } ); } @@ -45,6 +45,6 @@ fn test_secp256r1(runnable_version: RunnableCairo1) { pretty_assertions::assert_eq!( entry_point_call.execute_directly(&mut state).unwrap().execution, - CallExecution { gas_consumed: 27562930, ..Default::default() } + CallExecution { gas_consumed: 27572930, ..Default::default() } ); } diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/send_message_to_l1.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/send_message_to_l1.rs index c0569e2bd6..c3f4c04700 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/send_message_to_l1.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/send_message_to_l1.rs @@ -47,7 +47,7 @@ fn test_send_message_to_l1(runnable_version: RunnableCairo1) { entry_point_call.execute_directly(&mut state).unwrap().execution, CallExecution { l2_to_l1_messages: vec![OrderedL2ToL1Message { order: 0, message }], - gas_consumed: 20960, + gas_consumed: 30960, ..Default::default() } ); diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/sha256.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/sha256.rs index bf9b9cd36f..04afdb3d91 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/sha256.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/sha256.rs @@ -26,6 +26,6 @@ fn test_sha256(runnable_version: RunnableCairo1) { pretty_assertions::assert_eq!( entry_point_call.execute_directly(&mut state).unwrap().execution, - CallExecution { gas_consumed: 881425, ..CallExecution::from_retdata(retdata![]) } + CallExecution { gas_consumed: 891425, ..CallExecution::from_retdata(retdata![]) } ); } diff --git a/crates/blockifier/src/test_utils.rs b/crates/blockifier/src/test_utils.rs index 2606d3e7b4..fc72615b82 100644 --- a/crates/blockifier/src/test_utils.rs +++ b/crates/blockifier/src/test_utils.rs @@ -201,14 +201,18 @@ pub fn trivial_external_entry_point_new(contract: FeatureContract) -> CallEntryP pub fn trivial_external_entry_point_with_address( contract_address: ContractAddress, ) -> CallEntryPoint { + let vesrioned_const = VersionedConstants::create_for_testing(); + let initial_gas = vesrioned_const.os_constants + .gas_costs + .base + .default_initial_gas_cost + + // Caller's initial budget is refunded for paying entry point's initial budget. + vesrioned_const.os_constants.gas_costs.base.entry_point_initial_budget; + CallEntryPoint { code_address: Some(contract_address), storage_address: contract_address, - initial_gas: VersionedConstants::create_for_testing() - .os_constants - .gas_costs - .base - .default_initial_gas_cost, + initial_gas, ..Default::default() } } diff --git a/crates/blockifier/src/transaction/account_transactions_test.rs b/crates/blockifier/src/transaction/account_transactions_test.rs index e09f3948a7..25e71b6218 100644 --- a/crates/blockifier/src/transaction/account_transactions_test.rs +++ b/crates/blockifier/src/transaction/account_transactions_test.rs @@ -112,6 +112,7 @@ use crate::transaction::test_utils::{ use crate::transaction::transaction_types::TransactionType; use crate::transaction::transactions::ExecutableTransaction; use crate::utils::u64_from_usize; +use crate::versioned_constants::VersionedConstants; #[rstest] fn test_circuit(block_context: BlockContext, default_all_resource_bounds: ValidResourceBounds) { @@ -1735,6 +1736,13 @@ fn test_initial_gas( .execute_max_sierra_gas .min(user_gas_bound - GasAmount(validate_gas_consumed) + GasAmount(1)) .0; + // Caller's initial budget is refunded for paying entry point's initial budget. + prev_initial_gas += VersionedConstants::create_for_testing() + .os_constants + .gas_costs + .base + .entry_point_initial_budget; + let mut curr_initial_gas; let mut started_vm_mode = false; // The __validate__ call of a the account contract. diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index bd78fb3214..9a0362aa15 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -467,7 +467,7 @@ fn add_kzg_da_resources_to_resources_mapping( n_memory_holes: 0, builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 8)]), }, - validate_gas_consumed: 4740, // The gas consumption results from parsing the input + validate_gas_consumed: 14740, // The gas consumption results from parsing the input // arguments. execute_gas_consumed: 112080, }, @@ -1512,10 +1512,20 @@ fn declare_validate_callinfo( if version == TransactionVersion::ZERO { None } else { + let gas_consumed = match declared_contract_version { + CairoVersion::Cairo0 => 0, + CairoVersion::Cairo1(_) => { + VersionedConstants::create_for_testing() + .os_constants + .gas_costs + .base + .entry_point_initial_budget + } + }; expected_validate_call_info( account_class_hash, constants::VALIDATE_DECLARE_ENTRY_POINT_NAME, - 0, + gas_consumed, calldata![declared_class_hash.0], account_address, declared_contract_version, @@ -1647,10 +1657,27 @@ fn test_declare_tx( &starknet_resources, vec![&expected_validate_call_info], ); + let initial_gas = VersionedConstants::create_for_testing() + .os_constants + .gas_costs + .base + .entry_point_initial_budget; + let expected_gas_consumed = match account_cairo_version { + CairoVersion::Cairo0 => GasAmount(0), + CairoVersion::Cairo1(_) => { + if tx_version == TransactionVersion::ZERO { + GasAmount(0) + } else { + GasAmount(initial_gas) + } + } + }; + let mut expected_actual_resources = TransactionResources { starknet_resources, computation: ComputationResources { vm_resources: expected_cairo_resources, + sierra_gas: expected_gas_consumed, ..Default::default() }, }; @@ -1764,9 +1791,14 @@ fn test_declare_tx_v0(default_l1_resource_bounds: ValidResourceBounds) { } #[rstest] +#[case::with_cairo0_account(CairoVersion::Cairo0, 0)] +#[case::with_cairo1_account( + CairoVersion::Cairo1(RunnableCairo1::Casm), + VersionedConstants::create_for_testing().os_constants.gas_costs.base.entry_point_initial_budget +)] fn test_deploy_account_tx( - #[values(CairoVersion::Cairo0, CairoVersion::Cairo1(RunnableCairo1::Casm))] - cairo_version: CairoVersion, + #[case] cairo_version: CairoVersion, + #[case] expected_gas_consumed: u64, #[values(false, true)] use_kzg_da: bool, default_all_resource_bounds: ValidResourceBounds, ) { @@ -1817,7 +1849,6 @@ fn test_deploy_account_tx( panic!("Expected DeployAccount transaction.") }; - let expected_gas_consumed = 0; let expected_validate_call_info = expected_validate_call_info( account_class_hash, constants::VALIDATE_DEPLOY_ENTRY_POINT_NAME, @@ -1877,6 +1908,7 @@ fn test_deploy_account_tx( starknet_resources, computation: ComputationResources { vm_resources: expected_cairo_resources, + sierra_gas: expected_gas_consumed.into(), ..Default::default() }, }; @@ -2344,7 +2376,7 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) { // Build the expected call info. let accessed_storage_key = StorageKey::try_from(key).unwrap(); - let gas_consumed = GasAmount(6120); + let gas_consumed = GasAmount(16120); let expected_call_info = CallInfo { call: CallEntryPoint { class_hash: Some(test_contract.get_class_hash()), @@ -2376,11 +2408,11 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) { // (currently matches only starknet resources). let expected_gas = match use_kzg_da { true => GasVector { - l1_gas: 17899_u32.into(), + l1_gas: 17999_u32.into(), l1_data_gas: 160_u32.into(), l2_gas: 0_u32.into(), }, - false => GasVector::from_l1_gas(19593_u32.into()), + false => GasVector::from_l1_gas(19693_u32.into()), }; let expected_da_gas = match use_kzg_da {