Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Add documentation to transaction/fee module #889

Merged
merged 6 commits into from
Oct 2, 2023
Merged
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
14 changes: 10 additions & 4 deletions src/transaction/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ pub(crate) fn execute_fee_transfer<S: StateReader>(
call_info.ok_or(TransactionError::CallInfoIsNone)
}

// ----------------------------------------------------------------------------------------
/// Calculates the fee of a transaction given its execution resources.
/// We add the l1_gas_usage (which may include, for example, the direct cost of L2-to-L1
/// messages) to the gas consumed by Cairo resource and multiply by the L1 gas price.

pub fn calculate_tx_fee(
resources: &HashMap<String, usize>,
gas_price: u128,
Expand All @@ -93,11 +91,9 @@ pub fn calculate_tx_fee(
Ok(total_l1_gas_usage.ceil() as u128 * gas_price)
}

// ----------------------------------------------------------------------------------------
/// Calculates the L1 gas consumed when submitting the underlying Cairo program to SHARP.
/// I.e., returns the heaviest Cairo resource weight (in terms of L1 gas), as the size of
/// a proof is determined similarly - by the (normalized) largest segment.

pub(crate) fn calculate_l1_gas_by_cairo_usage(
block_context: &BlockContext,
cairo_resource_usage: &HashMap<String, usize>,
Expand All @@ -116,6 +112,7 @@ pub(crate) fn calculate_l1_gas_by_cairo_usage(
))
}

/// Calculates the maximum weighted value from a given resource usage mapping.
fn max_of_keys(cairo_rsc: &HashMap<String, usize>, weights: &HashMap<String, f64>) -> f64 {
let mut max = 0.0_f64;
for (k, v) in weights {
Expand All @@ -135,6 +132,11 @@ fn max_of_keys(cairo_rsc: &HashMap<String, usize>, weights: &HashMap<String, f64
/// - `tx_execution_context`: The transaction's execution context.
/// - `skip_fee_transfer`: Whether to skip the fee transfer.
///
/// # Errors
/// - [TransactionError::ActualFeeExceedsMaxFee] - If the actual fee is bigger than the maximal fee.
///
/// # Returns
/// The [FeeInfo] with the given actual fee.
pub fn charge_fee<S: StateReader>(
state: &mut CachedState<S>,
resources: &HashMap<String, usize>,
Expand Down Expand Up @@ -195,6 +197,8 @@ mod tests {
transaction::fee::charge_fee,
};

/// Tests the behavior of the charge_fee function when the actual fee exceeds the maximum fee
/// for version 0. It expects to return an ActualFeeExceedsMaxFee error.
#[test]
fn charge_fee_v0_max_fee_exceeded_should_charge_nothing() {
let mut state = CachedState::new(
Expand Down Expand Up @@ -224,6 +228,8 @@ mod tests {
assert_eq!(result.1, 0);
}

/// Tests the behavior of the charge_fee function when the actual fee exceeds the maximum fee
/// for version 1. It expects the function to return the maximum fee.
#[test]
fn charge_fee_v1_max_fee_exceeded_should_charge_max_fee() {
let mut state = CachedState::new(
Expand Down