@@ -19,14 +19,11 @@ use codec::{Encode, Decode, Joiner};
1919use frame_support:: {
2020 StorageValue , StorageMap ,
2121 traits:: Currency ,
22- weights:: {
23- GetDispatchInfo , DispatchInfo , DispatchClass , constants:: ExtrinsicBaseWeight ,
24- WeightToFeePolynomial ,
25- } ,
22+ weights:: { GetDispatchInfo , DispatchInfo , DispatchClass } ,
2623} ;
2724use sp_core:: { NeverNativeValue , traits:: Externalities , storage:: well_known_keys} ;
2825use sp_runtime:: {
29- ApplyExtrinsicResult , FixedI128 , FixedPointNumber ,
26+ ApplyExtrinsicResult ,
3027 traits:: Hash as HashT ,
3128 transaction_validity:: InvalidTransaction ,
3229} ;
@@ -35,7 +32,7 @@ use frame_system::{self, EventRecord, Phase};
3532
3633use node_runtime:: {
3734 Header , Block , UncheckedExtrinsic , CheckedExtrinsic , Call , Runtime , Balances ,
38- System , TransactionPayment , Event , TransactionByteFee ,
35+ System , TransactionPayment , Event ,
3936 constants:: currency:: * ,
4037} ;
4138use node_primitives:: { Balance , Hash } ;
@@ -52,16 +49,17 @@ use self::common::{*, sign};
5249/// test code paths that differ between native and wasm versions.
5350pub const BLOATY_CODE : & [ u8 ] = node_runtime:: WASM_BINARY_BLOATY ;
5451
55- /// Default transfer fee
56- fn transfer_fee < E : Encode > ( extrinsic : & E , fee_multiplier : FixedI128 ) -> Balance {
57- let length_fee = TransactionByteFee :: get ( ) * ( extrinsic. encode ( ) . len ( ) as Balance ) ;
58-
59- let base_weight = ExtrinsicBaseWeight :: get ( ) ;
60- let base_fee = <Runtime as pallet_transaction_payment:: Trait >:: WeightToFee :: calc ( & base_weight) ;
61- let weight = default_transfer_call ( ) . get_dispatch_info ( ) . weight ;
62- let weight_fee = <Runtime as pallet_transaction_payment:: Trait >:: WeightToFee :: calc ( & weight) ;
63-
64- base_fee + fee_multiplier. saturating_mul_acc_int ( length_fee + weight_fee)
52+ /// Default transfer fee. This will use the same logic that is implemented in transaction-payment module.
53+ ///
54+ /// Note that reads the multiplier from storage directly, hence to get the fee of `extrinsic`
55+ /// at block `n`, it must be called prior to executing block `n` to do the calculation with the
56+ /// correct multiplier.
57+ fn transfer_fee < E : Encode > ( extrinsic : & E ) -> Balance {
58+ TransactionPayment :: compute_fee (
59+ extrinsic. encode ( ) . len ( ) as u32 ,
60+ & default_transfer_call ( ) . get_dispatch_info ( ) ,
61+ 0 ,
62+ )
6563}
6664
6765fn xt ( ) -> UncheckedExtrinsic {
@@ -242,7 +240,7 @@ fn successful_execution_with_native_equivalent_code_gives_ok() {
242240 ) . 0 ;
243241 assert ! ( r. is_ok( ) ) ;
244242
245- let fm = t. execute_with ( TransactionPayment :: next_fee_multiplier ) ;
243+ let fees = t. execute_with ( || transfer_fee ( & xt ( ) ) ) ;
246244
247245 let r = executor_call :: < NeverNativeValue , fn ( ) -> _ > (
248246 & mut t,
@@ -254,7 +252,6 @@ fn successful_execution_with_native_equivalent_code_gives_ok() {
254252 assert ! ( r. is_ok( ) ) ;
255253
256254 t. execute_with ( || {
257- let fees = transfer_fee ( & xt ( ) , fm) ;
258255 assert_eq ! ( Balances :: total_balance( & alice( ) ) , 42 * DOLLARS - fees) ;
259256 assert_eq ! ( Balances :: total_balance( & bob( ) ) , 69 * DOLLARS ) ;
260257 } ) ;
@@ -286,7 +283,7 @@ fn successful_execution_with_foreign_code_gives_ok() {
286283 ) . 0 ;
287284 assert ! ( r. is_ok( ) ) ;
288285
289- let fm = t. execute_with ( TransactionPayment :: next_fee_multiplier ) ;
286+ let fees = t. execute_with ( || transfer_fee ( & xt ( ) ) ) ;
290287
291288 let r = executor_call :: < NeverNativeValue , fn ( ) -> _ > (
292289 & mut t,
@@ -298,7 +295,6 @@ fn successful_execution_with_foreign_code_gives_ok() {
298295 assert ! ( r. is_ok( ) ) ;
299296
300297 t. execute_with ( || {
301- let fees = transfer_fee ( & xt ( ) , fm) ;
302298 assert_eq ! ( Balances :: total_balance( & alice( ) ) , 42 * DOLLARS - fees) ;
303299 assert_eq ! ( Balances :: total_balance( & bob( ) ) , 69 * DOLLARS ) ;
304300 } ) ;
@@ -311,7 +307,7 @@ fn full_native_block_import_works() {
311307 let ( block1, block2) = blocks ( ) ;
312308
313309 let mut alice_last_known_balance: Balance = Default :: default ( ) ;
314- let mut fm = t. execute_with ( TransactionPayment :: next_fee_multiplier ) ;
310+ let mut fees = t. execute_with ( || transfer_fee ( & xt ( ) ) ) ;
315311
316312 executor_call :: < NeverNativeValue , fn ( ) -> _ > (
317313 & mut t,
@@ -322,7 +318,6 @@ fn full_native_block_import_works() {
322318 ) . 0 . unwrap ( ) ;
323319
324320 t. execute_with ( || {
325- let fees = transfer_fee ( & xt ( ) , fm) ;
326321 assert_eq ! ( Balances :: total_balance( & alice( ) ) , 42 * DOLLARS - fees) ;
327322 assert_eq ! ( Balances :: total_balance( & bob( ) ) , 169 * DOLLARS ) ;
328323 alice_last_known_balance = Balances :: total_balance ( & alice ( ) ) ;
@@ -361,7 +356,7 @@ fn full_native_block_import_works() {
361356 assert_eq ! ( System :: events( ) , events) ;
362357 } ) ;
363358
364- fm = t. execute_with ( TransactionPayment :: next_fee_multiplier ) ;
359+ fees = t. execute_with ( || transfer_fee ( & xt ( ) ) ) ;
365360
366361 executor_call :: < NeverNativeValue , fn ( ) -> _ > (
367362 & mut t,
@@ -372,7 +367,6 @@ fn full_native_block_import_works() {
372367 ) . 0 . unwrap ( ) ;
373368
374369 t. execute_with ( || {
375- let fees = transfer_fee ( & xt ( ) , fm) ;
376370 assert_eq ! (
377371 Balances :: total_balance( & alice( ) ) ,
378372 alice_last_known_balance - 10 * DOLLARS - fees,
@@ -450,7 +444,7 @@ fn full_wasm_block_import_works() {
450444 let ( block1, block2) = blocks ( ) ;
451445
452446 let mut alice_last_known_balance: Balance = Default :: default ( ) ;
453- let mut fm = t. execute_with ( TransactionPayment :: next_fee_multiplier ) ;
447+ let mut fees = t. execute_with ( || transfer_fee ( & xt ( ) ) ) ;
454448
455449 executor_call :: < NeverNativeValue , fn ( ) -> _ > (
456450 & mut t,
@@ -461,12 +455,12 @@ fn full_wasm_block_import_works() {
461455 ) . 0 . unwrap ( ) ;
462456
463457 t. execute_with ( || {
464- assert_eq ! ( Balances :: total_balance( & alice( ) ) , 42 * DOLLARS - transfer_fee ( & xt ( ) , fm ) ) ;
458+ assert_eq ! ( Balances :: total_balance( & alice( ) ) , 42 * DOLLARS - fees ) ;
465459 assert_eq ! ( Balances :: total_balance( & bob( ) ) , 169 * DOLLARS ) ;
466460 alice_last_known_balance = Balances :: total_balance ( & alice ( ) ) ;
467461 } ) ;
468462
469- fm = t. execute_with ( TransactionPayment :: next_fee_multiplier ) ;
463+ fees = t. execute_with ( || transfer_fee ( & xt ( ) ) ) ;
470464
471465 executor_call :: < NeverNativeValue , fn ( ) -> _ > (
472466 & mut t,
@@ -479,11 +473,11 @@ fn full_wasm_block_import_works() {
479473 t. execute_with ( || {
480474 assert_eq ! (
481475 Balances :: total_balance( & alice( ) ) ,
482- alice_last_known_balance - 10 * DOLLARS - transfer_fee ( & xt ( ) , fm ) ,
476+ alice_last_known_balance - 10 * DOLLARS - fees ,
483477 ) ;
484478 assert_eq ! (
485479 Balances :: total_balance( & bob( ) ) ,
486- 179 * DOLLARS - 1 * transfer_fee ( & xt ( ) , fm ) ,
480+ 179 * DOLLARS - 1 * fees ,
487481 ) ;
488482 } ) ;
489483}
@@ -755,7 +749,7 @@ fn successful_execution_gives_ok() {
755749 assert_eq ! ( Balances :: total_balance( & alice( ) ) , 111 * DOLLARS ) ;
756750 } ) ;
757751
758- let fm = t. execute_with ( TransactionPayment :: next_fee_multiplier ) ;
752+ let fees = t. execute_with ( || transfer_fee ( & xt ( ) ) ) ;
759753
760754 let r = executor_call :: < NeverNativeValue , fn ( ) -> _ > (
761755 & mut t,
@@ -770,7 +764,6 @@ fn successful_execution_gives_ok() {
770764 . expect ( "Extrinsic failed" ) ;
771765
772766 t. execute_with ( || {
773- let fees = transfer_fee ( & xt ( ) , fm) ;
774767 assert_eq ! ( Balances :: total_balance( & alice( ) ) , 42 * DOLLARS - fees) ;
775768 assert_eq ! ( Balances :: total_balance( & bob( ) ) , 69 * DOLLARS ) ;
776769 } ) ;
0 commit comments