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 StarknetResources::to_gas_vector #918

Open
wants to merge 1 commit into
base: amos/rename_max_resource_bounds
Choose a base branch
from
Open
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
24 changes: 14 additions & 10 deletions crates/blockifier/src/fee/gas_usage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::fee::fee_utils::get_fee_by_gas_vector;
use crate::fee::gas_usage::{get_da_gas_cost, get_message_segment_length};
use crate::state::cached_state::StateChangesCount;
use crate::test_utils::{DEFAULT_ETH_L1_DATA_GAS_PRICE, DEFAULT_ETH_L1_GAS_PRICE};
use crate::transaction::objects::GasVectorComputationMode::NoL2Gas;
use crate::transaction::objects::{
FeeType,
GasVector,
Expand All @@ -32,13 +31,16 @@ fn versioned_constants() -> &'static VersionedConstants {
#[rstest]
fn test_get_event_gas_cost(
versioned_constants: &VersionedConstants,
// TODO!(Aner): add `All` computation mode.
#[values(NoL2Gas)] gas_vector_computation_mode: GasVectorComputationMode,
#[values(false, true)] use_kzg_da: bool,
#[values(GasVectorComputationMode::NoL2Gas, GasVectorComputationMode::All)]
gas_vector_computation_mode: GasVectorComputationMode,
) {
let l2_resource_gas_costs = &versioned_constants.deprecated_l2_resource_gas_costs;
let archival_gas_costs = match gas_vector_computation_mode {
GasVectorComputationMode::All => &versioned_constants.archival_data_gas_costs,
GasVectorComputationMode::NoL2Gas => &versioned_constants.deprecated_l2_resource_gas_costs,
};
let (event_key_factor, data_word_cost) =
(l2_resource_gas_costs.event_key_factor, l2_resource_gas_costs.gas_per_data_felt);
(archival_gas_costs.event_key_factor, archival_gas_costs.gas_per_data_felt);
let call_infos = vec![CallInfo::default(), CallInfo::default(), CallInfo::default()];
let call_infos_iter = call_infos.iter();
let starknet_resources =
Expand Down Expand Up @@ -83,18 +85,20 @@ fn test_get_event_gas_cost(
};
let call_infos = vec![call_info_1, call_info_2, call_info_3];
let call_infos_iter = call_infos.iter();
let expected = GasVector::from_l1_gas(
// 8 keys and 11 data words overall.
(data_word_cost * (event_key_factor * 8_u128 + 11_u128)).to_integer(),
);
// 8 keys and 11 data words overall.
let expected = (data_word_cost * (event_key_factor * 8_u128 + 11_u128)).to_integer();
let expected_gas_vector = match gas_vector_computation_mode {
GasVectorComputationMode::NoL2Gas => GasVector::from_l1_gas(expected),
GasVectorComputationMode::All => GasVector::from_l2_gas(expected),
};
let starknet_resources =
StarknetResources::new(0, 0, 0, StateChangesCount::default(), None, call_infos_iter);
let gas_vector = starknet_resources.to_gas_vector(
versioned_constants,
use_kzg_da,
&gas_vector_computation_mode,
);
assert_eq!(expected, gas_vector);
assert_eq!(expected_gas_vector, gas_vector);
assert_ne!(GasVector::default(), gas_vector)
}

Expand Down
107 changes: 69 additions & 38 deletions crates/blockifier/src/fee/receipt_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rstest::{fixture, rstest};
use starknet_api::transaction::{L2ToL1Payload, ValidResourceBounds};
use starknet_api::transaction::L2ToL1Payload;
use starknet_api::{invoke_tx_args, nonce};
use starknet_types_core::felt::Felt;

Expand All @@ -25,7 +25,7 @@ use crate::transaction::objects::{
use crate::transaction::test_utils::{
account_invoke_tx,
calculate_class_info_for_testing,
max_l1_resource_bounds,
get_resource_bounds,
};
use crate::transaction::transactions::ExecutableTransaction;
use crate::utils::{u128_from_usize, usize_from_u128};
Expand All @@ -49,14 +49,18 @@ fn versioned_constants() -> &'static VersionedConstants {
// TODO(Aner, 29/01/24) Refactor with assert on GasVector objects.
// TODO(Aner, 29/01/24) Refactor to replace match with if when formatting is nicer
#[rstest]
fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool) {
fn test_calculate_tx_gas_usage_basic<'a>(
#[values(false, true)] use_kzg_da: bool,
#[values(GasVectorComputationMode::NoL2Gas, GasVectorComputationMode::All)]
gas_vector_computation_mode: GasVectorComputationMode,
) {
// An empty transaction (a theoretical case for sanity check).
let versioned_constants = VersionedConstants::create_for_account_testing();
let empty_tx_starknet_resources = StarknetResources::default();
let empty_tx_gas_usage_vector = empty_tx_starknet_resources.to_gas_vector(
&versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas,
&gas_vector_computation_mode,
);
assert_eq!(empty_tx_gas_usage_vector, GasVector::default());

Expand All @@ -72,19 +76,23 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
None,
std::iter::empty(),
);
let code_gas_cost = versioned_constants.archival_data_gas_costs.gas_per_code_byte
* versioned_constants.get_l1_to_l2_gas_price_ratio()
let code_gas_cost = (versioned_constants.archival_data_gas_costs.gas_per_code_byte
* u128_from_usize(
(class_info.bytecode_length() + class_info.sierra_program_length())
* eth_gas_constants::WORD_WIDTH
+ class_info.abi_length(),
);
let manual_gas_vector =
GasVector { l1_gas: code_gas_cost.to_integer(), ..Default::default() };
))
.to_integer();
let manual_gas_vector = match gas_vector_computation_mode {
GasVectorComputationMode::NoL2Gas => GasVector::from_l1_gas(
(versioned_constants.get_l1_to_l2_gas_price_ratio() * code_gas_cost).to_integer(),
),
GasVectorComputationMode::All => GasVector::from_l2_gas(code_gas_cost),
};
let declare_gas_usage_vector = declare_tx_starknet_resources.to_gas_vector(
&versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas,
&gas_vector_computation_mode,
);
assert_eq!(manual_gas_vector, declare_gas_usage_vector);
}
Expand All @@ -110,17 +118,23 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
std::iter::empty(),
);
let calldata_and_signature_gas_cost =
versioned_constants.archival_data_gas_costs.gas_per_data_felt
* versioned_constants.get_l1_to_l2_gas_price_ratio()
* u128_from_usize(calldata_length + signature_length);
let manual_starknet_gas_usage = calldata_and_signature_gas_cost.to_integer();
let manual_gas_vector = GasVector { l1_gas: manual_starknet_gas_usage, ..Default::default() }
(versioned_constants.archival_data_gas_costs.gas_per_data_felt
* u128_from_usize(calldata_length + signature_length))
.to_integer();
let manual_starknet_gas_usage_vector = match gas_vector_computation_mode {
GasVectorComputationMode::NoL2Gas => GasVector::from_l1_gas(
(versioned_constants.get_l1_to_l2_gas_price_ratio() * calldata_and_signature_gas_cost)
.to_integer(),
),
GasVectorComputationMode::All => GasVector::from_l2_gas(calldata_and_signature_gas_cost),
};
let manual_gas_vector = manual_starknet_gas_usage_vector
+ deploy_account_tx_starknet_resources.get_state_changes_cost(use_kzg_da);

