From 29b59d79d2e116587a7950f91a2a18106519b1ab Mon Sep 17 00:00:00 2001
From: Liam Aharon <liam.aharon@hotmail.com>
Date: Fri, 12 Apr 2024 09:58:13 +0400
Subject: [PATCH] cleaner benchmarkhelper

---
 .../assets/asset-hub-rococo/src/lib.rs        | 23 ++----
 .../assets/asset-hub-westend/src/lib.rs       | 23 ++----
 substrate/bin/node/runtime/src/lib.rs         | 14 ++--
 .../frame/asset-rewards/src/benchmarking.rs   | 78 +++++++------------
 substrate/frame/asset-rewards/src/mock.rs     | 22 +++++-
 5 files changed, 75 insertions(+), 85 deletions(-)

diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs
index 4dccf52c4a4..f7194f15d77 100644
--- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs
+++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/lib.rs
@@ -102,6 +102,9 @@ use xcm::latest::prelude::{
 };
 use xcm::latest::prelude::{AssetId, BodyId};
 
+#[cfg(feature = "runtime-benchmarks")]
+use frame_support::traits::PalletInfoAccess;
+
 use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};
 
 impl_opaque_keys! {
@@ -905,12 +908,6 @@ impl pallet_xcm_bridge_hub_router::Config<ToWestendXcmRouterInstance> for Runtim
 	type FeeAsset = xcm_config::bridging::XcmBridgeHubRouterFeeAssetId;
 }
 
-#[cfg(feature = "runtime-benchmarks")]
-use crate::fungible::NativeOrWithId;
-
-#[cfg(feature = "runtime-benchmarks")]
-use frame_support::traits::PalletInfoAccess;
-
 #[cfg(feature = "runtime-benchmarks")]
 pub struct PalletAssetRewardsBenchmarkHelper;
 
