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

Commit

Permalink
Change initial_gas to be of type u64 .
Browse files Browse the repository at this point in the history
  • Loading branch information
noaov1 committed Jun 28, 2023
1 parent d4d90ee commit 079653a
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 26 deletions.
6 changes: 3 additions & 3 deletions crates/blockifier/src/execution/cairo1_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub fn prepare_call_arguments(
return Err(PreExecutionError::InvalidBuiltin(builtin_name.clone()));
}
// Push gas counter.
args.push(CairoArg::Single(MaybeRelocatable::from(&call.initial_gas)));
args.push(CairoArg::Single(MaybeRelocatable::from(Felt252::from(call.initial_gas))));
// Push syscall ptr.
args.push(CairoArg::Single(MaybeRelocatable::from(initial_syscall_ptr)));

Expand Down Expand Up @@ -321,13 +321,13 @@ fn get_call_result(
Err(PostExecutionError::MalformedReturnData {
error_message: "Error extracting return data.".to_string()});
};
if gas < &Felt252::from(0) || gas > &syscall_handler.call.initial_gas {
if gas < &Felt252::from(0) || gas > &Felt252::from(syscall_handler.call.initial_gas) {
return Err(PostExecutionError::MalformedReturnData {
error_message: format!("Unexpected remaining gas: {gas}."),
});
}

let gas_consumed = &syscall_handler.call.initial_gas - gas;
let gas_consumed = &Felt252::from(syscall_handler.call.initial_gas) - gas;
Ok(CallResult {
failed,
retdata: read_execution_retdata(vm, retdata_size, retdata_start)?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ pub fn execute_library_call(
) -> DeprecatedSyscallResult<ReadOnlySegment> {
let entry_point_type =
if call_to_external { EntryPointType::External } else { EntryPointType::L1Handler };
let initial_gas = constants::INITIAL_GAS_COST.into();
let initial_gas = constants::INITIAL_GAS_COST;
let entry_point = CallEntryPoint {
class_hash: Some(class_hash),
code_address,
Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/execution/deprecated_syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub fn call_contract(
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<CallContractResponse> {
let storage_address = request.contract_address;
let initial_gas = constants::INITIAL_GAS_COST.into();
let initial_gas = constants::INITIAL_GAS_COST;
let entry_point = CallEntryPoint {
class_hash: None,
code_address: Some(storage_address),
Expand Down Expand Up @@ -290,7 +290,7 @@ pub fn deploy(
deployer_address_for_calculation,
)?;

let initial_gas = constants::INITIAL_GAS_COST.into();
let initial_gas = constants::INITIAL_GAS_COST;
let ctor_context = ConstructorContext {
class_hash: request.class_hash,
code_address: Some(deployed_contract_address),
Expand All @@ -303,7 +303,7 @@ pub fn deploy(
syscall_handler.context,
ctor_context,
request.constructor_calldata,
initial_gas,
Felt252::from(initial_gas),
)?;
syscall_handler.inner_calls.push(call_info);

Expand Down
9 changes: 6 additions & 3 deletions crates/blockifier/src/execution/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cairo_felt::Felt252;
use cairo_vm::vm::runners::cairo_runner::{
ExecutionResources as VmExecutionResources, RunResources,
};
use num_traits::ToPrimitive;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::hash::StarkFelt;
Expand Down Expand Up @@ -53,7 +54,7 @@ pub struct CallEntryPoint {
pub storage_address: ContractAddress,
pub caller_address: ContractAddress,
pub call_type: CallType,
pub initial_gas: Felt252,
pub initial_gas: u64,
}

pub struct ConstructorContext {
Expand Down Expand Up @@ -328,7 +329,7 @@ pub fn execute_constructor_entry_point(
storage_address: ctor_context.storage_address,
caller_address: ctor_context.caller_address,
call_type: CallType::Call,
initial_gas: remaining_gas,
initial_gas: remaining_gas.to_u64().expect("The gas must be representable with 64 bits."),
};

constructor_call.execute(state, resources, context)
Expand Down Expand Up @@ -357,7 +358,9 @@ pub fn handle_empty_constructor(
storage_address: ctor_context.storage_address,
caller_address: ctor_context.caller_address,
call_type: CallType::Call,
initial_gas: remaining_gas,
initial_gas: remaining_gas
.to_u64()
.expect("The gas must be representable with 64 bits."),
},
..Default::default()
};
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use cairo_vm::vm::errors::memory_errors::MemoryError;
use cairo_vm::vm::errors::vm_errors::VirtualMachineError;
use cairo_vm::vm::runners::cairo_runner::RunResources;
use cairo_vm::vm::vm_core::VirtualMachine;
use num_traits::ToPrimitive;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::hash::StarkFelt;
Expand Down Expand Up @@ -526,7 +527,7 @@ pub fn execute_library_call(
storage_address: syscall_handler.storage_address(),
caller_address: syscall_handler.caller_address(),
call_type: CallType::Delegate,
initial_gas: remaining_gas.clone(),
initial_gas: remaining_gas.to_u64().expect("The gas must be representable with 64 bits."),
};

execute_inner_call(entry_point, vm, syscall_handler, remaining_gas)
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/execution/syscalls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cairo_felt::Felt252;
use cairo_vm::types::relocatable::Relocatable;
use cairo_vm::vm::vm_core::VirtualMachine;
use num_traits::ToPrimitive;
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::core::{
calculate_contract_address, ClassHash, ContractAddress, EntryPointSelector,
Expand Down Expand Up @@ -163,7 +164,7 @@ pub fn call_contract(
storage_address,
caller_address: syscall_handler.storage_address(),
call_type: CallType::Call,
initial_gas: remaining_gas.clone(),
initial_gas: remaining_gas.to_u64().expect("The gas must be representable with 64 bits."),
};
let retdata_segment = execute_inner_call(entry_point, vm, syscall_handler, remaining_gas)?;

Expand Down
11 changes: 5 additions & 6 deletions crates/blockifier/src/execution/syscalls/syscalls_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::{HashMap, HashSet};

use assert_matches::assert_matches;
use cairo_felt::Felt252;
use cairo_vm::vm::runners::builtin_runner::RANGE_CHECK_BUILTIN_NAME;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources;
use itertools::concat;
Expand Down Expand Up @@ -248,7 +247,7 @@ fn test_nested_library_call() {
entry_point_selector: selector_from_name("test_nested_library_call"),
calldata: main_entry_point_calldata,
class_hash: Some(ClassHash(stark_felt!(TEST_CLASS_HASH))),
initial_gas: Felt252::from(9999906600_u64),
initial_gas: 9999906600,
..trivial_external_entry_point()
};
let nested_storage_entry_point = CallEntryPoint {
Expand All @@ -257,7 +256,7 @@ fn test_nested_library_call() {
class_hash: Some(ClassHash(stark_felt!(TEST_CLASS_HASH))),
code_address: None,
call_type: CallType::Delegate,
initial_gas: Felt252::from(9999719920_u64),
initial_gas: 9999719920,
..trivial_external_entry_point()
};
let library_entry_point = CallEntryPoint {
Expand All @@ -272,12 +271,12 @@ fn test_nested_library_call() {
class_hash: Some(ClassHash(stark_felt!(TEST_CLASS_HASH))),
code_address: None,
call_type: CallType::Delegate,
initial_gas: Felt252::from(9999813750_u64),
initial_gas: 9999813750,
..trivial_external_entry_point()
};
let storage_entry_point = CallEntryPoint {
calldata: calldata![stark_felt!(key), stark_felt!(value)],
initial_gas: Felt252::from(9999623870_u64),
initial_gas: 9999623870,
..nested_storage_entry_point
};
let storage_entry_point_vm_resources = VmExecutionResources {
Expand Down Expand Up @@ -536,7 +535,7 @@ fn test_out_of_gas() {
let entry_point_call = CallEntryPoint {
calldata,
entry_point_selector: selector_from_name("test_storage_read_write"),
initial_gas: Felt252::from(REQUIRED_GAS_STORAGE_READ_WRITE_TEST - 1),
initial_gas: REQUIRED_GAS_STORAGE_READ_WRITE_TEST - 1,
..trivial_external_entry_point()
};
let error = entry_point_call.execute_directly(&mut state).unwrap_err();
Expand Down
9 changes: 6 additions & 3 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cairo_felt::Felt252;
use itertools::concat;
use num_traits::ToPrimitive;
use starknet_api::calldata;
use starknet_api::core::{ContractAddress, EntryPointSelector};
use starknet_api::deprecated_contract_class::EntryPointType;
Expand Down Expand Up @@ -185,7 +186,9 @@ impl AccountTransaction {
storage_address,
caller_address: ContractAddress::default(),
call_type: CallType::Call,
initial_gas: remaining_gas.clone(),
initial_gas: remaining_gas
.to_u64()
.expect("The gas must be representable with 64 bits."),
};

let validate_call_info = validate_call
Expand Down Expand Up @@ -238,7 +241,7 @@ impl AccountTransaction {

let storage_address = block_context.fee_token_address;
// The fee-token contract is a Cairo 0 contract, hence the initial gas is irrelevant.
let initial_gas = abi_constants::INITIAL_GAS_COST.into();
let initial_gas = abi_constants::INITIAL_GAS_COST;
let fee_transfer_call = CallEntryPoint {
class_hash: None,
code_address: None,
Expand Down Expand Up @@ -280,7 +283,7 @@ impl<S: StateReader> ExecutableTransaction<S> for AccountTransaction {
let execute_call_info: Option<CallInfo>;
let tx_type = self.tx_type();
let mut resources = ExecutionResources::default();
let mut remaining_gas = Transaction::initial_gas();
let mut remaining_gas = Felt252::from(Transaction::initial_gas());

match &self {
Self::Declare(tx) => {
Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/transaction/transaction_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub enum Transaction {

impl Transaction {
/// Returns the initial gas of the transaction to run with.
pub fn initial_gas() -> Felt252 {
Felt252::from(abi_constants::INITIAL_GAS_COST - abi_constants::TRANSACTION_GAS_COST)
pub fn initial_gas() -> u64 {
abi_constants::INITIAL_GAS_COST - abi_constants::TRANSACTION_GAS_COST
}
}

Expand Down Expand Up @@ -84,7 +84,7 @@ impl<S: StateReader> ExecutableTransaction<S> for L1HandlerTransaction {
tx_context,
block_context.invoke_tx_max_n_steps,
);
let mut remaining_gas = Transaction::initial_gas();
let mut remaining_gas = Felt252::from(Transaction::initial_gas());
let execute_call_info =
self.run_execute(state, &mut resources, &mut context, &mut remaining_gas)?;

Expand Down
9 changes: 7 additions & 2 deletions crates/blockifier/src/transaction/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use cairo_felt::Felt252;
use num_traits::ToPrimitive;
use starknet_api::core::ContractAddress;
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::transaction::{Calldata, DeployAccountTransaction, Fee, InvokeTransaction};
Expand Down Expand Up @@ -222,7 +223,9 @@ impl<S: State> Executable<S> for InvokeTransaction {
storage_address,
caller_address: ContractAddress::default(),
call_type: CallType::Call,
initial_gas: remaining_gas.clone(),
initial_gas: remaining_gas
.to_u64()
.expect("The gas must be representable with 64 bits."),
};

let call_info = execute_call
Expand Down Expand Up @@ -259,7 +262,9 @@ impl<S: State> Executable<S> for L1HandlerTransaction {
storage_address,
caller_address: ContractAddress::default(),
call_type: CallType::Call,
initial_gas: remaining_gas.clone(),
initial_gas: remaining_gas
.to_u64()
.expect("The gas must be representable with 64 bits."),
};

execute_call
Expand Down

0 comments on commit 079653a

Please sign in to comment.