Skip to content

Commit 812bc59

Browse files
committed
extract subnet leasing weights + match to if let else
1 parent 96ae67c commit 812bc59

File tree

2 files changed

+40
-46
lines changed

2 files changed

+40
-46
lines changed

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use frame_support::pallet_macros::pallet_section;
66
/// This can later be imported into the pallet using [`import_section`].
77
#[pallet_section]
88
mod dispatches {
9+
use crate::subnets::leasing::SubnetLeasingWeightInfo;
910
use frame_support::traits::schedule::DispatchTime;
1011
use frame_support::traits::schedule::v3::Anon as ScheduleAnon;
1112
use frame_system::pallet_prelude::BlockNumberFor;
@@ -2357,16 +2358,7 @@ mod dispatches {
23572358
/// * `end_block` (Option<BlockNumberFor<T>>):
23582359
/// - The block at which the lease will end. If not defined, the lease is perpetual.
23592360
#[pallet::call_index(109)]
2360-
#[pallet::weight({
2361-
let k = T::MaxContributors::get().into();
2362-
Weight::from_parts(301_560_714, 10079)
2363-
.saturating_add(Weight::from_parts(26_884_006, 0).saturating_mul(k))
2364-
.saturating_add(T::DbWeight::get().reads(41_u64))
2365-
.saturating_add(T::DbWeight::get().reads((2_u64).saturating_mul(k)))
2366-
.saturating_add(T::DbWeight::get().writes(55_u64))
2367-
.saturating_add(T::DbWeight::get().writes((2_u64).saturating_mul(k)))
2368-
.saturating_add(Weight::from_parts(0, 2579).saturating_mul(k))
2369-
})]
2361+
#[pallet::weight(SubnetLeasingWeightInfo::<T>::do_register_leased_network(T::MaxContributors::get()))]
23702362
pub fn register_leased_network(
23712363
origin: T::RuntimeOrigin,
23722364
emissions_share: Percent,
@@ -2392,16 +2384,7 @@ mod dispatches {
23922384
/// * `hotkey` (T::AccountId):
23932385
/// - The hotkey of the beneficiary to mark as subnet owner hotkey.
23942386
#[pallet::call_index(110)]
2395-
#[pallet::weight({
2396-
let k = T::MaxContributors::get().into();
2397-
Weight::from_parts(56_635_122, 6148)
2398-
.saturating_add(Weight::from_parts(912_993, 0).saturating_mul(k))
2399-
.saturating_add(T::DbWeight::get().reads(4_u64))
2400-
.saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(k)))
2401-
.saturating_add(T::DbWeight::get().writes(6_u64))
2402-
.saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(k)))
2403-
.saturating_add(Weight::from_parts(0, 2529).saturating_mul(k))
2404-
})]
2387+
#[pallet::weight(SubnetLeasingWeightInfo::<T>::do_terminate_lease(T::MaxContributors::get()))]
24052388
pub fn terminate_lease(
24062389
origin: T::RuntimeOrigin,
24072390
lease_id: LeaseId,

pallets/subtensor/src/subnets/leasing.rs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,12 @@ impl<T: Config> Pallet<T> {
156156

157157
if crowdloan.contributors_count < T::MaxContributors::get() {
158158
// We have less contributors than the max allowed, so we need to refund the difference
159-
let k = crowdloan.contributors_count.into();
160-
Ok(Some(
161-
Weight::from_parts(301_560_714, 10079)
162-
.saturating_add(Weight::from_parts(26_884_006, 0).saturating_mul(k))
163-
.saturating_add(T::DbWeight::get().reads(41_u64))
164-
.saturating_add(T::DbWeight::get().reads(2_u64.saturating_mul(k)))
165-
.saturating_add(T::DbWeight::get().writes(55_u64))
166-
.saturating_add(T::DbWeight::get().writes(2_u64.saturating_mul(k)))
167-
.saturating_add(Weight::from_parts(0, 2579).saturating_mul(k)),
159+
Ok(
160+
Some(SubnetLeasingWeightInfo::<T>::do_register_leased_network(
161+
crowdloan.contributors_count,
162+
))
163+
.into(),
168164
)
169-
.into())
170165
} else {
171166
// We have the max number of contributors, so we don't need to refund anything
172167
Ok(().into())
@@ -224,16 +219,9 @@ impl<T: Config> Pallet<T> {
224219

225220
if clear_result.unique < T::MaxContributors::get() {
226221
// We have cleared less than the max number of shareholders, so we need to refund the difference
227-
let k = clear_result.unique.into();
228-
Ok(Some(
229-
Weight::from_parts(56_635_122, 6148)
230-
.saturating_add(Weight::from_parts(912_993, 0).saturating_mul(k))
231-
.saturating_add(T::DbWeight::get().reads(4_u64))
232-
.saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(k)))
233-
.saturating_add(T::DbWeight::get().writes(6_u64))
234-
.saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(k)))
235-
.saturating_add(Weight::from_parts(0, 2529).saturating_mul(k)),
236-
)
222+
Ok(Some(SubnetLeasingWeightInfo::<T>::do_terminate_lease(
223+
clear_result.unique,
224+
))
237225
.into())
238226
} else {
239227
// We have cleared the max number of shareholders, so we don't need to refund anything
@@ -248,12 +236,11 @@ impl<T: Config> Pallet<T> {
248236
/// and if not enough liquidity is available, it will accumulate the dividends for later distribution.
249237
pub fn distribute_leased_network_dividends(lease_id: LeaseId, owner_cut_alpha: u64) {
250238
// Ensure the lease exists
251-
let lease = match SubnetLeases::<T>::get(lease_id) {
252-
Some(lease) => lease,
253-
None => {
254-
log::debug!("Lease {lease_id} doesn't exists so we can't distribute dividends");
255-
return;
256-
}
239+
let lease = if let Some(lease) = SubnetLeases::<T>::get(lease_id) {
240+
lease
241+
} else {
242+
log::debug!("Lease {lease_id} doesn't exists so we can't distribute dividends");
243+
return;
257244
};
258245

259246
// Ensure the lease has not ended
@@ -380,3 +367,27 @@ impl<T: Config> Pallet<T> {
380367
Ok((crowdloan_id, crowdloan))
381368
}
382369
}
370+
371+
/// Weight functions needed for subnet leasing.
372+
pub struct SubnetLeasingWeightInfo<T>(PhantomData<T>);
373+
impl<T: frame_system::Config> SubnetLeasingWeightInfo<T> {
374+
pub fn do_register_leased_network(k: u32) -> Weight {
375+
Weight::from_parts(301_560_714, 10079)
376+
.saturating_add(Weight::from_parts(26_884_006, 0).saturating_mul(k.into()))
377+
.saturating_add(T::DbWeight::get().reads(41_u64))
378+
.saturating_add(T::DbWeight::get().reads(2_u64.saturating_mul(k.into())))
379+
.saturating_add(T::DbWeight::get().writes(55_u64))
380+
.saturating_add(T::DbWeight::get().writes(2_u64.saturating_mul(k.into())))
381+
.saturating_add(Weight::from_parts(0, 2579).saturating_mul(k.into()))
382+
}
383+
384+
pub fn do_terminate_lease(k: u32) -> Weight {
385+
Weight::from_parts(56_635_122, 6148)
386+
.saturating_add(Weight::from_parts(912_993, 0).saturating_mul(k.into()))
387+
.saturating_add(T::DbWeight::get().reads(4_u64))
388+
.saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(k.into())))
389+
.saturating_add(T::DbWeight::get().writes(6_u64))
390+
.saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(k.into())))
391+
.saturating_add(Weight::from_parts(0, 2529).saturating_mul(k.into()))
392+
}
393+
}

0 commit comments

Comments
 (0)