Skip to content

Commit

Permalink
build(fee): tx_resources depend on resource bounds signature
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-starkware committed Aug 15, 2024
1 parent 991209e commit 18c1d34
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 24 deletions.
1 change: 1 addition & 0 deletions crates/blockifier/src/fee/actual_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl TransactionReceipt {
let gas = tx_resources.to_gas_vector(
&tx_context.block_context.versioned_constants,
tx_context.block_context.block_info.use_kzg_da,
tx_context.tx_info.has_l2_gas_bounds(),
)?;

// L1 handler transactions are not charged an L2 fee but it is compared to the L1 fee.
Expand Down
2 changes: 2 additions & 0 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ impl TransactionResources {
&self,
block_context: &BlockContext,
fee_type: &FeeType,
include_l2_gas: bool,
) -> TransactionFeeResult<Fee> {
let gas_vector = self.to_gas_vector(
&block_context.versioned_constants,
block_context.block_info.use_kzg_da,
include_l2_gas,
)?;
Ok(get_fee_by_gas_vector(&block_context.block_info, gas_vector, fee_type))
}
Expand Down
14 changes: 12 additions & 2 deletions crates/blockifier/src/transaction/account_transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,14 +930,19 @@ fn test_max_fee_to_max_steps_conversion(
nonce: nonce_manager.next(account_address),
});
let tx_context1 = Arc::new(block_context.to_tx_context(&account_tx1));
let has_l2_gas_bounds = tx_context1.tx_info.has_l2_gas_bounds();
let execution_context1 = EntryPointExecutionContext::new_invoke(tx_context1, true).unwrap();
let max_steps_limit1 = execution_context1.vm_run_resources.get_n_steps();
let tx_execution_info1 = account_tx1.execute(&mut state, &block_context, true, true).unwrap();
let n_steps1 = tx_execution_info1.receipt.resources.vm_resources.n_steps;
let gas_used_vector1 = tx_execution_info1
.receipt
.resources
.to_gas_vector(&block_context.versioned_constants, block_context.block_info.use_kzg_da)
.to_gas_vector(
&block_context.versioned_constants,
block_context.block_info.use_kzg_da,
has_l2_gas_bounds,
)
.unwrap();

// Second invocation of `with_arg` gets twice the pre-calculated actual fee as max_fee.
Expand All @@ -950,14 +955,19 @@ fn test_max_fee_to_max_steps_conversion(
nonce: nonce_manager.next(account_address),
});
let tx_context2 = Arc::new(block_context.to_tx_context(&account_tx2));
let has_l2_gas_bounds = tx_context2.tx_info.has_l2_gas_bounds();
let execution_context2 = EntryPointExecutionContext::new_invoke(tx_context2, true).unwrap();
let max_steps_limit2 = execution_context2.vm_run_resources.get_n_steps();
let tx_execution_info2 = account_tx2.execute(&mut state, &block_context, true, true).unwrap();
let n_steps2 = tx_execution_info2.receipt.resources.vm_resources.n_steps;
let gas_used_vector2 = tx_execution_info2
.receipt
.resources
.to_gas_vector(&block_context.versioned_constants, block_context.block_info.use_kzg_da)
.to_gas_vector(
&block_context.versioned_constants,
block_context.block_info.use_kzg_da,
has_l2_gas_bounds,
)
.unwrap();

