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

Commit

Permalink
chore: clean fee utils (#1968)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoni-Starkware authored Jun 9, 2024
1 parent 952373d commit d179561
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
11 changes: 6 additions & 5 deletions crates/blockifier/src/fee/actual_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,21 @@ impl TransactionReceipt {
n_reverted_steps: reverted_steps,
};

let gas = tx_resources.to_gas_vector(
&tx_context.block_context.versioned_constants,
tx_context.block_context.block_info.use_kzg_da,
)?;

// L1 handler transactions are not charged an L2 fee but it is compared to the L1 fee.
let fee = if tx_context.tx_info.enforce_fee()? || tx_type == TransactionType::L1Handler {
tx_context.tx_info.calculate_tx_fee(&tx_resources, &tx_context.block_context)?
tx_context.tx_info.get_fee_by_gas_vector(&tx_context.block_context.block_info, gas)
} else {
Fee(0)
};
let da_gas = tx_resources
.starknet_resources
.get_state_changes_cost(tx_context.block_context.block_info.use_kzg_da);

let gas = tx_resources.to_gas_vector(
&tx_context.block_context.versioned_constants,
tx_context.block_context.block_info.use_kzg_da,
)?;
Ok(Self { resources: tx_resources, gas, da_gas, fee })
}

Expand Down
12 changes: 0 additions & 12 deletions crates/blockifier/src/fee/fee_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::state::state_api::StateReader;
use crate::transaction::errors::TransactionFeeError;
use crate::transaction::objects::{
ExecutionResourcesTraits, FeeType, GasVector, TransactionFeeResult, TransactionInfo,
TransactionResources,
};
use crate::utils::u128_from_usize;
use crate::versioned_constants::VersionedConstants;
Expand Down Expand Up @@ -75,17 +74,6 @@ pub fn get_fee_by_gas_vector(
)
}

/// Calculates the fee that should be charged, given transaction resources.
pub fn calculate_tx_fee(
tx_resources: &TransactionResources,
block_context: &BlockContext,
fee_type: &FeeType,
) -> TransactionFeeResult<Fee> {
let gas_vector = tx_resources
.to_gas_vector(&block_context.versioned_constants, block_context.block_info.use_kzg_da)?;
Ok(get_fee_by_gas_vector(&block_context.block_info, gas_vector, fee_type))
}

/// Returns the current fee balance and a boolean indicating whether the balance covers the fee.
pub fn get_balance_and_if_covers_fee(
state: &mut dyn StateReader,
Expand Down
20 changes: 19 additions & 1 deletion crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde_json::Value;
use starknet_api::block::{BlockNumber, BlockTimestamp};
use starknet_api::core::{ChainId, ContractAddress, PatriciaKey};
use starknet_api::hash::StarkHash;
use starknet_api::transaction::Fee;
use starknet_api::{contract_address, patricia_key};

use super::update_json_value;
Expand All @@ -16,14 +17,17 @@ use crate::execution::contract_class::{ContractClassV0, ContractClassV1};
use crate::execution::entry_point::{
CallEntryPoint, EntryPointExecutionContext, EntryPointExecutionResult,
};
use crate::fee::fee_utils::get_fee_by_gas_vector;
use crate::state::state_api::State;
use crate::test_utils::{
get_raw_contract_class, CHAIN_ID_NAME, CURRENT_BLOCK_NUMBER, CURRENT_BLOCK_TIMESTAMP,
DEFAULT_ETH_L1_DATA_GAS_PRICE, DEFAULT_ETH_L1_GAS_PRICE, DEFAULT_STRK_L1_DATA_GAS_PRICE,
DEFAULT_STRK_L1_GAS_PRICE, TEST_ERC20_CONTRACT_ADDRESS, TEST_ERC20_CONTRACT_ADDRESS2,
TEST_SEQUENCER_ADDRESS,
};
use crate::transaction::objects::{DeprecatedTransactionInfo, TransactionInfo};
use crate::transaction::objects::{
DeprecatedTransactionInfo, FeeType, TransactionFeeResult, TransactionInfo, TransactionResources,
};
use crate::versioned_constants::{
GasCosts, OsConstants, VersionedConstants, DEFAULT_CONSTANTS_JSON,
};
Expand Down Expand Up @@ -89,6 +93,20 @@ impl VersionedConstants {
}
}

impl TransactionResources {
pub fn calculate_tx_fee(
&self,
block_context: &BlockContext,
fee_type: &FeeType,
) -> TransactionFeeResult<Fee> {
let gas_vector = self.to_gas_vector(
&block_context.versioned_constants,
block_context.block_info.use_kzg_da,
)?;
Ok(get_fee_by_gas_vector(&block_context.block_info, gas_vector, fee_type))
}
}

