Companion for #6076 (Allow fee calculation to happen off-chain) (#1111)

* Switch from Convert to WeightToFeePolynomial

* Bump

Co-authored-by: Gav Wood <gavin@parity.io>
This commit is contained in:
Alexander Theißen
2020-05-21 13:47:55 +02:00
committed by GitHub
parent 162baa62fb
commit b6829b6d21
10 changed files with 214 additions and 162 deletions
+137 -131
View File
File diff suppressed because it is too large Load Diff
+1
View File
@@ -13,6 +13,7 @@ rustc-hex = { version = "2.0.1", default-features = false }
serde = { version = "1.0.102", default-features = false } serde = { version = "1.0.102", default-features = false }
serde_derive = { version = "1.0.102", optional = true } serde_derive = { version = "1.0.102", optional = true }
static_assertions = "1.1.0" 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 } 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 } babe-primitives = { package = "sp-consensus-babe", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
+20 -8
View File
@@ -52,9 +52,11 @@ pub mod time {
pub mod fee { pub mod fee {
pub use sp_runtime::Perbill; pub use sp_runtime::Perbill;
use primitives::Balance; use primitives::Balance;
use frame_support::weights::Weight;
use sp_runtime::traits::Convert;
use runtime_common::ExtrinsicBaseWeight; 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. /// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25); 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 `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged. /// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee; pub struct WeightToFee;
impl Convert<Weight, Balance> for WeightToFee { impl WeightToFeePolynomial for WeightToFee {
fn convert(x: Weight) -> Balance { type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// in Kusama, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT: // 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)] #[cfg(test)]
mod tests { mod tests {
use sp_runtime::traits::Convert; use frame_support::weights::WeightToFeePolynomial;
use runtime_common::{MaximumBlockWeight, ExtrinsicBaseWeight}; use runtime_common::{MaximumBlockWeight, ExtrinsicBaseWeight};
use super::fee::WeightToFee; use super::fee::WeightToFee;
use super::currency::{CENTS, DOLLARS}; use super::currency::{CENTS, DOLLARS};
@@ -89,13 +99,15 @@ mod tests {
// This function tests that the fee for `MaximumBlockWeight` of weight is correct // This function tests that the fee for `MaximumBlockWeight` of weight is correct
fn full_block_fee_is_correct() { fn full_block_fee_is_correct() {
// A full block should cost 16 DOLLARS // 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] #[test]
// This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct // This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
fn extrinsic_base_fee_is_correct() { fn extrinsic_base_fee_is_correct() {
// `ExtrinsicBaseWeight` should cost 1/10 of a CENT // `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)
} }
} }
+1 -1
View File
@@ -389,7 +389,7 @@ impl collective::Trait<CouncilCollective> for Runtime {
type MaxProposals = CouncilMaxProposals; type MaxProposals = CouncilMaxProposals;
} }
const DESIRED_MEMBERS: u32 = 13; const DESIRED_MEMBERS: u32 = 17;
parameter_types! { parameter_types! {
pub const CandidacyBond: Balance = 1 * DOLLARS; pub const CandidacyBond: Balance = 1 * DOLLARS;
pub const VotingBond: Balance = 5 * CENTS; pub const VotingBond: Balance = 5 * CENTS;
+1
View File
@@ -13,6 +13,7 @@ rustc-hex = { version = "2.0.1", default-features = false }
serde = { version = "1.0.102", default-features = false } serde = { version = "1.0.102", default-features = false }
serde_derive = { version = "1.0.102", optional = true } serde_derive = { version = "1.0.102", optional = true }
static_assertions = "1.1.0" 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 } 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 } babe-primitives = { package = "sp-consensus-babe", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
+18 -8
View File
@@ -44,9 +44,11 @@ pub mod time {
pub mod fee { pub mod fee {
pub use sp_runtime::Perbill; pub use sp_runtime::Perbill;
use primitives::Balance; use primitives::Balance;
use frame_support::weights::Weight;
use sp_runtime::traits::Convert;
use runtime_common::ExtrinsicBaseWeight; 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. /// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25); 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 `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged. /// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee; pub struct WeightToFee;
impl Convert<Weight, Balance> for WeightToFee { impl WeightToFeePolynomial for WeightToFee {
fn convert(x: Weight) -> Balance { type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// in Polkadot, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT: // 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)] #[cfg(test)]
mod tests { mod tests {
use sp_runtime::traits::Convert;
use runtime_common::{MaximumBlockWeight, ExtrinsicBaseWeight}; use runtime_common::{MaximumBlockWeight, ExtrinsicBaseWeight};
use super::fee::WeightToFee; use super::fee::WeightToFee;
use super::currency::{CENTS, DOLLARS}; use super::currency::{CENTS, DOLLARS};
use frame_support::weights::WeightToFeePolynomial;
#[test] #[test]
// This function tests that the fee for `MaximumBlockWeight` of weight is correct // This function tests that the fee for `MaximumBlockWeight` of weight is correct
fn full_block_fee_is_correct() { fn full_block_fee_is_correct() {
// A full block should cost 16 DOLLARS // A full block should cost 16 DOLLARS
assert_eq!(WeightToFee::convert(MaximumBlockWeight::get()), 16 * DOLLARS) assert_eq!(WeightToFee::calc(&MaximumBlockWeight::get()), 16 * DOLLARS)
} }
#[test] #[test]
// This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct // This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
fn extrinsic_base_fee_is_correct() { fn extrinsic_base_fee_is_correct() {
// `ExtrinsicBaseWeight` should cost 1/10 of a CENT // `ExtrinsicBaseWeight` should cost 1/10 of a CENT
assert_eq!(WeightToFee::convert(ExtrinsicBaseWeight::get()), CENTS / 10) assert_eq!(WeightToFee::calc(&ExtrinsicBaseWeight::get()), CENTS / 10)
} }
} }
+1
View File
@@ -12,6 +12,7 @@ log = { version = "0.3.9", optional = true }
rustc-hex = { version = "2.0.1", default-features = false } rustc-hex = { version = "2.0.1", default-features = false }
serde = { version = "1.0.102", default-features = false } serde = { version = "1.0.102", default-features = false }
serde_derive = { version = "1.0.102", optional = true } 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 } 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 } sp-api = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
+16 -6
View File
@@ -46,8 +46,11 @@ pub mod time {
pub mod fee { pub mod fee {
pub use sp_runtime::Perbill; pub use sp_runtime::Perbill;
use primitives::Balance; use primitives::Balance;
use frame_support::weights::Weight; use runtime_common::ExtrinsicBaseWeight;
use sp_runtime::traits::Convert; use frame_support::weights::{
WeightToFeePolynomial, WeightToFeeCoefficient, WeightToFeeCoefficients,
};
use smallvec::smallvec;
/// The block saturation level. Fees will be updates based on this value. /// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25); 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 `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged. /// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee; pub struct WeightToFee;
impl Convert<Weight, Balance> for WeightToFee { impl WeightToFeePolynomial for WeightToFee {
fn convert(x: Weight) -> Balance { type Balance = Balance;
// in Kusama a weight of 10_000_000 (smallest non-zero weight) is mapped to 1/10 CENT: fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
Balance::from(x).saturating_mul(super::currency::CENTS / (10 * 10_000_000)) 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,
}]
} }
} }
} }
+1
View File
@@ -12,6 +12,7 @@ log = { version = "0.3.9", optional = true }
rustc-hex = { version = "2.0.1", default-features = false } rustc-hex = { version = "2.0.1", default-features = false }
serde = { version = "1.0.102", default-features = false } serde = { version = "1.0.102", default-features = false }
serde_derive = { version = "1.0.102", optional = true } 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 } 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 } babe-primitives = { package = "sp-consensus-babe", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
+18 -8
View File
@@ -44,9 +44,11 @@ pub mod time {
pub mod fee { pub mod fee {
pub use sp_runtime::Perbill; pub use sp_runtime::Perbill;
use primitives::Balance; use primitives::Balance;
use frame_support::weights::Weight;
use sp_runtime::traits::Convert;
use runtime_common::ExtrinsicBaseWeight; 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. /// The block saturation level. Fees will be updates based on this value.
pub const TARGET_BLOCK_FULLNESS: Perbill = Perbill::from_percent(25); 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 `0` will essentially disable the weight fee.
/// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged. /// - Setting it to `1` will cause the literal `#[weight = x]` values to be charged.
pub struct WeightToFee; pub struct WeightToFee;
impl Convert<Weight, Balance> for WeightToFee { impl WeightToFeePolynomial for WeightToFee {
fn convert(x: Weight) -> Balance { type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// in Westend, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 CENT: // 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)] #[cfg(test)]
mod tests { mod tests {
use sp_runtime::traits::Convert;
use runtime_common::{MaximumBlockWeight, ExtrinsicBaseWeight}; use runtime_common::{MaximumBlockWeight, ExtrinsicBaseWeight};
use super::fee::WeightToFee; use super::fee::WeightToFee;
use super::currency::{CENTS, DOLLARS}; use super::currency::{CENTS, DOLLARS};
use frame_support::weights::WeightToFeePolynomial;
#[test] #[test]
// This function tests that the fee for `MaximumBlockWeight` of weight is correct // This function tests that the fee for `MaximumBlockWeight` of weight is correct
fn full_block_fee_is_correct() { fn full_block_fee_is_correct() {
// A full block should cost 16 DOLLARS // A full block should cost 16 DOLLARS
assert_eq!(WeightToFee::convert(MaximumBlockWeight::get()), 16 * DOLLARS) assert_eq!(WeightToFee::calc(&MaximumBlockWeight::get()), 16 * DOLLARS)
} }
#[test] #[test]
// This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct // This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct
fn extrinsic_base_fee_is_correct() { fn extrinsic_base_fee_is_correct() {
// `ExtrinsicBaseWeight` should cost 1/10 of a CENT // `ExtrinsicBaseWeight` should cost 1/10 of a CENT
assert_eq!(WeightToFee::convert(ExtrinsicBaseWeight::get()), CENTS / 10) assert_eq!(WeightToFee::calc(&ExtrinsicBaseWeight::get()), CENTS / 10)
} }
} }