From e1eb1d6587912cd20d2aabde215e0a90e5238cce Mon Sep 17 00:00:00 2001 From: Liam Aharon <liam.aharon@hotmail.com> Date: Sun, 7 Apr 2024 11:00:16 +0400 Subject: [PATCH] integration into kitchensink --- Cargo.lock | 1 + substrate/bin/node/runtime/Cargo.toml | 4 ++ substrate/bin/node/runtime/src/lib.rs | 45 ++++++++++++ .../frame/asset-rewards/src/benchmarking.rs | 69 ++++++++++--------- substrate/frame/asset-rewards/src/lib.rs | 2 +- 5 files changed, 88 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3bbda3c8548..f4f66e92be5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7033,6 +7033,7 @@ dependencies = [ "pallet-asset-conversion", "pallet-asset-conversion-tx-payment", "pallet-asset-rate", + "pallet-asset-rewards", "pallet-asset-tx-payment", "pallet-assets", "pallet-authority-discovery", diff --git a/substrate/bin/node/runtime/Cargo.toml b/substrate/bin/node/runtime/Cargo.toml index f4ed75b9196..0257f68a7bd 100644 --- a/substrate/bin/node/runtime/Cargo.toml +++ b/substrate/bin/node/runtime/Cargo.toml @@ -66,6 +66,7 @@ frame-system-rpc-runtime-api = { path = "../../../frame/system/rpc/runtime-api", frame-try-runtime = { path = "../../../frame/try-runtime", default-features = false, optional = true } pallet-alliance = { path = "../../../frame/alliance", default-features = false } pallet-asset-conversion = { path = "../../../frame/asset-conversion", default-features = false } +pallet-asset-rewards = { path = "../../../frame/asset-rewards", default-features = false } pallet-asset-rate = { path = "../../../frame/asset-rate", default-features = false } pallet-assets = { path = "../../../frame/assets", default-features = false } pallet-authority-discovery = { path = "../../../frame/authority-discovery", default-features = false } @@ -269,6 +270,7 @@ std = [ "sp-transaction-pool/std", "sp-version/std", "substrate-wasm-builder", + "pallet-asset-rewards/std" ] runtime-benchmarks = [ "frame-benchmarking-pallet-pov/runtime-benchmarks", @@ -345,6 +347,7 @@ runtime-benchmarks = [ "pallet-whitelist/runtime-benchmarks", "sp-runtime/runtime-benchmarks", "sp-staking/runtime-benchmarks", + "pallet-asset-rewards/runtime-benchmarks" ] try-runtime = [ "frame-benchmarking-pallet-pov/try-runtime", @@ -426,6 +429,7 @@ try-runtime = [ "pallet-vesting/try-runtime", "pallet-whitelist/try-runtime", "sp-runtime/try-runtime", + "pallet-asset-rewards/try-runtime" ] experimental = [ "frame-support/experimental", diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 09326de2732..ff949bdb897 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -71,11 +71,13 @@ use pallet_im_online::sr25519::AuthorityId as ImOnlineId; use pallet_nfts::PalletFeatures; use pallet_nis::WithMaximumOf; use pallet_session::historical as pallet_session_historical; +use sp_core::TypedGet; // Can't use `FungibleAdapter` here until Treasury pallet migrates to fungibles // <https://github.com/paritytech/polkadot-sdk/issues/226> #[allow(deprecated)] pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment}; use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo}; +use pallet_treasury::TreasuryAccountId; use pallet_tx_pause::RuntimeCallNameOf; use sp_api::impl_runtime_apis; use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId; @@ -1723,6 +1725,45 @@ impl pallet_asset_conversion::Config for Runtime { type BenchmarkHelper = (); } +pub type NativeAndAssets = + UnionOf<Balances, Assets, NativeFromLeft, NativeOrWithId<u32>, AccountId>; + +parameter_types! { + pub const StakingRewardsPalletId: PalletId = PalletId(*b"py/stkrd"); +} + +/// Benchmark Helper +#[cfg(feature = "runtime-benchmarks")] +pub struct AssetRewardsBenchmarkHelper; + +#[cfg(feature = "runtime-benchmarks")] +impl pallet_asset_rewards::benchmarking::BenchmarkHelper<NativeOrWithId<u32>, AccountId> + for AssetRewardsBenchmarkHelper +{ + fn to_asset_id(seed: NativeOrWithId<u32>) -> NativeOrWithId<u32> { + 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 permissioned_pool_creator() -> AccountId { + TreasuryAccountId::<Runtime>::get() + } +} + +impl pallet_asset_rewards::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type AssetId = NativeOrWithId<u32>; + type Balance = u128; + type Assets = NativeAndAssets; + type PalletId = StakingRewardsPalletId; + type PermissionedPoolCreator = EnsureRoot<AccountId>; + #[cfg(feature = "runtime-benchmarks")] + type BenchmarkHelper = AssetRewardsBenchmarkHelper; +} + parameter_types! { pub const QueueCount: u32 = 300; pub const MaxQueueLen: u32 = 1000; @@ -2450,6 +2491,9 @@ mod runtime { #[runtime::pallet_index(78)] pub type PalletExampleMbms = pallet_example_mbm; + + #[runtime::pallet_index(79)] + pub type AssetRewards = pallet_asset_rewards; } /// The address format for describing accounts. @@ -2562,6 +2606,7 @@ mod benches { [tasks_example, TasksExample] [pallet_democracy, Democracy] [pallet_asset_conversion, AssetConversion] + [pallet_asset_rewards, AssetRewards] [pallet_election_provider_multi_phase, ElectionProviderMultiPhase] [pallet_election_provider_support_benchmarking, EPSBench::<Runtime>] [pallet_elections_phragmen, Elections] diff --git a/substrate/frame/asset-rewards/src/benchmarking.rs b/substrate/frame/asset-rewards/src/benchmarking.rs index b91084a61c0..3b50e718d34 100644 --- a/substrate/frame/asset-rewards/src/benchmarking.rs +++ b/substrate/frame/asset-rewards/src/benchmarking.rs @@ -27,16 +27,18 @@ use frame_support::{ fungibles::{Create, Inspect, Mutate}, }, }; -use frame_system::RawOrigin as SystemOrigin; +use frame_system::RawOrigin; use sp_runtime::traits::One; use sp_std::prelude::*; /// Benchmark Helper pub trait BenchmarkHelper<AssetId, AccountId> { - /// When a specific asset, such as the native asset, is required in every pool, it should be - /// returned for each odd-numbered seed. + /// 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; + /// Get the permissioned pool creator + fn permissioned_pool_creator() -> AccountId; } impl<AssetId, AccountId> BenchmarkHelper<AssetId, AccountId> for () @@ -50,6 +52,9 @@ where fn to_account_id(seed: u32) -> AccountId { seed.into() } + fn permissioned_pool_creator() -> AccountId { + 1u32.into() + } } /// Create the `asset` and mint the `amount` for the `caller`. @@ -83,7 +88,7 @@ mod benchmarks { fn create_pool() { use super::*; - let admin = T::BenchmarkHelper::to_account_id(1); + let admin = T::BenchmarkHelper::permissioned_pool_creator(); let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1)); let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native); create_asset::<T>(&admin, &staked_asset, T::Assets::minimum_balance(staked_asset.clone())); @@ -91,7 +96,7 @@ mod benchmarks { #[extrinsic_call] _( - SystemOrigin::Signed(admin.clone()), + RawOrigin::Signed(admin.clone()), Box::new(staked_asset.clone()), Box::new(reward_asset.clone()), 100u32.into(), @@ -117,7 +122,7 @@ mod benchmarks { fn stake() { use super::*; - let admin = T::BenchmarkHelper::to_account_id(1); + let admin = T::BenchmarkHelper::permissioned_pool_creator(); 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); @@ -125,7 +130,7 @@ mod benchmarks { create_asset::<T>(&admin, &reward_asset, T::Assets::minimum_balance(reward_asset.clone())); assert_ok!(AssetRewards::<T>::create_pool( - SystemOrigin::Signed(admin.clone()).into(), + RawOrigin::Signed(admin.clone()).into(), Box::new(staked_asset.clone()), Box::new(reward_asset.clone()), 100u32.into(), @@ -134,7 +139,7 @@ mod benchmarks { )); #[extrinsic_call] - _(SystemOrigin::Signed(staker.clone()), 0u32.into(), 100u32.into()); + _(RawOrigin::Signed(staker.clone()), 0u32.into(), 100u32.into()); assert_last_event::<T>( Event::Staked { who: staker, pool_id: 0u32.into(), amount: 100u32.into() }.into(), @@ -145,7 +150,7 @@ mod benchmarks { fn unstake() { use super::*; - let admin = T::BenchmarkHelper::to_account_id(1); + let admin = T::BenchmarkHelper::permissioned_pool_creator(); 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); @@ -153,7 +158,7 @@ mod benchmarks { create_asset::<T>(&admin, &reward_asset, T::Assets::minimum_balance(reward_asset.clone())); assert_ok!(AssetRewards::<T>::create_pool( - SystemOrigin::Signed(admin.clone()).into(), + RawOrigin::Signed(admin.clone()).into(), Box::new(staked_asset.clone()), Box::new(reward_asset.clone()), 100u32.into(), @@ -162,13 +167,13 @@ mod benchmarks { )); assert_ok!(AssetRewards::<T>::stake( - SystemOrigin::Signed(staker.clone()).into(), + RawOrigin::Signed(staker.clone()).into(), 0u32.into(), 100u32.into() )); #[extrinsic_call] - _(SystemOrigin::Signed(staker.clone()), 0u32.into(), 100u32.into()); + _(RawOrigin::Signed(staker.clone()), 0u32.into(), 100u32.into()); assert_last_event::<T>( Event::Unstaked { who: staker, pool_id: 0u32.into(), amount: 100u32.into() }.into(), @@ -179,7 +184,7 @@ mod benchmarks { fn harvest_rewards() { use super::*; - let admin = T::BenchmarkHelper::to_account_id(1); + let admin = T::BenchmarkHelper::permissioned_pool_creator(); 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); @@ -187,7 +192,7 @@ mod benchmarks { create_asset::<T>(&admin, &reward_asset, T::Assets::minimum_balance(reward_asset.clone())); assert_ok!(AssetRewards::<T>::create_pool( - SystemOrigin::Signed(admin.clone()).into(), + RawOrigin::Signed(admin.clone()).into(), Box::new(staked_asset.clone()), Box::new(reward_asset.clone()), 100u32.into(), @@ -196,13 +201,13 @@ mod benchmarks { )); assert_ok!(AssetRewards::<T>::stake( - SystemOrigin::Signed(staker.clone()).into(), + RawOrigin::Signed(staker.clone()).into(), 0u32.into(), 100u32.into() )); #[extrinsic_call] - _(SystemOrigin::Signed(staker.clone()), 0u32.into(), None); + _(RawOrigin::Signed(staker.clone()), 0u32.into(), None); assert_last_event::<T>( Event::RewardsHarvested { @@ -219,14 +224,14 @@ mod benchmarks { fn set_pool_reward_rate_per_block() { use super::*; - let admin = T::BenchmarkHelper::to_account_id(1); + let admin = T::BenchmarkHelper::permissioned_pool_creator(); let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1)); let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native); create_asset::<T>(&admin, &staked_asset, T::Assets::minimum_balance(staked_asset.clone())); create_asset::<T>(&admin, &reward_asset, T::Assets::minimum_balance(reward_asset.clone())); assert_ok!(AssetRewards::<T>::create_pool( - SystemOrigin::Signed(admin.clone()).into(), + RawOrigin::Signed(admin.clone()).into(), Box::new(staked_asset.clone()), Box::new(reward_asset.clone()), 100u32.into(), @@ -235,7 +240,7 @@ mod benchmarks { )); #[extrinsic_call] - _(SystemOrigin::Signed(admin.clone()), 0u32.into(), 5u32.into()); + _(RawOrigin::Signed(admin.clone()), 0u32.into(), 5u32.into()); assert_last_event::<T>( Event::PoolRewardRateModified { @@ -250,7 +255,7 @@ mod benchmarks { fn set_pool_admin() { use super::*; - let admin = T::BenchmarkHelper::to_account_id(1); + let admin = T::BenchmarkHelper::permissioned_pool_creator(); 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); @@ -258,7 +263,7 @@ mod benchmarks { create_asset::<T>(&admin, &reward_asset, T::Assets::minimum_balance(reward_asset.clone())); assert_ok!(AssetRewards::<T>::create_pool( - SystemOrigin::Signed(admin.clone()).into(), + RawOrigin::Signed(admin.clone()).into(), Box::new(staked_asset.clone()), Box::new(reward_asset.clone()), 100u32.into(), @@ -267,7 +272,7 @@ mod benchmarks { )); #[extrinsic_call] - _(SystemOrigin::Signed(admin.clone()), 0u32.into(), new_admin.clone()); + _(RawOrigin::Signed(admin.clone()), 0u32.into(), new_admin.clone()); assert_last_event::<T>(Event::PoolAdminModified { pool_id: 0u32.into(), new_admin }.into()); } @@ -276,14 +281,14 @@ mod benchmarks { fn set_pool_expiry_block() { use super::*; - let admin = T::BenchmarkHelper::to_account_id(1); + let admin = T::BenchmarkHelper::permissioned_pool_creator(); let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1)); let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native); create_asset::<T>(&admin, &staked_asset, T::Assets::minimum_balance(staked_asset.clone())); create_asset::<T>(&admin, &reward_asset, T::Assets::minimum_balance(reward_asset.clone())); assert_ok!(AssetRewards::<T>::create_pool( - SystemOrigin::Signed(admin.clone()).into(), + RawOrigin::Signed(admin.clone()).into(), Box::new(staked_asset.clone()), Box::new(reward_asset.clone()), 100u32.into(), @@ -292,7 +297,7 @@ mod benchmarks { )); #[extrinsic_call] - _(SystemOrigin::Signed(admin.clone()), 0u32.into(), 1000u32.into()); + _(RawOrigin::Signed(admin.clone()), 0u32.into(), 1000u32.into()); assert_last_event::<T>( Event::PoolExpiryBlockModified { @@ -307,7 +312,7 @@ mod benchmarks { fn deposit_reward_tokens() { use super::*; - let admin = T::BenchmarkHelper::to_account_id(1); + let admin = T::BenchmarkHelper::permissioned_pool_creator(); let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1)); let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native); create_asset::<T>(&admin, &staked_asset, T::Assets::minimum_balance(staked_asset.clone())); @@ -315,7 +320,7 @@ mod benchmarks { T::Assets::set_balance(reward_asset.clone(), &admin, 100000u32.into()); assert_ok!(AssetRewards::<T>::create_pool( - SystemOrigin::Signed(admin.clone()).into(), + RawOrigin::Signed(admin.clone()).into(), Box::new(staked_asset.clone()), Box::new(reward_asset.clone()), 100u32.into(), @@ -326,7 +331,7 @@ mod benchmarks { let balance_before = T::Assets::balance(reward_asset.clone(), &admin); #[extrinsic_call] - _(SystemOrigin::Signed(admin.clone()), 0u32.into(), 10u32.into()); + _(RawOrigin::Signed(admin.clone()), 0u32.into(), 10u32.into()); let balance_after = T::Assets::balance(reward_asset.clone(), &admin); @@ -337,7 +342,7 @@ mod benchmarks { fn withdraw_reward_tokens() { use super::*; - let admin = T::BenchmarkHelper::to_account_id(1); + let admin = T::BenchmarkHelper::permissioned_pool_creator(); let staked_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::WithId(1)); let reward_asset = T::BenchmarkHelper::to_asset_id(NativeOrWithId::Native); create_asset::<T>(&admin, &staked_asset, T::Assets::minimum_balance(staked_asset.clone())); @@ -345,7 +350,7 @@ mod benchmarks { T::Assets::set_balance(reward_asset.clone(), &admin, 100000u32.into()); assert_ok!(AssetRewards::<T>::create_pool( - SystemOrigin::Signed(admin.clone()).into(), + RawOrigin::Signed(admin.clone()).into(), Box::new(staked_asset.clone()), Box::new(reward_asset.clone()), 100u32.into(), @@ -356,13 +361,13 @@ mod benchmarks { let balance_before = T::Assets::balance(reward_asset.clone(), &admin); assert_ok!(AssetRewards::<T>::deposit_reward_tokens( - SystemOrigin::Signed(admin.clone()).into(), + RawOrigin::Signed(admin.clone()).into(), 0u32.into(), 10u32.into() )); #[extrinsic_call] - _(SystemOrigin::Signed(admin.clone()), 0u32.into(), 5u32.into()); + _(RawOrigin::Signed(admin.clone()), 0u32.into(), 5u32.into()); let balance_after = T::Assets::balance(reward_asset.clone(), &admin); diff --git a/substrate/frame/asset-rewards/src/lib.rs b/substrate/frame/asset-rewards/src/lib.rs index b1af2847719..9f1c9083dfc 100644 --- a/substrate/frame/asset-rewards/src/lib.rs +++ b/substrate/frame/asset-rewards/src/lib.rs @@ -92,7 +92,7 @@ use sp_runtime::DispatchError; use sp_std::boxed::Box; #[cfg(feature = "runtime-benchmarks")] -mod benchmarking; +pub mod benchmarking; #[cfg(test)] mod mock; #[cfg(test)] -- GitLab