Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relay Chain Accounts to Asset Hub Migration #515

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ repository = "https://github.com/polkadot-fellows/runtimes.git"
license = "GPL-3.0-only" # TODO <https://github.com/polkadot-fellows/runtimes/issues/29>

[workspace.dependencies]

# Asset Hub Migration concerning deps
pallet-rc-migrator = { path = "pallets/rc-migrator", default-features = false }
pallet-ah-migrator = { path = "pallets/ah-migrator", default-features = false }
# End: Asset Hub Migration concerning deps

assert_matches = { version = "1.5.0" }
approx = { version = "0.5.1" }
asset-hub-kusama-emulated-chain = { path = "integration-tests/emulated/chains/parachains/assets/asset-hub-kusama" }
Expand Down Expand Up @@ -284,6 +290,8 @@ members = [
"integration-tests/emulated/tests/people/people-kusama",
"integration-tests/emulated/tests/people/people-polkadot",
"integration-tests/zombienet",
"pallets/ah-migrator",
"pallets/rc-migrator",
"relay/common",
"relay/kusama",
"relay/kusama/constants",
Expand Down
83 changes: 83 additions & 0 deletions pallets/ah-migrator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
[package]
name = "pallet-ah-migrator"
description = "Operational pallet managing and processing migration from Relay Chain to Asset Hub on Asset Hub"
license = "Apache-2.0"
version = "0.1.0"
edition.workspace = true
authors.workspace = true
repository.workspace = true

[dependencies]
codec = { workspace = true, features = ["max-encoded-len"] }
scale-info = { workspace = true, features = ["derive"] }
serde = { features = ["derive"], optional = true, workspace = true }
log = { workspace = true }

frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
sp-io = { workspace = true }

pallet-rc-migrator = { workspace = true }
pallet-balances = { workspace = true }
pallet-staking = { workspace = true }
pallet-preimage = { workspace = true }
pallet-state-trie-migration = { workspace = true }
pallet-nomination-pools = { workspace = true }

polkadot-runtime-common = { workspace = true }
runtime-parachains = { workspace = true }
polkadot-parachain-primitives = { workspace = true }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"serde",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
"sp-io/std",
"pallet-rc-migrator/std",
"pallet-balances/std",
"pallet-staking/std",
"pallet-preimage/std",
"pallet-state-trie-migration/std",
"pallet-nomination-pools/std",
"polkadot-runtime-common/std",
"runtime-parachains/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-rc-migrator/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
"pallet-preimage/runtime-benchmarks",
"pallet-state-trie-migration/runtime-benchmarks",
"pallet-nomination-pools/runtime-benchmarks",
"polkadot-runtime-common/runtime-benchmarks",
"runtime-parachains/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
"pallet-rc-migrator/try-runtime",
"pallet-balances/try-runtime",
"pallet-staking/try-runtime",
"pallet-preimage/try-runtime",
"pallet-state-trie-migration/try-runtime",
"pallet-nomination-pools/try-runtime",
"polkadot-runtime-common/try-runtime",
"runtime-parachains/try-runtime",
]
187 changes: 187 additions & 0 deletions pallets/ah-migrator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! The operational pallet for the Asset Hub, designed to manage and facilitate the migration of
//! subsystems such as Governance, Staking, Balances from the Relay Chain to the Asset Hub. This
//! pallet works alongside its counterpart, `pallet_rc_migrator`, which handles migration
//! processes on the Relay Chain side.
//!
//! This pallet is responsible for controlling the initiation, progression, and completion of the
//! migration process, including managing its various stages and transferring the necessary data.
//! The pallet directly accesses the storage of other pallets for read/write operations while
//! maintaining compatibility with their existing APIs.
//!
//! To simplify development and avoid the need to edit the original pallets, this pallet may
//! duplicate private items such as storage entries from the original pallets. This ensures that the
//! migration logic can be implemented without altering the original implementations.

#![cfg_attr(not(feature = "std"), no_std)]

pub mod types;
pub use pallet::*;

