Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
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
5 changes: 3 additions & 2 deletions node/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ mod tests {
use node_runtime::{
Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances, BuildStorage,
System, TransactionPayment, Event, TransferFee, TransactionBaseFee, TransactionByteFee,
WeightToFee, constants::currency::*,
WeightFeeCoefficient, constants::currency::*,
};
use node_runtime::impls::LinearWeightToFee;
use node_primitives::{Balance, Hash, BlockNumber};
use node_testing::keyring::*;
use wabt;
Expand Down Expand Up @@ -1046,7 +1047,7 @@ mod tests {
balance_alice -= length_fee;

let weight = default_transfer_call().get_dispatch_info().weight;
let weight_fee = WeightToFee::convert(weight);
let weight_fee = LinearWeightToFee::<WeightFeeCoefficient>::convert(weight);

// we know that weight to fee multiplier is effect-less in block 1.
assert_eq!(weight_fee as Balance, MILLICENTS);
Expand Down
18 changes: 13 additions & 5 deletions node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use sr_primitives::traits::{Convert, Saturating};
use sr_primitives::{Fixed64, Perbill};
use support::traits::{OnUnbalanced, Currency, Get};
use crate::{
Balances, System, Authorship, MaximumBlockWeight, NegativeImbalance, WeightToFee,
Balances, System, Authorship, MaximumBlockWeight, NegativeImbalance,
TargetedFeeAdjustment,
};

Expand All @@ -49,10 +49,18 @@ impl Convert<u128, Balance> for CurrencyToVoteHandler {
fn convert(x: u128) -> Balance { x * Self::factor() }
}

/// Simply multiply by a coefficient denoted by the `Get` implementation.
impl Convert<Weight, Balance> for WeightToFee {
fn convert(x: Weight) -> Balance {
Balance::from(x).saturating_mul(Self::get())
/// Convert from weight to balance via a simple coefficient multiplication
/// The associated type C encapsulates a constant in units of balance per weight
pub struct LinearWeightToFee<C>(rstd::marker::PhantomData<C>);

impl<C> Convert<Weight, Balance> for LinearWeightToFee<C>
where C: Get<Balance> {

fn convert(w: Weight) -> Balance {
// substrate-node a weight of 10_000 (smallest non-zero weight) to be mapped to 10^7 units of
// fees, hence:
let coefficient = C::get();
Balance::from(w).saturating_mul(coefficient)
}
}

Expand Down
6 changes: 3 additions & 3 deletions node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub use staking::StakerStatus;

/// Implementations of some helper traits passed into runtime modules as associated types.
pub mod impls;
use impls::{CurrencyToVoteHandler, Author};
use impls::{CurrencyToVoteHandler, Author, LinearWeightToFee};

/// Constant values used within the runtime.
pub mod constants;
Expand Down Expand Up @@ -178,7 +178,7 @@ parameter_types! {
pub const TransactionBaseFee: Balance = 1 * CENTS;
pub const TransactionByteFee: Balance = 10 * MILLICENTS;
// setting this to zero will disable the weight fee.
pub const WeightToFee: Balance = 1_000;
pub const WeightFeeCoefficient: Balance = 1_000;
// for a sane configuration, this should always be less than `AvailableBlockRatio`.
pub const TargetedFeeAdjustment: Perbill = Perbill::from_percent(25);
}
Expand All @@ -188,7 +188,7 @@ impl transaction_payment::Trait for Runtime {
type OnTransactionPayment = DealWithFees;
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = WeightToFee;
type WeightToFee = LinearWeightToFee<WeightFeeCoefficient>;
type FeeMultiplierUpdate = TargetedFeeAdjustment;
}

Expand Down