This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Improve post and pre migration checks for pallet-membership #9746
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f72b118
improve post and pre migration checks
gui1117 86c1105
prevent some more false positive
gui1117 9999f6b
prevent another false positive
gui1117 3b8bbab
fix unused import
gui1117 bbb6039
Apply suggestions from code review
gui1117 4a4a1d1
Merge remote-tracking branch 'origin/master' into gui-improve-memeber…
gui1117 7ce0438
Merge remote-tracking branch 'origin/master' into gui-improve-memeber…
gui1117 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,7 @@ | |
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use sp_core::hexdisplay::HexDisplay; | ||
| use sp_io::{hashing::twox_128, storage}; | ||
| use sp_io::hashing::twox_128; | ||
|
|
||
| use frame_support::{ | ||
| traits::{ | ||
|
|
@@ -85,28 +84,22 @@ pub fn pre_migrate<P: GetStorageVersion, N: AsRef<str>>(old_pallet_name: N, new_ | |
| let new_pallet_name = new_pallet_name.as_ref(); | ||
| log_migration("pre-migration", old_pallet_name, new_pallet_name); | ||
|
|
||
| let old_pallet_prefix = twox_128(old_pallet_name.as_bytes()); | ||
| assert!(storage::next_key(&old_pallet_prefix) | ||
| .map_or(true, |next_key| next_key.starts_with(&old_pallet_prefix))); | ||
| if new_pallet_name == old_pallet_name { | ||
| return | ||
| } | ||
|
|
||
| let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); | ||
| let storage_version_key = | ||
| [&new_pallet_prefix, &twox_128(STORAGE_VERSION_STORAGE_KEY_POSTFIX)[..]].concat(); | ||
| // ensure nothing is stored in the new prefix. | ||
| assert!( | ||
| storage::next_key(&new_pallet_prefix).map_or( | ||
| // either nothing is there | ||
| true, | ||
| // or we ensure that it has no common prefix with twox_128(new), | ||
| // or isn't the storage version that is already stored using the pallet name | ||
| |next_key| { | ||
| !next_key.starts_with(&new_pallet_prefix) || next_key == storage_version_key | ||
| }, | ||
| ), | ||
| "unexpected next_key({}) = {:?}", | ||
| new_pallet_name, | ||
| HexDisplay::from(&storage::next_key(&new_pallet_prefix).unwrap()), | ||
| let storage_version_key = twox_128(STORAGE_VERSION_STORAGE_KEY_POSTFIX); | ||
|
|
||
| let mut new_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new( | ||
| new_pallet_prefix.to_vec(), | ||
| new_pallet_prefix.to_vec(), | ||
| |key| Ok(key.to_vec()), | ||
| ); | ||
|
|
||
| // Ensure nothing except maybe the storage_version_key is stored in the new prefix. | ||
| assert!(new_pallet_prefix_iter.all(|key| key == storage_version_key)); | ||
|
|
||
| assert!(<P as GetStorageVersion>::on_chain_storage_version() < 4); | ||
| } | ||
|
|
||
|
|
@@ -119,26 +112,27 @@ pub fn post_migrate<P: GetStorageVersion, N: AsRef<str>>(old_pallet_name: N, new | |
| let new_pallet_name = new_pallet_name.as_ref(); | ||
| log_migration("post-migration", old_pallet_name, new_pallet_name); | ||
|
|
||
| let old_pallet_prefix = twox_128(old_pallet_name.as_bytes()); | ||
| #[cfg(test)] | ||
| { | ||
| let storage_version_key = | ||
| [&old_pallet_prefix, &twox_128(STORAGE_VERSION_STORAGE_KEY_POSTFIX)[..]].concat(); | ||
| assert!(storage::next_key(&old_pallet_prefix) | ||
| .map_or(true, |next_key| !next_key.starts_with(&old_pallet_prefix) || | ||
| next_key == storage_version_key)); | ||
| } | ||
| #[cfg(not(test))] | ||
| { | ||
| // Assert that nothing remains at the old prefix | ||
| assert!(storage::next_key(&old_pallet_prefix) | ||
| .map_or(true, |next_key| !next_key.starts_with(&old_pallet_prefix))); | ||
| if new_pallet_name == old_pallet_name { | ||
| return | ||
| } | ||
|
|
||
| // Assert that nothing remains at the old prefix. | ||
| let old_pallet_prefix = twox_128(old_pallet_name.as_bytes()); | ||
| let old_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new( | ||
| old_pallet_prefix.to_vec(), | ||
| old_pallet_prefix.to_vec(), | ||
| |_| Ok(()), | ||
| ); | ||
| assert_eq!(old_pallet_prefix_iter.count(), 0); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the past we only checked for the first key at the old prefix, and we accepted that it was the storage_version_key, now we ensure nothing remains there. |
||
|
|
||
| // NOTE: storage_version_key is already in the new prefix. | ||
| let new_pallet_prefix = twox_128(new_pallet_name.as_bytes()); | ||
| // Assert that the storages have been moved to the new prefix | ||
| assert!(storage::next_key(&new_pallet_prefix) | ||
| .map_or(true, |next_key| next_key.starts_with(&new_pallet_prefix))); | ||
| let new_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new( | ||
| new_pallet_prefix.to_vec(), | ||
| new_pallet_prefix.to_vec(), | ||
| |_| Ok(()), | ||
| ); | ||
| assert!(new_pallet_prefix_iter.count() >= 1); | ||
|
|
||
| assert_eq!(<P as GetStorageVersion>::on_chain_storage_version(), 4); | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
in the past we only ensured that the first key was sotrage_version_key, now we ensure it is the only one.
Uh oh!
There was an error while loading. Please reload this page.
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.
tiny nit, but this doesn't check length and at this point we only expect one key under the new prefix, right? Would it be more accurate to do:
?
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.
yes it is true, but I was thinking if they change the name, maybe it is ok not to have anything stored at the new prefix.