let deploy_account_gas_usage_vector = deploy_account_tx_starknet_resources.to_gas_vector(
&versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas,
&gas_vector_computation_mode,
);
assert_eq!(manual_gas_vector, deploy_account_gas_usage_vector);

Expand All @@ -138,26 +152,36 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
let l1_handler_gas_usage_vector = l1_handler_tx_starknet_resources.to_gas_vector(
&versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas,
&gas_vector_computation_mode,
);

// Manual calculation.
let message_segment_length = get_message_segment_length(&[], Some(l1_handler_payload_size));
let calldata_and_signature_gas_cost =
versioned_constants.archival_data_gas_costs.gas_per_data_felt
* versioned_constants.get_l1_to_l2_gas_price_ratio()
* u128_from_usize(l1_handler_payload_size + signature_length);
let manual_starknet_gas_usage = message_segment_length * eth_gas_constants::GAS_PER_MEMORY_WORD
(versioned_constants.archival_data_gas_costs.gas_per_data_felt
* u128_from_usize(l1_handler_payload_size + signature_length))
.to_integer();
let calldata_and_signature_gas_cost_vector = match gas_vector_computation_mode {
GasVectorComputationMode::NoL2Gas => GasVector::from_l1_gas(
(versioned_constants.get_l1_to_l2_gas_price_ratio() * calldata_and_signature_gas_cost)
.to_integer(),
),
GasVectorComputationMode::All => GasVector::from_l2_gas(calldata_and_signature_gas_cost),
};
let manual_starknet_l1_gas_usage = message_segment_length
* eth_gas_constants::GAS_PER_MEMORY_WORD
+ eth_gas_constants::GAS_PER_COUNTER_DECREASE
+ usize_from_u128(
get_consumed_message_to_l2_emissions_cost(Some(l1_handler_payload_size)).l1_gas,
)
.unwrap()
+ usize_from_u128(calldata_and_signature_gas_cost.to_integer()).unwrap();
.unwrap();
let manual_starknet_l1_gas_usage_vector =
GasVector::from_l1_gas(u128_from_usize(manual_starknet_l1_gas_usage));
let manual_sharp_gas_usage =
message_segment_length * eth_gas_constants::SHARP_GAS_PER_MEMORY_WORD;
let manual_gas_computation =
GasVector::from_l1_gas(u128_from_usize(manual_starknet_gas_usage + manual_sharp_gas_usage));
let manual_gas_computation = GasVector::from_l1_gas(u128_from_usize(manual_sharp_gas_usage))
+ manual_starknet_l1_gas_usage_vector
+ calldata_and_signature_gas_cost_vector;
assert_eq!(l1_handler_gas_usage_vector, manual_gas_computation);

