Skip to content

Commit 845cba9

Browse files
committed
Make commitment pallet rate limit configurable via sudo
1 parent a1d7e87 commit 845cba9

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

pallets/commitments/src/lib.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub mod pallet {
5858

5959
/// The rate limit for commitments
6060
#[pallet::constant]
61-
type RateLimit: Get<BlockNumberFor<Self>>;
61+
type DefaultRateLimit: Get<BlockNumberFor<Self>>;
6262
}
6363

6464
#[pallet::event]
@@ -83,6 +83,16 @@ pub mod pallet {
8383
CommitmentSetRateLimitExceeded,
8484
}
8585

86+
#[pallet::type_value]
87+
/// Default value for commitment rate limit.
88+
pub fn DefaultRateLimit<T: Config>() -> BlockNumberFor<T> {
89+
T::DefaultRateLimit::get()
90+
}
91+
92+
/// The rate limit for commitments
93+
#[pallet::storage]
94+
pub type RateLimit<T> = StorageValue<_, BlockNumberFor<T>, ValueQuery, DefaultRateLimit<T>>;
95+
8696
/// Identity data by account
8797
#[pallet::storage]
8898
#[pallet::getter(fn commitment_of)]
@@ -137,7 +147,7 @@ pub mod pallet {
137147
let cur_block = <frame_system::Pallet<T>>::block_number();
138148
if let Some(last_commit) = <LastCommitment<T>>::get(netuid, &who) {
139149
ensure!(
140-
cur_block >= last_commit.saturating_add(T::RateLimit::get()),
150+
cur_block >= last_commit.saturating_add(RateLimit::<T>::get()),
141151
Error::<T>::CommitmentSetRateLimitExceeded
142152
);
143153
}
@@ -173,6 +183,19 @@ pub mod pallet {
173183

174184
Ok(())
175185
}
186+
187+
/// Sudo-set the commitment rate limit
188+
#[pallet::call_index(1)]
189+
#[pallet::weight((
190+
T::WeightInfo::set_rate_limit(),
191+
DispatchClass::Operational,
192+
Pays::No
193+
))]
194+
pub fn set_rate_limit(origin: OriginFor<T>, rate_limit_blocks: u32) -> DispatchResult {
195+
ensure_root(origin)?;
196+
RateLimit::<T>::set(rate_limit_blocks.into());
197+
Ok(())
198+
}
176199
}
177200
}
178201

pallets/commitments/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl pallet_commitments::Config for Test {
8686
type CanCommit = ();
8787
type FieldDeposit = frame_support::traits::ConstU64<0>;
8888
type InitialDeposit = frame_support::traits::ConstU64<0>;
89-
type RateLimit = frame_support::traits::ConstU64<0>;
89+
type DefaultRateLimit = frame_support::traits::ConstU64<0>;
9090
}
9191

9292
// // Build genesis storage according to the mock runtime.

pallets/commitments/src/weights.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use core::marker::PhantomData;
3030
/// Weight functions needed for `pallet_commitments`.
3131
pub trait WeightInfo {
3232
fn set_commitment() -> Weight;
33+
fn set_rate_limit() -> Weight;
3334
}
3435

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

5359
// For backwards compatibility and tests.
@@ -65,4 +71,10 @@ impl WeightInfo for () {
6571
.saturating_add(RocksDbWeight::get().reads(2_u64))
6672
.saturating_add(RocksDbWeight::get().writes(2_u64))
6773
}
74+
75+
/// Sudo setting rate limit for commitments
76+
fn set_rate_limit() -> Weight {
77+
Weight::from_parts(10_000_000, 2000)
78+
.saturating_add(RocksDbWeight::get().reads(1_u64))
79+
}
6880
}

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ impl pallet_commitments::Config for Runtime {
974974
type MaxFields = MaxCommitFields;
975975
type InitialDeposit = CommitmentInitialDeposit;
976976
type FieldDeposit = CommitmentFieldDeposit;
977-
type RateLimit = CommitmentRateLimit;
977+
type DefaultRateLimit = CommitmentRateLimit;
978978
}
979979

980980
#[cfg(not(feature = "fast-blocks"))]

0 commit comments

Comments
 (0)