From 09454c6708679b6fe5bdaf31fed408c80eb39de7 Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Fri, 24 Jan 2025 13:42:47 +0100
Subject: [PATCH] move offchain config to common

---
 runtime/common/src/lib.rs      |  1 +
 runtime/common/src/offchain.rs | 48 ++++++++++++++++++++++++++++++++++
 runtime/g1/src/lib.rs          | 48 ++++------------------------------
 runtime/gdev/src/lib.rs        | 46 ++++----------------------------
 runtime/gtest/src/lib.rs       | 48 ++++------------------------------
 5 files changed, 64 insertions(+), 127 deletions(-)
 create mode 100644 runtime/common/src/offchain.rs

diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs
index 6ec87850..ea297185 100644
--- a/runtime/common/src/lib.rs
+++ b/runtime/common/src/lib.rs
@@ -22,6 +22,7 @@ pub mod constants;
 pub mod entities;
 pub mod fees;
 pub mod handlers;
+mod offchain;
 mod pallets_config;
 pub mod providers;
 
diff --git a/runtime/common/src/offchain.rs b/runtime/common/src/offchain.rs
new file mode 100644
index 00000000..aa857c51
--- /dev/null
+++ b/runtime/common/src/offchain.rs
@@ -0,0 +1,48 @@
+// Copyright 2021 Axiom-Team
+//
+// This file is part of Duniter-v2S.
+//
+// Duniter-v2S is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, version 3 of the License.
+//
+// Duniter-v2S is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.
+
+#[macro_export]
+macro_rules! offchain_config {
+    () => {
+        impl<LocalCall> frame_system::offchain::CreateTransaction<LocalCall> for Runtime
+        where
+            RuntimeCall: From<LocalCall>,
+        {
+            type Extension = TxExtension;
+
+            fn create_transaction(call: RuntimeCall, extension: TxExtension) -> UncheckedExtrinsic {
+                generic::UncheckedExtrinsic::new_transaction(call, extension)
+            }
+        }
+
+        impl<LocalCall> frame_system::offchain::CreateInherent<LocalCall> for Runtime
+        where
+            RuntimeCall: From<LocalCall>,
+        {
+            fn create_inherent(call: RuntimeCall) -> UncheckedExtrinsic {
+                generic::UncheckedExtrinsic::new_bare(call)
+            }
+        }
+
+        impl<LocalCall> frame_system::offchain::CreateTransactionBase<LocalCall> for Runtime
+        where
+            RuntimeCall: From<LocalCall>,
+        {
+            type Extrinsic = UncheckedExtrinsic;
+            type RuntimeCall = RuntimeCall;
+        }
+    };
+}
diff --git a/runtime/g1/src/lib.rs b/runtime/g1/src/lib.rs
index b448658a..91f34c21 100644
--- a/runtime/g1/src/lib.rs
+++ b/runtime/g1/src/lib.rs
@@ -73,8 +73,6 @@ pub use frame_support::{
     StorageValue,
 };
 
-common_runtime::declare_session_keys! {}
-
 // To learn more about runtime versioning and what each of the following value means:
 //   https://substrate.dev/docs/en/knowledgebase/runtime/upgrades#runtime-versioning
 #[sp_version::runtime_version]
@@ -131,8 +129,6 @@ pub type Executive = frame_executive::Executive<
 
 pub type TechnicalCommitteeInstance = Instance2;
 