// Any transaction with L2-to-L1 messages.
Expand Down Expand Up @@ -206,10 +230,11 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
let l2_to_l1_messages_gas_usage_vector = l2_to_l1_starknet_resources.to_gas_vector(
&versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas,
&gas_vector_computation_mode,
);

// Manual calculation.
// No L2 gas is used, so gas amount does not depend on gas vector computation mode.
let message_segment_length = get_message_segment_length(&l2_to_l1_payload_lengths, None);
let n_l2_to_l1_messages = l2_to_l1_payload_lengths.len();
let manual_starknet_gas_usage = message_segment_length * eth_gas_constants::GAS_PER_MEMORY_WORD
Expand Down Expand Up @@ -252,10 +277,11 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
let storage_writings_gas_usage_vector = storage_writes_starknet_resources.to_gas_vector(
&versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas,
&gas_vector_computation_mode,
);

// Manual calculation.
// No L2 gas is used, so gas amount does not depend on gas vector computation mode.
let manual_gas_computation =
storage_writes_starknet_resources.get_state_changes_cost(use_kzg_da);

Expand All @@ -281,7 +307,7 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
let gas_usage_vector = combined_cases_starknet_resources.to_gas_vector(
&versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas,
&gas_vector_computation_mode,
);

// Manual calculation.
Expand All @@ -303,22 +329,23 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
l1_data_gas: combined_cases_starknet_resources
.get_state_changes_cost(use_kzg_da)
.l1_data_gas,
..Default::default()
l2_gas: l1_handler_gas_usage_vector.l2_gas,
};

assert_eq!(expected_gas_vector, gas_usage_vector);
}

