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

migrate pallet-elections-phragmen to attribute macros #8044

Merged
26 commits merged into from
Apr 23, 2021

Conversation

kianenigma
Copy link
Contributor

@kianenigma kianenigma commented Feb 4, 2021

part of #7882.

Previous usage of decl_storage set the pallet storage prefix to PhragmenElection, while in both Polkadot and kusama it is set to ElectionsPhragmen

https://github.com/paritytech/polkadot/blob/4866f8e72029e719d7ce74e341bb1e94ebd724f7/runtime/polkadot/src/lib.rs#L1002

https://github.com/paritytech/polkadot/blob/4866f8e72029e719d7ce74e341bb1e94ebd724f7/runtime/kusama/src/lib.rs#L997

Therefore, this will need

  1. Either a migration
  2. Renaming of the pallet in polkadot repo

⚠️ Breaking Change ⚠️

From https://crates.parity.io/frame_support/attr.pallet.html#checking-upgrade-guidelines

storages now use PalletInfo for module_prefix instead of the one given to decl_storage: Thus any use of this pallet in construct_runtime! should be careful to update name in order not to break storage or to upgrade storage (moreover for instantiable pallet). If pallet is published, make sure to warn about this breaking change.

So users of the ElectionsPhragmen pallet must be careful about the name they used in construct_runtime!. Hence the runtime-migration label, which might not be needed depending on the configuration of the ElectionsPhragmen pallet.


polkadot companion: paritytech/polkadot#2765

@kianenigma kianenigma added A0-please_review Pull request needs code review. 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. labels Feb 4, 2021
@gui1117 gui1117 added B3-apinoteworthy and removed B0-silent Changes should not be mentioned in any release notes labels Feb 12, 2021
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.

Needs a companion and some nitpicks, otherwise looks good.

frame/elections-phragmen/src/lib.rs Show resolved Hide resolved
frame/elections-phragmen/src/lib.rs Outdated Show resolved Hide resolved
frame/elections-phragmen/src/lib.rs Show resolved Hide resolved
frame/elections-phragmen/src/lib.rs Outdated Show resolved Hide resolved
frame/elections-phragmen/src/lib.rs Outdated Show resolved Hide resolved
@kianenigma
Copy link
Contributor Author

kianenigma commented Feb 13, 2021

@thiolliere did you see my note about breaking change though? this will sadly need a migration, or we must rename the runtimes in polkadot/kusama.

Honestly, I wish we could migrate the prefix to ElectionsPhragmen, because the pallet is called elections-phragmen, and the current prefix is just confusing.

@gui1117
Copy link
Contributor

gui1117 commented Feb 13, 2021

Yes that's what I meant by need a companion, but indeed we can also write the migration code in this pallet. or we decide to rename the pallet instead

@kianenigma
Copy link
Contributor Author

I thought about this a bit further and:

  1. My personal preference is to write a migration that moves everything from PhragmenElection to ElectionsPhragmen prefix.
  2. But that will break polkadot js and potentially other tools.
  3. So I can add a companion that will rename the modules in construct_runtime! in our chains, and we should put this in the release notes as well so that other chains do the same.
  4. Or I wait for the hack that @thiolliere said he might come up with using which a pallet can override where in storage it will be kept. (We'll need it for Refactor Account Storage into Accounts Pallet #8254)

@gui1117
Copy link
Contributor

gui1117 commented Mar 18, 2021

I would prefer third party tools to fetch the storage from the metadata.
I expected polkadotjs to do that already, did they hardcoded the location of the storage for pallet-elections-phragmen ?

If third party tools fetch the storage using the metadata. then they shouldn't break.

@kianenigma
Copy link
Contributor Author

I recall that one of the reasons for preferring to keep the storage of #8254 under system was to remain backward compatible. I am using the same argument here, but honestly I am not sure how much breakage would happen.

@bkchr
Copy link
Member

bkchr commented Mar 18, 2021

AFAIK polkadot js already uses the prefix from the metadata and I think we should assume this model for the rest. Overriding the storage prefix is somewhat weird. This is again just a Polkadot specific feature, while we are here in Substrate ;)

@gui1117
Copy link
Contributor

gui1117 commented Mar 18, 2021

