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

Commit a46e33b

Browse files
xqftjuanbono
andauthored
Fail with an Err transactions whose calculated fee exceed max_fee (#892)
* Make tx fail when actual_fee exceeds max_fee * Changed test * Formatting * Fix logic * Leave fail only without charging * Change test * Fix test broken by better fee calc * Fixed test fee * Update fee on test_deploy_account * Remove comment --------- Co-authored-by: Juan Bono <juanbono94@gmail.com>
1 parent 3200858 commit a46e33b

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

src/transaction/fee.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,18 @@ pub fn charge_fee<S: StateReader>(
154154
block_context,
155155
)?;
156156

157+
if actual_fee > max_fee {
158+
// TODO: Charge max_fee
159+
return Err(TransactionError::ActualFeeExceedsMaxFee(
160+
actual_fee, max_fee,
161+
));
162+
}
163+
157164
let actual_fee = if tx_execution_context.version != 0.into()
158165
&& tx_execution_context.version != *QUERY_VERSION_BASE
159166
{
160167
min(actual_fee, max_fee) * FEE_FACTOR
161168
} else {
162-
if actual_fee > max_fee {
163-
return Err(TransactionError::ActualFeeExceedsMaxFee(
164-
actual_fee, max_fee,
165-
));
166-
}
167169
actual_fee
168170
};
169171

@@ -177,6 +179,7 @@ pub fn charge_fee<S: StateReader>(
177179
actual_fee,
178180
)?)
179181
};
182+
180183
Ok((fee_transfer_info, actual_fee))
181184
}
182185

@@ -218,7 +221,7 @@ mod tests {
218221
}
219222

220223
#[test]
221-
fn test_charge_fee_v1_actual_fee_exceeds_max_fee_should_return_max_fee() {
224+
fn test_charge_fee_v1_actual_fee_exceeds_max_fee_should_return_error() {
222225
let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None);
223226
let mut tx_execution_context = TransactionExecutionContext {
224227
version: 1.into(),
@@ -241,8 +244,8 @@ mod tests {
241244
&mut tx_execution_context,
242245
skip_fee_transfer,
243246
)
244-
.unwrap();
247+
.unwrap_err();
245248

246-
assert_eq!(result.1, max_fee);
249+
assert_matches!(result, TransactionError::ActualFeeExceedsMaxFee(_, _));
247250
}
248251
}

src/transaction/invoke_function.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ mod tests {
771771
}
772772

773773
#[test]
774-
fn test_execute_invoke_actual_fee_exceeded_max_fee_should_charge_max_fee() {
774+
fn test_execute_invoke_actual_fee_exceeded_max_fee_should_fail() {
775775
let max_fee = 5;
776776
let internal_invoke_function = InvokeFunction {
777777
contract_address: Address(0.into()),
@@ -824,8 +824,8 @@ mod tests {
824824

825825
let tx = internal_invoke_function
826826
.execute(&mut state, &block_context, 0)
827-
.unwrap();
828-
assert_eq!(tx.actual_fee, max_fee);
827+
.unwrap_err();
828+
assert_matches!(tx, TransactionError::ActualFeeExceedsMaxFee(_, _));
829829
}
830830

831831
#[test]

tests/internals.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ fn test_invoke_with_declarev2_tx() {
14591459
fn test_deploy_account() {
14601460
let (block_context, mut state) = create_account_tx_test_state().unwrap();
14611461

1462-
let expected_fee = 3684;
1462+
let expected_fee = 6157;
14631463

14641464
let deploy_account_tx = DeployAccount::new(
14651465
felt_to_hash(&TEST_ACCOUNT_CONTRACT_CLASS_HASH),
@@ -1576,7 +1576,7 @@ fn expected_deploy_account_states() -> (
15761576
CachedState<InMemoryStateReader>,
15771577
CachedState<InMemoryStateReader>,
15781578
) {
1579-
let fee = Felt252::from(3684);
1579+
let fee = Felt252::from(6157);
15801580
let mut state_before = CachedState::new(
15811581
Arc::new(InMemoryStateReader::new(
15821582
HashMap::from([

0 commit comments

Comments
 (0)