From aaf2900621502f5cd90c19d597855d72ee32b940 Mon Sep 17 00:00:00 2001 From: Igor Papandinas <26460174+ipapandinas@users.noreply.github.com> Date: Fri, 7 Feb 2025 19:39:59 +0400 Subject: [PATCH] benchmarks for both unstake scenarios --- pallets/dapp-staking/src/benchmarking/mod.rs | 56 ++++++++++++++++++++ pallets/dapp-staking/src/lib.rs | 11 ++-- pallets/dapp-staking/src/types.rs | 2 +- pallets/dapp-staking/src/weights.rs | 43 +++++++++++++++ 4 files changed, 108 insertions(+), 4 deletions(-) diff --git a/pallets/dapp-staking/src/benchmarking/mod.rs b/pallets/dapp-staking/src/benchmarking/mod.rs index a8510b66e..cf1175c78 100644 --- a/pallets/dapp-staking/src/benchmarking/mod.rs +++ b/pallets/dapp-staking/src/benchmarking/mod.rs @@ -893,6 +893,62 @@ mod benchmarks { ); } + #[benchmark] + fn move_stake_unregistered_source() { + initial_config::(); + + let staker: T::AccountId = whitelisted_caller(); + let owner: T::AccountId = account("dapp_owner", 0, SEED); + let source_contract = T::BenchmarkHelper::get_smart_contract(1); + let destination_contract = T::BenchmarkHelper::get_smart_contract(2); + assert_ok!(DappStaking::::register( + RawOrigin::Root.into(), + owner.clone().into(), + source_contract.clone(), + )); + assert_ok!(DappStaking::::register( + RawOrigin::Root.into(), + owner.clone().into(), + destination_contract.clone(), + )); + + let amount = T::MinimumLockedAmount::get(); + T::BenchmarkHelper::set_balance(&staker, amount); + assert_ok!(DappStaking::::lock( + RawOrigin::Signed(staker.clone()).into(), + amount, + )); + + assert_ok!(DappStaking::::stake( + RawOrigin::Signed(staker.clone()).into(), + source_contract.clone(), + amount + )); + + assert_ok!(DappStaking::::unregister( + RawOrigin::Root.into(), + source_contract.clone(), + )); + + #[extrinsic_call] + move_stake( + RawOrigin::Signed(staker.clone()), + source_contract.clone(), + destination_contract.clone(), + amount.clone(), + ); + + assert_last_event::( + Event::::StakeMoved { + account: staker, + source_contract, + destination_contract, + amount, + } + .into(), + ); + } + #[benchmark] fn on_initialize_voting_to_build_and_earn() { initial_config::(); diff --git a/pallets/dapp-staking/src/lib.rs b/pallets/dapp-staking/src/lib.rs index 8a2417abb..984553671 100644 --- a/pallets/dapp-staking/src/lib.rs +++ b/pallets/dapp-staking/src/lib.rs @@ -1340,13 +1340,13 @@ pub mod pallet { /// Transfers stake between two smart contracts, ensuring bonus status preservation if eligible. /// Emits a `StakeMoved` event. #[pallet::call_index(21)] - #[pallet::weight(T::WeightInfo::move_stake())] + #[pallet::weight(T::WeightInfo::move_stake_unregistered_source().max(T::WeightInfo::move_stake()))] pub fn move_stake( origin: OriginFor, source_contract: T::SmartContract, destination_contract: T::SmartContract, #[pallet::compact] amount: Balance, - ) -> DispatchResult { + ) -> DispatchResultWithPostInfo { Self::ensure_pallet_enabled()?; let account = ensure_signed(origin)?; @@ -1382,7 +1382,12 @@ pub mod pallet { amount: move_amount.total(), }); - Ok(()) + Ok(Some(if is_source_unregistered { + T::WeightInfo::move_stake_unregistered_source() + } else { + T::WeightInfo::move_stake() + }) + .into()) } } diff --git a/pallets/dapp-staking/src/types.rs b/pallets/dapp-staking/src/types.rs index 4cd6438de..862f35d81 100644 --- a/pallets/dapp-staking/src/types.rs +++ b/pallets/dapp-staking/src/types.rs @@ -911,7 +911,7 @@ impl StakeAmount { } // Compares `self` and `other` by total amount first or by lowest era in case of tie - pub fn compare_stake_amounts(&self, other: &StakeAmount) -> std::cmp::Ordering { + pub fn compare_stake_amounts(&self, other: &StakeAmount) -> sp_std::cmp::Ordering { let total_self = self.total(); let total_other = other.total(); diff --git a/pallets/dapp-staking/src/weights.rs b/pallets/dapp-staking/src/weights.rs index fdc55d079..439ec0f4e 100644 --- a/pallets/dapp-staking/src/weights.rs +++ b/pallets/dapp-staking/src/weights.rs @@ -69,6 +69,7 @@ pub trait WeightInfo { fn cleanup_expired_entries(x: u32, ) -> Weight; fn force() -> Weight; fn move_stake() -> Weight; + fn move_stake_unregistered_source() -> Weight; fn on_initialize_voting_to_build_and_earn() -> Weight; fn on_initialize_build_and_earn_to_voting() -> Weight; fn on_initialize_build_and_earn_to_build_and_earn() -> Weight; @@ -417,6 +418,27 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(9_u64)) .saturating_add(T::DbWeight::get().writes(6_u64)) } + /// Storage: `DappStaking::IntegratedDApps` (r:2 w:0) + /// Proof: `DappStaking::IntegratedDApps` (`max_values`: Some(65535), `max_size`: Some(116), added: 2096, mode: `MaxEncodedLen`) + /// Storage: `DappStaking::Ledger` (r:1 w:1) + /// Proof: `DappStaking::Ledger` (`max_values`: None, `max_size`: Some(310), added: 2785, mode: `MaxEncodedLen`) + /// Storage: `DappStaking::StakerInfo` (r:2 w:2) + /// Proof: `DappStaking::StakerInfo` (`max_values`: None, `max_size`: Some(179), added: 2654, mode: `MaxEncodedLen`) + /// Storage: `DappStaking::ContractStake` (r:2 w:2) + /// Proof: `DappStaking::ContractStake` (`max_values`: Some(65535), `max_size`: Some(91), added: 2071, mode: `MaxEncodedLen`) + /// Storage: `Balances::Freezes` (r:1 w:1) + /// Proof: `Balances::Freezes` (`max_values`: None, `max_size`: Some(67), added: 2542, mode: `MaxEncodedLen`) + /// Storage: `Balances::Locks` (r:1 w:0) + /// Proof: `Balances::Locks` (`max_values`: None, `max_size`: Some(1299), added: 3774, mode: `MaxEncodedLen`) + fn move_stake_unregistered_source() -> Weight { + // Proof Size summary in bytes: + // Measured: `536` + // Estimated: `6298` + // Minimum execution time: 59_000_000 picoseconds. + Weight::from_parts(60_000_000, 6298) + .saturating_add(T::DbWeight::get().reads(9_u64)) + .saturating_add(T::DbWeight::get().writes(6_u64)) + } /// Storage: DappStaking CurrentEraInfo (r:1 w:1) /// Proof: DappStaking CurrentEraInfo (max_values: Some(1), max_size: Some(112), added: 607, mode: MaxEncodedLen) /// Storage: DappStaking EraRewards (r:1 w:1) @@ -852,6 +874,27 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(9_u64)) .saturating_add(RocksDbWeight::get().writes(6_u64)) } + /// Storage: `DappStaking::IntegratedDApps` (r:2 w:0) + /// Proof: `DappStaking::IntegratedDApps` (`max_values`: Some(65535), `max_size`: Some(116), added: 2096, mode: `MaxEncodedLen`) + /// Storage: `DappStaking::Ledger` (r:1 w:1) + /// Proof: `DappStaking::Ledger` (`max_values`: None, `max_size`: Some(310), added: 2785, mode: `MaxEncodedLen`) + /// Storage: `DappStaking::StakerInfo` (r:2 w:2) + /// Proof: `DappStaking::StakerInfo` (`max_values`: None, `max_size`: Some(179), added: 2654, mode: `MaxEncodedLen`) + /// Storage: `DappStaking::ContractStake` (r:2 w:2) + /// Proof: `DappStaking::ContractStake` (`max_values`: Some(65535), `max_size`: Some(91), added: 2071, mode: `MaxEncodedLen`) + /// Storage: `Balances::Freezes` (r:1 w:1) + /// Proof: `Balances::Freezes` (`max_values`: None, `max_size`: Some(67), added: 2542, mode: `MaxEncodedLen`) + /// Storage: `Balances::Locks` (r:1 w:0) + /// Proof: `Balances::Locks` (`max_values`: None, `max_size`: Some(1299), added: 3774, mode: `MaxEncodedLen`) + fn move_stake_unregistered_source() -> Weight { + // Proof Size summary in bytes: + // Measured: `536` + // Estimated: `6298` + // Minimum execution time: 59_000_000 picoseconds. + Weight::from_parts(60_000_000, 6298) + .saturating_add(RocksDbWeight::get().reads(9_u64)) + .saturating_add(RocksDbWeight::get().writes(6_u64)) + } /// Storage: DappStaking CurrentEraInfo (r:1 w:1) /// Proof: DappStaking CurrentEraInfo (max_values: Some(1), max_size: Some(112), added: 607, mode: MaxEncodedLen) /// Storage: DappStaking EraRewards (r:1 w:1)