Skip to content

Commit 0c42ced

Browse files
kianenigmaapopiak
andauthored
Fix the broken weight multiplier update function (paritytech#6334)
* Initial draft, has some todos left * remove ununsed import * Apply suggestions from code review * Some refactors with migration * Fix more test and cleanup * Fix for companion * Apply suggestions from code review Co-authored-by: Alexander Popiak <[email protected]> * Update bin/node/runtime/src/impls.rs * Fix weight * Add integrity test * length is not affected. Co-authored-by: Alexander Popiak <[email protected]>
1 parent 17be6fd commit 0c42ced

File tree

10 files changed

+456
-251
lines changed

10 files changed

+456
-251
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/executor/tests/basic.rs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@ use codec::{Encode, Decode, Joiner};
1919
use 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
};
2724
use sp_core::{NeverNativeValue, traits::Externalities, storage::well_known_keys};
2825
use 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

3633
use node_runtime::{
3734
Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances,
38-
System, TransactionPayment, Event, TransactionByteFee,
35+
System, TransactionPayment, Event,
3936
constants::currency::*,
4037
};
4138
use node_primitives::{Balance, Hash};
@@ -52,16 +49,17 @@ use self::common::{*, sign};
5249
/// test code paths that differ between native and wasm versions.
5350
pub 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

6765
fn 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
});

bin/node/executor/tests/fees.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use frame_support::{
2222
weights::{GetDispatchInfo, constants::ExtrinsicBaseWeight, IdentityFee, WeightToFeePolynomial},
2323
};
2424
use sp_core::NeverNativeValue;
25-
use sp_runtime::{FixedPointNumber, FixedI128, Perbill};
25+
use sp_runtime::{Perbill, FixedPointNumber};
2626
use node_runtime::{
27-
CheckedExtrinsic, Call, Runtime, Balances, TransactionPayment,
27+
CheckedExtrinsic, Call, Runtime, Balances, TransactionPayment, Multiplier,
2828
TransactionByteFee,
2929
constants::currency::*,
3030
};
@@ -38,8 +38,8 @@ use self::common::{*, sign};
3838
fn fee_multiplier_increases_and_decreases_on_big_weight() {
3939
let mut t = new_test_ext(COMPACT_CODE, false);
4040

41-
// initial fee multiplier must be zero
42-
let mut prev_multiplier = FixedI128::from_inner(0);
41+
// initial fee multiplier must be one.
42+
let mut prev_multiplier = Multiplier::one();
4343

4444
t.execute_with(|| {
4545
assert_eq!(TransactionPayment::next_fee_multiplier(), prev_multiplier);
@@ -59,7 +59,7 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() {
5959
},
6060
CheckedExtrinsic {
6161
signed: Some((charlie(), signed_extra(0, 0))),
62-
function: Call::System(frame_system::Call::fill_block(Perbill::from_percent(90))),
62+
function: Call::System(frame_system::Call::fill_block(Perbill::from_percent(60))),
6363
}
6464
]
6565
);
@@ -122,7 +122,7 @@ fn fee_multiplier_increases_and_decreases_on_big_weight() {
122122
}
123123

124124
#[test]
125-
fn transaction_fee_is_correct_ultimate() {
125+
fn transaction_fee_is_correct() {
126126
// This uses the exact values of substrate-node.
127127
//
128128
// weight of transfer call as of now: 1_000_000

0 commit comments

Comments
 (0)