This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Transaction Fee Multiplier #2854
Merged
Merged
Changes from all commits
Commits
Show all changes
77 commits
Select commit
Hold shift + click to select a range
6c14a33
added fee calculations; need some type conversions
4meta5 20cc3e3
cleaned up make_payment and other stuff
4meta5 7e6133f
rename vars to compile
4meta5 3bfa6ee
add WeightToFee type
4meta5 a79beb2
clean test files after new type added to balances
4meta5 28bfe18
fmting
4meta5 8b686df
fix balance configs in tests
4meta5 a103566
more fixing mocks and tests
4meta5 5004f68
more comprehensive block weight limit test
4meta5 25016e5
fix compilation errors
4meta5 237f00b
more srml/executive tests && started fixing node/executor tests
4meta5 c473cd5
new fee multiplier; still overflows :(
4meta5 90501bc
perbill at the end attempt; needs to be changed
4meta5 838b4da
clean fmting, rename some vars
4meta5 074bac6
new PoC implementation.
kianenigma ec2d388
Merge branch 'amar/tx-fee-multiplier' of github.com:paritytech/substr…
kianenigma dcc8401
test weight_to_fee range and verify functionality
4meta5 ca81626
12 of 15 tests in node executor are passing
4meta5 a130696
1 test failing; big_block imports are failing for wrong reasons
4meta5 d9f4ffa
Update srml/executive/src/lib.rs
4meta5 6d8f3f0
Some cleanup.
kianenigma 7cd2ceb
consolidate tests in runtime impls
4meta5 68de4b9
clean and condition executive for stateful fee range test
4meta5 035cb70
remove comments to self
4meta5 4ef0384
Major cleanup.
kianenigma 83abfb7
Master.into() + Fix tests.
kianenigma e931f03
More cleanup.
kianenigma 74e0dd1
Merge branch 'master' of github.com:paritytech/substrate into amar/tx…
kianenigma 1297445
Fix lock files.
kianenigma 7ad863d
Master.into()
kianenigma 57691da
Fix build.
kianenigma 197c703
Update node-template/runtime/Cargo.toml
4meta5 da9174a
Update node/executor/src/lib.rs
4meta5 db1bfd6
Update node/executor/src/lib.rs
4meta5 af39f71
Update node/executor/src/lib.rs
4meta5 d98232b
Update node/executor/src/lib.rs
4meta5 f4ce8cb
Update node/executor/src/lib.rs
4meta5 c5fedee
Update node/executor/src/lib.rs
4meta5 6bf8c1f
Per-block update.
kianenigma bd9b8ad
Master.into() + Fix tests.
kianenigma 79a29c2
nit.
kianenigma 69f391f
Update docs.
kianenigma c25a910
Fix contracts test.
kianenigma 24aea1d
Stateful fee update.
kianenigma 1578763
Master.into()
kianenigma 480ce73
Update lock files.
kianenigma a7ad634
Update node/runtime/src/impls.rs
4meta5 75ddb98
Revamped again with fixed64.
kianenigma 5f88b16
Merge branch 'amar/tx-fee-multiplier' of github.com:paritytech/substr…
kianenigma 1a6033c
Master.into()
kianenigma 6d93145
fix cargo file.
kianenigma 3f0f30d
nits.
kianenigma e6db494
Some cleanup.
kianenigma 212b666
Some nits.
kianenigma e00f353
Master.into()
kianenigma a26b8c0
Fix build.
kianenigma d66ff3e
Bump.
kianenigma e5cec3f
Rename to WeightMultiplier
kianenigma 96aa67c
Master.into()
kianenigma 1c07451
Update node/executor/src/lib.rs
kianenigma 034129c
Add weight to election module mock.
kianenigma 0f011f5
Merge branch 'amar/tx-fee-multiplier' of github.com:paritytech/substr…
kianenigma d0a23a2
Fix build.
kianenigma a94d0a3
Master.into()
kianenigma 99bd89c
finalize merge
kianenigma 3b29928
Update srml/system/src/lib.rs
gavofyork f37c690
Master.into()
kianenigma 3e03fb3
Merge branch 'amar/tx-fee-multiplier' of github.com:paritytech/substr…
kianenigma 73c1ad8
Bring back fees.
kianenigma 7d21fca
Some nits.
kianenigma af14c50
Master.into()
kianenigma cb54a82
Code shifting for simplicity.
kianenigma 257d791
Fix build + more tests.
kianenigma cc72a6e
Update weights.rs
gavofyork 79aa423
Update core/sr-primitives/src/weights.rs
gavofyork 394157d
Update lib.rs
gavofyork 40ef12b
Fix test build
kianenigma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,18 +16,25 @@ | |
|
||
//! Primitives for transaction weighting. | ||
//! | ||
//! Each dispatch function within `decl_module!` can now have an optional | ||
//! `#[weight = $x]` attribute. $x can be any object that implements the | ||
//! `Weighable` trait. By default, All transactions are annotated by | ||
//! `#[weight = TransactionWeight::default()]`. | ||
//! Each dispatch function within `decl_module!` can have an optional `#[weight = $x]` attribute. | ||
//! $x can be any object that implements the `Weighable` trait. By default, All transactions are | ||
//! annotated by `#[weight = TransactionWeight::default()]`. | ||
//! | ||
//! Note that the decl_module macro _cannot_ enforce this and will simply fail | ||
//! if an invalid struct is passed in. | ||
//! Note that the decl_module macro _cannot_ enforce this and will simply fail if an invalid struct | ||
//! (something that does not implement `Weighable`) is passed in. | ||
|
||
use crate::{Fixed64, traits::Saturating}; | ||
use crate::codec::{Encode, Decode}; | ||
|
||
/// The final type that each `#[weight = $x:expr]`'s | ||
/// expression must evaluate to. | ||
pub type Weight = u32; | ||
|
||
/// Maximum block saturation: 4mb | ||
pub const MAX_TRANSACTIONS_WEIGHT: u32 = 4 * 1024 * 1024; | ||
/// Target block saturation: 25% of max block saturation = 1mb | ||
pub const IDEAL_TRANSACTIONS_WEIGHT: u32 = MAX_TRANSACTIONS_WEIGHT / 4; | ||
|
||
/// A `Call` enum (aka transaction) that can be weighted using the custom weight attribute of | ||
/// its dispatchable functions. Is implemented by default in the `decl_module!`. | ||
/// | ||
|
@@ -74,3 +81,76 @@ impl Default for TransactionWeight { | |
TransactionWeight::Basic(0, 1) | ||
} | ||
} | ||
|
||
/// Representation of a weight multiplier. This represents how a fee value can be computed from a | ||
/// weighted transaction. | ||
/// | ||
/// This is basically a wrapper for the `Fixed64` type a slightly tailored multiplication to u32 | ||
/// in the form of the `apply_to` method. | ||
#[cfg_attr(feature = "std", derive(Debug))] | ||
#[derive(Encode, Decode, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct WeightMultiplier(Fixed64); | ||
|
||
impl WeightMultiplier { | ||
/// Apply the inner Fixed64 as a weight multiplier to a weight value. | ||
/// | ||
/// This will perform a saturated `weight + weight * self.0`. | ||
pub fn apply_to(&self, weight: Weight) -> Weight { | ||
gavofyork marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should just be called |
||
self.0.saturated_multiply_accumulate(weight) | ||
} | ||
|
||
/// build self from raw parts per billion. | ||
#[cfg(feature = "std")] | ||
pub fn from_parts(parts: i64) -> Self { | ||
Self(Fixed64(parts)) | ||
} | ||
|
||
/// build self from a fixed64 value. | ||
pub fn from_fixed(f: Fixed64) -> Self { | ||
Self(f) | ||
} | ||
|
||
/// Approximate the fraction `n/d`. | ||
pub fn from_rational(n: i64, d: u64) -> Self { | ||
Self(Fixed64::from_rational(n, d)) | ||
} | ||
} | ||
|
||
impl Saturating for WeightMultiplier { | ||
fn saturating_add(self, rhs: Self) -> Self { | ||
Self(self.0.saturating_add(rhs.0)) | ||
} | ||
fn saturating_mul(self, rhs: Self) -> Self { | ||
Self(self.0.saturating_mul(rhs.0)) | ||
|
||
} | ||
fn saturating_sub(self, rhs: Self) -> Self { | ||
Self(self.0.saturating_sub(rhs.0)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn multiplier_apply_to_works() { | ||
let test_set = vec![0, 1, 10, 1000, 1_000_000_000]; | ||
|
||
// negative (1/2) | ||
let mut fm = WeightMultiplier::from_rational(-1, 2); | ||
test_set.clone().into_iter().for_each(|i| { assert_eq!(fm.apply_to(i) as i32, i as i32 - i as i32 / 2); }); | ||
|
||
// unit (1) multiplier | ||
fm = WeightMultiplier::from_parts(0); | ||
test_set.clone().into_iter().for_each(|i| { assert_eq!(fm.apply_to(i), i); }); | ||
|
||
// i.5 multiplier | ||
fm = WeightMultiplier::from_rational(1, 2); | ||
test_set.clone().into_iter().for_each(|i| { assert_eq!(fm.apply_to(i), i * 3 / 2); }); | ||
|
||
// dual multiplier | ||
fm = WeightMultiplier::from_rational(1, 1); | ||
test_set.clone().into_iter().for_each(|i| { assert_eq!(fm.apply_to(i), i * 2); }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
shouldn't checked_div be used here to avoid divide by zero issue?
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.
.max(0)