From 11cb408c3f5c4b80e494da216800afea8eb82432 Mon Sep 17 00:00:00 2001
From: tuxmain <tuxmain@zettascript.org>
Date: Wed, 20 Apr 2022 12:49:51 +0200
Subject: [PATCH] fix(certification): cannot certify self

---
 pallets/certification/src/lib.rs   | 15 +++++++++------
 pallets/certification/src/tests.rs | 26 ++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/pallets/certification/src/lib.rs b/pallets/certification/src/lib.rs
index 9801d83e9..b46f4eb88 100644
--- a/pallets/certification/src/lib.rs
+++ b/pallets/certification/src/lib.rs
@@ -260,22 +260,24 @@ pub mod pallet {
 
     #[pallet::error]
     pub enum Error<T, I = ()> {
+        /// An identity cannot certify itself
+        CannotCertifySelf,
         /// Certification non autorisée
         CertNotAllowed,
-        /// Issuer not found
-        IssuerNotFound,
         /// An identity must receive certifications before it can issue them.
         IdtyMustReceiveCertsBeforeCanIssue,
         /// This identity has already issued the maximum number of certifications
         IssuedTooManyCert,
-        /// Receiver not found
-        ReceiverNotFound,
+        /// Issuer not found
+        IssuerNotFound,
         /// Not enough certifications received
         NotEnoughCertReceived,
-        /// This certification has already been issued or renewed recently
-        NotRespectRenewablePeriod,
         /// This identity has already issued a certification too recently
         NotRespectCertPeriod,
+        /// This certification has already been issued or renewed recently
+        NotRespectRenewablePeriod,
+        /// Receiver not found
+        ReceiverNotFound,
     }
 
     #[pallet::hooks]
@@ -339,6 +341,7 @@ pub mod pallet {
             receiver: T::AccountId,
         ) -> DispatchResultWithPostInfo {
             let who = ensure_signed(origin)?;
+            ensure!(who != receiver, Error::<T, I>::CannotCertifySelf);
             let issuer = T::IdtyIndexOf::convert(who).ok_or(Error::<T, I>::IssuerNotFound)?;
             let receiver =
                 T::IdtyIndexOf::convert(receiver).ok_or(Error::<T, I>::ReceiverNotFound)?;
diff --git a/pallets/certification/src/tests.rs b/pallets/certification/src/tests.rs
index 25b32f01b..64b77fb2a 100644
--- a/pallets/certification/src/tests.rs
+++ b/pallets/certification/src/tests.rs
@@ -36,6 +36,32 @@ fn test_must_receive_cert_before_can_issue() {
     });
 }
 
+#[test]
+fn test_cannot_certify_self() {
+    new_test_ext(DefaultCertificationConfig {
+        apply_cert_period_at_genesis: true,
+        certs_by_issuer: btreemap![
+            1 => btreemap![
+                0 => 5,
+            ],
+            2 => btreemap![
+                0 => 5,
+            ],
+            3 => btreemap![
+                0 => 5,
+            ],
+        ],
+    })
+    .execute_with(|| {
+        run_to_block(2);
+
+        assert_eq!(
+            DefaultCertification::add_cert(Origin::signed(0), 0),
+            Err(Error::<Test, _>::CannotCertifySelf.into())
+        );
+    });
+}
+
 #[test]
 fn test_genesis_build() {
     new_test_ext(DefaultCertificationConfig {
-- 
GitLab