Skip to content

Commit

Permalink
pallet-identity state process (#124)
Browse files Browse the repository at this point in the history
* Add types folder

* Read storage out

* Decimal update

* Add remove subsOf and superOf

* Remove useless file

* Add README

* Process in runtime side

* Format

* Add SUDO back

* Fix doc link

* Identity migrate

* Fix runtime

* Add tests

* Add tests

* Code clean

* Remove sp-runtime

* Code format

* Delete useless reserve

* Reset the judgements

* Self review

* Fix

* Add identities runtime tests

* Fix tests

* Just format

* Tiny updates

* Update doc

Co-authored-by: Xavier Lau <xavier@inv.cafe>
  • Loading branch information
boundless-forest and AurevoirXavier authored Jan 16, 2023
1 parent a32342f commit ed7da51
Show file tree
Hide file tree
Showing 14 changed files with 492 additions and 14 deletions.
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.

2 changes: 2 additions & 0 deletions pallet/account-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ frame-support = { default-features = false, git = "https://github.com/parityte
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
pallet-assets = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
pallet-identity = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
pallet-vesting = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
Expand All @@ -49,6 +50,7 @@ std = [
"frame-system/std",
"pallet-assets/std",
"pallet-balances/std",
"pallet-identity/std",
"pallet-vesting/std",
"sp-core/std",
"sp-runtime/std",
Expand Down
48 changes: 47 additions & 1 deletion pallet/account-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ 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_vesting::VestingInfo;
use sp_core::sr25519::{Public, Signature};
use sp_runtime::{
Expand Down Expand Up @@ -87,6 +89,7 @@ pub mod pallet {
> + pallet_assets::Config<Balance = Balance, AssetId = AssetId>
+ pallet_balances::Config<Balance = Balance>
+ pallet_vesting::Config<Currency = pallet_balances::Pallet<Self>>
+ pallet_identity::Config<Currency = pallet_balances::Pallet<Self>>
+ darwinia_deposit::Config
+ darwinia_staking::Config
{
Expand Down Expand Up @@ -146,7 +149,26 @@ pub mod pallet {
#[pallet::getter(fn ledger_of)]
pub type Ledgers<T: Config> = StorageMap<_, Blake2_128Concat, AccountId32, Ledger<T>>;

// TODO: identity storages
/// [`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<
_,
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>
#[pallet::storage]
#[pallet::unbounded]
#[pallet::getter(fn registrars)]
pub type Registrars<T: Config> =
StorageValue<_, Vec<Option<RegistrarInfo<Balance, AccountId32>>>, ValueQuery>;

#[pallet::call]
impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -228,6 +250,30 @@ 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
63 changes: 63 additions & 0 deletions runtime/common/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ macro_rules! impl_account_migration_tests {
use frame_system::AccountInfo;
use pallet_assets::ExistenceReason;
use pallet_balances::AccountData;
use pallet_identity::{
Data, IdentityFields, IdentityInfo, RegistrarInfo, Registration,
};
use sp_core::{sr25519::Pair, Encode, Pair as PairT, H160};
use sp_keyring::sr25519::Keyring;
use sp_runtime::{
Expand Down Expand Up @@ -302,6 +305,66 @@ macro_rules! impl_account_migration_tests {
assert_eq!(Staking::ledger_of(to).unwrap().staked_kton, 20);
});
}

#[test]
fn identities_should_work() {
let (from, from_pk) = alice();
let to = H160::from_low_u64_be(255).into();

ExtBuilder::default().build().execute_with(|| {
preset_state_of(&from);

let info = IdentityInfo {
additional: Default::default(),
display: Data::Sha256([1u8; 32]),
legal: Data::None,
web: Data::None,
riot: Data::None,
email: Data::None,
pgp_fingerprint: None,
image: Data::None,
twitter: Data::None,
};
<darwinia_account_migration::IdentityOf<Runtime>>::insert(
from_pk,
Registration {
judgements: Default::default(),
deposit: RING_AMOUNT,
info: info.clone(),
},
);

assert_ok!(migrate(from, to,));
assert_eq!(Identity::identity(to).unwrap().info, info);
assert_eq!(Identity::identity(to).unwrap().deposit, RING_AMOUNT);
assert_eq!(Identity::identity(to).unwrap().judgements.len(), 0);
});
}

#[test]
fn registrars_should_work() {
let (from, from_pk) = alice();
let to = H160::from_low_u64_be(255).into();

ExtBuilder::default().build().execute_with(|| {
preset_state_of(&from);

let info = RegistrarInfo {
account: from_pk,
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);
});
}
}
};
}
27 changes: 24 additions & 3 deletions tool/state-processor/Cargo.lock

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

3 changes: 2 additions & 1 deletion tool/state-processor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ version = "0.0.0"
# crates.io
anyhow = { version = "1.0" }
array-bytes = { version = "6.0" }
enumflags2 = { version = "0.7" }
fxhash = { version = "0.2" }
log = { version = "0.4" }
once_cell = { version = "1.16" }
parity-scale-codec = { version = "3.2", features = ["derive"] }
pretty_env_logger = { version = "0.4" }
primitive-types = { version = "0.12.1", features = ["codec"] }
primitive-types = { version = "0.12", features = ["codec"] }
serde = { version = "1.0" }
serde_json = { version = "1.0" }
tar = { version = "0.4" }
Expand Down
14 changes: 14 additions & 0 deletions tool/state-processor/src/adjust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,17 @@ impl Adjust for Unbonding {
self.until.adjust();
}
}

impl Adjust for Registration {
fn adjust(&mut self) {
// Reset the judgements
self.judgements.clear();
self.deposit.adjust();
}
}

impl Adjust for RegistrarInfo {
fn adjust(&mut self) {
self.fee.adjust();
}
}
6 changes: 6 additions & 0 deletions tool/state-processor/src/identity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### 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`.
49 changes: 49 additions & 0 deletions tool/state-processor/src/identity/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// darwinia
use crate::*;

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 subs_of = Map::<(u128, Vec<[u8; 32]>)>::default();

log::info!("take `Identity::IdentityOf`, `Identity::Registrars`, `Identity::SuperOf` and `Identity::SuperOf`");
self.solo_state
.take_map(b"Identity", b"IdentityOf", &mut identities, get_hashed_key)
.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");
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!("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);

self
}
}
1 change: 1 addition & 0 deletions tool/state-processor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use type_registry::*;
// Runtime pallets.
mod balances;
mod evm;
mod identity;
mod indices;
mod proxy;
mod staking;
Expand Down
1 change: 1 addition & 0 deletions tool/state-processor/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ where

self.process_system()
.process_indices()
.process_identity()
.process_vesting()
.process_proxy()
.process_staking()
Expand Down
Loading

0 comments on commit ed7da51

Please sign in to comment.