Skip to content

Commit

Permalink
Keep identity judgments (#210)
Browse files Browse the repository at this point in the history
* Keep identity judgments

* Doc
  • Loading branch information
AurevoirXavier authored Jan 17, 2023
1 parent ed7da51 commit cadfb76
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 105 deletions.
65 changes: 26 additions & 39 deletions pallet/account-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -144,31 +143,22 @@ pub mod pallet {
#[pallet::getter(fn deposit_of)]
pub type Deposits<T: Config> = StorageMap<_, Blake2_128Concat, AccountId32, Vec<Deposit>>;

/// [`darwinia_staking::Ledgers`] data.
#[pallet::storage]
#[pallet::getter(fn ledger_of)]
pub type Ledgers<T: Config> = StorageMap<_, Blake2_128Concat, AccountId32, Ledger<T>>;

/// [`pallet_identity::IdentityOf`] data.
///
/// <https://github.com/paritytech/substrate/blob/polkadot-v0.9.30/frame/identity/src/lib.rs#L163>
#[pallet::storage]
#[pallet::getter(fn identity_of)]
pub type IdentityOf<T: Config> = StorageMap<
pub type Identities<T: Config> = StorageMap<
_,
Twox64Concat,
AccountId32,
Registration<Balance, ConstU32<100>, ConstU32<100>>,
>;

/// [`pallet_identity::Registrars`] data.
///
/// <https://github.com/paritytech/substrate/blob/polkadot-v0.9.30/frame/identity/src/lib.rs#L199>
/// [`darwinia_staking::Ledgers`] data.
#[pallet::storage]
#[pallet::unbounded]
#[pallet::getter(fn registrars)]
pub type Registrars<T: Config> =
StorageValue<_, Vec<Option<RegistrarInfo<Balance, AccountId32>>>, ValueQuery>;
#[pallet::getter(fn ledger_of)]
pub type Ledgers<T: Config> = StorageMap<_, Blake2_128Concat, AccountId32, Ledger<T>>;

#[pallet::call]
impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -213,6 +203,27 @@ pub mod pallet {
// https://github.dev/paritytech/substrate/blob/19162e43be45817b44c7d48e50d03f074f60fbf4/frame/vesting/src/lib.rs#L86
<pallet_balances::Pallet<T>>::set_lock(*b"vesting ", &to, locked, reasons);
}
if let Some(i) = <Identities<T>>::take(&from) {
migration::put_storage_value(
b"Identity",
b"IdentityOf",
&Twox64Concat::hash(&to.encode()),
i,
);
}
{
let mut rs = <pallet_identity::Pallet<T>>::registrars();

for r in rs.iter_mut().flatten() {
if r.account.0 == <AccountId32 as AsRef<[u8; 32]>>::as_ref(&from)[..20] {
r.account = to;

break;
}
}

migration::put_storage_value(b"Identity", b"Registrars", &[], rs);
}
if let Some(l) = <Ledgers<T>>::take(&from) {
if let Some(ds) = <Deposits<T>>::take(&from) {
<pallet_balances::Pallet<T> as Currency<_>>::transfer(
Expand Down Expand Up @@ -250,30 +261,6 @@ pub mod pallet {
<darwinia_staking::Ledgers<T>>::insert(to, l);
}

if let Some(identity) = IdentityOf::<T>::take(from.clone()) {
put_storage_value(
b"Identity",
b"IdentityOf",
&Twox64Concat::hash(&to.encode()),
identity,
);
}
let mut chain_rs = <pallet_identity::Pallet<T>>::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::<T>::mutate(|rs| rs.remove(i));
}
}
}
put_storage_value(b"Identity", b"Registrars", &[], chain_rs);

Self::deposit_event(Event::Migrated { from, to });

Ok(())
Expand Down
29 changes: 18 additions & 11 deletions runtime/common/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ macro_rules! impl_account_migration_tests {
image: Data::None,
twitter: Data::None,
};
<darwinia_account_migration::IdentityOf<Runtime>>::insert(
<darwinia_account_migration::Identities<Runtime>>::insert(
from_pk,
Registration {
judgements: Default::default(),
Expand All @@ -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(&<AccountId32 as AsRef<[u8; 32]>>::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::<Balance, AccountId> {
account: truncated_from.into(),
fee: RING_AMOUNT,
fields: IdentityFields::default(),
};
<darwinia_account_migration::Registrars<Runtime>>::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());
});
}
}
Expand Down
4 changes: 1 addition & 3 deletions tool/state-processor/src/adjust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
11 changes: 0 additions & 11 deletions tool/state-processor/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
11 changes: 6 additions & 5 deletions tool/state-processor/src/identity/README.md
Original file line number Diff line number Diff line change
@@ -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
41 changes: 25 additions & 16 deletions tool/state-processor/src/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ impl<S> Processor<S> {
/// Only care about the solo chain, since parachains don't have identity now.
pub fn process_identity(&mut self) -> &mut Self {
let mut identities = <Map<Registration>>::default();
let mut registrars = Vec::<Option<RegistrarInfo>>::default();
let mut registrars = Vec::<Option<RegistrarInfo<[u8; 32]>>>::default();
let mut subs_of = Map::<(u128, Vec<[u8; 32]>)>::default();

log::info!("take `Identity::IdentityOf`, `Identity::Registrars`, `Identity::SuperOf` and `Identity::SuperOf`");
Expand All @@ -14,35 +14,44 @@ impl<S> Processor<S> {
.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();

self.shell_state
.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");

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::<Vec<_>>();

log::info!("set `Identity::Registrars`");
self.shell_state.insert_value(b"Identity", b"Registrars", "", registrars);

self
}
Expand Down
31 changes: 13 additions & 18 deletions tool/state-processor/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,28 +736,23 @@ fn identities_adjust() {
}

#[test]
fn registrars_adust() {
fn registrars_adjust() {
run_test(|tester| {
let mut registrars: Vec<Option<RegistrarInfo>> = Vec::new();
tester.solo_state.get_value(b"Identity", b"Registrars", "", &mut registrars);
assert!(registrars.len() > 0);
let mut rs: Vec<Option<RegistrarInfo<[u8; 32]>>> = Vec::new();
tester.solo_state.get_value(b"Identity", b"Registrars", "", &mut rs);
assert!(!rs.is_empty());

// after migrated
let mut migrated_registrars: Vec<Option<RegistrarInfo>> = 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<Option<RegistrarInfo<[u8; 20]>>> = 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!"),
});
});
Expand Down
4 changes: 2 additions & 2 deletions tool/state-processor/src/type_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<A> {
pub account: A,
pub fee: u128,
pub fields: IdentityFields,
}
Expand Down

0 comments on commit cadfb76

Please sign in to comment.