Skip to content

Commit

Permalink
feat(blockifier): Add get_syscall_gas_cost to versioned constants impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonatan-Starkware committed Nov 24, 2024
1 parent c2f8f94 commit 8e62d52
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use starknet_api::execution_resources::GasAmount;
use starknet_api::transaction::fields::GasVectorComputationMode;
use strum::IntoEnumIterator;
use thiserror::Error;
use crate::utils::u64_from_usize;

use crate::execution::deprecated_syscalls::hint_processor::SyscallCounter;
use crate::execution::execution_utils::poseidon_hash_many_cost;
Expand Down Expand Up @@ -330,6 +331,28 @@ impl VersionedConstants {
GasVectorComputationMode::NoL2Gas => &self.deprecated_l2_resource_gas_costs,
}
}

/// Calculates a syscall's gas costs from the os resources
pub fn get_syscall_gas_cost(&self, syscall_selector: &SyscallSelector) -> u64 {
let gas_costs = &self.os_constants.gas_costs;
let execution_resources = &self
.os_resources
.execute_syscalls
.get(syscall_selector)
.expect("Fetching the execution resources of a syscall should not fail.");
let n_steps = u64_from_usize(execution_resources.n_steps);
let n_memory_holes = u64_from_usize(execution_resources.n_memory_holes);
let mut total_builtin_gas_cost: u64 = 0;
for (builtin, amount) in &execution_resources.builtin_instance_counter {
let builtin_cost = gas_costs.get_builtin_gas_cost(builtin).unwrap_or_else(|err| panic!("Failed to get gas cost: {}", err));
total_builtin_gas_cost += builtin_cost * u64_from_usize(*amount);
}


std::cmp::max(n_steps * gas_costs.step_gas_cost
+ n_memory_holes * gas_costs.memory_hole_gas_cost
+ total_builtin_gas_cost, gas_costs.syscall_base_gas_cost)
}
}

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
Expand Down
14 changes: 14 additions & 0 deletions crates/blockifier/src/versioned_constants_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,17 @@ fn test_all_jsons_in_enum() {
fn test_latest_no_panic() {
VersionedConstants::latest_constants();
}

#[test]
fn test_correct_syscall_gas_cost_calculation() {
const EXPECTED_CALL_CONTRACT_GAS_COST : u64 = 87650;
const EXPECTED_SECP256K1MUL_GAS_COST : u64 = 8143650;
const EXPECTED_SHA256PROCESSBLOCK_GAS_COST : u64 = 841095;


let versioned_constants = VersionedConstants::latest_constants().clone();

assert_eq!(versioned_constants.get_syscall_gas_cost(&SyscallSelector::CallContract), EXPECTED_CALL_CONTRACT_GAS_COST);
assert_eq!(versioned_constants.get_syscall_gas_cost(&SyscallSelector::Secp256k1Mul), EXPECTED_SECP256K1MUL_GAS_COST);
assert_eq!(versioned_constants.get_syscall_gas_cost(&SyscallSelector::Sha256ProcessBlock), EXPECTED_SHA256PROCESSBLOCK_GAS_COST);
}

0 comments on commit 8e62d52

Please sign in to comment.