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 26 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
4 changes: 2 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 227,
impl_version: 1,
spec_version: 228,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
};

Expand Down
105 changes: 21 additions & 84 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ mod tests_composite;
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
mod migration;

use sp_std::prelude::*;
use sp_std::{cmp, result, mem, fmt::Debug, ops::BitOr, convert::Infallible};
Expand Down Expand Up @@ -348,6 +349,21 @@ impl<Balance: Saturating + Copy + Ord> AccountData<Balance> {
}
}

// A value placed in storage that represents the current version of the Balances storage.
// This value is used by the `on_runtime_upgrade` logic to determine whether we run
// storage migration logic. This should match directly with the semantic versions of the Rust crate.
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug)]
enum Releases {
V1_0_0,
V2_0_0,
}

impl Default for Releases {
fn default() -> Self {
Releases::V1_0_0
}
}

decl_storage! {
trait Store for Module<T: Trait<I>, I: Instance=DefaultInstance> as Balances {
/// The total units issued in the system.
Expand All @@ -367,10 +383,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.
/// Storage version of the pallet.
///
/// True for new networks.
IsUpgraded build(|_: &GenesisConfig<T, I>| true): bool;
/// This is set to v2.0.0 for new networks.
StorageVersion build(|_: &GenesisConfig<T, I>| Releases::V2_0_0): Releases;
}
add_extra_genesis {
config(balances): Vec<(T::AccountId, T::Balance)>;
Expand Down Expand Up @@ -518,11 +534,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 @@ -548,82 +561,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
88 changes: 88 additions & 0 deletions frame/balances/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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>() {
match StorageVersion::<I>::get() {
Releases::V2_0_0 => return,
Releases::V1_0_0 => upgrade_v1_to_v2::<T, I>(),
}
}

fn upgrade_v1_to_v2<T: Trait<I>, I: Instance>() {
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
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);
}
4 changes: 2 additions & 2 deletions frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,7 +1555,7 @@ mod tests {
};
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup, Bounded, BadOrigin, OnInitialize},
traits::{BlakeTwo256, IdentityLookup, Bounded, BadOrigin, OnRuntimeUpgrade},
testing::Header, Perbill,
};
use pallet_balances::{BalanceLock, Error as BalancesError};
Expand Down Expand Up @@ -1713,7 +1713,7 @@ mod tests {
];
s.top = data.into_iter().collect();
sp_io::TestExternalities::new(s).execute_with(|| {
Balances::on_initialize(1);
Balances::on_runtime_upgrade();
assert_eq!(Balances::free_balance(1), 5);
assert_eq!(Balances::reserved_balance(1), 5);
assert_eq!(Balances::usable_balance(&1), 2);
Expand Down
17 changes: 14 additions & 3 deletions frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@
#![cfg_attr(not(feature = "std"), no_std)]

use sp_std::{prelude::*, marker::PhantomData};
use frame_support::weights::{GetDispatchInfo, WeighBlock, DispatchInfo};
use frame_support::{
storage::StorageValue,
weights::{GetDispatchInfo, WeighBlock, DispatchInfo}
};
use sp_runtime::{
generic::Digest,
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 +113,7 @@ impl<
Context: Default,
UnsignedValidator,
AllModules:
OnRuntimeUpgrade +
OnInitialize<System::BlockNumber> +
OnFinalize<System::BlockNumber> +
OffchainWorker<System::BlockNumber> +
Expand All @@ -135,6 +139,7 @@ impl<
Context: Default,
UnsignedValidator,
AllModules:
OnRuntimeUpgrade +
OnInitialize<System::BlockNumber> +
OnFinalize<System::BlockNumber> +
OffchainWorker<System::BlockNumber> +
Expand Down Expand Up @@ -176,6 +181,12 @@ where
extrinsics_root: &System::Hash,
digest: &Digest<System::Hash>,
) {
if frame_system::RuntimeUpgraded::take() {
<AllModules as OnRuntimeUpgrade>::on_runtime_upgrade();
<frame_system::Module<System>>::register_extra_weight_unchecked(
<AllModules as WeighBlock<System::BlockNumber>>::on_runtime_upgrade()
);
}
<frame_system::Module<System>>::initialize(
block_number,
parent_hash,
Expand Down Expand Up @@ -573,7 +584,7 @@ mod tests {
header: Header {
parent_hash: [69u8; 32].into(),
number: 1,
state_root: hex!("17caebd966d10cc6dc9659edf7fa3196511593f6c39f80f9b97cdbc3b0855cf3").into(),
state_root: hex!("8a22606e925c39bb0c8e8f6f5871c0aceab88a2fcff6b2d92660af8f6daff0b1").into(),
extrinsics_root: hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(),
digest: Digest { logs: vec![], },
},
Expand Down
Loading