Skip to content
Open
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
9 changes: 5 additions & 4 deletions pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2190,10 +2190,11 @@ mod pallet_benchmarks {
netuid,
);

assert!(
Lock::<T>::iter_prefix((coldkey, netuid))
.any(|(locked_hotkey, _)| locked_hotkey == hotkey_dest)
);
// Lock moving temporarily disabled
// assert!(
// Lock::<T>::iter_prefix((coldkey, netuid))
// .any(|(locked_hotkey, _)| locked_hotkey == hotkey_dest)
// );
}

impl_benchmark_test_suite!(
Expand Down
241 changes: 124 additions & 117 deletions pallets/subtensor/src/staking/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,62 +169,65 @@ impl<T: Config> Pallet<T> {
/// If no lock exists, creates one. If one exists, the hotkey must match.
/// Top-up adds to locked_mass after rolling forward.
pub fn do_lock_stake(
coldkey: &T::AccountId,
netuid: NetUid,
hotkey: &T::AccountId,
amount: AlphaBalance,
_coldkey: &T::AccountId,
_netuid: NetUid,
_hotkey: &T::AccountId,
_amount: AlphaBalance,
) -> dispatch::DispatchResult {
ensure!(!amount.is_zero(), Error::<T>::AmountTooLow);
Self::ensure_available_stake(coldkey, netuid, amount)
.map_err(|_| Error::<T>::InsufficientStakeForLock)?;

let now = Self::get_current_block_as_u64();
let existing = Lock::<T>::iter_prefix((coldkey, netuid)).next();

match existing {
None => {
Self::insert_lock_state(
coldkey,
netuid,
hotkey,
LockState {
locked_mass: amount,
unlocked_mass: 0.into(),
conviction: U64F64::saturating_from_num(0),
last_update: now,
},
);
}
Some((existing_hotkey, existing)) => {
ensure!(*hotkey == existing_hotkey, Error::<T>::LockHotkeyMismatch);

let lock = Self::roll_forward_lock(existing, now);
let new_locked = lock.locked_mass.saturating_add(amount);
Self::insert_lock_state(
coldkey,
netuid,
hotkey,
LockState {
locked_mass: new_locked,
unlocked_mass: lock.unlocked_mass,
conviction: lock.conviction,
last_update: now,
},
);
}
}

// Update the total hotkey lock
Self::upsert_hotkey_lock(hotkey, netuid, amount);

Self::deposit_event(Event::StakeLocked {
coldkey: coldkey.clone(),
hotkey: hotkey.clone(),
netuid,
amount,
});

// Temporarily disabled
Ok(())

// ensure!(!amount.is_zero(), Error::<T>::AmountTooLow);
// Self::ensure_available_stake(coldkey, netuid, amount)
// .map_err(|_| Error::<T>::InsufficientStakeForLock)?;

// let now = Self::get_current_block_as_u64();
// let existing = Lock::<T>::iter_prefix((coldkey, netuid)).next();

// match existing {
// None => {
// Self::insert_lock_state(
// coldkey,
// netuid,
// hotkey,
// LockState {
// locked_mass: amount,
// unlocked_mass: 0.into(),
// conviction: U64F64::saturating_from_num(0),
// last_update: now,
// },
// );
// }
// Some((existing_hotkey, existing)) => {
// ensure!(*hotkey == existing_hotkey, Error::<T>::LockHotkeyMismatch);

// let lock = Self::roll_forward_lock(existing, now);
// let new_locked = lock.locked_mass.saturating_add(amount);
// Self::insert_lock_state(
// coldkey,
// netuid,
// hotkey,
// LockState {
// locked_mass: new_locked,
// unlocked_mass: lock.unlocked_mass,
// conviction: lock.conviction,
// last_update: now,
// },
// );
// }
// }

// // Update the total hotkey lock
// Self::upsert_hotkey_lock(hotkey, netuid, amount);

// Self::deposit_event(Event::StakeLocked {
// coldkey: coldkey.clone(),
// hotkey: hotkey.clone(),
// netuid,
// amount,
// });

// Ok(())
}

pub fn do_unlock_stake(
Expand Down Expand Up @@ -527,71 +530,75 @@ impl<T: Config> Pallet<T> {
/// The conviction is reset to zero if the destination and source hotkeys
/// are owned by different coldkeys, otherwise it is preserved.
pub fn do_move_lock(
coldkey: &T::AccountId,
destination_hotkey: &T::AccountId,
netuid: NetUid,
_coldkey: &T::AccountId,
_destination_hotkey: &T::AccountId,
_netuid: NetUid,
) -> DispatchResult {
let now = Self::get_current_block_as_u64();

match Lock::<T>::iter_prefix((coldkey, netuid)).next() {
Some((origin_hotkey, existing)) => {
let old_hotkey_owner = Self::get_owning_coldkey_for_hotkey(&origin_hotkey);
let new_hotkey_owner = Self::get_owning_coldkey_for_hotkey(destination_hotkey);
let same_owner = old_hotkey_owner != DefaultAccount::<T>::get()
&& new_hotkey_owner != DefaultAccount::<T>::get()
&& old_hotkey_owner == new_hotkey_owner;

let mut existing_rolled = Self::roll_forward_lock(existing, now);
let existing_conviction = existing_rolled.conviction;
if !same_owner {
existing_rolled.conviction = U64F64::saturating_from_num(0);
}

Lock::<T>::remove((coldkey.clone(), netuid, origin_hotkey.clone()));
Self::insert_lock_state(
coldkey,
netuid,
destination_hotkey,
LockState {
locked_mass: existing_rolled.locked_mass,
unlocked_mass: existing_rolled.unlocked_mass,
conviction: existing_rolled.conviction,
last_update: now,
},
);

// Update the total hotkey locks for destination hotkey
Self::upsert_hotkey_lock(destination_hotkey, netuid, existing_rolled.locked_mass);

// Reduce the total hotkey locks and conviction for the origin hotkey
Self::reduce_hotkey_lock(
&origin_hotkey,
netuid,
existing_rolled.locked_mass,
existing_conviction,
);

// If the same coldkey owns both the origin and destination hotkeys, also
// transfer the conviction instead of resetting it
if same_owner {
HotkeyLock::<T>::mutate(netuid, destination_hotkey, |dest_lock_opt| {
if let Some(dest_lock) = dest_lock_opt {
dest_lock.conviction =
dest_lock.conviction.saturating_add(existing_conviction);
}
});
}
// Temporarily disabled
// TODO: When enabled, uncomment an assert in move_lock benchmark
Ok(())

Self::deposit_event(Event::LockMoved {
coldkey: coldkey.clone(),
origin_hotkey,
destination_hotkey: destination_hotkey.clone(),
netuid,
});
Ok(())
}
None => Err(Error::<T>::NoExistingLock.into()),
}
// let now = Self::get_current_block_as_u64();

// match Lock::<T>::iter_prefix((coldkey, netuid)).next() {
// Some((origin_hotkey, existing)) => {
// let old_hotkey_owner = Self::get_owning_coldkey_for_hotkey(&origin_hotkey);
// let new_hotkey_owner = Self::get_owning_coldkey_for_hotkey(destination_hotkey);
// let same_owner = old_hotkey_owner != DefaultAccount::<T>::get()
// && new_hotkey_owner != DefaultAccount::<T>::get()
// && old_hotkey_owner == new_hotkey_owner;

// let mut existing_rolled = Self::roll_forward_lock(existing, now);
// let existing_conviction = existing_rolled.conviction;
// if !same_owner {
// existing_rolled.conviction = U64F64::saturating_from_num(0);
// }

// Lock::<T>::remove((coldkey.clone(), netuid, origin_hotkey.clone()));
// Self::insert_lock_state(
// coldkey,
// netuid,
// destination_hotkey,
// LockState {
// locked_mass: existing_rolled.locked_mass,
// unlocked_mass: existing_rolled.unlocked_mass,
// conviction: existing_rolled.conviction,
// last_update: now,
// },
// );

// // Update the total hotkey locks for destination hotkey
// Self::upsert_hotkey_lock(destination_hotkey, netuid, existing_rolled.locked_mass);

// // Reduce the total hotkey locks and conviction for the origin hotkey
// Self::reduce_hotkey_lock(
// &origin_hotkey,
// netuid,
// existing_rolled.locked_mass,
// existing_conviction,
// );

// // If the same coldkey owns both the origin and destination hotkeys, also
// // transfer the conviction instead of resetting it
// if same_owner {
// HotkeyLock::<T>::mutate(netuid, destination_hotkey, |dest_lock_opt| {
// if let Some(dest_lock) = dest_lock_opt {
// dest_lock.conviction =
// dest_lock.conviction.saturating_add(existing_conviction);
// }
// });
// }

// Self::deposit_event(Event::LockMoved {
// coldkey: coldkey.clone(),
// origin_hotkey,
// destination_hotkey: destination_hotkey.clone(),
// netuid,
// });
// Ok(())
// }
// None => Err(Error::<T>::NoExistingLock.into()),
// }
}

pub fn auto_lock_owner_cut(netuid: NetUid, amount: AlphaBalance) {
Expand Down
12 changes: 12 additions & 0 deletions pallets/subtensor/src/subnets/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ impl<T: Config> Pallet<T> {

let owner_alpha_stake = AlphaBalance::ZERO;

// With the full lock retained in the reserve, this will normally be zero.
let tao_recycled_for_registration = actual_tao_lock_amount.saturating_sub(total_pool_tao);

// Core pool + ownership
SubnetTAO::<T>::insert(netuid_to_register, total_pool_tao);
SubnetAlphaIn::<T>::insert(netuid_to_register, total_pool_alpha);
Expand All @@ -245,6 +248,15 @@ impl<T: Config> Pallet<T> {
SubnetAlphaInProvided::<T>::insert(netuid_to_register, AlphaBalance::ZERO);
SubnetAlphaOut::<T>::insert(netuid_to_register, owner_alpha_stake);
SubnetVolume::<T>::insert(netuid_to_register, 0u128);
RAORecycledForRegistration::<T>::insert(netuid_to_register, tao_recycled_for_registration);

if tao_recycled_for_registration > TaoBalance::ZERO
&& let Some(subnet_account_id) = Self::get_subnet_account_id(netuid_to_register)
{
// The subnet account ID is guaranteed to have adequate balance for this
// recycle because of transfer operation earlier. No need to check this result.
let _ = Self::recycle_tao(&subnet_account_id, tao_recycled_for_registration);
}

if total_pool_tao > TaoBalance::ZERO {
// Record in TotalStake the initial TAO in the pool.
Expand Down
Loading
Loading