diff --git a/pallets/commitments/src/lib.rs b/pallets/commitments/src/lib.rs index a7d887b877..d8c36079fd 100644 --- a/pallets/commitments/src/lib.rs +++ b/pallets/commitments/src/lib.rs @@ -15,10 +15,13 @@ pub use types::*; pub use weights::WeightInfo; use ark_serialize::CanonicalDeserialize; -use frame_support::{BoundedVec, traits::Currency}; +use frame_support::{ + BoundedVec, + traits::{Currency, Get}, +}; use scale_info::prelude::collections::BTreeSet; use sp_runtime::SaturatedConversion; -use sp_runtime::{Saturating, traits::Zero}; +use sp_runtime::{Saturating, Weight, traits::Zero}; use sp_std::{boxed::Box, vec::Vec}; use subtensor_runtime_common::NetUid; use tle::{ @@ -344,10 +347,13 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { fn on_initialize(n: BlockNumberFor) -> Weight { - if let Err(e) = Self::reveal_timelocked_commitments() { - log::debug!("Failed to unveil matured commitments on block {n:?}: {e:?}"); + match Self::reveal_timelocked_commitments() { + Ok(w) => w, + Err(e) => { + log::debug!("Failed to unveil matured commitments on block {n:?}: {e:?}"); + Weight::from_parts(0, 0) + } } - Weight::from_parts(0, 0) } } } @@ -384,13 +390,22 @@ pub enum CallType { use frame_support::{dispatch::DispatchResult, pallet_prelude::TypeInfo}; impl Pallet { - pub fn reveal_timelocked_commitments() -> DispatchResult { + pub fn reveal_timelocked_commitments() -> Result { + let mut total_weight = Weight::from_parts(0, 0); + let index = TimelockedIndex::::get(); + total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1)); + for (netuid, who) in index.clone() { - let Some(mut registration) = >::get(netuid, &who) else { + let maybe_registration = >::get(netuid, &who); + total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1)); + + let Some(mut registration) = maybe_registration else { TimelockedIndex::::mutate(|idx| { idx.remove(&(netuid, who.clone())); }); + + total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); continue; }; @@ -404,6 +419,7 @@ impl Pallet { encrypted, reveal_round, } => { + total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1)); let pulse = match pallet_drand::Pulses::::get(reveal_round) { Some(p) => p, None => { @@ -471,6 +487,7 @@ impl Pallet { if !revealed_fields.is_empty() { let mut existing_reveals = RevealedCommitments::::get(netuid, &who).unwrap_or_default(); + total_weight = total_weight.saturating_add(T::DbWeight::get().reads(1)); let current_block = >::block_number(); let block_u64 = current_block.saturated_into::(); @@ -492,6 +509,7 @@ impl Pallet { } RevealedCommitments::::insert(netuid, &who, existing_reveals); + total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1)); } registration.info.fields = BoundedVec::try_from(remain_fields) @@ -500,12 +518,19 @@ impl Pallet { match registration.info.fields.is_empty() { true => { >::remove(netuid, &who); + total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1)); + TimelockedIndex::::mutate(|idx| { idx.remove(&(netuid, who.clone())); }); + + total_weight = + total_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); } false => { >::insert(netuid, &who, ®istration); + total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1)); + let has_timelock = registration .info .fields @@ -515,11 +540,14 @@ impl Pallet { TimelockedIndex::::mutate(|idx| { idx.remove(&(netuid, who.clone())); }); + + total_weight = + total_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); } } } } - Ok(()) + Ok(total_weight) } }