From a7c9816e5b29d67023d0f57c79821d9e1a6c9dfd Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Fri, 4 Aug 2023 11:44:42 +0300 Subject: [PATCH 1/2] added comments to src/transaction/fee.rs --- src/transaction/fee.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/transaction/fee.rs b/src/transaction/fee.rs index c823db554..26e512169 100644 --- a/src/transaction/fee.rs +++ b/src/transaction/fee.rs @@ -73,11 +73,9 @@ pub(crate) fn execute_fee_transfer( 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, gas_price: u128, @@ -94,11 +92,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, @@ -117,6 +113,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, weights: &HashMap) -> f64 { let mut max = 0.0_f64; for (k, v) in weights { @@ -127,7 +124,6 @@ fn max_of_keys(cairo_rsc: &HashMap, weights: &HashMap, weights: &HashMap( state: &mut CachedState, resources: &HashMap, @@ -191,6 +186,8 @@ mod tests { transaction::{error::TransactionError, 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 test_charge_fee_v0_actual_fee_exceeds_max_fee_should_return_error() { let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); @@ -217,6 +214,8 @@ mod tests { assert_matches!(result, TransactionError::ActualFeeExceedsMaxFee(_, _)); } + /// 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 test_charge_fee_v1_actual_fee_exceeds_max_fee_should_return_max_fee() { let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None); From 6b6997c37eafb87620fb89a608833348069b9b79 Mon Sep 17 00:00:00 2001 From: fannyguthmann Date: Thu, 10 Aug 2023 14:14:04 +0300 Subject: [PATCH 2/2] added return and error comments --- src/transaction/fee.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/transaction/fee.rs b/src/transaction/fee.rs index 88fbe4187..5916ea85e 100644 --- a/src/transaction/fee.rs +++ b/src/transaction/fee.rs @@ -124,6 +124,7 @@ fn max_of_keys(cairo_rsc: &HashMap, weights: &HashMap, weights: &HashMap( state: &mut CachedState, resources: &HashMap,