Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,33 +314,33 @@ benchmarks! {
assert_ok!(Subtensor::<T>::register_network(RawOrigin::Signed(coldkey.clone()).into()));
}: dissolve_network(RawOrigin::Signed(coldkey), 1)

swap_hotkey {
let seed: u32 = 1;
let coldkey: T::AccountId = account("Alice", 0, seed);
let old_hotkey: T::AccountId = account("Bob", 0, seed);
let new_hotkey: T::AccountId = account("Charlie", 0, seed);

let netuid = 1u16;
Subtensor::<T>::init_new_network(netuid, 100);
Subtensor::<T>::set_min_burn(netuid, 1);
Subtensor::<T>::set_max_burn(netuid, 1);
Subtensor::<T>::set_target_registrations_per_interval(netuid, 256);
Subtensor::<T>::set_max_registrations_per_block(netuid, 256);

Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64);
assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, old_hotkey.clone()));
assert_ok!(Subtensor::<T>::become_delegate(RawOrigin::Signed(coldkey.clone()).into(), old_hotkey.clone()));

let max_uids = Subtensor::<T>::get_max_allowed_uids(netuid) as u32;
for i in 0..max_uids - 1 {
let coldkey: T::AccountId = account("Axon", 0, i);
let hotkey: T::AccountId = account("Hotkey", 0, i);

Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64);
assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey));
assert_ok!(Subtensor::<T>::add_stake(RawOrigin::Signed(coldkey).into(), old_hotkey.clone(), 1_000_000_000));
}
}: _(RawOrigin::Signed(coldkey), old_hotkey, new_hotkey)
// swap_hotkey {
// let seed: u32 = 1;
// let coldkey: T::AccountId = account("Alice", 0, seed);
// let old_hotkey: T::AccountId = account("Bob", 0, seed);
// let new_hotkey: T::AccountId = account("Charlie", 0, seed);

// let netuid = 1u16;
// Subtensor::<T>::init_new_network(netuid, 100);
// Subtensor::<T>::set_min_burn(netuid, 1);
// Subtensor::<T>::set_max_burn(netuid, 1);
// Subtensor::<T>::set_target_registrations_per_interval(netuid, 256);
// Subtensor::<T>::set_max_registrations_per_block(netuid, 256);

// Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64);
// assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, old_hotkey.clone()));
// assert_ok!(Subtensor::<T>::become_delegate(RawOrigin::Signed(coldkey.clone()).into(), old_hotkey.clone()));

// let max_uids = Subtensor::<T>::get_max_allowed_uids(netuid) as u32;
// for i in 0..max_uids - 1 {
// let coldkey: T::AccountId = account("Axon", 0, i);
// let hotkey: T::AccountId = account("Hotkey", 0, i);

// Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64);
// assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey));
// assert_ok!(Subtensor::<T>::add_stake(RawOrigin::Signed(coldkey).into(), old_hotkey.clone(), 1_000_000_000));
// }
// }: _(RawOrigin::Signed(coldkey), old_hotkey, new_hotkey)

commit_weights {
let tempo: u16 = 1;
Expand Down
35 changes: 24 additions & 11 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2061,17 +2061,17 @@ pub mod pallet {
}

/// The extrinsic for user to change its hotkey
#[pallet::call_index(70)]
#[pallet::weight((Weight::from_parts(1_940_000_000, 0)
.saturating_add(T::DbWeight::get().reads(272))
.saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))]
pub fn swap_hotkey(
origin: OriginFor<T>,
hotkey: T::AccountId,
new_hotkey: T::AccountId,
) -> DispatchResultWithPostInfo {
Self::do_swap_hotkey(origin, &hotkey, &new_hotkey)
}
///#[pallet::call_index(70)]
///#[pallet::weight((Weight::from_parts(1_940_000_000, 0)
///.saturating_add(T::DbWeight::get().reads(272))
///.saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))]
///pub fn swap_hotkey(
/// origin: OriginFor<T>,
/// hotkey: T::AccountId,
/// new_hotkey: T::AccountId,
///) -> DispatchResultWithPostInfo {
/// Self::do_swap_hotkey(origin, &hotkey, &new_hotkey)
///}

/// The extrinsic for user to change the coldkey associated with their account.
///
Expand Down Expand Up @@ -2253,6 +2253,19 @@ pub mod pallet {
pub fn dissolve_network(origin: OriginFor<T>, netuid: u16) -> DispatchResult {
Self::user_remove_network(origin, netuid)
}

/// Sets values for liquid alpha
#[pallet::call_index(64)]
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
pub fn sudo_hotfix_swap_coldkey_delegates(
origin: OriginFor<T>,
old_coldkey: T::AccountId,
new_coldkey: T::AccountId,
) -> DispatchResult {
ensure_root(origin)?;
Self::swap_hotfix(&old_coldkey, &new_coldkey);
Ok(())
}
}

