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

Identity Deposits Relay to Parachain Migration #1814

Merged
merged 50 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
20fa029
add migration extrinsics to identity
joepetrowski Oct 6, 2023
7becd9e
add to runtimes
joepetrowski Oct 6, 2023
fd39b82
adjust trait
joepetrowski Oct 6, 2023
24b9d0d
Add OnReapIdentity implementation for Relay Chain
joepetrowski Oct 7, 2023
96b2add
add license
joepetrowski Oct 9, 2023
54d15a1
Merge branch 'master' into joe-identity-deposit-migration
joepetrowski Oct 9, 2023
f9ac75e
add ability to lock and unlock pallet
joepetrowski Oct 10, 2023
4f45cc8
address most of review comments
joepetrowski Oct 10, 2023
64acbb9
Merge branch 'joe-identity-deposit-migration' of https://github.com/p…
joepetrowski Oct 10, 2023
0b3708e
fix alliance test config
joepetrowski Oct 10, 2023
7ae9df0
benchmark reap_identity properly
joepetrowski Oct 11, 2023
b517c91
put deposit info into separate function
joepetrowski Oct 11, 2023
f6e8220
merge master
joepetrowski Oct 11, 2023
c4b45d7
update benchmarks to v2
joepetrowski Oct 11, 2023
cff4f4c
Merge branch 'master' into joe-identity-deposit-migration
joepetrowski Oct 11, 2023
776910c
test that correct functions get locked
joepetrowski Oct 14, 2023
c2eb330
Merge remote-tracking branch 'origin' into joe-identity-deposit-migra…
joepetrowski Oct 17, 2023
88a9923
no need to filter with pallet lock
joepetrowski Oct 17, 2023
67de971
Merge branch 'master' into joe-identity-deposit-migration
joepetrowski Oct 23, 2023
a46e4ef
Merge branch 'master' into joe-identity-deposit-migration
joepetrowski Oct 24, 2023
390ce01
fix compile errors on merge
joepetrowski Oct 24, 2023
9da0726
add poke call encoding to handler impl
joepetrowski Oct 25, 2023
255893d
don't need RuntimeDebug
joepetrowski Oct 25, 2023
298931a
Reduce Impact on Identity Pallet in Migration (#2088)
joepetrowski Oct 31, 2023
cd08c4f
Merge branch 'master' into joe-identity-deposit-migration
joepetrowski Nov 3, 2023
1f03941
merge master
joepetrowski Nov 3, 2023
8e70450
actually merge master
joepetrowski Nov 3, 2023
4aaa862
update identity pallet
joepetrowski Nov 3, 2023
70f708b
migrator updates
joepetrowski Nov 3, 2023
e56874c
Merge branch 'master' into joe-identity-deposit-migration
joepetrowski Nov 5, 2023
180352d
reap benchmark
joepetrowski Nov 5, 2023
9bf6499
remove migration benchmarks from pallet_identity
joepetrowski Nov 6, 2023
48a33d6
add to define_benchmarks
joepetrowski Nov 6, 2023
750c440
fix runtime errors
joepetrowski Nov 7, 2023
11e2999
Merge branch 'master' into joe-identity-deposit-migration
joepetrowski Nov 7, 2023
0030969
remove pallet lock leftovers
joepetrowski Nov 7, 2023
ed0d8e9
disable calls to identity (again)
joepetrowski Nov 7, 2023
cb7bda0
fix benchmark test suite
joepetrowski Nov 7, 2023
76258be
make unused imports happy
joepetrowski Nov 7, 2023
ace6f41
correct and clarify some docs
joepetrowski Nov 9, 2023
de3e4e8
nits
joepetrowski Nov 11, 2023
be38015
add westend config
joepetrowski Nov 11, 2023
1c670b7
fmt
joepetrowski Nov 11, 2023
836f824
set deposits for parachains
joepetrowski Nov 14, 2023
d698705
replace accidentally deleted docs
joepetrowski Nov 14, 2023
ea5d6a3
use benchmarked weight
joepetrowski Nov 15, 2023
f5df158
Merge remote-tracking branch 'origin' into joe-identity-deposit-migra…
joepetrowski Nov 15, 2023
bf42286
new xcm errors
joepetrowski Nov 15, 2023
73426e7
apply basti review
joepetrowski Nov 15, 2023
f18914f
Merge branch 'master' into joe-identity-deposit-migration
joepetrowski Nov 15, 2023
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
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions polkadot/runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sp-npos-elections = { path = "../../../substrate/primitives/npos-elections", def
pallet-authorship = { path = "../../../substrate/frame/authorship", default-features = false }
pallet-balances = { path = "../../../substrate/frame/balances", default-features = false }
pallet-fast-unstake = { path = "../../../substrate/frame/fast-unstake", default-features = false }
pallet-identity = { path = "../../../substrate/frame/identity", default-features = false }
pallet-session = { path = "../../../substrate/frame/session", default-features = false }
frame-support = { path = "../../../substrate/frame/support", default-features = false }
pallet-staking = { path = "../../../substrate/frame/staking", default-features = false }
Expand Down Expand Up @@ -85,6 +86,7 @@ std = [
"pallet-balances/std",
"pallet-election-provider-multi-phase/std",
"pallet-fast-unstake/std",
"pallet-identity/std",
"pallet-session/std",
"pallet-staking-reward-fn/std",
"pallet-staking/std",
Expand Down Expand Up @@ -124,6 +126,7 @@ runtime-benchmarks = [
"pallet-balances/runtime-benchmarks",
"pallet-election-provider-multi-phase/runtime-benchmarks",
"pallet-fast-unstake/runtime-benchmarks",
"pallet-identity/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
Expand All @@ -147,6 +150,7 @@ try-runtime = [
"pallet-balances/try-runtime",
"pallet-election-provider-multi-phase/try-runtime",
"pallet-fast-unstake/try-runtime",
"pallet-identity/try-runtime",
"pallet-session/try-runtime",
"pallet-staking/try-runtime",
"pallet-timestamp/try-runtime",
Expand Down
305 changes: 305 additions & 0 deletions polkadot/runtime/common/src/identity_migrator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
// Copyright (C) Parity Technologies (UK) Ltd.
Copy link
Contributor

@xlc xlc Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we stop adding more code to polkadot-runtime-common #2186

at least until some refactor is done to ensure this is only used by relay/system chains. I don't want my build to include this pallet for no reason.

// 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.

//! This pallet is designed to go into a source chain and destination chain to migrate data. The
//! design motivations are:
//!
//! - Call some function on the source chain that executes some migration (clearing state,
//! forwarding an XCM program).
//! - Call some function (probably from an XCM program) on the destination chain.
//! - Avoid cluttering the source pallet with new dispatchables that are unrelated to its
//! functionality and only used for migration.
//!
//! After the migration is complete, the pallet may be removed from both chains' runtimes as well as
//! the `polkadot-runtime-common` crate.

use frame_support::{dispatch::DispatchResult, traits::Currency, weights::Weight};
pub use pallet::*;
use pallet_identity;
use sp_core::Get;

#[cfg(feature = "runtime-benchmarks")]
use frame_benchmarking::{account, impl_benchmark_test_suite, v2::*, BenchmarkError};

pub trait WeightInfo {
fn reap_identity(r: u32, s: u32) -> Weight;
fn poke_deposit() -> Weight;
}

impl WeightInfo for () {
fn reap_identity(_r: u32, _s: u32) -> Weight {
Weight::MAX
}
fn poke_deposit() -> Weight {
Weight::MAX
}
}

pub struct TestWeightInfo;
impl WeightInfo for TestWeightInfo {
fn reap_identity(_r: u32, _s: u32) -> Weight {
Weight::zero()
}
fn poke_deposit() -> Weight {
Weight::zero()
}
}

// Must use the same `Balance` as `T`'s Identity pallet to handle deposits.
type BalanceOf<T> = <<T as pallet_identity::Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::Balance;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{
dispatch::{DispatchResultWithPostInfo, PostDispatchInfo},
pallet_prelude::*,
traits::EnsureOrigin,
};
use frame_system::pallet_prelude::*;

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

#[pallet::config]
pub trait Config: frame_system::Config + pallet_identity::Config {
/// Overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

/// The origin that can reap identities. Expected to be `EnsureSigned<AccountId>` on the
/// source chain such that anyone can all this function.
type Reaper: EnsureOrigin<Self::RuntimeOrigin>;

/// A handler for what to do when an identity is reaped.
type ReapIdentityHandler: OnReapIdentity<Self::AccountId>;

/// Weight information for the extrinsics in the pallet.
type WeightInfo: WeightInfo;
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// The identity and all sub accounts were reaped for `who`.
IdentityReaped { who: T::AccountId },
/// The deposits held for `who` were updated. `identity` is the new deposit held for
/// identity info, and `subs` is the new deposit held for the sub-accounts.
DepositUpdated { who: T::AccountId, identity: BalanceOf<T>, subs: BalanceOf<T> },
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Reap the `IdentityInfo` of `who` from the Identity pallet of `T`, unreserving any
/// deposits held and removing storage items associated with `who`.
#[pallet::call_index(0)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::reap_identity(
T::MaxRegistrars::get(),
T::MaxSubAccounts::get()
))]
pub fn reap_identity(
origin: OriginFor<T>,
who: T::AccountId,
) -> DispatchResultWithPostInfo {
T::Reaper::ensure_origin(origin)?;
// - number of registrars (required to calculate weight)
// - byte size of `IdentityInfo` (required to calculate remote deposit)
// - number of sub accounts (required to calculate both weight and remote deposit)
let (registrars, bytes, subs) = pallet_identity::Pallet::<T>::reap_identity(&who)?;
T::ReapIdentityHandler::on_reap_identity(&who, bytes, subs)?;
Self::deposit_event(Event::IdentityReaped { who });
let post = PostDispatchInfo {
actual_weight: Some(<T as pallet::Config>::WeightInfo::reap_identity(
registrars, subs,
)),
pays_fee: Pays::No,
};
Ok(post)
}

/// Update the deposit of `who`. Meant to be called by the system with an XCM `Transact`
/// Instruction.
#[pallet::call_index(1)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::poke_deposit())]
pub fn poke_deposit(origin: OriginFor<T>, who: T::AccountId) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
let (id_deposit, subs_deposit) = pallet_identity::Pallet::<T>::poke_deposit(&who)?;
Self::deposit_event(Event::DepositUpdated {
who,
identity: id_deposit,
subs: subs_deposit,
});
Ok(Pays::No.into())
}
}
}

