Skip to content

Commit

Permalink
Add move_all_stake dispatchable (#1010)
Browse files Browse the repository at this point in the history
* Add move_all_stake dispatchable

* cargo fmt

---------

Co-authored-by: Cameron Fairchild <[email protected]>
  • Loading branch information
keithtensor and camfairchild authored Nov 25, 2024
1 parent 35288b1 commit 37197f0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 58 deletions.
46 changes: 45 additions & 1 deletion pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1387,8 +1387,52 @@ mod dispatches {
destination_netuid: u16,
alpha_amount: u64,
) -> DispatchResult {
let coldkey = ensure_signed(origin)?;
Self::do_move_stake(
origin,
coldkey,
origin_hotkey,
destination_hotkey,
origin_netuid,
destination_netuid,
alpha_amount,
)
}

/// Moves all stake from one hotkey to another across subnets.
///
/// # Arguments
/// * `origin` - The origin of the transaction, which must be signed by the `origin_hotkey`.
/// * `origin_hotkey` - The account ID of the hotkey from which the stake is being moved.
/// * `destination_hotkey` - The account ID of the hotkey to which the stake is being moved.
/// * `origin_netuid` - The network ID of the origin subnet.
/// * `destination_netuid` - The network ID of the destination subnet.
///
/// # Returns
/// * `DispatchResult` - Indicates the success or failure of the operation.
///
/// # Errors
/// This function will return an error if:
/// * The origin is not signed by the `origin_hotkey`.
/// * Either the origin or destination subnet does not exist.
/// * The `origin_hotkey` or `destination_hotkey` does not exist.
/// * There are locked funds that cannot be moved across subnets.
///
/// # Events
/// Emits a `StakeMoved` event upon successful completion of the stake movement.
#[pallet::call_index(83)]
#[pallet::weight((Weight::from_parts(3_000_000, 0).saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Operational, Pays::No))]
pub fn move_all_stake(
origin: OriginFor<T>,
origin_hotkey: T::AccountId,
destination_hotkey: T::AccountId,
origin_netuid: u16,
destination_netuid: u16,
) -> DispatchResult {
let coldkey = ensure_signed(origin.clone())?;
let alpha_amount =
Alpha::<T>::get((origin_hotkey.clone(), coldkey.clone(), origin_netuid));
Self::do_move_stake(
coldkey,
origin_hotkey,
destination_hotkey,
origin_netuid,
Expand Down
21 changes: 9 additions & 12 deletions pallets/subtensor/src/staking/move_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ impl<T: Config> Pallet<T> {
/// # Events
/// Emits a `StakeMoved` event upon successful completion of the stake movement.
pub fn do_move_stake(
origin: T::RuntimeOrigin,
coldkey: T::AccountId,
origin_hotkey: T::AccountId,
destination_hotkey: T::AccountId,
origin_netuid: u16,
destination_netuid: u16,
alpha_amount: u64,
) -> dispatch::DispatchResult {
// --- 1. Check that the origin is signed by the origin_hotkey.
let coldkey = ensure_signed(origin)?;

// --- 2. Check that the subnet exists.
// --- 1. Check that the subnet exists.
ensure!(
Self::if_subnet_exist(origin_netuid),
Error::<T>::SubnetNotExists
Expand All @@ -44,19 +41,19 @@ impl<T: Config> Pallet<T> {
Error::<T>::SubnetNotExists
);

// --- 3. Check that the origin_hotkey exists.
// --- 2. Check that the origin_hotkey exists.
ensure!(
Self::hotkey_account_exists(&origin_hotkey),
Error::<T>::HotKeyAccountNotExists
);

// --- 4. Check that the destination_hotkey exists.
// --- 3. Check that the destination_hotkey exists.
ensure!(
Self::hotkey_account_exists(&destination_hotkey),
Error::<T>::HotKeyAccountNotExists
);

// --- 6. Get the current alpha stake for the origin hotkey-coldkey pair in the origin subnet
// --- 4. Get the current alpha stake for the origin hotkey-coldkey pair in the origin subnet
let origin_alpha = Alpha::<T>::get((origin_hotkey.clone(), coldkey.clone(), origin_netuid));
ensure!(
alpha_amount <= origin_alpha,
Expand All @@ -79,15 +76,15 @@ impl<T: Config> Pallet<T> {
Error::<T>::UnstakeRateLimitExceeded
);

// --- 7. Unstake the amount of alpha from the origin subnet, converting it to TAO
// --- 5. Unstake the amount of alpha from the origin subnet, converting it to TAO
let origin_tao = Self::unstake_from_subnet(
&origin_hotkey.clone(),
&coldkey.clone(),
origin_netuid,
alpha_amount,
);

// --- 8. Stake the resulting TAO into the destination subnet for the destination hotkey
// --- 6. Stake the resulting TAO into the destination subnet for the destination hotkey
Self::stake_into_subnet(
&destination_hotkey.clone(),
&coldkey.clone(),
Expand Down Expand Up @@ -116,7 +113,7 @@ impl<T: Config> Pallet<T> {
LastAddStakeIncrease::<T>::insert(&destination_hotkey, &coldkey, current_block);
}

// --- 10. Log the event.
// --- 7. Log the event.
log::info!(
"StakeMoved( coldkey:{:?}, origin_hotkey:{:?}, origin_netuid:{:?}, destination_hotkey:{:?}, destination_netuid:{:?} )",
coldkey.clone(),
Expand All @@ -133,7 +130,7 @@ impl<T: Config> Pallet<T> {
destination_netuid,
));

// -- 11. Ok and return.
// -- 8. Ok and return.
Ok(())
}
}
63 changes: 18 additions & 45 deletions pallets/subtensor/tests/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn test_do_move_success() {

// Perform the move
assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
coldkey,
origin_hotkey,
destination_hotkey,
netuid,
Expand Down Expand Up @@ -77,7 +77,7 @@ fn test_do_move_different_subnets() {

// Perform the move
assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
coldkey,
origin_hotkey,
destination_hotkey,
origin_netuid,
Expand Down Expand Up @@ -125,7 +125,7 @@ fn test_do_move_nonexistent_subnet() {
// Attempt to move stake to a non-existent subnet
assert_noop!(
SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
coldkey,
origin_hotkey,
destination_hotkey,
origin_netuid,
Expand Down Expand Up @@ -162,7 +162,7 @@ fn test_do_move_nonexistent_origin_hotkey() {
add_network(netuid, 0, 0);
assert_noop!(
SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
coldkey,
nonexistent_origin_hotkey,
destination_hotkey,
netuid,
Expand Down Expand Up @@ -211,7 +211,7 @@ fn test_do_move_nonexistent_destination_hotkey() {
add_network(netuid, 0, 0);
assert_noop!(
SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
coldkey,
origin_hotkey,
nonexistent_destination_hotkey,
netuid,
Expand Down Expand Up @@ -257,7 +257,7 @@ fn test_do_move_zero_stake() {
SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey);
SubtensorModule::create_account_if_non_existent(&coldkey, &destination_hotkey);
assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
coldkey,
origin_hotkey,
destination_hotkey,
netuid,
Expand Down Expand Up @@ -306,7 +306,7 @@ fn test_do_move_all_stake() {
SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey);
SubtensorModule::create_account_if_non_existent(&coldkey, &destination_hotkey);
assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
coldkey,
origin_hotkey,
destination_hotkey,
netuid,
Expand Down Expand Up @@ -352,7 +352,7 @@ fn test_do_move_half_stake() {
SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey);
SubtensorModule::create_account_if_non_existent(&coldkey, &destination_hotkey);
assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
coldkey,
origin_hotkey,
destination_hotkey,
netuid,
Expand Down Expand Up @@ -401,7 +401,7 @@ fn test_do_move_partial_stake() {
SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey);
SubtensorModule::create_account_if_non_existent(&coldkey, &destination_hotkey);
assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
coldkey,
origin_hotkey,
destination_hotkey,
netuid,
Expand Down Expand Up @@ -452,20 +452,10 @@ fn test_do_move_multiple_times() {
TargetStakesPerInterval::<Test>::set(1000);
for _ in 0..3 {
assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
hotkey1,
hotkey2,
netuid,
netuid,
alpha,
coldkey, hotkey1, hotkey2, netuid, netuid, alpha,
));
assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
hotkey2,
hotkey1,
netuid,
netuid,
alpha,
coldkey, hotkey2, hotkey1, netuid, netuid, alpha,
));
}

Expand Down Expand Up @@ -504,7 +494,7 @@ fn test_do_move_wrong_origin() {
SubtensorModule::create_account_if_non_existent(&coldkey, &destination_hotkey);
assert_err!(
SubtensorModule::do_move_stake(
RuntimeOrigin::signed(wrong_coldkey),
wrong_coldkey,
origin_hotkey,
destination_hotkey,
netuid,
Expand Down Expand Up @@ -553,12 +543,7 @@ fn test_do_move_same_hotkey() {

// Attempt to move stake to the same hotkey
assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
hotkey,
hotkey,
netuid,
netuid,
alpha,
coldkey, hotkey, hotkey, netuid, netuid, alpha,
));

// Check that stake remains unchanged
Expand Down Expand Up @@ -591,7 +576,7 @@ fn test_do_move_event_emission() {
// Move stake and capture events
System::reset_events();
assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
coldkey,
origin_hotkey,
destination_hotkey,
netuid,
Expand Down Expand Up @@ -630,7 +615,7 @@ fn test_do_move_storage_updates() {
let alpha = Alpha::<Test>::get((origin_hotkey, coldkey, origin_netuid));

assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
coldkey,
origin_hotkey,
destination_hotkey,
origin_netuid,
Expand Down Expand Up @@ -679,7 +664,7 @@ fn test_do_move_max_values() {

// Move maximum stake
assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
coldkey,
origin_hotkey,
destination_hotkey,
netuid,
Expand Down Expand Up @@ -726,22 +711,10 @@ fn test_do_move_rate_limit_enforced() {

// Move stake multiple times
assert_ok!(SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
hotkey1,
hotkey2,
netuid,
netuid,
alpha,
coldkey, hotkey1, hotkey2, netuid, netuid, alpha,
));
assert_err!(
SubtensorModule::do_move_stake(
RuntimeOrigin::signed(coldkey),
hotkey2,
hotkey1,
netuid,
netuid,
alpha,
),
SubtensorModule::do_move_stake(coldkey, hotkey2, hotkey1, netuid, netuid, alpha,),
Error::<Test>::StakeRateLimitExceeded,
);

Expand Down

0 comments on commit 37197f0

Please sign in to comment.