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

Commit

Permalink
feat(fee): set the resources slope values (#1363)
Browse files Browse the repository at this point in the history
  • Loading branch information
noaov1 committed Jan 31, 2024
1 parent 8a0e0e9 commit 7a5c141
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
15 changes: 9 additions & 6 deletions crates/blockifier/src/fee/os_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,14 @@ fn os_resources() -> serde_json::Value {
"n_memory_holes": 82,
"n_steps": 3798
},
// Calculation of transaction hash + contract address + memcpy.
"calldata_factor":{
"builtin_instance_counter": {
"pedersen_builtin": 0,
"pedersen_builtin": 2,
"range_check_builtin": 0
},
"n_memory_holes": 0,
"n_steps": 0
"n_steps": 21
},
},
"InvokeFunction": {
Expand All @@ -275,13 +276,14 @@ fn os_resources() -> serde_json::Value {
"n_memory_holes": 68,
"n_steps": 3549
},
// Calculation of transaction hash.
"calldata_factor": {
"builtin_instance_counter": {
"pedersen_builtin": 0,
"pedersen_builtin": 1,
"range_check_builtin": 0
},
"n_memory_holes": 0,
"n_steps": 0
"n_steps": 8
}
},
"L1Handler": {
Expand All @@ -293,13 +295,14 @@ fn os_resources() -> serde_json::Value {
"n_memory_holes": 0,
"n_steps": 1157
},
// Calculation of transaction hash + memcpy.
"calldata_factor": {
"builtin_instance_counter": {
"pedersen_builtin": 0,
"pedersen_builtin": 1,
"range_check_builtin": 0
},
"n_memory_holes": 0,
"n_steps": 0
"n_steps": 13
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ fn test_max_fee_to_max_steps_conversion(
) {
let TestInitData { mut state, account_address, contract_address, mut nonce_manager } =
create_test_init_data(&block_context.chain_info, CairoVersion::Cairo0);
let actual_gas_used = 6108;
let actual_gas_used = 6140;
let actual_gas_used_as_u128: u128 = actual_gas_used.into();
let actual_fee = actual_gas_used_as_u128 * 100000000000;
let actual_strk_gas_price =
Expand Down
16 changes: 8 additions & 8 deletions crates/blockifier/src/transaction/execution_flavors_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn test_simulate_validate_charge_fee_pre_validate(
);

// Second scenario: minimal fee not covered. Actual fee is precomputed.
let (actual_gas_used, actual_fee) = gas_and_fee(6095, validate, &fee_type);
let (actual_gas_used, actual_fee) = gas_and_fee(6127, validate, &fee_type);
let result = account_invoke_tx(invoke_tx_args! {
max_fee: Fee(10),
resource_bounds: l1_resource_bounds(10, 10),
Expand Down Expand Up @@ -329,7 +329,7 @@ fn test_simulate_validate_charge_fee_fail_validate(
} = create_flavors_test_state(&block_context.chain_info, cairo_version);

// Validation scenario: fallible validation.
let (actual_gas_used, actual_fee) = gas_and_fee(30830, validate, &fee_type);
let (actual_gas_used, actual_fee) = gas_and_fee(30854, validate, &fee_type);
let result = account_invoke_tx(invoke_tx_args! {
max_fee,
resource_bounds: l1_resource_bounds(MAX_L1_GAS_AMOUNT, MAX_L1_GAS_PRICE),
Expand Down Expand Up @@ -407,7 +407,7 @@ fn test_simulate_validate_charge_fee_mid_execution(
};

// First scenario: logic error. Should result in revert; actual fee should be shown.
let (revert_gas_used, revert_fee) = gas_and_fee(5312, validate, &fee_type);
let (revert_gas_used, revert_fee) = gas_and_fee(5344, validate, &fee_type);
let tx_execution_info = account_invoke_tx(invoke_tx_args! {
calldata: recurse_calldata(test_contract_address, true, 3),
nonce: nonce_manager.next(account_address),
Expand Down Expand Up @@ -439,7 +439,7 @@ fn test_simulate_validate_charge_fee_mid_execution(
// If `charge_fee` is true, execution is limited by sender bounds, so less resources will be
// used. Otherwise, execution is limited by block bounds, so more resources will be used.
let (limited_gas_used, limited_fee) = gas_and_fee(7721, validate, &fee_type);
let (unlimited_gas_used, unlimited_fee) = gas_and_fee(10087, validate, &fee_type);
let (unlimited_gas_used, unlimited_fee) = gas_and_fee(10119, validate, &fee_type);
let tx_execution_info = account_invoke_tx(invoke_tx_args! {
max_fee: fee_bound,
resource_bounds: l1_resource_bounds(gas_bound, gas_price),
Expand Down Expand Up @@ -559,8 +559,8 @@ fn test_simulate_validate_charge_fee_post_execution(
gas_and_fee(base_gas_bound, validate, &fee_type);
// `__validate__` and overhead resources + number of reverted steps, comes out slightly more
// than the gas bound.
let (revert_gas_usage, revert_fee) = gas_and_fee(9323, validate, &fee_type);
let (unlimited_gas_used, unlimited_fee) = gas_and_fee(10087, validate, &fee_type);
let (revert_gas_usage, revert_fee) = gas_and_fee(9355, validate, &fee_type);
let (unlimited_gas_used, unlimited_fee) = gas_and_fee(10119, validate, &fee_type);
let tx_execution_info = account_invoke_tx(invoke_tx_args! {
max_fee: just_not_enough_fee_bound,
resource_bounds: l1_resource_bounds(just_not_enough_gas_bound, gas_price),
Expand Down Expand Up @@ -599,8 +599,8 @@ fn test_simulate_validate_charge_fee_post_execution(

// Second scenario: balance too low.
// Execute a transfer, and make sure we get the expected result.
let (success_actual_gas, actual_fee) = gas_and_fee(8585, validate, &fee_type);
let (fail_actual_gas, fail_actual_cost) = gas_and_fee(5833, validate, &fee_type);
let (success_actual_gas, actual_fee) = gas_and_fee(8633, validate, &fee_type);
let (fail_actual_gas, fail_actual_cost) = gas_and_fee(5881, validate, &fee_type);
assert!(stark_felt!(actual_fee) < current_balance);
let transfer_amount = stark_felt_to_felt(current_balance) - Felt252::from(actual_fee.0 / 2);
let recipient = stark_felt!(7_u8);
Expand Down
14 changes: 8 additions & 6 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ fn default_invoke_tx_args(
#[case::with_cairo0_account(
ExpectedResultTestInvokeTx{
range_check: 102,
n_steps: 4464,
n_steps: 4496,
vm_resources: VmExecutionResources {
n_steps: 62,
n_memory_holes: 0,
Expand All @@ -310,7 +310,7 @@ fn default_invoke_tx_args(
#[case::with_cairo1_account(
ExpectedResultTestInvokeTx{
range_check: 115,
n_steps: 4917,
n_steps: 4949,
vm_resources: VmExecutionResources {
n_steps: 284,
n_memory_holes: 1,
Expand Down Expand Up @@ -340,6 +340,7 @@ fn test_invoke_tx(
// Extract invoke transaction fields for testing, as it is consumed when creating an account
// transaction.
let calldata = Calldata(Arc::clone(&invoke_tx.calldata().0));
let calldata_length = &invoke_tx.calldata().0.len();
let sender_address = invoke_tx.sender_address();

let account_tx = AccountTransaction::Invoke(invoke_tx);
Expand Down Expand Up @@ -426,7 +427,7 @@ fn test_invoke_tx(
usize_from_u128(expected_blob_gas_usage).unwrap(),
),
(abi_constants::L1_GAS_USAGE.to_string(), usize_from_u128(expected_gas_usage).unwrap()),
(HASH_BUILTIN_NAME.to_string(), 16),
(HASH_BUILTIN_NAME.to_string(), 16 + calldata_length),
(RANGE_CHECK_BUILTIN_NAME.to_string(), expected_arguments.range_check),
(abi_constants::N_STEPS_RESOURCE.to_string(), expected_arguments.n_steps),
])),
Expand Down Expand Up @@ -1685,6 +1686,7 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
let value = StarkFelt::from_u128(0x44);
let calldata = calldata![from_address, key, value];
let tx = l1_handler_tx(&calldata, Fee(1));
let payload_size = tx.payload_size();

let actual_execution_info = tx.execute(state, block_context, true, true).unwrap();

Expand Down Expand Up @@ -1723,8 +1725,8 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
};

let expected_resource_mapping = ResourcesMapping(HashMap::from([
(HASH_BUILTIN_NAME.to_string(), 11),
(abi_constants::N_STEPS_RESOURCE.to_string(), 1390),
(HASH_BUILTIN_NAME.to_string(), 11 + payload_size),
(abi_constants::N_STEPS_RESOURCE.to_string(), 1416),
(RANGE_CHECK_BUILTIN_NAME.to_string(), 23),
(abi_constants::L1_GAS_USAGE.to_string(), expected_gas_usage),
(abi_constants::BLOB_GAS_USAGE.to_string(), expected_blob_gas_usage),
Expand Down Expand Up @@ -1762,7 +1764,7 @@ fn test_l1_handler(#[values(false, true)] use_kzg_da: bool) {
error,
TransactionExecutionError::TransactionFeeError(
TransactionFeeError::InsufficientL1Fee { paid_fee, actual_fee, })
if paid_fee == Fee(0) && actual_fee == Fee(1741300000000000)
if paid_fee == Fee(0) && actual_fee == Fee(1743900000000000)
);
}

Expand Down

0 comments on commit 7a5c141

Please sign in to comment.