/// Trait to handle reaping identity from state.
pub trait OnReapIdentity<AccountId> {
/// What to do when an identity is reaped. For example, the implementation could send an XCM
/// program to another chain. Concretely, a type implementing this trait in the Polkadot
/// runtime would teleport enough DOT to the People Chain to cover the Identity deposit there.
///
/// This could also directly include `Transact { poke_deposit(..), ..}`.
///
/// Inputs
/// - `who`: Whose identity was reaped.
/// - `bytes`: The byte size of `IdentityInfo`.
/// - `subs`: The number of sub-accounts they had.
fn on_reap_identity(who: &AccountId, bytes: u32, subs: u32) -> DispatchResult;
}

impl<AccountId> OnReapIdentity<AccountId> for () {
fn on_reap_identity(_who: &AccountId, _bytes: u32, _subs: u32) -> DispatchResult {
Ok(())
}
}

#[cfg(feature = "runtime-benchmarks")]
#[benchmarks]
mod benchmarks {
use super::*;
use frame_support::traits::EnsureOrigin;
use frame_system::RawOrigin;
use pallet_identity::{Data, IdentityInformationProvider, Judgement, Pallet as Identity};
use parity_scale_codec::Encode;
use sp_runtime::{
traits::{Bounded, Hash, StaticLookup},
Saturating,
};
use sp_std::{boxed::Box, vec::Vec, *};

const SEED: u32 = 0;

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::RuntimeEvent = generic_event.into();
let frame_system::EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}