// Test that steps limit doubles as max_fee doubles, but actual consumed steps and fee remains.
Expand Down
36 changes: 30 additions & 6 deletions crates/blockifier/src/transaction/execution_flavors_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::test_utils::{
create_trivial_calldata,
get_syscall_resources,
get_tx_resources,
include_l2_gas,
u64_from_usize,
CairoVersion,
NonceManager,
Expand Down Expand Up @@ -124,12 +125,17 @@ fn check_gas_and_fee(
expected_actual_gas: u64,
expected_actual_fee: Fee,
expected_cost_of_resources: Fee,
include_l2_gas: bool,
) {
assert_eq!(
tx_execution_info
.receipt
.resources
.to_gas_vector(&block_context.versioned_constants, block_context.block_info.use_kzg_da)
.to_gas_vector(
&block_context.versioned_constants,
block_context.block_info.use_kzg_da,
include_l2_gas
)
.unwrap()
.l1_gas,
expected_actual_gas.into()
Expand All @@ -139,7 +145,11 @@ fn check_gas_and_fee(
// Future compatibility: resources other than the L1 gas usage may affect the fee (currently,
// `calculate_tx_fee` is simply the result of `calculate_tx_gas_usage_vector` times gas price).
assert_eq!(
tx_execution_info.receipt.resources.calculate_tx_fee(block_context, fee_type).unwrap(),
tx_execution_info
.receipt
.resources
.calculate_tx_fee(block_context, fee_type, include_l2_gas)
.unwrap(),
expected_cost_of_resources
);
}
Expand All @@ -153,7 +163,8 @@ fn recurse_calldata(contract_address: ContractAddress, fail: bool, depth: u32) -
}

/// Test simulate / validate / charge_fee flag combinations in pre-validation stage.
#[rstest]
#[allow(clippy::too_many_arguments)]
#[rstest_reuse::apply(include_l2_gas)]
#[case(TransactionVersion::ONE, FeeType::Eth, true)]
#[case(TransactionVersion::THREE, FeeType::Strk, false)]
fn test_simulate_validate_charge_fee_pre_validate(
Expand All @@ -165,6 +176,7 @@ fn test_simulate_validate_charge_fee_pre_validate(
#[case] version: TransactionVersion,
#[case] fee_type: FeeType,
#[case] is_deprecated: bool,
has_l2_gas: bool,
) {
let block_context = BlockContext::create_for_account_testing();
let max_fee = Fee(MAX_FEE);
Expand Down Expand Up @@ -238,6 +250,7 @@ fn test_simulate_validate_charge_fee_pre_validate(
actual_gas_used,
actual_fee,
actual_fee,
has_l2_gas,
);
} else {
nonce_manager.rollback(account_address);
Expand Down Expand Up @@ -282,6 +295,7 @@ fn test_simulate_validate_charge_fee_pre_validate(
actual_gas_used,
actual_fee,
actual_fee,
has_l2_gas,
);
} else {
nonce_manager.rollback(account_address);
Expand Down Expand Up @@ -322,6 +336,7 @@ fn test_simulate_validate_charge_fee_pre_validate(
actual_gas_used,
actual_fee,
actual_fee,
has_l2_gas,
);
} else {
nonce_manager.rollback(account_address);
Expand All @@ -338,7 +353,7 @@ fn test_simulate_validate_charge_fee_pre_validate(
}

/// Test simulate / validate / charge_fee flag combinations in (fallible) validation stage.
#[rstest]
#[rstest_reuse::apply(include_l2_gas)]
#[case(TransactionVersion::ONE, FeeType::Eth)]
#[case(TransactionVersion::THREE, FeeType::Strk)]
fn test_simulate_validate_charge_fee_fail_validate(
Expand All @@ -350,6 +365,7 @@ fn test_simulate_validate_charge_fee_fail_validate(
#[case] version: TransactionVersion,
#[case] fee_type: FeeType,
max_resource_bounds: ResourceBoundsMapping,
has_l2_gas: bool,
) {
let block_context = BlockContext::create_for_account_testing();
let max_fee = Fee(MAX_FEE);
Expand Down Expand Up @@ -391,6 +407,7 @@ fn test_simulate_validate_charge_fee_fail_validate(
actual_gas_used,
actual_fee,
actual_fee,
has_l2_gas,
);
} else {
assert!(
Expand All @@ -400,7 +417,7 @@ fn test_simulate_validate_charge_fee_fail_validate(
}

/// Test simulate / validate / charge_fee flag combinations during execution.
#[rstest]
#[rstest_reuse::apply(include_l2_gas)]
#[case(TransactionVersion::ONE, FeeType::Eth)]
#[case(TransactionVersion::THREE, FeeType::Strk)]
fn test_simulate_validate_charge_fee_mid_execution(
Expand All @@ -412,6 +429,7 @@ fn test_simulate_validate_charge_fee_mid_execution(
#[case] version: TransactionVersion,
#[case] fee_type: FeeType,
max_resource_bounds: ResourceBoundsMapping,
has_l2_gas: bool,
) {
let block_context = BlockContext::create_for_account_testing();
let chain_info = &block_context.chain_info;
Expand Down Expand Up @@ -462,6 +480,7 @@ fn test_simulate_validate_charge_fee_mid_execution(
revert_gas_used,
revert_fee,
revert_fee,
has_l2_gas,
);
let current_balance = check_balance(
current_balance,
Expand Down Expand Up @@ -513,6 +532,7 @@ fn test_simulate_validate_charge_fee_mid_execution(
// Complete resources used are reported as receipt.resources; but only the
// charged final fee is shown in actual_fee.
if charge_fee { limited_fee } else { unlimited_fee },
has_l2_gas,
);
let current_balance =
check_balance(current_balance, &state, account_address, chain_info, &fee_type, charge_fee);
Expand Down Expand Up @@ -553,11 +573,12 @@ fn test_simulate_validate_charge_fee_mid_execution(
block_limit_gas,
block_limit_fee,
block_limit_fee,
has_l2_gas,
);
check_balance(current_balance, &state, account_address, chain_info, &fee_type, charge_fee);
}

#[rstest]
#[rstest_reuse::apply(include_l2_gas)]
#[case(TransactionVersion::ONE, FeeType::Eth, true)]
#[case(TransactionVersion::THREE, FeeType::Strk, false)]
fn test_simulate_validate_charge_fee_post_execution(
Expand All @@ -569,6 +590,7 @@ fn test_simulate_validate_charge_fee_post_execution(
#[case] version: TransactionVersion,
#[case] fee_type: FeeType,
#[case] is_deprecated: bool,
has_l2_gas: bool,
) {
let block_context = BlockContext::create_for_account_testing();
let gas_price = block_context.block_info.gas_prices.get_l1_gas_price_by_fee_type(&fee_type);
Expand Down Expand Up @@ -641,6 +663,7 @@ fn test_simulate_validate_charge_fee_post_execution(
if charge_fee { revert_gas_usage } else { unlimited_gas_used },
if charge_fee { just_not_enough_fee_bound } else { unlimited_fee },
if charge_fee { revert_fee } else { unlimited_fee },
has_l2_gas,
);
let current_balance =
check_balance(current_balance, &state, account_address, chain_info, &fee_type, charge_fee);
Expand Down Expand Up @@ -705,6 +728,7 @@ fn test_simulate_validate_charge_fee_post_execution(
if charge_fee { fail_actual_gas } else { success_actual_gas },
actual_fee,
if charge_fee { fail_actual_fee } else { actual_fee },
has_l2_gas,
);
check_balance(
current_balance,
Expand Down
10 changes: 9 additions & 1 deletion crates/blockifier/src/transaction/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ impl TransactionInfo {
TransactionInfo::Deprecated(context) => Ok(context.max_fee != Fee(0)),
}
}

pub fn has_l2_gas_bounds(&self) -> bool {
match self {
TransactionInfo::Current(context) => context.resource_bounds.0.len() == 3,
TransactionInfo::Deprecated(_) => false,
}
}
}

impl HasRelatedFeeType for TransactionInfo {
Expand Down Expand Up @@ -470,8 +477,9 @@ impl TransactionResources {
&self,
versioned_constants: &VersionedConstants,
use_kzg_da: bool,
include_l2_gas: bool,
) -> TransactionFeeResult<GasVector> {
Ok(self.starknet_resources.to_gas_vector(versioned_constants, use_kzg_da, false)
Ok(self.starknet_resources.to_gas_vector(versioned_constants, use_kzg_da, include_l2_gas)
+ calculate_l1_gas_by_vm_usage(
versioned_constants,
&self.vm_resources,
Expand Down
6 changes: 5 additions & 1 deletion crates/blockifier/src/transaction/post_execution_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ fn test_revert_on_resource_overuse(
let actual_gas_usage: u64 = execution_info_measure
.receipt
.resources
.to_gas_vector(&block_context.versioned_constants, block_context.block_info.use_kzg_da)
.to_gas_vector(
&block_context.versioned_constants,
block_context.block_info.use_kzg_da,
false, // TODO(Nimrod): Derive this value from `max_resource_bounds`.
)
.unwrap()
.l1_gas
.try_into()
Expand Down
49 changes: 35 additions & 14 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,11 @@ fn test_invoke_tx(

// Build expected fee transfer call info.
let fee_type = &tx_context.tx_info.fee_type();
let expected_actual_fee =
actual_execution_info.receipt.resources.calculate_tx_fee(block_context, fee_type).unwrap();
let expected_actual_fee = actual_execution_info
.receipt
.resources
.calculate_tx_fee(block_context, fee_type, tx_context.tx_info.has_l2_gas_bounds())
.unwrap();
let expected_fee_transfer_call_info = expected_fee_transfer_call_info(
&tx_context,
sender_address,
Expand Down Expand Up @@ -507,7 +510,11 @@ fn test_invoke_tx(
);

let total_gas = expected_actual_resources
.to_gas_vector(&block_context.versioned_constants, block_context.block_info.use_kzg_da)
.to_gas_vector(
&block_context.versioned_constants,
block_context.block_info.use_kzg_da,
tx_context.tx_info.has_l2_gas_bounds(),
)
.unwrap();

let expected_execution_info = TransactionExecutionInfo {
Expand Down Expand Up @@ -1193,8 +1200,11 @@ fn test_declare_tx(
);

// Build expected fee transfer call info.
let expected_actual_fee =
actual_execution_info.receipt.resources.calculate_tx_fee(block_context, fee_type).unwrap();
let expected_actual_fee = actual_execution_info
.receipt
.resources
.calculate_tx_fee(block_context, fee_type, tx_context.tx_info.has_l2_gas_bounds())
.unwrap();
let expected_fee_transfer_call_info = expected_fee_transfer_call_info(
tx_context,
sender_address,
Expand Down Expand Up @@ -1223,8 +1233,9 @@ fn test_declare_tx(
use_kzg_da,
);

let expected_total_gas =
expected_actual_resources.to_gas_vector(versioned_constants, use_kzg_da).unwrap();
let expected_total_gas = expected_actual_resources
.to_gas_vector(versioned_constants, use_kzg_da, tx_context.tx_info.has_l2_gas_bounds())
.unwrap();

let expected_execution_info = TransactionExecutionInfo {
validate_call_info: expected_validate_call_info,
Expand Down Expand Up @@ -1354,8 +1365,11 @@ fn test_deploy_account_tx(
});

// Build expected fee transfer call info.
let expected_actual_fee =
actual_execution_info.receipt.resources.calculate_tx_fee(block_context, fee_type).unwrap();
let expected_actual_fee = actual_execution_info
.receipt
.resources
.calculate_tx_fee(block_context, fee_type, tx_context.tx_info.has_l2_gas_bounds())
.unwrap();
let expected_fee_transfer_call_info = expected_fee_transfer_call_info(
tx_context,
deployed_account_address,
Expand Down Expand Up @@ -1392,7 +1406,11 @@ fn test_deploy_account_tx(
);

let expected_total_gas = actual_resources
.to_gas_vector(&block_context.versioned_constants, block_context.block_info.use_kzg_da)
.to_gas_vector(
&block_context.versioned_constants,
block_context.block_info.use_kzg_da,
tx_context.tx_info.has_l2_gas_bounds(),
)
.unwrap();

let expected_execution_info = TransactionExecutionInfo {
Expand Down Expand Up @@ -1907,7 +1925,7 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
);

let total_gas = expected_tx_resources
.to_gas_vector(versioned_constants, block_context.block_info.use_kzg_da)
.to_gas_vector(versioned_constants, block_context.block_info.use_kzg_da, false)
.unwrap();

// Build the expected execution info.
Expand Down Expand Up @@ -1941,9 +1959,12 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
let tx_no_fee = L1HandlerTransaction::create_for_testing(Fee(0), contract_address);
let error = tx_no_fee.execute(state, block_context, true, true).unwrap_err();
// Today, we check that the paid_fee is positive, no matter what was the actual fee.
let expected_actual_fee =
(expected_execution_info.receipt.resources.calculate_tx_fee(block_context, &FeeType::Eth))
.unwrap();
let expected_actual_fee = (expected_execution_info.receipt.resources.calculate_tx_fee(
block_context,
&FeeType::Eth,
false,
))
.unwrap();
assert_matches!(
error,
TransactionExecutionError::TransactionFeeError(
Expand Down

0 comments on commit 18c1d34

Please sign in to comment.