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

Commit 6805c61

Browse files
authored
Fix actual_fee calculation for v0 (#838)
* Fix actual_fee calculation for v0 * Add ActualFeeExceedsMaxFeeError * Test charge_fee * Fix clippy * Fix version check
1 parent 250c609 commit 6805c61

File tree

1 file changed

+81
-2
lines changed

1 file changed

+81
-2
lines changed

src/transaction/fee.rs

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::error::TransactionError;
2-
use crate::definitions::constants::FEE_FACTOR;
2+
use crate::definitions::constants::{FEE_FACTOR, QUERY_VERSION_BASE};
33
use crate::execution::execution_entry_point::ExecutionResult;
44
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
55
use crate::state::cached_state::CachedState;
@@ -152,7 +152,19 @@ pub fn charge_fee<S: StateReader>(
152152
block_context.starknet_os_config.gas_price,
153153
block_context,
154154
)?;
155-
let actual_fee = min(actual_fee, max_fee) * FEE_FACTOR;
155+
156+
let actual_fee = if tx_execution_context.version != 0.into()
157+
&& tx_execution_context.version != *QUERY_VERSION_BASE
158+
{
159+
min(actual_fee, max_fee) * FEE_FACTOR
160+
} else {
161+
if actual_fee > max_fee {
162+
return Err(TransactionError::ActualFeeExceedsMaxFee(
163+
actual_fee, max_fee,
164+
));
165+
}
166+
actual_fee
167+
};
156168

157169
let fee_transfer_info = if skip_fee_transfer {
158170
None
@@ -166,3 +178,70 @@ pub fn charge_fee<S: StateReader>(
166178
};
167179
Ok((fee_transfer_info, actual_fee))
168180
}
181+
182+
#[cfg(test)]
183+
mod tests {
184+
use std::{collections::HashMap, sync::Arc};
185+
186+
use crate::{
187+
definitions::block_context::BlockContext,
188+
execution::TransactionExecutionContext,
189+
state::{cached_state::CachedState, in_memory_state_reader::InMemoryStateReader},
190+
transaction::{error::TransactionError, fee::charge_fee},
191+
};
192+
193+
#[test]
194+
fn test_charge_fee_v0_actual_fee_exceeds_max_fee_should_return_error() {
195+
let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None);
196+
let mut tx_execution_context = TransactionExecutionContext::default();
197+
let mut block_context = BlockContext::default();
198+
block_context.starknet_os_config.gas_price = 1;
199+
let resources = HashMap::from([
200+
("l1_gas_usage".to_string(), 200_usize),
201+
("pedersen_builtin".to_string(), 10000_usize),
202+
]);
203+
let max_fee = 100;
204+
let skip_fee_transfer = true;
205+
206+
let result = charge_fee(
207+
&mut state,
208+
&resources,
209+
&block_context,
210+
max_fee,
211+
&mut tx_execution_context,
212+
skip_fee_transfer,
213+
)
214+
.unwrap_err();
215+
216+
assert_matches!(result, TransactionError::ActualFeeExceedsMaxFee(_, _));
217+
}
218+
219+
#[test]
220+
fn test_charge_fee_v1_actual_fee_exceeds_max_fee_should_return_max_fee() {
221+
let mut state = CachedState::new(Arc::new(InMemoryStateReader::default()), None, None);
222+
let mut tx_execution_context = TransactionExecutionContext {
223+
version: 1.into(),
224+
..Default::default()
225+
};
226+
let mut block_context = BlockContext::default();
227+
block_context.starknet_os_config.gas_price = 1;
228+
let resources = HashMap::from([
229+
("l1_gas_usage".to_string(), 200_usize),
230+
("pedersen_builtin".to_string(), 10000_usize),
231+
]);
232+
let max_fee = 100;
233+
let skip_fee_transfer = true;
234+
235+
let result = charge_fee(
236+
&mut state,
237+
&resources,
238+
&block_context,
239+
max_fee,
240+
&mut tx_execution_context,
241+
skip_fee_transfer,
242+
)
243+
.unwrap();
244+
245+
assert_eq!(result.1, max_fee);
246+
}
247+
}

0 commit comments

Comments
 (0)