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

[0.13] Update post-execution fee checks #1204

Merged
merged 61 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
d0b2590
Add GasPricess & FeeTokenAddresses structs
fmoletta Jan 19, 2024
6327d7a
Update code
fmoletta Jan 19, 2024
3ea4172
Merge branch 'main' of github.com:lambdaclass/starknet_in_rust into u…
fmoletta Jan 19, 2024
e24dacd
RpcStateReader::get_gas_price fetch both
fmoletta Jan 19, 2024
5a4d984
Update code
fmoletta Jan 19, 2024
c1d4bd0
Clippy
fmoletta Jan 19, 2024
81a61f2
Fix comment
fmoletta Jan 19, 2024
8d6168d
Add VersionSpecificAccountTxFields struct
fmoletta Jan 22, 2024
badc6a5
Integrate into TransactionExecutionContext
fmoletta Jan 22, 2024
2bb0091
Replace max_fee field with account_tx_fields for InvokeFunction tx
fmoletta Jan 22, 2024
93d0d61
Check tx account fields version when creating Invoke tx
fmoletta Jan 22, 2024
bcaab19
Replace max_fee field with account_tx_fields for DeclareV3 tx
fmoletta Jan 22, 2024
7f44d13
Replace max_fee field with account_tx_fields for DeployAccount tx
fmoletta Jan 22, 2024
e7de24d
Update get_transaction_context for Txs not affected by this change
fmoletta Jan 22, 2024
444e288
Fixes
fmoletta Jan 22, 2024
a832b62
Fixes
fmoletta Jan 22, 2024
d08c64a
Fixes
fmoletta Jan 22, 2024
9815fa1
Fix handling of ignore_max_fee
fmoletta Jan 22, 2024
5bb3c5f
Improve placeholder method
fmoletta Jan 22, 2024
127da88
Handle TODOs
fmoletta Jan 22, 2024
0099b22
Implement get_onchain_data_cost
fmoletta Jan 23, 2024
a6940c4
Refactor
fmoletta Jan 23, 2024
8ae7451
Generalize fn
fmoletta Jan 23, 2024
791d45e
Start implementing check_fee_bounds
fmoletta Jan 23, 2024
d2aa130
Finish implementing check_fee_bounds
fmoletta Jan 23, 2024
f217d86
Replace old code
fmoletta Jan 23, 2024
c16c449
Fixes
fmoletta Jan 23, 2024
49c0830
Implement FeeType for VersionSpecificAccountTxFields
fmoletta Jan 23, 2024
f7b7400
Update other txs
fmoletta Jan 23, 2024
5c265b1
Clippy
fmoletta Jan 23, 2024
ccdfac2
Add test
fmoletta Jan 23, 2024
8070874
Simplify
fmoletta Jan 23, 2024
03f9487
Move test
fmoletta Jan 23, 2024
63f0ad3
Add revert_if_actual_cost_exceeds_bounds
fmoletta Jan 24, 2024
75a8d9a
Refactor & Repurpose
fmoletta Jan 24, 2024
a47e160
Implement run_post_execution_fee_checks
fmoletta Jan 24, 2024
255cee9
Apply new function to InvokeTransaction test
fmoletta Jan 24, 2024
f7bf94c
Fixes & improvements
fmoletta Jan 24, 2024
59ef846
Clippy
fmoletta Jan 24, 2024
f1b61f6
Add post execution fee checks to DeclareV2
fmoletta Jan 24, 2024
b13fc20
Use FeeType in charge_fee
fmoletta Jan 24, 2024
d5831fd
Use execute_fee_transfer instead of charge_fee in Invoke tx + fixes
fmoletta Jan 24, 2024
e6f3ada
Clean uneeded imports from charge_fee
fmoletta Jan 24, 2024
fc19746
Simplify
fmoletta Jan 24, 2024
8b9ec3e
Calculate fee outside of charge_fee to avoid calculating twice
fmoletta Jan 24, 2024
3c3250b
Remove revertible behaviour from DeployAccount + fixes
fmoletta Jan 24, 2024
163731b
Run checks on DeclareV0 also
fmoletta Jan 24, 2024
8d70cfc
Fix
fmoletta Jan 24, 2024
949a67c
Update test
fmoletta Jan 24, 2024
2959894
Use fee_type when counting storage changes
fmoletta Jan 25, 2024
4958563
Fix
fmoletta Jan 25, 2024
b0ab6a4
Fix test logic
fmoletta Jan 25, 2024
a86e710
Fix test logic
fmoletta Jan 25, 2024
9e492a0
Fix test logic
fmoletta Jan 25, 2024
2b40fef
Clippy
fmoletta Jan 25, 2024
1407c4c
Fix comments + update visibility
fmoletta Jan 25, 2024
bbf5460
Fix comment
fmoletta Jan 25, 2024
87c27a2
Merge branch 'main' of github.com:lambdaclass/starknet_in_rust into u…
fmoletta Jan 26, 2024
7f1308c
Merge branch 'main' into update-check-fee-balance
fmoletta Jan 30, 2024
67af8b7
Merge branch 'update-check-fee-balance' into update-post-exec-tx-checks
fmoletta Jan 30, 2024
dc72ed7
Merge branch 'main' of github.com:lambdaclass/starknet_in_rust into u…
fmoletta Jan 31, 2024
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
Prev Previous commit
Next Next commit
Simplify
  • Loading branch information
