on_initialize
returns a weight, but what is the effect of this weight exceeding block weight limit? Is the block valid? can extrinsic still be included?on_finalized
does not return a weight, does that mean it always runs regardless of what remains of the block weight budget as a result of extrinsics+on_initialize
1 Answer
Here is the schematical representation of the block weight:
┌────────────────────────────────────────────────────────────────────────────┐
│┌──────────────────┐┌───────────────────────────────────┐┌─────────────────┐│
││ ││ ││ ││
││ on_initialize ││ extrinsics ││ on_finalize ││
││ ││ ││ ││
│└──────────────────┘└───────────────────────────────────┘└─────────────────┘│
└────────────────────────────────────────────────────────────────────────────┘
◀─────────────────────────────────Block Weight─────────────────────────────▶
The on_initialize
is privileged in a way that it can take how much weight it wants. If necessary, the logic there can take almost the whole budget without leaving anything for the extrinsics processing.
on_finalize
is always executed and there is no way to skip that. on_finalize
takes control after all extrinsics are executed. The problem is that whoever packs the block can fill up the extrinsics so that there is no space left for the on_finalize
logic. That's why the convention is on_initialize
returns the weight for itself and for on_finalize
.
Substrate does not impose any hard limits on weight. However, Polkadot does. If a parachain validation function took more weight than alloted, such a block will be discarded.
-
Thank you. Are you saying that in Substrate,
on_finalize
can even exceed the block weight limit? Commented Aug 15, 2022 at 8:29 -
@BedehoMender yes
on_initialize
andon_finalize
can exceed the block weight and the block will still be valid.– pepyakinCommented Aug 15, 2022 at 10:06