Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/devnet-ready' into fix/remove-ex…
Browse files Browse the repository at this point in the history
…tra-do-set-pending-children
  • Loading branch information
sam0x17 committed Feb 13, 2025
2 parents 70637f2 + 85c71ac commit b378719
Show file tree
Hide file tree
Showing 7 changed files with 558 additions and 88 deletions.
2 changes: 1 addition & 1 deletion pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub mod pallet {
netuid: u16,
min_difficulty: u64,
) -> DispatchResult {
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
ensure_root(origin)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<T: Config> Pallet<T> {
let alpha_out_i = alpha_emission_i;
// Only emit TAO if the subnetwork allows registration.
if !Self::get_network_registration_allowed(*netuid_i)
&& Self::get_network_pow_registration_allowed(*netuid_i)
&& !Self::get_network_pow_registration_allowed(*netuid_i)
{
tao_in_i = asfloat!(0.0);
}
Expand Down
265 changes: 183 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 @@ -1885,15 +1914,24 @@ where
netuid,
amount_staked,
}) => {
if ColdkeySwapScheduled::<T>::contains_key(who) {
return InvalidTransaction::Custom(
CustomTransactionError::ColdkeyInSwapSchedule.into(),
)
.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 @@ -1902,33 +1940,46 @@ where
limit_price,
allow_partial,
}) => {
if ColdkeySwapScheduled::<T>::contains_key(who) {
return InvalidTransaction::Custom(
CustomTransactionError::ColdkeyInSwapSchedule.into(),
)
.into();
}

// Calcaulate the maximum amount that can be executed with price limit
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 +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 @@ -1957,19 +2011,29 @@ where
destination_netuid,
alpha_amount,
}) => {
if ColdkeySwapScheduled::<T>::contains_key(who) {
return InvalidTransaction::Custom(
CustomTransactionError::ColdkeyInSwapSchedule.into(),
)
.into();
}

// 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 @@ -1978,39 +2042,59 @@ where
destination_netuid,
alpha_amount,
}) => {
if ColdkeySwapScheduled::<T>::contains_key(who) {
return InvalidTransaction::Custom(
CustomTransactionError::ColdkeyInSwapSchedule.into(),
)
.into();
}

// 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,
origin_netuid,
destination_netuid,
alpha_amount,
}) => {
if ColdkeySwapScheduled::<T>::contains_key(who) {
return InvalidTransaction::Custom(
CustomTransactionError::ColdkeyInSwapSchedule.into(),
)
.into();
}

// 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 @@ -2020,6 +2104,13 @@ where
limit_price,
allow_partial,
}) => {
if ColdkeySwapScheduled::<T>::contains_key(who) {
return InvalidTransaction::Custom(
CustomTransactionError::ColdkeyInSwapSchedule.into(),
)
.into();
}

// Get the max amount possible to exchange
let max_amount = Pallet::<T>::get_max_amount_move(
*origin_netuid,
Expand All @@ -2028,20 +2119,30 @@ 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) {
return InvalidTransaction::Custom(
CustomTransactionError::ColdkeyInSwapSchedule.into(),
)
.into();
}

let registrations_this_interval =
Pallet::<T>::get_registrations_this_interval(*netuid);
let max_registrations_per_interval =
Expand Down
Loading

0 comments on commit b378719

Please sign in to comment.