Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(fee): change valid resource bound to tupled enum #509

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions crates/blockifier/src/blockifier/stateful_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ use crate::state::cached_state::CachedState;
use crate::state::errors::StateError;
use crate::state::state_api::StateReader;
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::errors::{TransactionExecutionError, TransactionPreValidationError};
use crate::transaction::errors::{
TransactionExecutionError,
TransactionInfoCreationError,
TransactionPreValidationError,
};
use crate::transaction::transaction_execution::Transaction;
use crate::transaction::transactions::ValidatableTransaction;

Expand All @@ -36,6 +40,8 @@ pub enum StatefulValidatorError {
TransactionExecutorError(#[from] TransactionExecutorError),
#[error(transparent)]
TransactionPreValidationError(#[from] TransactionPreValidationError),
#[error(transparent)]
TransactionCreationError(#[from] TransactionInfoCreationError),
}

pub type StatefulValidatorResult<T> = Result<T, StatefulValidatorError>;
Expand Down Expand Up @@ -65,7 +71,7 @@ impl<S: StateReader> StatefulValidator<S> {
return Ok(());
}

let tx_context = self.tx_executor.block_context.to_tx_context(&tx);
let tx_context = self.tx_executor.block_context.to_tx_context(&tx)?;
self.perform_pre_validation_stage(&tx, &tx_context)?;

if skip_validate {
Expand Down Expand Up @@ -112,7 +118,7 @@ impl<S: StateReader> StatefulValidator<S> {
mut remaining_gas: u64,
) -> StatefulValidatorResult<(Option<CallInfo>, TransactionReceipt)> {
let mut execution_resources = ExecutionResources::default();
let tx_context = Arc::new(self.tx_executor.block_context.to_tx_context(tx));
let tx_context = Arc::new(self.tx_executor.block_context.to_tx_context(tx)?);

let limit_steps_by_resources = true;
let validate_call_info = tx.validate_tx(
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/concurrency/fee_utils_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use num_bigint::BigUint;
use rstest::rstest;
use starknet_api::felt;
use starknet_api::transaction::{Fee, ResourceBoundsMapping};
use starknet_api::transaction::{DeprecatedResourceBoundsMapping, Fee};
use starknet_types_core::felt::Felt;

use crate::concurrency::fee_utils::{add_fee_to_sequencer_balance, fill_sequencer_balance_reads};
Expand All @@ -19,7 +19,7 @@ use crate::transaction::test_utils::{account_invoke_tx, block_context, max_resou
#[rstest]
pub fn test_fill_sequencer_balance_reads(
block_context: BlockContext,
max_resource_bounds: ResourceBoundsMapping,
max_resource_bounds: DeprecatedResourceBoundsMapping,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] erc20_version: CairoVersion,
) {
let account = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo1);
Expand Down
8 changes: 4 additions & 4 deletions crates/blockifier/src/concurrency/versioned_state_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use starknet_api::core::{
Nonce,
PatriciaKey,
};
use starknet_api::transaction::{Calldata, ContractAddressSalt, ResourceBoundsMapping};
use starknet_api::transaction::{Calldata, ContractAddressSalt, DeprecatedResourceBoundsMapping};
use starknet_api::{calldata, class_hash, contract_address, felt, patricia_key};

use crate::abi::abi_utils::{get_fee_token_var_address, get_storage_var_address};
Expand Down Expand Up @@ -201,7 +201,7 @@ fn test_versioned_state_proxy() {

#[rstest]
// Test parallel execution of two transactions that use the same versioned state.
fn test_run_parallel_txs(max_resource_bounds: ResourceBoundsMapping) {
fn test_run_parallel_txs(max_resource_bounds: DeprecatedResourceBoundsMapping) {
let block_context = BlockContext::create_for_account_testing();
let chain_info = &block_context.chain_info;
let zero_bounds = true;
Expand Down Expand Up @@ -233,7 +233,7 @@ fn test_run_parallel_txs(max_resource_bounds: ResourceBoundsMapping) {
&mut NonceManager::default(),
);
let account_tx_1 = AccountTransaction::DeployAccount(deploy_account_tx_1);
let enforce_fee = account_tx_1.create_tx_info().enforce_fee().unwrap();
let enforce_fee = account_tx_1.create_tx_info().unwrap().enforce_fee().unwrap();

let class_hash = grindy_account.get_class_hash();
let ctor_storage_arg = felt!(1_u8);
Expand All @@ -248,7 +248,7 @@ fn test_run_parallel_txs(max_resource_bounds: ResourceBoundsMapping) {
let deploy_account_tx_2 = deploy_account_tx(deploy_tx_args, nonce_manager);
let account_address = deploy_account_tx_2.contract_address;
let account_tx_2 = AccountTransaction::DeployAccount(deploy_account_tx_2);
let tx_context = block_context.to_tx_context(&account_tx_2);
let tx_context = block_context.to_tx_context(&account_tx_2).unwrap();
let fee_type = tx_context.tx_info.fee_type();

let deployed_account_balance_key = get_fee_token_var_address(account_address);
Expand Down
5 changes: 4 additions & 1 deletion crates/blockifier/src/concurrency/worker_logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ impl<'a, S: StateReader> WorkerExecutor<'a, S> {
&mut execution_output.as_mut().expect(EXECUTION_OUTPUTS_UNWRAP_ERROR).result;

if let Ok(tx_execution_info) = tx_result.as_mut() {
let tx_context = self.block_context.to_tx_context(&self.chunk[tx_index]);
let tx_context = self
.block_context
.to_tx_context(&self.chunk[tx_index])
.expect("Failed to create tx context.");
// Add the deleted sequencer balance key to the storage keys.
let concurrency_mode = true;
tx_state_changes_keys.update_sequencer_key_in_storage(
Expand Down
14 changes: 7 additions & 7 deletions crates/blockifier/src/concurrency/worker_logic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use rstest::rstest;
use starknet_api::core::{ContractAddress, Nonce, PatriciaKey};
use starknet_api::transaction::{
ContractAddressSalt,
DeprecatedResourceBoundsMapping,
Fee,
ResourceBoundsMapping,
TransactionVersion,
};
use starknet_api::{contract_address, felt, patricia_key};
Expand Down Expand Up @@ -174,7 +174,7 @@ pub fn test_commit_tx() {
assert_eq!(felt!(expected_sequencer_storage_read), actual_sequencer_storage_read,);
}
}
let tx_context = executor.block_context.to_tx_context(&txs[commit_idx]);
let tx_context = executor.block_context.to_tx_context(&txs[commit_idx]).unwrap();
expected_sequencer_balance_low += actual_fee;
// Check that the sequencer balance was updated correctly in the state.
verify_sequencer_balance_update(
Expand Down Expand Up @@ -228,7 +228,7 @@ fn test_commit_tx_when_sender_is_sequencer() {
let read_values_before_commit = fee_transfer_call_info.storage_read_values.clone();
drop(execution_task_outputs);

let tx_context = &executor.block_context.to_tx_context(&sequencer_tx[0]);
let tx_context = &executor.block_context.to_tx_context(&sequencer_tx[0]).unwrap();
let fee_token_address =
executor.block_context.chain_info.fee_token_address(&tx_context.tx_info.fee_type());
let sequencer_balance_high_before =
Expand Down Expand Up @@ -256,7 +256,7 @@ fn test_commit_tx_when_sender_is_sequencer() {
}

#[rstest]
fn test_worker_execute(max_resource_bounds: ResourceBoundsMapping) {
fn test_worker_execute(max_resource_bounds: DeprecatedResourceBoundsMapping) {
// Settings.
let block_context = BlockContext::create_for_account_testing();
let account_contract = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo1);
Expand Down Expand Up @@ -430,7 +430,7 @@ fn test_worker_execute(max_resource_bounds: ResourceBoundsMapping) {
}

#[rstest]
fn test_worker_validate(max_resource_bounds: ResourceBoundsMapping) {
fn test_worker_validate(max_resource_bounds: DeprecatedResourceBoundsMapping) {
// Settings.
let block_context = BlockContext::create_for_account_testing();
let account_contract = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo1);
Expand Down Expand Up @@ -537,7 +537,7 @@ fn test_worker_validate(max_resource_bounds: ResourceBoundsMapping) {
#[case::declare_cairo1(CairoVersion::Cairo1, TransactionVersion::THREE)]
fn test_deploy_before_declare(
max_fee: Fee,
max_resource_bounds: ResourceBoundsMapping,
max_resource_bounds: DeprecatedResourceBoundsMapping,
#[case] cairo_version: CairoVersion,
#[case] version: TransactionVersion,
) {
Expand Down Expand Up @@ -629,7 +629,7 @@ fn test_deploy_before_declare(
}

#[rstest]
fn test_worker_commit_phase(max_resource_bounds: ResourceBoundsMapping) {
fn test_worker_commit_phase(max_resource_bounds: DeprecatedResourceBoundsMapping) {
// Settings.
let block_context = BlockContext::create_for_account_testing();
let account_contract = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo1);
Expand Down
9 changes: 5 additions & 4 deletions crates/blockifier/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use starknet_api::core::{ChainId, ContractAddress};

use crate::blockifier::block::BlockInfo;
use crate::bouncer::BouncerConfig;
use crate::transaction::errors::TransactionInfoCreationError;
use crate::transaction::objects::{
FeeType,
HasRelatedFeeType,
Expand Down Expand Up @@ -65,11 +66,11 @@ impl BlockContext {
pub fn to_tx_context(
&self,
tx_info_creator: &impl TransactionInfoCreator,
) -> TransactionContext {
TransactionContext {
) -> Result<TransactionContext, TransactionInfoCreationError> {
Ok(TransactionContext {
block_context: self.clone(),
tx_info: tx_info_creator.create_tx_info(),
}
tx_info: tx_info_creator.create_tx_info()?,
})
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/execution/stack_trace_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use starknet_api::transaction::{
Calldata,
ContractAddressSalt,
Fee,
ResourceBoundsMapping,
DeprecatedResourceBoundsMapping,
TransactionSignature,
TransactionVersion,
};
Expand Down Expand Up @@ -599,7 +599,7 @@ An ASSERT_EQ instruction failed: 1 != 0.
/// point selector).
fn test_contract_ctor_frame_stack_trace(
block_context: BlockContext,
max_resource_bounds: ResourceBoundsMapping,
max_resource_bounds: DeprecatedResourceBoundsMapping,
#[values(CairoVersion::Cairo0, CairoVersion::Cairo1)] cairo_version: CairoVersion,
) {
let chain_info = &block_context.chain_info;
Expand Down
57 changes: 36 additions & 21 deletions crates/blockifier/src/execution/syscalls/hint_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use cairo_vm::vm::vm_core::VirtualMachine;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::state::StorageKey;
use starknet_api::transaction::{Calldata, Resource};
use starknet_api::transaction::{AllResourcesBounds, Calldata};
use starknet_api::StarknetApiError;
use starknet_types_core::felt::{Felt, FromStrError};
use thiserror::Error;
Expand Down Expand Up @@ -205,6 +205,8 @@ pub const INVALID_ARGUMENT: &str =
pub const L1_GAS: &str = "0x00000000000000000000000000000000000000000000000000004c315f474153";
// "L2_GAS";
pub const L2_GAS: &str = "0x00000000000000000000000000000000000000000000000000004c325f474153";
// "L1_DATA_GAS"
pub const L1_DATA_GAS: &str = "0x0000000000000000000000000000000000000000004c315f444154415f474153";

/// Executes Starknet syscalls (stateful protocol hints) during the execution of an entry point
/// call.
Expand Down Expand Up @@ -462,26 +464,39 @@ impl<'a> SyscallHintProcessor<'a> {
vm: &mut VirtualMachine,
tx_info: &CurrentTransactionInfo,
) -> SyscallResult<(Relocatable, Relocatable)> {
let l1_gas = Felt::from_hex(L1_GAS).map_err(SyscallExecutionError::from)?;
let l2_gas = Felt::from_hex(L2_GAS).map_err(SyscallExecutionError::from)?;
let flat_resource_bounds: Vec<Felt> = tx_info
.resource_bounds
.0
.iter()
.flat_map(|(resource, resource_bounds)| {
let resource = match resource {
Resource::L1Gas => l1_gas,
Resource::L2Gas => l2_gas,
Resource::L1DataGas => todo!(),
};

vec![
resource,
Felt::from(resource_bounds.max_amount),
Felt::from(resource_bounds.max_price_per_unit),
]
})
.collect();
let l1_gas_as_felt = Felt::from_hex(L1_GAS).map_err(SyscallExecutionError::from)?;
let l2_gas_as_felt = Felt::from_hex(L2_GAS).map_err(SyscallExecutionError::from)?;
let l1_data_gas_as_felt =
Felt::from_hex(L1_DATA_GAS).map_err(SyscallExecutionError::from)?;

let flat_resource_bounds =
match tx_info.resource_bounds {
starknet_api::transaction::ValidResourceBounds::L1Gas(l1_bounds) => {
vec![
l1_gas_as_felt,
Felt::from(l1_bounds.max_amount),
Felt::from(l1_bounds.max_price_per_unit),
l2_gas_as_felt,
Felt::ZERO,
Felt::ZERO,
]
}
starknet_api::transaction::ValidResourceBounds::AllResources(
AllResourcesBounds { l1_gas, l2_gas, l1_data_gas },
) => {
vec![
l1_gas_as_felt,
Felt::from(l1_gas.max_amount),
Felt::from(l1_gas.max_price_per_unit),
l2_gas_as_felt,
Felt::from(l2_gas.max_amount),
Felt::from(l2_gas.max_price_per_unit),
l1_data_gas_as_felt,
Felt::from(l1_data_gas.max_amount),
Felt::from(l1_data_gas.max_price_per_unit),
]
}
};

self.allocate_data_segment(vm, &flat_resource_bounds)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use starknet_api::transaction::{
PaymasterData,
Resource,
ResourceBounds,
ResourceBoundsMapping,
Tip,
TransactionHash,
TransactionVersion,
Expand Down Expand Up @@ -213,7 +212,7 @@ fn test_get_execution_info(
only_query,
..Default::default()
},
resource_bounds: ResourceBoundsMapping(BTreeMap::from([
resource_bounds: BTreeMap::from([
(
Resource::L1Gas,
// TODO(Ori, 1/2/2024): Write an indicative expect message explaining why
Expand All @@ -227,7 +226,9 @@ fn test_get_execution_info(
},
),
(Resource::L2Gas, ResourceBounds { max_amount: 0, max_price_per_unit: 0 }),
])),
])
.try_into()
.unwrap(),
tip: Tip::default(),
nonce_data_availability_mode: DataAvailabilityMode::L1,
fee_data_availability_mode: DataAvailabilityMode::L1,
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/fee/actual_cost_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rstest::{fixture, rstest};
use starknet_api::transaction::{L2ToL1Payload, ResourceBoundsMapping};
use starknet_api::transaction::{L2ToL1Payload, DeprecatedResourceBoundsMapping};
use starknet_types_core::felt::Felt;

use crate::context::BlockContext;
Expand Down Expand Up @@ -286,7 +286,7 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
// resources are taken into account).
#[rstest]
fn test_calculate_tx_gas_usage(
max_resource_bounds: ResourceBoundsMapping,
max_resource_bounds: DeprecatedResourceBoundsMapping,
#[values(false, true)] use_kzg_da: bool,
) {
let account_cairo_version = CairoVersion::Cairo0;
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/fee/fee_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ fn test_discounted_gas_overdraft(
let charge_fee = true;
let report = PostExecutionReport::new(
&mut state,
&block_context.to_tx_context(&tx),
&block_context.to_tx_context(&tx).unwrap(),
&tx_receipt,
charge_fee,
)
Expand Down
5 changes: 3 additions & 2 deletions crates/blockifier/src/fee/gas_usage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ fn test_get_message_segment_length(

#[rstest]
fn test_compute_discounted_gas_from_gas_vector() {
let tx_context =
BlockContext::create_for_testing().to_tx_context(&account_invoke_tx(invoke_tx_args! {}));
let tx_context = BlockContext::create_for_testing()
.to_tx_context(&account_invoke_tx(invoke_tx_args! {}))
.unwrap();
let gas_usage = GasVector { l1_gas: 100, l1_data_gas: 2, ..Default::default() };
let actual_result = compute_discounted_gas_from_gas_vector(&gas_usage, &tx_context);

Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use starknet_api::state::StorageKey;
use starknet_api::transaction::{
Calldata,
ContractAddressSalt,
DeprecatedResourceBoundsMapping,
Resource,
ResourceBounds,
ResourceBoundsMapping,
TransactionVersion,
};
use starknet_api::{contract_address, felt, patricia_key};
Expand Down Expand Up @@ -211,8 +211,8 @@ pub fn trivial_external_entry_point_with_address(
}
}

fn default_testing_resource_bounds() -> ResourceBoundsMapping {
ResourceBoundsMapping::try_from(vec![
fn default_testing_resource_bounds() -> DeprecatedResourceBoundsMapping {
DeprecatedResourceBoundsMapping::try_from(vec![
(Resource::L1Gas, ResourceBounds { max_amount: 0, max_price_per_unit: 1 }),
// TODO(Dori, 1/2/2024): When fee market is developed, change the default price of
// L2 gas.
Expand Down
Loading
Loading