From 3dcd7d560ec3f69e88baefe6b767b93e270204fd Mon Sep 17 00:00:00 2001
From: Guillaume Thiolliere <gui.thiolliere@gmail.com>
Date: Thu, 14 Nov 2024 11:27:00 +0900
Subject: [PATCH] Fix staking benchmark (#6463)

Found by @ggwpez

Fix staking benchmark, error was introduced when migrating to v2:
https://github.com/paritytech/polkadot-sdk/pull/6025

---------

Co-authored-by: GitHub Action <action@github.com>
(cherry picked from commit a1af8ed63668fc4a553776e381a12d165e7462f8)
---
 prdoc/pr_6463.prdoc                         |  8 ++++++++
 substrate/frame/staking/src/benchmarking.rs | 20 +++++++++++---------
 2 files changed, 19 insertions(+), 9 deletions(-)
 create mode 100644 prdoc/pr_6463.prdoc

diff --git a/prdoc/pr_6463.prdoc b/prdoc/pr_6463.prdoc
new file mode 100644
index 00000000000..9c4787540a4
--- /dev/null
+++ b/prdoc/pr_6463.prdoc
@@ -0,0 +1,8 @@
+title: Fix staking benchmark
+doc:
+- audience: Runtime Dev
+  description: 'Fix staking benchmark, error was introduced when migrating to v2:
+    https://github.com/paritytech/polkadot-sdk/pull/6025'
+crates:
+- name: pallet-staking
+  bump: patch
diff --git a/substrate/frame/staking/src/benchmarking.rs b/substrate/frame/staking/src/benchmarking.rs
index 96bd3860542..905f830f91d 100644
--- a/substrate/frame/staking/src/benchmarking.rs
+++ b/substrate/frame/staking/src/benchmarking.rs
@@ -975,20 +975,22 @@ mod benchmarks {
 	) -> Result<(), BenchmarkError> {
 		// number of nominator intention.
 		let n = MaxNominators::<T>::get();
+		create_validators_with_nominators_for_era::<T>(
+			v,
+			n,
+			MaxNominationsOf::<T>::get() as usize,
+			false,
+			None,
+		)?;
+
+		let targets;
 
 		#[block]
 		{
-			create_validators_with_nominators_for_era::<T>(
-				v,
-				n,
-				MaxNominationsOf::<T>::get() as usize,
-				false,
-				None,
-			)?;
+			// default bounds are unbounded.
+			targets = <Staking<T>>::get_npos_targets(DataProviderBounds::default());
 		}
 
-		// default bounds are unbounded.
-		let targets = <Staking<T>>::get_npos_targets(DataProviderBounds::default());
 		assert_eq!(targets.len() as u32, v);
 
 		Ok(())
-- 
GitLab