Skip to content

Commit

Permalink
migrations: Add migration for account data
Browse files Browse the repository at this point in the history
  • Loading branch information
cdamian committed Nov 16, 2023
1 parent 6a5f505 commit 0654dac
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
2 changes: 2 additions & 0 deletions runtime/altair/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub type UpgradeAltair1034 = (
xcm_v2_to_v3::SetSafeXcmVersion,
// Sets account codes for all precompiles
runtime_common::migrations::precompile_account_codes::Migration<crate::Runtime>,
// Converts the old account data to the new format
runtime_common::migrations::balances::Migration<crate::Runtime>,
);

mod asset_registry {
Expand Down
2 changes: 1 addition & 1 deletion runtime/centrifuge/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

pub type UpgradeCentrifuge1024 = ();
pub type UpgradeCentrifuge1024 = (runtime_common::migrations::balances::Migration<crate::Runtime>);

Check warning on line 13 in runtime/centrifuge/src/migrations.rs

View workflow job for this annotation

GitHub Actions / docs

unnecessary parentheses around type

Check failure on line 13 in runtime/centrifuge/src/migrations.rs

View workflow job for this annotation

GitHub Actions / docs

unnecessary parentheses around type
53 changes: 52 additions & 1 deletion runtime/common/src/migrations/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
dispatch::TypeInfo, storage::unhashed, traits::OnRuntimeUpgrade, weights::Weight, RuntimeDebug,

Check warning on line 15 in runtime/common/src/migrations/balances.rs

View workflow job for this annotation

GitHub Actions / docs

unused imports: `StoragePrefixedMap`, `storage::unhashed`
StoragePrefixedMap,
};
use sp_arithmetic::traits::EnsureAdd;
use sp_runtime::DispatchError;

Check warning on line 19 in runtime/common/src/migrations/balances.rs

View workflow job for this annotation

GitHub Actions / docs

unused import: `sp_runtime::DispatchError`

/// All balance information for an account.
Expand Down Expand Up @@ -40,6 +45,8 @@ pub struct OldAccountData<Balance> {
pub fee_frozen: Balance,
}

pub type NewAccountData<Balance> = pallet_balances::AccountData<Balance>;

pub struct Migration<T: pallet_balances::Config>(sp_std::marker::PhantomData<T>);

impl<T> OnRuntimeUpgrade for Migration<T>
Expand All @@ -48,6 +55,26 @@ where
{
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, DispatchError> {
// Logic similar to the one found in StoragePrefixedMap::translate_values.

let account_data_prefix = pallet_balances::Account::<T>::final_prefix();

let mut previous_key = account_data_prefix.clone().to_vec();

while let Some(next) =
sp_io::storage::next_key(&previous_key).filter(|n| n.starts_with(&account_data_prefix))
{
previous_key = next;

let old_account_data = unhashed::get::<OldAccountData<T::Balance>>(&previous_key)
.ok_or_else(|| DispatchError::Other("old account data decoding"))?;

let new_account_data = Self::try_convert_account_data(old_account_data)
.map_err(|_| DispatchError::Other("old account data conversion"))?;

unhashed::put::<NewAccountData<T::Balance>>(&previous_key, &new_account_data)
}

// CHECKING DECODING OLD DATASTRUCTURE WITH NEW LAYOUT WORKS:
// * Fetch storage from chain with NEW data structure
// * Check if fetched accounts matches on-chain storage entries
Expand All @@ -74,3 +101,27 @@ where
Ok(())
}
}

impl<T> Migration<T>
where
T: pallet_balances::Config,
{
fn try_convert_account_data(

Check warning on line 109 in runtime/common/src/migrations/balances.rs

View workflow job for this annotation

GitHub Actions / docs

associated function `try_convert_account_data` is never used
old_account_data: OldAccountData<T::Balance>,
) -> Result<NewAccountData<T::Balance>, ()> {
// TODO(cdamian): Should we use saturated add?
let total_frozen = old_account_data
.fee_frozen
.ensure_add(old_account_data.misc_frozen)
.map_err(|_| ())?;

let new_account_data = NewAccountData::<T::Balance> {
free: old_account_data.free,
reserved: old_account_data.reserved,
frozen: total_frozen,
flags: Default::default(),
};

Ok(new_account_data)
}
}
2 changes: 1 addition & 1 deletion runtime/development/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

pub type UpgradeDevelopment1033 = ();
pub type UpgradeDevelopment1033 = (runtime_common::migrations::balances::Migration<crate::Runtime>);

Check warning on line 13 in runtime/development/src/migrations.rs

View workflow job for this annotation

GitHub Actions / docs

unnecessary parentheses around type

0 comments on commit 0654dac

Please sign in to comment.