From 5ae3c8d0e2fd92c10f2a9ed21d2ca3d5fb794e71 Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Tue, 18 Jan 2022 01:07:04 +0100
Subject: [PATCH] feat(cert): add genesis param apply_cert_period_at_genesis

---
 node/src/chain_spec/gdev.rs        |  1 +
 node/src/chain_spec/gtest.rs       |  2 ++
 pallets/certification/src/lib.rs   | 18 +++++++++++-------
 pallets/certification/src/tests.rs |  4 ++++
 4 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/node/src/chain_spec/gdev.rs b/node/src/chain_spec/gdev.rs
index 133dddd49..31dc65ae8 100644
--- a/node/src/chain_spec/gdev.rs
+++ b/node/src/chain_spec/gdev.rs
@@ -140,6 +140,7 @@ fn devnet_genesis(
                 .collect(),
         },
         strong_cert: StrongCertConfig {
+            apply_cert_period_at_genesis: false,
             certs_by_issuer: clique_wot(
                 initial_identities.len(),
                 gdev_runtime::parameters::ValidityPeriod::get(),
diff --git a/node/src/chain_spec/gtest.rs b/node/src/chain_spec/gtest.rs
index 803554474..f601d2221 100644
--- a/node/src/chain_spec/gtest.rs
+++ b/node/src/chain_spec/gtest.rs
@@ -128,6 +128,7 @@ fn devnet_genesis(
                 .collect(),
         },
         strong_cert: StrongCertConfig {
+            apply_cert_period_at_genesis: false,
             certs_by_issuer: clique_wot(
                 initial_identities.len(),
                 gtest_runtime::parameters::ValidityPeriod::get(),
@@ -333,6 +334,7 @@ fn testnet_genesis(
                 .collect(),
         },
         strong_cert: StrongCertConfig {
+            apply_cert_period_at_genesis: false,
             certs_by_issuer: clique_wot(
                 initial_identities.len(),
                 gtest_runtime::parameters::ValidityPeriod::get(),
diff --git a/pallets/certification/src/lib.rs b/pallets/certification/src/lib.rs
index 3462440a0..2636ad729 100644
--- a/pallets/certification/src/lib.rs
+++ b/pallets/certification/src/lib.rs
@@ -39,6 +39,7 @@ pub mod pallet {
     use super::*;
     use frame_support::pallet_prelude::*;
     use frame_system::pallet_prelude::*;
+    use sp_runtime::traits::Saturating;
     use sp_std::collections::btree_map::BTreeMap;
 
     /// The current storage version.
@@ -89,6 +90,7 @@ pub mod pallet {
 
     #[pallet::genesis_config]
     pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
+        pub apply_cert_period_at_genesis: bool,
         pub certs_by_issuer: BTreeMap<T::IdtyIndex, BTreeMap<T::IdtyIndex, T::BlockNumber>>,
     }
 
@@ -96,6 +98,7 @@ pub mod pallet {
     impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
         fn default() -> Self {
             Self {
+                apply_cert_period_at_genesis: false,
                 certs_by_issuer: Default::default(),
             }
         }
@@ -152,13 +155,14 @@ pub mod pallet {
                         .or_default()
                         .push((*issuer, *receiver));
 
-                    use sp_runtime::traits::Saturating as _;
-                    let issuer_next_issuable_on = removable_on
-                        .saturating_add(T::CertPeriod::get())
-                        .saturating_sub(T::ValidityPeriod::get());
-                    if let Some(cert_meta) = cert_meta_by_issuer.get_mut(issuer) {
-                        if cert_meta.next_issuable_on < issuer_next_issuable_on {
-                            cert_meta.next_issuable_on = issuer_next_issuable_on;
+                    if self.apply_cert_period_at_genesis {
+                        let issuer_next_issuable_on = removable_on
+                            .saturating_add(T::CertPeriod::get())
+                            .saturating_sub(T::ValidityPeriod::get());
+                        if let Some(cert_meta) = cert_meta_by_issuer.get_mut(issuer) {
+                            if cert_meta.next_issuable_on < issuer_next_issuable_on {
+                                cert_meta.next_issuable_on = issuer_next_issuable_on;
+                            }
                         }
                     }
                     let renewable_on = removable_on.saturating_sub(
diff --git a/pallets/certification/src/tests.rs b/pallets/certification/src/tests.rs
index 7c50f5768..9c870412e 100644
--- a/pallets/certification/src/tests.rs
+++ b/pallets/certification/src/tests.rs
@@ -25,6 +25,7 @@ use sp_std::collections::btree_map::BTreeMap;
 #[test]
 fn test_must_receive_cert_before_can_issue() {
     new_test_ext(DefaultCertificationConfig {
+		apply_cert_period_at_genesis: true,
         certs_by_issuer: BTreeMap::new(),
     })
     .execute_with(|| {
@@ -38,6 +39,7 @@ fn test_must_receive_cert_before_can_issue() {
 #[test]
 fn test_genesis_build() {
     new_test_ext(DefaultCertificationConfig {
+		apply_cert_period_at_genesis: true,
         certs_by_issuer: btreemap![
             0 => btreemap![
                 1 => 10,
@@ -111,6 +113,7 @@ fn test_genesis_build() {
 #[test]
 fn test_cert_period() {
     new_test_ext(DefaultCertificationConfig {
+		apply_cert_period_at_genesis: true,
         certs_by_issuer: btreemap![0 => btreemap![1 => 10]],
     })
     .execute_with(|| {
@@ -133,6 +136,7 @@ fn test_cert_period() {
 #[test]
 fn test_renewable_period() {
     new_test_ext(DefaultCertificationConfig {
+		apply_cert_period_at_genesis: true,
         certs_by_issuer: btreemap![0 => btreemap![1 => 10]],
     })
     .execute_with(|| {
-- 
GitLab