From 6c8fa3d13e681e78462d61870fe54298e7396c6a Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Sat, 15 Jan 2022 18:16:16 +0100
Subject: [PATCH] feat(runtime): add param FirstIssuableOn

---
 pallets/certification/src/lib.rs     | 31 ++++++++++++++++++++--------
 pallets/certification/src/traits.rs  |  7 +++++++
 runtime/common/src/handlers.rs       | 25 ++++++++++++++++++----
 runtime/common/src/pallets_config.rs |  2 +-
 runtime/g1/src/parameters.rs         |  3 ++-
 runtime/gdev/src/lib.rs              |  9 --------
 runtime/gdev/src/parameters.rs       |  3 ++-
 runtime/gtest/src/parameters.rs      |  3 ++-
 8 files changed, 57 insertions(+), 26 deletions(-)

diff --git a/pallets/certification/src/lib.rs b/pallets/certification/src/lib.rs
index 6bdba417b..de8784028 100644
--- a/pallets/certification/src/lib.rs
+++ b/pallets/certification/src/lib.rs
@@ -30,7 +30,7 @@ pub use types::*;
 
 use crate::traits::*;
 use codec::Codec;
-use frame_support::traits::{Instance, StorageVersion};
+use frame_support::traits::StorageVersion;
 use sp_runtime::traits::AtLeast32BitUnsigned;
 use sp_std::{fmt::Debug, vec::Vec};
 
@@ -390,19 +390,19 @@ pub mod pallet {
             Self::remove_cert_inner(issuer, receiver, None);
             Ok(().into())
         }
-    }
-
-    // PUBLIC FUNCTIONS //
 
-    impl<T: Config<I>, I: Instance> Pallet<T, I> {
-        pub fn on_idty_removed(idty_index: T::IdtyIndex) -> Weight {
-            let mut total_weight: Weight = 0;
+        #[pallet::weight(0)]
+        pub fn remove_all_certs_received_by(
+            origin: OriginFor<T>,
+            idty_index: T::IdtyIndex,
+        ) -> DispatchResultWithPostInfo {
+            ensure_root(origin)?;
             if let Ok(issuers) = <StorageCertsByReceiver<T, I>>::try_get(idty_index) {
                 for issuer in issuers {
-                    total_weight += Self::remove_cert_inner(issuer, idty_index, None);
+                    Self::remove_cert_inner(issuer, idty_index, None);
                 }
             }
-            total_weight
+            Ok(().into())
         }
     }
 
@@ -491,3 +491,16 @@ impl<T: Config<I>, I: 'static> IsIdtyAllowedToCreateCert<T::IdtyIndex> for Palle
         }
     }
 }
+
+impl<T: Config<I>, I: 'static> SetNextIssuableOn<T::BlockNumber, T::IdtyIndex> for Pallet<T, I> {
+    fn set_next_issuable_on(
+        idty_index: T::IdtyIndex,
+        next_issuable_on: T::BlockNumber,
+    ) -> frame_support::pallet_prelude::Weight {
+        <StorageIdtyCertMeta<T, I>>::mutate_exists(idty_index, |cert_meta_opt| {
+            let cert_meta = cert_meta_opt.get_or_insert(IdtyCertMeta::default());
+            cert_meta.next_issuable_on = next_issuable_on;
+        });
+        0
+    }
+}
diff --git a/pallets/certification/src/traits.rs b/pallets/certification/src/traits.rs
index 0e195603f..e4ac1e7e0 100644
--- a/pallets/certification/src/traits.rs
+++ b/pallets/certification/src/traits.rs
@@ -57,3 +57,10 @@ impl<IdtyIndex> OnRemovedCert<IdtyIndex> for () {
         0
     }
 }
