4

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:

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.

2 Answers 2

4

I think what is important to understand as well is that weight and transaction fees are two different things. The transaction fee is defined based on the weight, but this doesn't mean that computing always has to be paid by someone. The concept of weight is nothing more than the measurement of computational effort and the reason why we annotate extrinsic with it is to make sure that we are safe against DDOS attacks. Long story short: No one directly pays for the computation in a hook, but the weight used is still relevant for the entire system (for e.x: avoiding long block times). Coming to your question about what to do with the on_initialize hook. I think the answer is you have to write a benchmark for it that is as accurate as possible as you would do it for an extrinsic. The reason is simply that the weight returned by this hook is taken into account into the total weight you can use in a block and if this value is wrong you will end up either wasting the computational power of your blockchain or block time will be longer than 6 seconds (or whatever your block time is).

0

This is a very interesting question.

My two cents here, it is true that hooks can do some computation, more or less complex, and so it will take some of the weight available. But this is some logic that the system is executing under certain conditions, this is not someone (an origin, external to the system) requesting for some storage modification or some execution. So, the weight used related to these hooks is something native to the design of the system itself, and not something you can make any other entity accountable for.

1

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.