From cadfb76573a007dad29cd05dd35dea27322ddf0d Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 17 Jan 2023 11:31:39 +0800 Subject: [PATCH] Keep identity judgments (#210) * Keep identity judgments * Doc --- pallet/account-migration/src/lib.rs | 65 +++++++++------------ runtime/common/src/test.rs | 29 +++++---- tool/state-processor/src/adjust.rs | 4 +- tool/state-processor/src/configuration.rs | 11 ---- tool/state-processor/src/identity/README.md | 11 ++-- tool/state-processor/src/identity/mod.rs | 41 ++++++++----- tool/state-processor/src/tests.rs | 31 +++++----- tool/state-processor/src/type_registry.rs | 4 +- 8 files changed, 91 insertions(+), 105 deletions(-) diff --git a/pallet/account-migration/src/lib.rs b/pallet/account-migration/src/lib.rs index dd1318227..2c2914485 100644 --- a/pallet/account-migration/src/lib.rs +++ b/pallet/account-migration/src/lib.rs @@ -53,14 +53,13 @@ use dc_primitives::{AccountId as AccountId20, AssetId, Balance, BlockNumber, Ind // substrate use frame_support::{ log, migration, - migration::put_storage_value, pallet_prelude::*, traits::{Currency, ExistenceRequirement::KeepAlive, LockableCurrency, WithdrawReasons}, StorageHasher, }; use frame_system::{pallet_prelude::*, AccountInfo, RawOrigin}; use pallet_balances::AccountData; -use pallet_identity::{RegistrarInfo, Registration}; +use pallet_identity::Registration; use pallet_vesting::VestingInfo; use sp_core::sr25519::{Public, Signature}; use sp_runtime::{ @@ -144,31 +143,22 @@ pub mod pallet { #[pallet::getter(fn deposit_of)] pub type Deposits = StorageMap<_, Blake2_128Concat, AccountId32, Vec>; - /// [`darwinia_staking::Ledgers`] data. - #[pallet::storage] - #[pallet::getter(fn ledger_of)] - pub type Ledgers = StorageMap<_, Blake2_128Concat, AccountId32, Ledger>; - /// [`pallet_identity::IdentityOf`] data. /// /// #[pallet::storage] #[pallet::getter(fn identity_of)] - pub type IdentityOf = StorageMap< + pub type Identities = StorageMap< _, Twox64Concat, AccountId32, Registration, ConstU32<100>>, >; - /// [`pallet_identity::Registrars`] data. - /// - /// + /// [`darwinia_staking::Ledgers`] data. #[pallet::storage] - #[pallet::unbounded] - #[pallet::getter(fn registrars)] - pub type Registrars = - StorageValue<_, Vec>>, ValueQuery>; + #[pallet::getter(fn ledger_of)] + pub type Ledgers = StorageMap<_, Blake2_128Concat, AccountId32, Ledger>; #[pallet::call] impl Pallet { @@ -213,6 +203,27 @@ pub mod pallet { // https://github.dev/paritytech/substrate/blob/19162e43be45817b44c7d48e50d03f074f60fbf4/frame/vesting/src/lib.rs#L86 >::set_lock(*b"vesting ", &to, locked, reasons); } + if let Some(i) = >::take(&from) { + migration::put_storage_value( + b"Identity", + b"IdentityOf", + &Twox64Concat::hash(&to.encode()), + i, + ); + } + { + let mut rs = >::registrars(); + + for r in rs.iter_mut().flatten() { + if r.account.0 == >::as_ref(&from)[..20] { + r.account = to; + + break; + } + } + + migration::put_storage_value(b"Identity", b"Registrars", &[], rs); + } if let Some(l) = >::take(&from) { if let Some(ds) = >::take(&from) { as Currency<_>>::transfer( @@ -250,30 +261,6 @@ pub mod pallet { >::insert(to, l); } - if let Some(identity) = IdentityOf::::take(from.clone()) { - put_storage_value( - b"Identity", - b"IdentityOf", - &Twox64Concat::hash(&to.encode()), - identity, - ); - } - let mut chain_rs = >::registrars().into_inner(); - for (i, rs) in Self::registrars().iter().enumerate() { - if let Some(rs) = rs { - if rs.account == from { - chain_rs.push(Some(RegistrarInfo { - account: to, - fee: rs.fee, - fields: rs.fields, - })); - - Registrars::::mutate(|rs| rs.remove(i)); - } - } - } - put_storage_value(b"Identity", b"Registrars", &[], chain_rs); - Self::deposit_event(Event::Migrated { from, to }); Ok(()) diff --git a/runtime/common/src/test.rs b/runtime/common/src/test.rs index f28e8a9c4..9ff2e948a 100644 --- a/runtime/common/src/test.rs +++ b/runtime/common/src/test.rs @@ -325,7 +325,7 @@ macro_rules! impl_account_migration_tests { image: Data::None, twitter: Data::None, }; - >::insert( + >::insert( from_pk, Registration { judgements: Default::default(), @@ -344,25 +344,32 @@ macro_rules! impl_account_migration_tests { #[test] fn registrars_should_work() { let (from, from_pk) = alice(); + let mut truncated_from = [0_u8; 20]; + + truncated_from.copy_from_slice(&>::as_ref(&from_pk)[..20]); + let to = H160::from_low_u64_be(255).into(); ExtBuilder::default().build().execute_with(|| { preset_state_of(&from); - let info = RegistrarInfo { - account: from_pk, + let info = RegistrarInfo:: { + account: truncated_from.into(), fee: RING_AMOUNT, fields: IdentityFields::default(), }; - >::put(vec![ - Some(info.clone()), - None, - ]); - assert_ok!(migrate(from, to,)); - assert!(!AccountMigration::registrars().contains(&Some(info.clone()))); - assert_eq!(Identity::registrars()[0].clone().unwrap().account, to); - assert_eq!(Identity::registrars()[0].clone().unwrap().fee, info.fee); + migration::put_storage_value( + b"Identity", + b"Registrars", + &[], + vec![Some(info.clone()), None], + ); + + assert_ok!(migrate(from, to)); + assert_eq!(Identity::registrars()[0].as_ref().unwrap().account, to); + assert_eq!(Identity::registrars()[0].as_ref().unwrap().fee, info.fee); + assert!(Identity::registrars()[1].is_none()); }); } } diff --git a/tool/state-processor/src/adjust.rs b/tool/state-processor/src/adjust.rs index 99d00b844..6943edff2 100644 --- a/tool/state-processor/src/adjust.rs +++ b/tool/state-processor/src/adjust.rs @@ -72,13 +72,11 @@ impl Adjust for Unbonding { impl Adjust for Registration { fn adjust(&mut self) { - // Reset the judgements - self.judgements.clear(); self.deposit.adjust(); } } -impl Adjust for RegistrarInfo { +impl Adjust for RegistrarInfo<[u8; 32]> { fn adjust(&mut self) { self.fee.adjust(); } diff --git a/tool/state-processor/src/configuration.rs b/tool/state-processor/src/configuration.rs index f02e49d52..d8438691e 100644 --- a/tool/state-processor/src/configuration.rs +++ b/tool/state-processor/src/configuration.rs @@ -5,33 +5,22 @@ pub const ROOT: [u8; 20] = [0x72, 0x6f, 0x6f, 0x74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 pub trait Configurable { const NAME: &'static str; - - const KTON_NAME: &'static [u8]; - const KTON_SYMBOL: &'static [u8]; } impl Configurable for () { - const KTON_NAME: &'static [u8] = b""; - const KTON_SYMBOL: &'static [u8] = b""; const NAME: &'static str = ""; } pub struct Darwinia; impl Configurable for Darwinia { - const KTON_NAME: &'static [u8] = b"Darwinia Commitment Token"; - const KTON_SYMBOL: &'static [u8] = b"KTON"; const NAME: &'static str = "darwinia"; } pub struct Crab; impl Configurable for Crab { - const KTON_NAME: &'static [u8] = b"Crab Commitment Token"; - const KTON_SYMBOL: &'static [u8] = b"CKTON"; const NAME: &'static str = "crab"; } pub struct Pangolin; impl Configurable for Pangolin { - const KTON_NAME: &'static [u8] = b"Pangolin Commitment Token"; - const KTON_SYMBOL: &'static [u8] = b"PKTON"; const NAME: &'static str = "pangolin"; } diff --git a/tool/state-processor/src/identity/README.md b/tool/state-processor/src/identity/README.md index b4c261656..3d3428f7c 100644 --- a/tool/state-processor/src/identity/README.md +++ b/tool/state-processor/src/identity/README.md @@ -1,6 +1,7 @@ ### Process steps -1. take `Identity::IdentityOf`, `Identity::Registrars`, `Identity::SuperOf` and `Identity::SuperOf`. -2. update identities's deposit decimal and reset judgements. -3. update registrars fee decimal. -4. update super_id's reserved balance. -5. set `AccountMigration::IdentityOf` and`AccountMigration::Registrars`. +1. take `Identity::IdentityOf`, `Identity::Registrars`, `Identity::SuperOf` and `Identity::SuperOf` +2. free super_id's reservation +3. adjust identities' deposit and judgement decimal +4. set `AccountMigration::IdentityOf` +5. truncate registrar account id and adjust registrars fee decimal +6. set `Identity::Registrars diff --git a/tool/state-processor/src/identity/mod.rs b/tool/state-processor/src/identity/mod.rs index fe39087e3..eb773e382 100644 --- a/tool/state-processor/src/identity/mod.rs +++ b/tool/state-processor/src/identity/mod.rs @@ -5,7 +5,7 @@ impl Processor { /// Only care about the solo chain, since parachains don't have identity now. pub fn process_identity(&mut self) -> &mut Self { let mut identities = >::default(); - let mut registrars = Vec::>::default(); + let mut registrars = Vec::>>::default(); let mut subs_of = Map::<(u128, Vec<[u8; 32]>)>::default(); log::info!("take `Identity::IdentityOf`, `Identity::Registrars`, `Identity::SuperOf` and `Identity::SuperOf`"); @@ -14,19 +14,7 @@ impl Processor { .take_value(b"Identity", b"Registrars", "", &mut registrars) .take_map(b"Identity", b"SubsOf", &mut subs_of, get_last_64_key); - log::info!("update identities's deposit and judgement decimal"); - identities.iter_mut().for_each(|(_k, v)| { - v.adjust(); - }); - - log::info!("update registrars fee decimal"); - registrars.iter_mut().for_each(|o| { - if let Some(r) = o { - r.adjust() - } - }); - - log::info!("update super_id's reserved balance"); + log::info!("free super_id's reservation"); subs_of.into_iter().for_each(|(super_id, (mut subs_deposit, _))| { subs_deposit.adjust(); @@ -34,6 +22,9 @@ impl Processor { .unreserve(array_bytes::hex2array_unchecked::<_, 32>(super_id), subs_deposit); }); + log::info!("adjust identities' deposit and judgement decimal"); + identities.iter_mut().for_each(|(_, v)| v.adjust()); + log::info!("set `AccountMigration::IdentityOf`"); { let ik = item_key(b"AccountMigration", b"IdentityOf"); @@ -41,8 +32,26 @@ impl Processor { self.shell_state.insert_map(identities, |h| format!("{ik}{h}")); } - log::info!("set `AccountMigration::Registrars`"); - self.shell_state.insert_value(b"AccountMigration", b"Registrars", "", registrars); + log::info!("truncate registrar account id and adjust registrars fee decimal"); + let registrars = registrars + .into_iter() + .map(|o| { + if let Some(mut r) = o { + r.adjust(); + + let mut account = [0_u8; 20]; + + account.copy_from_slice(&r.account[..20]); + + Some(RegistrarInfo { account, fee: r.fee, fields: r.fields }) + } else { + None + } + }) + .collect::>(); + + log::info!("set `Identity::Registrars`"); + self.shell_state.insert_value(b"Identity", b"Registrars", "", registrars); self } diff --git a/tool/state-processor/src/tests.rs b/tool/state-processor/src/tests.rs index 2471ee61b..238d0184d 100644 --- a/tool/state-processor/src/tests.rs +++ b/tool/state-processor/src/tests.rs @@ -736,28 +736,23 @@ fn identities_adjust() { } #[test] -fn registrars_adust() { +fn registrars_adjust() { run_test(|tester| { - let mut registrars: Vec> = Vec::new(); - tester.solo_state.get_value(b"Identity", b"Registrars", "", &mut registrars); - assert!(registrars.len() > 0); + let mut rs: Vec>> = Vec::new(); + tester.solo_state.get_value(b"Identity", b"Registrars", "", &mut rs); + assert!(!rs.is_empty()); // after migrated - let mut migrated_registrars: Vec> = Vec::new(); - tester.solo_state.get_value( - b"AccountMigration", - b"Registrars", - "", - &mut migrated_registrars, - ); - - registrars.iter().zip(migrated_registrars.iter()).for_each(|(a, b)| match (a, b) { - (Some(a), Some(b)) => { - assert_eq!(a.account, b.account); - assert_eq!(a.fee * GWEI, b.fee); - assert_eq!(a.fields, b.fields); + let mut migrated_rs: Vec>> = Vec::new(); + tester.shell_state.get_value(b"Identity", b"Registrars", "", &mut migrated_rs); + + rs.iter().zip(migrated_rs.iter()).for_each(|(r, m_r)| match (r, m_r) { + (Some(r), Some(m_r)) => { + assert_eq!(r.account, m_r.account[..20]); + assert_eq!(r.fee * GWEI, m_r.fee); + assert_eq!(r.fields, m_r.fields); }, - (None, None) => {}, + (None, None) => (), _ => panic!("this should never happen!"), }); }); diff --git a/tool/state-processor/src/type_registry.rs b/tool/state-processor/src/type_registry.rs index e18cdf411..c5b793013 100644 --- a/tool/state-processor/src/type_registry.rs +++ b/tool/state-processor/src/type_registry.rs @@ -285,8 +285,8 @@ impl<'a, T: Input> Input for AppendZerosInput<'a, T> { } #[derive(Debug, Encode, Decode, PartialEq, Eq)] -pub struct RegistrarInfo { - pub account: [u8; 32], +pub struct RegistrarInfo { + pub account: A, pub fee: u128, pub fields: IdentityFields, }