Skip to content

Commit

Permalink
feat(blockifier): only All resource bounds txs can run in SierraGas m…
Browse files Browse the repository at this point in the history
…ode (#1771)
  • Loading branch information
TzahiTaub authored Nov 6, 2024
1 parent a64cf6c commit 03ef939
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 28 deletions.
7 changes: 1 addition & 6 deletions crates/blockifier/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ impl TransactionContext {
self.tx_info.sender_address() == self.block_context.block_info.sequencer_address
}
pub fn get_gas_vector_computation_mode(&self) -> GasVectorComputationMode {
match &self.tx_info {
TransactionInfo::Current(info) => {
info.resource_bounds.get_gas_vector_computation_mode()
}
TransactionInfo::Deprecated(_) => GasVectorComputationMode::NoL2Gas,
}
self.tx_info.gas_mode()
}
pub fn get_gas_prices(&self) -> &GasPriceVector {
self.block_context
Expand Down
20 changes: 14 additions & 6 deletions crates/blockifier/src/execution/contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use starknet_api::deprecated_contract_class::{
EntryPointV0,
Program as DeprecatedProgram,
};
use starknet_api::transaction::GasVectorComputationMode;
use starknet_types_core::felt::Felt;

use crate::abi::constants::{self};
Expand Down Expand Up @@ -131,12 +132,19 @@ impl RunnableContractClass {
}

/// Returns whether this contract should run using Cairo steps or Sierra gas.
pub fn tracked_resource(&self, min_sierra_version: &CompilerVersion) -> TrackedResource {
match self {
Self::V0(_) => TrackedResource::CairoSteps,
Self::V1(contract_class) => contract_class.tracked_resource(min_sierra_version),
#[cfg(feature = "cairo_native")]
Self::V1Native(_) => TrackedResource::SierraGas,
pub fn tracked_resource(
&self,
min_sierra_version: &CompilerVersion,
gas_mode: GasVectorComputationMode,
) -> TrackedResource {
match gas_mode {
GasVectorComputationMode::All => match self {
Self::V0(_) => TrackedResource::CairoSteps,
Self::V1(contract_class) => contract_class.tracked_resource(min_sierra_version),
#[cfg(feature = "cairo_native")]
Self::V1Native(_) => TrackedResource::SierraGas,
},
GasVectorComputationMode::NoL2Gas => TrackedResource::CairoSteps,
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions crates/blockifier/src/execution/execution_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ pub fn execute_entry_point_call_wrapper(
context: &mut EntryPointExecutionContext,
remaining_gas: &mut u64,
) -> EntryPointExecutionResult<CallInfo> {
let contract_tracked_resource = contract_class
.tracked_resource(&context.versioned_constants().min_compiler_version_for_sierra_gas);
let contract_tracked_resource = contract_class.tracked_resource(
&context.versioned_constants().min_compiler_version_for_sierra_gas,
context.tx_context.tx_info.gas_mode(),
);
// Note: no return statements (explicit or implicit) should be added between the push and the
// pop commands.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use pretty_assertions::assert_eq;
use starknet_api::execution_utils::format_panic_data;
use starknet_api::transaction::GasVectorComputationMode;
use starknet_api::{calldata, felt, storage_key};
use test_case::test_case;

Expand Down Expand Up @@ -149,6 +150,7 @@ fn test_nested_library_call(test_contract: FeatureContract, expected_gas: u64) {
// The default VersionedConstants is used in the execute_directly call bellow.
let tracked_resource = test_contract.get_runnable_class().tracked_resource(
&VersionedConstants::create_for_testing().min_compiler_version_for_sierra_gas,
GasVectorComputationMode::All,
);

let nested_storage_call_info = CallInfo {
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl CompilerBasedVersion {
}

/// Returns the context-free tracked resource of this contract (does not take caller contract
/// into account).
/// and the transaction info into account).
pub fn own_tracked_resource(&self) -> TrackedResource {
match self {
Self::CairoVersion(CairoVersion::Cairo0) | Self::OldCairo1 => {
Expand Down
10 changes: 10 additions & 0 deletions crates/blockifier/src/transaction/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use starknet_api::transaction::{
AccountDeploymentData,
AllResourceBounds,
Fee,
GasVectorComputationMode,
PaymasterData,
ResourceBounds,
Tip,
Expand Down Expand Up @@ -85,6 +86,15 @@ impl TransactionInfo {
TransactionInfo::Deprecated(context) => context.max_fee != Fee(0),
}
}

pub fn gas_mode(&self) -> GasVectorComputationMode {
match self {
TransactionInfo::Current(info) => {
info.resource_bounds.get_gas_vector_computation_mode()
}
TransactionInfo::Deprecated(_) => GasVectorComputationMode::NoL2Gas,
}
}
}

impl HasRelatedFeeType for TransactionInfo {
Expand Down
35 changes: 22 additions & 13 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ fn add_kzg_da_resources_to_resources_mapping(
fn test_invoke_tx(
#[values(default_l1_resource_bounds(), default_all_resource_bounds())]
resource_bounds: ValidResourceBounds,
#[case] expected_arguments: ExpectedResultTestInvokeTx,
#[case] mut expected_arguments: ExpectedResultTestInvokeTx,
#[case] account_cairo_version: CairoVersion,
#[values(false, true)] use_kzg_da: bool,
) {
Expand Down Expand Up @@ -472,9 +472,15 @@ fn test_invoke_tx(

let actual_execution_info = account_tx.execute(state, block_context, true, true).unwrap();

let tracked_resource = account_contract
.get_runnable_class()
.tracked_resource(&versioned_constants.min_compiler_version_for_sierra_gas);
let tracked_resource = account_contract.get_runnable_class().tracked_resource(
&versioned_constants.min_compiler_version_for_sierra_gas,
tx_context.tx_info.gas_mode(),
);
if tracked_resource == TrackedResource::CairoSteps {
// In CairoSteps mode, the initial gas is set to the default once before the validate call.
expected_arguments.inner_call_initial_gas -=
expected_arguments.validate_gas_consumed + expected_arguments.execute_gas_consumed
}

// Build expected validate call info.
let expected_account_class_hash = account_contract.get_class_hash();
Expand Down Expand Up @@ -1525,9 +1531,10 @@ fn test_declare_tx(
class_hash,
account.get_class_hash(),
sender_address,
account
.get_runnable_class()
.tracked_resource(&versioned_constants.min_compiler_version_for_sierra_gas),
account.get_runnable_class().tracked_resource(
&versioned_constants.min_compiler_version_for_sierra_gas,
tx_context.tx_info.gas_mode(),
),
if tx_version >= TransactionVersion::THREE {
user_initial_gas_from_bounds(default_all_resource_bounds)
} else {
Expand Down Expand Up @@ -1725,9 +1732,10 @@ fn test_deploy_account_tx(
Calldata(validate_calldata.into()),
deployed_account_address,
cairo_version,
account
.get_runnable_class()
.tracked_resource(&versioned_constants.min_compiler_version_for_sierra_gas),
account.get_runnable_class().tracked_resource(
&versioned_constants.min_compiler_version_for_sierra_gas,
tx_context.tx_info.gas_mode(),
),
user_initial_gas,
);

Expand Down Expand Up @@ -2258,9 +2266,10 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
builtin_instance_counter: HashMap::from([(BuiltinName::range_check, 6)]),
}),
accessed_storage_keys: HashSet::from_iter(vec![accessed_storage_key]),
tracked_resource: test_contract
.get_runnable_class()
.tracked_resource(&versioned_constants.min_compiler_version_for_sierra_gas),
tracked_resource: test_contract.get_runnable_class().tracked_resource(
&versioned_constants.min_compiler_version_for_sierra_gas,
GasVectorComputationMode::NoL2Gas,
),
..Default::default()
};

Expand Down

0 comments on commit 03ef939

Please sign in to comment.