// Test that we exclude the fee token contract modification and adds the account’s balance change
// in the state changes.
// TODO(Aner, 21/01/24) modify for 4844 (taking blob_gas into account).
// TODO(Nimrod, 1/5/2024): Test regression w.r.t. all resources (including VM). (Only starknet
// resources are taken into account).
#[rstest]
fn test_calculate_tx_gas_usage(
max_l1_resource_bounds: ValidResourceBounds,
#[values(false, true)] use_kzg_da: bool,
#[values(GasVectorComputationMode::NoL2Gas, GasVectorComputationMode::All)]
gas_vector_computation_mode: GasVectorComputationMode,
) {
let max_resource_bounds = get_resource_bounds(&gas_vector_computation_mode);
let account_cairo_version = CairoVersion::Cairo0;
let test_contract_cairo_version = CairoVersion::Cairo0;
let block_context = &BlockContext::create_for_account_testing_with_kzg(use_kzg_da);
Expand All @@ -332,7 +359,7 @@ fn test_calculate_tx_gas_usage(
let account_tx = account_invoke_tx(invoke_tx_args! {
sender_address: account_contract_address,
calldata: create_trivial_calldata(test_contract.get_instance_address(0)),
resource_bounds: max_l1_resource_bounds,
resource_bounds: max_resource_bounds,
});
let calldata_length = account_tx.calldata_length();
let signature_length = account_tx.signature_length();
Expand Down Expand Up @@ -360,12 +387,12 @@ fn test_calculate_tx_gas_usage(
starknet_resources.to_gas_vector(
versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas
&gas_vector_computation_mode
),
tx_execution_info.receipt.resources.starknet_resources.to_gas_vector(
versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas
&gas_vector_computation_mode
)
);

Expand All @@ -382,7 +409,7 @@ fn test_calculate_tx_gas_usage(
);

let account_tx = account_invoke_tx(invoke_tx_args! {
resource_bounds: max_l1_resource_bounds,
resource_bounds: max_resource_bounds,
sender_address: account_contract_address,
calldata: execute_calldata,
nonce: nonce!(1_u8),
Expand All @@ -401,26 +428,30 @@ fn test_calculate_tx_gas_usage(
n_modified_contracts,
n_compiled_class_hash_updates: 0,
};
let execution_call_info =
&tx_execution_info.execute_call_info.expect("Execution call info should exist.");
let execution_call_info_iter = vec![execution_call_info].into_iter();

let starknet_resources = StarknetResources::new(
calldata_length,
signature_length,
0,
state_changes_count,
None,
std::iter::empty(),
// The transfer entrypoint emits an event - pass the call info to count its resources.
execution_call_info_iter,
);

assert_eq!(
starknet_resources.to_gas_vector(
versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas
&gas_vector_computation_mode
),
tx_execution_info.receipt.resources.starknet_resources.to_gas_vector(
versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas
&gas_vector_computation_mode
)
);
}
41 changes: 40 additions & 1 deletion crates/blockifier/src/transaction/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ use crate::test_utils::{
};
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::constants;
use crate::transaction::objects::{FeeType, TransactionExecutionInfo, TransactionExecutionResult};
use crate::transaction::objects::{
FeeType,
GasVectorComputationMode,
TransactionExecutionInfo,
TransactionExecutionResult,
};
use crate::transaction::transaction_types::TransactionType;
use crate::transaction::transactions::{ExecutableTransaction, InvokeTransaction};

Expand Down Expand Up @@ -92,6 +97,22 @@ pub fn max_l1_resource_bounds() -> ValidResourceBounds {
l1_resource_bounds(MAX_L1_GAS_AMOUNT, MAX_L1_GAS_PRICE)
}

pub fn get_resource_bounds(computation_mode: &GasVectorComputationMode) -> ValidResourceBounds {
match computation_mode {
GasVectorComputationMode::NoL2Gas => {
l1_resource_bounds(MAX_L1_GAS_AMOUNT, MAX_L1_GAS_PRICE)
}
GasVectorComputationMode::All => get_all_resource_bounds(
MAX_L1_GAS_AMOUNT,
MAX_L1_GAS_PRICE,
DEFAULT_L2_GAS_MAX_AMOUNT,
DEFAULT_STRK_L2_GAS_PRICE,
DEFAULT_L1_DATA_GAS_MAX_AMOUNT,
DEFAULT_STRK_L1_DATA_GAS_PRICE,
),
}
}

#[fixture]
pub fn block_context() -> BlockContext {
BlockContext::create_for_account_testing()
Expand Down Expand Up @@ -315,6 +336,24 @@ pub fn all_resource_bounds(
#[default(DEFAULT_STRK_L2_GAS_PRICE)] l2_max_price: u128,
#[default(DEFAULT_L1_DATA_GAS_MAX_AMOUNT)] l1_data_max_amount: u64,
#[default(DEFAULT_STRK_L1_DATA_GAS_PRICE)] l1_data_max_price: u128,
) -> ValidResourceBounds {
get_all_resource_bounds(
l1_max_amount,
l1_max_price,
l2_max_amount,
l2_max_price,
l1_data_max_amount,
l1_data_max_price,
)
}

fn get_all_resource_bounds(
l1_max_amount: u64,
l1_max_price: u128,
l2_max_amount: u64,
l2_max_price: u128,
l1_data_max_amount: u64,
l1_data_max_price: u128,
) -> ValidResourceBounds {
ValidResourceBounds::AllResources(AllResourceBounds {
l1_gas: ResourceBounds { max_amount: l1_max_amount, max_price_per_unit: l1_max_price },
Expand Down
Loading
Loading