Skip to content

Refactor rate limits #1713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
13 changes: 9 additions & 4 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub mod pallet {
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
pub fn sudo_set_tx_rate_limit(origin: OriginFor<T>, tx_rate_limit: u64) -> DispatchResult {
ensure_root(origin)?;
pallet_subtensor::Pallet::<T>::set_tx_rate_limit(tx_rate_limit);
pallet_subtensor::Pallet::<T>::set_tx_rate_limit(tx_rate_limit, false);
log::debug!("TxRateLimitSet( tx_rate_limit: {:?} ) ", tx_rate_limit);
Ok(())
}
Expand All @@ -196,7 +196,11 @@ pub mod pallet {
) -> DispatchResult {
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;

pallet_subtensor::Pallet::<T>::set_serving_rate_limit(netuid, serving_rate_limit);
pallet_subtensor::Pallet::<T>::set_serving_rate_limit(
netuid,
serving_rate_limit,
false,
);
log::debug!(
"ServingRateLimitSet( serving_rate_limit: {:?} ) ",
serving_rate_limit
Expand Down Expand Up @@ -329,6 +333,7 @@ pub mod pallet {
pallet_subtensor::Pallet::<T>::set_weights_set_rate_limit(
netuid,
weights_set_rate_limit,
false,
);
log::debug!(
"WeightsSetRateLimitSet( netuid: {:?} weights_set_rate_limit: {:?} ) ",
Expand Down Expand Up @@ -908,7 +913,7 @@ pub mod pallet {
rate_limit: u64,
) -> DispatchResult {
ensure_root(origin)?;
pallet_subtensor::Pallet::<T>::set_network_rate_limit(rate_limit);
pallet_subtensor::Pallet::<T>::set_network_rate_limit(rate_limit, false);
log::debug!("NetworkRateLimit( rate_limit: {:?} ) ", rate_limit);
Ok(())
}
Expand Down Expand Up @@ -1093,7 +1098,7 @@ pub mod pallet {
tx_rate_limit: u64,
) -> DispatchResult {
ensure_root(origin)?;
pallet_subtensor::Pallet::<T>::set_tx_delegate_take_rate_limit(tx_rate_limit);
pallet_subtensor::Pallet::<T>::set_tx_delegate_take_rate_limit(tx_rate_limit, false);
log::debug!(
"TxRateLimitDelegateTakeSet( tx_delegate_take_rate_limit: {:?} ) ",
tx_rate_limit
Expand Down
10 changes: 5 additions & 5 deletions pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ mod pallet_benchmarks {
netuid,
caller.clone()
));
Subtensor::<T>::set_serving_rate_limit(netuid, 0);
Subtensor::<T>::set_serving_rate_limit(netuid, 0, false);

#[extrinsic_call]
_(
Expand Down Expand Up @@ -387,7 +387,7 @@ mod pallet_benchmarks {
netuid,
caller.clone()
));
Subtensor::<T>::set_serving_rate_limit(netuid, 0);
Subtensor::<T>::set_serving_rate_limit(netuid, 0, false);

#[extrinsic_call]
_(
Expand Down Expand Up @@ -451,7 +451,7 @@ mod pallet_benchmarks {
let coldkey: T::AccountId = account("Test", 0, seed);
let hotkey: T::AccountId = account("TestHotkey", 0, seed);

Subtensor::<T>::set_network_rate_limit(1);
Subtensor::<T>::set_network_rate_limit(1, false);
let amount: u64 = 100_000_000_000_000u64.saturating_mul(2);
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey, amount);

Expand Down Expand Up @@ -670,7 +670,7 @@ mod pallet_benchmarks {
Subtensor::<T>::set_network_registration_allowed(netuid, true);
Subtensor::<T>::set_network_pow_registration_allowed(netuid, true);
Subtensor::<T>::set_commit_reveal_weights_enabled(netuid, true);
Subtensor::<T>::set_weights_set_rate_limit(netuid, 0);
Subtensor::<T>::set_weights_set_rate_limit(netuid, 0, false);

let block_number: u64 = Subtensor::<T>::get_current_block_as_u64();
let (nonce, work) =
Expand Down Expand Up @@ -1303,7 +1303,7 @@ mod pallet_benchmarks {
let identity: Option<SubnetIdentityOfV2> = None;

Subtensor::<T>::set_network_registration_allowed(1, true);
Subtensor::<T>::set_network_rate_limit(1);
Subtensor::<T>::set_network_rate_limit(1, false);
let amount: u64 = 9_999_999_999_999;
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey, amount);

Expand Down
68 changes: 13 additions & 55 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,14 +581,6 @@ pub mod pallet {
T::InitialSubnetOwnerCut::get()
}
#[pallet::type_value]
/// Default value for network rate limit.
pub fn DefaultNetworkRateLimit<T: Config>() -> u64 {
if cfg!(feature = "pow-faucet") {
return 0;
}
T::InitialNetworkRateLimit::get()
}
#[pallet::type_value]
/// Default value for weights version key rate limit.
/// In units of tempos.
pub fn DefaultWeightsVersionKeyRateLimit<T: Config>() -> u64 {
Expand Down Expand Up @@ -626,11 +618,6 @@ pub mod pallet {
T::InitialTempo::get()
}
#[pallet::type_value]
/// Default value for weights set rate limit.
pub fn DefaultWeightsSetRateLimit<T: Config>() -> u64 {
100
}
#[pallet::type_value]
/// Default block number at registration.
pub fn DefaultBlockAtRegistration<T: Config>() -> u64 {
0
Expand Down Expand Up @@ -770,31 +757,11 @@ pub mod pallet {
// T::InitialHotkeyEmissionTempo::get()
// } (DEPRECATED)
#[pallet::type_value]
/// Default value for rate limiting
pub fn DefaultTxRateLimit<T: Config>() -> u64 {
T::InitialTxRateLimit::get()
}
#[pallet::type_value]
/// Default value for delegate take rate limiting
pub fn DefaultTxDelegateTakeRateLimit<T: Config>() -> u64 {
T::InitialTxDelegateTakeRateLimit::get()
}
#[pallet::type_value]
/// Default value for chidlkey take rate limiting
pub fn DefaultTxChildKeyTakeRateLimit<T: Config>() -> u64 {
T::InitialTxChildKeyTakeRateLimit::get()
}
#[pallet::type_value]
/// Default value for last extrinsic block.
pub fn DefaultLastTxBlock<T: Config>() -> u64 {
0
}
#[pallet::type_value]
/// Default value for serving rate limit.
pub fn DefaultServingRateLimit<T: Config>() -> u64 {
T::InitialServingRateLimit::get()
}
#[pallet::type_value]
/// Default value for weight commit/reveal enabled.
pub fn DefaultCommitRevealWeightsEnabled<T: Config>() -> bool {
false
Expand Down Expand Up @@ -1211,9 +1178,6 @@ pub mod pallet {
#[pallet::storage]
/// ITEM( subnet_owner_cut )
pub type SubnetOwnerCut<T> = StorageValue<_, u16, ValueQuery, DefaultSubnetOwnerCut<T>>;
#[pallet::storage]
/// ITEM( network_rate_limit )
pub type NetworkRateLimit<T> = StorageValue<_, u64, ValueQuery, DefaultNetworkRateLimit<T>>;
#[pallet::storage] // --- ITEM( nominator_min_required_stake )
pub type NominatorMinRequiredStake<T> = StorageValue<_, u64, ValueQuery, DefaultZeroU64<T>>;
#[pallet::storage]
Expand Down Expand Up @@ -1323,10 +1287,7 @@ pub mod pallet {
/// --- MAP ( netuid ) --> subnet_owner_hotkey
pub type SubnetOwnerHotkey<T: Config> =
StorageMap<_, Identity, u16, T::AccountId, ValueQuery, DefaultSubnetOwner<T>>;
#[pallet::storage]
/// --- MAP ( netuid ) --> serving_rate_limit
pub type ServingRateLimit<T> =
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultServingRateLimit<T>>;

#[pallet::storage]
/// --- MAP ( netuid ) --> Rho
pub type Rho<T> = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultRho<T>>;
Expand Down Expand Up @@ -1392,10 +1353,6 @@ pub mod pallet {
/// --- MAP ( netuid ) --> bonds_reset
pub type BondsResetOn<T> =
StorageMap<_, Identity, u16, bool, ValueQuery, DefaultBondsResetOn<T>>;
/// --- MAP ( netuid ) --> weights_set_rate_limit
#[pallet::storage]
pub type WeightsSetRateLimit<T> =
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultWeightsSetRateLimit<T>>;
#[pallet::storage]
/// --- MAP ( netuid ) --> validator_prune_len
pub type ValidatorPruneLen<T> =
Expand Down Expand Up @@ -1453,17 +1410,6 @@ pub mod pallet {
pub type RAORecycledForRegistration<T> =
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultRAORecycledForRegistration<T>>;
#[pallet::storage]
/// --- ITEM ( tx_rate_limit )
pub type TxRateLimit<T> = StorageValue<_, u64, ValueQuery, DefaultTxRateLimit<T>>;
#[pallet::storage]
/// --- ITEM ( tx_delegate_take_rate_limit )
pub type TxDelegateTakeRateLimit<T> =
StorageValue<_, u64, ValueQuery, DefaultTxDelegateTakeRateLimit<T>>;
#[pallet::storage]
/// --- ITEM ( tx_childkey_take_rate_limit )
pub type TxChildkeyTakeRateLimit<T> =
StorageValue<_, u64, ValueQuery, DefaultTxChildKeyTakeRateLimit<T>>;
#[pallet::storage]
/// --- MAP ( netuid ) --> Whether or not Liquid Alpha is enabled
pub type LiquidAlphaOn<T> =
StorageMap<_, Blake2_128Concat, u16, bool, ValueQuery, DefaultLiquidAlpha<T>>;
Expand Down Expand Up @@ -2699,4 +2645,16 @@ impl<T, H, P> CollectiveInterface<T, H, P> for () {
pub enum RateLimitKey {
// The setting sn owner hotkey operation is rate limited per netuid
SetSNOwnerHotkey(u16),
// An axon or prometheus serving rate limit for a registered neuron.
ServingRateLimit(u16),
// Limits transaction number
TxRateLimit,
// Limits set_weights operation
SetWeightsRateLimit(u16),
// Network rate limits
NetworkRateLimit,
// Rate limit for delegate take transactions
TxDelegateRateLimit,
// Rate limit for delegate childkey take transactions
TxChildkeyTakeRateLimit,
}
12 changes: 6 additions & 6 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,17 +1045,17 @@ mod dispatches {
#[pallet::call_index(69)]
#[pallet::weight((
Weight::from_parts(6_873_000, 0)
.saturating_add(T::DbWeight::get().reads(0))
.saturating_add(T::DbWeight::get().writes(1)),
DispatchClass::Operational,
Pays::No
))]
.saturating_add(T::DbWeight::get().reads(0))
.saturating_add(T::DbWeight::get().writes(1)),
DispatchClass::Operational,
Pays::No
))]
pub fn sudo_set_tx_childkey_take_rate_limit(
origin: OriginFor<T>,
tx_rate_limit: u64,
) -> DispatchResult {
ensure_root(origin)?;
Self::set_tx_childkey_take_rate_limit(tx_rate_limit);
Self::set_tx_childkey_take_rate_limit(tx_rate_limit, false);
Ok(())
}

Expand Down
4 changes: 3 additions & 1 deletion pallets/subtensor/src/macros/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ mod hooks {
// Reset max burn
.saturating_add(migrations::migrate_reset_max_burn::migrate_reset_max_burn::<T>())
// Migrate ColdkeySwapScheduled structure to new format
.saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::<T>());
.saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::<T>())
// Migrate rate limit maps to new format
.saturating_add(migrations::migrate_obsolete_rate_limiting_maps::migrate_obsolete_rate_limiting_maps::<T>());
weight
}

Expand Down
Loading
Loading