3

I need to use HashMap in my struct that I store in StorageMap. As I searched online and other Substrate pallets, I didn't see that being used.

I came across use scale_info::prelude::collections::HashMap; but I don't think it's SCALE encodable.

Is there a way to use a HashMap in my pallet? What are the best practices or alternatives that I can use?

3 Answers 3

3

You should use BTreeMap, HashMap isn't available for the runtime storage.

The underlying reason being that HashMaps have undeterministic behavior, therefore could likely break your consensus. Otherwise you are free to use it from core::collections.

3

You should use BTreeMap, HashMap isn't available for the runtime storage.

Or use the StorageDoubleMap storage type.

3

A StorageMap is already a key -> value mapping. If you need more keys, you can also use a NMap which can have multiple keys.

HashMap doesn't work in no_std as it requires randomness which isn't available.

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.