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

Commit

Permalink
refactor(fee): moving calculate_tx_gas_usage_vector to ActualCostBuil…
Browse files Browse the repository at this point in the history
…der (#1478)
  • Loading branch information
barak-b-starkware authored Feb 12, 2024
1 parent 06ceabe commit fce18e1
Show file tree
Hide file tree
Showing 6 changed files with 477 additions and 445 deletions.
42 changes: 39 additions & 3 deletions crates/blockifier/src/fee/actual_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ use starknet_api::transaction::Fee;
use crate::abi::constants as abi_constants;
use crate::context::TransactionContext;
use crate::execution::call_info::CallInfo;
use crate::fee::gas_usage::{calculate_tx_gas_usage_vector, get_da_gas_cost};
use crate::state::cached_state::{CachedState, StateChanges};
use crate::fee::gas_usage::{
get_calldata_and_signature_gas_cost, get_code_gas_cost, get_da_gas_cost, get_messages_gas_cost,
get_tx_events_gas_cost,
};
use crate::state::cached_state::{CachedState, StateChanges, StateChangesCount};
use crate::state::state_api::{StateReader, StateResult};
use crate::transaction::objects::{
GasVector, HasRelatedFeeType, ResourcesMapping, TransactionExecutionResult,
};
use crate::transaction::transaction_types::TransactionType;
use crate::transaction::transaction_utils::calculate_tx_resources;
use crate::transaction::transactions::ClassInfo;
use crate::versioned_constants::VersionedConstants;

#[cfg(test)]
#[path = "actual_cost_test.rs"]
pub mod test;

// TODO(Gilad): Use everywhere instead of passing the `actual_{fee,resources}` tuple, which often
// get passed around together.
Expand Down Expand Up @@ -156,7 +164,7 @@ impl<'a> ActualCostBuilder<'a> {
self.validate_call_info.into_iter().chain(self.execute_call_info);
// Gas usage for SHARP costs and Starknet L1-L2 messages. Includes gas usage for data
// availability.
let gas_usage_vector = calculate_tx_gas_usage_vector(
let gas_usage_vector = Self::calculate_tx_gas_usage_vector(
&self.tx_context.block_context.versioned_constants,
non_optional_call_infos,
state_changes_count,
Expand Down Expand Up @@ -191,4 +199,32 @@ impl<'a> ActualCostBuilder<'a> {

Ok(ActualCost { actual_fee, da_gas, actual_resources })
}

/// Returns the gas usage of a transaction, specifically:
/// * L1 gas, used by Starknet's state update and the Verifier, e.g., a message from L2 to L1 is
/// followed by a storage write operation on L1.
/// * L1 data gas, for publishing data availability.
/// * L2 resources cost, e.g., for storing transaction calldata.
// TODO(Avi, 01/03/2024): Resolve the clippy error cleanly.
#[allow(clippy::too_many_arguments)]
fn calculate_tx_gas_usage_vector(
versioned_constants: &VersionedConstants,
call_infos: impl Iterator<Item = &'a CallInfo> + Clone,
state_changes_count: StateChangesCount,
calldata_length: usize,
signature_length: usize,
l1_handler_payload_size: Option<usize>,
class_info: Option<ClassInfo>,
use_kzg_da: bool,
) -> TransactionExecutionResult<GasVector> {
Ok(get_messages_gas_cost(call_infos.clone(), l1_handler_payload_size)?
+ get_da_gas_cost(state_changes_count, use_kzg_da)
+ get_calldata_and_signature_gas_cost(
calldata_length,
signature_length,
versioned_constants,
)
+ get_code_gas_cost(class_info, versioned_constants)
+ get_tx_events_gas_cost(call_infos, versioned_constants))
}
}
Loading

0 comments on commit fce18e1

Please sign in to comment.