Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
288d351
WIP username refactor
georgepisaltu Aug 23, 2024
06f8472
Minor fixes
georgepisaltu Aug 26, 2024
ebdd61d
Fix test compilation
georgepisaltu Aug 26, 2024
e42fb1b
Fix a couple of tests
georgepisaltu Aug 26, 2024
cfa7406
All tests pass
georgepisaltu Aug 26, 2024
7d351ae
Add tests related to username deletion
georgepisaltu Aug 27, 2024
5119afe
Fix tests for accepting usernames
georgepisaltu Aug 27, 2024
6b64952
Add the rest of unit tests
georgepisaltu Aug 27, 2024
083b168
Fix benches for new changes to usernames
georgepisaltu Aug 28, 2024
6269619
Add initial migration code
georgepisaltu Aug 28, 2024
8e326af
Finish writing migration code
georgepisaltu Aug 29, 2024
4b3ee47
Add benchmarked weight for migration steps
georgepisaltu Aug 29, 2024
987fc4e
WIP fix test
georgepisaltu Aug 29, 2024
831c556
WIP some other stuff
georgepisaltu Aug 30, 2024
f28afed
WIP test almost fixed
georgepisaltu Aug 30, 2024
34611fb
Fix first part of migration
georgepisaltu Aug 30, 2024
ac22ee8
Fix compilation fail
georgepisaltu Aug 30, 2024
717439a
Fix tests
georgepisaltu Aug 30, 2024
2736a05
Finish migration
georgepisaltu Aug 30, 2024
db04e86
Get rid of benchmarks
georgepisaltu Sep 2, 2024
040cd62
Some renames
georgepisaltu Sep 2, 2024
36b474e
Update docs
georgepisaltu Sep 2, 2024
c33ff74
Merge remote-tracking branch 'upstream/master' into identity-username…
georgepisaltu Sep 2, 2024
7c5193a
Small refactoring
georgepisaltu Sep 4, 2024
3c0beaa
Change unbinding username to store expiry
georgepisaltu Sep 4, 2024
494b521
Fix various configs
georgepisaltu Sep 4, 2024
f697ad8
Merge remote-tracking branch 'upstream/master' into identity-username…
georgepisaltu Sep 4, 2024
0d6fdd7
Fix `IdentityVerifier` impls
georgepisaltu Sep 4, 2024
ec16e56
Update readme
georgepisaltu Sep 5, 2024
7dec7be
Merge remote-tracking branch 'upstream/master' into identity-username…
georgepisaltu Sep 5, 2024
4cf38d1
Fix compilation in identity migrator
georgepisaltu Sep 5, 2024
23c353b
Reintroduce benchmarks for migration
georgepisaltu Sep 10, 2024
1035f6f
Refactor storage alias types
georgepisaltu Sep 10, 2024
448c034
Add dummy weights
georgepisaltu Sep 10, 2024
0acaffd
Merge remote-tracking branch 'upstream/master' into identity-username…
georgepisaltu Sep 10, 2024
35bcc68
Small refactor
georgepisaltu Sep 11, 2024
54c67b6
Add prdoc
georgepisaltu Sep 11, 2024
7a33cd4
Merge remote-tracking branch 'upstream/master' into identity-username…
georgepisaltu Sep 11, 2024
7a9d48c
Enable migration
georgepisaltu Sep 11, 2024
4acddb0
Fix compilation
georgepisaltu Sep 11, 2024
a49211e
Manually set the storage version
georgepisaltu Sep 11, 2024
e44d6d2
Fix bench compile
georgepisaltu Sep 11, 2024
bb0153c
Actually enable MBMs for omitted runtimes
georgepisaltu Sep 12, 2024
b39175f
Merge branch 'master' into identity-username-refactor
Ank4n Sep 13, 2024
af85fc7
Remove obsolete dangling username call
georgepisaltu Sep 12, 2024
7fe2d82
Update docs
georgepisaltu Sep 16, 2024
c3d22fe
Truncate keys
georgepisaltu Sep 16, 2024
112d870
Merge remote-tracking branch 'upstream/master' into identity-username…
georgepisaltu Sep 26, 2024
a11ef4a
Add try runtime checks
georgepisaltu Sep 30, 2024
976e0ff
Fix docs
georgepisaltu Sep 30, 2024
a3ed0b1
Merge remote-tracking branch 'upstream/master' into identity-username…
georgepisaltu Sep 30, 2024
f31fdd6
Merge remote-tracking branch 'upstream/master' into identity-username…
georgepisaltu Oct 29, 2024
ae07a64
Update prdoc
georgepisaltu Oct 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor storage alias types
Signed-off-by: georgepisaltu <[email protected]>
  • Loading branch information
georgepisaltu committed Sep 10, 2024
commit 1035f6f400b85efd239945a8981a6a7e8d17bb8c
2 changes: 1 addition & 1 deletion substrate/frame/identity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ The username functionality implemented in this pallet is meant to be a user frie
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 governane of a specific amount of usernames to issue for free,
- 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).
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
//! account".
//!
//! To grant a username, a username authority can either:
//! - be given an allocation by governane of a specific amount of usernames to issue for free,
//! - 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)
Expand Down
160 changes: 77 additions & 83 deletions substrate/frame/identity/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

