From 135918e07e16010c0e6f54bce2849263a05409ee Mon Sep 17 00:00:00 2001 From: unconst Date: Wed, 27 Mar 2024 10:48:25 -0500 Subject: [PATCH 1/8] allow decreasing delegate take --- pallets/subtensor/src/lib.rs | 35 ++++++++++++++ pallets/subtensor/src/staking.rs | 81 ++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index d4a3559d16..518d2348ce 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -871,6 +871,7 @@ pub mod pallet { NetworkMinLockCostSet(u64), // Event created when the network minimum locking cost is set. SubnetLimitSet(u16), // Event created when the maximum number of subnets is set NetworkLockCostReductionIntervalSet(u64), // Event created when the lock cost reduction is set + TakeDecreased( T::AccountId, T::AccountId, u16 ), // Event created when the take for a delegate is decreased. HotkeySwapped { coldkey: T::AccountId, old_hotkey: T::AccountId, @@ -937,6 +938,7 @@ pub mod pallet { StakeTooLowForRoot, // --- Thrown when a hotkey attempts to join the root subnet with too little stake AllNetworksInImmunity, // --- Thrown when all subnets are in the immunity period NotEnoughBalance, + InvalidTake, // -- Thrown when take being set is invalid. } // ================== @@ -1279,6 +1281,38 @@ pub mod pallet { Self::do_become_delegate(origin, hotkey, Self::get_default_take()) } + // --- Allows delegates to decrease its take value. + // + // # Args: + // * 'origin': (Origin): + // - The signature of the caller's coldkey. + // + // * 'hotkey' (T::AccountId): + // - The hotkey we are delegating (must be owned by the coldkey.) + // + // * 'take' (u64): + // - The new stake proportion that this hotkey takes from delegations. + // + // # Event: + // * DelegateAdded; + // - On successfully setting a hotkey as a delegate. + // + // # Raises: + // * 'NotRegistered': + // - The hotkey we are delegating is not registered on the network. + // + // * 'NonAssociatedColdKey': + // - The hotkey we are delegating is not owned by the calling coldkey. + // + // * 'InvalidTransaction': + // - The delegate is setting a take which is not lower than the previous. + // + #[pallet::call_index(63)] + #[pallet::weight((0, DispatchClass::Normal, Pays::No))] + pub fn decrease_take(origin: OriginFor, hotkey: T::AccountId, take: u16) -> DispatchResult { + Self::do_decrease_take(origin, hotkey, take) + } + // --- Adds stake to a hotkey. The call is made from the // coldkey account linked in the hotkey. // Only the associated coldkey is allowed to make staking and @@ -1325,6 +1359,7 @@ pub mod pallet { Self::do_add_stake(origin, hotkey, amount_staked) } + // ---- Remove stake from the staking account. The call must be made // from the coldkey account attached to the neuron metadata. Only this key // has permission to make staking and unstaking requests. diff --git a/pallets/subtensor/src/staking.rs b/pallets/subtensor/src/staking.rs index 7f723cb718..af062a202b 100644 --- a/pallets/subtensor/src/staking.rs +++ b/pallets/subtensor/src/staking.rs @@ -86,6 +86,87 @@ impl Pallet { Ok(()) } + // ---- The implementation for the extrinsic become_delegate: signals that this hotkey allows delegated stake. + // + // # Args: + // * 'origin': (RuntimeOrigin): + // - The signature of the caller's coldkey. + // + // * 'hotkey' (T::AccountId): + // - The hotkey we are delegating (must be owned by the coldkey.) + // + // * 'take' (u16): + // - The stake proportion that this hotkey takes from delegations. + // + // # Event: + // * DelegateAdded; + // - On successfully setting a hotkey as a delegate. + // + // # Raises: + // * 'NotRegistered': + // - The hotkey we are delegating is not registered on the network. + // + // * 'NonAssociatedColdKey': + // - The hotkey we are delegating is not owned by the calling coldket. + // + // * 'TxRateLimitExceeded': + // - Thrown if key has hit transaction rate limit + // + pub fn do_decrease_take( + origin: T::RuntimeOrigin, + hotkey: T::AccountId, + take: u16, + ) -> dispatch::DispatchResult { + // --- 1. We check the coldkey signature. + let coldkey = ensure_signed(origin)?; + log::info!( + "do_decrease_take( origin:{:?} hotkey:{:?}, take:{:?} )", + coldkey, + hotkey, + take + ); + + // --- 2. Ensure we are delegating an known key. + ensure!( + Self::hotkey_account_exists(&hotkey), + Error::::NotRegistered + ); + + // --- 3. Ensure that the coldkey is the owner. + ensure!( + Self::coldkey_owns_hotkey(&coldkey, &hotkey), + Error::::NonAssociatedColdKey + ); + + // --- 4. Ensure we are not already a delegate (dont allow changing delegate take.) + ensure!( + !Self::hotkey_is_delegate(&hotkey), + Error::::AlreadyDelegate + ); + + // --- 5. Ensure we are always decreasing take never increasing. + let current_take: u16 = Delegates::::get(hotkey.clone()); + ensure!( + take < current_take, + Error::::InvalidTake + ); + + // --- 6. Set the new take value. + Delegates::::insert(hotkey.clone(), take); + + // --- 7. Emit the take value. + log::info!( + "TakeDecreased( coldkey:{:?}, hotkey:{:?}, take:{:?} )", + coldkey, + hotkey, + take + ); + Self::deposit_event(Event::TakeDecreased(coldkey, hotkey, take)); + + // --- 8. Ok and return. + Ok(()) + } + // ---- The implementation for the extrinsic add_stake: Adds stake to a hotkey account. // // # Args: From cfbee665bc1e61f9bce9e564460545cbbf0fad44 Mon Sep 17 00:00:00 2001 From: Unconst <32490803+unconst@users.noreply.github.com> Date: Wed, 27 Mar 2024 11:04:06 -0500 Subject: [PATCH 2/8] Update pallets/subtensor/src/staking.rs Co-authored-by: cuteolaf --- pallets/subtensor/src/staking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/staking.rs b/pallets/subtensor/src/staking.rs index af062a202b..04be7c52c2 100644 --- a/pallets/subtensor/src/staking.rs +++ b/pallets/subtensor/src/staking.rs @@ -86,7 +86,7 @@ impl Pallet { Ok(()) } - // ---- The implementation for the extrinsic become_delegate: signals that this hotkey allows delegated stake. + // ---- The implementation for the extrinsic decrease_take // // # Args: // * 'origin': (RuntimeOrigin): From 28583483b29f472c4876d7e427f67d689092abdb Mon Sep 17 00:00:00 2001 From: Unconst <32490803+unconst@users.noreply.github.com> Date: Wed, 27 Mar 2024 11:04:12 -0500 Subject: [PATCH 3/8] Update pallets/subtensor/src/lib.rs Co-authored-by: cuteolaf --- pallets/subtensor/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 518d2348ce..287940d6b3 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -1284,7 +1284,7 @@ pub mod pallet { // --- Allows delegates to decrease its take value. // // # Args: - // * 'origin': (Origin): + // * 'origin': (::Origin): // - The signature of the caller's coldkey. // // * 'hotkey' (T::AccountId): From bfd5d8b84e960d0b7ee8e84aa27341bb6d0a6e79 Mon Sep 17 00:00:00 2001 From: Unconst <32490803+unconst@users.noreply.github.com> Date: Wed, 27 Mar 2024 11:04:17 -0500 Subject: [PATCH 4/8] Update pallets/subtensor/src/staking.rs Co-authored-by: cuteolaf --- pallets/subtensor/src/staking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/staking.rs b/pallets/subtensor/src/staking.rs index 04be7c52c2..ba90740895 100644 --- a/pallets/subtensor/src/staking.rs +++ b/pallets/subtensor/src/staking.rs @@ -89,7 +89,7 @@ impl Pallet { // ---- The implementation for the extrinsic decrease_take // // # Args: - // * 'origin': (RuntimeOrigin): + // * 'origin': (::RuntimeOrigin): // - The signature of the caller's coldkey. // // * 'hotkey' (T::AccountId): From 56492c1dfff5f4321d2033d196778a7dd1867c02 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Mon, 8 Apr 2024 12:21:53 -0400 Subject: [PATCH 5/8] comment nit Co-authored-by: cuteolaf --- pallets/subtensor/src/staking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/staking.rs b/pallets/subtensor/src/staking.rs index ba90740895..20caa6c55f 100644 --- a/pallets/subtensor/src/staking.rs +++ b/pallets/subtensor/src/staking.rs @@ -126,7 +126,7 @@ impl Pallet { take ); - // --- 2. Ensure we are delegating an known key. + // --- 2. Ensure we are delegating a known key. ensure!( Self::hotkey_account_exists(&hotkey), Error::::NotRegistered From 60ba03172e760f74e92983bf3c5afa2e9b54ed4d Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Mon, 8 Apr 2024 12:22:50 -0400 Subject: [PATCH 6/8] fix comment Co-authored-by: cuteolaf --- pallets/subtensor/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 287940d6b3..264160a421 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -1290,7 +1290,7 @@ pub mod pallet { // * 'hotkey' (T::AccountId): // - The hotkey we are delegating (must be owned by the coldkey.) // - // * 'take' (u64): + // * 'take' (u16): // - The new stake proportion that this hotkey takes from delegations. // // # Event: From 3b2fb136df7351677c98f532144996e318b0e3c9 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Mon, 8 Apr 2024 12:23:09 -0400 Subject: [PATCH 7/8] fix comment Co-authored-by: cuteolaf --- pallets/subtensor/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 264160a421..d6bf65348b 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -1294,8 +1294,8 @@ pub mod pallet { // - The new stake proportion that this hotkey takes from delegations. // // # Event: - // * DelegateAdded; - // - On successfully setting a hotkey as a delegate. + // * TakeDecreased; + // - On successfully setting a decreased take for this hotkey. // // # Raises: // * 'NotRegistered': From c1743742a82ce6643639f01527e886912789338e Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Mon, 8 Apr 2024 12:23:24 -0400 Subject: [PATCH 8/8] fix comment Co-authored-by: cuteolaf --- pallets/subtensor/src/staking.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/subtensor/src/staking.rs b/pallets/subtensor/src/staking.rs index 20caa6c55f..d8aa9573b5 100644 --- a/pallets/subtensor/src/staking.rs +++ b/pallets/subtensor/src/staking.rs @@ -99,8 +99,8 @@ impl Pallet { // - The stake proportion that this hotkey takes from delegations. // // # Event: - // * DelegateAdded; - // - On successfully setting a hotkey as a delegate. + // * TakeDecreased; + // - On successfully setting a decreased take for this hotkey. // // # Raises: // * 'NotRegistered':