Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
open-junius committed Jul 8, 2024
2 parents 60f3713 + 7d589c4 commit c210904
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 20 deletions.
1 change: 0 additions & 1 deletion pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ pub mod pallet {
#[pallet::storage] // --- MAP ( u64 ) --> Vec<coldkeys_to_drain> | Coldkeys to drain on the specific block.
pub type ColdkeysToSwapAtBlock<T: Config> =
StorageMap<_, Identity, u64, Vec<T::AccountId>, ValueQuery, EmptyAccounts<T>>;

/// -- ITEM (switches liquid alpha on)
#[pallet::type_value]
pub fn DefaultLiquidAlpha<T: Config>() -> bool {
Expand Down
21 changes: 21 additions & 0 deletions pallets/subtensor/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,27 @@ impl<T: Config> Pallet<T> {
// Self::add_balance_to_coldkey_account( &coldkey_a, all_current_stake_i );
// weight = weight.saturating_add(T::DbWeight::get().reads_writes(2, 2));
// }
// Unstake all delegate stake make by this coldkey to non-owned hotkeys
// let staking_hotkeys = StakingHotkeys::<T>::get(&current_coldkey);

// // iterate over all staking hotkeys.
// for hotkey in staking_hotkeys {
// // Get the current stake
// let current_stake: u64 =
// Self::get_stake_for_coldkey_and_hotkey(&current_coldkey, &hotkey);

// // Unstake all balance if there's any stake
// if current_stake > 0 {
// Self::do_remove_stake(
// RawOrigin::Signed(current_coldkey.clone()).into(),
// hotkey.clone(),
// current_stake,
// )?;
// }
// }

// let total_balance = Self::get_coldkey_balance(&current_coldkey);
// log::info!("Total Bank Balance: {:?}", total_balance);

// // Get the total balance here.
// let total_balance = Self::get_coldkey_balance( &coldkey_a );
Expand Down
48 changes: 31 additions & 17 deletions pallets/subtensor/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ impl<T: Config> Pallet<T> {
writes = writes.saturating_add(1u64); // One write for remove
}
StakingHotkeys::<T>::insert(coldkey.clone(), staking_hotkeys);
writes = writes.saturating_add(1u64); // One write for insert
}

// Clear the prefix for the old hotkey after transferring all stakes
Expand Down Expand Up @@ -769,25 +770,38 @@ impl<T: Config> Pallet<T> {
) {
// Swap the owners.
let old_owned_hotkeys = OwnedHotkeys::<T>::get(old_coldkey);
for owned_key in old_owned_hotkeys.iter() {
for owned_key in old_owned_hotkeys.clone().iter() {
Owner::<T>::insert(owned_key, new_coldkey);
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 3));
}
OwnedHotkeys::<T>::remove(old_coldkey.clone());
OwnedHotkeys::<T>::insert(new_coldkey.clone(), old_owned_hotkeys);

// Swap all the keys the coldkey is staking too.
let staking_hotkeys = StakingHotkeys::<T>::get(old_coldkey);
StakingHotkeys::<T>::remove(old_coldkey.clone());
for hotkey in staking_hotkeys.iter() {
// Remove the previous stake and re-insert it.
let stake = Stake::<T>::get(hotkey, old_coldkey);
Stake::<T>::remove(hotkey, old_coldkey);
Stake::<T>::insert(hotkey, new_coldkey, stake);
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 3));
// Find all hotkeys for this coldkey
let hotkeys = OwnedHotkeys::<T>::get(old_coldkey);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 0));
for hotkey in hotkeys.iter() {
let stake = Stake::<T>::get(&hotkey, old_coldkey);
Stake::<T>::remove(&hotkey, old_coldkey);
Stake::<T>::insert(&hotkey, new_coldkey, stake);

// Update StakingHotkeys map
let staking_hotkeys = StakingHotkeys::<T>::get(old_coldkey);
StakingHotkeys::<T>::insert(new_coldkey.clone(), staking_hotkeys);

weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 3));
}
OwnedHotkeys::<T>::remove(old_coldkey.clone());
OwnedHotkeys::<T>::insert(new_coldkey.clone(), old_owned_hotkeys.clone());

// Swap all the keys the coldkey is staking too.
let staking_hotkeys = StakingHotkeys::<T>::get(old_coldkey);
StakingHotkeys::<T>::remove(old_coldkey.clone());
for hotkey in staking_hotkeys.iter() {
// Remove the previous stake and re-insert it.
let stake = Stake::<T>::get(hotkey, old_coldkey);
Stake::<T>::remove(hotkey, old_coldkey);
Stake::<T>::insert(hotkey, new_coldkey, stake);
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 3));
}
// Add the new staking keys value.
StakingHotkeys::<T>::insert(new_coldkey.clone(), staking_hotkeys.clone());
}
// Add the new staking keys value.
StakingHotkeys::<T>::insert(new_coldkey.clone(), staking_hotkeys.clone());
}

/// Swaps the total hotkey-coldkey stakes for the current interval from the old coldkey to the new coldkey.
Expand Down
37 changes: 37 additions & 0 deletions pallets/subtensor/tests/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,43 @@ fn test_swap_stake_for_coldkey() {
// Verify TotalHotkeyStake remains unchanged
assert_eq!(TotalHotkeyStake::<Test>::get(hotkey1), stake_amount1);
assert_eq!(TotalHotkeyStake::<Test>::get(hotkey2), stake_amount2);

// Verify TotalStake and TotalIssuance remain unchanged
assert_eq!(TotalStake::<Test>::get(), stake_amount1 + stake_amount2);
assert_eq!(TotalIssuance::<Test>::get(), stake_amount1 + stake_amount2);

// Verify weight update
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads_writes(5, 6);
assert_eq!(weight, expected_weight);
});
}

#[test]
fn test_swap_owner_for_coldkey() {
new_test_ext(1).execute_with(|| {
let old_coldkey = U256::from(1);
let new_coldkey = U256::from(2);
let hotkey1 = U256::from(3);
let hotkey2 = U256::from(4);
let mut weight = Weight::zero();

// Initialize Owner for old_coldkey
Owner::<Test>::insert(hotkey1, old_coldkey);
Owner::<Test>::insert(hotkey2, old_coldkey);

// Initialize OwnedHotkeys map
OwnedHotkeys::<Test>::insert(old_coldkey, vec![hotkey1, hotkey2]);

// Perform the swap
SubtensorModule::swap_owner_for_coldkey(&old_coldkey, &new_coldkey, &mut weight);

// Verify the swap
assert_eq!(Owner::<Test>::get(hotkey1), new_coldkey);
assert_eq!(Owner::<Test>::get(hotkey2), new_coldkey);

// Verify weight update
let expected_weight = <Test as frame_system::Config>::DbWeight::get().reads_writes(1, 2);
assert_eq!(weight, expected_weight);
});
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 159,
spec_version: 160,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -294,7 +294,7 @@ parameter_types! {

pub const RootEnterDuration: BlockNumber = 5 * 60 * 24; // 24 hours

pub const RootExtendDuration: BlockNumber = 5 * 60 * 3; // 3 hours
pub const RootExtendDuration: BlockNumber = 5 * 60 * 12; // 12 hours

pub const DisallowPermissionlessEntering: Option<Balance> = None;
pub const DisallowPermissionlessExtending: Option<Balance> = None;
Expand Down

0 comments on commit c210904

Please sign in to comment.