Skip to content

Commit

Permalink
Balance account data migration fix (#2960)
Browse files Browse the repository at this point in the history
* feat: add migration fix

* chore: bump version

* chore

* chore: typo

* chore: replace comment
  • Loading branch information
wangminqi committed Aug 5, 2024
1 parent 31368f8 commit 58a8d1e
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 21 deletions.
2 changes: 1 addition & 1 deletion runtime/litentry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("litentry-parachain"),
authoring_version: 1,
// same versioning-mechanism as polkadot: use last digit for minor updates
spec_version: 9190,
spec_version: 9191,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
account_info.data.free = account_info.data.free.saturating_mul(DECIMAL_CONVERTOR);
account_info.data.reserved =
account_info.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
account_info.data.frozen = account_info.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
account_info.data.frozen = account_info.data.frozen.saturating_mul(DECIMAL_CONVERTOR);

<Account<T>>::insert(&account, account_info);
weight += T::DbWeight::get().reads_writes(1, 1);
Expand Down Expand Up @@ -142,7 +142,7 @@ where
new_account.data.reserved =
new_account.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
new_account.data.frozen =
new_account.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
new_account.data.frozen.saturating_mul(DECIMAL_CONVERTOR);

(account, new_account)
})
Expand Down
2 changes: 1 addition & 1 deletion runtime/litmus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("litmus-parachain"),
authoring_version: 1,
// same versioning-mechanism as polkadot: use last digit for minor updates
spec_version: 9190,
spec_version: 9191,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
4 changes: 2 additions & 2 deletions runtime/litmus/src/migration/balances_transaction_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
account_info.data.free = account_info.data.free.saturating_mul(DECIMAL_CONVERTOR);
account_info.data.reserved =
account_info.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
account_info.data.frozen = account_info.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
account_info.data.frozen = account_info.data.frozen.saturating_mul(DECIMAL_CONVERTOR);

<Account<T>>::insert(&account, account_info);
weight += T::DbWeight::get().reads_writes(1, 1);
Expand Down Expand Up @@ -140,7 +140,7 @@ where
new_account.data.reserved =
new_account.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
new_account.data.frozen =
new_account.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
new_account.data.frozen.saturating_mul(DECIMAL_CONVERTOR);

(account, new_account)
})
Expand Down
8 changes: 2 additions & 6 deletions runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,7 @@ pub type Executive = frame_executive::Executive<
// it was reverse order before.
// See the comment before collation related pallets too.
AllPalletsWithSystem,
(
migration::ReplaceParachainStakingStorage<Runtime>,
migration::ReplaceBalancesRelatedStorage<Runtime>,
migration::ReplaceBridgeRelatedStorage<Runtime>,
),
migration::ForceFixAccountFrozenStorage<Runtime>,
>;

impl fp_self_contained::SelfContainedCall for RuntimeCall {
Expand Down Expand Up @@ -255,7 +251,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_name: create_runtime_str!("rococo-parachain"),
authoring_version: 1,
// same versioning-mechanism as polkadot: use last digit for minor updates
spec_version: 9190,
spec_version: 9191,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ where
account_info.data.free = account_info.data.free.saturating_mul(DECIMAL_CONVERTOR);
account_info.data.reserved =
account_info.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
account_info.data.frozen = account_info.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
// Runtime migration is wrong at first. The below code has been mistakenly applied to
// online rococo account_info.data.frozen =
// account_info.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
account_info.data.frozen = account_info.data.frozen.saturating_mul(DECIMAL_CONVERTOR);

<Account<T>>::insert(&account, account_info);
weight += T::DbWeight::get().reads_writes(1, 1);
Expand Down Expand Up @@ -140,7 +143,7 @@ where
new_account.data.reserved =
new_account.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
new_account.data.frozen =
new_account.data.reserved.saturating_mul(DECIMAL_CONVERTOR);
new_account.data.frozen.saturating_mul(DECIMAL_CONVERTOR);

(account, new_account)
})
Expand Down
6 changes: 6 additions & 0 deletions runtime/rococo/src/migration/P9190/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod bridge_related;
pub use bridge_related::ReplaceBridgeRelatedStorage;
pub mod parachain_staking;
pub use parachain_staking::ReplaceParachainStakingStorage;
pub mod balances_transaction_payment;
pub use balances_transaction_payment::ReplaceBalancesRelatedStorage;
10 changes: 10 additions & 0 deletions runtime/rococo/src/migration/migration.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Migrate
Under files P9190
These migration is for the follwoing task
https://github.com/litentry/litentry-parachain/releases/tag/v0.9.19
(1) token decimal change from 12 to 18
(2) New token bridge related pallet storage migration.

