-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add increase_take and decrease_take extrinsics, rate limiting, and tests #380
Conversation
pallets/subtensor/tests/mock.rs
Outdated
@@ -123,10 +123,11 @@ parameter_types! { | |||
pub const InitialBondsMovingAverage: u64 = 900_000; | |||
pub const InitialStakePruningMin: u16 = 0; | |||
pub const InitialFoundationDistribution: u64 = 0; | |||
pub const InitialDefaultTake: u16 = 11_796; // 18% honest number. | |||
pub const InitialDefaultTake: u16 = 32_767; // 50% for tests (18% honest number is used in production (see runtime)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why use a different number in tests and runtime?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I would also prefer consistency here without a good reason not to have it
pallets/subtensor/src/staking.rs
Outdated
} | ||
|
||
// --- 4. Set the new take value. | ||
Delegates::<T>::insert(hotkey.clone(), take); | ||
|
||
// --- 5. Emit the take value. | ||
log::info!( | ||
"TakeDecreased( coldkey:{:?}, hotkey:{:?}, take:{:?} )", | ||
coldkey, | ||
hotkey, | ||
take | ||
); | ||
Self::deposit_event(Event::TakeDecreased(coldkey, hotkey, take)); | ||
|
||
// --- 6. Ok and return. | ||
Ok(()) | ||
} | ||
|
||
// ---- The implementation for the extrinsic increase_take | ||
// | ||
// # Args: | ||
// * 'origin': (<T as frame_system::Config>::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 for subnet ID. | ||
// | ||
// # Event: | ||
// * TakeDecreased; | ||
// - On successfully setting a decreased take for this hotkey. | ||
// | ||
// # 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_increase_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_increase_take( origin:{:?} hotkey:{:?}, take:{:?} )", | ||
coldkey, | ||
hotkey, | ||
take | ||
); | ||
|
||
// --- 2. Ensure we are delegating a known key. | ||
// Ensure that the coldkey is the owner. | ||
Self::do_take_checks(&coldkey, &hotkey)?; | ||
|
||
// --- 3. Ensure we are strinctly increasing take | ||
if let Ok(current_take) = Delegates::<T>::try_get(&hotkey) { | ||
ensure!(take > current_take, Error::<T>::InvalidTake); | ||
} | ||
|
||
// --- 4. Ensure take is within the 0 ..= InitialDefaultTake (18%) range | ||
let max_take = T::InitialDefaultTake::get(); | ||
ensure!(take <= max_take, Error::<T>::InvalidTake); | ||
|
||
// --- 5. Enforce the rate limit (independently on do_add_stake rate limits) | ||
let block: u64 = Self::get_current_block_as_u64(); | ||
ensure!( | ||
!Self::exceeds_tx_delegate_take_rate_limit( | ||
Self::get_last_tx_block_delegate_take(&coldkey), | ||
block | ||
), | ||
Error::<T>::TxRateLimitExceeded | ||
); | ||
|
||
// Set last block for rate limiting | ||
Self::set_last_tx_block_delegate_take(&coldkey, block); | ||
|
||
// --- 6. Set the new take value. | ||
Delegates::<T>::insert(hotkey.clone(), take); | ||
|
||
// --- 7. Emit the take value. | ||
log::info!( | ||
"TakeIncreased( coldkey:{:?}, hotkey:{:?}, take:{:?} )", | ||
coldkey, | ||
hotkey, | ||
take | ||
); | ||
Self::deposit_event(Event::TakeIncreased(coldkey, hotkey, take)); | ||
|
||
// --- 8. Ok and return. | ||
Ok(()) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these both just set the take
value, is there a reason they shouldn't be combined into a single extrinsic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still not convinced we can't combine the increase
and decrease
extrinsics, but not a big deal and otherwise lgtm
@@ -88,7 +88,7 @@ pub mod pallet { | |||
#[pallet::weight(T::WeightInfo::sudo_set_default_take())] | |||
pub fn sudo_set_default_take(origin: OriginFor<T>, default_take: u16) -> DispatchResult { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be renamed to reflect the changed functionality?
pub fn sudo_set_default_take(origin: OriginFor<T>, default_take: u16) -> DispatchResult { | |
pub fn sudo_set_max_delegate_take(origin: OriginFor<T>, max_take: u16) -> DispatchResult { |
Description
Added:
Related Issue(s)
Type of Change
Checklist
cargo fmt
andcargo clippy
to ensure my code is formatted and linted correctly