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

Commit

Permalink
chore(fee): integer VM gas usage
Browse files Browse the repository at this point in the history
Signed-off-by: Dori Medini <dori@starkware.co>
  • Loading branch information
dorimedini-starkware committed Jan 31, 2024
1 parent 2ca2f2e commit 3b19023
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions crates/blockifier/src/fee/fee_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{CairoVersion, BALANCE};
use crate::transaction::errors::TransactionFeeError;
use crate::transaction::objects::ResourcesMapping;
use crate::transaction::objects::{GasVector, ResourcesMapping};
use crate::transaction::test_utils::{account_invoke_tx, l1_resource_bounds};

fn get_vm_resource_usage() -> ResourcesMapping {
Expand All @@ -41,7 +41,7 @@ fn test_calculate_l1_gas_by_vm_usage() {
// Verify calculation - in our case, n_steps is the heaviest resource.
let l1_gas_by_vm_usage = vm_resource_usage.0.get(constants::N_STEPS_RESOURCE).unwrap();
assert_eq!(
*l1_gas_by_vm_usage as f64,
GasVector { l1_gas: *l1_gas_by_vm_usage as u128, blob_gas: 0 },
calculate_l1_gas_by_vm_usage(&block_context, &vm_resource_usage).unwrap()
);

Expand Down
13 changes: 7 additions & 6 deletions crates/blockifier/src/fee/fee_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn extract_l1_blob_gas_usage(resources: &ResourcesMapping) -> (usize, Resour
pub fn calculate_l1_gas_by_vm_usage(
block_context: &BlockContext,
vm_resource_usage: &ResourcesMapping,
) -> TransactionFeeResult<f64> {
) -> TransactionFeeResult<GasVector> {
let vm_resource_fee_costs = &block_context.block_info.vm_resource_fee_cost;
let vm_resource_names = HashSet::<&String>::from_iter(vm_resource_usage.0.keys());
if !vm_resource_names.is_subset(&HashSet::from_iter(vm_resource_fee_costs.keys())) {
Expand All @@ -56,7 +56,8 @@ pub fn calculate_l1_gas_by_vm_usage(
})
.fold(f64::NAN, f64::max);

Ok(vm_l1_gas_usage)
// TODO(Dori, 1/5/2024): Check this conversion.
Ok(GasVector { l1_gas: vm_l1_gas_usage.ceil() as u128, blob_gas: 0 })
}

/// Computes and returns the total L1 gas consumption.
Expand All @@ -68,14 +69,14 @@ pub fn calculate_tx_gas_vector(
) -> TransactionFeeResult<GasVector> {
let (l1_gas_usage, vm_resources) = extract_l1_gas_and_vm_usage(resources);
let (l1_blob_gas_usage, vm_resources) = extract_l1_blob_gas_usage(&vm_resources);
let l1_gas_by_vm_usage = calculate_l1_gas_by_vm_usage(block_context, &vm_resources)?;
let total_l1_gas_usage = l1_gas_usage as f64 + l1_gas_by_vm_usage;
let vm_usage_gas_vector = calculate_l1_gas_by_vm_usage(block_context, &vm_resources)?;

Ok(GasVector {
l1_gas: total_l1_gas_usage.ceil() as u128,
l1_gas: u128_from_usize(l1_gas_usage)
.expect("Conversion from usize to u128 should not fail."),
blob_gas: u128_from_usize(l1_blob_gas_usage)
.expect("Conversion from usize to u128 should not fail."),
})
} + vm_usage_gas_vector)
}

pub fn get_fee_by_gas_vector(
Expand Down

0 comments on commit 3b19023

Please sign in to comment.