Skip to content

Commit

Permalink
IdentitySetter
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnn committed Apr 12, 2024
1 parent 7a4cc5a commit b0a9b03
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 9 additions & 3 deletions libs/utils/src/identity_provider.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use sp_runtime::DispatchResult;

/// Provides methods to work with an account's identity.
/// Provides methods to retrieve an account's identity.
pub trait IdentityProvider<T: frame_system::Config> {
/// Stored account's identity.
type Identity: Default;
type Identity;

/// Returns identity for the supplied account [if it exists].
fn identity(who: &T::AccountId) -> Option<Self::Identity>;
Expand All @@ -12,9 +12,15 @@ pub trait IdentityProvider<T: frame_system::Config> {
fn has_identity(who: &T::AccountId) -> bool {
Self::identity(who).is_some()
}
}

/// Provides methods to set an account's identity.
pub trait IdentitySetter<T: frame_system::Config> {
/// Idenity information to be associated with the account.
type IdentityInfo: Default;

/// Attempts to set identity for the account.
fn set_identity(account: T::AccountId, identity: Self::Identity) -> DispatchResult;
fn set_identity(account: T::AccountId, identity: Self::IdentityInfo) -> DispatchResult;

/// Attempts to remove identity of the account.
fn remove_identity(account: &T::AccountId) -> DispatchResult;
Expand Down
10 changes: 9 additions & 1 deletion substrate/frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ use sp_runtime::{
DispatchError, Perbill, RuntimeDebug,
};
use sp_std::{cmp::Ordering, prelude::*};
use utils::IdentityProvider;
use utils::*;

mod benchmarking;
pub mod weights;
Expand Down Expand Up @@ -296,7 +296,10 @@ pub mod pallet {
type WeightInfo: WeightInfo;

/// Verifies account submitted as a candidate.
#[cfg(not(any(test, feature = "runtime-benchmarks")))]
type CandidateIdentityProvider: IdentityProvider<Self>;
#[cfg(any(test, feature = "runtime-benchmarks"))]
type CandidateIdentityProvider: IdentityProvider<Self> + IdentitySetter<Self>;
}

#[pallet::hooks]
Expand Down Expand Up @@ -1325,6 +1328,7 @@ mod tests {
BuildStorage, DispatchResult,
};
use substrate_test_utils::assert_eq_uvec;
use utils::IdentitySetter;

parameter_types! {
pub BlockWeights: frame_system::limits::BlockWeights =
Expand Down Expand Up @@ -1455,6 +1459,10 @@ mod tests {
fn identity(account: &AccountId) -> Option<Self::Identity> {
ValidCandidates::<Test>::get(account)
}
}

impl IdentitySetter<Test> for CandidateIdentityProvider<Test> {
type IdentityInfo = ();

fn set_identity(account: AccountId, (): ()) -> DispatchResult {
ValidCandidates::<Test>::insert(account, ());
Expand Down

0 comments on commit b0a9b03

Please sign in to comment.