diff --git a/Cargo.lock b/Cargo.lock index e9829ca4633f..dd1f25a08f18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2268,6 +2268,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "smallvec 1.4.0", "sp-api", "sp-authority-discovery", "sp-block-builder", @@ -4341,6 +4342,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "smallvec 1.4.0", "sp-api", "sp-authority-discovery", "sp-block-builder", @@ -4508,6 +4510,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "smallvec 1.4.0", "sp-api", "sp-block-builder", "sp-consensus-babe", @@ -8536,6 +8539,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "smallvec 1.4.0", "sp-api", "sp-authority-discovery", "sp-block-builder", diff --git a/runtime/kusama/Cargo.toml b/runtime/kusama/Cargo.toml index 3b23e86cd4f9..24dc4a4d1f78 100644 --- a/runtime/kusama/Cargo.toml +++ b/runtime/kusama/Cargo.toml @@ -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 } diff --git a/runtime/kusama/src/constants.rs b/runtime/kusama/src/constants.rs index 8cc626f3c766..b0b692f9d265 100644 --- a/runtime/kusama/src/constants.rs +++ b/runtime/kusama/src/constants.rs @@ -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); @@ -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 for WeightToFee { - fn convert(x: Weight) -> Balance { + impl WeightToFeePolynomial for WeightToFee { + type Balance = Balance; + fn polynomial() -> WeightToFeeCoefficients { // 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, 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}; @@ -89,13 +99,15 @@ 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) + println!("Base: {}", ExtrinsicBaseWeight::get()); + 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) + println!("Base: {}", ExtrinsicBaseWeight::get()); + assert_eq!(WeightToFee::calc(&ExtrinsicBaseWeight::get()), CENTS / 10) } } diff --git a/runtime/polkadot/Cargo.toml b/runtime/polkadot/Cargo.toml index 5d57e0e99c2b..7fc9d2983492 100644 --- a/runtime/polkadot/Cargo.toml +++ b/runtime/polkadot/Cargo.toml @@ -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 } diff --git a/runtime/polkadot/src/constants.rs b/runtime/polkadot/src/constants.rs index a80d6fdc6e1e..27affd820e64 100644 --- a/runtime/polkadot/src/constants.rs +++ b/runtime/polkadot/src/constants.rs @@ -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); @@ -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 for WeightToFee { - fn convert(x: Weight) -> Balance { + impl WeightToFeePolynomial for WeightToFee { + type Balance = Balance; + fn polynomial() -> WeightToFeeCoefficients { // 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, 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) } } diff --git a/runtime/test-runtime/Cargo.toml b/runtime/test-runtime/Cargo.toml index 70b39ee68a66..6af1b8f39038 100644 --- a/runtime/test-runtime/Cargo.toml +++ b/runtime/test-runtime/Cargo.toml @@ -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 } diff --git a/runtime/test-runtime/src/constants.rs b/runtime/test-runtime/src/constants.rs index 203ce348cd96..b0431e55f268 100644 --- a/runtime/test-runtime/src/constants.rs +++ b/runtime/test-runtime/src/constants.rs @@ -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); @@ -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 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 { + 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, q), + coeff_integer: p / q, + }] } } } diff --git a/runtime/westend/Cargo.toml b/runtime/westend/Cargo.toml index a9e9a6ed0db0..c1097d68fe43 100644 --- a/runtime/westend/Cargo.toml +++ b/runtime/westend/Cargo.toml @@ -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 } diff --git a/runtime/westend/src/constants.rs b/runtime/westend/src/constants.rs index 8310e770fa6e..101fef913fd4 100644 --- a/runtime/westend/src/constants.rs +++ b/runtime/westend/src/constants.rs @@ -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); @@ -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 for WeightToFee { - fn convert(x: Weight) -> Balance { + impl WeightToFeePolynomial for WeightToFee { + type Balance = Balance; + fn polynomial() -> WeightToFeeCoefficients { // 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, 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) } }