Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Replace TransactionBaseFee with ExtrinsicBaseWeight #5761

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,12 @@ impl balances::Trait for Runtime {
}

parameter_types! {
pub const TransactionBaseFee: Balance = 0;
pub const TransactionByteFee: Balance = 1;
}

impl transaction_payment::Trait for Runtime {
type Currency = balances::Module<Runtime>;
type OnTransactionPayment = ();
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = ConvertInto;
type FeeMultiplierUpdate = ();
Expand Down
8 changes: 4 additions & 4 deletions bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use frame_system::{self, EventRecord, Phase};

use node_runtime::{
Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances,
System, TransactionPayment, Event, TransactionBaseFee, TransactionByteFee,
System, TransactionPayment, Event, TransactionByteFee, ExtrinsicBaseWeight,
constants::currency::*,
};
use node_primitives::{Balance, Hash};
Expand All @@ -54,11 +54,11 @@ pub const BLOATY_CODE: &[u8] = node_runtime::WASM_BINARY_BLOATY;
fn transfer_fee<E: Encode>(extrinsic: &E, fee_multiplier: Fixed128) -> Balance {
let length_fee = TransactionByteFee::get() * (extrinsic.encode().len() as Balance);

let base_weight = ExtrinsicBaseWeight::get();
let base_fee = <Runtime as pallet_transaction_payment::Trait>::WeightToFee::convert(base_weight);
let weight = default_transfer_call().get_dispatch_info().weight;
let weight_fee = <Runtime as pallet_transaction_payment::Trait>
::WeightToFee::convert(weight);
let weight_fee = <Runtime as pallet_transaction_payment::Trait>::WeightToFee::convert(weight);

let base_fee = TransactionBaseFee::get();
base_fee + fee_multiplier.saturated_multiply_accumulate(length_fee + weight_fee)
}

Expand Down
13 changes: 8 additions & 5 deletions bin/node/executor/tests/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use frame_support::{
use sp_core::{NeverNativeValue, map, storage::Storage};
use sp_runtime::{Fixed128, Perbill, traits::{Convert, BlakeTwo256}};
use node_runtime::{
CheckedExtrinsic, Call, Runtime, Balances, TransactionPayment, TransactionBaseFee,
CheckedExtrinsic, Call, Runtime, Balances, TransactionPayment,
TransactionByteFee, WeightFeeCoefficient,
constants::currency::*,
constants::currency::*, ExtrinsicBaseWeight,
};
use node_runtime::impls::LinearWeightToFee;
use node_primitives::Balance;
Expand Down Expand Up @@ -173,15 +173,17 @@ fn transaction_fee_is_correct_ultimate() {
t.execute_with(|| {
assert_eq!(Balances::total_balance(&bob()), (10 + 69) * DOLLARS);
// Components deducted from alice's balances:
// - Base fee
// - Weight fee
// - Length fee
// - Tip
// - Creation-fee of bob's account.
let mut balance_alice = (100 - 69) * DOLLARS;

let length_fee = TransactionBaseFee::get() +
TransactionByteFee::get() *
(xt.clone().encode().len() as Balance);
let base_weight = ExtrinsicBaseWeight::get();
let base_fee = LinearWeightToFee::<WeightFeeCoefficient>::convert(base_weight);

let length_fee = TransactionByteFee::get() * (xt.clone().encode().len() as Balance);
balance_alice -= length_fee;

let weight = default_transfer_call().get_dispatch_info().weight;
Expand All @@ -191,6 +193,7 @@ fn transaction_fee_is_correct_ultimate() {
// current weight of transfer = 200_000_000
// Linear weight to fee is 1:1 right now (1 weight = 1 unit of balance)
assert_eq!(weight_fee, weight as Balance);
balance_alice -= base_fee;
balance_alice -= weight_fee;
balance_alice -= tip;

Expand Down
2 changes: 1 addition & 1 deletion bin/node/executor/tests/submit_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fn submitted_transaction_should_be_valid() {
let res = Executive::validate_transaction(source, extrinsic);

assert_eq!(res.unwrap(), ValidTransaction {
priority: 2_411_380_000_000,
priority: 1_411_390_000_000,
requires: vec![],
provides: vec![(address, 0).encode()],
longevity: 128,
Expand Down
2 changes: 0 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ impl pallet_balances::Trait for Runtime {
}

parameter_types! {
pub const TransactionBaseFee: Balance = 1 * CENTS;
pub const TransactionByteFee: Balance = 10 * MILLICENTS;
// In the Substrate node, a weight of 10_000_000 (smallest non-zero weight)
// is mapped to 10_000_000 units of fees, hence:
Expand All @@ -230,7 +229,6 @@ parameter_types! {
impl pallet_transaction_payment::Trait for Runtime {
type Currency = Balances;
type OnTransactionPayment = DealWithFees;
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = LinearWeightToFee<WeightFeeCoefficient>;
type FeeMultiplierUpdate = TargetedFeeAdjustment<TargetBlockFullness>;
Expand Down
2 changes: 0 additions & 2 deletions frame/balances/src/tests_composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,11 @@ impl frame_system::Trait for Test {
type OnKilledAccount = ();
}
parameter_types! {
pub const TransactionBaseFee: u64 = 0;
pub const TransactionByteFee: u64 = 1;
}
impl pallet_transaction_payment::Trait for Test {
type Currency = Module<Test>;
type OnTransactionPayment = ();
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = ConvertInto;
type FeeMultiplierUpdate = ();
Expand Down
2 changes: 0 additions & 2 deletions frame/balances/src/tests_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,11 @@ impl frame_system::Trait for Test {
type OnKilledAccount = Module<Test>;
}
parameter_types! {
pub const TransactionBaseFee: u64 = 0;
pub const TransactionByteFee: u64 = 1;
}
impl pallet_transaction_payment::Trait for Test {
type Currency = Module<Test>;
type OnTransactionPayment = ();
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = ConvertInto;
type FeeMultiplierUpdate = ();
Expand Down
33 changes: 18 additions & 15 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ mod tests {
use sp_core::H256;
use sp_runtime::{
generic::Era, Perbill, DispatchError, testing::{Digest, Header, Block},
traits::{Header as HeaderT, BlakeTwo256, IdentityLookup, ConvertInto},
traits::{Header as HeaderT, BlakeTwo256, IdentityLookup, Convert, ConvertInto},
transaction_validity::{InvalidTransaction, UnknownTransaction, TransactionValidityError},
};
use frame_support::{
Expand Down Expand Up @@ -503,29 +503,29 @@ mod tests {
type MaximumBlockLength = MaximumBlockLength;
type Version = RuntimeVersion;
type ModuleToIndex = ();
type AccountData = pallet_balances::AccountData<u64>;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
}

type Balance = u64;
parameter_types! {
pub const ExistentialDeposit: u64 = 1;
pub const ExistentialDeposit: Balance = 1;
}
impl pallet_balances::Trait for Runtime {
type Balance = u64;
type Balance = Balance;
type Event = MetaEvent;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
}

parameter_types! {
pub const TransactionBaseFee: u64 = 10;
pub const TransactionByteFee: u64 = 0;
pub const TransactionByteFee: Balance = 0;
}
impl pallet_transaction_payment::Trait for Runtime {
type Currency = Balances;
type OnTransactionPayment = ();
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = ConvertInto;
type FeeMultiplierUpdate = ();
Expand Down Expand Up @@ -572,7 +572,7 @@ mod tests {
type TestXt = sp_runtime::testing::TestXt<Call, SignedExtra>;
type Executive = super::Executive<Runtime, Block<TestXt>, ChainContext<Runtime>, Runtime, AllModules>;

fn extra(nonce: u64, fee: u64) -> SignedExtra {
fn extra(nonce: u64, fee: Balance) -> SignedExtra {
(
frame_system::CheckEra::from(Era::Immortal),
frame_system::CheckNonce::from(nonce),
Expand All @@ -581,7 +581,7 @@ mod tests {
)
}

fn sign_extra(who: u64, nonce: u64, fee: u64) -> Option<(u64, SignedExtra)> {
fn sign_extra(who: u64, nonce: u64, fee: Balance) -> Option<(u64, SignedExtra)> {
Some((who, extra(nonce, fee)))
}

Expand All @@ -592,7 +592,8 @@ mod tests {
balances: vec![(1, 211)],
}.assimilate_storage(&mut t).unwrap();
let xt = TestXt::new(Call::Balances(BalancesCall::transfer(2, 69)), sign_extra(1, 0, 0));
let weight = xt.get_dispatch_info().weight as u64;
let weight = xt.get_dispatch_info().weight + <Runtime as frame_system::Trait>::ExtrinsicBaseWeight::get();
let fee: Balance = <Runtime as pallet_transaction_payment::Trait>::WeightToFee::convert(weight);
let mut t = sp_io::TestExternalities::new(t);
t.execute_with(|| {
Executive::initialize_block(&Header::new(
Expand All @@ -604,12 +605,12 @@ mod tests {
));
let r = Executive::apply_extrinsic(xt);
assert!(r.is_ok());
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&1), 142 - 10 - weight);
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&1), 142 - fee);
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&2), 69);
});
}

fn new_test_ext(balance_factor: u64) -> sp_io::TestExternalities {
fn new_test_ext(balance_factor: Balance) -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
pallet_balances::GenesisConfig::<Runtime> {
balances: vec![(1, 111 * balance_factor)],
Expand Down Expand Up @@ -776,7 +777,7 @@ mod tests {
let execute_with_lock = |lock: WithdrawReasons| {
let mut t = new_test_ext(1);
t.execute_with(|| {
<pallet_balances::Module<Runtime> as LockableCurrency<u64>>::set_lock(
<pallet_balances::Module<Runtime> as LockableCurrency<Balance>>::set_lock(
id,
&1,
110,
Expand All @@ -786,7 +787,9 @@ mod tests {
Call::System(SystemCall::remark(vec![1u8])),
sign_extra(1, 0, 0),
);
let weight = xt.get_dispatch_info().weight as u64;
let weight = xt.get_dispatch_info().weight
+ <Runtime as frame_system::Trait>::ExtrinsicBaseWeight::get();
let fee: Balance = <Runtime as pallet_transaction_payment::Trait>::WeightToFee::convert(weight);
Executive::initialize_block(&Header::new(
1,
H256::default(),
Expand All @@ -798,7 +801,7 @@ mod tests {
if lock == WithdrawReasons::except(WithdrawReason::TransactionPayment) {
assert!(Executive::apply_extrinsic(xt).unwrap().is_ok());
// tx fee has been deducted.
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&1), 111 - 10 - weight);
assert_eq!(<pallet_balances::Module<Runtime>>::total_balance(&1), 111 - fee);
} else {
assert_eq!(
Executive::apply_extrinsic(xt),
Expand Down
Loading