Skip to content

Commit

Permalink
clippy and fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
const authored and const committed Jul 22, 2024
1 parent b8cfd6c commit 6b6460d
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 82 deletions.
6 changes: 2 additions & 4 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,11 +1088,9 @@ pub mod pallet {
pub type Active<T: Config> =
StorageMap<_, Identity, u16, Vec<bool>, ValueQuery, EmptyBoolVec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> rank
pub type Rank<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
pub type Rank<T: Config> = StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> trust
pub type Trust<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
pub type Trust<T: Config> = StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> consensus
pub type Consensus<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
Expand Down
4 changes: 0 additions & 4 deletions pallets/subtensor/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use frame_support::{storage::IterableStorageDoubleMap, weights::Weight};
use sp_core::{Get, U256};

impl<T: Config> Pallet<T> {

/// Swaps the coldkey associated with a set of hotkeys from an old coldkey to a new coldkey.
///
/// # Arguments
Expand Down Expand Up @@ -378,7 +377,6 @@ impl<T: Config> Pallet<T> {
netuid_is_member
}


/// Swaps the total stake associated with a coldkey from the old coldkey to the new coldkey.
///
/// # Arguments
Expand Down Expand Up @@ -635,6 +633,4 @@ impl<T: Config> Pallet<T> {
}
weight.saturating_accrue(T::DbWeight::get().reads(TotalNetworks::<T>::get() as u64));
}


}
57 changes: 34 additions & 23 deletions pallets/subtensor/src/swap_hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,12 @@ impl<T: Config> Pallet<T> {
/// # Note
/// This function performs extensive storage reads and writes, which can be computationally expensive.
/// The accumulated weight should be carefully considered in the context of block limits.
pub fn perform_hotkey_swap( old_hotkey: &T::AccountId, new_hotkey: &T::AccountId, coldkey: &T::AccountId, weight: &mut Weight ) -> DispatchResult {

pub fn perform_hotkey_swap(
old_hotkey: &T::AccountId,
new_hotkey: &T::AccountId,
coldkey: &T::AccountId,
weight: &mut Weight,
) -> DispatchResult {
// 1. Swap owner.
// Owner( hotkey ) -> coldkey -- the coldkey that owns the hotkey.
Owner::<T>::remove(old_hotkey);
Expand All @@ -153,23 +157,27 @@ impl<T: Config> Pallet<T> {
// Add the new key if needed.
if !hotkeys.contains(new_hotkey) {
hotkeys.push(new_hotkey.clone());
}
}
// Remove the old key.
hotkeys.retain(|hk| *hk != *old_hotkey);
OwnedHotkeys::<T>::insert(coldkey, hotkeys);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));

// 3. Swap total hotkey stake.
// TotalHotkeyStake( hotkey ) -> stake -- the total stake that the hotkey has across all delegates.
let old_total_hotkey_stake = TotalHotkeyStake::<T>::get( old_hotkey ); // Get the old total hotkey stake.
let new_total_hotkey_stake = TotalHotkeyStake::<T>::get( new_hotkey ); // Get the new total hotkey stake.
TotalHotkeyStake::<T>::remove( old_hotkey ); // Remove the old total hotkey stake.
TotalHotkeyStake::<T>::insert( new_hotkey, old_total_hotkey_stake.saturating_add( new_total_hotkey_stake ) ); // Insert the new total hotkey stake via the addition.
let old_total_hotkey_stake = TotalHotkeyStake::<T>::get(old_hotkey); // Get the old total hotkey stake.
let new_total_hotkey_stake = TotalHotkeyStake::<T>::get(new_hotkey); // Get the new total hotkey stake.
TotalHotkeyStake::<T>::remove(old_hotkey); // Remove the old total hotkey stake.
TotalHotkeyStake::<T>::insert(
new_hotkey,
old_total_hotkey_stake.saturating_add(new_total_hotkey_stake),
); // Insert the new total hotkey stake via the addition.
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));

