Skip to content

Commit

Permalink
Merge pull request #1281 from opentensor/update-staking-priority
Browse files Browse the repository at this point in the history
update staking priority
  • Loading branch information
sam0x17 authored Feb 13, 2025
2 parents 8356aa9 + 1916e1c commit 85c71ac
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 82 deletions.
217 changes: 135 additions & 82 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,18 @@ pub mod pallet {
pub type RevealPeriodEpochs<T: Config> =
StorageMap<_, Twox64Concat, u16, u64, ValueQuery, DefaultRevealPeriodEpochs<T>>;

#[pallet::storage]
/// --- Map (coldkey, hotkey) --> u64 the last block at which stake was added/removed.
pub type LastColdkeyHotkeyStakeBlock<T: Config> = StorageDoubleMap<
_,
Twox64Concat,
T::AccountId,
Twox64Concat,
T::AccountId,
u64,
OptionQuery,
>;

/// ==================
/// ==== Genesis =====
/// ==================
Expand Down Expand Up @@ -1588,6 +1600,19 @@ pub mod pallet {
0
}

/// Returns the transaction priority for stake operations.
pub fn get_priority_staking(coldkey: &T::AccountId, hotkey: &T::AccountId) -> u64 {
match LastColdkeyHotkeyStakeBlock::<T>::get(coldkey, hotkey) {
Some(last_stake_block) => {
let current_block_number = Self::get_current_block_as_u64();
let default_priority = current_block_number.saturating_sub(last_stake_block);

default_priority.saturating_add(u32::MAX as u64)
}
None => 0,
}
}

/// Is the caller allowed to set weights
pub fn check_weights_min_stake(hotkey: &T::AccountId, netuid: u16) -> bool {
// Blacklist weights transactions for low stake peers.
Expand Down Expand Up @@ -1705,11 +1730,15 @@ where
Pallet::<T>::get_priority_set_weights(who, netuid)
}

pub fn get_priority_staking(coldkey: &T::AccountId, hotkey: &T::AccountId) -> u64 {
Pallet::<T>::get_priority_staking(coldkey, hotkey)
}

pub fn check_weights_min_stake(who: &T::AccountId, netuid: u16) -> bool {
Pallet::<T>::check_weights_min_stake(who, netuid)
}

pub fn result_to_validity(result: Result<(), Error<T>>) -> TransactionValidity {
pub fn result_to_validity(result: Result<(), Error<T>>, priority: u64) -> TransactionValidity {
if let Err(err) = result {
match err {
Error::<T>::AmountTooLow => Err(InvalidTransaction::Custom(
Expand Down Expand Up @@ -1750,7 +1779,7 @@ where
}
} else {
Ok(ValidTransaction {
priority: Self::get_priority_vanilla(),
priority,
..Default::default()
})
}
Expand Down Expand Up @@ -1892,14 +1921,17 @@ where
.into();
}
// Fully validate the user input
Self::result_to_validity(Pallet::<T>::validate_add_stake(
who,
hotkey,
*netuid,
*amount_staked,
*amount_staked,
false,
))
Self::result_to_validity(
Pallet::<T>::validate_add_stake(
who,
hotkey,
*netuid,
*amount_staked,
*amount_staked,
false,
),
Self::get_priority_staking(who, hotkey),
)
}
Some(Call::add_stake_limit {
hotkey,
Expand All @@ -1919,29 +1951,35 @@ where
let max_amount = Pallet::<T>::get_max_amount_add(*netuid, *limit_price);

// Fully validate the user input
Self::result_to_validity(Pallet::<T>::validate_add_stake(
who,
hotkey,
*netuid,
*amount_staked,
max_amount,
*allow_partial,
))
Self::result_to_validity(
Pallet::<T>::validate_add_stake(
who,
hotkey,
*netuid,
*amount_staked,
max_amount,
*allow_partial,
),
Self::get_priority_staking(who, hotkey),
)
}
Some(Call::remove_stake {
hotkey,
netuid,
amount_unstaked,
}) => {
// Fully validate the user input
Self::result_to_validity(Pallet::<T>::validate_remove_stake(
who,
hotkey,
*netuid,
*amount_unstaked,
*amount_unstaked,
false,
))
Self::result_to_validity(
Pallet::<T>::validate_remove_stake(
who,
hotkey,
*netuid,
*amount_unstaked,
*amount_unstaked,
false,
),
Self::get_priority_staking(who, hotkey),
)
}
Some(Call::remove_stake_limit {
hotkey,
Expand All @@ -1954,14 +1992,17 @@ where
let max_amount = Pallet::<T>::get_max_amount_remove(*netuid, *limit_price);

// Fully validate the user input
Self::result_to_validity(Pallet::<T>::validate_remove_stake(
who,
hotkey,
*netuid,
*amount_unstaked,
max_amount,
*allow_partial,
))
Self::result_to_validity(
Pallet::<T>::validate_remove_stake(
who,
hotkey,
*netuid,
*amount_unstaked,
max_amount,
*allow_partial,
),
Self::get_priority_staking(who, hotkey),
)
}
Some(Call::move_stake {
origin_hotkey,
Expand All @@ -1978,18 +2019,21 @@ where
}

// Fully validate the user input
Self::result_to_validity(Pallet::<T>::validate_stake_transition(
who,
who,
origin_hotkey,
destination_hotkey,
*origin_netuid,
*destination_netuid,
*alpha_amount,
*alpha_amount,
None,
false,
))
Self::result_to_validity(
Pallet::<T>::validate_stake_transition(
who,
who,
origin_hotkey,
destination_hotkey,
*origin_netuid,
*destination_netuid,
*alpha_amount,
*alpha_amount,
None,
false,
),
Self::get_priority_staking(who, origin_hotkey),
)
}
Some(Call::transfer_stake {
destination_coldkey,
Expand All @@ -2006,18 +2050,21 @@ where
}

// Fully validate the user input
Self::result_to_validity(Pallet::<T>::validate_stake_transition(
who,
destination_coldkey,
hotkey,
hotkey,
*origin_netuid,
*destination_netuid,
*alpha_amount,
*alpha_amount,
None,
true,
))
Self::result_to_validity(
Pallet::<T>::validate_stake_transition(
who,
destination_coldkey,
hotkey,
hotkey,
*origin_netuid,
*destination_netuid,
*alpha_amount,
*alpha_amount,
None,
true,
),
Self::get_priority_staking(who, hotkey),
)
}
Some(Call::swap_stake {
hotkey,
Expand All @@ -2033,18 +2080,21 @@ where
}

// Fully validate the user input
Self::result_to_validity(Pallet::<T>::validate_stake_transition(
who,
who,
hotkey,
hotkey,
*origin_netuid,
*destination_netuid,
*alpha_amount,
*alpha_amount,
None,
false,
))
Self::result_to_validity(
Pallet::<T>::validate_stake_transition(
who,
who,
hotkey,
hotkey,
*origin_netuid,
*destination_netuid,
*alpha_amount,
*alpha_amount,
None,
false,
),
Self::get_priority_staking(who, hotkey),
)
}
Some(Call::swap_stake_limit {
hotkey,
Expand All @@ -2069,18 +2119,21 @@ where
);

// Fully validate the user input
Self::result_to_validity(Pallet::<T>::validate_stake_transition(
who,
who,
hotkey,
hotkey,
*origin_netuid,
*destination_netuid,
*alpha_amount,
max_amount,
Some(*allow_partial),
false,
))
Self::result_to_validity(
Pallet::<T>::validate_stake_transition(
who,
who,
hotkey,
hotkey,
*origin_netuid,
*destination_netuid,
*alpha_amount,
max_amount,
Some(*allow_partial),
false,
),
Self::get_priority_staking(who, hotkey),
)
}
Some(Call::register { netuid, .. } | Call::burned_register { netuid, .. }) => {
if ColdkeySwapScheduled::<T>::contains_key(who) {
Expand Down
2 changes: 2 additions & 0 deletions pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ impl<T: Config> Pallet<T> {
TotalStake::<T>::mutate(|total| {
*total = total.saturating_add(actual_fee);
});
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());

// Step 5. Deposit and log the unstaking event.
Self::deposit_event(Event::StakeRemoved(
Expand Down Expand Up @@ -807,6 +808,7 @@ impl<T: Config> Pallet<T> {
TotalStake::<T>::mutate(|total| {
*total = total.saturating_add(actual_fee);
});
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());

// Step 6. Deposit and log the staking event.
Self::deposit_event(Event::StakeAdded(
Expand Down

0 comments on commit 85c71ac

Please sign in to comment.