mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-29 10:17:57 +00:00
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:
committed by
GitHub
parent
6e5141fc23
commit
b5201fa0ec
@@ -15,8 +15,12 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use crate::{pallet::Def, COUNTER};
|
||||
use quote::ToTokens;
|
||||
use crate::{
|
||||
pallet::{parse::call::CallWeightDef, Def},
|
||||
COUNTER,
|
||||
};
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::{quote, ToTokens};
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
///
|
||||
@@ -74,15 +78,12 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
call_index_warnings.push(warning);
|
||||
}
|
||||
|
||||
let fn_weight = methods.iter().map(|method| &method.weight);
|
||||
let mut fn_weight = Vec::<TokenStream2>::new();
|
||||
let mut weight_warnings = Vec::new();
|
||||
for weight in fn_weight.clone() {
|
||||
if def.dev_mode {
|
||||
continue
|
||||
}
|
||||
|
||||
match weight {
|
||||
syn::Expr::Lit(lit) => {
|
||||
for method in &methods {
|
||||
match &method.weight {
|
||||
CallWeightDef::DevModeDefault => fn_weight.push(syn::parse_quote!(0)),
|
||||
CallWeightDef::Immediate(e @ syn::Expr::Lit(lit)) if !def.dev_mode => {
|
||||
let warning = proc_macro_warning::Warning::new_deprecated("ConstantWeight")
|
||||
.index(weight_warnings.len())
|
||||
.old("use hard-coded constant as call weight")
|
||||
@@ -91,10 +92,26 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
|
||||
.span(lit.span())
|
||||
.build();
|
||||
weight_warnings.push(warning);
|
||||
fn_weight.push(e.into_token_stream());
|
||||
},
|
||||
CallWeightDef::Immediate(e) => fn_weight.push(e.into_token_stream()),
|
||||
CallWeightDef::Inherited => {
|
||||
let pallet_weight = def
|
||||
.call
|
||||
.as_ref()
|
||||
.expect("we have methods; we have calls; qed")
|
||||
.inherited_call_weight
|
||||
.as_ref()
|
||||
.expect("the parser prevents this");
|
||||
|
||||
// Expand `<<T as Config>::WeightInfo>::call_name()`.
|
||||
let t = &pallet_weight.typename;
|
||||
let n = &method.name;
|
||||
fn_weight.push(quote!({ < #t > :: #n () }));
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
debug_assert_eq!(fn_weight.len(), methods.len());
|
||||
|
||||
let fn_doc = methods.iter().map(|method| &method.docs).collect::<Vec<_>>();
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use super::helper;
|
||||
use super::{helper, InheritedCallWeightAttr};
|
||||
use frame_support_procedural_tools::get_doc_literals;
|
||||
use quote::ToTokens;
|
||||
use std::collections::HashMap;
|
||||
@@ -46,6 +46,23 @@ pub struct CallDef {
|
||||
pub attr_span: proc_macro2::Span,
|
||||
/// Docs, specified on the impl Block.
|
||||
pub docs: Vec<syn::Expr>,
|
||||
/// The optional `weight` attribute on the `pallet::call`.
|
||||
pub inherited_call_weight: Option<InheritedCallWeightAttr>,
|
||||
}
|
||||
|
||||
/// The weight of a call.
|
||||
#[derive(Clone)]
|
||||
pub enum CallWeightDef {
|
||||
/// Explicitly set on the call itself with `#[pallet::weight(…)]`. This value is used.
|
||||
Immediate(syn::Expr),
|
||||
|
||||
/// The default value that should be set for dev-mode pallets. Usually zero.
|
||||
DevModeDefault,
|
||||
|
||||
/// Inherits whatever value is configured on the pallet level.
|
||||
///
|
||||
/// The concrete value is not known at this point.
|
||||
Inherited,
|
||||
}
|
||||
|
||||
/// Definition of dispatchable typically: `#[weight...] fn foo(origin .., param1: ...) -> ..`
|
||||
@@ -55,8 +72,8 @@ pub struct CallVariantDef {
|
||||
pub name: syn::Ident,
|
||||
/// Information on args: `(is_compact, name, type)`
|
||||
pub args: Vec<(bool, syn::Ident, Box<syn::Type>)>,
|
||||
/// Weight formula.
|
||||
pub weight: syn::Expr,
|
||||
/// Weight for the call.
|
||||
pub weight: CallWeightDef,
|
||||
/// Call index of the dispatchable.
|
||||
pub call_index: u8,
|
||||
/// Whether an explicit call index was specified.
|
||||
@@ -151,6 +168,7 @@ impl CallDef {
|
||||
index: usize,
|
||||
item: &mut syn::Item,
|
||||
dev_mode: bool,
|
||||
inherited_call_weight: Option<InheritedCallWeightAttr>,
|
||||
) -> syn::Result<Self> {
|
||||
let item_impl = if let syn::Item::Impl(item) = item {
|
||||
item
|
||||
@@ -228,17 +246,23 @@ impl CallDef {
|
||||
weight_attrs.push(FunctionAttr::Weight(empty_weight));
|
||||
}
|
||||
|
||||
if weight_attrs.len() != 1 {
|
||||
let msg = if weight_attrs.is_empty() {
|
||||
"Invalid pallet::call, requires weight attribute i.e. `#[pallet::weight($expr)]`"
|
||||
} else {
|
||||
"Invalid pallet::call, too many weight attributes given"
|
||||
};
|
||||
return Err(syn::Error::new(method.sig.span(), msg))
|
||||
}
|
||||
let weight = match weight_attrs.pop().unwrap() {
|
||||
FunctionAttr::Weight(w) => w,
|
||||
_ => unreachable!("checked during creation of the let binding"),
|
||||
let weight = match weight_attrs.len() {
|
||||
0 if inherited_call_weight.is_some() => CallWeightDef::Inherited,
|
||||
0 if dev_mode => CallWeightDef::DevModeDefault,
|
||||
0 => return Err(syn::Error::new(
|
||||
method.sig.span(),
|
||||
"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.",
|
||||
)),
|
||||
1 => match weight_attrs.pop().unwrap() {
|
||||
FunctionAttr::Weight(w) => CallWeightDef::Immediate(w),
|
||||
_ => unreachable!("checked during creation of the let binding"),
|
||||
},
|
||||
_ => {
|
||||
let msg = "Invalid pallet::call, too many weight attributes given";
|
||||
return Err(syn::Error::new(method.sig.span(), msg))
|
||||
},
|
||||
};
|
||||
|
||||
if call_idx_attrs.len() > 1 {
|
||||
@@ -321,6 +345,7 @@ impl CallDef {
|
||||
methods,
|
||||
where_clause: item_impl.generics.where_clause.clone(),
|
||||
docs: get_doc_literals(&item_impl.attrs),
|
||||
inherited_call_weight,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,8 +110,8 @@ impl Def {
|
||||
let m = hooks::HooksDef::try_from(span, index, item)?;
|
||||
hooks = Some(m);
|
||||
},
|
||||
Some(PalletAttr::RuntimeCall(span)) if call.is_none() =>
|
||||
call = Some(call::CallDef::try_from(span, index, item, dev_mode)?),
|
||||
Some(PalletAttr::RuntimeCall(cw, span)) if call.is_none() =>
|
||||
call = Some(call::CallDef::try_from(span, index, item, dev_mode, cw)?),
|
||||
Some(PalletAttr::Error(span)) if error.is_none() =>
|
||||
error = Some(error::ErrorDef::try_from(span, index, item)?),
|
||||
Some(PalletAttr::RuntimeEvent(span)) if event.is_none() =>
|
||||
@@ -402,6 +402,7 @@ impl GenericKind {
|
||||
mod keyword {
|
||||
syn::custom_keyword!(origin);
|
||||
syn::custom_keyword!(call);
|
||||
syn::custom_keyword!(weight);
|
||||
syn::custom_keyword!(event);
|
||||
syn::custom_keyword!(config);
|
||||
syn::custom_keyword!(hooks);
|
||||
@@ -425,7 +426,44 @@ enum PalletAttr {
|
||||
Config(proc_macro2::Span),
|
||||
Pallet(proc_macro2::Span),
|
||||
Hooks(proc_macro2::Span),
|
||||
RuntimeCall(proc_macro2::Span),
|
||||
/// A `#[pallet::call]` with optional attributes to specialize the behaviour.
|
||||
///
|
||||
/// # Attributes
|
||||
///
|
||||
/// Each attribute `attr` can take the form of `#[pallet::call(attr = …)]` or
|
||||
/// `#[pallet::call(attr(…))]`. The possible attributes are:
|
||||
///
|
||||
/// ## `weight`
|
||||
///
|
||||
/// Can be used to reduce the repetitive weight annotation in the trivial case. It accepts one
|
||||
/// argument that is expected to be an implementation of the `WeightInfo` or something that
|
||||
/// behaves syntactically equivalent. This allows to annotate a `WeightInfo` for all the calls.
|
||||
/// Now each call does not need to specify its own `#[pallet::weight]` but can instead use the
|
||||
/// one from the `#[pallet::call]` definition. So instead of having to write it on each call:
|
||||
///
|
||||
/// ```ignore
|
||||
/// #[pallet::call]
|
||||
/// impl<T: Config> Pallet<T> {
|
||||
/// #[pallet::weight(T::WeightInfo::create())]
|
||||
/// pub fn create(
|
||||
/// ```
|
||||
/// you can now omit it on the call itself, if the name of the weigh function matches the call:
|
||||
///
|
||||
/// ```ignore
|
||||
/// #[pallet::call(weight = <T as crate::Config>::WeightInfo)]
|
||||
/// impl<T: Config> Pallet<T> {
|
||||
/// pub fn create(
|
||||
/// ```
|
||||
///
|
||||
/// It is possible to use this syntax together with instantiated pallets by using `Config<I>`
|
||||
/// instead.
|
||||
///
|
||||
/// ### Dev Mode
|
||||
///
|
||||
/// Normally the `dev_mode` sets all weights of calls without a `#[pallet::weight]` annotation
|
||||
/// to zero. Now when there is a `weight` attribute on the `#[pallet::call]`, then that is used
|
||||
/// instead of the zero weight. So to say: it works together with `dev_mode`.
|
||||
RuntimeCall(Option<InheritedCallWeightAttr>, proc_macro2::Span),
|
||||
Error(proc_macro2::Span),
|
||||
RuntimeEvent(proc_macro2::Span),
|
||||
RuntimeOrigin(proc_macro2::Span),
|
||||
@@ -445,7 +483,7 @@ impl PalletAttr {
|
||||
Self::Config(span) => *span,
|
||||
Self::Pallet(span) => *span,
|
||||
Self::Hooks(span) => *span,
|
||||
Self::RuntimeCall(span) => *span,
|
||||
Self::RuntimeCall(_, span) => *span,
|
||||
Self::Error(span) => *span,
|
||||
Self::RuntimeEvent(span) => *span,
|
||||
Self::RuntimeOrigin(span) => *span,
|
||||
@@ -477,7 +515,12 @@ impl syn::parse::Parse for PalletAttr {
|
||||
} else if lookahead.peek(keyword::hooks) {
|
||||
Ok(PalletAttr::Hooks(content.parse::<keyword::hooks>()?.span()))
|
||||
} else if lookahead.peek(keyword::call) {
|
||||
Ok(PalletAttr::RuntimeCall(content.parse::<keyword::call>()?.span()))
|
||||
let span = content.parse::<keyword::call>().expect("peeked").span();
|
||||
let attr = match content.is_empty() {
|
||||
true => None,
|
||||
false => Some(InheritedCallWeightAttr::parse(&content)?),
|
||||
};
|
||||
Ok(PalletAttr::RuntimeCall(attr, span))
|
||||
} else if lookahead.peek(keyword::error) {
|
||||
Ok(PalletAttr::Error(content.parse::<keyword::error>()?.span()))
|
||||
} else if lookahead.peek(keyword::event) {
|
||||
@@ -505,3 +548,33 @@ impl syn::parse::Parse for PalletAttr {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The optional weight annotation on a `#[pallet::call]` like `#[pallet::call(weight($type))]`.
|
||||
#[derive(Clone)]
|
||||
pub struct InheritedCallWeightAttr {
|
||||
pub typename: syn::Type,
|
||||
pub span: proc_macro2::Span,
|
||||
}
|
||||
|
||||
impl syn::parse::Parse for InheritedCallWeightAttr {
|
||||
// Parses `(weight($type))` or `(weight = $type)`.
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
let content;
|
||||
syn::parenthesized!(content in input);
|
||||
content.parse::<keyword::weight>()?;
|
||||
let lookahead = content.lookahead1();
|
||||
|
||||
let buffer = if lookahead.peek(syn::token::Paren) {
|
||||
let inner;
|
||||
syn::parenthesized!(inner in content);
|
||||
inner
|
||||
} else if lookahead.peek(syn::Token![=]) {
|
||||
content.parse::<syn::Token![=]>().expect("peeked");
|
||||
content
|
||||
} else {
|
||||
return Err(lookahead.error())
|
||||
};
|
||||
|
||||
Ok(Self { typename: buffer.parse()?, span: input.span() })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,8 +61,8 @@ pub enum PalletStructAttr {
|
||||
impl PalletStructAttr {
|
||||
fn span(&self) -> proc_macro2::Span {
|
||||
match self {
|
||||
Self::GenerateStore { span, .. } => *span,
|
||||
Self::WithoutStorageInfoTrait(span) => *span,
|
||||
Self::GenerateStore { span, .. } |
|
||||
Self::WithoutStorageInfoTrait(span) |
|
||||
Self::StorageVersion { span, .. } => *span,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2600,7 +2600,7 @@ pub mod pallet_prelude {
|
||||
/// #[pallet::extra_constants]
|
||||
/// impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
/// /// Some description
|
||||
/// fn exra_constant_name() -> u128 { 4u128 }
|
||||
/// fn extra_constant_name() -> u128 { 4u128 }
|
||||
/// }
|
||||
///
|
||||
/// #[pallet::pallet]
|
||||
|
||||
@@ -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() {
|
||||
}
|
||||
Reference in New Issue
Block a user