Skip to content

Commit 51cdd9c

Browse files
authored
Merge pull request #1050 from opentensor/feat/pending-childkeys
Feat/pending childkeys
2 parents f87d97d + 7e03512 commit 51cdd9c

File tree

17 files changed

+583
-466
lines changed

17 files changed

+583
-466
lines changed

Diff for: pallets/admin-utils/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,9 @@ pub mod pallet {
890890
/// The extrinsic will call the Subtensor pallet to set the weights min stake.
891891
#[pallet::call_index(42)]
892892
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
893-
pub fn sudo_set_weights_min_stake(origin: OriginFor<T>, min_stake: u64) -> DispatchResult {
893+
pub fn sudo_set_stake_threshold(origin: OriginFor<T>, min_stake: u64) -> DispatchResult {
894894
ensure_root(origin)?;
895-
pallet_subtensor::Pallet::<T>::set_weights_min_stake(min_stake);
895+
pallet_subtensor::Pallet::<T>::set_stake_threshold(min_stake);
896896
Ok(())
897897
}
898898

Diff for: pallets/admin-utils/src/tests/mod.rs

+6-6
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

Diff for: pallets/subtensor/src/coinbase/root.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ impl<T: Config> Pallet<T> {
784784

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

Diff for: pallets/subtensor/src/coinbase/run_coinbase.rs

+3
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ impl<T: Config> Pallet<T> {
166166
);
167167
log::debug!("Accumulated emissions on hotkey {:?} for netuid {:?}: mining {:?}, validator {:?}", hotkey, *netuid, mining_emission, validator_emission);
168168
}
169+
170+
// 4.5 Apply pending childkeys of this subnet for the next epoch
171+
Self::do_set_pending_children(*netuid);
169172
} else {
170173
// No epoch, increase blocks since last step and continue
171174
Self::set_blocks_since_last_step(

Diff for: pallets/subtensor/src/lib.rs

+32-3
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ pub mod pallet {
311311
vec![]
312312
}
313313
#[pallet::type_value]
314+
/// Default pending childkeys
315+
pub fn DefaultPendingChildkeys<T: Config>() -> (Vec<(u64, T::AccountId)>, u64) {
316+
(vec![], 0)
317+
}
318+
#[pallet::type_value]
314319
/// Default account linkage
315320
pub fn DefaultProportion<T: Config>() -> u64 {
316321
0
@@ -576,7 +581,7 @@ pub mod pallet {
576581
}
577582
#[pallet::type_value]
578583
/// Default minimum stake for weights.
579-
pub fn DefaultWeightsMinStake<T: Config>() -> u64 {
584+
pub fn DefaultStakeThreshold<T: Config>() -> u64 {
580585
0
581586
}
582587
#[pallet::type_value]
@@ -677,6 +682,18 @@ pub mod pallet {
677682
T::InitialColdkeySwapScheduleDuration::get()
678683
}
679684

685+
#[pallet::type_value]
686+
/// Default value for applying pending items (e.g. childkeys).
687+
pub fn DefaultPendingCooldown<T: Config>() -> u64 {
688+
7200
689+
}
690+
691+
#[pallet::type_value]
692+
/// Default minimum stake for setting childkeys.
693+
pub fn DefaultChildkeysMinStake<T: Config>() -> u64 {
694+
1_000_000_000_000
695+
}
696+
680697
#[pallet::storage]
681698
pub type ColdkeySwapScheduleDuration<T: Config> =
682699
StorageValue<_, BlockNumberFor<T>, ValueQuery, DefaultColdkeySwapScheduleDuration<T>>;
@@ -824,6 +841,18 @@ pub mod pallet {
824841
DefaultStakeDelta<T>,
825842
>;
826843
#[pallet::storage]
844+
/// DMAP ( netuid, parent ) --> (Vec<(proportion,child)>, cool_down_block)
845+
pub type PendingChildKeys<T: Config> = StorageDoubleMap<
846+
_,
847+
Identity,
848+
u16,
849+
Blake2_128Concat,
850+
T::AccountId,
851+
(Vec<(u64, T::AccountId)>, u64),
852+
ValueQuery,
853+
DefaultPendingChildkeys<T>,
854+
>;
855+
#[pallet::storage]
827856
/// DMAP ( parent, netuid ) --> Vec<(proportion,child)>
828857
pub type ChildKeys<T: Config> = StorageDoubleMap<
829858
_,
@@ -1270,7 +1299,7 @@ pub mod pallet {
12701299
StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock<T>>;
12711300
#[pallet::storage]
12721301
/// ITEM( weights_min_stake )
1273-
pub type WeightsMinStake<T> = StorageValue<_, u64, ValueQuery, DefaultWeightsMinStake<T>>;
1302+
pub type StakeThreshold<T> = StorageValue<_, u64, ValueQuery, DefaultStakeThreshold<T>>;
12741303
#[pallet::storage]
12751304
/// --- 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.
12761305
pub type WeightCommits<T: Config> = StorageDoubleMap<
@@ -1342,7 +1371,7 @@ pub mod pallet {
13421371
/// Is the caller allowed to set weights
13431372
pub fn check_weights_min_stake(hotkey: &T::AccountId, netuid: u16) -> bool {
13441373
// Blacklist weights transactions for low stake peers.
1345-
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_weights_min_stake()
1374+
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_stake_threshold()
13461375
}
13471376

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

Diff for: pallets/subtensor/src/macros/dispatches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ mod dispatches {
11971197
netuid: u16,
11981198
children: Vec<(u64, T::AccountId)>,
11991199
) -> DispatchResultWithPostInfo {
1200-
Self::do_set_children(origin, hotkey, netuid, children)?;
1200+
Self::do_schedule_children(origin, hotkey, netuid, children)?;
12011201
Ok(().into())
12021202
}
12031203

Diff for: pallets/subtensor/src/macros/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ mod errors {
3535
/// The caller is requesting to set weights but the caller has less than minimum stake
3636
/// required to set weights (less than WeightsMinStake).
3737
NotEnoughStakeToSetWeights,
38+
/// The parent hotkey doesn't have enough own stake to set childkeys.
39+
NotEnoughStakeToSetChildkeys,
3840
/// The caller is requesting adding more stake than there exists in the coldkey account.
3941
/// See: "[add_stake()]"
4042
NotEnoughBalanceToStake,

Diff for: pallets/subtensor/src/macros/events.rs

+3-1
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.
@@ -179,6 +179,8 @@ mod events {
179179
/// The account ID of the coldkey
180180
coldkey: T::AccountId,
181181
},
182+
/// Setting of children of a hotkey have been scheduled
183+
SetChildrenScheduled(T::AccountId, u16, u64, Vec<(u64, T::AccountId)>),
182184
/// The children of a hotkey have been set
183185
SetChildren(T::AccountId, u16, Vec<(u64, T::AccountId)>),
184186
/// The hotkey emission tempo has been set
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
HasMigrationRun::<T>::insert(&migration_name, true);
40+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
41+
42+
log::info!(
43+
"Migration '{:?}' completed successfully.",
44+
String::from_utf8_lossy(&migration_name)
45+
);
46+
47+
weight
48+
}

Diff for: pallets/subtensor/src/migrations/mod.rs

+1
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;

Diff for: pallets/subtensor/src/staking/remove_stake.rs

+7
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ impl<T: Config> Pallet<T> {
9090
let new_stake = Self::get_stake_for_coldkey_and_hotkey(&coldkey, &hotkey);
9191
Self::clear_small_nomination_if_required(&hotkey, &coldkey, new_stake);
9292

93+
// Check if stake lowered below MinStake and remove Pending children if it did
94+
if Self::get_total_stake_for_hotkey(&hotkey) < StakeThreshold::<T>::get() {
95+
Self::get_all_subnet_netuids().iter().for_each(|netuid| {
96+
PendingChildKeys::<T>::remove(netuid, &hotkey);
97+
})
98+
}
99+
93100
// Set last block for rate limiting
94101
let block: u64 = Self::get_current_block_as_u64();
95102
Self::set_last_tx_block(&coldkey, block);

0 commit comments

Comments
 (0)