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

Commit

Permalink
feat(fee): add events gas cost
Browse files Browse the repository at this point in the history
  • Loading branch information
barak-b-starkware committed Feb 11, 2024
1 parent 40908fc commit 4d31291
Show file tree
Hide file tree
Showing 9 changed files with 564 additions and 348 deletions.
4 changes: 4 additions & 0 deletions crates/blockifier/resources/versioned_constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"max_calldata_length": 4000,
"max_contract_bytecode_size": 61440
},
"event_gas_cost": {
"milligas_per_data_word": 2560,
"milligas_per_key": 1280
},
"invoke_tx_max_n_steps": 3000000,
"max_recursion_depth": 50,
"os_constants": {
Expand Down
29 changes: 26 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,20 @@ use crate::abi::constants as abi_constants;
use crate::context::TransactionContext;
use crate::execution::call_info::CallInfo;
use crate::execution::entry_point::ExecutionResources;
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_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 @@ -140,6 +145,23 @@ impl<'a> ActualCostBuilder<'a> {
self.tx_context.block_context.block_info.use_kzg_da
}

/// Returns an estimation of the L1 gas amount that will be used (by Starknet's state update and
/// the Verifier) following the addition of a transaction with the given parameters to a batch;
/// e.g., a message from L2 to L1 is followed by a storage write operation in Starknet L1
/// contract which requires gas.
fn calculate_tx_gas_usage_vector(
versioned_constants: &VersionedConstants,
call_infos: impl Iterator<Item = &'a CallInfo> + Clone,
state_changes_count: StateChangesCount,
l1_handler_payload_size: Option<usize>,
use_kzg_da: bool,
) -> TransactionExecutionResult<GasVector> {
// TODO(barak, 18/03/2024): Iterate over call_infos once without cloning.
Ok(get_messages_gas_cost(call_infos.clone(), l1_handler_payload_size)?
+ get_da_gas_cost(state_changes_count, use_kzg_da)
+ get_tx_events_gas_cost(call_infos, versioned_constants))
}

// Construct the actual cost object using all fields that were set in the builder.
fn calculate_actual_fee_and_resources(
self,
Expand All @@ -158,7 +180,8 @@ 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,
self.l1_payload_size,
Expand Down
Loading

0 comments on commit 4d31291

Please sign in to comment.