1

This is probably a newbie question Is it correct to execute a for loop which has the potential to loop over and process possibly 5000 or more records in on_initialize hook? could you provide me a detailed answer as to why not? I guess this is related to block size and could slow down the chain if I am correct.

I do have a list of tasks that have to end at a certain block, I am guessing if this is not the right approach I will have to allow the owner of each task to initiate the end task

2 Answers 2

1

I guess this is related to block size and could slow down the chain if I am correct.

Yes this is correct. Storage iterations can be really expensive. If this is for a Parachain, each read will also increase the proof size. If you don't ensure that all of this stays within reasonable limits, you could easily brick your chain.

I do have a list of tasks that have to end at a certain block, I am guessing if this is not the right approach I will have to allow the owner of each task to initiate the end task

Lazy operations should always be favored. If you have the possibility to move this off to the user it would be much better than iterating in on_initialize. You could also move the operation into on_idle and only process a subset of these tasks per block.

1

Basti's answer is the best but for whatever reason if you wanted to do this in on_initialize then another way is to break it up into "chunks" to process by setting a weight limit per block e.g. 5% of block weight will be allocated to this and store "checkpoints" in storage. Similar to how we do multi-block migrations (example here).

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.