From f82560874293cf28228f19cf51964152e04be49c Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Fri, 21 Jan 2022 20:55:46 +0100
Subject: [PATCH] feat(gdev): make param babe.epochDuration dynamic

---
 node/src/chain_spec/gdev.rs                | 6 ++++--
 pallets/authority-members/src/lib.rs       | 4 ++--
 pallets/duniter-test-parameters/src/lib.rs | 1 +
 runtime/common/src/apis.rs                 | 1 +
 runtime/gdev/src/lib.rs                    | 1 +
 runtime/gdev/src/parameters.rs             | 4 +---
 6 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/node/src/chain_spec/gdev.rs b/node/src/chain_spec/gdev.rs
index 56a250e97..e8f1b157f 100644
--- a/node/src/chain_spec/gdev.rs
+++ b/node/src/chain_spec/gdev.rs
@@ -67,7 +67,7 @@ pub fn development_chain_spec() -> Result<ChainSpec, String> {
         "gdev",
         ChainType::Development,
         move || {
-            devnet_genesis(
+            gen_genesis_conf(
                 wasm_binary,
                 // Initial authorities len
                 1,
@@ -101,7 +101,7 @@ pub fn development_chain_spec() -> Result<ChainSpec, String> {
     ))
 }
 
-fn devnet_genesis(
+fn gen_genesis_conf(
     wasm_binary: &[u8],
     initial_authorities_len: usize,
     initial_smiths_len: usize,
@@ -113,6 +113,7 @@ fn devnet_genesis(
     assert!(initial_smiths_len <= initial_identities_len);
     assert!(initial_authorities_len <= initial_smiths_len);
 
+    let babe_epoch_duration = get_env_u32("DUNITER_BABE_EPOCH_DURATION", 600) as u64;
     let cert_validity_period = get_env_u32("DUNITER_CERT_VALIDITY_PERIOD", 1_000);
     let membership_period = get_env_u32("DUNITER_MEMBERSHIP_PERIOD", 1_000);
     let membership_renewable_period = get_env_u32("DUNITER_MEMBERSHIP_RENEWABLE_PERIOD", 50);
@@ -140,6 +141,7 @@ fn devnet_genesis(
         },
         parameters: ParametersConfig {
             parameters: GenesisParameters {
+                babe_epoch_duration,
                 cert_period: 15,
                 cert_max_by_issuer: 10,
                 cert_min_received_cert_to_issue_cert: 2,
diff --git a/pallets/authority-members/src/lib.rs b/pallets/authority-members/src/lib.rs
index 44a26d949..6510b2926 100644
--- a/pallets/authority-members/src/lib.rs
+++ b/pallets/authority-members/src/lib.rs
@@ -234,10 +234,10 @@ pub mod pallet {
                 return Err(Error::<T>::SessionKeysNotProvided.into());
             }
 
-			if Self::is_incoming(member_id) {
+            if Self::is_incoming(member_id) {
                 return Err(Error::<T>::AlreadyIncoming.into());
             }
-			let is_outgoing = Self::is_outgoing(member_id);
+            let is_outgoing = Self::is_outgoing(member_id);
             if Self::is_online(member_id) && !is_outgoing {
                 return Err(Error::<T>::AlreadyOnline.into());
             }
diff --git a/pallets/duniter-test-parameters/src/lib.rs b/pallets/duniter-test-parameters/src/lib.rs
index 3f310c552..4f5df4b75 100644
--- a/pallets/duniter-test-parameters/src/lib.rs
+++ b/pallets/duniter-test-parameters/src/lib.rs
@@ -36,6 +36,7 @@ pub mod types {
         CertCount: Default + Parameter,
         PeriodCount: Default + Parameter,
     > {
+        pub babe_epoch_duration: PeriodCount,
         pub cert_period: BlockNumber,
         pub cert_max_by_issuer: CertCount,
         pub cert_min_received_cert_to_issue_cert: CertCount,
diff --git a/runtime/common/src/apis.rs b/runtime/common/src/apis.rs
index 88dd18485..41edbb02f 100644
--- a/runtime/common/src/apis.rs
+++ b/runtime/common/src/apis.rs
@@ -33,6 +33,7 @@ macro_rules! runtime_apis {
 					// slot duration and expected target block time, for safely
 					// resisting network delays of maximum two seconds.
 					// <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results>
+					use frame_support::traits::Get as _;
 					sp_consensus_babe::BabeGenesisConfiguration {
 						slot_duration: Babe::slot_duration(),
 						epoch_length: EpochDuration::get(),
diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs
index 26e05354e..a26e98bc7 100644
--- a/runtime/gdev/src/lib.rs
+++ b/runtime/gdev/src/lib.rs
@@ -150,6 +150,7 @@ impl frame_support::traits::Contains<Call> for BaseCallFilter {
 // Configure FRAME pallets to include in runtime.
 common_runtime::pallets_config! {
     // Dynamic parameters
+    pub type EpochDuration = pallet_duniter_test_parameters::BabeEpochDuration<Runtime>;
     pub type CertPeriod = pallet_duniter_test_parameters::CertPeriod<Runtime>;
     pub type MaxByIssuer = pallet_duniter_test_parameters::CertMaxByIssuer<Runtime>;
     pub type MinReceivedCertToBeAbleToIssueCert = pallet_duniter_test_parameters::CertMinReceivedCertToIssueCert<Runtime>;
diff --git a/runtime/gdev/src/parameters.rs b/runtime/gdev/src/parameters.rs
index 83ed87e36..95a18530c 100644
--- a/runtime/gdev/src/parameters.rs
+++ b/runtime/gdev/src/parameters.rs
@@ -51,11 +51,9 @@ parameter_types! {
 }
 
 // Babe
-pub const EPOCH_DURATION_IN_SLOTS: BlockNumber = HOURS;
 parameter_types! {
-    pub const EpochDuration: u64 = EPOCH_DURATION_IN_SLOTS as u64;
     pub const ExpectedBlockTime: u64 = MILLISECS_PER_BLOCK;
-    pub const ReportLongevity: u64 = 168 * EpochDuration::get();
+    pub const ReportLongevity: u64 = 168 * HOURS as u64;
 }
 
 // ImOnline
-- 
GitLab