Hooks can do computation and read/write storage. Operations that usually comes at price.
Those hooks return a -> Weight
value, but from my understanding nobody is paying for this. It's just used to increment BlockWeights
a counter that must not go above base_block
.
This given while reading FRAME
pallets it's unclear for me what part of the hook computation should be taken into account into this returned Weight
.
I more or less found two cases:
- small hooks with few computation (eg: https://paritytech.github.io/substrate/latest/src/pallet_aura/lib.rs.html#88-115), where only cost of the read and write operations are taken into account
- more complex ones (eg: https://paritytech.github.io/substrate/monthly-2021-09+1/src/pallet_elections_phragmen/lib.rs.html#253), where a benchmark have been trained and it's output is returned. Seems to take both computation and storage operation into account.
But in some pallets, part of the operation seems to be not taken into account: https://paritytech.github.io/substrate/latest/src/pallet_election_provider_multi_phase/lib.rs.html#700. The let current_phase = Self::current_phase();
is a read that I don't see being added to the weight.
And the last category is hooks that return 0, such as autorship
, babe
.
I would greatly appreciate to have at least a rule of thumb on what I am suppose to do with my on_initialize
hooks, and if that exist a clear guide on what to do and list of the different cases I can face.