fmoletta committed Jan 24, 2024
commit fc19746e653f9b671c9e1cd59352d0e30ff94209
4 changes: 0 additions & 4 deletions src/definitions/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ pub(crate) const CONSUMED_MSG_TO_L2_ENCODED_DATA_SIZE: usize =
pub(crate) const LOG_MSG_TO_L1_ENCODED_DATA_SIZE: usize =
(L2_TO_L1_MSG_HEADER_SIZE + 1) - LOG_MSG_TO_L1_N_TOPICS;

/// Fee factor used for the final fee calculation:
/// actual_fee = min(max_fee, consumed_resources) * FEE_FACTOR
pub(crate) const FEE_FACTOR: u128 = 1;

/// The (empirical) L1 gas cost of each Cairo step.
pub(crate) const N_STEPS_FEE_WEIGHT: f64 = 0.01;

Expand Down
70 changes: 2 additions & 68 deletions src/transaction/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
use crate::{
definitions::{
block_context::{BlockContext, FeeType},
constants::{FEE_FACTOR, INITIAL_GAS_COST, TRANSFER_ENTRY_POINT_SELECTOR},
constants::{INITIAL_GAS_COST, TRANSFER_ENTRY_POINT_SELECTOR},
},
execution::{
execution_entry_point::{ExecutionEntryPoint, ExecutionResult},
Expand Down Expand Up @@ -49,61 +49,6 @@ pub(crate) fn execute_fee_transfer<S: StateReader, C: ContractClassCache>(
#[cfg(feature = "cairo-native")] program_cache: Option<
Rc<RefCell<ProgramCache<'_, ClassHash>>>,
>,
) -> Result<CallInfo, TransactionError> {
if actual_fee > tx_execution_context.account_tx_fields.max_fee() {
return Err(TransactionError::ActualFeeExceedsMaxFee(
actual_fee,
tx_execution_context.account_tx_fields.max_fee(),
));
}

let fee_token_address = block_context.starknet_os_config.fee_token_address.clone();

let calldata = [
block_context.block_info.sequencer_address.0,
Felt252::from(actual_fee), // U256.low
0.into(), // U256.high
]
.to_vec();

let fee_transfer_call = ExecutionEntryPoint::new(
fee_token_address.get_by_fee_type(&FeeType::Eth).clone(),
calldata,
*TRANSFER_ENTRY_POINT_SELECTOR,
tx_execution_context.account_contract_address.clone(),
EntryPointType::External,
Some(CallType::Call),
None,
INITIAL_GAS_COST,
);

let mut resources_manager = ExecutionResourcesManager::default();
let ExecutionResult { call_info, .. } = fee_transfer_call
.execute(
state,
block_context,
&mut resources_manager,
tx_execution_context,
false,
block_context.invoke_tx_max_n_steps,
#[cfg(feature = "cairo-native")]
program_cache,
)
.map_err(|e| TransactionError::FeeTransferError(Box::new(e)))?;

call_info.ok_or(TransactionError::CallInfoIsNone)
}

/// Transfers the amount actual_fee from the caller account to the sequencer.
/// Returns the resulting CallInfo of the transfer call.
pub(crate) fn execute_fee_transfer_updated<S: StateReader, C: ContractClassCache>(
state: &mut CachedState<S, C>,
block_context: &BlockContext,
tx_execution_context: &mut TransactionExecutionContext,
actual_fee: u128,
#[cfg(feature = "cairo-native")] program_cache: Option<
Rc<RefCell<ProgramCache<'_, ClassHash>>>,
>,
) -> Result<CallInfo, TransactionError> {
let fee_token_address = block_context
.get_fee_token_address_by_fee_type(&tx_execution_context.account_tx_fields.fee_type())
Expand Down Expand Up @@ -235,18 +180,7 @@ pub fn charge_fee<S: StateReader, C: ContractClassCache>(
&tx_execution_context.account_tx_fields.fee_type(),
)?;

let actual_fee = {
let version_0 = tx_execution_context.version.is_zero();
let fee_exceeded_max = actual_fee > max_fee;

if version_0 && fee_exceeded_max {
0
} else if version_0 && !fee_exceeded_max {
actual_fee
} else {
actual_fee.min(max_fee) * FEE_FACTOR
}
};
let actual_fee = actual_fee.min(max_fee);

let fee_transfer_info = if skip_fee_transfer {
None
Expand Down
5 changes: 2 additions & 3 deletions src/transaction/invoke_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use super::{
check_account_tx_fields_version,
error::FeeCheckError,
fee::{
calculate_tx_fee, check_fee_bounds, execute_fee_transfer_updated,
run_post_execution_fee_checks,
calculate_tx_fee, check_fee_bounds, execute_fee_transfer, run_post_execution_fee_checks,
},
get_tx_version, ResourceBounds, Transaction, VersionSpecificAccountTxFields,
};
Expand Down Expand Up @@ -462,7 +461,7 @@ impl InvokeFunction {
&& !actual_fee.is_zero()
&& !self.account_tx_fields.max_fee().is_zero()
{
Some(execute_fee_transfer_updated(
Some(execute_fee_transfer(
state,
block_context,
&mut tx_execution_context,
Expand Down