I defined a storage item as StorageDoubleMap, and I need to provide a method that returns the list of elements with the first key (k1). This getter method has to be provided by the pallet. I found a method in frame that provides what I need.

However, I'm aware that iterating over an unbounded storage item might lead to undesired behavior. Is there any recommendation or best practice to accomplish this?

EDIT: The use case is to provide a way to iterate over the collection in a safe manner. Let's imagine this collection of items is consulted by another pallet.

  • You'd better improving your service structure. Iterating over a map haphazardly is unwise IMO.
    – AurevoirXavier
    Dec 19, 2023 at 14:31
  • @AurevoirXavier I see, do you know if there is any common pattern? It seems to be an issue that people commonly face
    – magecnion
    Dec 19, 2023 at 14:46
  • What's the use case? maybe with the idea of what you need we can see a better solution than iterate the StorageDoubleMap inside the runtime
    – Alex Bean
    Dec 22, 2023 at 9:08
  • @AlexBean use case added in the edit section
    – magecnion
    Dec 22, 2023 at 21:40

1 Answer 1


There are two functions called iter_prefix and iter_prefix_from in the StorageDoubleMap that can be used for this.
The iter_prefix_from takes a previous key and resume iteration at that spot. So each time after iterating x keys, the last key is stored and in the next block the iteration can be resumed.

The tricky part it that iterating through a map should only be done while the map is not being modified. So possibly you need to add some locking mechanism or somehow differently ensure that the map will not have insert/deletes while it is being iterated.
AFAIK it still "works", but i think its not tested for and generally bad practice.

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.