Skip to content

Commit 47038b4

Browse files
authored
Merge pull request #1986 from opentensor/reveal-tl-commitments-weights
Fix/Return Weight When Revealing Timelocked Commitments
2 parents 032cd5e + e7def5d commit 47038b4

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

pallets/commitments/src/lib.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ pub use types::*;
1515
pub use weights::WeightInfo;
1616

1717
use ark_serialize::CanonicalDeserialize;
18-
use frame_support::{BoundedVec, traits::Currency};
18+
use frame_support::{
19+
BoundedVec,
20+
traits::{Currency, Get},
21+
};
1922
use scale_info::prelude::collections::BTreeSet;
2023
use sp_runtime::SaturatedConversion;
21-
use sp_runtime::{Saturating, traits::Zero};
24+
use sp_runtime::{Saturating, Weight, traits::Zero};
2225
use sp_std::{boxed::Box, vec::Vec};
2326
use subtensor_runtime_common::NetUid;
2427
use tle::{
@@ -344,10 +347,13 @@ pub mod pallet {
344347
#[pallet::hooks]
345348
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
346349
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
347-
if let Err(e) = Self::reveal_timelocked_commitments() {
348-
log::debug!("Failed to unveil matured commitments on block {n:?}: {e:?}");
350+
match Self::reveal_timelocked_commitments() {
351+
Ok(w) => w,
352+
Err(e) => {
353+
log::debug!("Failed to unveil matured commitments on block {n:?}: {e:?}");
354+
Weight::from_parts(0, 0)
355+
}
349356
}
350-
Weight::from_parts(0, 0)
351357
}
352358
}
353359
}
@@ -384,13 +390,22 @@ pub enum CallType {
384390
use frame_support::{dispatch::DispatchResult, pallet_prelude::TypeInfo};
385391

386392
impl<T: Config> Pallet<T> {
387-
pub fn reveal_timelocked_commitments() -> DispatchResult {
393+
pub fn reveal_timelocked_commitments() -> Result<Weight, sp_runtime::DispatchError> {
394+
let mut total_weight = Weight::from_parts(0, 0);
395+
388396
let index = TimelockedIndex::<T>::get();
397+
total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1));
398+
389399
for (netuid, who) in index.clone() {
390-
let Some(mut registration) = <CommitmentOf<T>>::get(netuid, &who) else {
400+
let maybe_registration = <CommitmentOf<T>>::get(netuid, &who);
401+
total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1));
402+
403+
let Some(mut registration) = maybe_registration else {
391404
TimelockedIndex::<T>::mutate(|idx| {
392405
idx.remove(&(netuid, who.clone()));
393406
});
407+
408+
total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
394409
continue;
395410
};
396411

@@ -404,6 +419,7 @@ impl<T: Config> Pallet<T> {
404419
encrypted,
405420
reveal_round,
406421
} => {
422+
total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1));
407423
let pulse = match pallet_drand::Pulses::<T>::get(reveal_round) {
408424
Some(p) => p,
409425
None => {
@@ -471,6 +487,7 @@ impl<T: Config> Pallet<T> {
471487
if !revealed_fields.is_empty() {
472488
let mut existing_reveals =
473489
RevealedCommitments::<T>::get(netuid, &who).unwrap_or_default();
490+
total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1));
474491

475492
let current_block = <frame_system::Pallet<T>>::block_number();
476493
let block_u64 = current_block.saturated_into::<u64>();
@@ -492,6 +509,7 @@ impl<T: Config> Pallet<T> {
492509
}
493510

494511
RevealedCommitments::<T>::insert(netuid, &who, existing_reveals);
512+
total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1));
495513
}
496514

497515
registration.info.fields = BoundedVec::try_from(remain_fields)
@@ -500,12 +518,19 @@ impl<T: Config> Pallet<T> {
500518
match registration.info.fields.is_empty() {
501519
true => {
502520
<CommitmentOf<T>>::remove(netuid, &who);
521+
total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1));
522+
503523
TimelockedIndex::<T>::mutate(|idx| {
504524
idx.remove(&(netuid, who.clone()));
505525
});
526+
527+
total_weight =
528+
total_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
506529
}
507530
false => {
508531
<CommitmentOf<T>>::insert(netuid, &who, &registration);
532+
total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1));
533+
509534
let has_timelock = registration
510535
.info
511536
.fields
@@ -515,11 +540,14 @@ impl<T: Config> Pallet<T> {
515540
TimelockedIndex::<T>::mutate(|idx| {
516541
idx.remove(&(netuid, who.clone()));
517542
});
543+
544+
total_weight =
545+
total_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
518546
}
519547
}
520548
}
521549
}
522550

523-
Ok(())
551+
Ok(total_weight)
524552
}
525553
}

0 commit comments

Comments
 (0)