Skip to content
Merged
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
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 @@ -1886,14 +1915,17 @@ where
amount_staked,
}) => {
// 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 @@ -1906,29 +1938,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 @@ -1941,14 +1979,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 @@ -1958,18 +1999,21 @@ where
alpha_amount,
}) => {
// 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 @@ -1979,18 +2023,21 @@ where
alpha_amount,
}) => {
// 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 @@ -1999,18 +2046,21 @@ where
alpha_amount,
}) => {
// 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 @@ -2028,18 +2078,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, .. }) => {
let registrations_this_interval =
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
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 233,
spec_version: 234,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading