This repository was 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.7k
Per-transaction weight for srml #2799
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
769c9bf
debug checkpoint.
kianenigma eb97440
new
kianenigma 9a1b4f9
Worked.
kianenigma 04a64ad
Worked and weight propagated to executive.
kianenigma 0dad22b
Works with some tests.
kianenigma f546e10
Cleanup debug prints.
kianenigma 89dd83b
Master.into()
kianenigma dea4232
More cleanup.
kianenigma 2ba3087
Undo more logs.
kianenigma da9b861
Master.into()
kianenigma 6f8f2e5
Undo a few more.
kianenigma 64796e2
Fix build.
kianenigma 07a72cb
Allow len to be used in weight calculation.
kianenigma f53ef4b
Remove noop function from dispath.
kianenigma 620e64e
Cleanup.
kianenigma 56495ab
Unify traits.
kianenigma ae124ea
Update docs and nits.
kianenigma 389e225
line width
kianenigma 66e957e
Update core/sr-primitives/src/weights.rs
kianenigma 1658bc6
Update core/sr-primitives/src/weights.rs
kianenigma c05f0f9
Update core/sr-primitives/src/weights.rs
kianenigma 18f8242
Update core/sr-primitives/src/weights.rs
kianenigma 5f69fe4
Update srml/example/src/lib.rs
kianenigma 152c1ab
Final cleanup.
kianenigma f14236b
Master.into()
kianenigma dcdad9d
Merge branch 'tx-weight-fee' of github.com:paritytech/substrate into …
kianenigma 18cecd1
Fix 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright 2019 Parity Technologies (UK) Ltd. | ||
| // This file is part of Substrate. | ||
|
|
||
| // Substrate is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Substrate is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Substrate. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! 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()]`. | ||
| //! | ||
| //! Note that the decl_module macro _cannot_ enforce this and will simply fail | ||
| //! if an invalid struct is passed in. | ||
|
|
||
| /// The final type that each `#[weight = $x:expr]`'s | ||
| /// expression must evaluate to. | ||
| pub type Weight = u32; | ||
|
|
||
| /// 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!`. | ||
| /// | ||
| /// Both the outer Call enum and the per-module individual ones will implement this. | ||
| /// The outer enum simply calls the inner ones based on call type. | ||
| pub trait Weighable { | ||
| /// Return the weight of this call. | ||
| /// The `len` argument is the encoded length of the transaction/call. | ||
| fn weight(&self, len: usize) -> Weight; | ||
| } | ||
|
|
||
| /// Default type used as the weight representative in a `#[weight = x]` attribute. | ||
| /// | ||
| /// A user may pass in any other type that implements [`Weighable`]. If not, the `Default` | ||
| /// implementation of [`TransactionWeight`] is used. | ||
| pub enum TransactionWeight { | ||
| /// Basic weight (base, byte). | ||
| /// The values contained are the base weight and byte weight respectively. | ||
| Basic(Weight, Weight), | ||
| /// Maximum fee. This implies that this transaction _might_ get included but | ||
| /// no more transaction can be added. This can be done by setting the | ||
| /// implementation to _maximum block weight_. | ||
| Max, | ||
| /// Free. The transaction does not increase the total weight | ||
| /// (i.e. is not included in weight calculation). | ||
| Free, | ||
| } | ||
|
|
||
| impl Weighable for TransactionWeight { | ||
| fn weight(&self, len: usize) -> Weight { | ||
| match self { | ||
| TransactionWeight::Basic(base, byte) => base + byte * len as Weight, | ||
| TransactionWeight::Max => 3 * 1024 * 1024, | ||
| TransactionWeight::Free => 0, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Default for TransactionWeight { | ||
| fn default() -> Self { | ||
| // This implies that the weight is currently equal to tx-size, nothing more | ||
| // for all substrate transactions that do NOT explicitly annotate weight. | ||
| // TODO #2431 needs to be updated with proper max values. | ||
| TransactionWeight::Basic(0, 1) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
I just realised that we could calculate max given current block weight (we would have to pass it to the function along with
len) - then you could just return whatever is left in a block.I don't like it that much though, due to additional argument that has to be passed so if we have a better solution for longer term or we are satisfied enough with this, then let's just leave it as is.
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.
What you propose will actually work (haven't tried but shouldn't have a problem).
The potential long term solution is the other approach I explained here. I think in that design the weight calculation is internal to the
Module<T>and can simply querysystem. I am still not fully convinced of how that should work (+ if it is absolutely needed) hence rather get this stable and clean first to be able to proceed to fee + tip steps and then refactor if needed (hopefully a decision I won't regret ♻️)