Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Used CountedStorageMap in pallet-staking #10233

Merged

Conversation

ayevbeosa
Copy link
Contributor

@ayevbeosa ayevbeosa commented Nov 11, 2021

As raised in issue #10180, CountedStorageMap has been used for the Validators & Nominators type and its corresponding counters has also been removed.

Polkadot address: 12dVgRw6ntsMQiETAoCBzu6MA7LPWagvU4Y1rH9jGPDHQvPX

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
@shawntabrizi
Copy link
Member

shawntabrizi commented Nov 11, 2021

Looking good otherwise, thank you.

Check the CI for some compiler issues and things you still need to fix.

@shawntabrizi
Copy link
Member

/tip medium

@substrate-tip-bot
Copy link

Please fix the following problems before calling the tip bot again:

  • Contributor did not properly post their Polkadot or Kusama address. Make sure the pull request has: "{network} address: {address}".

@shawntabrizi shawntabrizi added B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit labels Nov 11, 2021
@shawntabrizi shawntabrizi added D5-nicetohaveaudit ⚠️ PR contains trivial changes to logic that should be properly reviewed. and removed D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit labels Nov 11, 2021
@ayevbeosa
Copy link
Contributor Author

ayevbeosa commented Nov 11, 2021

/tip medium

Thanks for the tip 😀
I have added my wallet address now

@shawntabrizi
Copy link
Member

/tip medium

@substrate-tip-bot
Copy link

A medium tip was successfully submitted for ayevbeosa (12dVgRw6ntsMQiETAoCBzu6MA7LPWagvU4Y1rH9jGPDHQvPX on polkadot).

https://polkadot.js.org/apps/#/treasury/tips

@@ -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());
Copy link
Contributor

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.

Copy link
Contributor Author

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);
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor

@kianenigma kianenigma left a 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.

Copy link
Contributor

@gui1117 gui1117 left a 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 ?

frame/support/src/storage/types/counted_map.rs Outdated Show resolved Hide resolved
frame/bags-list/remote-tests/src/snapshot.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@gui1117 gui1117 left a 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

@gui1117
Copy link
Contributor

gui1117 commented Dec 2, 2021

@@ -99,6 +99,17 @@ where
OnEmpty: Get<QueryKind::Query> + 'static,
MaxValues: Get<Option<u32>>,
{
/// The key used to store the counter of the map.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very good 👍

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gui1117
Copy link
Contributor

gui1117 commented Dec 7, 2021

bot merge

@paritytech-processbot
Copy link

Waiting for commit status.

@paritytech-processbot
Copy link

Merge cancelled due to error. Error: Checks failed for da0f59f

@gui1117
Copy link
Contributor

gui1117 commented Dec 7, 2021

bot merge

@paritytech-processbot
Copy link

Waiting for commit status.

@paritytech-processbot paritytech-processbot bot merged commit 6079539 into paritytech:master Dec 7, 2021
grishasobol pushed a commit to gear-tech/substrate that referenced this pull request Mar 28, 2022
* 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>
AurevoirXavier added a commit to darwinia-network/darwinia-common that referenced this pull request Sep 8, 2022
ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
* 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>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D5-nicetohaveaudit ⚠️ PR contains trivial changes to logic that should be properly reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants