Skip to content

Commit

Permalink
Remove get_stake_for_coldkey_and_hotkey from pallet-subtensor::Pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
ales-otf committed Dec 17, 2024
1 parent 30c03f6 commit 52d2625
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 327 deletions.
60 changes: 12 additions & 48 deletions pallets/admin-utils/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,10 +961,7 @@ mod sudo_set_nominator_min_required_stake {
hot1,
1
));
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&cold1, &hot1),
1
);
assert_eq!(Stake::<Test>::get(hot1, cold1), 1);
assert_eq!(Balances::free_balance(cold1), 4);

// Add stake cold2 --> hot1 (is delegation.)
Expand All @@ -974,10 +971,7 @@ mod sudo_set_nominator_min_required_stake {
hot1,
1
));
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&cold2, &hot1),
1
);
assert_eq!(Stake::<Test>::get(hot1, cold2), 1);
assert_eq!(Balances::free_balance(cold2), 4);

// Add stake cold1 --> hot2 (non delegation.)
Expand All @@ -987,10 +981,7 @@ mod sudo_set_nominator_min_required_stake {
hot2,
1
));
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&cold1, &hot2),
1
);
assert_eq!(Stake::<Test>::get(hot2, cold1), 1);
assert_eq!(Balances::free_balance(cold1), 8);

// Add stake cold2 --> hot2 (is delegation.)
Expand All @@ -1000,55 +991,28 @@ mod sudo_set_nominator_min_required_stake {
hot2,
1
));
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&cold2, &hot2),
1
);
assert_eq!(Stake::<Test>::get(hot2, cold2), 1);
assert_eq!(Balances::free_balance(cold2), 8);

// Set min stake to 0 (noop)
assert_ok!(AdminUtils::sudo_set_nominator_min_required_stake(
<<Test as Config>::RuntimeOrigin>::root(),
0u64
));
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&cold1, &hot1),
1
);
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&cold1, &hot2),
1
);
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&cold2, &hot1),
1
);
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&cold2, &hot2),
1
);
assert_eq!(Stake::<Test>::get(hot1, cold1), 1);
assert_eq!(Stake::<Test>::get(hot2, cold1), 1);
assert_eq!(Stake::<Test>::get(hot1, cold2), 1);
assert_eq!(Stake::<Test>::get(hot2, cold2), 1);

// Set min nomination to 10: should clear (cold2, hot1) and (cold1, hot2).
assert_ok!(AdminUtils::sudo_set_nominator_min_required_stake(
<<Test as Config>::RuntimeOrigin>::root(),
10u64
));
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&cold1, &hot1),
1
);
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&cold1, &hot2),
0
);
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&cold2, &hot1),
0
);
assert_eq!(
SubtensorModule::get_stake_for_coldkey_and_hotkey(&cold2, &hot2),
1
);
assert_eq!(Stake::<Test>::get(hot1, cold1), 1);
assert_eq!(Stake::<Test>::get(hot2, cold1), 0);
assert_eq!(Stake::<Test>::get(hot1, cold2), 0);
assert_eq!(Stake::<Test>::get(hot2, cold2), 1);

