Skip to content

Commit

Permalink
activate & deactivate identity (#1906)
Browse files Browse the repository at this point in the history
* activate & deactivate identity

* fix & add batch tests

* return inactive identities in trusted call events

* [benchmarking bot] Auto commit generated weights files (#1909)

Co-authored-by: kziemianek <kziemianek@users.noreply.github.com>

* added weights

* remove log statement

* review suggestions

* missing activate_identity tc ts definition

* add activate_identity imp test

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: kziemianek <kziemianek@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 21, 2023
1 parent 6fdae03 commit 5a493f9
Show file tree
Hide file tree
Showing 45 changed files with 963 additions and 364 deletions.
39 changes: 33 additions & 6 deletions pallets/identity-management/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ benchmarks! {
assert_last_event::<T>(Event::LinkIdentityRequested{ shard }.into());
}

// Benchmark `remove_identity`. There are no worst conditions. The benchmark showed that
// Benchmark `deactivate_identity`. There are no worst conditions. The benchmark showed that
// execution time is constant irrespective of encrypted_data size.
remove_identity {
deactivate_identity {
let caller: T::AccountId = frame_benchmarking::account("TEST_A", 0u32, USER_SEED);
let shard = H256::from_slice(&TEST8_MRENCLAVE);
let encrypted_did = vec![1u8; 2048];
Expand All @@ -78,7 +78,22 @@ benchmarks! {
IdentityManagement::<T>::link_identity(RawOrigin::Signed(caller.clone()).into(), shard, caller.clone(), encrypted_did.clone(), encrypted_validation_data, encrypted_web3networks, nonce)?;
}: _(RawOrigin::Signed(caller), shard, encrypted_did)
verify {
assert_last_event::<T>(Event::RemoveIdentityRequested{ shard }.into());
assert_last_event::<T>(Event::DeactivateIdentityRequested{ shard }.into());
}

// Benchmark `activate_identity`. There are no worst conditions. The benchmark showed that
// execution time is constant irrespective of encrypted_data size.
activate_identity {
let caller: T::AccountId = frame_benchmarking::account("TEST_A", 0u32, USER_SEED);
let shard = H256::from_slice(&TEST8_MRENCLAVE);
let encrypted_did = vec![1u8; 2048];
let encrypted_validation_data = vec![1u8; 2048];
let encrypted_web3networks = vec![1u8; 2048];
let nonce = UserShieldingKeyNonceType::default();
IdentityManagement::<T>::link_identity(RawOrigin::Signed(caller.clone()).into(), shard, caller.clone(), encrypted_did.clone(), encrypted_validation_data, encrypted_web3networks, nonce)?;
}: _(RawOrigin::Signed(caller), shard, encrypted_did)
verify {
assert_last_event::<T>(Event::ActivateIdentityRequested{ shard }.into());
}

// Benchmark `set_user_shielding_key`. There are no worst conditions. The benchmark showed that
Expand Down Expand Up @@ -117,16 +132,28 @@ benchmarks! {
assert_last_event::<T>(Event::IdentityLinked { account, identity, id_graph, req_ext_hash }.into());
}

// Benchmark `identity_removed`. There are no worst conditions. The benchmark showed that
// Benchmark `identity_deactivated`. There are no worst conditions. The benchmark showed that
// execution time is constant irrespective of encrypted_data size.
identity_deactivated {
let req_ext_hash = H256::default();
let identity = AesOutput::default();
let call_origin = T::TEECallOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let account: T::AccountId = frame_benchmarking::account("TEST_A", 0u32, USER_SEED);
}: _<T::RuntimeOrigin>(call_origin, account.clone(), identity.clone(), req_ext_hash)
verify {
assert_last_event::<T>(Event::IdentityDeactivated { account, identity, req_ext_hash }.into());
}

// Benchmark `identity_activated`. There are no worst conditions. The benchmark showed that
// execution time is constant irrespective of encrypted_data size.
identity_removed {
identity_activated {
let req_ext_hash = H256::default();
let identity = AesOutput::default();
let call_origin = T::TEECallOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let account: T::AccountId = frame_benchmarking::account("TEST_A", 0u32, USER_SEED);
}: _<T::RuntimeOrigin>(call_origin, account.clone(), identity.clone(), req_ext_hash)
verify {
assert_last_event::<T>(Event::IdentityRemoved { account, identity, req_ext_hash }.into());
assert_last_event::<T>(Event::IdentityActivated { account, identity, req_ext_hash }.into());
}

// Benchmark `some_error`. There are no worst conditions. The benchmark showed that
Expand Down
69 changes: 57 additions & 12 deletions pallets/identity-management/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ pub mod pallet {
LinkIdentityRequested {
shard: ShardIdentifier,
},
RemoveIdentityRequested {
DeactivateIdentityRequested {
shard: ShardIdentifier,
},
ActivateIdentityRequested {
shard: ShardIdentifier,
},
SetUserShieldingKeyRequested {
Expand All @@ -106,7 +109,12 @@ pub mod pallet {
id_graph: AesOutput,
req_ext_hash: H256,
},
IdentityRemoved {
IdentityDeactivated {
account: T::AccountId,
identity: AesOutput,
req_ext_hash: H256,
},
IdentityActivated {
account: T::AccountId,
identity: AesOutput,
req_ext_hash: H256,
Expand All @@ -127,7 +135,12 @@ pub mod pallet {
detail: ErrorDetail,
req_ext_hash: H256,
},
RemoveIdentityFailed {
DeactivateIdentityFailed {
account: Option<T::AccountId>,
detail: ErrorDetail,
req_ext_hash: H256,
},
ActivateIdentityFailed {
account: Option<T::AccountId>,
detail: ErrorDetail,
req_ext_hash: H256,
Expand Down Expand Up @@ -232,16 +245,29 @@ pub mod pallet {
Ok(().into())
}

/// Remove an identity
/// Deactivate an identity
#[pallet::call_index(4)]
#[pallet::weight(<T as Config>::WeightInfo::remove_identity())]
pub fn remove_identity(
#[pallet::weight(<T as Config>::WeightInfo::deactivate_identity())]
pub fn deactivate_identity(
origin: OriginFor<T>,
shard: ShardIdentifier,
encrypted_identity: Vec<u8>,
) -> DispatchResultWithPostInfo {
let _ = T::ExtrinsicWhitelistOrigin::ensure_origin(origin)?;
Self::deposit_event(Event::RemoveIdentityRequested { shard });
Self::deposit_event(Event::DeactivateIdentityRequested { shard });
Ok(().into())
}

/// Activate an identity
#[pallet::call_index(5)]
#[pallet::weight(<T as Config>::WeightInfo::activate_identity())]
pub fn activate_identity(
origin: OriginFor<T>,
shard: ShardIdentifier,
encrypted_identity: Vec<u8>,
) -> DispatchResultWithPostInfo {
let _ = T::ExtrinsicWhitelistOrigin::ensure_origin(origin)?;
Self::deposit_event(Event::ActivateIdentityRequested { shard });
Ok(().into())
}

Expand Down Expand Up @@ -281,19 +307,32 @@ pub mod pallet {
}

#[pallet::call_index(32)]
#[pallet::weight(<T as Config>::WeightInfo::identity_removed())]
pub fn identity_removed(
#[pallet::weight(<T as Config>::WeightInfo::identity_deactivated())]
pub fn identity_deactivated(
origin: OriginFor<T>,
account: T::AccountId,
identity: AesOutput,
req_ext_hash: H256,
) -> DispatchResultWithPostInfo {
let _ = T::TEECallOrigin::ensure_origin(origin)?;
Self::deposit_event(Event::IdentityRemoved { account, identity, req_ext_hash });
Self::deposit_event(Event::IdentityDeactivated { account, identity, req_ext_hash });
Ok(Pays::No.into())
}

#[pallet::call_index(33)]
#[pallet::weight(<T as Config>::WeightInfo::identity_activated())]
pub fn identity_activated(
origin: OriginFor<T>,
account: T::AccountId,
identity: AesOutput,
req_ext_hash: H256,
) -> DispatchResultWithPostInfo {
let _ = T::TEECallOrigin::ensure_origin(origin)?;
Self::deposit_event(Event::IdentityActivated { account, identity, req_ext_hash });
Ok(Pays::No.into())
}

#[pallet::call_index(34)]
#[pallet::weight(<T as Config>::WeightInfo::some_error())]
pub fn some_error(
origin: OriginFor<T>,
Expand All @@ -311,8 +350,14 @@ pub mod pallet {
}),
IMPError::LinkIdentityFailed(detail) =>
Self::deposit_event(Event::LinkIdentityFailed { account, detail, req_ext_hash }),
IMPError::RemoveIdentityFailed(detail) =>
Self::deposit_event(Event::RemoveIdentityFailed {
IMPError::DeactivateIdentityFailed(detail) =>
Self::deposit_event(Event::DeactivateIdentityFailed {
account,
detail,
req_ext_hash,
}),
IMPError::ActivateIdentityFailed(detail) =>
Self::deposit_event(Event::ActivateIdentityFailed {
account,
detail,
req_ext_hash,
Expand Down
22 changes: 19 additions & 3 deletions pallets/identity-management/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,33 @@ fn link_identity_with_unauthorized_delegatee_fails() {
}

#[test]
fn remove_identity_works() {
fn deactivate_identity_works() {
new_test_ext().execute_with(|| {
let alice: SystemAccountId = test_utils::get_signer(ALICE_PUBKEY);
let shard: ShardIdentifier = H256::from_slice(&TEST8_MRENCLAVE);
assert_ok!(IdentityManagement::remove_identity(
assert_ok!(IdentityManagement::deactivate_identity(
RuntimeOrigin::signed(alice),
shard,
vec![1u8; 2048]
));
System::assert_last_event(RuntimeEvent::IdentityManagement(
crate::Event::RemoveIdentityRequested { shard },
crate::Event::DeactivateIdentityRequested { shard },
));
});
}

#[test]
fn activate_identity_works() {
new_test_ext().execute_with(|| {
let alice: SystemAccountId = test_utils::get_signer(ALICE_PUBKEY);
let shard: ShardIdentifier = H256::from_slice(&TEST8_MRENCLAVE);
assert_ok!(IdentityManagement::activate_identity(
RuntimeOrigin::signed(alice),
shard,
vec![1u8; 2048]
));
System::assert_last_event(RuntimeEvent::IdentityManagement(
crate::Event::ActivateIdentityRequested { shard },
));
});
}
Expand Down
Loading

0 comments on commit 5a493f9

Please sign in to comment.