Use Custom Weights
Last updated
Last updated
Here, you'll learn how to use the weights in your pallet from the output of benchmarking your pallet's extrinsics. It assumes that you have a file in your pallet's directory called weights.rs
, containing the auto generated weights from running FRAME's benchmarking tool.
Use weights generated by in the extrinsics of your pallet.
Using accurate weights generated from benchmarking for a pallet's extrinsics.
WeightInfo
trait in your pallet's weight.rs
fileIn order to use the weights.rs
file generated by the benchmarking tool in your pallet (an example of Substrate ), define a trait that will make it easy to access its functions. For example:
pallets/example/src/weights.rs
WeightInfo
in your palletIn your pallet's lib.rs
, declare pub mod weights;
at the top and introduce pub use crate::weights::WeightInfo;
to expose the WeightInfo
trait to your pallet. Then, introduce a WeightInfo
type into the Config
trait of your pallet:
pallets/example/src/lib.rs
For each of your dispatchables, introduce an appropriate weights line to determine the weight using the configured WeightInfo
type. For example, T::WeightInfo::example
would be the weight function returned from weights.rs
for the example
extrinsic:
pallets/example/src/lib.rs
WeightInfo
type in Config
trait implsYou'll need to define the WeightInfo
trait in both your pallet's tests mockup mock.rs
and your Substrate node
's runtime.
In your pallet's Config
trait impl in mock.rs
, introduce the following line to exclude weights in our testing.
In pallets/example/src/mock.rs
or pallets/example/src/test.rs
:
In your pallet's Config
trait impl in runtime/src/lib.rs
, introduce the following line to include weights in our runtime.
Once you recompile your node, your extrinsic will now be using the custom weights defined in pallets/example/src/weights.rs
.