#[benchmark]
fn reap_identity(
r: Linear<0, { T::MaxRegistrars::get() }>,
s: Linear<0, { T::MaxSubAccounts::get() }>,
) -> Result<(), BenchmarkError> {
// set up target
let target: T::AccountId = account("target", 0, SEED);
let target_origin =
<T as frame_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(target.clone()));
let target_lookup = T::Lookup::unlookup(target.clone());
let _ = T::Currency::make_free_balance_be(&target, BalanceOf::<T>::max_value());

// set identity
let info = <T as pallet_identity::Config>::IdentityInformation::create_identity_info();
Identity::<T>::set_identity(
RawOrigin::Signed(target.clone()).into(),
Box::new(info.clone()),
)?;

// create and set subs
let mut subs = Vec::new();
let data = Data::Raw(vec![0; 32].try_into().unwrap());
for ii in 0..s {
let sub_account = account("sub", ii, SEED);
subs.push((sub_account, data.clone()));
}
Identity::<T>::set_subs(target_origin.clone(), subs.clone())?;

// add registrars and provide judgements
let registrar_origin = T::RegistrarOrigin::try_successful_origin()
.expect("RegistrarOrigin has no successful origin required for the benchmark");
for ii in 0..r {
// registrar account
let registrar: T::AccountId = account("registrar", ii, SEED);
let registrar_lookup = T::Lookup::unlookup(registrar.clone());
let _ = <T as pallet_identity::Config>::Currency::make_free_balance_be(
&registrar,
<T as pallet_identity::Config>::Currency::minimum_balance(),
);

// add registrar
Identity::<T>::add_registrar(registrar_origin.clone(), registrar_lookup)?;
Identity::<T>::set_fee(RawOrigin::Signed(registrar.clone()).into(), ii, 10u32.into())?;
let fields = <T as pallet_identity::Config>::IdentityInformation::all_fields();
Identity::<T>::set_fields(RawOrigin::Signed(registrar.clone()).into(), ii, fields)?;

// request and provide judgement
Identity::<T>::request_judgement(target_origin.clone(), ii, 10u32.into())?;
Identity::<T>::provide_judgement(
RawOrigin::Signed(registrar).into(),
ii,
target_lookup.clone(),
Judgement::Reasonable,
<T as frame_system::Config>::Hashing::hash_of(&info),
)?;
}

