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

Introduce on_runtime_upgrade #5058

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
12dc5ef
Initial idea of `on_runtime_upgrade`
shawntabrizi Feb 25, 2020
c0f524a
Runtime storage for module version
shawntabrizi Feb 25, 2020
42f8297
Merge remote-tracking branch 'upstream/master' into shawntabrizi-runt…
shawntabrizi Mar 3, 2020
8e8d588
Gui shawntabrizi runtime upgrade (#5118)
shawntabrizi Mar 3, 2020
731ea6c
make compile
shawntabrizi Mar 3, 2020
a36e3ab
Add some tests
shawntabrizi Mar 3, 2020
9eb8a21
docs
shawntabrizi Mar 3, 2020
dd6c6c3
Remove "useless" code
shawntabrizi Mar 3, 2020
5edb8e8
Fix merge and use n + 1 block number
shawntabrizi Mar 3, 2020
464229a
Fix tests
shawntabrizi Mar 3, 2020
9806b4d
unfix ui tests
shawntabrizi Mar 3, 2020
2f51595
Update on_initialize.stderr
shawntabrizi Mar 3, 2020
d039747
fix test
shawntabrizi Mar 3, 2020
7a23da9
Fix test
shawntabrizi Mar 3, 2020
3b55060
Bump spec
shawntabrizi Mar 3, 2020
f31ff58
Remove `on_finalise` and `on_initialise`
shawntabrizi Mar 3, 2020
8aecd6e
Use bool for tracking runtime upgraded
shawntabrizi Mar 3, 2020
107436e
typo
shawntabrizi Mar 4, 2020
ef19542
Support runtime upgrade with `set_storage`
shawntabrizi Mar 4, 2020
f0ddeb8
Refactor migration code location
shawntabrizi Mar 4, 2020
cf9a189
add trailing newlines
shawntabrizi Mar 4, 2020
fbc40bb
Remove old `IsUpgraded` flag
shawntabrizi Mar 4, 2020
490d69c
Merge remote-tracking branch 'upstream/master' into shawntabrizi-runt…
shawntabrizi Mar 4, 2020
0de3f97
Update state root
shawntabrizi Mar 4, 2020
446f77e
Exhaustive match statement
shawntabrizi Mar 5, 2020
d91b972
Merge branch 'master' into shawntabrizi-runtime-upgrade
shawntabrizi Mar 5, 2020
d8e7607
Apply suggestions from code review
shawntabrizi Mar 5, 2020
0a68c6f
Merge branch 'master' into shawntabrizi-runtime-upgrade
shawntabrizi Mar 5, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 6 additions & 84 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ mod tests_composite;
#[macro_use]
mod tests;
mod benchmarking;
mod migration;

use sp_std::prelude::*;
use sp_std::{cmp, result, mem, fmt::Debug, ops::BitOr, convert::Infallible};
Expand Down Expand Up @@ -366,10 +367,10 @@ decl_storage! {
/// NOTE: Should only be accessed when setting, changing and freeing a lock.
pub Locks get(fn locks): map hasher(blake2_256) T::AccountId => Vec<BalanceLock<T::Balance>>;

/// True if network has been upgraded to this version.
/// Module version number.
///
/// True for new networks.
IsUpgraded build(|_: &GenesisConfig<T, I>| true): bool;
/// Default to version 1 for new networks.
ModuleVersion build(|_: &GenesisConfig<T, I>| 1): u8;
}
add_extra_genesis {
config(balances): Vec<(T::AccountId, T::Balance)>;
Expand Down Expand Up @@ -517,11 +518,8 @@ decl_module! {
<Self as Currency<_>>::transfer(&transactor, &dest, value, KeepAlive)?;
}

fn on_initialize() {
if !IsUpgraded::<I>::get() {
IsUpgraded::<I>::put(true);
Self::do_upgrade();
}
fn on_runtime_upgrade() {
migration::on_runtime_upgrade::<T, I>();
}
}
}
Expand All @@ -547,82 +545,6 @@ impl<Balance, BlockNumber> OldBalanceLock<Balance, BlockNumber> {
impl<T: Trait<I>, I: Instance> Module<T, I> {
// PRIVATE MUTABLES

// Upgrade from the pre-#4649 balances/vesting into the new balances.
pub fn do_upgrade() {
sp_runtime::print("Upgrading account balances...");
// 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));
}
}

/// Get the free balance of an account.
pub fn free_balance(who: impl sp_std::borrow::Borrow<T::AccountId>) -> T::Balance {
Self::account(who.borrow()).free
Expand Down
87 changes: 87 additions & 0 deletions frame/balances/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use super::*;

// Upgrade from the pre-#4649 balances/vesting into the new balances.
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
pub fn on_runtime_upgrade<T: Trait<I>, I: Instance>() {
let current_version = ModuleVersion::<I>::get();

if current_version == 1 {
return
} else if current_version == 0 {
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
ModuleVersion::<I>::put(1);
sp_runtime::print("Upgrading account balances...");
// 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);
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
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));
}

// Recursively call the upgrade function to apply all upgrades.
on_runtime_upgrade::<T, I>()
}
}
12 changes: 11 additions & 1 deletion frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ use sp_runtime::{
ApplyExtrinsicResult,
traits::{
self, Header, Zero, One, Checkable, Applyable, CheckEqual, OnFinalize, OnInitialize,
NumberFor, Block as BlockT, OffchainWorker, Dispatchable, Saturating,
NumberFor, Block as BlockT, OffchainWorker, Dispatchable, Saturating, OnRuntimeUpgrade,
},
transaction_validity::TransactionValidity,
};
Expand Down Expand Up @@ -110,6 +110,7 @@ impl<
Context: Default,
UnsignedValidator,
AllModules:
OnRuntimeUpgrade +
OnInitialize<System::BlockNumber> +
OnFinalize<System::BlockNumber> +
OffchainWorker<System::BlockNumber> +
Expand All @@ -135,6 +136,7 @@ impl<
Context: Default,
UnsignedValidator,
AllModules:
OnRuntimeUpgrade +
OnInitialize<System::BlockNumber> +
OnFinalize<System::BlockNumber> +
OffchainWorker<System::BlockNumber> +
Expand Down Expand Up @@ -176,13 +178,21 @@ where
extrinsics_root: &System::Hash,
digest: &Digest<System::Hash>,
) {
<AllModules as OnRuntimeUpgrade>::on_runtime_upgrade();
gui1117 marked this conversation as resolved.
Show resolved Hide resolved
<frame_system::Module<System>>::initialize(
block_number,
parent_hash,
extrinsics_root,
digest,
frame_system::InitKind::Full,
);
let last_runtime_upgrade = <frame_system::Module<System>>::last_runtime_upgrade();
if last_runtime_upgrade.map(|n| n == *block_number).unwrap_or(false) {
<AllModules as OnRuntimeUpgrade>::on_runtime_upgrade();
<frame_system::Module<System>>::register_extra_weight_unchecked(
<AllModules as WeighBlock<System::BlockNumber>>::on_runtime_upgrade()
);
}
<AllModules as OnInitialize<System::BlockNumber>>::on_initialize(*block_number);
<frame_system::Module<System>>::register_extra_weight_unchecked(
<AllModules as WeighBlock<System::BlockNumber>>::on_initialize(*block_number)
Expand Down
5 changes: 4 additions & 1 deletion frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,10 +927,13 @@ decl_module! {

fn deposit_event() = default;

fn on_initialize() {
fn on_runtime_upgrade() {
Self::ensure_storage_upgraded();
}

fn on_initialize() {
}

fn on_finalize() {
// Set the start of the first era.
if let Some(mut active_era) = Self::active_era() {
Expand Down
Loading