paritytech / substrate

Substrate: The platform for blockchain innovators
Apache License 2.0
8.38k stars 2.65k forks source link

StoragePrefixedMap iterate with keys #4959

Closed xlc closed 4 years ago

xlc commented 4 years ago

Currently StoragePrefixedMap::iter can be used to iterate values, but I would like to iterate with keys as well.

We would probably want a version iterate with raw keys (for blake2_256 hasher) and a version iterate with decoded value (for twox_64_concat / blake2_128_concat).

Not sure what is the best API, but a raw key version can be a good starter.

I can do this but unsure what to call the new method.

Should I make iter always returns raw key and values? (breaking change) Or add a new iter_with_raw_key? Please suggest a better name Or iter(key_decoder: KeyDecoder) ?

enum KeyDecoder { 
  Skip, // no key
  Identity, // raw key
  Twox64Concat, // decode assuming 64bit prefix is hash
  Blake2_128Concat // decode assuming 128bit prefix is hash
}
bkchr commented 4 years ago

Duplicate of https://github.com/paritytech/substrate/issues/4610

xlc commented 4 years ago

It is more of follow up of #4610 that can happen independently.

I don't see where does #4610 specify how the iterating API going to look like. This is a blocking issue for us and I would like to fix it myself with help from your guys, but I don't want to do #4610 because it is a lot of work to migrate all storages.

bkchr commented 4 years ago

It is not written there explicitly, you are right. However the goal of the issue to also have some api to do the iteration easily.

If you want to do that now, you can just use sp_io::storage::next_key to do the iteration manually. That should resolve your blocker for now.