// 4. Swap total hotkey stakes.
// TotalHotkeyColdkeyStakesThisInterval( hotkey ) --> (u64: stakes, u64: block_number)
let stake_tuples: Vec<(T::AccountId, (u64, u64))> = TotalHotkeyColdkeyStakesThisInterval::<T>::iter_prefix(old_hotkey).collect();
let stake_tuples: Vec<(T::AccountId, (u64, u64))> =
TotalHotkeyColdkeyStakesThisInterval::<T>::iter_prefix(old_hotkey).collect();
for (coldkey, stake_tup) in stake_tuples {
// NOTE: You could use this to increase your allowed stake operations but this would cost.
TotalHotkeyColdkeyStakesThisInterval::<T>::insert(new_hotkey, &coldkey, stake_tup);
Expand All @@ -179,14 +187,14 @@ impl<T: Config> Pallet<T> {

// 5. Swap LastTxBlock
// LastTxBlock( hotkey ) --> u64 -- the last transaction block for the hotkey.
LastTxBlock::<T>::remove( old_hotkey );
LastTxBlock::<T>::insert( new_hotkey, Self::get_current_block_as_u64() );
LastTxBlock::<T>::remove(old_hotkey);
LastTxBlock::<T>::insert(new_hotkey, Self::get_current_block_as_u64());
weight.saturating_accrue(T::DbWeight::get().reads_writes(0, 2));

// 6. Swap LastTxBlockDelegateTake
// LastTxBlockDelegateTake( hotkey ) --> u64 -- the last transaction block for the hotkey delegate take.
LastTxBlockDelegateTake::<T>::remove( old_hotkey );
LastTxBlockDelegateTake::<T>::insert( new_hotkey, Self::get_current_block_as_u64() );
LastTxBlockDelegateTake::<T>::remove(old_hotkey);
LastTxBlockDelegateTake::<T>::insert(new_hotkey, Self::get_current_block_as_u64());
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));

// 7. Swap Senate members.
Expand All @@ -198,19 +206,19 @@ impl<T: Config> Pallet<T> {

// 8. Swap delegates.
// Delegates( hotkey ) -> take value -- the hotkey delegate take value.
let old_delegate_take = Delegates::<T>::get( old_hotkey );
Delegates::<T>::remove( old_hotkey ); // Remove the old delegate take.
Delegates::<T>::insert( new_hotkey, old_delegate_take ); // Insert the new delegate take.
let old_delegate_take = Delegates::<T>::get(old_hotkey);
Delegates::<T>::remove(old_hotkey); // Remove the old delegate take.
Delegates::<T>::insert(new_hotkey, old_delegate_take); // Insert the new delegate take.
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));

// 9. Swap all subnet specific info.
let all_netuids: Vec<u16> = Self::get_all_subnet_netuids();
for netuid in all_netuids {
for netuid in all_netuids {
// 9.1 Remove the previous hotkey and insert the new hotkey from membership.
// IsNetworkMember( hotkey, netuid ) -> bool -- is the hotkey a subnet member.
let is_network_member: bool = IsNetworkMember::<T>::get( old_hotkey, netuid );
IsNetworkMember::<T>::remove( old_hotkey, netuid );
IsNetworkMember::<T>::insert( new_hotkey, netuid, is_network_member );
let is_network_member: bool = IsNetworkMember::<T>::get(old_hotkey, netuid);
IsNetworkMember::<T>::remove(old_hotkey, netuid);
IsNetworkMember::<T>::insert(new_hotkey, netuid, is_network_member);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));

// 9.2 Swap Uids + Keys.
Expand Down Expand Up @@ -249,7 +257,7 @@ impl<T: Config> Pallet<T> {
}
}

// 9.5 Swap WeightCommits
// 9.5 Swap WeightCommits
// WeightCommits( hotkey ) --> Vec<u64> -- the weight commits for the hotkey.
if is_network_member {
if let Ok(old_weight_commits) = WeightCommits::<T>::try_get(netuid, old_hotkey) {
Expand All @@ -273,14 +281,13 @@ impl<T: Config> Pallet<T> {
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
}
}

}

// 10. Swap Stake.
// Stake( hotkey, coldkey ) -> stake -- the stake that the hotkey controls on behalf of the coldkey.
let stakes: Vec<(T::AccountId, u64)> = Stake::<T>::iter_prefix(old_hotkey).collect();
// Clear the entire old prefix here.
let _ = Stake::<T>::clear_prefix( old_hotkey, stakes.len() as u32, None );
let _ = Stake::<T>::clear_prefix(old_hotkey, stakes.len() as u32, None);
// Iterate over all the staking rows and insert them into the new hotkey.
for (coldkey, old_stake_amount) in stakes {
weight.saturating_accrue(T::DbWeight::get().reads(1));
Expand All @@ -290,7 +297,11 @@ impl<T: Config> Pallet<T> {
// Get the new stake value.
let new_stake_value: u64 = Stake::<T>::get(new_hotkey, &coldkey);
// Insert the new stake value.
Stake::<T>::insert(new_hotkey, &coldkey, new_stake_value.saturating_add(old_stake_amount));
Stake::<T>::insert(
new_hotkey,
&coldkey,
new_stake_value.saturating_add(old_stake_amount),
);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));

// Swap StakingHotkeys.
Expand Down
1 change: 0 additions & 1 deletion pallets/subtensor/tests/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use mock::*;
use pallet_subtensor::*;
use sp_core::U256;


#[test]
fn test_do_swap_coldkey_success() {
new_test_ext(1).execute_with(|| {
Expand Down
Loading

0 comments on commit 6b6460d

Please sign in to comment.