use frame_support::{
pallet_prelude::*,
traits::{
fungible::{InspectFreeze, Mutate, MutateFreeze, MutateHold},
LockableCurrency, ReservableCurrency, WithdrawReasons as LockWithdrawReasons,
},
};
use frame_system::pallet_prelude::*;
use pallet_balances::{AccountData, Reasons as LockReasons};
use pallet_rc_migrator::accounts::Account as RcAccount;
use sp_runtime::{traits::Convert, AccountId32};
use sp_std::prelude::*;

/// The log target of this pallet.
pub const LOG_TARGET: &str = "runtime::ah-migrator";

#[frame_support::pallet(dev_mode)]
pub mod pallet {
use super::*;

/// Super config trait for all pallets that the migration depends on, providing convenient
/// access to their items.
#[pallet::config]
pub trait Config:
frame_system::Config<AccountData = AccountData<u128>, AccountId = AccountId32>
+ pallet_balances::Config<Balance = u128>
{
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Native asset registry type.
type Currency: Mutate<Self::AccountId, Balance = u128>
+ MutateHold<Self::AccountId, Reason = Self::RuntimeHoldReason>
+ InspectFreeze<Self::AccountId, Id = Self::FreezeIdentifier>
+ MutateFreeze<Self::AccountId>
+ ReservableCurrency<Self::AccountId, Balance = u128>
+ LockableCurrency<Self::AccountId, Balance = u128>;
/// XCM check account.
type CheckingAccount: Get<Self::AccountId>;
/// Relay Chain Hold Reasons.
type RcHoldReason: Parameter;
/// Relay Chain Freeze Reasons.
type RcFreezeReason: Parameter;
/// Relay Chain to Asset Hub Hold Reasons mapping;
type RcToAhHoldReason: Convert<Self::RcHoldReason, Self::RuntimeHoldReason>;
/// Relay Chain to Asset Hub Freeze Reasons mapping;
type RcToAhFreezeReason: Convert<Self::RcFreezeReason, Self::FreezeIdentifier>;
}

#[pallet::error]
pub enum Error<T> {
/// TODO
TODO,
}

#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
/// TODO
TODO,
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::call]
impl<T: Config> Pallet<T> {
// TODO: Currently, we use `debug_assert!` for easy test checks against a production
// snapshot.

/// Receive accounts from the Relay Chain.
///
/// The accounts that sent with `pallet_rc_migrator::Pallet::migrate_accounts` function.
#[pallet::call_index(0)]
#[pallet::weight({1})]
pub fn receive_accounts(
origin: OriginFor<T>,
accounts: Vec<RcAccount<T::AccountId, T::Balance, T::RcHoldReason, T::RcFreezeReason>>,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;

for account in accounts {
let who = account.who;
let total_balance = account.free + account.reserved;
let minted = T::Currency::mint_into(&who, total_balance)
// TODO handle error
.unwrap();
debug_assert!(minted == total_balance);

for hold in account.holds {
let _ = T::Currency::hold(
&T::RcToAhHoldReason::convert(hold.id),
&who,
hold.amount,
)
// TODO handle error
.unwrap();
}

let _ = T::Currency::reserve(&who, account.unnamed_reserve)
// TODO handle error
.unwrap();

for freeze in account.freezes {
let _ = T::Currency::set_freeze(
&T::RcToAhFreezeReason::convert(freeze.id),
&who,
freeze.amount,
)
// TODO handle error
.unwrap();
}

for lock in account.locks {
T::Currency::set_lock(
lock.id,
&who,
lock.amount,
types::map_lock_reason(lock.reasons),
);
}

let storage_account = pallet_balances::Account::<T>::get(&who);
debug_assert!(storage_account.free == account.free);
debug_assert!(storage_account.frozen == account.frozen);
debug_assert!(storage_account.reserved == account.reserved);

(0..account.consumers).for_each(|_| {
frame_system::Pallet::<T>::inc_consumers(&who)
// TODO handle error
.unwrap();
});
(0..account.providers).for_each(|_| {
frame_system::Pallet::<T>::inc_providers(&who);
});

// TODO: publish event
}

// TODO: publish event

Ok(().into())
}
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
Weight::zero()
}
}
}
Loading
Loading