From c48cb99144417a6d5d4e761c2b8d1f449abfe65d Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Tue, 31 Oct 2023 20:51:36 +0100
Subject: [PATCH] wip

---
 Cargo.lock                           |  3 ++
 pallets/distance/Cargo.toml          | 21 +++++---
 pallets/distance/src/benchmarking.rs | 74 ++++++++++++++++++++++++++++
 runtime/gdev/Cargo.toml              |  1 +
 runtime/gdev/src/lib.rs              |  1 +
 5 files changed, 94 insertions(+), 6 deletions(-)
 create mode 100644 pallets/distance/src/benchmarking.rs

diff --git a/Cargo.lock b/Cargo.lock
index 6e2559994..582ce61c7 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5849,6 +5849,7 @@ dependencies = [
  "frame-system",
  "pallet-authority-members",
  "pallet-authorship",
+ "pallet-balances",
  "pallet-identity",
  "pallet-membership",
  "pallet-session",
@@ -5857,6 +5858,8 @@ dependencies = [
  "sp-core",
  "sp-distance",
  "sp-inherents",
+ "sp-io",
+ "sp-keystore",
  "sp-runtime",
  "sp-std 5.0.0",
 ]
diff --git a/pallets/distance/Cargo.toml b/pallets/distance/Cargo.toml
index b1910ffba..5a04dab80 100644
--- a/pallets/distance/Cargo.toml
+++ b/pallets/distance/Cargo.toml
@@ -11,7 +11,9 @@ version = '1.0.0'
 
 [features]
 default = ['std']
-runtime-benchmarks = ['frame-benchmarking/runtime-benchmarks']
+runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks",
+"pallet-balances",
+]
 std = [
     'codec/std',
     'frame-support/std',
@@ -39,11 +41,7 @@ scale-info = { version = "2.1.1", default-features = false, features = [
     "derive",
 ] }
 
-[dependencies.frame-benchmarking]
-default-features = false
-git = 'https://github.com/duniter/substrate'
-optional = true
-branch = 'duniter-substrate-v0.9.42'
+pallet-balances = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', optional = true, default-features = false }
 
 [dependencies.codec]
 default-features = false
@@ -51,6 +49,12 @@ features = ['derive']
 package = 'parity-scale-codec'
 version = '3.1.5'
 
+[dependencies.frame-benchmarking]
+default-features = false
+git = 'https://github.com/duniter/substrate'
+optional = true
+branch = 'duniter-substrate-v0.9.42'
+
 [dependencies.frame-support]
 default-features = false
 git = 'https://github.com/duniter/substrate'
@@ -95,3 +99,8 @@ branch = 'duniter-substrate-v0.9.42'
 
 [package.metadata.docs.rs]
 targets = ['x86_64-unknown-linux-gnu']
+
+[dev-dependencies]
+pallet-balances = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42' }
+sp-io = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42' }
+sp-keystore = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42' }
diff --git a/pallets/distance/src/benchmarking.rs b/pallets/distance/src/benchmarking.rs
new file mode 100644
index 000000000..9c400ad45
--- /dev/null
+++ b/pallets/distance/src/benchmarking.rs
@@ -0,0 +1,74 @@
+// Copyright 2021-2023 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/>.
+
+#![cfg(feature = "runtime-benchmarks")]
+
+use super::*;
+
+//use codec::Encode;
+use codec::Encode;
+use frame_benchmarking::{account, benchmarks, Vec};
+use frame_support::pallet_prelude::IsType;
+use frame_support::traits::Currency;
+use frame_support::traits::OnInitialize;
+use frame_support::ConsensusEngineId;
+use frame_system::RawOrigin;
+use pallet_balances::Pallet as Balances;
+use pallet_identity::Identities;
+use sp_runtime::traits::{Bounded, One};
+use sp_runtime::DigestItem;
+use sp_runtime::Perbill;
+
+use crate::Pallet;
+
+/*fn assert_has_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
+    frame_system::Pallet::<T>::assert_has_event(generic_event.into());
+}*/
+
+benchmarks! {
+    where_clause {
+        where
+        T: pallet_balances::Config, T::Balance: From<u64>,
+        T::BlockNumber: From<u32>,
+    }
+    request_distance_evaluation {
+            let idty = T::IdtyIndex::one();
+            let caller: T::AccountId  = pallet_identity::Identities::<T>::get(idty).unwrap().owner_key;
+            let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into();
+            let _ = <Balances<T> as Currency<_>>::make_free_balance_be(&caller, T::Balance::max_value());
+    }: _<T::RuntimeOrigin>(caller_origin.clone())
+    verify {
+        assert!(IdentityDistanceStatus::<T>::get(&idty) == Some((caller, DistanceStatus::Pending)), "Request not added");
+    }
+    update_evaluation {
+            let idty = T::IdtyIndex::one();
+            let caller: T::AccountId  = pallet_identity::Identities::<T>::get(idty).unwrap().owner_key;
+            let caller_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into();
+            //let _ = <Balances<T> as Currency<_>>::make_free_balance_be(&caller, T::Balance::max_value());
+            let computation_result = ComputationResult{distances: Vec::<Perbill>::default()};
+
+            /// TODO: set the author of the block.
+            /*let author = 1;
+            let mut digest:<frame_system::Pallet<T>>::digest  = <frame_system::Pallet<T>>::digest();
+            const TEST_ID: ConsensusEngineId = [1, 2, 3, 4];
+            digest.logs.push(DigestItem::PreRuntime(TEST_ID, author.encode()));
+            //digest.logs.push(DigestItem::Seal(TEST_ID, author.encode()));
+            //digest.pop();
+            frame_system::Pallet::<T>::reset_events();
+            frame_system::Pallet::<T>::initialize(&0.into(), &Default::default(), &digest);
+            pallet_authorship::Pallet::<T>::author().unwrap();*/
+    }: _<T::RuntimeOrigin>(RawOrigin::None.into(), computation_result)
+}
diff --git a/runtime/gdev/Cargo.toml b/runtime/gdev/Cargo.toml
index c7a84e499..58faf3aec 100644
--- a/runtime/gdev/Cargo.toml
+++ b/runtime/gdev/Cargo.toml
@@ -27,6 +27,7 @@ runtime-benchmarks = [
     'pallet-balances/runtime-benchmarks',
     'pallet-certification/runtime-benchmarks',
     'pallet-collective/runtime-benchmarks',
+    'pallet-distance/runtime-benchmarks',
     'pallet-duniter-test-parameters/runtime-benchmarks',
     'pallet-duniter-account/runtime-benchmarks',
     'pallet-duniter-wot/runtime-benchmarks',
diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs
index 5c4491398..ac89084e6 100644
--- a/runtime/gdev/src/lib.rs
+++ b/runtime/gdev/src/lib.rs
@@ -143,6 +143,7 @@ mod benches {
     define_benchmarks!(
         [pallet_certification, Cert]
         [pallet_certification, SmithCert]
+        [pallet_distance, Distance]
         [pallet_oneshot_account, OneshotAccount]
         [pallet_universal_dividend, UniversalDividend]
         [pallet_provide_randomness, ProvideRandomness]
-- 
GitLab