Module frame_support::weights
source · Expand description
Primitives for transaction weighting.
Every dispatchable function is responsible for providing #[weight = $x]
attribute. In this
snipped, $x
can be any user provided struct that implements the following traits:
WeighData
: the weight amount.ClassifyDispatch
: class of the dispatch.PaysFee
: whether this weight should be translated to fee and deducted upon dispatch.
Substrate then bundles the output information of the three traits into DispatchInfo
struct
and provides it by implementing the GetDispatchInfo
for all Call
both inner and outer call
types.
Substrate provides two pre-defined ways to annotate weight:
1. Fixed values
This can only be used when all 3 traits can be resolved statically. You have 3 degrees of configuration:
- Define only weight, in which case
ClassifyDispatch
will beNormal
andPaysFee
will beYes
.
frame_support::decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
#[weight = 1000]
fn dispatching(origin) { unimplemented!() }
}
}
2.1 Define weight and class, in which case PaysFee
would be Yes
.
frame_support::decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
#[weight = (1000, DispatchClass::Operational)]
fn dispatching(origin) { unimplemented!() }
}
}
2.2 Define weight and PaysFee
, in which case ClassifyDispatch
would be Normal
.
frame_support::decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
#[weight = (1000, Pays::No)]
fn dispatching(origin) { unimplemented!() }
}
}
- Define all 3 parameters.
frame_support::decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
#[weight = (1000, DispatchClass::Operational, Pays::No)]
fn dispatching(origin) { unimplemented!() }
}
}
2. Define weights as a function of input arguments.
The arguments of the dispatch are available in the weight expressions as a borrowed value.
frame_support::decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
#[weight = (
*a as u64 + *b,
DispatchClass::Operational,
if *a > 1000 { Pays::Yes } else { Pays::No }
)]
fn dispatching(origin, a: u32, b: u64) { unimplemented!() }
}
}
FRAME assumes a weight of 1_000_000_000_000
equals 1 second of compute on a standard machine.
Latest machine specification used to benchmark are:
- Digital Ocean: ubuntu-s-2vcpu-4gb-ams3-01
- 2x Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.20GHz
- 4GB RAM
- Ubuntu 19.10 (GNU/Linux 5.3.0-18-generic x86_64)
- rustc 1.42.0 (b8cedc004 2020-03-09)
Modules
Structs
WeightToFee
that uses a constant multiplier.#[weight = $x]
attributes.WeightToFee
that maps one unit of weight to one unit of fee.DispatchClass
.WeightToFee
.Enums
Traits
Dispatchable
function (aka transaction) that can carry some static information along with
it, using the #[weight]
attribute.Pays::No
, the block resource limits are applied, yet no fee is deducted.T
).DispatchError
to DispatchErrorWithPostInfo
for dispatchables
that want to return a custom a posterior weight on error.