From baef83b3488eb3d00c02460a83be14a10e4c8de3 Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Sat, 4 Nov 2023 10:24:40 +0100
Subject: [PATCH] add digest decoding complexity

---
 Cargo.lock                           |  1 +
 pallets/distance/Cargo.toml          | 11 +++++++----
 pallets/distance/src/benchmarking.rs | 14 ++++++++++----
 runtime/common/src/pallets_config.rs |  3 ---
 runtime/common/src/providers.rs      | 13 -------------
 5 files changed, 18 insertions(+), 24 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 582ce61c7..1f943e54c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -5855,6 +5855,7 @@ dependencies = [
  "pallet-session",
  "parity-scale-codec",
  "scale-info",
+ "sp-consensus-babe",
  "sp-core",
  "sp-distance",
  "sp-inherents",
diff --git a/pallets/distance/Cargo.toml b/pallets/distance/Cargo.toml
index 9e2f557a5..f6835b563 100644
--- a/pallets/distance/Cargo.toml
+++ b/pallets/distance/Cargo.toml
@@ -12,7 +12,9 @@ version = '1.0.0'
 [features]
 default = ['std']
 runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks",
-"pallet-balances", "pallet-identity/runtime-benchmarks",
+"pallet-balances",
+"pallet-identity/runtime-benchmarks",
+"sp-consensus-babe",
 ]
 std = [
     'codec/std',
@@ -35,6 +37,7 @@ pallet-authority-members = { path = "../authority-members", default-features = f
 pallet-identity = { path = "../identity", default-features = false }
 pallet-membership = { path = "../membership", default-features = false }
 sp-distance = { path = "../../primitives/distance", default-features = false }
+sp-consensus-babe = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false, optional = true }
 
 # substrate
 scale-info = { version = "2.1.1", default-features = false, features = [
@@ -101,6 +104,6 @@ branch = 'duniter-substrate-v0.9.42'
 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' }
+pallet-balances = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false }
+sp-io = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false }
+sp-keystore = { git = 'https://github.com/duniter/substrate', branch = 'duniter-substrate-v0.9.42', default-features = false }
diff --git a/pallets/distance/src/benchmarking.rs b/pallets/distance/src/benchmarking.rs
index 8f5e33803..ed8127e94 100644
--- a/pallets/distance/src/benchmarking.rs
+++ b/pallets/distance/src/benchmarking.rs
@@ -18,6 +18,7 @@
 
 use super::*;
 
+use codec::Encode;
 use frame_benchmarking::{benchmarks, vec};
 use frame_support::traits::{Currency, OnFinalize};
 use frame_system::RawOrigin;
@@ -55,10 +56,15 @@ benchmarks! {
         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 i in 1 .. MAX_EVALUATIONS_PER_SESSION => populate_pool::<T>(i)?;
+        let digest_data = sp_consensus_babe::digests::PreDigest::SecondaryPlain(
+        sp_consensus_babe::digests::SecondaryPlainPreDigest { authority_index: 0u32, slot: Default::default() },
+    );
+        let digest = sp_runtime::DigestItem::PreRuntime(*b"BABE", digest_data.encode());
+        let _ = <frame_system::Pallet<T>>::deposit_log(digest);
+        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 i in 1 .. MAX_EVALUATIONS_PER_SESSION => populate_pool::<T>(i)?;
     }: _<T::RuntimeOrigin>(RawOrigin::None.into(), ComputationResult{distances: vec![Perbill::one(); i as usize]})
     force_update_evaluation {
             let idty = T::IdtyIndex::one();
diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs
index 561ea867f..afd0919f8 100644
--- a/runtime/common/src/pallets_config.rs
+++ b/runtime/common/src/pallets_config.rs
@@ -220,9 +220,6 @@ macro_rules! pallets_config {
         }
         impl pallet_authorship::Config for Runtime {
             type EventHandler = ImOnline;
-            #[cfg(feature = "runtime-benchmarks")]
-            type FindAuthor = common_runtime::providers::ConstantAuthor<Self::AccountId>;
-            #[cfg(not(feature = "runtime-benchmarks"))]
             type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Babe>;
         }
         impl pallet_im_online::Config for Runtime {
diff --git a/runtime/common/src/providers.rs b/runtime/common/src/providers.rs
index 7d5a79a7e..9680798aa 100644
--- a/runtime/common/src/providers.rs
+++ b/runtime/common/src/providers.rs
@@ -155,16 +155,3 @@ macro_rules! impl_benchmark_setup_handler {
 impl_benchmark_setup_handler!(pallet_membership::SetupBenchmark<<T as pallet_identity::Config>::IdtyIndex, T::AccountId>);
 #[cfg(feature = "runtime-benchmarks")]
 impl_benchmark_setup_handler!(pallet_identity::traits::SetupBenchmark<<T as pallet_identity::Config>::IdtyIndex, T::AccountId>);
-
-#[cfg(feature = "runtime-benchmarks")]
-pub struct ConstantAuthor<T>(PhantomData<T>);
-
-#[cfg(feature = "runtime-benchmarks")]
-impl<T: From<[u8; 32]>> frame_support::traits::FindAuthor<T> for ConstantAuthor<T> {
-    fn find_author<'a, I>(_: I) -> Option<T>
-    where
-        I: 'a + IntoIterator<Item = (sp_runtime::ConsensusEngineId, &'a [u8])>,
-    {
-        Some([0; 32].into())
-    }
-}
-- 
GitLab