From 96e5728621b56028efe9bcc19aedbe0f26cd921a Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Sat, 19 Oct 2019 01:32:59 -0400 Subject: [PATCH 1/7] Price to Weight ration as type parameter --- node/runtime/src/impls.rs | 17 ++++++++++++----- node/runtime/src/lib.rs | 6 +++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/node/runtime/src/impls.rs b/node/runtime/src/impls.rs index ae53c59a057a5..1db01c267d074 100644 --- a/node/runtime/src/impls.rs +++ b/node/runtime/src/impls.rs @@ -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, }; @@ -49,10 +49,17 @@ impl Convert for CurrencyToVoteHandler { fn convert(x: u128) -> Balance { x * Self::factor() } } -/// Simply multiply by a coefficient denoted by the `Get` implementation. -impl Convert for WeightToFee { - fn convert(x: Weight) -> Balance { - Balance::from(x).saturating_mul(Self::get()) +// M is the coefficient in units of currency / weight +pub struct LinearWeight(Multiplier); + +impl Convert for LinearWeight + where Multiplier: Get { + + 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 multiplier = Multiplier::get(); + Balance::from(w).saturating_mul(multiplier) } } diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index 944ef5127d40b..ee0d33e109788 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -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, LinearWeight}; /// Constant values used within the runtime. pub mod constants; @@ -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 TransactionWeightFee: Balance = 1_000; // for a sane configuration, this should always be less than `AvailableBlockRatio`. pub const TargetedFeeAdjustment: Perbill = Perbill::from_percent(25); } @@ -188,7 +188,7 @@ impl transaction_payment::Trait for Runtime { type OnTransactionPayment = DealWithFees; type TransactionBaseFee = TransactionBaseFee; type TransactionByteFee = TransactionByteFee; - type WeightToFee = WeightToFee; + type WeightToFee = LinearWeight; type FeeMultiplierUpdate = TargetedFeeAdjustment; } From 2fe7e7feee5428425dd143f81f1b86d481913a67 Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Sat, 19 Oct 2019 10:54:14 -0400 Subject: [PATCH 2/7] Kian feedback --- node/runtime/src/impls.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/node/runtime/src/impls.rs b/node/runtime/src/impls.rs index 1db01c267d074..ba1b4a6a3e65a 100644 --- a/node/runtime/src/impls.rs +++ b/node/runtime/src/impls.rs @@ -49,16 +49,17 @@ impl Convert for CurrencyToVoteHandler { fn convert(x: u128) -> Balance { x * Self::factor() } } -// M is the coefficient in units of currency / weight -pub struct LinearWeight(Multiplier); +/// Convert from weight to balance via a simple coefficient multiplication +/// The associated type M encapsulates a constant in units of balance per weight +pub struct LinearWeight(M); -impl Convert for LinearWeight - where Multiplier: Get { +impl Convert for LinearWeight + where M: Get { 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 multiplier = Multiplier::get(); + let multiplier = M::get(); Balance::from(w).saturating_mul(multiplier) } } From a50d911ebc682e8f9dcc5432dfc048e1c8818fa8 Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Mon, 21 Oct 2019 09:47:03 -0400 Subject: [PATCH 3/7] Some renames. --- node/runtime/src/impls.rs | 12 ++++++------ node/runtime/src/lib.rs | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/node/runtime/src/impls.rs b/node/runtime/src/impls.rs index ba1b4a6a3e65a..e61ff9d0aa856 100644 --- a/node/runtime/src/impls.rs +++ b/node/runtime/src/impls.rs @@ -50,17 +50,17 @@ impl Convert for CurrencyToVoteHandler { } /// Convert from weight to balance via a simple coefficient multiplication -/// The associated type M encapsulates a constant in units of balance per weight -pub struct LinearWeight(M); +/// The associated type C encapsulates a constant in units of balance per weight +pub struct LinearWeightToFee(C); -impl Convert for LinearWeight - where M: Get { +impl Convert for LinearWeightToFee + where C: Get { 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 multiplier = M::get(); - Balance::from(w).saturating_mul(multiplier) + let coefficient = C::get(); + Balance::from(w).saturating_mul(coefficient) } } diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index ee0d33e109788..25e3ad1dff588 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -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, LinearWeight}; +use impls::{CurrencyToVoteHandler, Author, LinearWeightToFee}; /// Constant values used within the runtime. pub mod constants; @@ -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 TransactionWeightFee: 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); } @@ -188,7 +188,7 @@ impl transaction_payment::Trait for Runtime { type OnTransactionPayment = DealWithFees; type TransactionBaseFee = TransactionBaseFee; type TransactionByteFee = TransactionByteFee; - type WeightToFee = LinearWeight; + type WeightToFee = LinearWeightToFee; type FeeMultiplierUpdate = TargetedFeeAdjustment; } From 48f8596923a2e42143158bd698272933479e35fd Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Mon, 21 Oct 2019 11:25:47 -0400 Subject: [PATCH 4/7] Fix executor tests --- node/executor/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/node/executor/src/lib.rs b/node/executor/src/lib.rs index f5cb7c8c64bb6..bab9dbddfcce7 100644 --- a/node/executor/src/lib.rs +++ b/node/executor/src/lib.rs @@ -56,7 +56,7 @@ mod tests { use node_runtime::{ Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances, BuildStorage, System, TransactionPayment, Event, TransferFee, TransactionBaseFee, TransactionByteFee, - WeightToFee, constants::currency::*, + LinearWeightToFee, constants::currency::*, }; use node_primitives::{Balance, Hash, BlockNumber}; use node_testing::keyring::*; @@ -94,7 +94,7 @@ mod tests { (extrinsic.encode().len() as Balance); let weight = default_transfer_call().get_dispatch_info().weight; - let weight_fee = ::WeightToFee::convert(weight); + let weight_fee = ::LinearWeightToFee::convert(weight); fee_multiplier.saturated_multiply_accumulate(length_fee + weight_fee) + TransferFee::get() } @@ -1046,7 +1046,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::convert(weight); // we know that weight to fee multiplier is effect-less in block 1. assert_eq!(weight_fee as Balance, MILLICENTS); From 9fcb533f975dc12d7958138aa98fbe2ac508fa50 Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Mon, 21 Oct 2019 11:49:36 -0400 Subject: [PATCH 5/7] Getting Closer. --- node/executor/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/executor/src/lib.rs b/node/executor/src/lib.rs index bab9dbddfcce7..332517ff5840e 100644 --- a/node/executor/src/lib.rs +++ b/node/executor/src/lib.rs @@ -94,7 +94,7 @@ mod tests { (extrinsic.encode().len() as Balance); let weight = default_transfer_call().get_dispatch_info().weight; - let weight_fee = ::LinearWeightToFee::convert(weight); + let weight_fee = ::WeightToFee::convert(weight); fee_multiplier.saturated_multiply_accumulate(length_fee + weight_fee) + TransferFee::get() } From 1dee1abb9492230bb5e164191ffa5924c14196b1 Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Mon, 21 Oct 2019 14:32:53 -0400 Subject: [PATCH 6/7] Phantom Data --- node/runtime/src/impls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/runtime/src/impls.rs b/node/runtime/src/impls.rs index e61ff9d0aa856..26a90fe6097f8 100644 --- a/node/runtime/src/impls.rs +++ b/node/runtime/src/impls.rs @@ -51,7 +51,7 @@ impl Convert for CurrencyToVoteHandler { /// 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); +pub struct LinearWeightToFee(rstd::marker::PhantomData); impl Convert for LinearWeightToFee where C: Get { From 849ca60d9fda28b3ac0a786d7718753cbae76895 Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Mon, 21 Oct 2019 14:33:23 -0400 Subject: [PATCH 7/7] Actually fix executor tests. --- node/executor/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/node/executor/src/lib.rs b/node/executor/src/lib.rs index 332517ff5840e..cb8627f65beab 100644 --- a/node/executor/src/lib.rs +++ b/node/executor/src/lib.rs @@ -56,8 +56,9 @@ mod tests { use node_runtime::{ Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances, BuildStorage, System, TransactionPayment, Event, TransferFee, TransactionBaseFee, TransactionByteFee, - LinearWeightToFee, constants::currency::*, + WeightFeeCoefficient, constants::currency::*, }; + use node_runtime::impls::LinearWeightToFee; use node_primitives::{Balance, Hash, BlockNumber}; use node_testing::keyring::*; use wabt; @@ -1046,7 +1047,7 @@ mod tests { balance_alice -= length_fee; let weight = default_transfer_call().get_dispatch_info().weight; - let weight_fee = LinearWeightToFee::convert(weight); + let weight_fee = LinearWeightToFee::::convert(weight); // we know that weight to fee multiplier is effect-less in block 1. assert_eq!(weight_fee as Balance, MILLICENTS);