Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(blockifier): added tests for get_vm_resources_cost #927

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
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
63 changes: 45 additions & 18 deletions crates/blockifier/src/fee/fee_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::fee::resources::{GasVector, GasVectorComputationMode};
use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{
gas_vector_from_vm_usage,
CairoVersion,
BALANCE,
DEFAULT_ETH_L1_DATA_GAS_PRICE,
Expand Down Expand Up @@ -43,84 +44,110 @@ fn get_vm_resource_usage() -> ExecutionResources {
}
}

#[test]
fn test_simple_get_vm_resource_usage() {
#[rstest]
fn test_simple_get_vm_resource_usage(
#[values(GasVectorComputationMode::NoL2Gas, GasVectorComputationMode::All)]
gas_vector_computation_mode: GasVectorComputationMode,
) {
let versioned_constants = VersionedConstants::create_for_account_testing();
let mut vm_resource_usage = get_vm_resource_usage();
let n_reverted_steps = 15;

// Positive flow.
// Verify calculation - in our case, n_steps is the heaviest resource.
let l1_gas_by_vm_usage = (versioned_constants.vm_resource_fee_cost().n_steps
let vm_usage_in_l1_gas = (versioned_constants.vm_resource_fee_cost().n_steps
* (u128_from_usize(vm_resource_usage.n_steps + n_reverted_steps)))
.ceil()
.to_integer();
let expected_gas_vector = gas_vector_from_vm_usage(
vm_usage_in_l1_gas,
&gas_vector_computation_mode,
&versioned_constants,
);
assert_eq!(
GasVector::from_l1_gas(l1_gas_by_vm_usage),
expected_gas_vector,
get_vm_resources_cost(
&versioned_constants,
&vm_resource_usage,
n_reverted_steps,
&GasVectorComputationMode::NoL2Gas
&gas_vector_computation_mode
)
);

// Another positive flow, this time the heaviest resource is range_check_builtin.
let n_reverted_steps = 0;
vm_resource_usage.n_steps =
vm_resource_usage.builtin_instance_counter.get(&BuiltinName::range_check).unwrap() - 1;
let l1_gas_by_vm_usage =
vm_resource_usage.builtin_instance_counter.get(&BuiltinName::range_check).unwrap();
let vm_usage_in_l1_gas = u128_from_usize(
*vm_resource_usage.builtin_instance_counter.get(&BuiltinName::range_check).unwrap(),
);
let expected_gas_vector = gas_vector_from_vm_usage(
vm_usage_in_l1_gas,
&gas_vector_computation_mode,
&versioned_constants,
);
assert_eq!(
GasVector::from_l1_gas(u128_from_usize(*l1_gas_by_vm_usage)),
expected_gas_vector,
get_vm_resources_cost(
&versioned_constants,
&vm_resource_usage,
n_reverted_steps,
&GasVectorComputationMode::NoL2Gas
&gas_vector_computation_mode
)
);
}

#[test]
fn test_float_get_vm_resource_usage() {
#[rstest]
fn test_float_get_vm_resource_usage(
#[values(GasVectorComputationMode::NoL2Gas, GasVectorComputationMode::All)]
gas_vector_computation_mode: GasVectorComputationMode,
) {
let versioned_constants = VersionedConstants::create_for_testing();
let mut vm_resource_usage = get_vm_resource_usage();

// Positive flow.
// Verify calculation - in our case, n_steps is the heaviest resource.
let n_reverted_steps = 300;
let l1_gas_by_vm_usage = (versioned_constants.vm_resource_fee_cost().n_steps
let vm_usage_in_l1_gas = (versioned_constants.vm_resource_fee_cost().n_steps
* u128_from_usize(vm_resource_usage.n_steps + n_reverted_steps))
.ceil()
.to_integer();
let expected_gas_vector = gas_vector_from_vm_usage(
vm_usage_in_l1_gas,
&gas_vector_computation_mode,
&versioned_constants,
);
assert_eq!(
GasVector::from_l1_gas(l1_gas_by_vm_usage),
expected_gas_vector,
get_vm_resources_cost(
&versioned_constants,
&vm_resource_usage,
n_reverted_steps,
&GasVectorComputationMode::NoL2Gas
&gas_vector_computation_mode
)
);

// Another positive flow, this time the heaviest resource is ecdsa_builtin.
vm_resource_usage.n_steps = 200;
let l1_gas_by_vm_usage =
let vm_usage_in_l1_gas =
((*versioned_constants.vm_resource_fee_cost().builtins.get(&BuiltinName::ecdsa).unwrap())
* u128_from_usize(
*vm_resource_usage.builtin_instance_counter.get(&BuiltinName::ecdsa).unwrap(),
))
.ceil()
.to_integer();

let expected_gas_vector = gas_vector_from_vm_usage(
vm_usage_in_l1_gas,
&gas_vector_computation_mode,
&versioned_constants,
);
assert_eq!(
GasVector::from_l1_gas(l1_gas_by_vm_usage),
expected_gas_vector,
get_vm_resources_cost(
&versioned_constants,
&vm_resource_usage,
n_reverted_steps,
&GasVectorComputationMode::NoL2Gas
&gas_vector_computation_mode
)
);
}
Expand Down
20 changes: 19 additions & 1 deletion crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ use crate::execution::call_info::ExecutionSummary;
use crate::execution::deprecated_syscalls::hint_processor::SyscallCounter;
use crate::execution::entry_point::CallEntryPoint;
use crate::execution::syscalls::SyscallSelector;
use crate::fee::resources::{StarknetResources, StateResources};
use crate::fee::resources::{
GasVector,
GasVectorComputationMode,
StarknetResources,
StateResources,
};
use crate::test_utils::contracts::FeatureContract;
use crate::transaction::transaction_types::TransactionType;
use crate::utils::{const_max, u128_from_usize};
Expand Down Expand Up @@ -383,3 +388,16 @@ pub fn update_json_value(base: &mut serde_json::Value, update: serde_json::Value
_ => panic!("Both base and update should be of type serde_json::Value::Object."),
}
}

pub fn gas_vector_from_vm_usage(
vm_usage_in_l1_gas: u128,
computation_mode: &GasVectorComputationMode,
versioned_constants: &VersionedConstants,
) -> GasVector {
match computation_mode {
GasVectorComputationMode::NoL2Gas => GasVector::from_l1_gas(vm_usage_in_l1_gas),
GasVectorComputationMode::All => GasVector::from_l2_gas(
versioned_constants.convert_l1_to_l2_gas_amount_round_up(vm_usage_in_l1_gas),
),
}
}
Loading