I recall that one of the reasons for preferring to keep the storage of #8254 under system was to remain backward compatible. I am using the same argument here, but honestly I am not sure how much breakage would happen.

at some point, if third party tools are using metadata to fetch system accounts, then I'm not sure keeping it at the same location will help them. Because the Account storage will no longer be in the metadata in system pallet.

@kianenigma
Copy link
Contributor Author

The conclusion is therefore to change the prefix, and let tools figure it out via the metadata.

@shawntabrizi
Copy link
Member

Previous usage of decl_storage set the pallet storage prefix to PhragmenElection, while in both Polkadot and kusama it is set to ElectionsPhragmen

Can't we just change the name in construct_runtime?

@kianenigma
Copy link
Contributor Author

kianenigma commented Apr 6, 2021

Previous usage of decl_storage set the pallet storage prefix to PhragmenElection, while in both Polkadot and kusama it is set to ElectionsPhragmen

Can't we just change the name in construct_runtime?

We could, but as noted above my preference is to fix this and move the storage as well.

I am fine with removing the migration from this PR and adding it as a separate PR, but that should make no difference and the code is not that complicated.

Also, we should provide the migration code anyhow, even if we are not going to use it ourselves. This pallet is the only one that I am experimentally trying to maintain with proper versions and all migrations are kept in the code as reusable snippets.

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.

looks good to me. with:

  • removal of added origin
  • change in pre_migration test function.
  • needs to merge master: ModuleId is changed into PalletId

log::info!("pre-migration elections-phragmen test with new = {}", new);

// ensure some stuff exist in the old prefix.
assert!(sp_io::storage::next_key(OLD_PREFIX).is_some());
Copy link
Contributor

Choose a reason for hiding this comment

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

this only ensures that there exist some key which are after OLD_PREFIX in the lexical order.
If OLD_PREFIX starts with 0 then any key which starts with 1 will make this succeed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

-       // ensure some stuff exist in the old prefix.
-       assert!(sp_io::storage::next_key(OLD_PREFIX).is_some());
+       // the next key must exist, and start with the hash of `OLD_PREFIX`.
+       let next_key = sp_io::storage::next_key(OLD_PREFIX).unwrap();
+       assert!(next_key.starts_with(&sp_io::hashing::twox_128(OLD_PREFIX)));
+

I think this makes good sense.

frame/elections-phragmen/src/lib.rs Outdated Show resolved Hide resolved
kianenigma and others added 3 commits April 15, 2021 09:14
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
…aritytech/substrate into kiz-elections-phragmen-attribute-macro
@kianenigma kianenigma added the D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit label Apr 15, 2021
@kianenigma
Copy link
Contributor Author

as recommended by @shawntabrizi, I removed the migrations from polkadot repo. The companion will instead rename the name of the pallet. Please check it letter by letter! :D

And once both PRs are approved I will merge this.

Note that I will still provide the migration scripts since other chains might use to actually migrate. I home my changelog is also enough to prevent people from breaking their chain if they accidentally apply this.


match maybe_storage_version {
Some(storage_version) if storage_version <= PalletVersion::new(3, 0, 0) => {
log::info!("new prefix: {}", new_pallet_name.as_ref());
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should check if old prefix == new prefix and if so we don't do the migration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed.

Copy link
Member

@shawntabrizi shawntabrizi left a comment

Choose a reason for hiding this comment

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

Not too sure we want to bump the version here, but okay.

@gnunicorn thoughts on that?

@kianenigma
Copy link
Contributor Author

FYI I've bumped this crates version in the past as well. I don't think it causes any issues for @gnunicorn as long as we don't release anything solo.

@kianenigma
Copy link
Contributor Author

bot merge

@ghost
Copy link

ghost commented Apr 23, 2021

Waiting for commit status.

@ghost
Copy link

ghost commented Apr 23, 2021

Merge aborted: Head SHA changed from 5ce2589 to cd908ed

@kianenigma
Copy link
Contributor Author

bot merge

@ghost
Copy link

ghost commented Apr 23, 2021

Trying merge.

@ghost ghost merged commit 20b1a0e into master Apr 23, 2021
@ghost ghost deleted the kiz-elections-phragmen-attribute-macro branch April 23, 2021 07:12
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants