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

Commit

Permalink
Switch from Convert to WeightToFeePolynomial
Browse files Browse the repository at this point in the history
  • Loading branch information
athei committed May 19, 2020
1 parent a468a62 commit 3b637f0
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 34 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions runtime/kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rustc-hex = { version = "2.0.1", default-features = false }
serde = { version = "1.0.102", default-features = false }
serde_derive = { version = "1.0.102", optional = true }
static_assertions = "1.1.0"
smallvec = "1.4.0"

authority-discovery-primitives = { package = "sp-authority-discovery", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
babe-primitives = { package = "sp-consensus-babe", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down
26 changes: 18 additions & 8 deletions runtime/kusama/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ pub mod time {
pub mod fee {
pub use sp_runtime::Perbill;
use primitives::Balance;
use frame_support::weights::Weight;
use sp_runtime::traits::Convert;
use runtime_common::ExtrinsicBaseWeight;
use frame_support::weights::{
WeightToFeePolynomial, WeightToFeeCoefficient, WeightToFeeCoefficients,
};
use smallvec::smallvec;

/// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);
Expand All @@ -70,17 +72,25 @@ pub mod fee {
/// - Setting it to `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee;
impl Convert<Weight, Balance> for WeightToFee {
fn convert(x: Weight) -> Balance {
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// in Kusama, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
Balance::from(x).saturating_mul(super::currency::CENTS / 10) / Balance::from(ExtrinsicBaseWeight::get())
let p = super::currency::CENTS;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get());
smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational_approximation(p, q),
coeff_integer: p / q,
}]
}
}
}

#[cfg(test)]
mod tests {
use sp_runtime::traits::Convert;
use frame_support::weights::WeightToFeePolynomial;
use runtime_common::{MaximumBlockWeight, ExtrinsicBaseWeight};
use super::fee::WeightToFee;
use super::currency::{CENTS, DOLLARS};
Expand All @@ -89,13 +99,13 @@ mod tests {
// This function tests that the fee for `MaximumBlockWeight` of weight is correct
fn full_block_fee_is_correct() {
// A full block should cost 16 DOLLARS
assert_eq!(WeightToFee::convert(MaximumBlockWeight::get()), 16 * DOLLARS)
assert_eq!(WeightToFee::calc(&MaximumBlockWeight::get()), 16 * DOLLARS)
}

#[test]
// This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
fn extrinsic_base_fee_is_correct() {
// `ExtrinsicBaseWeight` should cost 1/10 of a CENT
assert_eq!(WeightToFee::convert(ExtrinsicBaseWeight::get()), CENTS / 10)
assert_eq!(WeightToFee::calc(&ExtrinsicBaseWeight::get()), CENTS / 10)
}
}
3 changes: 2 additions & 1 deletion runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ parameter_types! {
}

impl transaction_payment::Trait for Runtime {
type Event = Event;
type Currency = Balances;
type OnTransactionPayment = DealWithFees;
type TransactionByteFee = TransactionByteFee;
Expand Down Expand Up @@ -784,7 +785,7 @@ construct_runtime! {
Timestamp: timestamp::{Module, Call, Storage, Inherent},
Indices: indices::{Module, Call, Storage, Config<T>, Event<T>},
Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: transaction_payment::{Module, Storage},
TransactionPayment: transaction_payment::{Module, Storage, Event},

// Consensus support.
Authorship: authorship::{Module, Call, Storage},
Expand Down
1 change: 1 addition & 0 deletions runtime/polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rustc-hex = { version = "2.0.1", default-features = false }
serde = { version = "1.0.102", default-features = false }
serde_derive = { version = "1.0.102", optional = true }
static_assertions = "1.1.0"
smallvec = "1.4.0"

authority-discovery-primitives = { package = "sp-authority-discovery", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
babe-primitives = { package = "sp-consensus-babe", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down
26 changes: 18 additions & 8 deletions runtime/polkadot/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ pub mod time {
pub mod fee {
pub use sp_runtime::Perbill;
use primitives::Balance;
use frame_support::weights::Weight;
use sp_runtime::traits::Convert;
use runtime_common::ExtrinsicBaseWeight;
use frame_support::weights::{
WeightToFeePolynomial, WeightToFeeCoefficient, WeightToFeeCoefficients,
};
use smallvec::smallvec;

/// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);
Expand All @@ -62,32 +64,40 @@ pub mod fee {
/// - Setting it to `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee;
impl Convert<Weight, Balance> for WeightToFee {
fn convert(x: Weight) -> Balance {
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// in Polkadot, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
Balance::from(x).saturating_mul(super::currency::CENTS / 10) / Balance::from(ExtrinsicBaseWeight::get())
let p = super::currency::CENTS;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get());
smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational_approximation(p, q),
coeff_integer: p / q,
}]
}
}
}

#[cfg(test)]
mod tests {
use sp_runtime::traits::Convert;
use runtime_common::{MaximumBlockWeight, ExtrinsicBaseWeight};
use super::fee::WeightToFee;
use super::currency::{CENTS, DOLLARS};
use frame_support::weights::WeightToFeePolynomial;

#[test]
// This function tests that the fee for `MaximumBlockWeight` of weight is correct
fn full_block_fee_is_correct() {
// A full block should cost 16 DOLLARS
assert_eq!(WeightToFee::convert(MaximumBlockWeight::get()), 16 * DOLLARS)
assert_eq!(WeightToFee::calc(&MaximumBlockWeight::get()), 16 * DOLLARS)
}

#[test]
// This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
fn extrinsic_base_fee_is_correct() {
// `ExtrinsicBaseWeight` should cost 1/10 of a CENT
assert_eq!(WeightToFee::convert(ExtrinsicBaseWeight::get()), CENTS / 10)
assert_eq!(WeightToFee::calc(&ExtrinsicBaseWeight::get()), CENTS / 10)
}
}
3 changes: 2 additions & 1 deletion runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ parameter_types! {
}

impl transaction_payment::Trait for Runtime {
type Event = Event;
type Currency = Balances;
type OnTransactionPayment = DealWithFees;
type TransactionByteFee = TransactionByteFee;
Expand Down Expand Up @@ -712,7 +713,7 @@ construct_runtime! {
Timestamp: timestamp::{Module, Call, Storage, Inherent},
Indices: indices::{Module, Call, Storage, Config<T>, Event<T>},
Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: transaction_payment::{Module, Storage},
TransactionPayment: transaction_payment::{Module, Storage, Event},

// Consensus support.
Authorship: authorship::{Module, Call, Storage},
Expand Down
1 change: 1 addition & 0 deletions runtime/test-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ log = { version = "0.3.9", optional = true }
rustc-hex = { version = "2.0.1", default-features = false }
serde = { version = "1.0.102", default-features = false }
serde_derive = { version = "1.0.102", optional = true }
smallvec = "1.4.0"

babe-primitives = { package = "sp-consensus-babe", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down
22 changes: 16 additions & 6 deletions runtime/test-runtime/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ pub mod time {
pub mod fee {
pub use sp_runtime::Perbill;
use primitives::Balance;
use frame_support::weights::Weight;
use sp_runtime::traits::Convert;
use runtime_common::ExtrinsicBaseWeight;
use frame_support::weights::{
WeightToFeePolynomial, WeightToFeeCoefficient, WeightToFeeCoefficients,
};
use smallvec::smallvec;

/// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);
Expand All @@ -63,10 +66,17 @@ pub mod fee {
/// - Setting it to `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee;
impl Convert<Weight, Balance> for WeightToFee {
fn convert(x: Weight) -> Balance {
// in Kusama a weight of 10_000_000 (smallest non-zero weight) is mapped to 1/10 CENT:
Balance::from(x).saturating_mul(super::currency::CENTS / (10 * 10_000_000))
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
let p = super::currency::CENTS;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get());
smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational_approximation(p, q),
coeff_integer: p / q,
}]
}
}
}
3 changes: 2 additions & 1 deletion runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ parameter_types! {
}

impl transaction_payment::Trait for Runtime {
type Event = Event;
type Currency = Balances;
type OnTransactionPayment = ();
type TransactionByteFee = TransactionByteFee;
Expand Down Expand Up @@ -494,7 +495,7 @@ construct_runtime! {
Timestamp: timestamp::{Module, Call, Storage, Inherent},
Indices: indices::{Module, Call, Storage, Config<T>, Event<T>},
Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: transaction_payment::{Module, Storage},
TransactionPayment: transaction_payment::{Module, Storage, Event},

// Consensus support.
Authorship: authorship::{Module, Call, Storage},
Expand Down
1 change: 1 addition & 0 deletions runtime/westend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ log = { version = "0.3.9", optional = true }
rustc-hex = { version = "2.0.1", default-features = false }
serde = { version = "1.0.102", default-features = false }
serde_derive = { version = "1.0.102", optional = true }
smallvec = "1.4.0"

authority-discovery-primitives = { package = "sp-authority-discovery", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
babe-primitives = { package = "sp-consensus-babe", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand Down
26 changes: 18 additions & 8 deletions runtime/westend/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ pub mod time {
pub mod fee {
pub use sp_runtime::Perbill;
use primitives::Balance;
use frame_support::weights::Weight;
use sp_runtime::traits::Convert;
use runtime_common::ExtrinsicBaseWeight;
use frame_support::weights::{
WeightToFeePolynomial, WeightToFeeCoefficient, WeightToFeeCoefficients,
};
use smallvec::smallvec;

/// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25);
Expand All @@ -62,32 +64,40 @@ pub mod fee {
/// - Setting it to `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee;
impl Convert<Weight, Balance> for WeightToFee {
fn convert(x: Weight) -> Balance {
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// in Westend, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT:
Balance::from(x).saturating_mul(super::currency::CENTS / 10) / Balance::from(ExtrinsicBaseWeight::get())
let p = super::currency::CENTS;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get());
smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
coeff_frac: Perbill::from_rational_approximation(p, q),
coeff_integer: p / q,
}]
}
}
}

#[cfg(test)]
mod tests {
use sp_runtime::traits::Convert;
use runtime_common::{MaximumBlockWeight, ExtrinsicBaseWeight};
use super::fee::WeightToFee;
use super::currency::{CENTS, DOLLARS};
use frame_support::weights::WeightToFeePolynomial;

#[test]
// This function tests that the fee for `MaximumBlockWeight` of weight is correct
fn full_block_fee_is_correct() {
// A full block should cost 16 DOLLARS
assert_eq!(WeightToFee::convert(MaximumBlockWeight::get()), 16 * DOLLARS)
assert_eq!(WeightToFee::calc(&MaximumBlockWeight::get()), 16 * DOLLARS)
}

#[test]
// This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
fn extrinsic_base_fee_is_correct() {
// `ExtrinsicBaseWeight` should cost 1/10 of a CENT
assert_eq!(WeightToFee::convert(ExtrinsicBaseWeight::get()), CENTS / 10)
assert_eq!(WeightToFee::calc(&ExtrinsicBaseWeight::get()), CENTS / 10)
}
}
3 changes: 2 additions & 1 deletion runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ parameter_types! {
}

impl transaction_payment::Trait for Runtime {
type Event = Event;
type Currency = Balances;
type OnTransactionPayment = ToAuthor<Runtime>;
type TransactionByteFee = TransactionByteFee;
Expand Down Expand Up @@ -594,7 +595,7 @@ construct_runtime! {
Timestamp: timestamp::{Module, Call, Storage, Inherent},
Indices: indices::{Module, Call, Storage, Config<T>, Event<T>},
Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: transaction_payment::{Module, Storage},
TransactionPayment: transaction_payment::{Module, Storage, Event},

// Consensus support.
Authorship: authorship::{Module, Call, Storage},
Expand Down

0 comments on commit 3b637f0

Please sign in to comment.