FRAME: inherited call weight syntax (#13932)

* First approach on pallet::call_weight

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Use attr on pallet::call instead

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Ui tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Rename to weight(prefix = ...))

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Simplify to #[pallet::call(weight(T))]

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Add stray token error

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Cleanup

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Migrate remaining pallets

Using script from https://github.com/ggwpez/substrate-scripts/blob/e1b5ea5b5b4018867f3e869fce6f448b4ba9d71f/frame-code-migration/src/call_weight.rs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Try to add some docs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Revert "Migrate remaining pallets"

Lets do this as a follow-up, I dont want to bloat this small MR.

This reverts commit 331d4b42d72de1dacaed714d69166fa1bc9c92dd.

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Renames

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Review fixes

Co-authored-by: Sam Johnson <sam@durosoft.com>

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Test weights

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update UI tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/support/procedural/src/pallet/parse/mod.rs

Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com>

* Remove old code

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update docs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: parity-processbot <>
Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com>
This commit is contained in:
Oliver Tale-Yazdi
2023-04-27 16:08:08 +02:00
committed by GitHub
parent 6e5141fc23
commit b5201fa0ec
30 changed files with 698 additions and 92 deletions
@@ -1,5 +1,7 @@
error: Invalid pallet::call, requires weight attribute i.e. `#[pallet::weight($expr)]`
--> $DIR/call_missing_weight.rs:17:7
error: A pallet::call requires either a concrete `#[pallet::weight($expr)]` or an
inherited weight from the `#[pallet:call(weight($type))]` attribute, but
none were given.
--> tests/pallet_ui/call_missing_weight.rs:17:7
|
17 | pub fn foo(origin: OriginFor<T>) -> DispatchResultWithPostInfo {}
| ^^
@@ -0,0 +1,50 @@
use frame_support::pallet_prelude::*;
pub trait WeightInfo {
fn foo() -> Weight;
}
#[frame_support::pallet]
mod pallet {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(invalid)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call = invalid]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,11 @@
error: expected `weight`
--> tests/pallet_ui/call_weight_inherited_invalid.rs:19:17
|
19 | #[pallet::call(invalid)]
| ^^^^^^^
error: expected parentheses
--> tests/pallet_ui/call_weight_inherited_invalid.rs:40:17
|
40 | #[pallet::call = invalid]
| ^
@@ -0,0 +1,53 @@
// Weight is an ident instead of a type.
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub trait WeightInfo {
fn foo() -> Weight;
}
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(prefix))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight = prefix)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,11 @@
error[E0412]: cannot find type `prefix` in this scope
--> tests/pallet_ui/call_weight_inherited_invalid2.rs:22:24
|
22 | #[pallet::call(weight(prefix))]
| ^^^^^^ not found in this scope
error[E0412]: cannot find type `prefix` in this scope
--> tests/pallet_ui/call_weight_inherited_invalid2.rs:43:26
|
43 | #[pallet::call(weight = prefix)]
| ^^^^^^ not found in this scope
@@ -0,0 +1,53 @@
// Call weight is an LitInt instead of a type.
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub trait WeightInfo {
fn foo() -> Weight;
}
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(123))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight = 123)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,19 @@
error: expected one of: `for`, parentheses, `fn`, `unsafe`, `extern`, identifier, `::`, `<`, `dyn`, square brackets, `*`, `&`, `!`, `impl`, `_`, lifetime
--> tests/pallet_ui/call_weight_inherited_invalid3.rs:22:24
|
22 | #[pallet::call(weight(123))]
| ^^^
error: expected one of: `for`, parentheses, `fn`, `unsafe`, `extern`, identifier, `::`, `<`, `dyn`, square brackets, `*`, `&`, `!`, `impl`, `_`, lifetime
--> tests/pallet_ui/call_weight_inherited_invalid3.rs:43:26
|
43 | #[pallet::call(weight = 123)]
| ^^^
error: unused import: `frame_system::pallet_prelude::*`
--> tests/pallet_ui/call_weight_inherited_invalid3.rs:4:5
|
4 | use frame_system::pallet_prelude::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D unused-imports` implied by `-D warnings`
@@ -0,0 +1,52 @@
// Function does not exist in the trait.
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub trait WeightInfo {
}
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(<T as Config>::WeightInfo))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight = <T as Config>::WeightInfo)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,11 @@
error[E0599]: no function or associated item named `foo` found for associated type `<T as Config>::WeightInfo` in the current scope
--> tests/pallet_ui/call_weight_inherited_invalid4.rs:24:10
|
24 | pub fn foo(_: OriginFor<T>) -> DispatchResult {
| ^^^ function or associated item not found in `<T as Config>::WeightInfo`
error[E0599]: no function or associated item named `foo` found for associated type `<T as Config>::WeightInfo` in the current scope
--> tests/pallet_ui/call_weight_inherited_invalid4.rs:45:10
|
45 | pub fn foo(_: OriginFor<T>) -> DispatchResult {
| ^^^ function or associated item not found in `<T as Config>::WeightInfo`
@@ -0,0 +1,44 @@
// Stray tokens after good input.
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(<T as Config>::WeightInfo straycat))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight = <T as Config>::WeightInfo straycat)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,11 @@
error: unexpected token
--> tests/pallet_ui/call_weight_inherited_invalid5.rs:14:50
|
14 | #[pallet::call(weight(<T as Config>::WeightInfo straycat))]
| ^^^^^^^^
error: unexpected token
--> tests/pallet_ui/call_weight_inherited_invalid5.rs:34:52
|
34 | #[pallet::call(weight = <T as Config>::WeightInfo straycat)]
| ^^^^^^^^
@@ -1,7 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
@@ -1,11 +1,7 @@
error: Invalid pallet::call, requires weight attribute i.e. `#[pallet::weight($expr)]`
--> tests/pallet_ui/dev_mode_without_arg.rs:24:7
error: A pallet::call requires either a concrete `#[pallet::weight($expr)]` or an
inherited weight from the `#[pallet:call(weight($type))]` attribute, but
none were given.
--> tests/pallet_ui/dev_mode_without_arg.rs:22:7
|
24 | pub fn my_call(_origin: OriginFor<T>) -> DispatchResult {
22 | pub fn my_call(_origin: OriginFor<T>) -> DispatchResult {
| ^^
error[E0432]: unresolved import `pallet`
--> tests/pallet_ui/dev_mode_without_arg.rs:3:9
|
3 | pub use pallet::*;
| ^^^^^^ help: a similar path exists: `test_pallet::pallet`
@@ -0,0 +1,51 @@
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub trait WeightInfo {
fn foo() -> Weight;
}
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(<T as Config>::WeightInfo))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight = <T as Config>::WeightInfo)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,57 @@
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub trait WeightInfo {
fn foo() -> Weight;
}
impl WeightInfo for () {
fn foo() -> Weight {
Weight::zero()
}
}
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
// Crazy man just uses `()`, but it still works ;)
#[pallet::call(weight(()))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
// Crazy man just uses `()`, but it still works ;)
#[pallet::call(weight = ())]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,54 @@
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
// If, for whatever reason, you dont to not use a `WeightInfo` trait - it will still work.
struct Impl;
impl Impl {
fn foo() -> Weight {
Weight::zero()
}
}
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(crate::Impl))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight = crate::Impl)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,30 @@
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub trait WeightInfo {
fn foo() -> Weight;
}
#[frame_support::pallet(dev_mode)]
mod pallet {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(<T as Config>::WeightInfo))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}