3

I'm wondering about weights. The docs are quite outdated here and I do not understand what is going on. I've managed to create benchmarks and generate a weight.rs.

https://github.com/paritytech/substrate/blob/master/frame/examples/basic/src/weights.rs has SubstrateWeight and WeightInfo whereas it seems that the tutorial suggest to write WeightInfo somewhat as a copy & paste from SubstrateWeight in what looks to me like a lot of redundant code, because they only differ in the database weights they assume. There is no explanation whatsoever.

The weight.rs is generated as WeightInfo which seems to be required to be renamed to SubstrateWeight in order for things to work.

This feels cumbersome. Can someone share some light on how this is all meant

1
  • 1
    Could you please link the tutorial? It is probably out-dated. Jan 10, 2023 at 20:42

2 Answers 2

0

There is no manual editing necessary, also no Copy & Paste. Generating weights for a pallet works with the benchmark pallet command.

Depending on where you want to use your Pallet weights, you may want to use different templates.
If you are operating in the Substrate repo; You can look at other pallet weight.rs files to see examples, eg the Example that you linked.
As you notice: A special --template is used. This is the FRAME template and only really makes sense for Substrate itself. It produces a config dependent SubstrateWeight<T>, which third party down-stream projects like Polkadot can use. Instead of doing that they should rather benchmark themselves.
Using these default weights looks like this:

type SystemWeightInfo = frame_system::weights::SubstrateWeight<Runtime>;

Or when you are doing testing and just need some weight:

type WeightInfo = ();

Now if you are not in the Substrate repo but say in Polkadot: Looking at any of the runtime files you can see that it is not using any of the SubstrateWeight or () implementations. Rather it has its own weight definitions in the weights/ folder.
The Polkadot runtime then uses these weights here like this:

type WeightInfo = weights::pallet_balances::WeightInfo<Runtime>;

This is important, since weights are runtime-dependent. The WeightInfo trait is always the same though.
You can go even further and implement mocked weights.

4
  • Can you elaborate what you mean by "makes sense in substrate" vs "makes sense in polkadot"? Does this mean standalone vs parachain? I'm talking about docs.substrate.io/reference/how-to-guides/weights/…. To be honest, the entire documentation is an insane mashup of various versions with breaking changes, I think it would already help to delete the entire docs and just tell people to look at source. I myself cannot even create a WeightInfo trait because the benchmark framework generated by exact that tutorial autocreates a struct called WeightInfo that clashes.
    – shredding
    Jan 10, 2023 at 20:59
  • Given your example, why is then this doing this weird duplication between SubstrateWeightInfo and WeightInfo here: github.com/paritytech/substrate/blob/master/frame/balances/src/…? I mean this is the pallet that I download if I add it to my parachain. Why is the generated weights.rs different? I can adapt that like this, but I fundamentally don't understand why both of those exists. I know have generated my weights.rs and I need to change it to make it work and I don't understand those changes. I see they use a different template, but why is everyone using that.
    – shredding
    Jan 10, 2023 at 21:07
  • I think my problem boils down to why is WeightInfo a struct in weights/. I need to add a trait WaightInfo to even add it to my config and for that I already need to delete the struct created.
    – shredding
    Jan 10, 2023 at 21:20
  • Ok, so I gave it another shot today. I'm coming from here docs.substrate.io/reference/how-to-guides/weights/… and it send me to docs.substrate.io/reference/how-to-guides/weights/… This is totally incompatible because the latter is referencing a FRAME based pallet with that template. Is there any example of an existing project that using it outside and without that template?
    – shredding
    Jan 11, 2023 at 10:10
0

I ended up using https://github.com/PureStake/moonbeam/blob/master/benchmarking/frame-weight-template.hbs as a template and followed the source, completly ignoring the official docs that seem to be a mashup of various conflicting versions.

Put it in /benchmarking and run this:

./target/release/your-node benchmark pallet --chain dev --pallet pallet_dao_core --extrinsic '*' --steps 20 --repeat 10 --output pallets/dao-core/src/weights.rs --template ./benchmarking/frame-weight-template.hbs

Then you can import the weights using

pub mod weights;
use weights::WeightInfo;

The upper template creates the correct Weights for testing:

/// For mocks rs
type WeightInfo = ();
/// For your runtime
type WeightInfo = pallet_your_pallet::weights::SubstrateWeight<Runtime>;

I stand being corrected, but the default used without a template is not usable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.