impl GasCosts {
pub fn create_for_testing_from_subset(subset_of_os_constants: &str) -> Self {
let subset_of_os_constants: Value = serde_json::from_str(subset_of_os_constants).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/transaction/execution_flavors_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use starknet_api::transaction::{Calldata, Fee, TransactionSignature, Transaction
use crate::context::{BlockContext, ChainInfo};
use crate::execution::execution_utils::{felt_to_stark_felt, stark_felt_to_felt};
use crate::execution::syscalls::SyscallSelector;
use crate::fee::fee_utils::{calculate_tx_fee, get_fee_by_gas_vector};
use crate::fee::fee_utils::get_fee_by_gas_vector;
use crate::state::cached_state::CachedState;
use crate::state::state_api::StateReader;
use crate::test_utils::contracts::FeatureContract;
Expand Down Expand Up @@ -119,7 +119,7 @@ fn check_gas_and_fee(
// Future compatibility: resources other than the L1 gas usage may affect the fee (currently,
// `calculate_tx_fee` is simply the result of `calculate_tx_gas_usage_vector` times gas price).
assert_eq!(
calculate_tx_fee(&tx_execution_info.actual_resources, block_context, fee_type).unwrap(),
tx_execution_info.actual_resources.calculate_tx_fee(block_context, fee_type).unwrap(),
expected_cost_of_resources
);
}
Expand Down
12 changes: 4 additions & 8 deletions crates/blockifier/src/transaction/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use starknet_api::transaction::{
use strum_macros::EnumIter;

use crate::abi::constants as abi_constants;
use crate::context::BlockContext;
use crate::blockifier::block::BlockInfo;
use crate::execution::call_info::{CallInfo, ExecutionSummary, MessageL1CostInfo, OrderedEvent};
use crate::execution::execution_utils::{felt_to_stark_felt, stark_felt_to_felt};
use crate::fee::eth_gas_constants;
use crate::fee::fee_utils::{calculate_l1_gas_by_vm_usage, calculate_tx_fee};
use crate::fee::fee_utils::{calculate_l1_gas_by_vm_usage, get_fee_by_gas_vector};
use crate::fee::gas_usage::{
get_consumed_message_to_l2_emissions_cost, get_da_gas_cost,
get_log_message_to_l1_emissions_cost, get_onchain_data_segment_length,
Expand Down Expand Up @@ -535,12 +535,8 @@ pub trait HasRelatedFeeType {
}
}

fn calculate_tx_fee(
&self,
tx_resources: &TransactionResources,
block_context: &BlockContext,
) -> TransactionExecutionResult<Fee> {
Ok(calculate_tx_fee(tx_resources, block_context, &self.fee_type())?)
fn get_fee_by_gas_vector(&self, block_info: &BlockInfo, gas_vector: GasVector) -> Fee {
get_fee_by_gas_vector(block_info, gas_vector, &self.fee_type())
}
}

Expand Down
10 changes: 4 additions & 6 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use crate::execution::errors::{ConstructorEntryPointExecutionError, EntryPointEx
use crate::execution::execution_utils::{felt_to_stark_felt, stark_felt_to_felt};
use crate::execution::syscalls::hint_processor::EmitEventError;
use crate::execution::syscalls::SyscallSelector;
use crate::fee::fee_utils::calculate_tx_fee;
use crate::fee::gas_usage::{
estimate_minimal_gas_vector, get_da_gas_cost, get_onchain_data_segment_length,
};
Expand Down Expand Up @@ -435,7 +434,7 @@ fn test_invoke_tx(
// Build expected fee transfer call info.
let fee_type = &tx_context.tx_info.fee_type();
let expected_actual_fee =
calculate_tx_fee(&actual_execution_info.actual_resources, block_context, fee_type).unwrap();
actual_execution_info.actual_resources.calculate_tx_fee(block_context, fee_type).unwrap();
let expected_fee_transfer_call_info = expected_fee_transfer_call_info(
&tx_context,
sender_address,
Expand Down Expand Up @@ -1146,7 +1145,7 @@ fn test_declare_tx(

// Build expected fee transfer call info.
let expected_actual_fee =
calculate_tx_fee(&actual_execution_info.actual_resources, block_context, fee_type).unwrap();
actual_execution_info.actual_resources.calculate_tx_fee(block_context, fee_type).unwrap();
let expected_fee_transfer_call_info = expected_fee_transfer_call_info(
tx_context,
sender_address,
Expand Down Expand Up @@ -1284,8 +1283,7 @@ fn test_deploy_account_tx(

// Build expected fee transfer call info.
let expected_actual_fee =
calculate_tx_fee(&actual_execution_info.actual_resources.clone(), block_context, fee_type)
.unwrap();
actual_execution_info.actual_resources.calculate_tx_fee(block_context, fee_type).unwrap();
let expected_fee_transfer_call_info = expected_fee_transfer_call_info(
tx_context,
deployed_account_address,
Expand Down Expand Up @@ -1855,7 +1853,7 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
let error = tx_no_fee.execute(state, block_context, true, true).unwrap_err();
// Today, we check that the paid_fee is positive, no matter what was the actual fee.
let expected_actual_fee =
calculate_tx_fee(&expected_execution_info.actual_resources, block_context, &FeeType::Eth)
(expected_execution_info.actual_resources.calculate_tx_fee(block_context, &FeeType::Eth))
.unwrap();
assert_matches!(
error,
Expand Down

0 comments on commit d179561

Please sign in to comment.