Skip to content

Commit

Permalink
fix: delegated stake tests, whitelist serve_axon, lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Dare committed Jul 9, 2024
1 parent 4563e78 commit 4eefcf9
Show file tree
Hide file tree
Showing 5 changed files with 445 additions and 103 deletions.
2 changes: 2 additions & 0 deletions pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ parameter_types! {
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
pub const InitialBaseDifficulty: u64 = 10_000; // Base difficulty
}

impl pallet_subtensor::Config for Test {
Expand Down Expand Up @@ -169,6 +170,7 @@ impl pallet_subtensor::Config for Test {
type AlphaHigh = InitialAlphaHigh;
type AlphaLow = InitialAlphaLow;
type LiquidAlphaOn = InitialLiquidAlphaOn;
type InitialBaseDifficulty = InitialBaseDifficulty;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
Expand Down
67 changes: 37 additions & 30 deletions pallets/subtensor/src/delegate_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,39 +131,46 @@ impl<T: Config> Pallet<T> {
delegates
}

/// Returns the total delegated stake for a given delegate, excluding the stake from the delegate's owner.
///
/// # Arguments
///
/// * `delegate` - A reference to the account ID of the delegate.
///
/// # Returns
///
/// * `u64` - The total amount of stake delegated to the delegate, excluding the owner's stake.
///
///
/// # Notes
///
/// This function retrieves the delegate's information and calculates the total stake from all nominators,
/// excluding the stake from the delegate's owner.
pub fn get_total_delegated_stake(delegate: &T::AccountId) -> u64 {
if !<Delegates<T>>::contains_key(delegate) {
return 0;
pub fn get_total_delegated_stake(coldkey: &T::AccountId) -> u64 {
let mut total_delegated = 0u64;

// Get all hotkeys associated with this coldkey
let hotkeys = StakingHotkeys::<T>::get(coldkey);

for hotkey in hotkeys {
let owner = Owner::<T>::get(&hotkey);

for (delegator, stake) in Stake::<T>::iter_prefix(&hotkey) {
if delegator != owner {
total_delegated += stake;
}
}
}

// Retrieve the delegate's information
let delegate_info: DelegateInfo<T> =
Self::get_delegate_by_existing_account(delegate.clone());
log::info!(
"Total delegated stake for coldkey {:?}: {}",
coldkey,
total_delegated
);
total_delegated
}

// Helper function to get total delegated stake for a hotkey
pub fn get_total_hotkey_delegated_stake(hotkey: &T::AccountId) -> u64 {
let mut total_delegated = 0u64;

// Retrieve the owner's account ID for the given delegate
let owner: T::AccountId = Self::get_owning_coldkey_for_hotkey(delegate);
// Iterate through all delegators for this hotkey
for (delegator, stake) in Stake::<T>::iter_prefix(hotkey) {
if delegator != Self::get_coldkey_for_hotkey(hotkey) {
total_delegated += stake;
}
}

total_delegated
}

// Calculate the total stake from all nominators, excluding the owner's stake
delegate_info
.nominators
.iter()
.filter(|(nominator, _)| nominator != &owner) // Exclude the owner's stake
.map(|(_, stake)| stake.0 as u64) // Map the stake to u64
.sum() // Sum the stakes
// Helper function to get the coldkey associated with a hotkey
pub fn get_coldkey_for_hotkey(hotkey: &T::AccountId) -> T::AccountId {
Owner::<T>::get(hotkey)
}
}
13 changes: 9 additions & 4 deletions pallets/subtensor/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ impl<T: Config> Pallet<T> {
let all_staked_keys: Vec<T::AccountId> = StakingHotkeys::<T>::get(coldkey);
let mut total_staking_balance: u64 = 0;
for hotkey in all_staked_keys {
total_staking_balance += Self::get_stake_for_coldkey_and_hotkey(&coldkey, &hotkey);
total_staking_balance += Self::get_stake_for_coldkey_and_hotkey(coldkey, &hotkey);
}
total_staking_balance += Self::get_coldkey_balance(&coldkey);
total_staking_balance += Self::get_coldkey_balance(coldkey);
total_staking_balance >= MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP
}

Expand Down Expand Up @@ -250,10 +250,15 @@ impl<T: Config> Pallet<T> {
let is_subnet_owner = (0..=TotalNetworks::<T>::get())
.any(|netuid| SubnetOwner::<T>::get(netuid) == *old_coldkey);

// Check if the old_coldkey has more than 500 TAO delegated
let total_delegated = Self::get_total_delegated_stake(old_coldkey);
let has_sufficient_delegation = total_delegated > 500_000_000_000; // 500 TAO in RAO

// Only check the minimum balance if the old_coldkey is not a subnet owner
if !is_subnet_owner {
// and doesn't have sufficient delegation
if !(is_subnet_owner || has_sufficient_delegation) {
ensure!(
Self::meets_min_allowed_coldkey_balance(&old_coldkey),
Self::meets_min_allowed_coldkey_balance(old_coldkey),
Error::<T>::InsufficientBalanceToPerformColdkeySwap
);
}
Expand Down
Loading

0 comments on commit 4eefcf9

Please sign in to comment.