feat: Rebrand Polkadot/Substrate references to PezkuwiChain

This commit systematically rebrands various references from Parity Technologies'
Polkadot/Substrate ecosystem to PezkuwiChain within the kurdistan-sdk.

Key changes include:
- Updated external repository URLs (zombienet-sdk, parity-db, parity-scale-codec, wasm-instrument) to point to pezkuwichain forks.
- Modified internal documentation and code comments to reflect PezkuwiChain naming and structure.
- Replaced direct references to  with  or specific paths within the  for XCM, Pezkuwi, and other modules.
- Cleaned up deprecated  issue and PR references in various  and  files, particularly in  and  modules.
- Adjusted image and logo URLs in documentation to point to PezkuwiChain assets.
- Removed or rephrased comments related to external Polkadot/Substrate PRs and issues.

This is a significant step towards fully customizing the SDK for the PezkuwiChain ecosystem.
This commit is contained in:
2025-12-14 00:04:10 +03:00
parent 286de54384
commit 1c0e57d984
9084 changed files with 997839 additions and 997557 deletions
+63
View File
@@ -0,0 +1,63 @@
[package]
name = "pezpallet-identity"
version = "29.0.0"
authors.workspace = true
edition.workspace = true
license = "Apache-2.0"
homepage.workspace = true
repository.workspace = true
description = "FRAME identity management pallet"
readme = "README.md"
[lints]
workspace = true
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { features = ["derive", "max-encoded-len"], workspace = true }
enumflags2 = { workspace = true }
pezframe-benchmarking = { optional = true, workspace = true }
pezframe-support = { workspace = true }
pezframe-system = { workspace = true }
log = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
pezsp-io = { workspace = true }
pezsp-runtime = { workspace = true }
[dev-dependencies]
pezpallet-balances = { workspace = true, default-features = true }
pezsp-core = { workspace = true, default-features = true }
pezsp-keystore = { workspace = true, default-features = true }
[features]
default = ["std"]
std = [
"codec/std",
"enumflags2/std",
"pezframe-benchmarking?/std",
"pezframe-support/std",
"pezframe-system/std",
"log/std",
"pezpallet-balances/std",
"scale-info/std",
"pezsp-core/std",
"pezsp-io/std",
"pezsp-keystore/std",
"pezsp-runtime/std",
]
runtime-benchmarks = [
"pezframe-benchmarking/runtime-benchmarks",
"pezframe-support/runtime-benchmarks",
"pezframe-system/runtime-benchmarks",
"pezpallet-balances/runtime-benchmarks",
"pezsp-io/runtime-benchmarks",
"pezsp-runtime/runtime-benchmarks",
]
try-runtime = [
"pezframe-support/try-runtime",
"pezframe-system/try-runtime",
"pezpallet-balances/try-runtime",
"pezsp-runtime/try-runtime",
]
+90
View File
@@ -0,0 +1,90 @@
# pezpallet-identity
## Identity Pallet
- [`Config`]
- [`Call`]
### Overview
A federated naming system, allowing for multiple registrars to be added from a specified origin.
Registrars can set a fee to provide identity-verification service. Anyone can put forth a
proposed identity for a fixed deposit and ask for review by any number of registrars (paying
each of their fees). Registrar judgements are given as an `enum`, allowing for sophisticated,
multi-tier opinions.
Some judgements are identified as *sticky*, which means they cannot be removed except by
complete removal of the identity, or by the registrar. Judgements are allowed to represent a
portion of funds that have been reserved for the registrar.
A super-user can remove accounts and in doing so, slash the deposit.
All accounts may also have a limited number of sub-accounts which may be specified by the owner;
by definition, these have equivalent ownership and each has an individual name.
The number of registrars should be limited, and the deposit made sufficiently large, to ensure
no state-bloat attack is viable.
#### Usernames
The pallet provides functionality for username authorities to issue usernames, which are independent
of the identity information functionality; an account can set:
- an identity without setting a username
- a username without setting an identity
- an identity and a username
The username functionality implemented in this pallet is meant to be a user friendly lookup of
accounts. There are mappings in both directions, "account -> username" and "username -> account".
To grant a username, a username authority can either:
- be given an allocation by governance of a specific amount of usernames to issue for free,
without any deposit associated with storage costs;
- put up a deposit for each username it issues (usually a subsidized, reduced deposit, relative
to other deposits in the system).
Users can have multiple usernames that map to the same `AccountId`, however one `AccountId` can only
map to a single username, known as the _primary_. This primary username will be the result of a
lookup in the `UsernameOf` map for any given account.
### Interface
#### Dispatchable Functions
##### For General Users
- `set_identity` - Set the associated identity of an account; a small deposit is reserved if not
already taken.
- `clear_identity` - Remove an account's associated identity; the deposit is returned.
- `request_judgement` - Request a judgement from a registrar, paying a fee.
- `cancel_request` - Cancel the previous request for a judgement.
- `accept_username` - Accept a username issued by a username authority.
- `remove_expired_approval` - Remove a username that was issued but never accepted.
- `set_primary_username` - Set a given username as an account's primary.
- `remove_username` - Remove a username after its grace period has ended.
##### For General Users with Sub-Identities
- `set_subs` - Set the sub-accounts of an identity.
- `add_sub` - Add a sub-identity to an identity.
- `remove_sub` - Remove a sub-identity of an identity.
- `rename_sub` - Rename a sub-identity of an identity.
- `quit_sub` - Remove a sub-identity of an identity (called by the sub-identity).
##### For Registrars
- `set_fee` - Set the fee required to be paid for a judgement to be given by the registrar.
- `set_fields` - Set the fields that a registrar cares about in their judgements.
- `provide_judgement` - Provide a judgement to an identity.
##### For Username Authorities
- `set_username_for` - Set a username for a given account. The account must approve it.
- `unbind_username` - Start the grace period for a username.
##### For Superusers
- `add_registrar` - Add a new registrar to the system.
- `kill_identity` - Forcibly remove the associated identity; the deposit is lost.
- `add_username_authority` - Add an account with the ability to issue usernames.
- `remove_username_authority` - Remove an account with the ability to issue usernames.
- `kill_username` - Forcibly remove a username.
[`Call`]: ./enum.Call.html
[`Config`]: ./trait.Config.html
License: Apache-2.0
@@ -0,0 +1,933 @@
// This file is part of Bizinikiwi.
// 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.
//! Identity pallet benchmarking.
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use crate::{migration::v2::LazyMigrationV1ToV2, Pallet as Identity};
use alloc::{vec, vec::Vec};
use pezframe_benchmarking::{account, v2::*, whitelisted_caller, BenchmarkError};
use pezframe_support::{
assert_ok, ensure,
traits::{EnsureOrigin, Get, OnFinalize, OnInitialize},
};
use pezframe_system::RawOrigin;
use pezsp_runtime::traits::{Bounded, One};
const SEED: u32 = 0;
fn assert_has_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
pezframe_system::Pallet::<T>::assert_has_event(generic_event.into());
}
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
pezframe_system::Pallet::<T>::assert_last_event(generic_event.into());
}
fn run_to_block<T: Config>(n: pezframe_system::pezpallet_prelude::BlockNumberFor<T>) {
while pezframe_system::Pallet::<T>::block_number() < n {
crate::Pallet::<T>::on_finalize(pezframe_system::Pallet::<T>::block_number());
pezframe_system::Pallet::<T>::on_finalize(pezframe_system::Pallet::<T>::block_number());
pezframe_system::Pallet::<T>::set_block_number(
pezframe_system::Pallet::<T>::block_number() + One::one(),
);
pezframe_system::Pallet::<T>::on_initialize(pezframe_system::Pallet::<T>::block_number());
crate::Pallet::<T>::on_initialize(pezframe_system::Pallet::<T>::block_number());
}
}
// Adds `r` registrars to the Identity Pallet. These registrars will have set fees and fields.
fn add_registrars<T: Config>(r: u32) -> Result<(), &'static str> {
for i in 0..r {
let registrar: T::AccountId = account("registrar", i, SEED);
let registrar_lookup = T::Lookup::unlookup(registrar.clone());
let _ = T::Currency::make_free_balance_be(&registrar, BalanceOf::<T>::max_value());
let registrar_origin = T::RegistrarOrigin::try_successful_origin()
.expect("RegistrarOrigin has no successful origin required for the benchmark");
Identity::<T>::add_registrar(registrar_origin, registrar_lookup)?;
Identity::<T>::set_fee(RawOrigin::Signed(registrar.clone()).into(), i, 10u32.into())?;
let fields = T::IdentityInformation::all_fields();
Identity::<T>::set_fields(RawOrigin::Signed(registrar.clone()).into(), i, fields)?;
}
assert_eq!(Registrars::<T>::get().len(), r as usize);
Ok(())
}
// Create `s` sub-accounts for the identity of `who` and return them.
// Each will have 32 bytes of raw data added to it.
fn create_sub_accounts<T: Config>(
who: &T::AccountId,
s: u32,
) -> Result<Vec<(T::AccountId, Data)>, &'static str> {
let mut subs = Vec::new();
let who_origin = RawOrigin::Signed(who.clone());
let data = Data::Raw(vec![0; 32].try_into().unwrap());
for i in 0..s {
let sub_account = account("sub", i, SEED);
subs.push((sub_account, data.clone()));
}
// Set identity so `set_subs` does not fail.
if IdentityOf::<T>::get(who).is_none() {
let _ = T::Currency::make_free_balance_be(who, BalanceOf::<T>::max_value() / 2u32.into());
let info = T::IdentityInformation::create_identity_info();
Identity::<T>::set_identity(who_origin.into(), Box::new(info))?;
}
Ok(subs)
}
// Adds `s` sub-accounts to the identity of `who`. Each will have 32 bytes of raw data added to it.
// This additionally returns the vector of sub-accounts so it can be modified if needed.
fn add_sub_accounts<T: Config>(
who: &T::AccountId,
s: u32,
) -> Result<Vec<(T::AccountId, Data)>, &'static str> {
let who_origin = RawOrigin::Signed(who.clone());
let subs = create_sub_accounts::<T>(who, s)?;
Identity::<T>::set_subs(who_origin.into(), subs.clone())?;
Ok(subs)
}
fn bench_suffix() -> Vec<u8> {
b"bench".to_vec()
}
fn bench_username() -> Vec<u8> {
// len = 24
b"veryfastbenchmarkmachine".to_vec()
}
fn bounded_username<T: Config>(username: Vec<u8>, suffix: Vec<u8>) -> Username<T> {
let mut full_username = Vec::with_capacity(username.len() + suffix.len() + 1);
full_username.extend(username);
full_username.extend(b".");
full_username.extend(suffix);
Username::<T>::try_from(full_username).expect("test usernames should fit within bounds")
}
#[benchmarks]
mod benchmarks {
use super::*;
#[benchmark]
fn add_registrar(r: Linear<1, { T::MaxRegistrars::get() - 1 }>) -> Result<(), BenchmarkError> {
add_registrars::<T>(r)?;
ensure!(Registrars::<T>::get().len() as u32 == r, "Registrars not set up correctly.");
let origin =
T::RegistrarOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let account = T::Lookup::unlookup(account("registrar", r + 1, SEED));
#[extrinsic_call]
_(origin as T::RuntimeOrigin, account);
ensure!(Registrars::<T>::get().len() as u32 == r + 1, "Registrars not added.");
Ok(())
}
#[benchmark]
fn set_identity(r: Linear<1, { T::MaxRegistrars::get() }>) -> Result<(), BenchmarkError> {
add_registrars::<T>(r)?;
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let caller_origin: <T as pezframe_system::Config>::RuntimeOrigin =
RawOrigin::Signed(caller.clone()).into();
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
// Add an initial identity
let initial_info = T::IdentityInformation::create_identity_info();
Identity::<T>::set_identity(caller_origin.clone(), Box::new(initial_info.clone()))?;
// User requests judgement from all the registrars, and they approve
for i in 0..r {
let registrar: T::AccountId = account("registrar", i, SEED);
let _ = T::Lookup::unlookup(registrar.clone());
let balance_to_use = T::Currency::minimum_balance() * 10u32.into();
let _ = T::Currency::make_free_balance_be(&registrar, balance_to_use);
Identity::<T>::request_judgement(caller_origin.clone(), i, 10u32.into())?;
Identity::<T>::provide_judgement(
RawOrigin::Signed(registrar).into(),
i,
caller_lookup.clone(),
Judgement::Reasonable,
T::Hashing::hash_of(&initial_info),
)?;
}
#[extrinsic_call]
_(
RawOrigin::Signed(caller.clone()),
Box::new(T::IdentityInformation::create_identity_info()),
);
assert_last_event::<T>(Event::<T>::IdentitySet { who: caller }.into());
Ok(())
}
// We need to split `set_subs` into two benchmarks to accurately isolate the potential
// writes caused by new or old sub accounts. The actual weight should simply be
// the sum of these two weights.
#[benchmark]
fn set_subs_new(s: Linear<0, { T::MaxSubAccounts::get() }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
// Create a new subs vec with sub accounts
let subs = create_sub_accounts::<T>(&caller, s)?;
ensure!(SubsOf::<T>::get(&caller).1.len() == 0, "Caller already has subs");
#[extrinsic_call]
set_subs(RawOrigin::Signed(caller.clone()), subs);
ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s, "Subs not added");
Ok(())
}
#[benchmark]
fn set_subs_old(p: Linear<0, { T::MaxSubAccounts::get() }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
// Give them p many previous sub accounts.
add_sub_accounts::<T>(&caller, p)?;
// Remove all subs.
let subs = create_sub_accounts::<T>(&caller, 0)?;
ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == p, "Caller does have subs",);
#[extrinsic_call]
set_subs(RawOrigin::Signed(caller.clone()), subs);
ensure!(SubsOf::<T>::get(&caller).1.len() == 0, "Subs not removed");
Ok(())
}
#[benchmark]
fn clear_identity(
r: Linear<1, { T::MaxRegistrars::get() }>,
s: Linear<0, { T::MaxSubAccounts::get() }>,
) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let caller_origin =
<T as pezframe_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));
let caller_lookup = <T::Lookup as StaticLookup>::unlookup(caller.clone());
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
// Register the registrars
add_registrars::<T>(r)?;
// Add sub accounts
add_sub_accounts::<T>(&caller, s)?;
// Create their main identity with x additional fields
let info = T::IdentityInformation::create_identity_info();
Identity::<T>::set_identity(caller_origin.clone(), Box::new(info.clone()))?;
// User requests judgement from all the registrars, and they approve
for i in 0..r {
let registrar: T::AccountId = account("registrar", i, SEED);
let balance_to_use = T::Currency::minimum_balance() * 10u32.into();
let _ = T::Currency::make_free_balance_be(&registrar, balance_to_use);
Identity::<T>::request_judgement(caller_origin.clone(), i, 10u32.into())?;
Identity::<T>::provide_judgement(
RawOrigin::Signed(registrar).into(),
i,
caller_lookup.clone(),
Judgement::Reasonable,
T::Hashing::hash_of(&info),
)?;
}
ensure!(IdentityOf::<T>::contains_key(&caller), "Identity does not exist.");
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()));
ensure!(!IdentityOf::<T>::contains_key(&caller), "Identity not cleared.");
Ok(())
}
#[benchmark]
fn request_judgement(r: Linear<1, { T::MaxRegistrars::get() }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
// Register the registrars
add_registrars::<T>(r)?;
// Create their main identity with x additional fields
let info = T::IdentityInformation::create_identity_info();
let caller_origin =
<T as pezframe_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));
Identity::<T>::set_identity(caller_origin.clone(), Box::new(info))?;
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), r - 1, 10u32.into());
assert_last_event::<T>(
Event::<T>::JudgementRequested { who: caller, registrar_index: r - 1 }.into(),
);
Ok(())
}
#[benchmark]
fn cancel_request(r: Linear<1, { T::MaxRegistrars::get() }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
// Register the registrars
add_registrars::<T>(r)?;
// Create their main identity with x additional fields
let info = T::IdentityInformation::create_identity_info();
let caller_origin =
<T as pezframe_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(caller.clone()));
Identity::<T>::set_identity(caller_origin.clone(), Box::new(info))?;
Identity::<T>::request_judgement(caller_origin.clone(), r - 1, 10u32.into())?;
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), r - 1);
assert_last_event::<T>(
Event::<T>::JudgementUnrequested { who: caller, registrar_index: r - 1 }.into(),
);
Ok(())
}
#[benchmark]
fn set_fee(r: Linear<1, { T::MaxRegistrars::get() - 1 }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
add_registrars::<T>(r)?;
let registrar_origin = T::RegistrarOrigin::try_successful_origin()
.expect("RegistrarOrigin has no successful origin required for the benchmark");
Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;
let registrars = Registrars::<T>::get();
ensure!(registrars[r as usize].as_ref().unwrap().fee == 0u32.into(), "Fee already set.");
#[extrinsic_call]
_(RawOrigin::Signed(caller), r, 100u32.into());
let updated_registrars = Registrars::<T>::get();
ensure!(
updated_registrars[r as usize].as_ref().unwrap().fee == 100u32.into(),
"Fee not changed."
);
Ok(())
}
#[benchmark]
fn set_account_id(r: Linear<1, { T::MaxRegistrars::get() - 1 }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
add_registrars::<T>(r)?;
let registrar_origin = T::RegistrarOrigin::try_successful_origin()
.expect("RegistrarOrigin has no successful origin required for the benchmark");
Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;
let registrars = Registrars::<T>::get();
ensure!(registrars[r as usize].as_ref().unwrap().account == caller, "id not set.");
let new_account = T::Lookup::unlookup(account("new", 0, SEED));
#[extrinsic_call]
_(RawOrigin::Signed(caller), r, new_account);
let updated_registrars = Registrars::<T>::get();
ensure!(
updated_registrars[r as usize].as_ref().unwrap().account == account("new", 0, SEED),
"id not changed."
);
Ok(())
}
#[benchmark]
fn set_fields(r: Linear<1, { T::MaxRegistrars::get() - 1 }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
add_registrars::<T>(r)?;
let registrar_origin = T::RegistrarOrigin::try_successful_origin()
.expect("RegistrarOrigin has no successful origin required for the benchmark");
Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;
let registrars = Registrars::<T>::get();
ensure!(
registrars[r as usize].as_ref().unwrap().fields == Default::default(),
"fields already set."
);
let fields = T::IdentityInformation::all_fields();
#[extrinsic_call]
_(RawOrigin::Signed(caller), r, fields);
let updated_registrars = Registrars::<T>::get();
ensure!(
updated_registrars[r as usize].as_ref().unwrap().fields != Default::default(),
"fields not set."
);
Ok(())
}
#[benchmark]
fn provide_judgement(
r: Linear<1, { T::MaxRegistrars::get() - 1 }>,
) -> Result<(), BenchmarkError> {
// The user
let user: T::AccountId = account("user", r, SEED);
let user_origin =
<T as pezframe_system::Config>::RuntimeOrigin::from(RawOrigin::Signed(user.clone()));
let user_lookup = <T::Lookup as StaticLookup>::unlookup(user.clone());
let _ = T::Currency::make_free_balance_be(&user, BalanceOf::<T>::max_value());
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
add_registrars::<T>(r)?;
let info = T::IdentityInformation::create_identity_info();
let info_hash = T::Hashing::hash_of(&info);
Identity::<T>::set_identity(user_origin.clone(), Box::new(info))?;
let registrar_origin = T::RegistrarOrigin::try_successful_origin()
.expect("RegistrarOrigin has no successful origin required for the benchmark");
Identity::<T>::add_registrar(registrar_origin, caller_lookup)?;
Identity::<T>::request_judgement(user_origin, r, 10u32.into())?;
#[extrinsic_call]
_(RawOrigin::Signed(caller), r, user_lookup, Judgement::Reasonable, info_hash);
assert_last_event::<T>(
Event::<T>::JudgementGiven { target: user, registrar_index: r }.into(),
);
Ok(())
}
#[benchmark]
fn kill_identity(
r: Linear<1, { T::MaxRegistrars::get() }>,
s: Linear<0, { T::MaxSubAccounts::get() }>,
) -> Result<(), BenchmarkError> {
add_registrars::<T>(r)?;
let target: T::AccountId = account("target", 0, SEED);
let target_origin: <T as pezframe_system::Config>::RuntimeOrigin =
RawOrigin::Signed(target.clone()).into();
let target_lookup = T::Lookup::unlookup(target.clone());
let _ = T::Currency::make_free_balance_be(&target, BalanceOf::<T>::max_value());
let info = T::IdentityInformation::create_identity_info();
Identity::<T>::set_identity(target_origin.clone(), Box::new(info.clone()))?;
add_sub_accounts::<T>(&target, s)?;
// User requests judgement from all the registrars, and they approve
for i in 0..r {
let registrar: T::AccountId = account("registrar", i, SEED);
let balance_to_use = T::Currency::minimum_balance() * 10u32.into();
let _ = T::Currency::make_free_balance_be(&registrar, balance_to_use);
Identity::<T>::request_judgement(target_origin.clone(), i, 10u32.into())?;
Identity::<T>::provide_judgement(
RawOrigin::Signed(registrar).into(),
i,
target_lookup.clone(),
Judgement::Reasonable,
T::Hashing::hash_of(&info),
)?;
}
ensure!(IdentityOf::<T>::contains_key(&target), "Identity not set");
let origin =
T::ForceOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
#[extrinsic_call]
_(origin as T::RuntimeOrigin, target_lookup);
ensure!(!IdentityOf::<T>::contains_key(&target), "Identity not removed");
Ok(())
}
#[benchmark]
fn add_sub(s: Linear<0, { T::MaxSubAccounts::get() - 1 }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
add_sub_accounts::<T>(&caller, s)?;
let sub = account("new_sub", 0, SEED);
let data = Data::Raw(vec![0; 32].try_into().unwrap());
ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s, "Subs not set.");
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), T::Lookup::unlookup(sub), data);
ensure!(SubsOf::<T>::get(&caller).1.len() as u32 == s + 1, "Subs not added.");
Ok(())
}
#[benchmark]
fn rename_sub(s: Linear<1, { T::MaxSubAccounts::get() }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let (sub, _) = add_sub_accounts::<T>(&caller, s)?.remove(0);
let data = Data::Raw(vec![1; 32].try_into().unwrap());
ensure!(SuperOf::<T>::get(&sub).unwrap().1 != data, "data already set");
#[extrinsic_call]
_(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()), data.clone());
ensure!(SuperOf::<T>::get(&sub).unwrap().1 == data, "data not set");
Ok(())
}
#[benchmark]
fn remove_sub(s: Linear<1, { T::MaxSubAccounts::get() }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let (sub, _) = add_sub_accounts::<T>(&caller, s)?.remove(0);
ensure!(SuperOf::<T>::contains_key(&sub), "Sub doesn't exists");
#[extrinsic_call]
_(RawOrigin::Signed(caller), T::Lookup::unlookup(sub.clone()));
ensure!(!SuperOf::<T>::contains_key(&sub), "Sub not removed");
Ok(())
}
#[benchmark]
fn quit_sub(s: Linear<0, { T::MaxSubAccounts::get() - 1 }>) -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let sup = account("super", 0, SEED);
add_sub_accounts::<T>(&sup, s)?;
let sup_origin = RawOrigin::Signed(sup).into();
Identity::<T>::add_sub(
sup_origin,
T::Lookup::unlookup(caller.clone()),
Data::Raw(vec![0; 32].try_into().unwrap()),
)?;
ensure!(SuperOf::<T>::contains_key(&caller), "Sub doesn't exists");
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()));
ensure!(!SuperOf::<T>::contains_key(&caller), "Sub not removed");
Ok(())
}
#[benchmark]
fn add_username_authority() -> Result<(), BenchmarkError> {
let origin =
T::UsernameAuthorityOrigin::try_successful_origin().expect("can generate origin");
let authority: T::AccountId = account("authority", 0, SEED);
let authority_lookup = T::Lookup::unlookup(authority.clone());
let suffix = bench_suffix();
let allocation = 10;
#[extrinsic_call]
_(origin as T::RuntimeOrigin, authority_lookup, suffix, allocation);
assert_last_event::<T>(Event::<T>::AuthorityAdded { authority }.into());
Ok(())
}
#[benchmark]
fn remove_username_authority() -> Result<(), BenchmarkError> {
let origin =
T::UsernameAuthorityOrigin::try_successful_origin().expect("can generate origin");
let authority: T::AccountId = account("authority", 0, SEED);
let authority_lookup = T::Lookup::unlookup(authority.clone());
let suffix = bench_suffix();
let allocation = 10;
assert_ok!(Identity::<T>::add_username_authority(
origin.clone(),
authority_lookup.clone(),
suffix.clone(),
allocation
));
#[extrinsic_call]
_(origin as T::RuntimeOrigin, suffix.into(), authority_lookup);
assert_last_event::<T>(Event::<T>::AuthorityRemoved { authority }.into());
Ok(())
}
#[benchmark]
fn set_username_for(p: Linear<0, 1>) -> Result<(), BenchmarkError> {
// Set up a username authority.
let auth_origin =
T::UsernameAuthorityOrigin::try_successful_origin().expect("can generate origin");
let authority: T::AccountId = account("authority", 0, SEED);
let authority_lookup = T::Lookup::unlookup(authority.clone());
let suffix = bench_suffix();
let allocation = 10;
let _ = T::Currency::make_free_balance_be(&authority, BalanceOf::<T>::max_value());
Identity::<T>::add_username_authority(
auth_origin,
authority_lookup,
suffix.clone(),
allocation,
)?;
let username = bench_username();
let bounded_username = bounded_username::<T>(username.clone(), suffix.clone());
let (public, signature) = T::BenchmarkHelper::sign_message(&bounded_username[..]);
let who_account = public.into_account();
let who_lookup = T::Lookup::unlookup(who_account.clone());
// Verify signature here to avoid surprise errors at runtime
assert!(signature.verify(&bounded_username[..], &who_account));
let use_allocation = match p {
0 => false,
1 => true,
_ => unreachable!(),
};
#[extrinsic_call]
set_username_for(
RawOrigin::Signed(authority.clone()),
who_lookup,
bounded_username.clone().into(),
Some(signature.into()),
use_allocation,
);
assert_has_event::<T>(
Event::<T>::UsernameSet {
who: who_account.clone(),
username: bounded_username.clone(),
}
.into(),
);
assert_has_event::<T>(
Event::<T>::PrimaryUsernameSet { who: who_account, username: bounded_username }.into(),
);
if use_allocation {
let suffix: Suffix<T> = suffix.try_into().unwrap();
assert_eq!(AuthorityOf::<T>::get(&suffix).unwrap().allocation, 9);
} else {
assert_eq!(
T::Currency::free_balance(&authority),
BalanceOf::<T>::max_value() - T::UsernameDeposit::get()
);
}
Ok(())
}
#[benchmark]
fn accept_username() -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let username = bounded_username::<T>(bench_username(), bench_suffix());
Identity::<T>::queue_acceptance(&caller, username.clone(), Provider::Allocation);
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), username.clone());
assert_last_event::<T>(Event::<T>::UsernameSet { who: caller, username }.into());
Ok(())
}
#[benchmark]
fn remove_expired_approval(p: Linear<0, 1>) -> Result<(), BenchmarkError> {
// Set up a username authority.
let auth_origin =
T::UsernameAuthorityOrigin::try_successful_origin().expect("can generate origin");
let authority: T::AccountId = account("authority", 0, SEED);
let authority_lookup = T::Lookup::unlookup(authority.clone());
let suffix = bench_suffix();
let allocation = 10;
let _ = T::Currency::make_free_balance_be(&authority, BalanceOf::<T>::max_value());
Identity::<T>::add_username_authority(
auth_origin,
authority_lookup,
suffix.clone(),
allocation,
)?;
let caller: T::AccountId = whitelisted_caller();
let username = bounded_username::<T>(bench_username(), suffix.clone());
let username_deposit = T::UsernameDeposit::get();
let provider = match p {
0 => {
let _ = T::Currency::reserve(&authority, username_deposit);
Provider::AuthorityDeposit(username_deposit)
},
1 => Provider::Allocation,
_ => unreachable!(),
};
Identity::<T>::queue_acceptance(&caller, username.clone(), provider);
let expected_expiration =
pezframe_system::Pallet::<T>::block_number() + T::PendingUsernameExpiration::get();
run_to_block::<T>(expected_expiration + One::one());
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), username);
assert_last_event::<T>(Event::<T>::PreapprovalExpired { whose: caller }.into());
match p {
0 => {
assert_eq!(T::Currency::free_balance(&authority), BalanceOf::<T>::max_value());
},
1 => {
let suffix: Suffix<T> = suffix.try_into().unwrap();
assert_eq!(AuthorityOf::<T>::get(&suffix).unwrap().allocation, 10);
},
_ => unreachable!(),
}
Ok(())
}
#[benchmark]
fn set_primary_username() -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let first_username = bounded_username::<T>(bench_username(), bench_suffix());
let second_username = bounded_username::<T>(b"slowbenchmark".to_vec(), bench_suffix());
// First one will be set as primary. Second will not be.
Identity::<T>::insert_username(&caller, first_username, Provider::Allocation);
Identity::<T>::insert_username(&caller, second_username.clone(), Provider::Allocation);
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), second_username.clone());
assert_last_event::<T>(
Event::<T>::PrimaryUsernameSet { who: caller, username: second_username }.into(),
);
Ok(())
}
#[benchmark]
fn unbind_username() -> Result<(), BenchmarkError> {
// Set up a username authority.
let auth_origin =
T::UsernameAuthorityOrigin::try_successful_origin().expect("can generate origin");
let authority: T::AccountId = account("authority", 0, SEED);
let authority_lookup = T::Lookup::unlookup(authority.clone());
let suffix = bench_suffix();
let allocation = 10;
let _ = T::Currency::make_free_balance_be(&authority, BalanceOf::<T>::max_value());
Identity::<T>::add_username_authority(
auth_origin,
authority_lookup,
suffix.clone(),
allocation,
)?;
let caller: T::AccountId = whitelisted_caller();
let username = bounded_username::<T>(bench_username(), suffix.clone());
let username_deposit = T::UsernameDeposit::get();
Identity::<T>::insert_username(
&caller,
username.clone(),
Provider::AuthorityDeposit(username_deposit),
);
#[extrinsic_call]
_(RawOrigin::Signed(authority), username.clone());
assert_last_event::<T>(Event::<T>::UsernameUnbound { username }.into());
Ok(())
}
#[benchmark]
fn remove_username() -> Result<(), BenchmarkError> {
// Set up a username authority.
let authority: T::AccountId = account("authority", 0, SEED);
let suffix = bench_suffix();
let _ = T::Currency::make_free_balance_be(&authority, BalanceOf::<T>::max_value());
let caller: T::AccountId = whitelisted_caller();
let username = bounded_username::<T>(bench_username(), suffix.clone());
let username_deposit = T::UsernameDeposit::get();
Identity::<T>::insert_username(
&caller,
username.clone(),
Provider::AuthorityDeposit(username_deposit),
);
let now = pezframe_system::Pallet::<T>::block_number();
let expiry = now + T::UsernameGracePeriod::get();
UnbindingUsernames::<T>::insert(&username, expiry);
pezframe_system::Pallet::<T>::set_block_number(expiry);
#[extrinsic_call]
_(RawOrigin::Signed(caller), username.clone());
assert_last_event::<T>(Event::<T>::UsernameRemoved { username }.into());
Ok(())
}
#[benchmark]
fn kill_username(p: Linear<0, 1>) -> Result<(), BenchmarkError> {
// Set up a username authority.
let auth_origin =
T::UsernameAuthorityOrigin::try_successful_origin().expect("can generate origin");
let authority: T::AccountId = account("authority", 0, SEED);
let authority_lookup = T::Lookup::unlookup(authority.clone());
let suffix = bench_suffix();
let allocation = 10;
let _ = T::Currency::make_free_balance_be(&authority, BalanceOf::<T>::max_value());
Identity::<T>::add_username_authority(
auth_origin,
authority_lookup,
suffix.clone(),
allocation,
)?;
let caller: T::AccountId = whitelisted_caller();
let username = bounded_username::<T>(bench_username(), suffix.clone());
let username_deposit = T::UsernameDeposit::get();
let provider = match p {
0 => {
let _ = T::Currency::reserve(&authority, username_deposit);
Provider::AuthorityDeposit(username_deposit)
},
1 => Provider::Allocation,
_ => unreachable!(),
};
Identity::<T>::insert_username(&caller, username.clone(), provider);
UnbindingUsernames::<T>::insert(&username, pezframe_system::Pallet::<T>::block_number());
#[extrinsic_call]
_(RawOrigin::Root, username.clone());
assert_last_event::<T>(Event::<T>::UsernameKilled { username }.into());
match p {
0 => {
assert_eq!(
T::Currency::free_balance(&authority),
BalanceOf::<T>::max_value() - username_deposit
);
},
1 => {
let suffix: Suffix<T> = suffix.try_into().unwrap();
assert_eq!(AuthorityOf::<T>::get(&suffix).unwrap().allocation, 10);
},
_ => unreachable!(),
}
Ok(())
}
#[benchmark]
fn migration_v2_authority_step() -> Result<(), BenchmarkError> {
let setup = LazyMigrationV1ToV2::<T>::setup_benchmark_env_for_migration();
assert_eq!(AuthorityOf::<T>::iter().count(), 0);
#[block]
{
LazyMigrationV1ToV2::<T>::authority_step(None);
}
assert_eq!(AuthorityOf::<T>::get(&setup.suffix).unwrap().account_id, setup.authority);
Ok(())
}
#[benchmark]
fn migration_v2_username_step() -> Result<(), BenchmarkError> {
let setup = LazyMigrationV1ToV2::<T>::setup_benchmark_env_for_migration();
assert_eq!(UsernameInfoOf::<T>::iter().count(), 0);
#[block]
{
LazyMigrationV1ToV2::<T>::username_step(None);
}
assert_eq!(UsernameInfoOf::<T>::iter().next().unwrap().1.owner, setup.account);
Ok(())
}
#[benchmark]
fn migration_v2_identity_step() -> Result<(), BenchmarkError> {
let setup = LazyMigrationV1ToV2::<T>::setup_benchmark_env_for_migration();
#[block]
{
LazyMigrationV1ToV2::<T>::identity_step(None);
}
assert!(IdentityOf::<T>::get(&setup.account).is_some());
Ok(())
}
#[benchmark]
fn migration_v2_pending_username_step() -> Result<(), BenchmarkError> {
let setup = LazyMigrationV1ToV2::<T>::setup_benchmark_env_for_migration();
#[block]
{
LazyMigrationV1ToV2::<T>::pending_username_step(None);
}
assert!(PendingUsernames::<T>::get(&setup.username).is_some());
Ok(())
}
#[benchmark]
fn migration_v2_cleanup_authority_step() -> Result<(), BenchmarkError> {
let setup = LazyMigrationV1ToV2::<T>::setup_benchmark_env_for_cleanup();
#[block]
{
LazyMigrationV1ToV2::<T>::cleanup_authority_step(None);
}
LazyMigrationV1ToV2::<T>::check_authority_cleanup_validity(setup.suffix, setup.authority);
Ok(())
}
#[benchmark]
fn migration_v2_cleanup_username_step() -> Result<(), BenchmarkError> {
let setup = LazyMigrationV1ToV2::<T>::setup_benchmark_env_for_cleanup();
#[block]
{
LazyMigrationV1ToV2::<T>::cleanup_username_step(None);
}
LazyMigrationV1ToV2::<T>::check_username_cleanup_validity(setup.username, setup.account);
Ok(())
}
impl_benchmark_test_suite!(Identity, crate::tests::new_test_ext(), crate::tests::Test);
}
+204
View File
@@ -0,0 +1,204 @@
// This file is part of Bizinikiwi.
// 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.
#[cfg(feature = "runtime-benchmarks")]
use alloc::vec;
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
#[cfg(feature = "runtime-benchmarks")]
use enumflags2::BitFlag;
use enumflags2::{bitflags, BitFlags};
use pezframe_support::{traits::Get, CloneNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound};
use scale_info::{build::Variants, Path, Type, TypeInfo};
use pezsp_runtime::{BoundedVec, RuntimeDebug};
use crate::types::{Data, IdentityInformationProvider};
/// The fields that we use to identify the owner of an account with. Each corresponds to a field
/// in the `IdentityInfo` struct.
#[bitflags]
#[repr(u64)]
#[derive(Clone, Copy, PartialEq, Eq, RuntimeDebug)]
pub enum IdentityField {
Display,
Legal,
Web,
Riot,
Email,
PgpFingerprint,
Image,
Twitter,
}
impl TypeInfo for IdentityField {
type Identity = Self;
fn type_info() -> scale_info::Type {
Type::builder().path(Path::new("IdentityField", module_path!())).variant(
Variants::new()
.variant("Display", |v| v.index(0))
.variant("Legal", |v| v.index(1))
.variant("Web", |v| v.index(2))
.variant("Riot", |v| v.index(3))
.variant("Email", |v| v.index(4))
.variant("PgpFingerprint", |v| v.index(5))
.variant("Image", |v| v.index(6))
.variant("Twitter", |v| v.index(7)),
)
}
}
/// Information concerning the identity of the controller of an account.
///
/// NOTE: This should be stored at the end of the storage item to facilitate the addition of extra
/// fields in a backwards compatible way through a specialized `Decode` impl.
#[derive(
CloneNoBound,
Encode,
Decode,
DecodeWithMemTracking,
EqNoBound,
MaxEncodedLen,
PartialEqNoBound,
RuntimeDebugNoBound,
TypeInfo,
)]
#[codec(mel_bound())]
#[scale_info(skip_type_params(FieldLimit))]
pub struct IdentityInfo<FieldLimit: Get<u32>> {
/// Additional fields of the identity that are not catered for with the struct's explicit
/// fields.
pub additional: BoundedVec<(Data, Data), FieldLimit>,
/// A reasonable display name for the controller of the account. This should be whatever it is
/// that it is typically known as and should not be confusable with other entities, given
/// reasonable context.
///
/// Stored as UTF-8.
pub display: Data,
/// The full legal name in the local jurisdiction of the entity. This might be a bit
/// long-winded.
///
/// Stored as UTF-8.
pub legal: Data,
/// A representative website held by the controller of the account.
///
/// NOTE: `https://` is automatically prepended.
///
/// Stored as UTF-8.
pub web: Data,
/// The Riot/Matrix handle held by the controller of the account.
///
/// Stored as UTF-8.
pub riot: Data,
/// The email address of the controller of the account.
///
/// Stored as UTF-8.
pub email: Data,
/// The PGP/GPG public key of the controller of the account.
pub pgp_fingerprint: Option<[u8; 20]>,
/// A graphic image representing the controller of the account. Should be a company,
/// organization or project logo or a headshot in the case of a human.
pub image: Data,
/// The Twitter identity. The leading `@` character may be elided.
pub twitter: Data,
}
impl<FieldLimit: Get<u32> + 'static> IdentityInformationProvider for IdentityInfo<FieldLimit> {
type FieldsIdentifier = u64;
fn has_identity(&self, fields: Self::FieldsIdentifier) -> bool {
self.fields().bits() & fields == fields
}
#[cfg(feature = "runtime-benchmarks")]
fn create_identity_info() -> Self {
let data = Data::Raw(vec![0; 32].try_into().unwrap());
IdentityInfo {
additional: vec![(data.clone(), data.clone()); FieldLimit::get().try_into().unwrap()]
.try_into()
.unwrap(),
display: data.clone(),
legal: data.clone(),
web: data.clone(),
riot: data.clone(),
email: data.clone(),
pgp_fingerprint: Some([0; 20]),
image: data.clone(),
twitter: data,
}
}
#[cfg(feature = "runtime-benchmarks")]
fn all_fields() -> Self::FieldsIdentifier {
IdentityField::all().bits()
}
}
impl<FieldLimit: Get<u32>> Default for IdentityInfo<FieldLimit> {
fn default() -> Self {
IdentityInfo {
additional: BoundedVec::default(),
display: Data::None,
legal: Data::None,
web: Data::None,
riot: Data::None,
email: Data::None,
pgp_fingerprint: None,
image: Data::None,
twitter: Data::None,
}
}
}
impl<FieldLimit: Get<u32>> IdentityInfo<FieldLimit> {
pub(crate) fn fields(&self) -> BitFlags<IdentityField> {
let mut res = BitFlags::<IdentityField>::empty();
if !self.display.is_none() {
res.insert(IdentityField::Display);
}
if !self.legal.is_none() {
res.insert(IdentityField::Legal);
}
if !self.web.is_none() {
res.insert(IdentityField::Web);
}
if !self.riot.is_none() {
res.insert(IdentityField::Riot);
}
if !self.email.is_none() {
res.insert(IdentityField::Email);
}
if self.pgp_fingerprint.is_some() {
res.insert(IdentityField::PgpFingerprint);
}
if !self.image.is_none() {
res.insert(IdentityField::Image);
}
if !self.twitter.is_none() {
res.insert(IdentityField::Twitter);
}
res
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,850 @@
// 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.
//! Storage migrations for the Identity pallet.
extern crate alloc;
use super::*;
use pezframe_support::{
migrations::VersionedMigration, pezpallet_prelude::*, storage_alias,
traits::UncheckedOnRuntimeUpgrade, IterableStorageMap,
};
#[cfg(feature = "try-runtime")]
use alloc::collections::BTreeMap;
#[cfg(feature = "try-runtime")]
use codec::{Decode, Encode};
#[cfg(feature = "try-runtime")]
use pezsp_runtime::TryRuntimeError;
pub const PALLET_MIGRATIONS_ID: &[u8; 15] = b"pezpallet-identity";
pub mod versioned {
use super::*;
pub type V0ToV1<T, const KL: u64> = VersionedMigration<
0,
1,
v1::VersionUncheckedMigrateV0ToV1<T, KL>,
crate::pallet::Pallet<T>,
<T as pezframe_system::Config>::DbWeight,
>;
}
/// The old identity types in v0.
mod types_v0 {
use super::*;
#[storage_alias]
pub type IdentityOf<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<T as pezframe_system::Config>::AccountId,
Registration<
BalanceOf<T>,
<T as pallet::Config>::MaxRegistrars,
<T as pallet::Config>::IdentityInformation,
>,
OptionQuery,
>;
}
/// The old identity types in v1.
mod types_v1 {
use super::*;
#[storage_alias]
pub type IdentityOf<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<T as pezframe_system::Config>::AccountId,
(
Registration<
BalanceOf<T>,
<T as pallet::Config>::MaxRegistrars,
<T as pallet::Config>::IdentityInformation,
>,
Option<Username<T>>,
),
OptionQuery,
>;
#[storage_alias]
pub type UsernameAuthorities<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<T as pezframe_system::Config>::AccountId,
AuthorityProperties<Suffix<T>>,
OptionQuery,
>;
#[storage_alias]
pub type AccountOfUsername<T: Config> = StorageMap<
Pallet<T>,
Blake2_128Concat,
Username<T>,
<T as pezframe_system::Config>::AccountId,
OptionQuery,
>;
#[cfg(feature = "try-runtime")]
#[storage_alias]
pub type PendingUsernames<T: Config> = StorageMap<
Pallet<T>,
Blake2_128Concat,
Username<T>,
(<T as pezframe_system::Config>::AccountId, BlockNumberFor<T>),
OptionQuery,
>;
}
pub mod v1 {
use super::*;
/// The log target.
const TARGET: &'static str = "runtime::identity::migration::v1";
/// Migration to add usernames to Identity info.
///
/// `T` is the runtime and `KL` is the key limit to migrate. This is just a safety guard to
/// prevent stalling a teyrchain by accumulating too much weight in the migration. To have an
/// unlimited migration (e.g. in a chain without PoV limits), set this to `u64::MAX`.
pub struct VersionUncheckedMigrateV0ToV1<T, const KL: u64>(PhantomData<T>);
impl<T: Config, const KL: u64> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateV0ToV1<T, KL> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
let identities = types_v0::IdentityOf::<T>::iter().count();
log::info!(
target: TARGET,
"pre-upgrade state contains '{}' identities.",
identities
);
ensure!((identities as u64) < KL, "too many identities to migrate");
Ok((identities as u64).encode())
}
fn on_runtime_upgrade() -> Weight {
log::info!(
target: TARGET,
"running storage migration from version 0 to version 1."
);
let mut weight = T::DbWeight::get().reads(1);
let mut translated: u64 = 0;
let mut interrupted = false;
for (account, registration) in types_v0::IdentityOf::<T>::iter() {
types_v1::IdentityOf::<T>::insert(account, (registration, None::<Username<T>>));
translated.saturating_inc();
if translated >= KL {
log::warn!(
"Incomplete! Migration limit reached. Only {} identities migrated.",
translated
);
interrupted = true;
break;
}
}
if !interrupted {
log::info!("all {} identities migrated", translated);
}
weight.saturating_accrue(T::DbWeight::get().reads_writes(translated, translated));
weight.saturating_accrue(T::DbWeight::get().writes(1));
weight
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), TryRuntimeError> {
let identities_to_migrate: u64 = Decode::decode(&mut &state[..])
.expect("failed to decode the state from pre-upgrade.");
let identities = types_v1::IdentityOf::<T>::iter().count() as u64;
log::info!("post-upgrade expects '{}' identities to have been migrated.", identities);
ensure!(identities_to_migrate == identities, "must migrate all identities.");
log::info!(target: TARGET, "migrated all identities.");
Ok(())
}
}
}
pub mod v2 {
use super::*;
use pezframe_support::{
migrations::{MigrationId, SteppedMigration, SteppedMigrationError},
weights::WeightMeter,
};
type HashedKey = BoundedVec<u8, ConstU32<256>>;
// The resulting state of the step and the actual weight consumed.
type StepResultOf<T> =
MigrationState<<T as pezframe_system::Config>::AccountId, Username<T>, Suffix<T>>;
#[cfg(feature = "runtime-benchmarks")]
pub(crate) type BenchmarkingSetupOf<T> =
BenchmarkingSetup<Suffix<T>, <T as pezframe_system::Config>::AccountId, Username<T>>;
/// Progressive states of a migration. The migration starts with the first variant and ends with
/// the last.
#[derive(Decode, Encode, MaxEncodedLen, Eq, PartialEq)]
pub enum MigrationState<A, U, S> {
Authority(A),
FinishedAuthorities,
Identity(HashedKey),
FinishedIdentities,
Username(U),
FinishedUsernames,
PendingUsername(HashedKey),
FinishedPendingUsernames,
CleanupAuthorities(S),
FinishedCleanupAuthorities,
CleanupUsernames(U),
Finished,
}
#[cfg(feature = "try-runtime")]
#[derive(Encode, Decode)]
struct TryRuntimeState<T: Config> {
authorities: BTreeMap<Suffix<T>, (T::AccountId, u32)>,
identities: BTreeMap<
T::AccountId,
Registration<
BalanceOf<T>,
<T as Config>::MaxRegistrars,
<T as Config>::IdentityInformation,
>,
>,
primary_usernames: BTreeMap<T::AccountId, Username<T>>,
usernames: BTreeMap<Username<T>, T::AccountId>,
pending_usernames: BTreeMap<Username<T>, (T::AccountId, BlockNumberFor<T>)>,
}
pub struct LazyMigrationV1ToV2<T: Config>(PhantomData<T>);
impl<T: Config> SteppedMigration for LazyMigrationV1ToV2<T> {
type Cursor = MigrationState<T::AccountId, Username<T>, Suffix<T>>;
type Identifier = MigrationId<15>;
fn id() -> Self::Identifier {
MigrationId { pezpallet_id: *PALLET_MIGRATIONS_ID, version_from: 1, version_to: 2 }
}
fn step(
mut cursor: Option<Self::Cursor>,
meter: &mut WeightMeter,
) -> Result<Option<Self::Cursor>, SteppedMigrationError> {
if Pallet::<T>::on_chain_storage_version() != Self::id().version_from as u16 {
return Ok(None);
}
// Check that we have enough weight for at least the next step. If we don't, then the
// migration cannot be complete.
let required = match &cursor {
Some(state) => Self::required_weight(&state),
// Worst case weight for `authority_step`.
None => T::WeightInfo::migration_v2_authority_step(),
};
if meter.remaining().any_lt(required) {
return Err(SteppedMigrationError::InsufficientWeight { required });
}
loop {
// Check that we would have enough weight to perform this step in the worst case
// scenario.
let required_weight = match &cursor {
Some(state) => Self::required_weight(&state),
// Worst case weight for `authority_step`.
None => T::WeightInfo::migration_v2_authority_step(),
};
if !meter.can_consume(required_weight) {
break;
}
let next = match &cursor {
// At first, migrate any authorities.
None => Self::authority_step(None),
// Migrate any remaining authorities.
Some(MigrationState::Authority(maybe_last_authority)) =>
Self::authority_step(Some(maybe_last_authority)),
// After the last authority was migrated, start migrating usernames from
// the former `AccountOfUsername` into `UsernameInfoOf`.
Some(MigrationState::FinishedAuthorities) => Self::username_step(None),
// Keep migrating usernames.
Some(MigrationState::Username(maybe_last_username)) =>
Self::username_step(Some(maybe_last_username)),
// After the last username was migrated, start migrating all identities in
// `IdentityOf`, which currently hold the primary username of the owner account
// as well as any associated identity. Accounts which set a username but not an
// identity also have a zero deposit identity stored, which will be removed.
Some(MigrationState::FinishedUsernames) => Self::identity_step(None),
// Keep migrating identities.
Some(MigrationState::Identity(last_key)) =>
Self::identity_step(Some(last_key.clone())),
// After the last identity was migrated, start migrating usernames pending
// approval from `PendingUsernames`.
Some(MigrationState::FinishedIdentities) => Self::pending_username_step(None),
// Keep migrating pending usernames.
Some(MigrationState::PendingUsername(last_key)) =>
Self::pending_username_step(Some(last_key.clone())),
// After the last pending username was migrated, start clearing the storage
// previously associated with authorities in `UsernameAuthority`.
Some(MigrationState::FinishedPendingUsernames) =>
Self::cleanup_authority_step(None),
// Keep clearing the obsolete authority storage.
Some(MigrationState::CleanupAuthorities(maybe_last_username)) =>
Self::cleanup_authority_step(Some(maybe_last_username)),
// After the last obsolete authority was cleared from storage, start clearing
// the storage previously associated with usernames in `AccountOfUsername`.
Some(MigrationState::FinishedCleanupAuthorities) =>
Self::cleanup_username_step(None),
// Keep clearing the obsolete username storage.
Some(MigrationState::CleanupUsernames(maybe_last_username)) =>
Self::cleanup_username_step(Some(maybe_last_username)),
// After the last obsolete username was cleared from storage, the migration is
// done.
Some(MigrationState::Finished) => {
StorageVersion::new(Self::id().version_to as u16).put::<Pallet<T>>();
return Ok(None);
},
};
cursor = Some(next);
meter.consume(required_weight);
}
Ok(cursor)
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, pezsp_runtime::TryRuntimeError> {
let authorities: BTreeMap<Suffix<T>, (T::AccountId, u32)> =
types_v1::UsernameAuthorities::<T>::iter()
.map(|(account, authority_properties)| {
(
authority_properties.account_id,
(account, authority_properties.allocation),
)
})
.collect();
let mut primary_usernames: BTreeMap<_, _> = Default::default();
let identities = types_v1::IdentityOf::<T>::iter()
.map(|(account, (identity, maybe_username))| {
if let Some(username) = maybe_username {
primary_usernames.insert(account.clone(), username);
}
(account, identity)
})
.collect::<BTreeMap<_, _>>();
let usernames = types_v1::AccountOfUsername::<T>::iter().collect::<BTreeMap<_, _>>();
let pending_usernames: BTreeMap<Username<T>, (T::AccountId, BlockNumberFor<T>)> =
types_v1::PendingUsernames::<T>::iter().collect();
let state: TryRuntimeState<T> = TryRuntimeState {
authorities,
identities,
primary_usernames,
usernames,
pending_usernames,
};
Ok(state.encode())
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), pezsp_runtime::TryRuntimeError> {
let mut prev_state: TryRuntimeState<T> = TryRuntimeState::<T>::decode(&mut &state[..])
.expect("Failed to decode the previous storage state");
for (suffix, authority_properties) in AuthorityOf::<T>::iter() {
let (prev_account, prev_allocation) = prev_state
.authorities
.remove(&suffix)
.expect("should have authority in previous state");
assert_eq!(prev_account, authority_properties.account_id);
assert_eq!(prev_allocation, authority_properties.allocation);
}
assert!(prev_state.authorities.is_empty());
for (account, identity) in IdentityOf::<T>::iter() {
assert!(identity.deposit > 0u32.into());
let prev_identity = prev_state
.identities
.remove(&account)
.expect("should have identity in previous state");
assert_eq!(identity, prev_identity);
}
for (account, free_identity) in prev_state.identities.iter() {
assert_eq!(free_identity.deposit, 0u32.into());
assert!(UsernameOf::<T>::contains_key(&account));
}
prev_state.identities.clear();
for (account, primary_username) in UsernameOf::<T>::iter() {
let prev_primary_username = prev_state
.primary_usernames
.remove(&account)
.expect("should have primary username in previous state");
assert_eq!(prev_primary_username, primary_username);
}
for (username, username_info) in UsernameInfoOf::<T>::iter() {
let prev_account = prev_state
.usernames
.remove(&username)
.expect("should have username info in previous state");
assert_eq!(prev_account, username_info.owner);
assert_eq!(username_info.provider, Provider::Allocation);
}
assert!(prev_state.usernames.is_empty());
for (username, (account, expiration, provider)) in PendingUsernames::<T>::iter() {
let (prev_account, prev_expiration) = prev_state
.pending_usernames
.remove(&username)
.expect("should have pending username in previous state");
assert_eq!(prev_account, account);
assert_eq!(prev_expiration, expiration);
assert_eq!(provider, Provider::Allocation);
}
assert!(prev_state.pending_usernames.is_empty());
Ok(())
}
}
impl<T: Config> LazyMigrationV1ToV2<T> {
pub(crate) fn required_weight(
step: &MigrationState<T::AccountId, Username<T>, Suffix<T>>,
) -> Weight {
match step {
MigrationState::Authority(_) => T::WeightInfo::migration_v2_authority_step(),
MigrationState::FinishedAuthorities | MigrationState::Username(_) =>
T::WeightInfo::migration_v2_username_step(),
MigrationState::FinishedUsernames | MigrationState::Identity(_) =>
T::WeightInfo::migration_v2_identity_step(),
MigrationState::FinishedIdentities | MigrationState::PendingUsername(_) =>
T::WeightInfo::migration_v2_pending_username_step(),
MigrationState::FinishedPendingUsernames |
MigrationState::CleanupAuthorities(_) => T::WeightInfo::migration_v2_cleanup_authority_step(),
MigrationState::FinishedCleanupAuthorities |
MigrationState::CleanupUsernames(_) => T::WeightInfo::migration_v2_cleanup_username_step(),
MigrationState::Finished => Weight::zero(),
}
}
// Migrate one entry from `UsernameAuthorities` to `AuthorityOf`.
pub(crate) fn authority_step(maybe_last_key: Option<&T::AccountId>) -> StepResultOf<T> {
let mut iter = if let Some(last_key) = maybe_last_key {
types_v1::UsernameAuthorities::<T>::iter_from(
types_v1::UsernameAuthorities::<T>::hashed_key_for(last_key),
)
} else {
types_v1::UsernameAuthorities::<T>::iter()
};
if let Some((authority_account, properties)) = iter.next() {
let suffix = properties.account_id;
let allocation = properties.allocation;
let new_properties =
AuthorityProperties { account_id: authority_account.clone(), allocation };
AuthorityOf::<T>::insert(&suffix, new_properties);
MigrationState::Authority(authority_account)
} else {
MigrationState::FinishedAuthorities
}
}
// Migrate one entry from `AccountOfUsername` to `UsernameInfoOf`.
pub(crate) fn username_step(maybe_last_key: Option<&Username<T>>) -> StepResultOf<T> {
let mut iter = if let Some(last_key) = maybe_last_key {
types_v1::AccountOfUsername::<T>::iter_from(
types_v1::AccountOfUsername::<T>::hashed_key_for(last_key),
)
} else {
types_v1::AccountOfUsername::<T>::iter()
};
if let Some((username, owner_account)) = iter.next() {
let username_info = UsernameInformation {
owner: owner_account,
provider: Provider::new_with_allocation(),
};
UsernameInfoOf::<T>::insert(&username, username_info);
MigrationState::Username(username)
} else {
MigrationState::FinishedUsernames
}
}
// Migrate one entry from `IdentityOf` to `UsernameOf`, if it has a username associated with
// it. Remove the entry if there was no real identity associated with the account.
pub(crate) fn identity_step(maybe_last_key: Option<HashedKey>) -> StepResultOf<T> {
if let Some(mut last_key) =
IdentityOf::<T>::translate_next::<
(
Registration<
BalanceOf<T>,
<T as pallet::Config>::MaxRegistrars,
<T as pallet::Config>::IdentityInformation,
>,
Option<Username<T>>,
),
_,
>(maybe_last_key.map(|b| b.to_vec()), |account, (identity, maybe_username)| {
if let Some(primary_username) = maybe_username {
UsernameOf::<T>::insert(&account, primary_username);
}
if identity.deposit > BalanceOf::<T>::zero() {
Some(identity)
} else {
None
}
}) {
last_key.truncate(HashedKey::bound());
MigrationState::Identity(
HashedKey::try_from(last_key)
.expect("truncated to bound so the conversion must succeed; qed"),
)
} else {
MigrationState::FinishedIdentities
}
}
// Migrate one entry from `PendingUsernames` to contain the new `Provider` field.
pub(crate) fn pending_username_step(maybe_last_key: Option<HashedKey>) -> StepResultOf<T> {
if let Some(mut last_key) =
PendingUsernames::<T>::translate_next::<(T::AccountId, BlockNumberFor<T>), _>(
maybe_last_key.map(|b| b.to_vec()),
|_, (owner_account, since)| {
Some((owner_account, since, Provider::new_with_allocation()))
},
) {
last_key.truncate(HashedKey::bound());
MigrationState::PendingUsername(
HashedKey::try_from(last_key)
.expect("truncated to bound so the conversion must succeed; qed"),
)
} else {
MigrationState::FinishedPendingUsernames
}
}
// Remove one entry from `UsernameAuthorities`.
pub(crate) fn cleanup_authority_step(
maybe_last_key: Option<&Suffix<T>>,
) -> StepResultOf<T> {
let mut iter = if let Some(last_key) = maybe_last_key {
AuthorityOf::<T>::iter_from(AuthorityOf::<T>::hashed_key_for(last_key))
} else {
AuthorityOf::<T>::iter()
};
if let Some((suffix, properties)) = iter.next() {
let _ = types_v1::UsernameAuthorities::<T>::take(&properties.account_id);
MigrationState::CleanupAuthorities(suffix)
} else {
MigrationState::FinishedCleanupAuthorities
}
}
// Remove one entry from `AccountOfUsername`.
pub(crate) fn cleanup_username_step(
maybe_last_key: Option<&Username<T>>,
) -> StepResultOf<T> {
let mut iter = if let Some(last_key) = maybe_last_key {
UsernameInfoOf::<T>::iter_from(UsernameInfoOf::<T>::hashed_key_for(last_key))
} else {
UsernameInfoOf::<T>::iter()
};
if let Some((username, _)) = iter.next() {
let _ = types_v1::AccountOfUsername::<T>::take(&username);
MigrationState::CleanupUsernames(username)
} else {
MigrationState::Finished
}
}
}
#[cfg(feature = "runtime-benchmarks")]
pub(crate) struct BenchmarkingSetup<S, A, U> {
pub(crate) suffix: S,
pub(crate) authority: A,
pub(crate) account: A,
pub(crate) username: U,
}
#[cfg(feature = "runtime-benchmarks")]
impl<T: Config> LazyMigrationV1ToV2<T> {
pub(crate) fn setup_benchmark_env_for_migration() -> BenchmarkingSetupOf<T> {
use pezframe_support::Hashable;
let suffix: Suffix<T> = b"bench".to_vec().try_into().unwrap();
let authority: T::AccountId = pezframe_benchmarking::account("authority", 0, 0);
let account_id: T::AccountId = pezframe_benchmarking::account("account", 1, 0);
let prop: AuthorityProperties<Suffix<T>> =
AuthorityProperties { account_id: suffix.clone(), allocation: 10 };
types_v1::UsernameAuthorities::<T>::insert(&authority, &prop);
let username: Username<T> = b"account.bench".to_vec().try_into().unwrap();
let info = T::IdentityInformation::create_identity_info();
let registration: Registration<
BalanceOf<T>,
<T as Config>::MaxRegistrars,
<T as Config>::IdentityInformation,
> = Registration { judgements: Default::default(), deposit: 10u32.into(), info };
pezframe_support::migration::put_storage_value(
b"Identity",
b"IdentityOf",
&account_id.twox_64_concat(),
(&registration, Some(username.clone())),
);
types_v1::AccountOfUsername::<T>::insert(&username, &account_id);
let since: BlockNumberFor<T> = 0u32.into();
pezframe_support::migration::put_storage_value(
b"Identity",
b"PendingUsernames",
&username.blake2_128_concat(),
(&account_id, since),
);
BenchmarkingSetup { suffix, authority, account: account_id, username }
}
pub(crate) fn setup_benchmark_env_for_cleanup() -> BenchmarkingSetupOf<T> {
let suffix: Suffix<T> = b"bench".to_vec().try_into().unwrap();
let authority: T::AccountId = pezframe_benchmarking::account("authority", 0, 0);
let account_id: T::AccountId = pezframe_benchmarking::account("account", 1, 0);
let prop: AuthorityProperties<Suffix<T>> =
AuthorityProperties { account_id: suffix.clone(), allocation: 10 };
types_v1::UsernameAuthorities::<T>::insert(&authority, &prop);
let prop: AuthorityProperties<T::AccountId> =
AuthorityProperties { account_id: authority.clone(), allocation: 10 };
AuthorityOf::<T>::insert(&suffix, &prop);
let username: Username<T> = b"account.bench".to_vec().try_into().unwrap();
let info = T::IdentityInformation::create_identity_info();
let registration: Registration<
BalanceOf<T>,
<T as Config>::MaxRegistrars,
<T as Config>::IdentityInformation,
> = Registration { judgements: Default::default(), deposit: 10u32.into(), info };
IdentityOf::<T>::insert(&account_id, &registration);
UsernameOf::<T>::insert(&account_id, &username);
let username_info = UsernameInformation {
owner: account_id.clone(),
provider: Provider::new_with_allocation(),
};
UsernameInfoOf::<T>::insert(&username, username_info);
types_v1::AccountOfUsername::<T>::insert(&username, &account_id);
let since: BlockNumberFor<T> = 0u32.into();
PendingUsernames::<T>::insert(
&username,
(&account_id, since, Provider::new_with_allocation()),
);
BenchmarkingSetup { suffix, authority, account: account_id, username }
}
pub(crate) fn check_authority_cleanup_validity(suffix: Suffix<T>, authority: T::AccountId) {
assert_eq!(types_v1::UsernameAuthorities::<T>::iter().count(), 0);
assert_eq!(AuthorityOf::<T>::get(&suffix).unwrap().account_id, authority);
}
pub(crate) fn check_username_cleanup_validity(
username: Username<T>,
account_id: T::AccountId,
) {
assert_eq!(types_v1::AccountOfUsername::<T>::iter().count(), 0);
assert_eq!(UsernameInfoOf::<T>::get(&username).unwrap().owner, account_id);
}
}
#[cfg(test)]
mod tests {
use pezframe_support::Hashable;
use super::*;
use crate::tests::{new_test_ext, Test};
fn registration(
with_deposit: bool,
) -> Registration<
BalanceOf<Test>,
<Test as Config>::MaxRegistrars,
<Test as Config>::IdentityInformation,
> {
Registration {
judgements: Default::default(),
deposit: if with_deposit { 10u32.into() } else { 0u32.into() },
info: Default::default(),
}
}
fn account_from_u8(byte: u8) -> <Test as pezframe_system::Config>::AccountId {
[byte; 32].into()
}
#[test]
fn migrate_to_v2() {
new_test_ext().execute_with(|| {
StorageVersion::new(1).put::<Pallet<Test>>();
// Set up the first authority.
let authority_1 = account_from_u8(151);
let suffix_1: Suffix<Test> = b"evn".to_vec().try_into().unwrap();
let prop = AuthorityProperties { account_id: suffix_1.clone(), allocation: 10 };
types_v1::UsernameAuthorities::<Test>::insert(&authority_1, &prop);
// Set up the first authority.
let authority_2 = account_from_u8(152);
let suffix_2: Suffix<Test> = b"odd".to_vec().try_into().unwrap();
let prop = AuthorityProperties { account_id: suffix_2.clone(), allocation: 10 };
types_v1::UsernameAuthorities::<Test>::insert(&authority_2, &prop);
// (owner_account, primary_username, maybe_secondary_username, has_identity)
// If `has_identity` is set, this `owner_account` will have a real identity
// associated and a non-zero deposit for it.
let mut usernames = vec![];
for i in 0u8..100u8 {
let account_id = account_from_u8(i);
let bare_username = format!("acc{}.", i).as_bytes().to_vec();
let mut username_1 = bare_username.clone();
username_1.extend(suffix_1.iter());
let username_1: Username<Test> = username_1.try_into().unwrap();
types_v1::AccountOfUsername::<Test>::insert(&username_1, &account_id);
if i % 2 == 0 {
let has_identity = i % 4 == 0;
let reg = registration(has_identity);
pezframe_support::migration::put_storage_value(
b"Identity",
b"IdentityOf",
&account_id.twox_64_concat(),
(reg, Some(username_1.clone())),
);
usernames.push((account_id, username_1, None, has_identity));
} else {
let has_identity = i % 3 == 0;
let mut username_2 = bare_username.clone();
username_2.extend(suffix_2.iter());
let username_2: Username<Test> = username_2.try_into().unwrap();
types_v1::AccountOfUsername::<Test>::insert(&username_2, &account_id);
let reg = registration(has_identity);
pezframe_support::migration::put_storage_value(
b"Identity",
b"IdentityOf",
&account_id.twox_64_concat(),
(reg, Some(username_2.clone())),
);
usernames.push((account_id, username_2, Some(username_1), has_identity));
}
}
// (username, owner_account, since)
let mut pending = vec![];
for i in 100u8..110u8 {
let account_id = account_from_u8(i);
let mut bare_username = format!("acc{}.", i).as_bytes().to_vec();
bare_username.extend(suffix_1.iter());
let username: Username<Test> = bare_username.try_into().unwrap();
let since: BlockNumberFor<Test> = i.into();
pezframe_support::migration::put_storage_value(
b"Identity",
b"PendingUsernames",
&username.blake2_128_concat(),
(&account_id, since),
);
pending.push((username, account_id, since));
}
let mut identity_only = vec![];
for i in 120u8..130u8 {
let account_id = account_from_u8(i);
let reg = registration(true);
pezframe_support::migration::put_storage_value(
b"Identity",
b"IdentityOf",
&account_id.twox_64_concat(),
(reg, None::<Username<Test>>),
);
identity_only.push(account_id);
}
// Run the actual migration.
let mut weight_meter = WeightMeter::new();
let mut cursor = None;
while let Some(new_cursor) =
LazyMigrationV1ToV2::<Test>::step(cursor, &mut weight_meter).unwrap()
{
cursor = Some(new_cursor);
}
assert_eq!(Pallet::<Test>::on_chain_storage_version(), 2);
// Check that the authorities were migrated.
let expected_prop =
AuthorityProperties { account_id: authority_1.clone(), allocation: 10 };
assert_eq!(AuthorityOf::<Test>::get(&suffix_1), Some(expected_prop));
let expected_prop =
AuthorityProperties { account_id: authority_2.clone(), allocation: 10 };
assert_eq!(AuthorityOf::<Test>::get(&suffix_2), Some(expected_prop));
// Check that the username information was migrated.
let count_of_usernames_without_identities =
usernames.iter().filter(|(_, _, _, has_id)| *has_id).count();
assert_eq!(UsernameOf::<Test>::iter().count(), usernames.len());
// All accounts have `evn` usernames, only half of them have `odd` usernames.
assert_eq!(
UsernameInfoOf::<Test>::iter().count(),
usernames.len() + usernames.len() / 2
);
for (owner, primary, maybe_secondary, has_identity) in usernames.iter() {
let username_info = UsernameInfoOf::<Test>::get(primary).unwrap();
assert_eq!(&username_info.owner, owner);
let actual_primary = UsernameOf::<Test>::get(owner).unwrap();
assert_eq!(primary, &actual_primary);
assert_eq!(IdentityOf::<Test>::contains_key(owner), *has_identity);
if let Some(secondary) = maybe_secondary {
let expected_info = UsernameInformation {
owner: owner.clone(),
provider: Provider::new_with_allocation(),
};
assert_eq!(UsernameInfoOf::<Test>::get(secondary), Some(expected_info));
}
}
// Check that existing identities were preserved.
for id in identity_only.iter() {
let expected_reg = registration(true);
assert_eq!(IdentityOf::<Test>::get(id), Some(expected_reg));
assert!(!UsernameOf::<Test>::contains_key(id));
}
let identity_count = IdentityOf::<Test>::iter().count();
assert_eq!(
identity_count,
count_of_usernames_without_identities + identity_only.len()
);
// Check that pending usernames were migrated.
let pending_count = PendingUsernames::<Test>::iter().count();
assert_eq!(pending_count, pending.len());
for (username, owner, since) in pending.iter() {
let expected_pending = (owner.clone(), *since, Provider::Allocation);
assert_eq!(PendingUsernames::<Test>::get(username), Some(expected_pending));
}
// Check that obsolete storage was cleared.
assert_eq!(types_v1::AccountOfUsername::<Test>::iter().count(), 0);
assert_eq!(types_v1::UsernameAuthorities::<Test>::iter().count(), 0);
});
}
}
}
File diff suppressed because it is too large Load Diff
+444
View File
@@ -0,0 +1,444 @@
// This file is part of Bizinikiwi.
// 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.
use super::*;
use alloc::{vec, vec::Vec};
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use core::{fmt::Debug, iter::once, ops::Add};
use pezframe_support::{
traits::{ConstU32, Get},
BoundedVec, CloneNoBound, PartialEqNoBound, RuntimeDebugNoBound,
};
use scale_info::{
build::{Fields, Variants},
Path, Type, TypeInfo,
};
use pezsp_runtime::{
traits::{Member, Zero},
RuntimeDebug,
};
/// An identifier for a single name registrar/identity verification service.
pub type RegistrarIndex = u32;
/// Either underlying data blob if it is at most 32 bytes, or a hash of it. If the data is greater
/// than 32-bytes then it will be truncated when encoding.
///
/// Can also be `None`.
#[derive(Clone, DecodeWithMemTracking, Eq, PartialEq, RuntimeDebug, MaxEncodedLen)]
pub enum Data {
/// No data here.
None,
/// The data is stored directly.
Raw(BoundedVec<u8, ConstU32<32>>),
/// Only the Blake2 hash of the data is stored. The preimage of the hash may be retrieved
/// through some hash-lookup service.
BlakeTwo256([u8; 32]),
/// Only the SHA2-256 hash of the data is stored. The preimage of the hash may be retrieved
/// through some hash-lookup service.
Sha256([u8; 32]),
/// Only the Keccak-256 hash of the data is stored. The preimage of the hash may be retrieved
/// through some hash-lookup service.
Keccak256([u8; 32]),
/// Only the SHA3-256 hash of the data is stored. The preimage of the hash may be retrieved
/// through some hash-lookup service.
ShaThree256([u8; 32]),
}
impl Data {
pub fn is_none(&self) -> bool {
self == &Data::None
}
}
impl Decode for Data {
fn decode<I: codec::Input>(input: &mut I) -> core::result::Result<Self, codec::Error> {
let b = input.read_byte()?;
Ok(match b {
0 => Data::None,
n @ 1..=33 => {
let mut r: BoundedVec<_, _> = vec![0u8; n as usize - 1]
.try_into()
.expect("bound checked in match arm condition; qed");
input.read(&mut r[..])?;
Data::Raw(r)
},
34 => Data::BlakeTwo256(<[u8; 32]>::decode(input)?),
35 => Data::Sha256(<[u8; 32]>::decode(input)?),
36 => Data::Keccak256(<[u8; 32]>::decode(input)?),
37 => Data::ShaThree256(<[u8; 32]>::decode(input)?),
_ => return Err(codec::Error::from("invalid leading byte")),
})
}
}
impl Encode for Data {
fn encode(&self) -> Vec<u8> {
match self {
Data::None => vec![0u8; 1],
Data::Raw(ref x) => {
let l = x.len().min(32);
let mut r = vec![l as u8 + 1; l + 1];
r[1..].copy_from_slice(&x[..l as usize]);
r
},
Data::BlakeTwo256(ref h) => once(34u8).chain(h.iter().cloned()).collect(),
Data::Sha256(ref h) => once(35u8).chain(h.iter().cloned()).collect(),
Data::Keccak256(ref h) => once(36u8).chain(h.iter().cloned()).collect(),
Data::ShaThree256(ref h) => once(37u8).chain(h.iter().cloned()).collect(),
}
}
}
impl codec::EncodeLike for Data {}
/// Add a Raw variant with the given index and a fixed sized byte array
macro_rules! data_raw_variants {
($variants:ident, $(($index:literal, $size:literal)),* ) => {
$variants
$(
.variant(concat!("Raw", stringify!($size)), |v| v
.index($index)
.fields(Fields::unnamed().field(|f| f.ty::<[u8; $size]>()))
)
)*
}
}
impl TypeInfo for Data {
type Identity = Self;
fn type_info() -> Type {
let variants = Variants::new().variant("None", |v| v.index(0));
// create a variant for all sizes of Raw data from 0-32
let variants = data_raw_variants!(
variants,
(1, 0),
(2, 1),
(3, 2),
(4, 3),
(5, 4),
(6, 5),
(7, 6),
(8, 7),
(9, 8),
(10, 9),
(11, 10),
(12, 11),
(13, 12),
(14, 13),
(15, 14),
(16, 15),
(17, 16),
(18, 17),
(19, 18),
(20, 19),
(21, 20),
(22, 21),
(23, 22),
(24, 23),
(25, 24),
(26, 25),
(27, 26),
(28, 27),
(29, 28),
(30, 29),
(31, 30),
(32, 31),
(33, 32)
);
let variants = variants
.variant("BlakeTwo256", |v| {
v.index(34).fields(Fields::unnamed().field(|f| f.ty::<[u8; 32]>()))
})
.variant("Sha256", |v| {
v.index(35).fields(Fields::unnamed().field(|f| f.ty::<[u8; 32]>()))
})
.variant("Keccak256", |v| {
v.index(36).fields(Fields::unnamed().field(|f| f.ty::<[u8; 32]>()))
})
.variant("ShaThree256", |v| {
v.index(37).fields(Fields::unnamed().field(|f| f.ty::<[u8; 32]>()))
});
Type::builder().path(Path::new("Data", module_path!())).variant(variants)
}
}
impl Default for Data {
fn default() -> Self {
Self::None
}
}
/// An attestation of a registrar over how accurate some `IdentityInfo` is in describing an account.
///
/// NOTE: Registrars may pay little attention to some fields. Registrars may want to make clear
/// which fields their attestation is relevant for by off-chain means.
#[derive(
Copy,
Clone,
Encode,
Decode,
DecodeWithMemTracking,
Eq,
PartialEq,
RuntimeDebug,
MaxEncodedLen,
TypeInfo,
)]
pub enum Judgement<Balance: Encode + Decode + MaxEncodedLen + Copy + Clone + Debug + Eq + PartialEq>
{
/// The default value; no opinion is held.
Unknown,
/// No judgement is yet in place, but a deposit is reserved as payment for providing one.
FeePaid(Balance),
/// The data appears to be reasonably acceptable in terms of its accuracy, however no in depth
/// checks (such as in-person meetings or formal KYC) have been conducted.
Reasonable,
/// The target is known directly by the registrar and the registrar can fully attest to the
/// the data's accuracy.
KnownGood,
/// The data was once good but is currently out of date. There is no malicious intent in the
/// inaccuracy. This judgement can be removed through updating the data.
OutOfDate,
/// The data is imprecise or of sufficiently low-quality to be problematic. It is not
/// indicative of malicious intent. This judgement can be removed through updating the data.
LowQuality,
/// The data is erroneous. This may be indicative of malicious intent. This cannot be removed
/// except by the registrar.
Erroneous,
}
impl<Balance: Encode + Decode + MaxEncodedLen + Copy + Clone + Debug + Eq + PartialEq>
Judgement<Balance>
{
/// Returns `true` if this judgement is indicative of a deposit being currently held. This means
/// it should not be cleared or replaced except by an operation which utilizes the deposit.
pub(crate) fn has_deposit(&self) -> bool {
matches!(self, Judgement::FeePaid(_))
}
/// Returns `true` if this judgement is one that should not be generally be replaced outside
/// of specialized handlers. Examples include "malicious" judgements and deposit-holding
/// judgements.
pub(crate) fn is_sticky(&self) -> bool {
matches!(self, Judgement::FeePaid(_) | Judgement::Erroneous)
}
}
/// Information concerning the identity of the controller of an account.
pub trait IdentityInformationProvider:
Encode + Decode + MaxEncodedLen + Clone + Debug + Eq + PartialEq + TypeInfo + Default
{
/// Type capable of holding information on which identity fields are set.
type FieldsIdentifier: Member + Encode + Decode + MaxEncodedLen + TypeInfo + Default;
/// Check if an identity registered information for some given `fields`.
fn has_identity(&self, fields: Self::FieldsIdentifier) -> bool;
/// Create a basic instance of the identity information.
#[cfg(feature = "runtime-benchmarks")]
fn create_identity_info() -> Self;
/// The identity information representation for all identity fields enabled.
#[cfg(feature = "runtime-benchmarks")]
fn all_fields() -> Self::FieldsIdentifier;
}
/// Information on an identity along with judgements from registrars.
///
/// NOTE: This is stored separately primarily to facilitate the addition of extra fields in a
/// backwards compatible way through a specialized `Decode` impl.
#[derive(
CloneNoBound, Encode, Eq, MaxEncodedLen, PartialEqNoBound, RuntimeDebugNoBound, TypeInfo,
)]
#[codec(mel_bound())]
#[scale_info(skip_type_params(MaxJudgements))]
pub struct Registration<
Balance: Encode + Decode + MaxEncodedLen + Copy + Clone + Debug + Eq + PartialEq,
MaxJudgements: Get<u32>,
IdentityInfo: IdentityInformationProvider,
> {
/// Judgements from the registrars on this identity. Stored ordered by `RegistrarIndex`. There
/// may be only a single judgement from each registrar.
pub judgements: BoundedVec<(RegistrarIndex, Judgement<Balance>), MaxJudgements>,
/// Amount held on deposit for this information.
pub deposit: Balance,
/// Information on the identity.
pub info: IdentityInfo,
}
impl<
Balance: Encode + Decode + MaxEncodedLen + Copy + Clone + Debug + Eq + PartialEq + Zero + Add,
MaxJudgements: Get<u32>,
IdentityInfo: IdentityInformationProvider,
> Registration<Balance, MaxJudgements, IdentityInfo>
{
pub(crate) fn total_deposit(&self) -> Balance {
self.deposit +
self.judgements
.iter()
.map(|(_, ref j)| if let Judgement::FeePaid(fee) = j { *fee } else { Zero::zero() })
.fold(Zero::zero(), |a, i| a + i)
}
}
impl<
Balance: Encode + Decode + MaxEncodedLen + Copy + Clone + Debug + Eq + PartialEq,
MaxJudgements: Get<u32>,
IdentityInfo: IdentityInformationProvider,
> Decode for Registration<Balance, MaxJudgements, IdentityInfo>
{
fn decode<I: codec::Input>(input: &mut I) -> core::result::Result<Self, codec::Error> {
let (judgements, deposit, info) = Decode::decode(&mut AppendZerosInput::new(input))?;
Ok(Self { judgements, deposit, info })
}
}
/// Information concerning a registrar.
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, MaxEncodedLen, TypeInfo)]
pub struct RegistrarInfo<
Balance: Encode + Decode + Clone + Debug + Eq + PartialEq,
AccountId: Encode + Decode + Clone + Debug + Eq + PartialEq,
IdField: Encode + Decode + Clone + Debug + Default + Eq + PartialEq + TypeInfo + MaxEncodedLen,
> {
/// The account of the registrar.
pub account: AccountId,
/// Amount required to be given to the registrar for them to provide judgement.
pub fee: Balance,
/// Relevant fields for this registrar. Registrar judgements are limited to attestations on
/// these fields.
pub fields: IdField,
}
/// The number of usernames that an authority may allocate.
type Allocation = u32;
/// A byte vec used to represent a username.
pub(crate) type Suffix<T> = BoundedVec<u8, <T as Config>::MaxSuffixLength>;
/// Properties of a username authority.
#[derive(Clone, Encode, Decode, MaxEncodedLen, TypeInfo, PartialEq, Debug)]
pub struct AuthorityProperties<Account> {
/// The account of the authority.
pub account_id: Account,
/// The number of usernames remaining that this authority can grant.
pub allocation: Allocation,
}
/// A byte vec used to represent a username.
pub(crate) type Username<T> = BoundedVec<u8, <T as Config>::MaxUsernameLength>;
#[derive(Clone, Encode, Decode, MaxEncodedLen, TypeInfo, PartialEq, Debug)]
pub enum Provider<Balance> {
Allocation,
AuthorityDeposit(Balance),
System,
}
impl<Balance> Provider<Balance> {
pub fn new_with_allocation() -> Self {
Self::Allocation
}
pub fn new_with_deposit(deposit: Balance) -> Self {
Self::AuthorityDeposit(deposit)
}
#[allow(unused)]
pub fn new_permanent() -> Self {
Self::System
}
}
#[derive(Clone, Encode, Decode, MaxEncodedLen, TypeInfo, PartialEq, Debug)]
pub struct UsernameInformation<Account, Balance> {
pub owner: Account,
pub provider: Provider<Balance>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn manual_data_type_info() {
let mut registry = scale_info::Registry::new();
let type_id = registry.register_type(&scale_info::meta_type::<Data>());
let registry: scale_info::PortableRegistry = registry.into();
let type_info = registry.resolve(type_id.id).unwrap();
let check_type_info = |data: &Data| {
let variant_name = match data {
Data::None => "None".to_string(),
Data::BlakeTwo256(_) => "BlakeTwo256".to_string(),
Data::Sha256(_) => "Sha256".to_string(),
Data::Keccak256(_) => "Keccak256".to_string(),
Data::ShaThree256(_) => "ShaThree256".to_string(),
Data::Raw(bytes) => format!("Raw{}", bytes.len()),
};
if let scale_info::TypeDef::Variant(variant) = &type_info.type_def {
let variant = variant
.variants
.iter()
.find(|v| v.name == variant_name)
.expect(&format!("Expected to find variant {}", variant_name));
let field_arr_len = variant
.fields
.first()
.and_then(|f| registry.resolve(f.ty.id))
.map(|ty| {
if let scale_info::TypeDef::Array(arr) = &ty.type_def {
arr.len
} else {
panic!("Should be an array type")
}
})
.unwrap_or(0);
let encoded = data.encode();
assert_eq!(encoded[0], variant.index);
assert_eq!(encoded.len() as u32 - 1, field_arr_len);
} else {
panic!("Should be a variant type")
};
};
let mut data = vec![
Data::None,
Data::BlakeTwo256(Default::default()),
Data::Sha256(Default::default()),
Data::Keccak256(Default::default()),
Data::ShaThree256(Default::default()),
];
// A Raw instance for all possible sizes of the Raw data
for n in 0..32 {
data.push(Data::Raw(vec![0u8; n as usize].try_into().unwrap()))
}
for d in data.iter() {
check_type_info(d);
}
}
}
File diff suppressed because it is too large Load Diff