// Balances have been added back into accounts.
assert_eq!(Balances::free_balance(cold1), 9);
Expand Down
35 changes: 8 additions & 27 deletions pallets/subtensor/src/migrations/migrate_fix_pending_emission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,7 @@ impl<T: Config> Pallet<T> {

assert!(StakingHotkeys::<T>::get(migration_ck_acct).contains(taostats_old_hk_acct));

assert_eq!(
Self::get_stake_for_coldkey_and_hotkey(null_account, taostats_old_hk_acct),
0
);
assert_eq!(Stake::<T>::get(taostats_old_hk_acct, null_account), 0);

// Check the total hotkey stake is the same
assert_eq!(
Expand All @@ -217,7 +214,7 @@ impl<T: Config> Pallet<T> {
);

let new_null_stake_taostats =
Self::get_stake_for_coldkey_and_hotkey(migration_ck_acct, taostats_old_hk_acct);
Stake::<T>::get(taostats_old_hk_acct, migration_ck_acct);

assert_eq!(
new_null_stake_taostats,
Expand Down Expand Up @@ -257,10 +254,7 @@ impl<T: Config> Pallet<T> {

assert!(StakingHotkeys::<T>::get(migration_ck_acct).contains(datura_old_hk_acct));

assert_eq!(
Self::get_stake_for_coldkey_and_hotkey(null_account, datura_old_hk_acct),
0
);
assert_eq!(Stake::<T>::get(datura_old_hk_acct, null_account), 0);

// Check the total hotkey stake is the same
assert_eq!(
Expand All @@ -269,8 +263,7 @@ impl<T: Config> Pallet<T> {
.saturating_add(old.old_migration_stake_datura)
);

let new_null_stake_datura =
Self::get_stake_for_coldkey_and_hotkey(migration_ck_acct, datura_old_hk_acct);
let new_null_stake_datura = Stake::<T>::get(datura_old_hk_acct, migration_ck_acct);

assert_eq!(
new_null_stake_datura,
Expand Down Expand Up @@ -353,14 +346,8 @@ pub mod migration {
.saturating_add(PendingdHotkeyEmission::<T>::get(taostats_new_hk_acct));

Ok::<(u64, u64), sp_runtime::TryRuntimeError>((
crate::Pallet::<T>::get_stake_for_coldkey_and_hotkey(
null_account,
taostats_old_hk_acct,
),
crate::Pallet::<T>::get_stake_for_coldkey_and_hotkey(
migration_acct,
taostats_old_hk_acct,
),
Stake::<T>::get(taostats_old_hk_acct, null_account),
Stake::<T>::get(taostats_old_hk_acct, migration_acct),
))
}
_ => {
Expand All @@ -386,14 +373,8 @@ pub mod migration {
.saturating_add(PendingdHotkeyEmission::<T>::get(datura_new_hk_acct));

Ok::<(u64, u64), sp_runtime::TryRuntimeError>((
crate::Pallet::<T>::get_stake_for_coldkey_and_hotkey(
null_account,
datura_old_hk_acct,
),
crate::Pallet::<T>::get_stake_for_coldkey_and_hotkey(
migration_acct,
datura_old_hk_acct,
),
Stake::<T>::get(datura_old_hk_acct, null_account),
Stake::<T>::get(datura_old_hk_acct, migration_acct),
))
}
_ => {
Expand Down
3 changes: 1 addition & 2 deletions pallets/subtensor/src/rpc_info/delegate_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ impl<T: Config> Pallet<T> {

let mut delegates: Vec<(DelegateInfo<T>, Compact<u64>)> = Vec::new();
for delegate in <Delegates<T> as IterableStorageMap<T::AccountId, u16>>::iter_keys() {
let staked_to_this_delegatee =
Self::get_stake_for_coldkey_and_hotkey(&delegatee.clone(), &delegate.clone());
let staked_to_this_delegatee = Stake::<T>::get(&delegate, &delegatee);
if staked_to_this_delegatee == 0 {
continue; // No stake to this delegate
}
Expand Down
8 changes: 1 addition & 7 deletions pallets/subtensor/src/staking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ impl<T: Config> Pallet<T> {
TotalColdkeyStake::<T>::get(coldkey)
}

// Returns the stake under the cold - hot pairing in the staking table.
//
pub fn get_stake_for_coldkey_and_hotkey(coldkey: &T::AccountId, hotkey: &T::AccountId) -> u64 {
Stake::<T>::get(hotkey, coldkey)
}

pub fn get_target_stakes_per_interval() -> u64 {
TargetStakesPerInterval::<T>::get()
}
Expand Down Expand Up @@ -111,7 +105,7 @@ impl<T: Config> Pallet<T> {
/// # Returns
/// True if the account has enough balance, false otherwise.
pub fn has_enough_stake(coldkey: &T::AccountId, hotkey: &T::AccountId, decrement: u64) -> bool {
Self::get_stake_for_coldkey_and_hotkey(coldkey, hotkey) >= decrement
Stake::<T>::get(hotkey, coldkey) >= decrement
}

/// Increases the stake on the hotkey account under its owning coldkey.
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/staking/remove_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<T: Config> Pallet<T> {
// If the stake is below the minimum, we clear the nomination from storage.
// This only applies to nominator stakes.
// If the coldkey does not own the hotkey, it's a nominator stake.
let new_stake = Self::get_stake_for_coldkey_and_hotkey(&coldkey, &hotkey);
let new_stake = Stake::<T>::get(&hotkey, &coldkey);
Self::clear_small_nomination_if_required(&hotkey, &coldkey, new_stake);

// Check if stake lowered below MinStake and remove Pending children if it did
Expand Down
26 changes: 8 additions & 18 deletions pallets/subtensor/src/tests/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1823,8 +1823,7 @@ fn test_childkey_single_parent_emission() {
}
step_block(7200 + 1);
// Check emission distribution
let parent_stake: u64 =
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey_parent, &parent);
let parent_stake: u64 = Stake::<Test>::get(parent, coldkey_parent);
let parent_stake_on_subnet: u64 =
SubtensorModule::get_stake_for_hotkey_on_subnet(&parent, netuid);

Expand All @@ -1834,8 +1833,7 @@ fn test_childkey_single_parent_emission() {
parent_stake_on_subnet
);

let child_stake: u64 =
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey_child, &child);
let child_stake: u64 = Stake::<Test>::get(child, coldkey_child);
let child_stake_on_subnet: u64 =
SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid);

Expand All @@ -1845,10 +1843,7 @@ fn test_childkey_single_parent_emission() {
child_stake_on_subnet
);

let weight_setter_stake: u64 = SubtensorModule::get_stake_for_coldkey_and_hotkey(
&coldkey_weight_setter,
&weight_setter,
);
let weight_setter_stake: u64 = Stake::<Test>::get(weight_setter, coldkey_weight_setter);
let weight_setter_stake_on_subnet: u64 =
SubtensorModule::get_stake_for_hotkey_on_subnet(&weight_setter, netuid);

Expand Down Expand Up @@ -1969,7 +1964,7 @@ fn test_childkey_multiple_parents_emission() {
];

for (coldkey, hotkey, name) in stakes.iter() {
let stake = SubtensorModule::get_stake_for_coldkey_and_hotkey(coldkey, hotkey);
let stake = Stake::<Test>::get(hotkey, coldkey);
let stake_on_subnet = SubtensorModule::get_stake_for_hotkey_on_subnet(hotkey, netuid);
log::debug!(
"{} stake: {:?}, {} stake on subnet: {:?}",
Expand All @@ -1980,15 +1975,10 @@ fn test_childkey_multiple_parents_emission() {
);
}

let parent1_stake =
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey_parent1, &parent1);
let parent2_stake =
SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey_parent2, &parent2);
let child_stake = SubtensorModule::get_stake_for_coldkey_and_hotkey(&coldkey_child, &child);
let weight_setter_stake = SubtensorModule::get_stake_for_coldkey_and_hotkey(
&coldkey_weight_setter,
&weight_setter,
);
let parent1_stake = Stake::<Test>::get(parent1, coldkey_parent1);
let parent2_stake = Stake::<Test>::get(parent2, coldkey_parent2);
let child_stake = Stake::<Test>::get(child, coldkey_child);
let weight_setter_stake = Stake::<Test>::get(weight_setter, coldkey_weight_setter);

assert!(
parent1_stake > 200_000,
Expand Down
Loading

0 comments on commit 52d2625

Please sign in to comment.