-common_runtime::benchmarks_config!();
-
 pub struct BaseCallFilter;
 impl Contains<RuntimeCall> for BaseCallFilter {
     fn contains(call: &RuntimeCall) -> bool {
@@ -193,9 +189,6 @@ impl frame_support::traits::InstanceFilter<RuntimeCall> for ProxyType {
     }
 }
 
-// Configure pallets to include in runtime.
-common_runtime::pallets_config!();
-
 // Create the runtime by composing the pallets that were previously configured.
 construct_runtime!(
     pub enum Runtime
@@ -252,42 +245,11 @@ construct_runtime!(
     }
 );
 
-impl<LocalCall> frame_system::offchain::CreateTransaction<LocalCall> for Runtime
-where
-    RuntimeCall: From<LocalCall>,
-{
-    type Extension = TxExtension;
-
-    fn create_transaction(call: RuntimeCall, extension: TxExtension) -> UncheckedExtrinsic {
-        generic::UncheckedExtrinsic::new_transaction(call, extension)
-    }
-}
-
-impl<LocalCall> frame_system::offchain::CreateInherent<LocalCall> for Runtime
-where
-    RuntimeCall: From<LocalCall>,
-{
-    fn create_inherent(call: RuntimeCall) -> UncheckedExtrinsic {
-        generic::UncheckedExtrinsic::new_bare(call)
-    }
-}
-
-impl<C> frame_system::offchain::CreateTransactionBase<C> for Runtime
-where
-    RuntimeCall: From<C>,
-{
-    type Extrinsic = UncheckedExtrinsic;
-    type RuntimeCall = RuntimeCall;
-}
-
 // All of our runtimes share most of their Runtime API implementations.
 // We use a macro to implement this common part and add runtime-specific additional implementations.
-// This macro expands to :
-// ```
-// impl_runtime_apis! {
-//     // All impl blocks shared between all runtimes.
-//
-//     // Specific impls provided to the `runtime_apis!` macro.
-// }
-// ```
+common_runtime::pallets_config!();
+common_runtime::declare_session_keys! {}
+#[cfg(feature = "runtime-benchmarks")]
+common_runtime::benchmarks_config!();
+common_runtime::offchain_config! {}
 common_runtime::runtime_apis! {}
diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs
index 7d2b5a2c..71d340ec 100644
--- a/runtime/gdev/src/lib.rs
+++ b/runtime/gdev/src/lib.rs
@@ -73,8 +73,6 @@ pub use frame_support::{
     StorageValue,
 };
 
-common_runtime::declare_session_keys! {}
-
 // To learn more about runtime versioning and what each of the following value means:
 //   https://substrate.dev/docs/en/knowledgebase/runtime/upgrades#runtime-versioning
 #[sp_version::runtime_version]
@@ -132,8 +130,6 @@ pub type Executive = frame_executive::Executive<
 
 pub type TechnicalCommitteeInstance = Instance2;
 
-common_runtime::benchmarks_config!();
-
 pub struct BaseCallFilter;
 impl Contains<RuntimeCall> for BaseCallFilter {
     fn contains(call: &RuntimeCall) -> bool {
@@ -235,7 +231,6 @@ impl pallet_duniter_test_parameters::Config for Runtime {
     type PeriodCount = Balance;
     type SessionCount = u32;
 }
-common_runtime::pallets_config!();
 
 // Create the runtime by composing the pallets that were previously configured.
 construct_runtime!(
@@ -295,42 +290,11 @@ construct_runtime!(
     }
 );
 
-impl<LocalCall> frame_system::offchain::CreateTransaction<LocalCall> for Runtime
-where
-    RuntimeCall: From<LocalCall>,
-{
-    type Extension = TxExtension;
-
-    fn create_transaction(call: RuntimeCall, extension: TxExtension) -> UncheckedExtrinsic {
-        generic::UncheckedExtrinsic::new_transaction(call, extension)
-    }
-}
-
-impl<LocalCall> frame_system::offchain::CreateInherent<LocalCall> for Runtime
-where
-    RuntimeCall: From<LocalCall>,
-{
-    fn create_inherent(call: RuntimeCall) -> UncheckedExtrinsic {
-        generic::UncheckedExtrinsic::new_bare(call)
-    }
-}
-
-impl<C> frame_system::offchain::CreateTransactionBase<C> for Runtime
-where
-    RuntimeCall: From<C>,
-{
-    type Extrinsic = UncheckedExtrinsic;
-    type RuntimeCall = RuntimeCall;
-}
-
 // All of our runtimes share most of their Runtime API implementations.
 // We use a macro to implement this common part and add runtime-specific additional implementations.
-// This macro expands to :
-// ```
-// impl_runtime_apis! {
-//     // All impl blocks shared between all runtimes.
-//
-//     // Specific impls provided to the `runtime_apis!` macro.
-// }
-// ```
+common_runtime::pallets_config!();
+common_runtime::declare_session_keys! {}
+#[cfg(feature = "runtime-benchmarks")]
+common_runtime::benchmarks_config!();
+common_runtime::offchain_config! {}
 common_runtime::runtime_apis! {}
diff --git a/runtime/gtest/src/lib.rs b/runtime/gtest/src/lib.rs
index 518e9b83..6876b754 100644
--- a/runtime/gtest/src/lib.rs
+++ b/runtime/gtest/src/lib.rs
@@ -72,8 +72,6 @@ pub use frame_support::{
     StorageValue,
 };
 
-common_runtime::declare_session_keys! {}
-
 // To learn more about runtime versioning and what each of the following value means:
 //   https://substrate.dev/docs/en/knowledgebase/runtime/upgrades#runtime-versioning
 #[sp_version::runtime_version]
@@ -131,8 +129,6 @@ pub type Executive = frame_executive::Executive<
 
 pub type TechnicalCommitteeInstance = Instance2;
 
-common_runtime::benchmarks_config!();
-
 pub struct BaseCallFilter;
 impl Contains<RuntimeCall> for BaseCallFilter {
     fn contains(call: &RuntimeCall) -> bool {
@@ -200,9 +196,6 @@ impl frame_support::traits::InstanceFilter<RuntimeCall> for ProxyType {
     }
 }
 
-// Configure pallets to include in runtime.
-common_runtime::pallets_config!();
-
 // Create the runtime by composing the pallets that were previously configured.
 construct_runtime!(
     pub enum Runtime
@@ -259,42 +252,11 @@ construct_runtime!(
     }
 );
 
-impl<LocalCall> frame_system::offchain::CreateTransaction<LocalCall> for Runtime
-where
-    RuntimeCall: From<LocalCall>,
-{
-    type Extension = TxExtension;
-
-    fn create_transaction(call: RuntimeCall, extension: TxExtension) -> UncheckedExtrinsic {
-        generic::UncheckedExtrinsic::new_transaction(call, extension)
-    }
-}
-
-impl<LocalCall> frame_system::offchain::CreateInherent<LocalCall> for Runtime
-where
-    RuntimeCall: From<LocalCall>,
-{
-    fn create_inherent(call: RuntimeCall) -> UncheckedExtrinsic {
-        generic::UncheckedExtrinsic::new_bare(call)
-    }
-}
-
-impl<C> frame_system::offchain::CreateTransactionBase<C> for Runtime
-where
-    RuntimeCall: From<C>,
-{
-    type Extrinsic = UncheckedExtrinsic;
-    type RuntimeCall = RuntimeCall;
-}
-
 // All of our runtimes share most of their Runtime API implementations.
 // We use a macro to implement this common part and add runtime-specific additional implementations.
-// This macro expands to :
-// ```
-// impl_runtime_apis! {
-//     // All impl blocks shared between all runtimes.
-//
-//     // Specific impls provided to the `runtime_apis!` macro.
-// }
-// ```
+common_runtime::pallets_config!();
+common_runtime::declare_session_keys! {}
+#[cfg(feature = "runtime-benchmarks")]
+common_runtime::benchmarks_config!();
+common_runtime::offchain_config! {}
 common_runtime::runtime_apis! {}
-- 
GitLab