From 603a392b6799d6ea3e13dabc411242f821c30215 Mon Sep 17 00:00:00 2001
From: Joseph Zhao
 <65984904+programskillforverification@users.noreply.github.com>
Date: Mon, 18 Nov 2024 10:09:15 +0800
Subject: [PATCH] Migrate some pallets to benchmark v2 (#6311)

Part of #6202

---------

Co-authored-by: Guillaume Thiolliere <guillaume.thiolliere@parity.io>
Co-authored-by: Giuseppe Re <giuseppe.re@parity.io>
---
 prdoc/pr_6311.prdoc                           |  13 ++
 substrate/frame/babe/src/benchmarking.rs      |  27 ++--
 .../frame/fast-unstake/src/benchmarking.rs    | 116 +++++++++---------
 3 files changed, 88 insertions(+), 68 deletions(-)
 create mode 100644 prdoc/pr_6311.prdoc

diff --git a/prdoc/pr_6311.prdoc b/prdoc/pr_6311.prdoc
new file mode 100644
index 00000000000..a63876f4e4a
--- /dev/null
+++ b/prdoc/pr_6311.prdoc
@@ -0,0 +1,13 @@
+# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
+# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
+
+title: Migrate pallet-fast-unstake and pallet-babe benchmark to v2
+doc:
+- audience: Runtime Dev
+  description: |-
+    Migrate pallet-fast-unstake and pallet-babe benchmark to v2
+crates:
+- name: pallet-babe
+  bump: patch
+- name: pallet-fast-unstake
+  bump: patch
diff --git a/substrate/frame/babe/src/benchmarking.rs b/substrate/frame/babe/src/benchmarking.rs
index 6b0e31e8471..33e275fcb5e 100644
--- a/substrate/frame/babe/src/benchmarking.rs
+++ b/substrate/frame/babe/src/benchmarking.rs
@@ -20,14 +20,16 @@
 #![cfg(feature = "runtime-benchmarks")]
 
 use super::*;
-use frame_benchmarking::v1::benchmarks;
+use frame_benchmarking::v2::*;
 
 type Header = sp_runtime::generic::Header<u64, sp_runtime::traits::BlakeTwo256>;
 
-benchmarks! {
-	check_equivocation_proof {
-		let x in 0 .. 1;
+#[benchmarks]
+mod benchmarks {
+	use super::*;
 
+	#[benchmark]
+	fn check_equivocation_proof(x: Linear<0, 1>) {
 		// NOTE: generated with the test below `test_generate_equivocation_report_blob`.
 		// the output is not deterministic since keys are generated randomly (and therefore
 		// signature content changes). it should not affect the benchmark.
@@ -53,22 +55,21 @@ benchmarks! {
 			124, 11, 167, 227, 103, 88, 78, 23, 228, 33, 96, 41, 207, 183, 227, 189, 114, 70, 254,
 			30, 128, 243, 233, 83, 214, 45, 74, 182, 120, 119, 64, 243, 219, 119, 63, 240, 205,
 			123, 231, 82, 205, 174, 143, 70, 2, 86, 182, 20, 16, 141, 145, 91, 116, 195, 58, 223,
-			175, 145, 255, 7, 121, 133
+			175, 145, 255, 7, 121, 133,
 		];
 
 		let equivocation_proof1: sp_consensus_babe::EquivocationProof<Header> =
 			Decode::decode(&mut &EQUIVOCATION_PROOF_BLOB[..]).unwrap();
 
 		let equivocation_proof2 = equivocation_proof1.clone();
-	}: {
-		sp_consensus_babe::check_equivocation_proof::<Header>(equivocation_proof1);
-	} verify {
+
+		#[block]
+		{
+			sp_consensus_babe::check_equivocation_proof::<Header>(equivocation_proof1);
+		}
+
 		assert!(sp_consensus_babe::check_equivocation_proof::<Header>(equivocation_proof2));
 	}
 
-	impl_benchmark_test_suite!(
-		Pallet,
-		crate::mock::new_test_ext(3),
-		crate::mock::Test,
-	)
+	impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(3), crate::mock::Test,);
 }
diff --git a/substrate/frame/fast-unstake/src/benchmarking.rs b/substrate/frame/fast-unstake/src/benchmarking.rs
index d01ff715ca4..750f348c459 100644
--- a/substrate/frame/fast-unstake/src/benchmarking.rs
+++ b/substrate/frame/fast-unstake/src/benchmarking.rs
@@ -19,9 +19,9 @@
 
 #![cfg(feature = "runtime-benchmarks")]
 
-use crate::{types::*, Pallet as FastUnstake, *};
+use crate::{types::*, *};
 use alloc::{vec, vec::Vec};
-use frame_benchmarking::v1::{benchmarks, whitelist_account, BenchmarkError};
+use frame_benchmarking::v2::*;
 use frame_support::{
 	assert_ok,
 	traits::{Currency, EnsureOrigin, Get, Hooks},
@@ -89,22 +89,21 @@ fn setup_staking<T: Config>(v: u32, until: EraIndex) {
 
 fn on_idle_full_block<T: Config>() {
 	let remaining_weight = <T as frame_system::Config>::BlockWeights::get().max_block;
-	FastUnstake::<T>::on_idle(Zero::zero(), remaining_weight);
+	Pallet::<T>::on_idle(Zero::zero(), remaining_weight);
 }
 
-benchmarks! {
+#[benchmarks]
+mod benchmarks {
+	use super::*;
 	// on_idle, we don't check anyone, but fully unbond them.
-	on_idle_unstake {
-		let b in 1 .. T::BatchSize::get();
-
+	#[benchmark]
+	fn on_idle_unstake(b: Linear<1, { T::BatchSize::get() }>) {
 		ErasToCheckPerBlock::<T>::put(1);
 		for who in create_unexposed_batch::<T>(b).into_iter() {
-			assert_ok!(FastUnstake::<T>::register_fast_unstake(
-				RawOrigin::Signed(who.clone()).into(),
-			));
+			assert_ok!(Pallet::<T>::register_fast_unstake(RawOrigin::Signed(who.clone()).into(),));
 		}
 
-		// run on_idle once. This will check era 0.
+		// Run on_idle once. This will check era 0.
 		assert_eq!(Head::<T>::get(), None);
 		on_idle_full_block::<T>();
 
@@ -116,21 +115,19 @@ benchmarks! {
 				..
 			}) if checked.len() == 1 && stashes.len() as u32 == b
 		));
+
+		#[block]
+		{
+			on_idle_full_block::<T>();
+		}
+
+		assert_eq!(fast_unstake_events::<T>().last(), Some(&Event::BatchFinished { size: b }));
 	}
-	: {
-		on_idle_full_block::<T>();
-	}
-	verify {
-		assert!(matches!(
-			fast_unstake_events::<T>().last(),
-			Some(Event::BatchFinished { size: b })
-		));
-	}
 
-	// on_idle, when we check some number of eras and the queue is already set.
-	on_idle_check {
-		let v in 1 .. 256;
-		let b in 1 .. T::BatchSize::get();
+	#[benchmark]
+	fn on_idle_check(v: Linear<1, 256>, b: Linear<1, { T::BatchSize::get() }>) {
+		// on_idle: When we check some number of eras and the queue is already set.
+
 		let u = T::MaxErasToCheckPerBlock::get().min(T::Staking::bonding_duration());
 
 		ErasToCheckPerBlock::<T>::put(u);
@@ -139,64 +136,73 @@ benchmarks! {
 		// setup staking with v validators and u eras of data (0..=u+1)
 		setup_staking::<T>(v, u);
 
-		let stashes = create_unexposed_batch::<T>(b).into_iter().map(|s| {
-			assert_ok!(FastUnstake::<T>::register_fast_unstake(
-				RawOrigin::Signed(s.clone()).into(),
-			));
-			(s, T::Deposit::get())
-		}).collect::<Vec<_>>();
+		let stashes = create_unexposed_batch::<T>(b)
+			.into_iter()
+			.map(|s| {
+				assert_ok!(
+					Pallet::<T>::register_fast_unstake(RawOrigin::Signed(s.clone()).into(),)
+				);
+				(s, T::Deposit::get())
+			})
+			.collect::<Vec<_>>();
 
 		// no one is queued thus far.
 		assert_eq!(Head::<T>::get(), None);
 
-		Head::<T>::put(UnstakeRequest { stashes: stashes.clone().try_into().unwrap(), checked: Default::default() });
-	}
-	: {
-		on_idle_full_block::<T>();
-	}
-	verify {
+		Head::<T>::put(UnstakeRequest {
+			stashes: stashes.clone().try_into().unwrap(),
+			checked: Default::default(),
+		});
+
+		#[block]
+		{
+			on_idle_full_block::<T>();
+		}
+
 		let checked = (1..=u).rev().collect::<Vec<EraIndex>>();
 		let request = Head::<T>::get().unwrap();
 		assert_eq!(checked, request.checked.into_inner());
-		assert!(matches!(
-			fast_unstake_events::<T>().last(),
-			Some(Event::BatchChecked { .. })
-		));
+		assert!(matches!(fast_unstake_events::<T>().last(), Some(Event::BatchChecked { .. })));
 		assert!(stashes.iter().all(|(s, _)| request.stashes.iter().any(|(ss, _)| ss == s)));
 	}
 
-	register_fast_unstake {
+	#[benchmark]
+	fn register_fast_unstake() {
 		ErasToCheckPerBlock::<T>::put(1);
 		let who = create_unexposed_batch::<T>(1).get(0).cloned().unwrap();
 		whitelist_account!(who);
 		assert_eq!(Queue::<T>::count(), 0);
 
-	}
-	:_(RawOrigin::Signed(who.clone()))
-	verify {
+		#[extrinsic_call]
+		_(RawOrigin::Signed(who.clone()));
+
 		assert_eq!(Queue::<T>::count(), 1);
 	}
 
-	deregister {
+	#[benchmark]
+	fn deregister() {
 		ErasToCheckPerBlock::<T>::put(1);
 		let who = create_unexposed_batch::<T>(1).get(0).cloned().unwrap();
-		assert_ok!(FastUnstake::<T>::register_fast_unstake(
-			RawOrigin::Signed(who.clone()).into(),
-		));
+		assert_ok!(Pallet::<T>::register_fast_unstake(RawOrigin::Signed(who.clone()).into(),));
 		assert_eq!(Queue::<T>::count(), 1);
 		whitelist_account!(who);
-	}
-	:_(RawOrigin::Signed(who.clone()))
-	verify {
+
+		#[extrinsic_call]
+		_(RawOrigin::Signed(who.clone()));
+
 		assert_eq!(Queue::<T>::count(), 0);
 	}
 
-	control {
+	#[benchmark]
+	fn control() -> Result<(), BenchmarkError> {
 		let origin = <T as Config>::ControlOrigin::try_successful_origin()
 			.map_err(|_| BenchmarkError::Weightless)?;
+
+		#[extrinsic_call]
+		_(origin as T::RuntimeOrigin, T::MaxErasToCheckPerBlock::get());
+
+		Ok(())
 	}
-	: _<T::RuntimeOrigin>(origin, T::MaxErasToCheckPerBlock::get())
-	verify {}
 
-	impl_benchmark_test_suite!(Pallet, crate::mock::ExtBuilder::default().build(), crate::mock::Runtime)
+	impl_benchmark_test_suite!(Pallet, mock::ExtBuilder::default().build(), mock::Runtime);
 }
-- 
GitLab