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 7, 2024
1 parent 27ac871 commit f4c840a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
10 changes: 9 additions & 1 deletion crates/blockifier/src/fee/eth_gas_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ pub const GAS_PER_NONZERO_TO_INT_STORAGE_SET: usize = 2900;
pub const GAS_PER_COUNTER_DECREASE: usize =
GAS_PER_COLD_STORAGE_ACCESS + GAS_PER_NONZERO_TO_INT_STORAGE_SET;

// Events.
// L1 Events.
pub const GAS_PER_LOG: usize = 375;
pub const GAS_PER_LOG_TOPIC: usize = 375;
pub const GAS_PER_LOG_DATA_BYTE: usize = 8;
pub const GAS_PER_LOG_DATA_WORD: usize = GAS_PER_LOG_DATA_BYTE * WORD_WIDTH;

// L2 Events.
pub const MILLI_GAS_PER_KEY_BYTE: usize = 40;
pub const MILLI_GAS_PER_DATA_BYTE: usize = 2 * MILLI_GAS_PER_KEY_BYTE;
// TODO(barak, 18/03/2024): Remove calculation per StarkFelt.
// Every key and every data word is a StarkFelt which contains 32 bytes.
pub const MILLI_GAS_PER_KEY: usize = 32 * MILLI_GAS_PER_KEY_BYTE;
pub const MILLI_GAS_PER_DATA_WORD: usize = 32 * MILLI_GAS_PER_DATA_BYTE;

// SHARP empirical costs.
pub const SHARP_ADDITIONAL_GAS_PER_MEMORY_WORD: usize = 100; // This value is not accurate.
pub const SHARP_GAS_PER_MEMORY_WORD: usize =
Expand Down
32 changes: 28 additions & 4 deletions crates/blockifier/src/fee/gas_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,42 @@ pub mod test;
/// e.g., a message from L2 to L1 is followed by a storage write operation in Starknet L1 contract
/// which requires gas.
pub fn calculate_tx_gas_usage_vector<'a>(
call_infos: impl Iterator<Item = &'a CallInfo>,
call_infos: impl Iterator<Item = &'a CallInfo> + Clone,
state_changes_count: StateChangesCount,
l1_handler_payload_size: Option<usize>,
use_kzg_da: bool,
) -> TransactionExecutionResult<GasVector> {
Ok(calculate_messages_gas_vector(call_infos, l1_handler_payload_size)?
+ get_da_gas_cost(state_changes_count, use_kzg_da))
// 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_events_gas_cost(call_infos))
}

fn get_events_gas_cost<'a>(call_infos: impl Iterator<Item = &'a CallInfo>) -> GasVector {
let mut milli_gas_vector = GasVector { l1_gas: 0, blob_gas: 0 };
for call_info in call_infos {
milli_gas_vector = milli_gas_vector
+ call_info.execution.events.iter().fold(
GasVector { l1_gas: 0, blob_gas: 0 },
|sum, ordered_event| {
sum + GasVector {
l1_gas: u128_from_usize(
ordered_event.event.keys.len() * eth_gas_constants::MILLI_GAS_PER_KEY
+ ordered_event.event.data.0.len()
* eth_gas_constants::MILLI_GAS_PER_DATA_WORD,
)
.expect("Could not convert starknet gas usage from usize to u128."),
blob_gas: 0,
}
},
)
}
GasVector { l1_gas: milli_gas_vector.l1_gas / 1000, blob_gas: milli_gas_vector.blob_gas / 1000 }
}

/// Returns an estimation of the gas usage for processing L1<>L2 messages on L1. Accounts for both
/// Starknet and SHARP contracts.
pub fn calculate_messages_gas_vector<'a>(
pub fn get_messages_gas_cost<'a>(
call_infos: impl Iterator<Item = &'a CallInfo>,
l1_handler_payload_size: Option<usize>,
) -> TransactionExecutionResult<GasVector> {
Expand Down

0 comments on commit f4c840a

Please sign in to comment.