Skip to content

Commit

Permalink
benchmarks for both unstake scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Feb 7, 2025
1 parent 437d9a7 commit aaf2900
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
56 changes: 56 additions & 0 deletions pallets/dapp-staking/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,62 @@ mod benchmarks {
);
}

#[benchmark]
fn move_stake_unregistered_source() {
initial_config::<T>();

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::<T>::register(
RawOrigin::Root.into(),
owner.clone().into(),
source_contract.clone(),
));
assert_ok!(DappStaking::<T>::register(
RawOrigin::Root.into(),
owner.clone().into(),
destination_contract.clone(),
));

let amount = T::MinimumLockedAmount::get();
T::BenchmarkHelper::set_balance(&staker, amount);
assert_ok!(DappStaking::<T>::lock(
RawOrigin::Signed(staker.clone()).into(),
amount,
));

assert_ok!(DappStaking::<T>::stake(
RawOrigin::Signed(staker.clone()).into(),
source_contract.clone(),
amount
));

assert_ok!(DappStaking::<T>::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::<T>(
Event::<T>::StakeMoved {
account: staker,
source_contract,
destination_contract,
amount,
}
.into(),
);
}

#[benchmark]
fn on_initialize_voting_to_build_and_earn() {
initial_config::<T>();
Expand Down
11 changes: 8 additions & 3 deletions pallets/dapp-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>,
source_contract: T::SmartContract,
destination_contract: T::SmartContract,
#[pallet::compact] amount: Balance,
) -> DispatchResult {
) -> DispatchResultWithPostInfo {
Self::ensure_pallet_enabled()?;
let account = ensure_signed(origin)?;

Expand Down Expand Up @@ -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())
}
}

Expand Down
2 changes: 1 addition & 1 deletion pallets/dapp-staking/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
43 changes: 43 additions & 0 deletions pallets/dapp-staking/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -417,6 +418,27 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit aaf2900

Please sign in to comment.