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
Introduce on_runtime_upgrade
#5058
Merged
gavofyork
merged 28 commits into
paritytech:master
from
shawntabrizi:shawntabrizi-runtime-upgrade
Mar 5, 2020
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
12dc5ef
Initial idea of `on_runtime_upgrade`
shawntabrizi c0f524a
Runtime storage for module version
shawntabrizi 42f8297
Merge remote-tracking branch 'upstream/master' into shawntabrizi-runt…
shawntabrizi 8e8d588
Gui shawntabrizi runtime upgrade (#5118)
shawntabrizi 731ea6c
make compile
shawntabrizi a36e3ab
Add some tests
shawntabrizi 9eb8a21
docs
shawntabrizi dd6c6c3
Remove "useless" code
shawntabrizi 5edb8e8
Fix merge and use n + 1 block number
shawntabrizi 464229a
Fix tests
shawntabrizi 9806b4d
unfix ui tests
shawntabrizi 2f51595
Update on_initialize.stderr
shawntabrizi d039747
fix test
shawntabrizi 7a23da9
Fix test
shawntabrizi 3b55060
Bump spec
shawntabrizi f31ff58
Remove `on_finalise` and `on_initialise`
shawntabrizi 8aecd6e
Use bool for tracking runtime upgraded
shawntabrizi 107436e
typo
shawntabrizi ef19542
Support runtime upgrade with `set_storage`
shawntabrizi f0ddeb8
Refactor migration code location
shawntabrizi cf9a189
add trailing newlines
shawntabrizi fbc40bb
Remove old `IsUpgraded` flag
shawntabrizi 490d69c
Merge remote-tracking branch 'upstream/master' into shawntabrizi-runt…
shawntabrizi 0de3f97
Update state root
shawntabrizi 446f77e
Exhaustive match statement
shawntabrizi d91b972
Merge branch 'master' into shawntabrizi-runtime-upgrade
shawntabrizi d8e7607
Apply suggestions from code review
shawntabrizi 0a68c6f
Merge branch 'master' into shawntabrizi-runtime-upgrade
shawntabrizi 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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| use super::*; | ||
|
|
||
| pub fn on_runtime_upgrade<T: Trait<I>, I: Instance>() { | ||
| match StorageVersion::<I>::get() { | ||
| Releases::V2_0_0 => return, | ||
| Releases::V1_0_0 => upgrade_v1_to_v2::<T, I>(), | ||
| } | ||
| } | ||
|
|
||
| // Upgrade from the pre-#4649 balances/vesting into the new balances. | ||
| fn upgrade_v1_to_v2<T: Trait<I>, I: Instance>() { | ||
| sp_runtime::print("Upgrading account balances..."); | ||
shawntabrizi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // First, migrate from old FreeBalance to new Account. | ||
| // We also move all locks across since only accounts with FreeBalance values have locks. | ||
| // FreeBalance: map T::AccountId => T::Balance | ||
| for (hash, free) in StorageIterator::<T::Balance>::new(b"Balances", b"FreeBalance").drain() { | ||
| let mut account = AccountData { free, ..Default::default() }; | ||
| // Locks: map T::AccountId => Vec<BalanceLock> | ||
| let old_locks = get_storage_value::<Vec<OldBalanceLock<T::Balance, T::BlockNumber>>>(b"Balances", b"Locks", &hash); | ||
| if let Some(locks) = old_locks { | ||
| let locks = locks.into_iter() | ||
| .map(|i| { | ||
| let (result, expiry) = i.upgraded(); | ||
| if expiry != T::BlockNumber::max_value() { | ||
| // Any `until`s that are not T::BlockNumber::max_value come from | ||
| // democracy and need to be migrated over there. | ||
| // Democracy: Locks get(locks): map T::AccountId => Option<T::BlockNumber>; | ||
| put_storage_value(b"Democracy", b"Locks", &hash, expiry); | ||
| } | ||
| result | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
| for l in locks.iter() { | ||
| if l.reasons == Reasons::All || l.reasons == Reasons::Misc { | ||
| account.misc_frozen = account.misc_frozen.max(l.amount); | ||
| } | ||
| if l.reasons == Reasons::All || l.reasons == Reasons::Fee { | ||
| account.fee_frozen = account.fee_frozen.max(l.amount); | ||
| } | ||
| } | ||
| put_storage_value(b"Balances", b"Locks", &hash, locks); | ||
| } | ||
| put_storage_value(b"Balances", b"Account", &hash, account); | ||
| } | ||
| // Second, migrate old ReservedBalance into new Account. | ||
| // ReservedBalance: map T::AccountId => T::Balance | ||
| for (hash, reserved) in StorageIterator::<T::Balance>::new(b"Balances", b"ReservedBalance").drain() { | ||
| let mut account = get_storage_value::<AccountData<T::Balance>>(b"Balances", b"Account", &hash).unwrap_or_default(); | ||
| account.reserved = reserved; | ||
| put_storage_value(b"Balances", b"Account", &hash, account); | ||
| } | ||
|
|
||
| // Finally, migrate vesting and ensure locks are in place. We will be lazy and just lock | ||
| // for the maximum amount (i.e. at genesis). Users will need to call "vest" to reduce the | ||
| // lock to something sensible. | ||
| // pub Vesting: map T::AccountId => Option<VestingSchedule>; | ||
| for (hash, vesting) in StorageIterator::<(T::Balance, T::Balance, T::BlockNumber)>::new(b"Balances", b"Vesting").drain() { | ||
| let mut account = get_storage_value::<AccountData<T::Balance>>(b"Balances", b"Account", &hash).unwrap_or_default(); | ||
| let mut locks = get_storage_value::<Vec<BalanceLock<T::Balance>>>(b"Balances", b"Locks", &hash).unwrap_or_default(); | ||
| locks.push(BalanceLock { | ||
| id: *b"vesting ", | ||
| amount: vesting.0.clone(), | ||
| reasons: Reasons::Misc, | ||
| }); | ||
| account.misc_frozen = account.misc_frozen.max(vesting.0.clone()); | ||
| put_storage_value(b"Vesting", b"Vesting", &hash, vesting); | ||
| put_storage_value(b"Balances", b"Locks", &hash, locks); | ||
| put_storage_value(b"Balances", b"Account", &hash, account); | ||
| } | ||
|
|
||
| for (hash, balances) in StorageIterator::<AccountData<T::Balance>>::new(b"Balances", b"Account").drain() { | ||
| let nonce = take_storage_value::<T::Index>(b"System", b"AccountNonce", &hash).unwrap_or_default(); | ||
| let mut refs: system::RefCount = 0; | ||
| // The items in Kusama that would result in a ref count being incremented. | ||
| if have_storage_value(b"Democracy", b"Proxy", &hash) { refs += 1 } | ||
| // We skip Recovered since it's being replaced anyway. | ||
| let mut prefixed_hash = twox_64(&b":session:keys"[..]).to_vec(); | ||
| prefixed_hash.extend(&b":session:keys"[..]); | ||
| prefixed_hash.extend(&hash[..]); | ||
| if have_storage_value(b"Session", b"NextKeys", &prefixed_hash) { refs += 1 } | ||
| if have_storage_value(b"Staking", b"Bonded", &hash) { refs += 1 } | ||
| put_storage_value(b"System", b"Account", &hash, (nonce, refs, &balances)); | ||
| } | ||
|
|
||
| take_storage_value::<T::Index>(b"Balances", b"IsUpgraded", &[]); | ||
|
|
||
| StorageVersion::<I>::put(Releases::V2_0_0); | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.