Skip to content

Commit 91c0d4a

Browse files
committed
Use StakeThreshold instead of WeightsMinStake and ChildkeyMinStake
1 parent b4d41d0 commit 91c0d4a

File tree

13 files changed

+91
-44
lines changed

13 files changed

+91
-44
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,9 @@ pub mod pallet {
885885
/// The extrinsic will call the Subtensor pallet to set the weights min stake.
886886
#[pallet::call_index(42)]
887887
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
888-
pub fn sudo_set_weights_min_stake(origin: OriginFor<T>, min_stake: u64) -> DispatchResult {
888+
pub fn sudo_set_stake_threshold(origin: OriginFor<T>, min_stake: u64) -> DispatchResult {
889889
ensure_root(origin)?;
890-
pallet_subtensor::Pallet::<T>::set_weights_min_stake(min_stake);
890+
pallet_subtensor::Pallet::<T>::set_stake_threshold(min_stake);
891891
Ok(())
892892
}
893893

pallets/admin-utils/src/tests/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -682,23 +682,23 @@ fn test_sudo_set_max_allowed_validators() {
682682
}
683683

684684
#[test]
685-
fn test_sudo_set_weights_min_stake() {
685+
fn test_sudo_set_stake_threshold() {
686686
new_test_ext().execute_with(|| {
687687
let to_be_set: u64 = 10;
688-
let init_value: u64 = SubtensorModule::get_weights_min_stake();
688+
let init_value: u64 = SubtensorModule::get_stake_threshold();
689689
assert_eq!(
690-
AdminUtils::sudo_set_weights_min_stake(
690+
AdminUtils::sudo_set_stake_threshold(
691691
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
692692
to_be_set
693693
),
694694
Err(DispatchError::BadOrigin)
695695
);
696-
assert_eq!(SubtensorModule::get_weights_min_stake(), init_value);
697-
assert_ok!(AdminUtils::sudo_set_weights_min_stake(
696+
assert_eq!(SubtensorModule::get_stake_threshold(), init_value);
697+
assert_ok!(AdminUtils::sudo_set_stake_threshold(
698698
<<Test as Config>::RuntimeOrigin>::root(),
699699
to_be_set
700700
));
701-
assert_eq!(SubtensorModule::get_weights_min_stake(), to_be_set);
701+
assert_eq!(SubtensorModule::get_stake_threshold(), to_be_set);
702702
});
703703
}
704704

pallets/subtensor/src/coinbase/root.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ impl<T: Config> Pallet<T> {
783783

784784
// Check to see if the hotkey has enough stake to set weights.
785785
ensure!(
786-
Self::get_total_stake_for_hotkey(&hotkey) >= Self::get_weights_min_stake(),
786+
Self::get_total_stake_for_hotkey(&hotkey) >= Self::get_stake_threshold(),
787787
Error::<T>::NotEnoughStakeToSetWeights
788788
);
789789

pallets/subtensor/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ pub mod pallet {
578578
}
579579
#[pallet::type_value]
580580
/// Default minimum stake for weights.
581-
pub fn DefaultWeightsMinStake<T: Config>() -> u64 {
581+
pub fn DefaultStakeThreshold<T: Config>() -> u64 {
582582
0
583583
}
584584
#[pallet::type_value]
@@ -1296,10 +1296,7 @@ pub mod pallet {
12961296
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
12971297
#[pallet::storage]
12981298
/// ITEM( weights_min_stake )
1299-
pub type WeightsMinStake<T> = StorageValue<_, u64, ValueQuery, DefaultWeightsMinStake<T>>;
1300-
#[pallet::storage]
1301-
/// ITEM( childkeys_min_stake )
1302-
pub type ChildkeysMinStake<T> = StorageValue<_, u64, ValueQuery, DefaultChildkeysMinStake<T>>;
1299+
pub type StakeThreshold<T> = StorageValue<_, u64, ValueQuery, DefaultStakeThreshold<T>>;
13031300
#[pallet::storage]
13041301
/// --- MAP (netuid, who) --> VecDeque<(hash, commit_block, first_reveal_block, last_reveal_block)> | Stores a queue of commits for an account on a given netuid.
13051302
pub type WeightCommits<T: Config> = StorageDoubleMap<
@@ -1356,7 +1353,7 @@ pub mod pallet {
13561353
/// Is the caller allowed to set weights
13571354
pub fn check_weights_min_stake(hotkey: &T::AccountId, netuid: u16) -> bool {
13581355
// Blacklist weights transactions for low stake peers.
1359-
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_weights_min_stake()
1356+
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_stake_threshold()
13601357
}
13611358

13621359
/// Helper function to check if register is allowed

pallets/subtensor/src/macros/events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ mod events {
102102
/// setting the RAO recycled for registration.
103103
RAORecycledForRegistrationSet(u16, u64),
104104
/// min stake is set for validators to set weights.
105-
WeightsMinStake(u64),
105+
StakeThresholdSet(u64),
106106
/// setting the minimum required stake amount for senate registration.
107107
SenateRequiredStakePercentSet(u64),
108108
/// setting the adjustment alpha on a subnet.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use super::*;
2+
use crate::HasMigrationRun;
3+
use frame_support::pallet_prelude::ValueQuery;
4+
use frame_support::storage_alias;
5+
use frame_support::{traits::Get, weights::Weight};
6+
use scale_info::prelude::string::String;
7+
8+
/// Module containing deprecated storage format for WeightsMinStake
9+
pub mod deprecated_weights_min_stake {
10+
use super::*;
11+
12+
#[storage_alias]
13+
pub(super) type WeightsMinStake<T: Config> = StorageValue<Pallet<T>, u64, ValueQuery>;
14+
}
15+
16+
pub fn migrate_stake_threshold<T: Config>() -> Weight {
17+
let migration_name = b"migrate_stake_threshold".to_vec();
18+
let mut weight = T::DbWeight::get().reads(1);
19+
20+
if HasMigrationRun::<T>::get(&migration_name) {
21+
log::info!(
22+
"Migration '{:?}' has already run. Skipping.",
23+
migration_name
24+
);
25+
return weight;
26+
}
27+
28+
log::info!(
29+
"Running migration '{}'",
30+
String::from_utf8_lossy(&migration_name)
31+
);
32+
33+
let min_stake = deprecated_weights_min_stake::WeightsMinStake::<T>::get();
34+
StakeThreshold::<T>::set(min_stake);
35+
deprecated_weights_min_stake::WeightsMinStake::<T>::kill();
36+
37+
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
38+
39+
log::info!(
40+
"Migration '{:?}' completed successfully.",
41+
String::from_utf8_lossy(&migration_name)
42+
);
43+
44+
weight
45+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod migrate_fix_total_coldkey_stake;
99
pub mod migrate_init_total_issuance;
1010
pub mod migrate_populate_owned_hotkeys;
1111
pub mod migrate_populate_staking_hotkeys;
12+
pub mod migrate_stake_threshold;
1213
pub mod migrate_to_v1_separate_emission;
1314
pub mod migrate_to_v2_fixed_total_stake;
1415
pub mod migrate_total_issuance;

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<T: Config> Pallet<T> {
9191
Self::clear_small_nomination_if_required(&hotkey, &coldkey, new_stake);
9292

9393
// Check if stake lowered below MinStake and remove Pending children if it did
94-
if Self::get_total_stake_for_hotkey(&hotkey) < ChildkeysMinStake::<T>::get() {
94+
if Self::get_total_stake_for_hotkey(&hotkey) < StakeThreshold::<T>::get() {
9595
Self::get_all_subnet_netuids().iter().for_each(|netuid| {
9696
PendingChildKeys::<T>::remove(netuid, &hotkey);
9797
})

pallets/subtensor/src/staking/set_children.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<T: Config> Pallet<T> {
113113
// (checking with check_weights_min_stake wouldn't work because it considers
114114
// grandparent stake in this case)
115115
ensure!(
116-
Self::get_total_stake_for_hotkey(&hotkey) >= ChildkeysMinStake::<T>::get(),
116+
Self::get_total_stake_for_hotkey(&hotkey) >= StakeThreshold::<T>::get(),
117117
Error::<T>::NotEnoughStakeToSetChildkeys
118118
);
119119

pallets/subtensor/src/tests/children.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3169,12 +3169,12 @@ fn test_childkey_set_weights_single_parent() {
31693169
));
31703170

31713171
// Set the min stake very high
3172-
SubtensorModule::set_weights_min_stake(stake_to_give_child * 5);
3172+
SubtensorModule::set_stake_threshold(stake_to_give_child * 5);
31733173

31743174
// Check the child has less stake than required
31753175
assert!(
31763176
SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid)
3177-
< SubtensorModule::get_weights_min_stake()
3177+
< SubtensorModule::get_stake_threshold()
31783178
);
31793179

31803180
// Check the child cannot set weights
@@ -3192,12 +3192,12 @@ fn test_childkey_set_weights_single_parent() {
31923192
assert!(!SubtensorModule::check_weights_min_stake(&child, netuid));
31933193

31943194
// Set a minimum stake to set weights
3195-
SubtensorModule::set_weights_min_stake(stake_to_give_child - 5);
3195+
SubtensorModule::set_stake_threshold(stake_to_give_child - 5);
31963196

31973197
// Check if the stake for the child is above
31983198
assert!(
31993199
SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid)
3200-
>= SubtensorModule::get_weights_min_stake()
3200+
>= SubtensorModule::get_stake_threshold()
32013201
);
32023202

32033203
// Check the child can set weights
@@ -3252,12 +3252,12 @@ fn test_set_weights_no_parent() {
32523252
let version_key = SubtensorModule::get_weights_version_key(netuid);
32533253

32543254
// Set the min stake very high
3255-
SubtensorModule::set_weights_min_stake(stake_to_give_child * 5);
3255+
SubtensorModule::set_stake_threshold(stake_to_give_child * 5);
32563256

32573257
// Check the key has less stake than required
32583258
assert!(
32593259
SubtensorModule::get_stake_for_hotkey_on_subnet(&hotkey, netuid)
3260-
< SubtensorModule::get_weights_min_stake()
3260+
< SubtensorModule::get_stake_threshold()
32613261
);
32623262

32633263
// Check the hotkey cannot set weights
@@ -3275,12 +3275,12 @@ fn test_set_weights_no_parent() {
32753275
assert!(!SubtensorModule::check_weights_min_stake(&hotkey, netuid));
32763276

32773277
// Set a minimum stake to set weights
3278-
SubtensorModule::set_weights_min_stake(stake_to_give_child - 5);
3278+
SubtensorModule::set_stake_threshold(stake_to_give_child - 5);
32793279

32803280
// Check if the stake for the hotkey is above
32813281
assert!(
32823282
SubtensorModule::get_stake_for_hotkey_on_subnet(&hotkey, netuid)
3283-
>= SubtensorModule::get_weights_min_stake()
3283+
>= SubtensorModule::get_stake_threshold()
32843284
);
32853285

32863286
// Check the hotkey can set weights
@@ -3643,6 +3643,7 @@ fn test_do_set_child_below_min_stake() {
36433643
// Add network and register hotkey
36443644
add_network(netuid, 13, 0);
36453645
register_ok_neuron(netuid, hotkey, coldkey, 0);
3646+
StakeThreshold::<Test>::set(1_000_000_000_000);
36463647

36473648
// Attempt to set child
36483649
assert_err!(
@@ -3671,10 +3672,13 @@ fn test_do_remove_stake_clears_pending_childkeys() {
36713672
add_network(netuid, 13, 0);
36723673
register_ok_neuron(netuid, hotkey, coldkey, 0);
36733674

3675+
// Set non-default value for childkey stake threshold
3676+
StakeThreshold::<Test>::set(1_000_000_000_000);
3677+
36743678
SubtensorModule::increase_stake_on_coldkey_hotkey_account(
36753679
&coldkey,
36763680
&hotkey,
3677-
ChildkeysMinStake::<Test>::get(),
3681+
StakeThreshold::<Test>::get(),
36783682
);
36793683

36803684
// Attempt to set child
@@ -3720,7 +3724,7 @@ fn test_do_set_child_cooldown_period() {
37203724

37213725
// Set minimum stake for setting children
37223726
let parent_total_stake_original = TotalHotkeyStake::<Test>::get(parent);
3723-
TotalHotkeyStake::<Test>::insert(parent, ChildkeysMinStake::<Test>::get());
3727+
TotalHotkeyStake::<Test>::insert(parent, StakeThreshold::<Test>::get());
37243728

37253729
// Schedule parent-child relationship
37263730
assert_ok!(SubtensorModule::do_schedule_children(

0 commit comments

Comments
 (0)