Skip to content

Commit 4ad316e

Browse files
authored
Merge pull request #924 from opentensor/check-childkey-stake
Duplicate hotfixes to devnet-ready
2 parents c05342a + 7cf32c2 commit 4ad316e

File tree

14 files changed

+1690
-32
lines changed

14 files changed

+1690
-32
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ impl<T: Config> Pallet<T> {
4848
// --- 3. Drain the subnet block emission and accumulate it as subnet emission, which increases until the tempo is reached in #4.
4949
// subnet_blockwise_emission -> subnet_pending_emission
5050
for netuid in subnets.clone().iter() {
51+
if *netuid == 0 {
52+
continue;
53+
}
5154
// --- 3.1 Get the network's block-wise emission amount.
5255
// This value is newly minted TAO which has not reached staking accounts yet.
5356
let subnet_blockwise_emission: u64 = EmissionValues::<T>::get(*netuid);
@@ -87,6 +90,11 @@ impl<T: Config> Pallet<T> {
8790
Self::set_blocks_since_last_step(*netuid, 0);
8891
Self::set_last_mechanism_step_block(*netuid, current_block);
8992

93+
if *netuid == 0 {
94+
// Skip netuid 0 payouts
95+
continue;
96+
}
97+
9098
// --- 4.4 Distribute owner take.
9199
if SubnetOwner::<T>::contains_key(netuid) {
92100
// Does the subnet have an owner?
@@ -295,8 +303,8 @@ impl<T: Config> Pallet<T> {
295303
// --- 8 Iterate over each nominator.
296304
if total_viable_nominator_stake != 0 {
297305
for (nominator, nominator_stake) in Stake::<T>::iter_prefix(hotkey) {
298-
// --- 9 Check if the stake was manually increased by the user since the last emission drain for this hotkey.
299-
// If it was, skip this nominator as they will not receive their proportion of the emission.
306+
// --- 9 Skip emission for any stake the was added by the nominator since the last emission drain.
307+
// This means the nominator will get emission on existing stake, but not on new stake, until the next emission drain.
300308
let viable_nominator_stake =
301309
nominator_stake.saturating_sub(Self::get_nonviable_stake(hotkey, &nominator));
302310

@@ -323,7 +331,10 @@ impl<T: Config> Pallet<T> {
323331
let hotkey_new_tao: u64 = hotkey_take.saturating_add(remainder);
324332
Self::increase_stake_on_hotkey_account(hotkey, hotkey_new_tao);
325333

326-
// --- 14 Record new tao creation event and return the amount created.
334+
// --- 14 Reset the stake delta for the hotkey.
335+
let _ = StakeDeltaSinceLastEmissionDrain::<T>::clear_prefix(hotkey, u32::MAX, None);
336+
337+
// --- 15 Record new tao creation event and return the amount created.
327338
total_new_tao = total_new_tao.saturating_add(hotkey_new_tao);
328339
total_new_tao
329340
}

pallets/subtensor/src/lib.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ pub mod pallet {
12991299
/// Returns the transaction priority for setting weights.
13001300
pub fn get_priority_set_weights(hotkey: &T::AccountId, netuid: u16) -> u64 {
13011301
if let Ok(uid) = Self::get_uid_for_net_and_hotkey(netuid, hotkey) {
1302-
let _stake = Self::get_total_stake_for_hotkey(hotkey);
1302+
let _stake = Self::get_stake_for_hotkey_on_subnet(hotkey, netuid);
13031303
let current_block_number: u64 = Self::get_current_block_as_u64();
13041304
let default_priority: u64 =
13051305
current_block_number.saturating_sub(Self::get_last_update_for_uid(netuid, uid));
@@ -1309,9 +1309,9 @@ pub mod pallet {
13091309
}
13101310

13111311
/// Is the caller allowed to set weights
1312-
pub fn check_weights_min_stake(hotkey: &T::AccountId) -> bool {
1312+
pub fn check_weights_min_stake(hotkey: &T::AccountId, netuid: u16) -> bool {
13131313
// Blacklist weights transactions for low stake peers.
1314-
Self::get_total_stake_for_hotkey(hotkey) >= Self::get_weights_min_stake()
1314+
Self::get_stake_for_hotkey_on_subnet(hotkey, netuid) >= Self::get_weights_min_stake()
13151315
}
13161316

13171317
/// Helper function to check if register is allowed
@@ -1404,8 +1404,8 @@ where
14041404
Pallet::<T>::get_priority_set_weights(who, netuid)
14051405
}
14061406

1407-
pub fn check_weights_min_stake(who: &T::AccountId) -> bool {
1408-
Pallet::<T>::check_weights_min_stake(who)
1407+
pub fn check_weights_min_stake(who: &T::AccountId, netuid: u16) -> bool {
1408+
Pallet::<T>::check_weights_min_stake(who, netuid)
14091409
}
14101410
}
14111411

@@ -1443,7 +1443,7 @@ where
14431443
) -> TransactionValidity {
14441444
match call.is_sub_type() {
14451445
Some(Call::commit_weights { netuid, .. }) => {
1446-
if Self::check_weights_min_stake(who) {
1446+
if Self::check_weights_min_stake(who, *netuid) {
14471447
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
14481448
Ok(ValidTransaction {
14491449
priority,
@@ -1455,7 +1455,7 @@ where
14551455
}
14561456
}
14571457
Some(Call::reveal_weights { netuid, .. }) => {
1458-
if Self::check_weights_min_stake(who) {
1458+
if Self::check_weights_min_stake(who, *netuid) {
14591459
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
14601460
Ok(ValidTransaction {
14611461
priority,
@@ -1467,7 +1467,7 @@ where
14671467
}
14681468
}
14691469
Some(Call::batch_reveal_weights { netuid, .. }) => {
1470-
if Self::check_weights_min_stake(who) {
1470+
if Self::check_weights_min_stake(who, *netuid) {
14711471
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
14721472
Ok(ValidTransaction {
14731473
priority,
@@ -1479,7 +1479,7 @@ where
14791479
}
14801480
}
14811481
Some(Call::set_weights { netuid, .. }) => {
1482-
if Self::check_weights_min_stake(who) {
1482+
if Self::check_weights_min_stake(who, *netuid) {
14831483
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
14841484
Ok(ValidTransaction {
14851485
priority,
@@ -1491,7 +1491,7 @@ where
14911491
}
14921492
}
14931493
Some(Call::set_root_weights { netuid, hotkey, .. }) => {
1494-
if Self::check_weights_min_stake(hotkey) {
1494+
if Self::check_weights_min_stake(hotkey, *netuid) {
14951495
let priority: u64 = Self::get_priority_set_weights(hotkey, *netuid);
14961496
Ok(ValidTransaction {
14971497
priority,

pallets/subtensor/src/rpc_info/neuron_info.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::*;
22
use frame_support::pallet_prelude::{Decode, Encode};
3-
use frame_support::storage::IterableStorageDoubleMap;
43
extern crate alloc;
54
use codec::Compact;
65

@@ -179,12 +178,10 @@ impl<T: Config> Pallet<T> {
179178
let last_update = Self::get_last_update_for_uid(netuid, uid);
180179
let validator_permit = Self::get_validator_permit_for_uid(netuid, uid);
181180

182-
let stake: Vec<(T::AccountId, Compact<u64>)> =
183-
<Stake<T> as IterableStorageDoubleMap<T::AccountId, T::AccountId, u64>>::iter_prefix(
184-
hotkey.clone(),
185-
)
186-
.map(|(coldkey, stake)| (coldkey, stake.into()))
187-
.collect();
181+
let stake: Vec<(T::AccountId, Compact<u64>)> = vec![(
182+
coldkey.clone(),
183+
Self::get_stake_for_hotkey_on_subnet(&hotkey, netuid).into(),
184+
)];
188185

189186
let neuron = NeuronInfoLite {
190187
hotkey: hotkey.clone(),

pallets/subtensor/src/staking/helpers.rs

+6
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ impl<T: Config> Pallet<T> {
297297
staking_hotkeys.retain(|h| h != hotkey);
298298
StakingHotkeys::<T>::insert(coldkey, staking_hotkeys);
299299

300+
// Update stake delta
301+
StakeDeltaSinceLastEmissionDrain::<T>::remove(hotkey, coldkey);
302+
300303
current_stake
301304
}
302305

@@ -431,6 +434,9 @@ impl<T: Config> Pallet<T> {
431434

432435
// Add the balance to the coldkey account.
433436
Self::add_balance_to_coldkey_account(&delegate_coldkey_i, stake_i);
437+
438+
// Remove stake delta
439+
StakeDeltaSinceLastEmissionDrain::<T>::remove(hotkey, &delegate_coldkey_i);
434440
}
435441
}
436442
}

pallets/subtensor/src/staking/remove_stake.rs

+5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ impl<T: Config> Pallet<T> {
7676
// We remove the balance from the hotkey.
7777
Self::decrease_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_to_be_removed);
7878

79+
// Track this removal in the stake delta.
80+
StakeDeltaSinceLastEmissionDrain::<T>::mutate(&hotkey, &coldkey, |stake_delta| {
81+
*stake_delta = stake_delta.saturating_sub_unsigned(stake_to_be_removed as u128);
82+
});
83+
7984
// We add the balance to the coldkey. If the above fails we will not credit this coldkey.
8085
Self::add_balance_to_coldkey_account(&coldkey, stake_to_be_removed);
8186

pallets/subtensor/src/subnets/uids.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<T: Config> Pallet<T> {
120120
///
121121
pub fn get_stake_for_uid_and_subnetwork(netuid: u16, neuron_uid: u16) -> u64 {
122122
if let Ok(hotkey) = Self::get_hotkey_for_net_and_uid(netuid, neuron_uid) {
123-
Self::get_total_stake_for_hotkey(&hotkey)
123+
Self::get_stake_for_hotkey_on_subnet(&hotkey, netuid)
124124
} else {
125125
0
126126
}

pallets/subtensor/src/subnets/weights.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,9 @@ impl<T: Config> Pallet<T> {
525525
Error::<T>::HotKeyNotRegisteredInSubNet
526526
);
527527

528-
// --- 6. Check to see if the hotkey has enought stake to set weights.
528+
// --- 6. Check to see if the hotkey has enough stake to set weights.
529529
ensure!(
530-
Self::get_total_stake_for_hotkey(&hotkey) >= Self::get_weights_min_stake(),
530+
Self::check_weights_min_stake(&hotkey, netuid),
531531
Error::<T>::NotEnoughStakeToSetWeights
532532
);
533533

pallets/subtensor/src/swap/swap_coldkey.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,20 @@ impl<T: Config> Pallet<T> {
169169
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
170170
}
171171

172-
// 4. Swap total coldkey stake.
172+
// 4. Swap StakeDeltaSinceLastEmissionDrain
173+
for hotkey in StakingHotkeys::<T>::get(old_coldkey) {
174+
let old_stake_delta = StakeDeltaSinceLastEmissionDrain::<T>::get(&hotkey, old_coldkey);
175+
let new_stake_delta = StakeDeltaSinceLastEmissionDrain::<T>::get(&hotkey, new_coldkey);
176+
StakeDeltaSinceLastEmissionDrain::<T>::insert(
177+
&hotkey,
178+
new_coldkey,
179+
new_stake_delta.saturating_add(old_stake_delta),
180+
);
181+
StakeDeltaSinceLastEmissionDrain::<T>::remove(&hotkey, old_coldkey);
182+
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
183+
}
184+
185+
// 5. Swap total coldkey stake.
173186
// TotalColdkeyStake: MAP ( coldkey ) --> u64 | Total stake of the coldkey.
174187
let old_coldkey_stake: u64 = TotalColdkeyStake::<T>::get(old_coldkey);
175188
// Get the stake of the new coldkey.
@@ -183,7 +196,7 @@ impl<T: Config> Pallet<T> {
183196
);
184197
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
185198

186-
// 5. Swap StakingHotkeys.
199+
// 6. Swap StakingHotkeys.
187200
// StakingHotkeys: MAP ( coldkey ) --> Vec<hotkeys> | Hotkeys staking for the coldkey.
188201
let old_staking_hotkeys: Vec<T::AccountId> = StakingHotkeys::<T>::get(old_coldkey);
189202
let mut new_staking_hotkeys: Vec<T::AccountId> = StakingHotkeys::<T>::get(new_coldkey);
@@ -197,7 +210,7 @@ impl<T: Config> Pallet<T> {
197210
StakingHotkeys::<T>::insert(new_coldkey, new_staking_hotkeys);
198211
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
199212

200-
// 6. Swap hotkey owners.
213+
// 7. Swap hotkey owners.
201214
// Owner: MAP ( hotkey ) --> coldkey | Owner of the hotkey.
202215
// OwnedHotkeys: MAP ( coldkey ) --> Vec<hotkeys> | Hotkeys owned by the coldkey.
203216
let old_owned_hotkeys: Vec<T::AccountId> = OwnedHotkeys::<T>::get(old_coldkey);
@@ -216,7 +229,7 @@ impl<T: Config> Pallet<T> {
216229
OwnedHotkeys::<T>::insert(new_coldkey, new_owned_hotkeys);
217230
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
218231

219-
// 7. Transfer remaining balance.
232+
// 8. Transfer remaining balance.
220233
// Balance: MAP ( coldkey ) --> u64 | Balance of the coldkey.
221234
// Transfer any remaining balance from old_coldkey to new_coldkey
222235
let remaining_balance = Self::get_coldkey_balance(old_coldkey);

0 commit comments

Comments
 (0)