3

I am currently trying to iterate over a storage map over multiple blocks. For this I need to ensure that the StorageMap was not modified.

My plan is to derive a hash from the current StorageMap and save that hash in another storage item in order to check if the new call in the next block has the same StorageMap state as the previous call. So, I derive the hash of the StorageMap again and check for equality.

If the hashes are equal, I can use StorageMap::iter_from(raw_key), otherwise I return an error and request the user to call the multi-block execution again.

So: How can I derive a hash for the current StorageMap state?

1
  • I am thinking about a child trie here. Or is there another way of creating a state hash out of a storage map?
    – Chralt
    Mar 20, 2023 at 7:39

2 Answers 2

3

Storage Map does not have a fingerprint of the data inside it, and it would be very expensive to create this.

It seems you may want to use a child-trie, which has a very similar API to a Storage Map, but also has a storage root, which can be used for the kind of check you want.

You simply store the storage root, and then later can check if it has changed.

Beyond that, remember as a runtime engineer, you control all the logic on your chain. It should not really be a mystery if some storage map has changed or not. You can create any kind of logic you want to track when a user changes a map, or even prevent a user from doing so within your pallet.

I would definitely not do any kind of storage iteration or brute force equality check for this kind of thing. Those operations are very expensive for the runtime.

0

You can use the method .iter() of StorageMap combined with the itertools method .collect_vec() see this response here:

use itertools::Itertools;
let collection = <YourStorage<T>>::iter().collect_vec();

Then you can generate the hash of the collection variable.

1
  • 1
    This is not something I recommend as a part of the runtime logic. It is very expensive and will certainly brick your chain in most cases.
    – Shawn Tabrizi
    Mar 27, 2023 at 17:00

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.