Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions pallets/commitments/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub mod pallet {

/// The rate limit for commitments
#[pallet::constant]
type RateLimit: Get<BlockNumberFor<Self>>;
type DefaultRateLimit: Get<BlockNumberFor<Self>>;
}

#[pallet::event]
Expand All @@ -83,6 +83,16 @@ pub mod pallet {
CommitmentSetRateLimitExceeded,
}

#[pallet::type_value]
/// Default value for commitment rate limit.
pub fn DefaultRateLimit<T: Config>() -> BlockNumberFor<T> {
T::DefaultRateLimit::get()
}

/// The rate limit for commitments
#[pallet::storage]
pub type RateLimit<T> = StorageValue<_, BlockNumberFor<T>, ValueQuery, DefaultRateLimit<T>>;

/// Identity data by account
#[pallet::storage]
#[pallet::getter(fn commitment_of)]
Expand Down Expand Up @@ -137,7 +147,7 @@ pub mod pallet {
let cur_block = <frame_system::Pallet<T>>::block_number();
if let Some(last_commit) = <LastCommitment<T>>::get(netuid, &who) {
ensure!(
cur_block >= last_commit.saturating_add(T::RateLimit::get()),
cur_block >= last_commit.saturating_add(RateLimit::<T>::get()),
Error::<T>::CommitmentSetRateLimitExceeded
);
}
Expand Down Expand Up @@ -173,6 +183,19 @@ pub mod pallet {

Ok(())
}

/// Sudo-set the commitment rate limit
#[pallet::call_index(1)]
#[pallet::weight((
T::WeightInfo::set_rate_limit(),
DispatchClass::Operational,
Pays::No
))]
pub fn set_rate_limit(origin: OriginFor<T>, rate_limit_blocks: u32) -> DispatchResult {
ensure_root(origin)?;
RateLimit::<T>::set(rate_limit_blocks.into());
Ok(())
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pallets/commitments/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl pallet_commitments::Config for Test {
type CanCommit = ();
type FieldDeposit = frame_support::traits::ConstU64<0>;
type InitialDeposit = frame_support::traits::ConstU64<0>;
type RateLimit = frame_support::traits::ConstU64<0>;
type DefaultRateLimit = frame_support::traits::ConstU64<0>;
}

// // Build genesis storage according to the mock runtime.
Expand Down
12 changes: 12 additions & 0 deletions pallets/commitments/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use core::marker::PhantomData;
/// Weight functions needed for `pallet_commitments`.
pub trait WeightInfo {
fn set_commitment() -> Weight;
fn set_rate_limit() -> Weight;
}

/// Weights for `pallet_commitments` using the Substrate node and recommended hardware.
Expand All @@ -48,6 +49,11 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
/// Sudo setting rate limit for commitments
fn set_rate_limit() -> Weight {
Weight::from_parts(10_000_000, 2000)
.saturating_add(RocksDbWeight::get().reads(1_u64))
}
}

// For backwards compatibility and tests.
Expand All @@ -65,4 +71,10 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}

/// Sudo setting rate limit for commitments
fn set_rate_limit() -> Weight {
Weight::from_parts(10_000_000, 2000)
.saturating_add(RocksDbWeight::get().reads(1_u64))
}
}
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ impl pallet_commitments::Config for Runtime {
type MaxFields = MaxCommitFields;
type InitialDeposit = CommitmentInitialDeposit;
type FieldDeposit = CommitmentFieldDeposit;
type RateLimit = CommitmentRateLimit;
type DefaultRateLimit = CommitmentRateLimit;
}

#[cfg(not(feature = "fast-blocks"))]
Expand Down
Loading