+
+pub trait SetNextIssuableOn<BlockNumber, IdtyIndex> {
+    fn set_next_issuable_on(
+        idty_index: IdtyIndex,
+        next_issuable_on: BlockNumber,
+    ) -> frame_support::dispatch::Weight;
+}
diff --git a/runtime/common/src/handlers.rs b/runtime/common/src/handlers.rs
index 47797dfdd..ce6f6753d 100644
--- a/runtime/common/src/handlers.rs
+++ b/runtime/common/src/handlers.rs
@@ -15,7 +15,9 @@
 // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.
 
 use crate::entities::IdtyRight;
+use crate::BlockNumber;
 use frame_support::instances::Instance1;
+use frame_support::pallet_prelude::Get;
 use frame_support::weights::Weight;
 use pallet_identity::traits::IdtyEvent;
 
@@ -72,17 +74,28 @@ impl<
 }
 
 pub struct OnNewStrongCertHandler<
+    FirstIssuableOn,
     Runtime,
+    SetNextIssuableOnImpl,
     const MIN_STRONG_CERT_FOR_UD: u32,
     const MIN_STRONG_CERT_FOR_STRONG_CERT: u32,
->(core::marker::PhantomData<Runtime>);
+>(core::marker::PhantomData<(FirstIssuableOn, Runtime, SetNextIssuableOnImpl)>);
 impl<
-        IdtyIndex,
-        Runtime: pallet_identity::Config<IdtyIndex = IdtyIndex, IdtyRight = IdtyRight>,
+        FirstIssuableOn: Get<BlockNumber>,
+        IdtyIndex: Copy,
+        Runtime: frame_system::Config<BlockNumber = BlockNumber>
+            + pallet_identity::Config<IdtyIndex = IdtyIndex, IdtyRight = IdtyRight>,
+        SetNextIssuableOnImpl: pallet_certification::traits::SetNextIssuableOn<BlockNumber, IdtyIndex>,
         const MIN_STRONG_CERT_FOR_UD: u32,
         const MIN_STRONG_CERT_FOR_STRONG_CERT: u32,
     > pallet_certification::traits::OnNewcert<IdtyIndex>
-    for OnNewStrongCertHandler<Runtime, MIN_STRONG_CERT_FOR_UD, MIN_STRONG_CERT_FOR_STRONG_CERT>
+    for OnNewStrongCertHandler<
+        FirstIssuableOn,
+        Runtime,
+        SetNextIssuableOnImpl,
+        MIN_STRONG_CERT_FOR_UD,
+        MIN_STRONG_CERT_FOR_STRONG_CERT,
+    >
 {
     fn on_new_cert(
         _issuer: IdtyIndex,
@@ -105,6 +118,10 @@ impl<
                 receiver,
                 IdtyRight::StrongCert,
             );
+            let _ = SetNextIssuableOnImpl::set_next_issuable_on(
+                receiver,
+                frame_system::Pallet::<Runtime>::block_number() + FirstIssuableOn::get(),
+            );
         }
         total_weight
     }
diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs
index eb86e90d0..77101f901 100644
--- a/runtime/common/src/pallets_config.rs
+++ b/runtime/common/src/pallets_config.rs
@@ -212,7 +212,7 @@ macro_rules! pallets_config {
             type IdtyIndex = IdtyIndex;
             type MaxByIssuer = MaxByIssuer;
             type OnNewcert =
-                OnNewStrongCertHandler<Runtime, MIN_STRONG_CERT_FOR_UD, MIN_STRONG_CERT_FOR_STRONG_CERT>;
+                OnNewStrongCertHandler<FirstIssuableOn, Runtime, StrongCert, MIN_STRONG_CERT_FOR_UD, MIN_STRONG_CERT_FOR_STRONG_CERT>;
             type OnRemovedCert = OnRemovedStrongCertHandler<Runtime, MIN_STRONG_CERT_FOR_UD>;
             type CertRenewablePeriod = StrongCertRenewablePeriod;
             type ValidityPeriod = ValidityPeriod;
diff --git a/runtime/g1/src/parameters.rs b/runtime/g1/src/parameters.rs
index 8019329e7..03a91519c 100644
--- a/runtime/g1/src/parameters.rs
+++ b/runtime/g1/src/parameters.rs
@@ -96,9 +96,10 @@ parameter_types! {
 pub const IDTY_CREATE_PERIOD: BlockNumber = 100;
 frame_support::parameter_types! {
     pub const ConfirmPeriod: BlockNumber = 3 * DAYS;
+    pub const FirstIssuableOn: BlockNumber = 30* DAYS;
+    pub const IdtyRenewablePeriod: BlockNumber = 6 * MONTHS;
     pub const MaxInactivityPeriod: BlockNumber = YEARS;
     pub const MaxNoRightPeriod: BlockNumber = YEARS;
-    pub const IdtyRenewablePeriod: BlockNumber = 6 * MONTHS;
     pub const ValidationPeriod: BlockNumber = YEARS;
 }
 
diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs
index 001fb85f1..e450fb271 100644
--- a/runtime/gdev/src/lib.rs
+++ b/runtime/gdev/src/lib.rs
@@ -199,15 +199,6 @@ common_runtime::runtime_apis! {
     impl sp_consensus_babe::BabeApi<Block> for Runtime {
         fn configuration() -> sp_consensus_babe::BabeGenesisConfiguration {
             unimplemented!()
-            // TODO: we should return a value or see how can force the client to not call this API
-            /*sp_consensus_babe::BabeGenesisConfiguration {
-                slot_duration: 0,
-                epoch_length: 0,
-                c: BABE_GENESIS_EPOCH_CONFIG.c,
-                genesis_authorities: vec![],
-                randomness: Babe::randomness(),
-                allowed_slots: BABE_GENESIS_EPOCH_CONFIG.allowed_slots,
-            }*/
         }
 
         fn current_epoch_start() -> sp_consensus_babe::Slot {
diff --git a/runtime/gdev/src/parameters.rs b/runtime/gdev/src/parameters.rs
index 6fd35e057..87ba20d7c 100644
--- a/runtime/gdev/src/parameters.rs
+++ b/runtime/gdev/src/parameters.rs
@@ -50,9 +50,10 @@ frame_support::parameter_types! {
 pub const IDTY_CREATE_PERIOD: BlockNumber = 100;
 frame_support::parameter_types! {
     pub const ConfirmPeriod: BlockNumber = 12 * HOURS;
+    pub const FirstIssuableOn: BlockNumber = 10;
+    pub const IdtyRenewablePeriod: BlockNumber = 6 * MONTHS;
     pub const MaxInactivityPeriod: BlockNumber = YEARS;
     pub const MaxNoRightPeriod: BlockNumber = YEARS;
-    pub const IdtyRenewablePeriod: BlockNumber = 6 * MONTHS;
     pub const ValidationPeriod: BlockNumber = 2 * MONTHS;
 }
 
diff --git a/runtime/gtest/src/parameters.rs b/runtime/gtest/src/parameters.rs
index 10abeb8ee..73dddb1b3 100644
--- a/runtime/gtest/src/parameters.rs
+++ b/runtime/gtest/src/parameters.rs
@@ -95,9 +95,10 @@ parameter_types! {
 pub const IDTY_CREATE_PERIOD: BlockNumber = 100;
 frame_support::parameter_types! {
     pub const ConfirmPeriod: BlockNumber = DAYS;
+    pub const FirstIssuableOn: BlockNumber = DAYS;
+    pub const IdtyRenewablePeriod: BlockNumber = 12 * DAYS;
     pub const MaxInactivityPeriod: BlockNumber = 73 * DAYS;
     pub const MaxNoRightPeriod: BlockNumber = 73 * DAYS;
-    pub const IdtyRenewablePeriod: BlockNumber = 12 * DAYS;
     pub const ValidationPeriod: BlockNumber = 73 * DAYS;
 }
 
-- 
GitLab