From 2e943e2b7f4cf66f72d595b9b01b909ef02d8c39 Mon Sep 17 00:00:00 2001
From: Benjamin Gallois <business@gallois.cc>
Date: Mon, 22 May 2023 18:04:39 +0200
Subject: [PATCH] Add check that benchmark run on appropriate genesis
 (nodes/rust/duniter-v2s!170)

* fix(runtime) fix benchmarks for g1 and gtest

* feat(runtime) enforce gdev-benchmark for benchmark commands
---
 node/src/command.rs      | 15 +++++++++++++++
 runtime/g1/src/lib.rs    | 19 +++++++++++++------
 runtime/gtest/src/lib.rs | 19 +++++++++++++------
 3 files changed, 41 insertions(+), 12 deletions(-)

diff --git a/node/src/command.rs b/node/src/command.rs
index ce6f6d500..a7a594f94 100644
--- a/node/src/command.rs
+++ b/node/src/command.rs
@@ -42,6 +42,20 @@ lazy_static! {
     };
 }*/
 
+//Currently, only `gdev-benchmark` supports the benchmark of all the extrinsics. Storage and overhead are equivalent with `gdev-benchmark` and `gdev`, so we enforce `gdev-benchmark` for all benchmark-related commands.
+#[cfg(feature = "runtime-benchmarks")]
+fn ensure_dev(spec: &Box<dyn sc_service::ChainSpec>) -> std::result::Result<(), String> {
+    if spec.id() == "gdev-benchmark" {
+        Ok(())
+    } else {
+        Err(format!(
+            "{}{}",
+            "can only use subcommand with --chain [gdev-benchmark], got ",
+            spec.id()
+        ))
+    }
+}
+
 /// Unwraps a [`crate::client::Client`] into the concrete runtime client.
 #[cfg(feature = "runtime-benchmarks")]
 macro_rules! unwrap_client {
@@ -263,6 +277,7 @@ pub fn run() -> sc_cli::Result<()> {
         Some(Subcommand::Benchmark(cmd)) => {
             let runner = cli.create_runner(cmd)?;
             let chain_spec = &runner.config().chain_spec;
+            ensure_dev(chain_spec)?;
 
             match cmd {
                 BenchmarkCmd::Storage(cmd) => runner.sync_run(|mut config| {
diff --git a/runtime/g1/src/lib.rs b/runtime/g1/src/lib.rs
index e6cc61575..c6b533d68 100644
--- a/runtime/g1/src/lib.rs
+++ b/runtime/g1/src/lib.rs
@@ -130,16 +130,23 @@ pub type TechnicalCommitteeInstance = Instance2;
 #[cfg(feature = "runtime-benchmarks")]
 mod benches {
     define_benchmarks!(
-        // Duniter
-        // NOTE: Make sure to prefix these with `common_runtime::` so
-        // the that path resolves correctly in the generated file.
-        [common_runtime::oneshot_account, OneshotAccount]
-        [common_runtime::universal_dividend, UniversalDividend]
-        [common_runtime::upgrade_origin, UpgradeOrigin]
+        // For completion, benchmarks only running on `gdev-benchmark`.
+        [pallet_certification, Cert]
+        [pallet_certification, SmithCert]
+        [pallet_oneshot_account, OneshotAccount]
+        [pallet_universal_dividend, UniversalDividend]
+        [pallet_provide_randomness, ProvideRandomness]
+        [pallet_upgrade_origin, UpgradeOrigin]
+        [pallet_duniter_account, Account]
+        [pallet_identity, Identity]
+        [pallet_membership, Membership]
+        [pallet_membership, SmithMembership]
+        [pallet_authority_members, AuthorityMembers]
         // Substrate
         [pallet_balances, Balances]
         [frame_benchmarking::baseline, Baseline::<Runtime>]
         [pallet_collective, TechnicalCommittee]
+        [pallet_session, SessionBench::<Runtime>]
         [pallet_im_online, ImOnline]
         [pallet_multisig, Multisig]
         [pallet_preimage, Preimage]
diff --git a/runtime/gtest/src/lib.rs b/runtime/gtest/src/lib.rs
index 27d585905..3d2282f26 100644
--- a/runtime/gtest/src/lib.rs
+++ b/runtime/gtest/src/lib.rs
@@ -131,16 +131,23 @@ pub type TechnicalCommitteeInstance = Instance2;
 #[cfg(feature = "runtime-benchmarks")]
 mod benches {
     define_benchmarks!(
-        // Duniter
-        // NOTE: Make sure to prefix these with `common_runtime::` so
-        // the that path resolves correctly in the generated file.
-        [common_runtime::oneshot_account, OneshotAccount]
-        [common_runtime::universal_dividend, UniversalDividend]
-        [common_runtime::upgrade_origin, UpgradeOrigin]
+        // For completion, benchmarks only running on `gdev-benchmark`.
+        [pallet_certification, Cert]
+        [pallet_certification, SmithCert]
+        [pallet_oneshot_account, OneshotAccount]
+        [pallet_universal_dividend, UniversalDividend]
+        [pallet_provide_randomness, ProvideRandomness]
+        [pallet_upgrade_origin, UpgradeOrigin]
+        [pallet_duniter_account, Account]
+        [pallet_identity, Identity]
+        [pallet_membership, Membership]
+        [pallet_membership, SmithMembership]
+        [pallet_authority_members, AuthorityMembers]
         // Substrate
         [pallet_balances, Balances]
         [frame_benchmarking::baseline, Baseline::<Runtime>]
         [pallet_collective, TechnicalCommittee]
+        [pallet_session, SessionBench::<Runtime>]
         [pallet_im_online, ImOnline]
         [pallet_multisig, Multisig]
         [pallet_preimage, Preimage]
-- 
GitLab