-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Used CountedStorageMap in pallet-staking #10233
Used CountedStorageMap in pallet-staking #10233
Conversation
… ayevbeosa-employ-counted-map
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Looking good otherwise, thank you. Check the CI for some compiler issues and things you still need to fix. |
/tip medium |
Please fix the following problems before calling the tip bot again:
|
Thanks for the tip 😀 |
/tip medium |
A medium tip was successfully submitted for ayevbeosa (12dVgRw6ntsMQiETAoCBzu6MA7LPWagvU4Y1rH9jGPDHQvPX on polkadot). |
frame/staking/src/pallet/impls.rs
Outdated
@@ -865,10 +855,10 @@ impl<T: Config> ElectionDataProvider<T::AccountId, BlockNumberFor<T>> for Pallet | |||
fn voters( | |||
maybe_max_len: Option<usize>, | |||
) -> data_provider::Result<Vec<(T::AccountId, VoteWeight, Vec<T::AccountId>)>> { | |||
debug_assert!(<Nominators<T>>::iter().count() as u32 == CounterForNominators::<T>::get()); | |||
debug_assert!(<Validators<T>>::iter().count() as u32 == CounterForValidators::<T>::get()); | |||
debug_assert!(<Nominators<T>>::iter().count() as u32 == Nominators::<T>::count()); |
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.
This is kinda pointless now to have. We already check these in fn check_count
as well, which is called after every test.
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.
Okay that makes sense, I will have them removed.
@@ -83,9 +83,6 @@ pub mod v7 { | |||
let validator_count = Validators::<T>::iter().count() as u32; | |||
let nominator_count = Nominators::<T>::iter().count() as u32; | |||
|
|||
CounterForValidators::<T>::put(validator_count); |
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.
This is technically making this migration code WRONG. Either remove it completely, or make sure it is correct.
we don't enforce this now, but a correct way would be to make sure this migration code stays correct over time. That is, it should have its own storage items and not depend on the pallet types. In this case, you need to create (mock) storage types via generate_storage_alias
and use them to set a correct value for the counters.
Or, we need some special set_counter
to set the counters upon migration, but I don't think we have this for now.
If all of this is too much, with despair and sadness, I am also fine with just removing the migration code altogether. But the long term correct thing to do is as I said above.
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.
Could you help with the correct implementation details on how to write the migration code, I am still new to the Substrate repo.
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.
you can look into other use cases of generate_storage_alias
and its documentation. This allows you to generate a storage value type that mimics the one that is being removed. In this case, you can create a
generate_storage_value!(CounterForValidators => Value<u32>);
and now you have CounterForValidators
back in scope, and you can use 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.
is staking pallet using the pallet macro in v7 ?
If so then the generate_storage_value can use a wrong pallet prefix.
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.
Is this question for me or @kianenigma
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.
yeah it was already migrated. Then the very pedantic course of action would be to accept the prefix as the argument of the migration function, but I will be fine with skipping it since the migration code is already at risk to becoming outdated (since it depends on T: Config
). So the assumption is that whoever uses this migration code should test it, and we're good.
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.
Thanks! have some minor complains.
… ayevbeosa-employ-counted-map
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.
this is what I have in mind WDYT ?
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.
maybe we can have a test that the counter is correctly stored at twox128(pallet prefix)++twox128("CounterForNominators")
.
We can just do some assertion to asset that Nominators::counter_storage_final_key
is equal to twox128(b"Staking") ++ twox128(b"CounterForNominators")
.
But otherwise looks good to me
|
@@ -99,6 +99,17 @@ where | |||
OnEmpty: Get<QueryKind::Query> + 'static, | |||
MaxValues: Get<Option<u32>>, | |||
{ | |||
/// The key used to store the counter of the map. |
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.
very good 👍
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
bot merge |
Waiting for commit status. |
Merge cancelled due to error. Error: Checks failed for da0f59f |
bot merge |
Waiting for commit status. |
* Removed counters and used CountedStorageMap instead. * Little refactoring * Update frame/staking/src/migrations.rs Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> * Removed redundant code to update counter for validator & nominator. * Removed redundant code to update counter for validator & nominator. * Removed unreachable code to inject the hashed prefix for nominator & validator. * Removed redundant check for nominator & validator count. * Generated `fn prefix_hash` for `CountedStorageMap`. * Applied changes from `cargo fmt` * Possible correct implementation of migration code * Implemented fn module_prefix, storage_prefix and prefix_hash. * Removed counted_map.rs * Renamed `fn storage_prefix` to `storage_counter_prefix`. * Update frame/support/src/storage/types/counted_map.rs * Update frame/bags-list/remote-tests/src/snapshot.rs * Update frame/support/src/storage/types/counted_map.rs * Fixed errors. Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
* Removed counters and used CountedStorageMap instead. * Little refactoring * Update frame/staking/src/migrations.rs Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> * Removed redundant code to update counter for validator & nominator. * Removed redundant code to update counter for validator & nominator. * Removed unreachable code to inject the hashed prefix for nominator & validator. * Removed redundant check for nominator & validator count. * Generated `fn prefix_hash` for `CountedStorageMap`. * Applied changes from `cargo fmt` * Possible correct implementation of migration code * Implemented fn module_prefix, storage_prefix and prefix_hash. * Removed counted_map.rs * Renamed `fn storage_prefix` to `storage_counter_prefix`. * Update frame/support/src/storage/types/counted_map.rs * Update frame/bags-list/remote-tests/src/snapshot.rs * Update frame/support/src/storage/types/counted_map.rs * Fixed errors. Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
As raised in issue #10180,
CountedStorageMap
has been used for theValidators
&Nominators
type and its corresponding counters has also been removed.Polkadot address: 12dVgRw6ntsMQiETAoCBzu6MA7LPWagvU4Y1rH9jGPDHQvPX