use super::*;
use frame_support::{
migrations::VersionedMigration, pallet_prelude::*, traits::UncheckedOnRuntimeUpgrade,
IterableStorageMap,
migrations::VersionedMigration, pallet_prelude::*, storage_alias,
traits::UncheckedOnRuntimeUpgrade, IterableStorageMap,
};

#[cfg(feature = "try-runtime")]
Expand All @@ -40,51 +40,68 @@ pub mod versioned {
>;
}

pub mod v1 {
/// The old identity types in v0.
mod types_v0 {
use super::*;
use frame_support::storage_alias;

/// The log target.
const TARGET: &'static str = "runtime::identity::migration::v1";
#[storage_alias]
pub type IdentityOf<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<T as frame_system::Config>::AccountId,
Registration<
BalanceOf<T>,
<T as pallet::Config>::MaxRegistrars,
<T as pallet::Config>::IdentityInformation,
>,
OptionQuery,
>;
}

/// The old identity type, useful in pre-upgrade.
mod v0 {
use super::*;
/// The old identity types in v1.
mod types_v1 {
use super::*;

#[storage_alias]
pub type IdentityOf<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<T as frame_system::Config>::AccountId,
#[storage_alias]
pub type IdentityOf<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<T as frame_system::Config>::AccountId,
(
Registration<
BalanceOf<T>,
<T as pallet::Config>::MaxRegistrars,
<T as pallet::Config>::IdentityInformation,
>,
OptionQuery,
>;
}
Option<Username<T>>,
),
OptionQuery,
>;

mod vx {
use super::*;
#[storage_alias]
pub type UsernameAuthorities<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<T as frame_system::Config>::AccountId,
AuthorityProperties<Suffix<T>>,
OptionQuery,
>;

#[storage_alias]
pub type IdentityOf<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<T as frame_system::Config>::AccountId,
(
Registration<
BalanceOf<T>,
<T as pallet::Config>::MaxRegistrars,
<T as pallet::Config>::IdentityInformation,
>,
Option<Username<T>>,
),
OptionQuery,
>;
}
#[storage_alias]
pub type AccountOfUsername<T: Config> = StorageMap<
Pallet<T>,
Blake2_128Concat,
Username<T>,
<T as frame_system::Config>::AccountId,
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
Expand All @@ -94,7 +111,7 @@ pub mod v1 {
impl<T: Config, const KL: u64> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateV0ToV1<T, KL> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
let identities = v0::IdentityOf::<T>::iter().count();
let identities = types_v0::IdentityOf::<T>::iter().count();
log::info!(
target: TARGET,
"pre-upgrade state contains '{}' identities.",
Expand All @@ -114,8 +131,8 @@ pub mod v1 {
let mut translated: u64 = 0;
let mut interrupted = false;

for (account, registration) in v0::IdentityOf::<T>::iter() {
vx::IdentityOf::<T>::insert(account, (registration, None::<Username<T>>));
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!(
Expand All @@ -139,7 +156,7 @@ pub mod v1 {
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 = IdentityOf::<T>::iter().count() as u64;
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.");
Expand All @@ -152,7 +169,6 @@ pub mod v2 {
use super::*;
use frame_support::{
migrations::{MigrationId, SteppedMigration, SteppedMigrationError},
storage_alias,
weights::WeightMeter,
};

Expand All @@ -165,28 +181,6 @@ pub mod v2 {
pub(crate) type BenchmarkingSetupOf<T> =
BenchmarkingSetup<Suffix<T>, <T as frame_system::Config>::AccountId, Username<T>>;

mod v1 {
use super::*;

#[storage_alias]
pub type UsernameAuthorities<T: Config> = StorageMap<
Pallet<T>,
Twox64Concat,
<T as frame_system::Config>::AccountId,
AuthorityProperties<Suffix<T>>,
OptionQuery,
>;

#[storage_alias]
pub type AccountOfUsername<T: Config> = StorageMap<
Pallet<T>,
Blake2_128Concat,
Username<T>,
<T as frame_system::Config>::AccountId,
OptionQuery,
>;
}

#[derive(Decode, Encode, MaxEncodedLen, Eq, PartialEq)]
pub enum MigrationState<A, U, S> {
Authority(A),
Expand Down Expand Up @@ -315,11 +309,11 @@ pub mod v2 {
// 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 {
v1::UsernameAuthorities::<T>::iter_from(
v1::UsernameAuthorities::<T>::hashed_key_for(last_key),
types_v1::UsernameAuthorities::<T>::iter_from(
types_v1::UsernameAuthorities::<T>::hashed_key_for(last_key),
)
} else {
v1::UsernameAuthorities::<T>::iter()
types_v1::UsernameAuthorities::<T>::iter()
};
if let Some((authority_account, properties)) = iter.next() {
let suffix = properties.account_id;
Expand All @@ -336,11 +330,11 @@ pub mod v2 {
// 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 {
v1::AccountOfUsername::<T>::iter_from(v1::AccountOfUsername::<T>::hashed_key_for(
last_key,
))
types_v1::AccountOfUsername::<T>::iter_from(
types_v1::AccountOfUsername::<T>::hashed_key_for(last_key),
)
} else {
v1::AccountOfUsername::<T>::iter()
types_v1::AccountOfUsername::<T>::iter()
};

if let Some((username, owner_account)) = iter.next() {
Expand Down Expand Up @@ -412,7 +406,7 @@ pub mod v2 {
};

if let Some((suffix, properties)) = iter.next() {
let _ = v1::UsernameAuthorities::<T>::take(&properties.account_id);
let _ = types_v1::UsernameAuthorities::<T>::take(&properties.account_id);
MigrationState::CleanupAuthorities(suffix)
} else {
MigrationState::FinishedCleanupAuthorities
Expand All @@ -430,7 +424,7 @@ pub mod v2 {
};

if let Some((username, _)) = iter.next() {
let _ = v1::AccountOfUsername::<T>::take(&username);
let _ = types_v1::AccountOfUsername::<T>::take(&username);
MigrationState::CleanupUsernames(username)
} else {
MigrationState::Finished
Expand All @@ -456,7 +450,7 @@ pub mod v2 {

let prop: AuthorityProperties<Suffix<T>> =
AuthorityProperties { account_id: suffix.clone(), allocation: 10 };
v1::UsernameAuthorities::<T>::insert(&authority, &prop);
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();
Expand All @@ -471,7 +465,7 @@ pub mod v2 {
&account_id.twox_64_concat(),
(&registration, Some(username.clone())),
);
v1::AccountOfUsername::<T>::insert(&username, &account_id);
types_v1::AccountOfUsername::<T>::insert(&username, &account_id);
let since: BlockNumberFor<T> = 0u32.into();
frame_support::migration::put_storage_value(
b"Identity",
Expand All @@ -489,7 +483,7 @@ pub mod v2 {

let prop: AuthorityProperties<Suffix<T>> =
AuthorityProperties { account_id: suffix.clone(), allocation: 10 };
v1::UsernameAuthorities::<T>::insert(&authority, &prop);
types_v1::UsernameAuthorities::<T>::insert(&authority, &prop);
let prop: AuthorityProperties<T::AccountId> =
AuthorityProperties { account_id: authority.clone(), allocation: 10 };
AuthorityOf::<T>::insert(&suffix, &prop);
Expand All @@ -508,7 +502,7 @@ pub mod v2 {
provider: Provider::new_with_allocation(),
};
UsernameInfoOf::<T>::insert(&username, username_info);
v1::AccountOfUsername::<T>::insert(&username, &account_id);
types_v1::AccountOfUsername::<T>::insert(&username, &account_id);
let since: BlockNumberFor<T> = 0u32.into();
PendingUsernames::<T>::insert(
&username,
Expand All @@ -518,15 +512,15 @@ pub mod v2 {
}

pub(crate) fn check_authority_cleanup_validity(suffix: Suffix<T>, authority: T::AccountId) {
assert_eq!(v1::UsernameAuthorities::<T>::iter().count(), 0);
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!(v1::AccountOfUsername::<T>::iter().count(), 0);
assert_eq!(types_v1::AccountOfUsername::<T>::iter().count(), 0);
assert_eq!(UsernameInfoOf::<T>::get(&username).unwrap().owner, account_id);
}
}
Expand Down Expand Up @@ -563,12 +557,12 @@ pub mod v2 {
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 };
v1::UsernameAuthorities::<Test>::insert(&authority_1, &prop);
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 };
v1::UsernameAuthorities::<Test>::insert(&authority_2, &prop);
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
Expand All @@ -580,7 +574,7 @@ pub mod v2 {
let mut username_1 = bare_username.clone();
username_1.extend(suffix_1.iter());
let username_1: Username<Test> = username_1.try_into().unwrap();
v1::AccountOfUsername::<Test>::insert(&username_1, &account_id);
types_v1::AccountOfUsername::<Test>::insert(&username_1, &account_id);

if i % 2 == 0 {
let has_identity = i % 4 == 0;
Expand All @@ -597,7 +591,7 @@ pub mod v2 {
let mut username_2 = bare_username.clone();
username_2.extend(suffix_2.iter());
let username_2: Username<Test> = username_2.try_into().unwrap();
v1::AccountOfUsername::<Test>::insert(&username_2, &account_id);
types_v1::AccountOfUsername::<Test>::insert(&username_2, &account_id);
let reg = registration(has_identity);
frame_support::migration::put_storage_value(
b"Identity",
Expand Down Expand Up @@ -702,8 +696,8 @@ pub mod v2 {
}

// Check that obsolete storage was cleared.
assert_eq!(v1::AccountOfUsername::<Test>::iter().count(), 0);
assert_eq!(v1::UsernameAuthorities::<Test>::iter().count(), 0);
assert_eq!(types_v1::AccountOfUsername::<Test>::iter().count(), 0);
assert_eq!(types_v1::UsernameAuthorities::<Test>::iter().count(), 0);
});
}
}
Expand Down