-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Conversation
Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com> Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>
} | ||
|
||
/// Remove all value of the storage. | ||
pub fn remove_all() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we want to support limit, then we need sp-io to return the number of deleted key in the overlay, once merged I will open an issue, and we can work on it when we need it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is limit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for other storage the remove_all method allow to remove only up to a certain number of key in the backend: https://substrate.dev/rustdocs/latest/frame_support/storage/types/struct.StorageMap.html#method.remove_all
Here it is not possible without changing sp-io. Because we need to count the value removed in the overlay. And https://substrate.dev/rustdocs/latest/sp_io/storage/fn.clear_prefix.html doesn't give us this information
would StorageCountedMap be a better name than CountedStorageMap, this makes all storages type start with |
I think |
@jacogr i think this is a change to the metadata |
AFAICT this looks good to me. However, I wonder how scalable this overall approach is. I can imagine we will continue to want to add new and different kinds of storage types to make runtime development easier and more ergonomic. Do you see that writing all this code every time is sustainable? |
Note also that we will already need to extend this API for all of our needs. See here in Assets we have a StorageNMap: https://github.com/paritytech/substrate/blob/master/frame/assets/src/lib.rs#L245
We are not only interested in the total number of values for this whole map, but also just the number of values for the map under a specific user. So something like |
I honestly think #8605 is a better approach here, but it looks like there is already a pressing need for counters? If so, the question then becomes maintaining a sensible API that's compatible with the storage mutation hooks. I agree that adding additional storage item types don't seem to be a scalable solution, and we may end up with a system that is overly complex. I think all storage map types should simply support the basic CRUD operations, and if there is a need to add additional functionality to them, creating extension traits or functions sounds like the best way forward to me. |
You will need to bump the metadata version - this is smack-bang in the middle of the enum which means V13 parsers cannot parse this at all. |
This PR doesn't change the metadata... The countedstoragemap declare a
storage map entry and a storage value entry in the metadata.
Maybe we can enhance the metadata later in order to be able to declare the
relationship between those 2 storages usage.
About the hook on storage map i agree it would be better, it might be a bit
more difficult to optimize. E.g. we don't want to read if a previous key
exists before each insert when we know a previous exist or not like in a
mutate operation.
But it should be doable.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM except a few suggestions, there should be a follow up that migrates the staking counters to this PR.
Hey, is anyone still working on this? Due to the inactivity this issue has been automatically marked as stale. It will be closed if no further activity occurs. Thank you for your contributions. |
I slightly refactored the metadata for storages, instead of having the trait StorageEntryMetadata which gives the name, the type, etc.. I did StorageEntryMetadataBuilder which directly build the metadata of the storage. This is good because in case of counted storage map it allows to generate both inner storage metadata (the map and the value holding the counter), without any additional code from the macro. EDIT: if people want they can do a final review, but I think it is good to merge, and as behavior hasn't change, the current review are valid. |
bot merge |
Trying merge. |
This PR introduces a new Storage abstraction:
CountedStorageMap
The
CountedStorageMap
is a new type with a similar API asStorageMap
which internally use aStorageMap
andStorageValue<u32>
and keeps count of the number of items in the map by using the value as a counter.