let origin = T::Reaper::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

#[extrinsic_call]
_(origin as T::RuntimeOrigin, target.clone());

assert_last_event::<T>(Event::<T>::IdentityReaped { who: target.clone() }.into());

let fields = <T as pallet_identity::Config>::IdentityInformation::all_fields();
assert!(!Identity::<T>::has_identity(&target, fields));
assert_eq!(Identity::<T>::subs(&target).len(), 0);

Ok(())
}

#[benchmark]
fn poke_deposit() -> Result<(), BenchmarkError> {
let target: T::AccountId = account("target", 0, SEED);
let _ = T::Currency::make_free_balance_be(&target, BalanceOf::<T>::max_value());
let info = <T as pallet_identity::Config>::IdentityInformation::create_identity_info();

let _ = Identity::<T>::set_identity_no_deposit(&target, info.clone());

let sub_account: T::AccountId = account("sub", 0, SEED);
let _ = Identity::<T>::set_sub_no_deposit(&target, sub_account.clone());

// expected deposits
let expected_id_deposit = <T as pallet_identity::Config>::BasicDeposit::get()
.saturating_add(
<T as pallet_identity::Config>::ByteDeposit::get()
.saturating_mul(<BalanceOf<T>>::from(info.encoded_size() as u32)),
);
// only 1 sub
let expected_sub_deposit = <T as pallet_identity::Config>::SubAccountDeposit::get();

#[extrinsic_call]
_(RawOrigin::Root, target.clone());

assert_last_event::<T>(
Event::<T>::DepositUpdated {
who: target,
identity: expected_id_deposit,
subs: expected_sub_deposit,
}
.into(),
);

Ok(())
}

impl_benchmark_test_suite!(
Pallet,
crate::integration_tests::new_test_ext(),
crate::integration_tests::Test,
);
}
Loading