For rococo, the initial migration contains some error code which make account.frozen = account.reserve * 10^6
This storage error should be fixed in the following migration of rococo

# MigrateCollatorSelectionIntoParachainStaking
P9100.rs
https://github.com/litentry/litentry-parachain/releases/tag/v0.9.10
Expand Down
95 changes: 89 additions & 6 deletions runtime/rococo/src/migration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,89 @@
pub mod bridge_related;
pub use bridge_related::ReplaceBridgeRelatedStorage;
pub mod parachain_staking;
pub use parachain_staking::ReplaceParachainStakingStorage;
pub mod balances_transaction_payment;
pub use balances_transaction_payment::ReplaceBalancesRelatedStorage;
// Copyright 2020-2024 Trust Computing GmbH.
// This file is part of Litentry.
//
// Litentry is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Litentry is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.
#![allow(clippy::type_complexity)]

use frame_support::{
migration::storage_key_iter,
pallet_prelude::*,
traits::{Get, OnRuntimeUpgrade},
};
use frame_system::{Account, AccountInfo};
use pallet_balances::AccountData;
use sp_std::{marker::PhantomData, vec::Vec};

#[cfg(feature = "try-runtime")]
use sp_std::collections::btree_map::BTreeMap;

// Force System Balances Storage frozen amount to 0
pub struct ForceFixAccountFrozenStorage<T>(PhantomData<T>);
impl<T> OnRuntimeUpgrade for ForceFixAccountFrozenStorage<T>
where
T: frame_system::Config<AccountData = AccountData<u128>>
+ pallet_balances::Config<Balance = u128>,
{
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
let result: BTreeMap<T::AccountId, AccountInfo<T::Index, AccountData<u128>>> =
<Account<T>>::iter()
.map(|(account, state)| {
let mut new_account: AccountInfo<T::Index, AccountData<u128>> = state;
new_account.data.frozen = 0u128;

(account, new_account)
})
.collect();
Ok(result.encode())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
// The storage of Account for pallet balances is in frame_system pallet
log::info!(
target: "ReplaceBalancesRelatedStorage",
"running migration to Frame System Account"
);
let pallet_prefix: &[u8] = b"System";
let storage_item_prefix: &[u8] = b"Account";
let mut weight: Weight = frame_support::weights::Weight::zero();

for (account, mut account_info) in storage_key_iter::<
T::AccountId,
AccountInfo<T::Index, T::AccountData>,
Blake2_128Concat,
>(pallet_prefix, storage_item_prefix)
.drain()
{
account_info.data.frozen = 0u128;
<Account<T>>::insert(&account, account_info);
weight += T::DbWeight::get().reads_writes(1, 1);
}
weight
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> {
let expected_state =
BTreeMap::<T::AccountId, AccountInfo<T::Index, AccountData<u128>>>::decode(
&mut &state[..],
)
.map_err(|_| "Failed to decode AccountInfo")?;
for (account, actual_result) in <Account<T>>::iter() {
let expected_result: AccountInfo<T::Index, AccountData<u128>> =
expected_state.get(&account).ok_or("Not Expected AccountInfo")?.clone();
assert_eq!(expected_result, actual_result);
}
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export async function assertVc(context: IntegrationTestContext, subject: CorePri
// check runtime version is present
assert.deepEqual(
vcPayloadJson.issuer.runtimeVersion,
{ parachain: 9190, sidechain: 109 },
{ parachain: 9191, sidechain: 109 },
'Check VC runtime version: it should equal the current defined versions'
);

Expand Down

0 comments on commit 58a8d1e

Please sign in to comment.