mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 09:21:05 +00:00
Relocate weight to paint + decouple from extensions (#4124)
* Fisr migrated version of weight to paint * Bump * Minor nits * Some review fixes. * Line width * Revert spec bump * Fix build * Update lock file * Update palette/executive/src/lib.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update palette/membership/src/lib.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
@@ -22,7 +22,6 @@ use crate::traits::{
|
||||
};
|
||||
#[allow(deprecated)]
|
||||
use crate::traits::ValidateUnsigned;
|
||||
use crate::weights::{GetDispatchInfo, DispatchInfo};
|
||||
use crate::transaction_validity::TransactionValidity;
|
||||
|
||||
/// Definition of something that the external world might want to say; its
|
||||
@@ -38,16 +37,18 @@ pub struct CheckedExtrinsic<AccountId, Call, Extra> {
|
||||
pub function: Call,
|
||||
}
|
||||
|
||||
impl<AccountId, Call, Extra, Origin> traits::Applyable for
|
||||
impl<AccountId, Call, Extra, Origin, Info> traits::Applyable for
|
||||
CheckedExtrinsic<AccountId, Call, Extra>
|
||||
where
|
||||
AccountId: Member + MaybeDisplay,
|
||||
Call: Member + Dispatchable<Origin=Origin>,
|
||||
Extra: SignedExtension<AccountId=AccountId, Call=Call>,
|
||||
Extra: SignedExtension<AccountId=AccountId, Call=Call, DispatchInfo=Info>,
|
||||
Origin: From<Option<AccountId>>,
|
||||
Info: Clone,
|
||||
{
|
||||
type AccountId = AccountId;
|
||||
type Call = Call;
|
||||
type DispatchInfo = Info;
|
||||
|
||||
fn sender(&self) -> Option<&Self::AccountId> {
|
||||
self.signed.as_ref().map(|x| &x.0)
|
||||
@@ -56,11 +57,11 @@ where
|
||||
#[allow(deprecated)] // Allow ValidateUnsigned
|
||||
fn validate<U: ValidateUnsigned<Call = Self::Call>>(
|
||||
&self,
|
||||
info: DispatchInfo,
|
||||
info: Self::DispatchInfo,
|
||||
len: usize,
|
||||
) -> TransactionValidity {
|
||||
if let Some((ref id, ref extra)) = self.signed {
|
||||
Extra::validate(extra, id, &self.function, info, len)
|
||||
Extra::validate(extra, id, &self.function, info.clone(), len)
|
||||
} else {
|
||||
let valid = Extra::validate_unsigned(&self.function, info, len)?;
|
||||
let unsigned_validation = U::validate_unsigned(&self.function)?;
|
||||
@@ -71,28 +72,19 @@ where
|
||||
#[allow(deprecated)] // Allow ValidateUnsigned
|
||||
fn apply<U: ValidateUnsigned<Call=Self::Call>>(
|
||||
self,
|
||||
info: DispatchInfo,
|
||||
info: Self::DispatchInfo,
|
||||
len: usize,
|
||||
) -> crate::ApplyResult {
|
||||
let (maybe_who, pre) = if let Some((id, extra)) = self.signed {
|
||||
let pre = Extra::pre_dispatch(extra, &id, &self.function, info, len)?;
|
||||
let pre = Extra::pre_dispatch(extra, &id, &self.function, info.clone(), len)?;
|
||||
(Some(id), pre)
|
||||
} else {
|
||||
let pre = Extra::pre_dispatch_unsigned(&self.function, info, len)?;
|
||||
let pre = Extra::pre_dispatch_unsigned(&self.function, info.clone(), len)?;
|
||||
U::pre_dispatch(&self.function)?;
|
||||
(None, pre)
|
||||
};
|
||||
let res = self.function.dispatch(Origin::from(maybe_who));
|
||||
Extra::post_dispatch(pre, info, len);
|
||||
Extra::post_dispatch(pre, info.clone(), len);
|
||||
Ok(res.map_err(Into::into))
|
||||
}
|
||||
}
|
||||
|
||||
impl<AccountId, Call, Extra> GetDispatchInfo for CheckedExtrinsic<AccountId, Call, Extra>
|
||||
where
|
||||
Call: GetDispatchInfo,
|
||||
{
|
||||
fn get_dispatch_info(&self) -> DispatchInfo {
|
||||
self.function.get_dispatch_info()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ use codec::{Decode, Encode, EncodeLike, Input, Error};
|
||||
use crate::{
|
||||
traits::{self, Member, MaybeDisplay, SignedExtension, Checkable, Extrinsic, IdentifyAccount},
|
||||
generic::CheckedExtrinsic, transaction_validity::{TransactionValidityError, InvalidTransaction},
|
||||
weights::{GetDispatchInfo, DispatchInfo},
|
||||
};
|
||||
|
||||
const TRANSACTION_VERSION: u8 = 4;
|
||||
@@ -280,17 +279,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<Address, Call, Signature, Extra> GetDispatchInfo
|
||||
for UncheckedExtrinsic<Address, Call, Signature, Extra>
|
||||
where
|
||||
Call: GetDispatchInfo,
|
||||
Extra: SignedExtension,
|
||||
{
|
||||
fn get_dispatch_info(&self) -> DispatchInfo {
|
||||
self.function.get_dispatch_info()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -331,6 +319,7 @@ mod tests {
|
||||
type AccountId = u64;
|
||||
type Call = ();
|
||||
type AdditionalSigned = ();
|
||||
type DispatchInfo = ();
|
||||
type Pre = ();
|
||||
|
||||
fn additional_signed(&self) -> rstd::result::Result<(), TransactionValidityError> { Ok(()) }
|
||||
|
||||
@@ -52,7 +52,6 @@ pub mod offchain;
|
||||
pub mod testing;
|
||||
pub mod traits;
|
||||
pub mod transaction_validity;
|
||||
pub mod weights;
|
||||
|
||||
/// Re-export these since they're only "kind of" generic.
|
||||
pub use generic::{DigestItem, Digest};
|
||||
|
||||
@@ -26,7 +26,6 @@ use crate::traits::{
|
||||
#[allow(deprecated)]
|
||||
use crate::traits::ValidateUnsigned;
|
||||
use crate::{generic, KeyTypeId, ApplyResult};
|
||||
use crate::weights::{GetDispatchInfo, DispatchInfo};
|
||||
pub use primitives::{H256, sr25519};
|
||||
use primitives::{crypto::{CryptoType, Dummy, key_types, Public}, U256};
|
||||
use crate::transaction_validity::{TransactionValidity, TransactionValidityError};
|
||||
@@ -326,13 +325,15 @@ impl<Call: Codec + Sync + Send, Extra> traits::Extrinsic for TestXt<Call, Extra>
|
||||
}
|
||||
}
|
||||
|
||||
impl<Origin, Call, Extra> Applyable for TestXt<Call, Extra> where
|
||||
impl<Origin, Call, Extra, Info> Applyable for TestXt<Call, Extra> where
|
||||
Call: 'static + Sized + Send + Sync + Clone + Eq + Codec + Debug + Dispatchable<Origin=Origin>,
|
||||
Extra: SignedExtension<AccountId=u64, Call=Call>,
|
||||
Extra: SignedExtension<AccountId=u64, Call=Call, DispatchInfo=Info>,
|
||||
Origin: From<Option<u64>>,
|
||||
Info: Clone,
|
||||
{
|
||||
type AccountId = u64;
|
||||
type Call = Call;
|
||||
type DispatchInfo = Info;
|
||||
|
||||
fn sender(&self) -> Option<&Self::AccountId> { self.0.as_ref().map(|x| &x.0) }
|
||||
|
||||
@@ -340,7 +341,7 @@ impl<Origin, Call, Extra> Applyable for TestXt<Call, Extra> where
|
||||
#[allow(deprecated)] // Allow ValidateUnsigned
|
||||
fn validate<U: ValidateUnsigned<Call=Self::Call>>(
|
||||
&self,
|
||||
_info: DispatchInfo,
|
||||
_info: Self::DispatchInfo,
|
||||
_len: usize,
|
||||
) -> TransactionValidity {
|
||||
Ok(Default::default())
|
||||
@@ -351,7 +352,7 @@ impl<Origin, Call, Extra> Applyable for TestXt<Call, Extra> where
|
||||
#[allow(deprecated)] // Allow ValidateUnsigned
|
||||
fn apply<U: ValidateUnsigned<Call=Self::Call>>(
|
||||
self,
|
||||
info: DispatchInfo,
|
||||
info: Self::DispatchInfo,
|
||||
len: usize,
|
||||
) -> ApplyResult {
|
||||
let maybe_who = if let Some((who, extra)) = self.0 {
|
||||
@@ -365,13 +366,3 @@ impl<Origin, Call, Extra> Applyable for TestXt<Call, Extra> where
|
||||
Ok(self.1.dispatch(maybe_who.into()).map_err(Into::into))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Call: Encode, Extra: Encode> GetDispatchInfo for TestXt<Call, Extra> {
|
||||
fn get_dispatch_info(&self) -> DispatchInfo {
|
||||
// for testing: weight == size.
|
||||
DispatchInfo {
|
||||
weight: self.encode().len() as _,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ use crate::transaction_validity::{
|
||||
ValidTransaction, TransactionValidity, TransactionValidityError, UnknownTransaction,
|
||||
};
|
||||
use crate::generic::{Digest, DigestItem};
|
||||
use crate::weights::DispatchInfo;
|
||||
pub use arithmetic::traits::{
|
||||
SimpleArithmetic, UniqueSaturatedInto, UniqueSaturatedFrom, Saturating, SaturatedConversion,
|
||||
Zero, One, Bounded, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv,
|
||||
@@ -713,6 +712,11 @@ pub trait SignedExtension: Codec + Debug + Sync + Send + Clone + Eq + PartialEq
|
||||
/// The type that encodes information that can be passed from pre_dispatch to post-dispatch.
|
||||
type Pre: Default;
|
||||
|
||||
/// An opaque set of information attached to the transaction. This could be constructed anywhere
|
||||
/// down the line in a runtime. The current substrate runtime uses a struct with the same name
|
||||
/// to represent the dispatch class and weight.
|
||||
type DispatchInfo: Clone;
|
||||
|
||||
/// Construct any additional data that should be in the signed payload of the transaction. Can
|
||||
/// also perform any pre-signature-verification checks and return an error if needed.
|
||||
fn additional_signed(&self) -> Result<Self::AdditionalSigned, TransactionValidityError>;
|
||||
@@ -730,7 +734,7 @@ pub trait SignedExtension: Codec + Debug + Sync + Send + Clone + Eq + PartialEq
|
||||
&self,
|
||||
_who: &Self::AccountId,
|
||||
_call: &Self::Call,
|
||||
_info: DispatchInfo,
|
||||
_info: Self::DispatchInfo,
|
||||
_len: usize,
|
||||
) -> TransactionValidity {
|
||||
Ok(ValidTransaction::default())
|
||||
@@ -748,10 +752,10 @@ pub trait SignedExtension: Codec + Debug + Sync + Send + Clone + Eq + PartialEq
|
||||
self,
|
||||
who: &Self::AccountId,
|
||||
call: &Self::Call,
|
||||
info: DispatchInfo,
|
||||
info: Self::DispatchInfo,
|
||||
len: usize,
|
||||
) -> Result<Self::Pre, crate::ApplyError> {
|
||||
self.validate(who, call, info, len)
|
||||
self.validate(who, call, info.clone(), len)
|
||||
.map(|_| Self::Pre::default())
|
||||
.map_err(Into::into)
|
||||
}
|
||||
@@ -766,7 +770,7 @@ pub trait SignedExtension: Codec + Debug + Sync + Send + Clone + Eq + PartialEq
|
||||
/// Make sure to perform the same checks in `pre_dispatch_unsigned` function.
|
||||
fn validate_unsigned(
|
||||
_call: &Self::Call,
|
||||
_info: DispatchInfo,
|
||||
_info: Self::DispatchInfo,
|
||||
_len: usize,
|
||||
) -> TransactionValidity {
|
||||
Ok(ValidTransaction::default())
|
||||
@@ -782,16 +786,16 @@ pub trait SignedExtension: Codec + Debug + Sync + Send + Clone + Eq + PartialEq
|
||||
/// perform the same validation as in `validate_unsigned`.
|
||||
fn pre_dispatch_unsigned(
|
||||
call: &Self::Call,
|
||||
info: DispatchInfo,
|
||||
info: Self::DispatchInfo,
|
||||
len: usize,
|
||||
) -> Result<Self::Pre, crate::ApplyError> {
|
||||
Self::validate_unsigned(call, info, len)
|
||||
Self::validate_unsigned(call, info.clone(), len)
|
||||
.map(|_| Self::Pre::default())
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Do any post-flight stuff for a transaction.
|
||||
fn post_dispatch(_pre: Self::Pre, _info: DispatchInfo, _len: usize) { }
|
||||
fn post_dispatch(_pre: Self::Pre, _info: Self::DispatchInfo, _len: usize) { }
|
||||
}
|
||||
|
||||
/// An error that is returned by a dispatchable function of a module.
|
||||
@@ -806,10 +810,11 @@ pub trait ModuleDispatchError {
|
||||
}
|
||||
|
||||
#[impl_for_tuples(1, 12)]
|
||||
impl<AccountId, Call> SignedExtension for Tuple {
|
||||
for_tuples!( where #( Tuple: SignedExtension<AccountId=AccountId, Call=Call> )* );
|
||||
impl<AccountId, Call, Info: Clone> SignedExtension for Tuple {
|
||||
for_tuples!( where #( Tuple: SignedExtension<AccountId=AccountId, Call=Call, DispatchInfo=Info> )* );
|
||||
type AccountId = AccountId;
|
||||
type Call = Call;
|
||||
type DispatchInfo = Info;
|
||||
for_tuples!( type AdditionalSigned = ( #( Tuple::AdditionalSigned ),* ); );
|
||||
for_tuples!( type Pre = ( #( Tuple::Pre ),* ); );
|
||||
|
||||
@@ -821,44 +826,44 @@ impl<AccountId, Call> SignedExtension for Tuple {
|
||||
&self,
|
||||
who: &Self::AccountId,
|
||||
call: &Self::Call,
|
||||
info: DispatchInfo,
|
||||
info: Self::DispatchInfo,
|
||||
len: usize,
|
||||
) -> TransactionValidity {
|
||||
let valid = ValidTransaction::default();
|
||||
for_tuples!( #( let valid = valid.combine_with(Tuple.validate(who, call, info, len)?); )* );
|
||||
for_tuples!( #( let valid = valid.combine_with(Tuple.validate(who, call, info.clone(), len)?); )* );
|
||||
Ok(valid)
|
||||
}
|
||||
|
||||
fn pre_dispatch(self, who: &Self::AccountId, call: &Self::Call, info: DispatchInfo, len: usize)
|
||||
fn pre_dispatch(self, who: &Self::AccountId, call: &Self::Call, info: Self::DispatchInfo, len: usize)
|
||||
-> Result<Self::Pre, crate::ApplyError>
|
||||
{
|
||||
Ok(for_tuples!( ( #( Tuple.pre_dispatch(who, call, info, len)? ),* ) ))
|
||||
Ok(for_tuples!( ( #( Tuple.pre_dispatch(who, call, info.clone(), len)? ),* ) ))
|
||||
}
|
||||
|
||||
fn validate_unsigned(
|
||||
call: &Self::Call,
|
||||
info: DispatchInfo,
|
||||
info: Self::DispatchInfo,
|
||||
len: usize,
|
||||
) -> TransactionValidity {
|
||||
let valid = ValidTransaction::default();
|
||||
for_tuples!( #( let valid = valid.combine_with(Tuple::validate_unsigned(call, info, len)?); )* );
|
||||
for_tuples!( #( let valid = valid.combine_with(Tuple::validate_unsigned(call, info.clone(), len)?); )* );
|
||||
Ok(valid)
|
||||
}
|
||||
|
||||
fn pre_dispatch_unsigned(
|
||||
call: &Self::Call,
|
||||
info: DispatchInfo,
|
||||
info: Self::DispatchInfo,
|
||||
len: usize,
|
||||
) -> Result<Self::Pre, crate::ApplyError> {
|
||||
Ok(for_tuples!( ( #( Tuple::pre_dispatch_unsigned(call, info, len)? ),* ) ))
|
||||
Ok(for_tuples!( ( #( Tuple::pre_dispatch_unsigned(call, info.clone(), len)? ),* ) ))
|
||||
}
|
||||
|
||||
fn post_dispatch(
|
||||
pre: Self::Pre,
|
||||
info: DispatchInfo,
|
||||
info: Self::DispatchInfo,
|
||||
len: usize,
|
||||
) {
|
||||
for_tuples!( #( Tuple::post_dispatch(pre.Tuple, info, len); )* )
|
||||
for_tuples!( #( Tuple::post_dispatch(pre.Tuple, info.clone(), len); )* )
|
||||
}
|
||||
}
|
||||
|
||||
@@ -869,6 +874,7 @@ impl SignedExtension for () {
|
||||
type AdditionalSigned = ();
|
||||
type Call = ();
|
||||
type Pre = ();
|
||||
type DispatchInfo = ();
|
||||
fn additional_signed(&self) -> rstd::result::Result<(), TransactionValidityError> { Ok(()) }
|
||||
}
|
||||
|
||||
@@ -885,6 +891,9 @@ pub trait Applyable: Sized + Send + Sync {
|
||||
/// Type by which we can dispatch. Restricts the UnsignedValidator type.
|
||||
type Call;
|
||||
|
||||
/// An opaque set of information attached to the transaction.
|
||||
type DispatchInfo: Clone;
|
||||
|
||||
/// Returns a reference to the sender if any.
|
||||
fn sender(&self) -> Option<&Self::AccountId>;
|
||||
|
||||
@@ -892,7 +901,7 @@ pub trait Applyable: Sized + Send + Sync {
|
||||
#[allow(deprecated)] // Allow ValidateUnsigned
|
||||
fn validate<V: ValidateUnsigned<Call=Self::Call>>(
|
||||
&self,
|
||||
info: DispatchInfo,
|
||||
info: Self::DispatchInfo,
|
||||
len: usize,
|
||||
) -> TransactionValidity;
|
||||
|
||||
@@ -901,7 +910,7 @@ pub trait Applyable: Sized + Send + Sync {
|
||||
#[allow(deprecated)] // Allow ValidateUnsigned
|
||||
fn apply<V: ValidateUnsigned<Call=Self::Call>>(
|
||||
self,
|
||||
info: DispatchInfo,
|
||||
info: Self::DispatchInfo,
|
||||
len: usize,
|
||||
) -> crate::ApplyResult;
|
||||
}
|
||||
|
||||
@@ -1,221 +0,0 @@
|
||||
// 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.
|
||||
//!
|
||||
//! All dispatchable functions defined in `decl_module!` must provide two trait implementations:
|
||||
//! - [`WeightData`]: To determine the weight of the dispatch.
|
||||
//! - [`ClassifyDispatch`]: To determine the class of the dispatch. See the enum definition for
|
||||
//! more information on dispatch classes.
|
||||
//!
|
||||
//! Every dispatchable function is responsible for providing this data via an optional `#[weight =
|
||||
//! $x]` attribute. In this snipped, `$x` can be any user provided struct that implements the
|
||||
//! two aforementioned traits.
|
||||
//!
|
||||
//! Substrate then bundles then output information of the two traits into [`DispatchInfo`] struct
|
||||
//! and provides it by implementing the [`GetDispatchInfo`] for all `Call` variants, and opaque
|
||||
//! extrinsic types.
|
||||
//!
|
||||
//! If no `#[weight]` is defined, the macro automatically injects the `Default` implementation of
|
||||
//! the [`SimpleDispatchInfo`].
|
||||
//!
|
||||
//! 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.
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Serialize, Deserialize};
|
||||
use impl_trait_for_tuples::impl_for_tuples;
|
||||
use codec::{Encode, Decode};
|
||||
use arithmetic::traits::{Bounded, Zero};
|
||||
use crate::RuntimeDebug;
|
||||
|
||||
/// Re-export priority as type
|
||||
pub use crate::transaction_validity::TransactionPriority;
|
||||
|
||||
/// Numeric range of a transaction weight.
|
||||
pub type Weight = u32;
|
||||
|
||||
/// Means of weighing some particular kind of data (`T`).
|
||||
pub trait WeighData<T> {
|
||||
/// Weigh the data `T` given by `target`. When implementing this for a dispatchable, `T` will be
|
||||
/// a tuple of all arguments given to the function (except origin).
|
||||
fn weigh_data(&self, target: T) -> Weight;
|
||||
}
|
||||
|
||||
/// Means of classifying a dispatchable function.
|
||||
pub trait ClassifyDispatch<T> {
|
||||
/// Classify the dispatch function based on input data `target` of type `T`. When implementing
|
||||
/// this for a dispatchable, `T` will be a tuple of all arguments given to the function (except
|
||||
/// origin).
|
||||
fn classify_dispatch(&self, target: T) -> DispatchClass;
|
||||
}
|
||||
|
||||
/// Means of determining the weight of a block's lifecycle hooks: on_initialize, on_finalize and
|
||||
/// such.
|
||||
pub trait WeighBlock<BlockNumber> {
|
||||
/// Return the weight of the block's on_initialize hook.
|
||||
fn on_initialize(_: BlockNumber) -> Weight { Zero::zero() }
|
||||
/// Return the weight of the block's on_finalize hook.
|
||||
fn on_finalize(_: BlockNumber) -> Weight { Zero::zero() }
|
||||
}
|
||||
|
||||
/// Maybe I can do something to remove the duplicate code here.
|
||||
#[impl_for_tuples(30)]
|
||||
impl<BlockNumber: Copy> WeighBlock<BlockNumber> for SingleModule {
|
||||
fn on_initialize(n: BlockNumber) -> Weight {
|
||||
let mut accumulated_weight: Weight = Zero::zero();
|
||||
for_tuples!(
|
||||
#( accumulated_weight = accumulated_weight.saturating_add(SingleModule::on_initialize(n)); )*
|
||||
);
|
||||
accumulated_weight
|
||||
}
|
||||
|
||||
fn on_finalize(n: BlockNumber) -> Weight {
|
||||
let mut accumulated_weight: Weight = Zero::zero();
|
||||
for_tuples!(
|
||||
#( accumulated_weight = accumulated_weight.saturating_add(SingleModule::on_finalize(n)); )*
|
||||
);
|
||||
accumulated_weight
|
||||
}
|
||||
}
|
||||
|
||||
/// A generalized group of dispatch types. This is only distinguishing normal, user-triggered transactions
|
||||
/// (`Normal`) and anything beyond which serves a higher purpose to the system (`Operational`).
|
||||
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
|
||||
#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, RuntimeDebug)]
|
||||
pub enum DispatchClass {
|
||||
/// A normal dispatch.
|
||||
Normal,
|
||||
/// An operational dispatch.
|
||||
Operational,
|
||||
}
|
||||
|
||||
impl Default for DispatchClass {
|
||||
fn default() -> Self {
|
||||
DispatchClass::Normal
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SimpleDispatchInfo> for DispatchClass {
|
||||
fn from(tx: SimpleDispatchInfo) -> Self {
|
||||
match tx {
|
||||
SimpleDispatchInfo::FixedOperational(_) => DispatchClass::Operational,
|
||||
SimpleDispatchInfo::MaxOperational => DispatchClass::Operational,
|
||||
SimpleDispatchInfo::FreeOperational => DispatchClass::Operational,
|
||||
|
||||
SimpleDispatchInfo::FixedNormal(_) => DispatchClass::Normal,
|
||||
SimpleDispatchInfo::MaxNormal => DispatchClass::Normal,
|
||||
SimpleDispatchInfo::FreeNormal => DispatchClass::Normal,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A bundle of static information collected from the `#[weight = $x]` attributes.
|
||||
#[cfg_attr(feature = "std", derive(PartialEq, Eq))]
|
||||
#[derive(Clone, Copy, Default, RuntimeDebug)]
|
||||
pub struct DispatchInfo {
|
||||
/// Weight of this transaction.
|
||||
pub weight: Weight,
|
||||
/// Class of this transaction.
|
||||
pub class: DispatchClass,
|
||||
}
|
||||
|
||||
impl DispatchInfo {
|
||||
/// Determine if this dispatch should pay the base length-related fee or not.
|
||||
pub fn pay_length_fee(&self) -> bool {
|
||||
match self.class {
|
||||
DispatchClass::Normal => true,
|
||||
// For now we assume all operational transactions don't pay the length fee.
|
||||
DispatchClass::Operational => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A `Dispatchable` function (aka transaction) that can carry some static information along with
|
||||
/// it, using the `#[weight]` attribute.
|
||||
pub trait GetDispatchInfo {
|
||||
/// Return a `DispatchInfo`, containing relevant information of this dispatch.
|
||||
///
|
||||
/// This is done independently of its encoded size.
|
||||
fn get_dispatch_info(&self) -> DispatchInfo;
|
||||
}
|
||||
|
||||
/// Default type used with the `#[weight = x]` attribute in a substrate chain.
|
||||
///
|
||||
/// A user may pass in any other type that implements the correct traits. If not, the `Default`
|
||||
/// implementation of [`SimpleDispatchInfo`] is used.
|
||||
///
|
||||
/// For each generalized group (`Normal` and `Operation`):
|
||||
/// - A `Fixed` variant means weight fee is charged normally and the weight is the number
|
||||
/// specified in the inner value of the variant.
|
||||
/// - A `Free` variant is equal to `::Fixed(0)`. Note that this does not guarantee inclusion.
|
||||
/// - A `Max` variant is equal to `::Fixed(Weight::max_value())`.
|
||||
///
|
||||
/// As for the generalized groups themselves:
|
||||
/// - `Normal` variants will be assigned a priority proportional to their weight. They can only
|
||||
/// consume a portion (defined in the system module) of the maximum block resource limits.
|
||||
/// - `Operational` variants will be assigned the maximum priority. They can potentially consume
|
||||
/// the entire block resource limit.
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum SimpleDispatchInfo {
|
||||
/// A normal dispatch with fixed weight.
|
||||
FixedNormal(Weight),
|
||||
/// A normal dispatch with the maximum weight.
|
||||
MaxNormal,
|
||||
/// A normal dispatch with no weight.
|
||||
FreeNormal,
|
||||
/// An operational dispatch with fixed weight.
|
||||
FixedOperational(Weight),
|
||||
/// An operational dispatch with the maximum weight.
|
||||
MaxOperational,
|
||||
/// An operational dispatch with no weight.
|
||||
FreeOperational,
|
||||
}
|
||||
|
||||
impl<T> WeighData<T> for SimpleDispatchInfo {
|
||||
fn weigh_data(&self, _: T) -> Weight {
|
||||
match self {
|
||||
SimpleDispatchInfo::FixedNormal(w) => *w,
|
||||
SimpleDispatchInfo::MaxNormal => Bounded::max_value(),
|
||||
SimpleDispatchInfo::FreeNormal => Bounded::min_value(),
|
||||
|
||||
SimpleDispatchInfo::FixedOperational(w) => *w,
|
||||
SimpleDispatchInfo::MaxOperational => Bounded::max_value(),
|
||||
SimpleDispatchInfo::FreeOperational => Bounded::min_value(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ClassifyDispatch<T> for SimpleDispatchInfo {
|
||||
fn classify_dispatch(&self, _: T) -> DispatchClass {
|
||||
DispatchClass::from(*self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SimpleDispatchInfo {
|
||||
fn default() -> Self {
|
||||
// Default weight of all transactions.
|
||||
SimpleDispatchInfo::FixedNormal(10_000)
|
||||
}
|
||||
}
|
||||
|
||||
impl SimpleDispatchInfo {
|
||||
/// An _additive zero_ variant of SimpleDispatchInfo.
|
||||
pub fn zero() -> Self {
|
||||
Self::FixedNormal(0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user