@@ -918,16 +915,12 @@ pub struct PalletAssetRewardsBenchmarkHelper;
 impl pallet_asset_rewards::benchmarking::BenchmarkHelper<xcm::v3::Location, AccountId>
 	for PalletAssetRewardsBenchmarkHelper
 {
-	fn to_asset_id(seed: NativeOrWithId<u32>) -> xcm::v3::Location {
-		match seed {
-			NativeOrWithId::Native => xcm::v3::Location::here().into(),
-			NativeOrWithId::WithId(id) => xcm::v3::Location::ancestor(id.try_into().unwrap()),
-		}
+	fn to_asset_id(seed: u32) -> xcm::v3::Location {
+		// Any Location is fine for benchmarking.
+		xcm::v3::Location::ancestor(seed.try_into().unwrap())
 	}
-	fn to_account_id(seed: u32) -> AccountId {
-		let mut bytes = [0u8; 32];
-		bytes[0..4].copy_from_slice(&seed.to_be_bytes());
-		bytes.into()
+	fn to_account_id(seed: [u8; 32]) -> AccountId {
+		seed.into()
 	}
 	fn sufficient_asset() -> xcm::v3::Location {
 		xcm::v3::Junction::PalletInstance(<Balances as PalletInfoAccess>::index() as u8).into()
diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
index 8cd1cfcfaff..970d3a4717f 100644
--- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
+++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
@@ -97,6 +97,9 @@ use xcm::latest::prelude::{
 };
 use xcm::latest::{prelude::AssetId, BodyId};
 
+#[cfg(feature = "runtime-benchmarks")]
+use frame_support::traits::PalletInfoAccess;
+
 impl_opaque_keys! {
 	pub struct SessionKeys {
 		pub aura: Aura,
@@ -360,12 +363,6 @@ impl pallet_asset_conversion::Config for Runtime {
 	>;
 }
 
-#[cfg(feature = "runtime-benchmarks")]
-use crate::fungible::NativeOrWithId;
-
-#[cfg(feature = "runtime-benchmarks")]
-use frame_support::traits::PalletInfoAccess;
-
 #[cfg(feature = "runtime-benchmarks")]
 pub struct PalletAssetRewardsBenchmarkHelper;
 
@@ -373,16 +370,12 @@ pub struct PalletAssetRewardsBenchmarkHelper;
 impl pallet_asset_rewards::benchmarking::BenchmarkHelper<xcm::v3::Location, AccountId>
 	for PalletAssetRewardsBenchmarkHelper
 {
-	fn to_asset_id(seed: NativeOrWithId<u32>) -> xcm::v3::Location {
-		match seed {
-			NativeOrWithId::Native => xcm::v3::Location::here().into(),
-			NativeOrWithId::WithId(id) => xcm::v3::Location::ancestor(id.try_into().unwrap()),
-		}
+	fn to_asset_id(seed: u32) -> xcm::v3::Location {
+		// Any Location is fine for benchmarking.
+		xcm::v3::Location::ancestor(seed.try_into().unwrap())
 	}
-	fn to_account_id(seed: u32) -> AccountId {
-		let mut bytes = [0u8; 32];
-		bytes[0..4].copy_from_slice(&seed.to_be_bytes());
-		bytes.into()
+	fn to_account_id(seed: [u8; 32]) -> AccountId {
+		seed.into()
 	}
 	fn sufficient_asset() -> xcm::v3::Location {
 		xcm::v3::Junction::PalletInstance(<Balances as PalletInfoAccess>::index() as u8).into()
diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs
index 3757059c1d8..af63ffe8f33 100644
--- a/substrate/bin/node/runtime/src/lib.rs
+++ b/substrate/bin/node/runtime/src/lib.rs
@@ -1742,13 +1742,15 @@ pub struct AssetRewardsBenchmarkHelper;
 impl pallet_asset_rewards::benchmarking::BenchmarkHelper<NativeOrWithId<u32>, AccountId>
 	for AssetRewardsBenchmarkHelper
 {
-	fn to_asset_id(seed: NativeOrWithId<u32>) -> NativeOrWithId<u32> {
-		seed
+	fn to_asset_id(seed: u32) -> NativeOrWithId<u32> {
+		if seed == 0 {
+			NativeOrWithId::<u32>::Native
+		} else {
+			NativeOrWithId::<u32>::WithId(seed)
+		}
 	}
-	fn to_account_id(seed: u32) -> AccountId {
-		let mut bytes = [0u8; 32]; // Create a 32-byte array filled with zeros
-		bytes[0..4].copy_from_slice(&seed.to_be_bytes()); // Place the u32 value at the beginning (big-endian for this example)
-		bytes.into()
+	fn to_account_id(seed: [u8; 32]) -> AccountId {
+		seed.into()
 	}
 	fn sufficient_asset() -> NativeOrWithId<u32> {
 		NativeOrWithId::<u32>::Native
diff --git a/substrate/frame/asset-rewards/src/benchmarking.rs b/substrate/frame/asset-rewards/src/benchmarking.rs
index e589ecd5312..187a04e3de4 100644
--- a/substrate/frame/asset-rewards/src/benchmarking.rs
+++ b/substrate/frame/asset-rewards/src/benchmarking.rs
@@ -23,7 +23,6 @@ use frame_benchmarking::v2::*;
 use frame_support::{
 	assert_ok,
 	traits::{
-		fungible::NativeOrWithId,
 		fungibles::{Create, Inspect, Mutate},
 		EnsureOrigin,
 	},
@@ -34,30 +33,14 @@ use sp_std::prelude::*;
 
 /// Benchmark Helper
 pub trait BenchmarkHelper<AssetId, AccountId> {
-	/// Convert a NativeOrWithId to an AssetId
-	fn to_asset_id(seed: NativeOrWithId<u32>) -> AssetId;
-	/// Convert a u32 to an AccountId
-	fn to_account_id(seed: u32) -> AccountId;
+	/// Convert a u32 to an AssetId
+	fn to_asset_id(seed: u32) -> AssetId;
+	/// Convert a [u8; 32] to an AccountId
+	fn to_account_id(seed: [u8; 32]) -> AccountId;
 	/// Return the ID of the asset whos minimum balance is sufficient for an account to exist
 	fn sufficient_asset() -> AssetId;
 }
 
-impl<AssetId, AccountId> BenchmarkHelper<AssetId, AccountId> for ()
-where
-	AssetId: From<NativeOrWithId<u32>>,
-	AccountId: From<u32>,
-{
-	fn to_asset_id(seed: NativeOrWithId<u32>) -> AssetId {
-		seed.into()
-	}
-	fn to_account_id(seed: u32) -> AccountId {
-		seed.into()
-	}
-	fn sufficient_asset() -> AssetId {
-		NativeOrWithId::Native.into()
-	}
-}
-
 /// Create and mint the minimum amount of the sufficient asset.
 fn create_and_mint_sufficient<T: Config>(caller: &T::AccountId)
 where
@@ -103,8 +86,8 @@ mod benchmarks {
 		use super::*;
 
 		let root_acc = T::PermissionedOrigin::ensure_origin(Root.into()).unwrap();
-		let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1));
-		let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native);
+		let staked_asset = T::BenchmarkHelper::to_asset_id(1);
+		let reward_asset = T::BenchmarkHelper::to_asset_id(2);
 		create_and_mint_asset::<T>(
 			&root_acc,
 			&staked_asset,
@@ -144,9 +127,9 @@ mod benchmarks {
 	fn stake() {
 		use super::*;
 
-		let staker = T::BenchmarkHelper::to_account_id(2);
-		let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1));
-		let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native);
+		let staker = T::BenchmarkHelper::to_account_id([1u8; 32]);
+		let staked_asset = T::BenchmarkHelper::to_asset_id(1);
+		let reward_asset = T::BenchmarkHelper::to_asset_id(0);
 		create_and_mint_asset::<T>(
 			&staker,
 			&staked_asset,
@@ -179,9 +162,9 @@ mod benchmarks {
 	fn unstake() {
 		use super::*;
 
-		let staker = T::BenchmarkHelper::to_account_id(2);
-		let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1));
-		let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native);
+		let staker = T::BenchmarkHelper::to_account_id([1u8; 32]);
+		let staked_asset = T::BenchmarkHelper::to_asset_id(1);
+		let reward_asset = T::BenchmarkHelper::to_asset_id(0);
 		create_and_mint_asset::<T>(
 			&staker,
 			&staked_asset,
@@ -221,10 +204,9 @@ mod benchmarks {
 		use super::*;
 
 		let block_number_before = frame_system::Pallet::<T>::block_number();
-
-		let staker = T::BenchmarkHelper::to_account_id(2);
-		let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1));
-		let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native);
+		let staker = T::BenchmarkHelper::to_account_id([2u8; 32]);
+		let staked_asset = T::BenchmarkHelper::to_asset_id(1);
+		let reward_asset = T::BenchmarkHelper::to_asset_id(2);
 		create_and_mint_asset::<T>(
 			&staker,
 			&staked_asset,
@@ -279,9 +261,9 @@ mod benchmarks {
 	fn set_pool_reward_rate_per_block() {
 		use super::*;
 
-		let acc = T::BenchmarkHelper::to_account_id(1);
-		let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1));
-		let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native);
+		let acc = T::BenchmarkHelper::to_account_id([3u8; 32]);
+		let staked_asset = T::BenchmarkHelper::to_asset_id(1);
+		let reward_asset = T::BenchmarkHelper::to_asset_id(5);
 		create_and_mint_asset::<T>(
 			&acc,
 			&staked_asset,
@@ -318,9 +300,9 @@ mod benchmarks {
 	fn set_pool_admin() {
 		use super::*;
 
-		let new_admin = T::BenchmarkHelper::to_account_id(2);
-		let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1));
-		let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native);
+		let new_admin = T::BenchmarkHelper::to_account_id([2u8; 32]);
+		let staked_asset = T::BenchmarkHelper::to_asset_id(1);
+		let reward_asset = T::BenchmarkHelper::to_asset_id(0);
 		create_and_mint_asset::<T>(
 			&new_admin,
 			&staked_asset,
@@ -351,9 +333,9 @@ mod benchmarks {
 	fn set_pool_expiry_block() {
 		use super::*;
 
-		let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1));
-		let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native);
-		let acc = T::BenchmarkHelper::to_account_id(1);
+		let staked_asset = T::BenchmarkHelper::to_asset_id(2);
+		let reward_asset = T::BenchmarkHelper::to_asset_id(5);
+		let acc = T::BenchmarkHelper::to_account_id([5u8; 32]);
 		create_and_mint_asset::<T>(
 			&acc,
 			&staked_asset,
@@ -390,9 +372,9 @@ mod benchmarks {
 	fn deposit_reward_tokens() {
 		use super::*;
 
-		let acc = T::BenchmarkHelper::to_account_id(1);
-		let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1));
-		let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native);
+		let acc = T::BenchmarkHelper::to_account_id([3u8; 32]);
+		let staked_asset = T::BenchmarkHelper::to_asset_id(1);
+		let reward_asset = T::BenchmarkHelper::to_asset_id(4);
 		create_and_mint_asset::<T>(&acc, &reward_asset, 100_000u32.into());
 		create_and_mint_asset::<T>(&acc, &staked_asset, 100_000u32.into());
 
@@ -422,9 +404,9 @@ mod benchmarks {
 	fn withdraw_reward_tokens() {
 		use super::*;
 
-		let acc = T::BenchmarkHelper::to_account_id(1);
-		let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1));
-		let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native);
+		let acc = T::BenchmarkHelper::to_account_id([3u8; 32]);
+		let staked_asset = T::BenchmarkHelper::to_asset_id(1);
+		let reward_asset = T::BenchmarkHelper::to_asset_id(2);
 		create_and_mint_asset::<T>(&acc, &staked_asset, 10000u32.into());
 		create_and_mint_asset::<T>(&acc, &reward_asset, 10000u32.into());
 
diff --git a/substrate/frame/asset-rewards/src/mock.rs b/substrate/frame/asset-rewards/src/mock.rs
index dad428c4d44..a5cab540997 100644
--- a/substrate/frame/asset-rewards/src/mock.rs
+++ b/substrate/frame/asset-rewards/src/mock.rs
@@ -17,6 +17,7 @@
 
 //! Test environment for Staking Rewards pallet.
 
+use self::benchmarking::BenchmarkHelper;
 use super::*;
 use crate as pallet_staking_rewards;
 use core::default::Default;
@@ -119,6 +120,25 @@ impl EnsureOrigin<RuntimeOrigin> for MockPermissionedOrigin {
 
 pub type NativeAndAssets = UnionOf<Balances, Assets, NativeFromLeft, NativeOrWithId<u32>, u128>;
 
+pub struct AssetRewardsBenchmarkHelper;
+impl BenchmarkHelper<NativeOrWithId<u32>, u128> for AssetRewardsBenchmarkHelper {
+	fn to_asset_id(seed: u32) -> NativeOrWithId<u32> {
+		if seed == 0 {
+			NativeOrWithId::<u32>::Native
+		} else {
+			NativeOrWithId::<u32>::WithId(seed)
+		}
+	}
+	fn to_account_id(seed: [u8; 32]) -> u128 {
+		// only 16 bytes fit into u128
+		let bytes = <[u8; 16]>::try_from(&seed[0..16]).unwrap();
+		u128::from_be_bytes(bytes)
+	}
+	fn sufficient_asset() -> NativeOrWithId<u32> {
+		NativeOrWithId::<u32>::Native
+	}
+}
+
 impl Config for MockRuntime {
 	type RuntimeEvent = RuntimeEvent;
 	type AssetId = NativeOrWithId<u32>;
@@ -127,7 +147,7 @@ impl Config for MockRuntime {
 	type PalletId = StakingRewardsPalletId;
 	type PermissionedOrigin = MockPermissionedOrigin;
 	#[cfg(feature = "runtime-benchmarks")]
-	type BenchmarkHelper = ();
+	type BenchmarkHelper = AssetRewardsBenchmarkHelper;
 }
 
 pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
-- 
GitLab