From 08beaa3364d46d039a76e507e8044a4fa10b0076 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo@trentesaux.fr>
Date: Fri, 23 Sep 2022 00:02:02 +0200
Subject: [PATCH] wip replace identity removal check

---
 pallets/duniter-wot/src/lib.rs | 11 ++++++++---
 pallets/identity/src/lib.rs    |  7 +------
 pallets/identity/src/traits.rs |  8 ++++----
 3 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/pallets/duniter-wot/src/lib.rs b/pallets/duniter-wot/src/lib.rs
index 525d63bb0..14883a236 100644
--- a/pallets/duniter-wot/src/lib.rs
+++ b/pallets/duniter-wot/src/lib.rs
@@ -105,6 +105,8 @@ pub mod pallet {
         MaxEmitedCertsReached,
         /// Not allowed to change identity address
         NotAllowedToChangeIdtyAddress,
+        /// Not allowed to remove identity
+        NotAllowedToRemoveIdty,
     }
 }
 
@@ -178,11 +180,14 @@ where
             Ok(())
         }
     }
-    fn can_remove_identity(idty_index: IdtyIndex) -> bool {
+    fn check_remove_identity(idty_index: IdtyIndex) -> Result<(), DispatchError> {
         if T::IsSubWot::get() {
-            !pallet_membership::Pallet::<T, I>::is_member(&idty_index)
+            match pallet_membership::Pallet::<T, I>::is_member(&idty_index) {
+                true => Err(Error::<T, I>::NotAllowedToRemoveIdty.into()),
+                false => Ok(()),
+            }
         } else {
-            true
+            Ok(())
         }
     }
 }
diff --git a/pallets/identity/src/lib.rs b/pallets/identity/src/lib.rs
index 32be08e4b..0c91a9546 100644
--- a/pallets/identity/src/lib.rs
+++ b/pallets/identity/src/lib.rs
@@ -498,10 +498,7 @@ pub mod pallet {
                 Error::<T>::InvalidRevocationKey
             );
 
-            ensure!(
-                T::EnsureIdtyCallAllowed::can_remove_identity(idty_index),
-                Error::<T>::NotAllowedToRemoveIdty
-            );
+            T::EnsureIdtyCallAllowed::check_remove_identity(idty_index)?;
 
             let genesis_hash = frame_system::Pallet::<T>::block_hash(T::BlockNumber::zero());
             let revocation_payload = RevocationPayload {
@@ -601,8 +598,6 @@ pub mod pallet {
         InvalidRevocationKey,
         /// Revocation payload signature is invalid
         InvalidRevocationSig,
-        /// Not allowed to remove identity
-        NotAllowedToRemoveIdty,
         /// Identity creation period is not respected
         NotRespectIdtyCreationPeriod,
         /// Not the same identity name
diff --git a/pallets/identity/src/traits.rs b/pallets/identity/src/traits.rs
index e4a20190f..181dec6cc 100644
--- a/pallets/identity/src/traits.rs
+++ b/pallets/identity/src/traits.rs
@@ -24,7 +24,7 @@ pub trait EnsureIdtyCallAllowed<T: Config> {
     fn check_confirm_identity(idty_index: T::IdtyIndex) -> Result<(), DispatchError>;
     fn check_validate_identity(idty_index: T::IdtyIndex) -> Result<(), DispatchError>;
     fn check_change_identity_address(idty_index: T::IdtyIndex) -> Result<(), DispatchError>;
-    fn can_remove_identity(idty_index: T::IdtyIndex) -> bool;
+    fn check_remove_identity(idty_index: T::IdtyIndex) -> Result<(), DispatchError>;
 }
 
 #[impl_for_tuples(5)]
@@ -46,9 +46,9 @@ impl<T: Config> EnsureIdtyCallAllowed<T> for Tuple {
         for_tuples!( #( Tuple::check_change_identity_address(idty_index)?; )* );
         Ok(())
     }
-    fn can_remove_identity(idty_index: T::IdtyIndex) -> bool {
-        for_tuples!( #( if !Tuple::can_remove_identity(idty_index) { return false; } )* );
-        true
+    fn check_remove_identity(idty_index: T::IdtyIndex) -> Result<(), DispatchError> {
+        for_tuples!( #( Tuple::check_remove_identity(idty_index)?; )* );
+        Ok(())
     }
 }
 
-- 
GitLab