// ---- Subtensor helper functions.
Expand Down
102 changes: 98 additions & 4 deletions pallets/subtensor/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,8 @@ impl<T: Config> Pallet<T> {
log::info!("Transferring stake for hotkey {:?}: {}", hotkey, stake);
if stake > 0 {
// Insert the stake for the hotkey and new coldkey
Stake::<T>::insert(hotkey, new_coldkey, stake);
let old_stake = Stake::<T>::get(hotkey, new_coldkey);
Stake::<T>::insert(hotkey, new_coldkey, stake.saturating_add(old_stake));
total_transferred_stake = total_transferred_stake.saturating_add(stake);

// Update the owner of the hotkey to the new coldkey
Expand All @@ -861,6 +862,52 @@ impl<T: Config> Pallet<T> {
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
}
}
log::info!(
"Starting transfer of delegated stakes for old coldkey: {:?}",
old_coldkey
);

for staking_hotkey in StakingHotkeys::<T>::get(old_coldkey) {
log::info!("Processing staking hotkey: {:?}", staking_hotkey);
if Stake::<T>::contains_key(staking_hotkey.clone(), old_coldkey) {
let hotkey = &staking_hotkey;
// Retrieve and remove the stake associated with the hotkey and old coldkey
let stake: u64 = Stake::<T>::get(hotkey, old_coldkey);
Stake::<T>::remove(hotkey, old_coldkey);
log::info!(
"Transferring delegated stake for hotkey {:?}: {}",
hotkey,
stake
);
if stake > 0 {
// Insert the stake for the hotkey and new coldkey
let old_stake = Stake::<T>::get(hotkey, new_coldkey);
Stake::<T>::insert(hotkey, new_coldkey, stake.saturating_add(old_stake));
total_transferred_stake = total_transferred_stake.saturating_add(stake);
log::info!(
"Updated stake for hotkey {:?} under new coldkey {:?}: {}",
hotkey,
new_coldkey,
stake.saturating_add(old_stake)
);

// Update the transaction weight
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1));
}
} else {
log::info!(
"No stake found for staking hotkey {:?} under old coldkey {:?}",
staking_hotkey,
old_coldkey
);
weight.saturating_accrue(T::DbWeight::get().reads(1));
}
}

log::info!(
"Completed transfer of delegated stakes for old coldkey: {:?}",
old_coldkey
);

// Log the total transferred stake
log::info!("Total transferred stake: {}", total_transferred_stake);
Expand Down Expand Up @@ -888,13 +935,30 @@ impl<T: Config> Pallet<T> {
}

// Update the list of owned hotkeys for both old and new coldkeys

let mut new_owned_hotkeys = OwnedHotkeys::<T>::get(new_coldkey);
for hotkey in old_owned_hotkeys {
if !new_owned_hotkeys.contains(&hotkey) {
new_owned_hotkeys.push(hotkey);
}
}

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

// Update the staking hotkeys for both old and new coldkeys
let staking_hotkeys: Vec<T::AccountId> = StakingHotkeys::<T>::take(old_coldkey);
StakingHotkeys::<T>::insert(new_coldkey, staking_hotkeys);
let staking_hotkeys: Vec<T::AccountId> = StakingHotkeys::<T>::get(old_coldkey);

let mut existing_staking_hotkeys = StakingHotkeys::<T>::get(new_coldkey);
for hotkey in staking_hotkeys {
if !existing_staking_hotkeys.contains(&hotkey) {
existing_staking_hotkeys.push(hotkey);
}
}

StakingHotkeys::<T>::remove(old_coldkey);
StakingHotkeys::<T>::insert(new_coldkey, existing_staking_hotkeys);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));

// Log the total stake of old and new coldkeys after the swap
Expand All @@ -907,6 +971,36 @@ impl<T: Config> Pallet<T> {
TotalColdkeyStake::<T>::get(new_coldkey)
);
}

pub fn swap_hotfix(old_coldkey: &T::AccountId, new_coldkey: &T::AccountId) {
// let weight = T::DbWeight::get().reads_writes(2, 1);
let staking_hotkeys = StakingHotkeys::<T>::get(old_coldkey);
for staking_hotkey in staking_hotkeys {
if Stake::<T>::contains_key(staking_hotkey.clone(), old_coldkey) {
let hotkey = &staking_hotkey;
// Retrieve and remove the stake associated with the hotkey and old coldkey
let stake: u64 = Stake::<T>::get(hotkey, old_coldkey);
Stake::<T>::remove(hotkey, old_coldkey);
if stake > 0 {
// Insert the stake for the hotkey and new coldkey
let old_stake = Stake::<T>::get(hotkey, new_coldkey);
Stake::<T>::insert(hotkey, new_coldkey, stake.saturating_add(old_stake));
}
}
}

let mut existing_staking_hotkeys = StakingHotkeys::<T>::get(new_coldkey);

let staking_hotkeys = StakingHotkeys::<T>::get(old_coldkey);
for hotkey in staking_hotkeys {
if !existing_staking_hotkeys.contains(&hotkey) {
existing_staking_hotkeys.push(hotkey);
}
}
StakingHotkeys::<T>::insert(new_coldkey, existing_staking_hotkeys);
StakingHotkeys::<T>::remove(old_coldkey);
}

/// Swaps the total hotkey-coldkey stakes for the current interval from the old coldkey to the new coldkey.
///
/// # Arguments
Expand Down
Loading