-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Price to Weight ratio as type parameter #3856
Conversation
|
It looks like @JoshOrndorff signed our Contributor License Agreement. 👍 Many thanks, Parity Technologies CLA Bot |
|
I do recall our discussion, but can't quite see what the PR is trying to achieve here. Can you elaborate a bit more? I think the logic of a simple coefficient multiplier is simple enough to be handled by just one struct, rather than one encapsulating the number and one encapsulating the multiplication. What do you think? |
|
Although, maybe @thiolliere would be happier like this? you commented that you think having both |
e0cf386 to
2fe7e7f
Compare
Here's what the two types are representing
Perhaps this could exemplified more simply with a more general example. Consider this alternative weight to fee converter. /// Convert from weight to balance via a quadratic curve
/// The type parameters encapsulate the coefficients
pub struct QuadraticWeight<C0, C1, C2>(C0, C1, C2);
impl<C0, C1, C2> Convert<Weight, Balance> for QuadraticWeight<C0, C1, C2>
where C0: Get<Balance>, C1: Get<Balance>, C2: Get<Balance> {
fn convert(w: Weight) -> Balance {
let c0 = C0::get();
let c1 = C1::get();
let c2 = C2::get();
let w = Balance::from(w);
//TODO use saturating arithmetic
c0 + c1 * w + c2 * w * w
}
}Then to actually use the quadratic converter in your runtime you would need to do something like this parameter_types! {
pub const TransactionBaseFee: Balance = 1 * CENTS;
pub const TransactionByteFee: Balance = 10 * MILLICENTS;
// Now there are even more structs because there are even more constants
pub const WeightFeeConstant: Balance = 1_000;
pub const WeightFeeLinear: Balance = 100;
pub const WeightFeeQuadratic : Balance = 10;
// for a sane configuration, this should always be less than `AvailableBlockRatio`.
pub const TargetedFeeAdjustment: Perbill = Perbill::from_percent(25);
}The above code separate the ultimate weight to fee calculation into four structs. One actual struct that represents quadratic calculation, and three that just wrap the constants. It wouldn't make sense to implement the entire conversion on any one of the three constant-wrapping types here. |
|
I still like this idea, but I also had another idea that you may prefer. New idea: Turn an instance of parameter_types!{
// --snip--
pub const WeightToFee: LinearWeightFee = LinearWeightFee{ coefficient: 10_000 };
}I've prototyped it 420e162 but I can't get it to compile. I'll wait for feedback before doing anything else. |
|
Separating the coefficient into a separate struct only makes sense in an example like in Even if not in node-runtime, we should put this example in the docs or maybe even in node-template. Nonetheless, I will add some naming suggestions here to make it better, then we can merge. |
I don't think you should pursue this. Using |
node/runtime/src/impls.rs
Outdated
| Balance::from(x).saturating_mul(Self::get()) | ||
| /// 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>(M); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| pub struct LinearWeight<M>(M); | |
| pub struct LinearWeightToFee<C>(C); |
(Sorry if I am taking back my own word :D, if it is a coefficient then C makes more sense. sry)
node/runtime/src/impls.rs
Outdated
| 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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| let multiplier = M::get(); | |
| let coefficient = C::get(); |
node/runtime/src/lib.rs
Outdated
| 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| pub const TransactionWeightFee: Balance = 1_000; | |
| pub const WeightFeeCoefficient: Balance = 1_000; |
node/runtime/src/lib.rs
Outdated
| type TransactionBaseFee = TransactionBaseFee; | ||
| type TransactionByteFee = TransactionByteFee; | ||
| type WeightToFee = WeightToFee; | ||
| type WeightToFee = LinearWeight<TransactionWeightFee>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| type WeightToFee = LinearWeight<TransactionWeightFee>; | |
| type WeightToFee = LinearWeightToFee< WeightFeeCoefficient >; |
kianenigma
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some naming changes and then good to go
|
Yes I think I like this, the parameter type is only used in the same file, and there is no like hidden implementation on top of it in another file. Also maybe LinearWeight structure could be moved to srml/transaction-payment as an optionnal useful structure to use. |
|
I've included the renames @kianenigma requested. I'm ready to merge this back into #3823 and if necessary finish any last pieces there. |
* Better fee parameters * Fix build * Better runtime tests * Price to Weight ratio as type parameter (#3856) * Price to Weight ration as type parameter * Kian feedback * Some renames. * Fix executor tests * Getting Closer. * Phantom Data * Actually fix executor tests. * Fix tests. * Remove todo * Fix build
Builds on top of #3823 but has both desirable properties:
parameter_types!are used as associated types withget@kianenigma and I had both previously wanted to use an instance of a struct to calculate weight fees because we could give it a field to represent the slope. But
transaction_payment::Traitrequires an associated type. Generating a type when you really want a constant is exactly whatparameter_types!is for. The solution is to make the fee:weight ratio itself a type parameter for the struct as I've done here.