Skip to content

Commit e118499

Browse files
committed
Remove check for minimum stake for revoking childkeys
1 parent 85fa7dd commit e118499

File tree

4 files changed

+1111
-13
lines changed

4 files changed

+1111
-13
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ impl<T: Config> Pallet<T> {
105105
};
106106
}
107107

108-
// --- 4.3 Drain the subnet emission.
108+
// 4.3 Apply pending childkeys of this subnet for the next epoch
109+
Self::do_set_pending_children(*netuid);
110+
111+
// --- 4.4 Drain the subnet emission.
109112
let mut subnet_emission: u64 = PendingEmission::<T>::get(*netuid);
110113
PendingEmission::<T>::insert(*netuid, 0);
111114
log::debug!(
@@ -114,7 +117,7 @@ impl<T: Config> Pallet<T> {
114117
subnet_emission
115118
);
116119

117-
// --- 4.4 Set last step counter.
120+
// --- 4.5 Set last step counter.
118121
Self::set_blocks_since_last_step(*netuid, 0);
119122
Self::set_last_mechanism_step_block(*netuid, current_block);
120123

@@ -123,30 +126,30 @@ impl<T: Config> Pallet<T> {
123126
continue;
124127
}
125128

126-
// --- 4.5 Distribute owner take.
129+
// --- 4.6 Distribute owner take.
127130
if SubnetOwner::<T>::contains_key(netuid) {
128131
// Does the subnet have an owner?
129132

130-
// --- 4.5.1 Compute the subnet owner cut.
133+
// --- 4.6.1 Compute the subnet owner cut.
131134
let owner_cut: I96F32 = I96F32::from_num(subnet_emission).saturating_mul(
132135
I96F32::from_num(Self::get_subnet_owner_cut())
133136
.saturating_div(I96F32::from_num(u16::MAX)),
134137
);
135138

136-
// --- 4.5.2 Remove the cut from the subnet emission
139+
// --- 4.6.2 Remove the cut from the subnet emission
137140
subnet_emission = subnet_emission.saturating_sub(owner_cut.to_num::<u64>());
138141

139-
// --- 4.5.3 Add the cut to the balance of the owner
142+
// --- 4.6.3 Add the cut to the balance of the owner
140143
Self::add_balance_to_coldkey_account(
141144
&Self::get_subnet_owner(*netuid),
142145
owner_cut.to_num::<u64>(),
143146
);
144147

145-
// --- 4.5.4 Increase total issuance on the chain.
148+
// --- 4.6.4 Increase total issuance on the chain.
146149
Self::coinbase(owner_cut.to_num::<u64>());
147150
}
148151

149-
// 4.6 Pass emission through epoch() --> hotkey emission.
152+
// 4.7 Pass emission through epoch() --> hotkey emission.
150153
let hotkey_emission: Vec<(T::AccountId, u64, u64)> =
151154
Self::epoch(*netuid, subnet_emission);
152155
log::debug!(
@@ -155,7 +158,7 @@ impl<T: Config> Pallet<T> {
155158
hotkey_emission
156159
);
157160

158-
// 4.7 Accumulate the tuples on hotkeys:
161+
// 4.8 Accumulate the tuples on hotkeys:
159162
for (hotkey, mining_emission, validator_emission) in hotkey_emission {
160163
// 4.8 Accumulate the emission on the hotkey and parent hotkeys.
161164
Self::accumulate_hotkey_emission(
@@ -166,9 +169,6 @@ impl<T: Config> Pallet<T> {
166169
);
167170
log::debug!("Accumulated emissions on hotkey {:?} for netuid {:?}: mining {:?}, validator {:?}", hotkey, *netuid, mining_emission, validator_emission);
168171
}
169-
170-
// 4.5 Apply pending childkeys of this subnet for the next epoch
171-
Self::do_set_pending_children(*netuid);
172172
} else {
173173
// No epoch, increase blocks since last step and continue
174174
Self::set_blocks_since_last_step(

pallets/subtensor/src/staking/set_children.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,12 @@ impl<T: Config> Pallet<T> {
110110
}
111111

112112
// Check that the parent key has at least the minimum own stake
113+
// if children vector is not empty
113114
// (checking with check_weights_min_stake wouldn't work because it considers
114115
// grandparent stake in this case)
115116
ensure!(
116-
Self::get_total_stake_for_hotkey(&hotkey) >= StakeThreshold::<T>::get(),
117+
children.is_empty()
118+
|| Self::get_total_stake_for_hotkey(&hotkey) >= StakeThreshold::<T>::get(),
117119
Error::<T>::NotEnoughStakeToSetChildkeys
118120
);
119121

pallets/subtensor/src/tests/children.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3747,3 +3747,112 @@ fn test_do_set_child_cooldown_period() {
37473747
assert_eq!(children_after, vec![(proportion, child)]);
37483748
});
37493749
}
3750+
3751+
// Test that setting childkeys requires minimum stake
3752+
#[test]
3753+
fn test_do_set_child_min_stake_check() {
3754+
new_test_ext(1).execute_with(|| {
3755+
let coldkey = U256::from(1);
3756+
let parent = U256::from(2);
3757+
let child = U256::from(3);
3758+
let netuid: u16 = 1;
3759+
let proportion: u64 = 1000;
3760+
3761+
// Add network and register hotkey
3762+
add_network(netuid, 13, 0);
3763+
register_ok_neuron(netuid, parent, coldkey, 0);
3764+
3765+
// Set below minimum stake for setting children
3766+
let parent_total_stake_original = TotalHotkeyStake::<Test>::get(parent);
3767+
StakeThreshold::<Test>::put(1_000_000_000_000);
3768+
TotalHotkeyStake::<Test>::insert(parent, StakeThreshold::<Test>::get() - 1);
3769+
3770+
// Schedule parent-child relationship
3771+
assert_err!(
3772+
SubtensorModule::do_schedule_children(
3773+
RuntimeOrigin::signed(coldkey),
3774+
parent,
3775+
netuid,
3776+
vec![(proportion, child)],
3777+
),
3778+
Error::<Test>::NotEnoughStakeToSetChildkeys
3779+
);
3780+
3781+
// Ensure the childkeys are not yet applied
3782+
let children_before = SubtensorModule::get_children(&parent, netuid);
3783+
assert_eq!(children_before, vec![]);
3784+
3785+
wait_and_set_pending_children(netuid);
3786+
TotalHotkeyStake::<Test>::insert(parent, parent_total_stake_original);
3787+
3788+
// Ensure the childkeys are still not applied
3789+
let children_after = SubtensorModule::get_children(&parent, netuid);
3790+
assert_eq!(children_after, vec![]);
3791+
});
3792+
}
3793+
3794+
// Test that revoking childkeys does not require minimum stake
3795+
#[test]
3796+
fn test_revoke_child_no_min_stake_check() {
3797+
new_test_ext(1).execute_with(|| {
3798+
let coldkey = U256::from(1);
3799+
let parent = U256::from(2);
3800+
let child = U256::from(3);
3801+
let netuid: u16 = 1;
3802+
let proportion: u64 = 1000;
3803+
3804+
// Add network and register hotkey
3805+
add_network(netuid, 13, 0);
3806+
register_ok_neuron(netuid, parent, coldkey, 0);
3807+
3808+
// Set below minimum stake for setting children
3809+
let parent_total_stake_original = TotalHotkeyStake::<Test>::get(parent);
3810+
StakeThreshold::<Test>::put(1_000_000_000_000);
3811+
TotalHotkeyStake::<Test>::insert(parent, StakeThreshold::<Test>::get());
3812+
3813+
// Schedule parent-child relationship
3814+
assert_ok!(SubtensorModule::do_schedule_children(
3815+
RuntimeOrigin::signed(coldkey),
3816+
parent,
3817+
netuid,
3818+
vec![(proportion, child)],
3819+
));
3820+
3821+
// Ensure the childkeys are not yet applied
3822+
let children_before = SubtensorModule::get_children(&parent, netuid);
3823+
assert_eq!(children_before, vec![]);
3824+
3825+
wait_and_set_pending_children(netuid);
3826+
TotalHotkeyStake::<Test>::insert(parent, parent_total_stake_original);
3827+
3828+
// Ensure the childkeys are applied
3829+
let children_after = SubtensorModule::get_children(&parent, netuid);
3830+
assert_eq!(children_after, vec![(proportion, child)]);
3831+
3832+
// Reduce the stake below required threshold
3833+
TotalHotkeyStake::<Test>::insert(parent, StakeThreshold::<Test>::get() - 1);
3834+
3835+
// Bypass tx rate limit
3836+
SubtensorModule::set_last_transaction_block_on_subnet(
3837+
&parent,
3838+
netuid,
3839+
&TransactionType::SetChildren,
3840+
0,
3841+
);
3842+
3843+
// Schedule parent-child relationship revokation
3844+
assert_ok!(SubtensorModule::do_schedule_children(
3845+
RuntimeOrigin::signed(coldkey),
3846+
parent,
3847+
netuid,
3848+
vec![],
3849+
));
3850+
3851+
wait_and_set_pending_children(netuid);
3852+
TotalHotkeyStake::<Test>::insert(parent, parent_total_stake_original);
3853+
3854+
// Ensure the childkeys are revoked
3855+
let children_after = SubtensorModule::get_children(&parent, netuid);
3856+
assert_eq!(children_after, vec![]);
3857+
});
3858+
}

0 commit comments

Comments
 (0)