1
1
use super :: error:: TransactionError ;
2
- use crate :: definitions:: constants:: FEE_FACTOR ;
2
+ use crate :: definitions:: constants:: { FEE_FACTOR , QUERY_VERSION_BASE } ;
3
3
use crate :: execution:: execution_entry_point:: ExecutionResult ;
4
4
use crate :: services:: api:: contract_classes:: deprecated_contract_class:: EntryPointType ;
5
5
use crate :: state:: cached_state:: CachedState ;
@@ -152,7 +152,19 @@ pub fn charge_fee<S: StateReader>(
152
152
block_context. starknet_os_config . gas_price ,
153
153
block_context,
154
154
) ?;
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
+ } ;
156
168
157
169
let fee_transfer_info = if skip_fee_transfer {
158
170
None
@@ -166,3 +178,70 @@ pub fn charge_fee<S: StateReader>(
166
178
} ;
167
179
Ok ( ( fee_transfer_info, actual_fee) )
168
180
}
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