Skip to content
Open
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
2 changes: 1 addition & 1 deletion pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ pub mod pallet {
/// The extrinsic will call the Subtensor pallet to set the maximum allowed UIDs for a subnet.
#[pallet::call_index(15)]
#[pallet::weight(Weight::from_parts(32_140_000, 0)
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(5_u64))
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(6_u64))
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
pub fn sudo_set_max_allowed_uids(
origin: OriginFor<T>,
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1723,7 +1723,7 @@ mod pallet_benchmarks {
}

#[benchmark]
fn subnet_buyback() {
fn add_stake_burn() {
let netuid = NetUid::from(1);
let tempo: u16 = 1;
let seed: u32 = 1;
Expand Down
6 changes: 3 additions & 3 deletions pallets/subtensor/src/extensions/subtensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use sp_std::vec::Vec;
use subtensor_macros::freeze_struct;
use subtensor_runtime_common::{NetUid, NetUidStorageIndex};

const SUBNET_BUYBACK_PRIORITY_BOOST: u64 = 100;
const ADD_STAKE_BURN_PRIORITY_BOOST: u64 = 100;

type CallOf<T> = <T as frame_system::Config>::RuntimeCall;
type OriginOf<T> = <T as frame_system::Config>::RuntimeOrigin;
Expand Down Expand Up @@ -297,12 +297,12 @@ where
.map_err(|_| CustomTransactionError::EvmKeyAssociateRateLimitExceeded)?;
Ok((Default::default(), (), origin))
}
Some(Call::subnet_buyback { netuid, .. }) => {
Some(Call::add_stake_burn { netuid, .. }) => {
Pallet::<T>::ensure_subnet_owner(origin.clone(), *netuid).map_err(|_| {
TransactionValidityError::Invalid(InvalidTransaction::BadSigner)
})?;

Ok((Self::validity_ok(SUBNET_BUYBACK_PRIORITY_BOOST), (), origin))
Ok((Self::validity_ok(ADD_STAKE_BURN_PRIORITY_BOOST), (), origin))
}
_ => Ok((Default::default(), (), origin)),
}
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2722,9 +2722,9 @@ pub enum RateLimitKey<AccountId> {
// Last tx block delegate key limit per account ID
#[codec(index = 5)]
LastTxBlockDelegateTake(AccountId),
// Subnet buyback rate limit
// "Add stake and burn" rate limit
#[codec(index = 6)]
SubnetBuyback(NetUid),
AddStakeBurn(NetUid),
}

pub trait ProxyInterface<AccountId> {
Expand Down
8 changes: 4 additions & 4 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2580,8 +2580,8 @@ mod dispatches {
Self::do_set_voting_power_ema_alpha(netuid, alpha)
}

/// --- Subnet buyback: the extrinsic is a combination of add_stake(add_stake_limit) and
/// burn_alpha. We buy alpha token first and immediately burn the acquired amount of alpha.
/// --- The extrinsic is a combination of add_stake(add_stake_limit) and burn_alpha. We buy
/// alpha token first and immediately burn the acquired amount of alpha (aka Subnet buyback).
#[pallet::call_index(132)]
#[pallet::weight((
Weight::from_parts(368_000_000, 8556)
Expand All @@ -2590,14 +2590,14 @@ mod dispatches {
DispatchClass::Normal,
Pays::Yes
))]
pub fn subnet_buyback(
pub fn add_stake_burn(
origin: T::RuntimeOrigin,
hotkey: T::AccountId,
netuid: NetUid,
amount: TaoCurrency,
limit: Option<TaoCurrency>,
) -> DispatchResult {
Self::do_subnet_buyback(origin, hotkey, netuid, amount, limit)
Self::do_add_stake_burn(origin, hotkey, netuid, amount, limit)
}
}
}
4 changes: 2 additions & 2 deletions pallets/subtensor/src/macros/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ mod errors {
PrecisionLoss,
/// Deprecated call.
Deprecated,
/// Subnet buyback exceeded the operation rate limit
SubnetBuybackRateLimitExceeded,
/// "Add stake and burn" exceeded the operation rate limit
AddStakeBurnRateLimitExceeded,
}
}
4 changes: 2 additions & 2 deletions pallets/subtensor/src/macros/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ mod events {
alpha: AlphaCurrency,
},

/// Subnet buyback event: alpha token was purchased and burned.
SubnetBuyback {
/// "Add stake and burn" event: alpha token was purchased and burned.
AddStakeBurn {
/// The subnet ID
netuid: NetUid,
/// hotky account ID
Expand Down
12 changes: 6 additions & 6 deletions pallets/subtensor/src/staking/recycle_alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl<T: Config> Pallet<T> {

Ok(())
}
pub(crate) fn do_subnet_buyback(
pub(crate) fn do_add_stake_burn(
origin: T::RuntimeOrigin,
hotkey: T::AccountId,
netuid: NetUid,
Expand All @@ -146,12 +146,12 @@ impl<T: Config> Pallet<T> {
Self::ensure_subnet_owner(origin.clone(), netuid)?;

let current_block = Self::get_current_block_as_u64();
let last_block = Self::get_rate_limited_last_block(&RateLimitKey::SubnetBuyback(netuid));
let rate_limit = TransactionType::SubnetBuyback.rate_limit_on_subnet::<T>(netuid);
let last_block = Self::get_rate_limited_last_block(&RateLimitKey::AddStakeBurn(netuid));
let rate_limit = TransactionType::AddStakeBurn.rate_limit_on_subnet::<T>(netuid);

ensure!(
last_block.is_zero() || current_block.saturating_sub(last_block) >= rate_limit,
Error::<T>::SubnetBuybackRateLimitExceeded
Error::<T>::AddStakeBurnRateLimitExceeded
);

let alpha = if let Some(limit) = limit {
Expand All @@ -162,9 +162,9 @@ impl<T: Config> Pallet<T> {

Self::do_burn_alpha(origin, hotkey.clone(), alpha, netuid)?;

Self::set_rate_limited_last_block(&RateLimitKey::SubnetBuyback(netuid), current_block);
Self::set_rate_limited_last_block(&RateLimitKey::AddStakeBurn(netuid), current_block);

Self::deposit_event(Event::SubnetBuyback {
Self::deposit_event(Event::AddStakeBurn {
netuid,
hotkey,
amount,
Expand Down
62 changes: 31 additions & 31 deletions pallets/subtensor/src/tests/recycle_alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ fn test_burn_precision_loss() {
}

#[test]
fn test_subnet_buyback_success() {
fn test_add_stake_burn_success() {
new_test_ext(1).execute_with(|| {
let hotkey_account_id = U256::from(533453);
let coldkey_account_id = U256::from(55453);
Expand All @@ -642,16 +642,16 @@ fn test_subnet_buyback_success() {
TaoCurrency::ZERO
);

// Execute subnet_buyback - this stakes TAO to get Alpha, then burns the Alpha
assert_ok!(SubtensorModule::subnet_buyback(
// Execute add_stake_burn - this stakes TAO to get Alpha, then burns the Alpha
assert_ok!(SubtensorModule::add_stake_burn(
RuntimeOrigin::signed(coldkey_account_id),
hotkey_account_id,
netuid,
amount.into(),
None,
));

// After buyback, hotkey should have zero stake since alpha is burned immediately
// After "add stake and burn", hotkey should have zero stake since alpha is burned immediately
assert_eq!(
SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id),
TaoCurrency::ZERO
Expand All @@ -672,18 +672,18 @@ fn test_subnet_buyback_success() {
)
}));

// Verify SubnetBuyback event was emitted
// Verify AddStakeBurn event was emitted
assert!(System::events().iter().any(|e| {
matches!(
&e.event,
RuntimeEvent::SubtensorModule(Event::SubnetBuyback { .. })
RuntimeEvent::SubtensorModule(Event::AddStakeBurn { .. })
)
}));
});
}

#[test]
fn test_subnet_buyback_with_limit_success() {
fn test_add_stake_burn_with_limit_success() {
new_test_ext(1).execute_with(|| {
let hotkey_account_id = U256::from(533453);
let coldkey_account_id = U256::from(55453);
Expand Down Expand Up @@ -711,16 +711,16 @@ fn test_subnet_buyback_with_limit_success() {
// With 100 TAO into 1000/1000 pool, price moves from 1.0 to ~1.21
let limit_price = TaoCurrency::from(2_000_000_000); // 2.0 TAO per Alpha

// Execute subnet_buyback with limit
assert_ok!(SubtensorModule::subnet_buyback(
// Execute add_stake_burn with limit
assert_ok!(SubtensorModule::add_stake_burn(
RuntimeOrigin::signed(coldkey_account_id),
hotkey_account_id,
netuid,
amount.into(),
Some(limit_price),
));

// After buyback, hotkey should have zero stake since alpha is burned immediately
// After "add stake and burn", hotkey should have zero stake since alpha is burned immediately
assert_eq!(
SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id),
TaoCurrency::ZERO
Expand Down Expand Up @@ -753,7 +753,7 @@ fn test_subnet_buyback_with_limit_success() {
}

#[test]
fn test_subnet_buyback_non_owner_fails() {
fn test_add_stake_burn_non_owner_fails() {
new_test_ext(1).execute_with(|| {
let hotkey_account_id = U256::from(1);
let coldkey_account_id = U256::from(2);
Expand All @@ -772,9 +772,9 @@ fn test_subnet_buyback_non_owner_fails() {
// Give non-owner some balance
SubtensorModule::add_balance_to_coldkey_account(&non_owner_coldkey, amount);

// Non-owner trying to call subnet_buyback should fail with BadOrigin
// Non-owner trying to call add_stake_burn should fail with BadOrigin
assert_noop!(
SubtensorModule::subnet_buyback(
SubtensorModule::add_stake_burn(
RuntimeOrigin::signed(non_owner_coldkey),
hotkey_account_id,
netuid,
Expand All @@ -787,7 +787,7 @@ fn test_subnet_buyback_non_owner_fails() {
}

#[test]
fn test_subnet_buyback_nonexistent_subnet_fails() {
fn test_add_stake_burn_nonexistent_subnet_fails() {
new_test_ext(1).execute_with(|| {
let hotkey_account_id = U256::from(1);
let coldkey_account_id = U256::from(2);
Expand All @@ -796,10 +796,10 @@ fn test_subnet_buyback_nonexistent_subnet_fails() {
// Give some balance
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, amount);

// Try to call subnet_buyback on non-existent subnet
// Try to call add_stake_burn on non-existent subnet
let nonexistent_netuid = NetUid::from(999);
assert_noop!(
SubtensorModule::subnet_buyback(
SubtensorModule::add_stake_burn(
RuntimeOrigin::signed(coldkey_account_id),
hotkey_account_id,
nonexistent_netuid,
Expand All @@ -812,7 +812,7 @@ fn test_subnet_buyback_nonexistent_subnet_fails() {
}

#[test]
fn test_subnet_buyback_insufficient_balance_fails() {
fn test_add_stake_burn_insufficient_balance_fails() {
new_test_ext(1).execute_with(|| {
let hotkey_account_id = U256::from(1);
let coldkey_account_id = U256::from(2);
Expand All @@ -827,9 +827,9 @@ fn test_subnet_buyback_insufficient_balance_fails() {
(amount * 10_000_000).into(),
);

// Try to call subnet_buyback without sufficient balance
// Try to call add_stake_burn without sufficient balance
assert_noop!(
SubtensorModule::subnet_buyback(
SubtensorModule::add_stake_burn(
RuntimeOrigin::signed(coldkey_account_id),
hotkey_account_id,
netuid,
Expand All @@ -842,7 +842,7 @@ fn test_subnet_buyback_insufficient_balance_fails() {
}

#[test]
fn test_subnet_buyback_rate_limit_exceeded() {
fn test_add_stake_burn_rate_limit_exceeded() {
new_test_ext(1).execute_with(|| {
let hotkey_account_id = U256::from(533453);
let coldkey_account_id = U256::from(55453);
Expand All @@ -856,16 +856,16 @@ fn test_subnet_buyback_rate_limit_exceeded() {
let alpha_in = AlphaCurrency::from(1_000_000_000_000);
mock::setup_reserves(netuid, tao_reserve, alpha_in);

// Give coldkey sufficient balance for multiple buybacks
// Give coldkey sufficient balance for multiple "add stake and burn" operations.
SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, amount * 10);

assert_eq!(
SubtensorModule::get_rate_limited_last_block(&RateLimitKey::SubnetBuyback(netuid)),
SubtensorModule::get_rate_limited_last_block(&RateLimitKey::AddStakeBurn(netuid)),
0
);

// First buyback should succeed
assert_ok!(SubtensorModule::subnet_buyback(
// First "add stake and burn" should succeed
assert_ok!(SubtensorModule::add_stake_burn(
RuntimeOrigin::signed(coldkey_account_id),
hotkey_account_id,
netuid,
Expand All @@ -874,27 +874,27 @@ fn test_subnet_buyback_rate_limit_exceeded() {
));

assert_eq!(
SubtensorModule::get_rate_limited_last_block(&RateLimitKey::SubnetBuyback(netuid)),
SubtensorModule::get_rate_limited_last_block(&RateLimitKey::AddStakeBurn(netuid)),
SubtensorModule::get_current_block_as_u64()
);

// Second buyback immediately after should fail due to rate limit
// Second "add stake and burn" immediately after should fail due to rate limit
assert_noop!(
SubtensorModule::subnet_buyback(
SubtensorModule::add_stake_burn(
RuntimeOrigin::signed(coldkey_account_id),
hotkey_account_id,
netuid,
amount.into(),
None,
),
Error::<Test>::SubnetBuybackRateLimitExceeded
Error::<Test>::AddStakeBurnRateLimitExceeded
);

// After stepping past the rate limit, buyback should succeed again
let rate_limit = TransactionType::SubnetBuyback.rate_limit_on_subnet::<Test>(netuid);
// After stepping past the rate limit, "add stake and burn" should succeed again
let rate_limit = TransactionType::AddStakeBurn.rate_limit_on_subnet::<Test>(netuid);
step_block(rate_limit as u16);

assert_ok!(SubtensorModule::subnet_buyback(
assert_ok!(SubtensorModule::add_stake_burn(
RuntimeOrigin::signed(coldkey_account_id),
hotkey_account_id,
netuid,
Expand Down
8 changes: 4 additions & 4 deletions pallets/subtensor/src/utils/rate_limiting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum TransactionType {
MechanismCountUpdate,
MechanismEmission,
MaxUidsTrimming,
SubnetBuyback,
AddStakeBurn,
}

impl TransactionType {
Expand Down Expand Up @@ -45,7 +45,7 @@ impl TransactionType {
(Tempo::<T>::get(netuid) as u64).saturating_mul(epochs)
}
Self::SetSNOwnerHotkey => DefaultSetSNOwnerHotkeyRateLimit::<T>::get(),
Self::SubnetBuyback => Tempo::<T>::get(netuid) as u64,
Self::AddStakeBurn => Tempo::<T>::get(netuid) as u64,

_ => self.rate_limit::<T>(),
}
Expand Down Expand Up @@ -143,7 +143,7 @@ impl From<TransactionType> for u16 {
TransactionType::MechanismCountUpdate => 7,
TransactionType::MechanismEmission => 8,
TransactionType::MaxUidsTrimming => 9,
TransactionType::SubnetBuyback => 10,
TransactionType::AddStakeBurn => 10,
}
}
}
Expand All @@ -161,7 +161,7 @@ impl From<u16> for TransactionType {
7 => TransactionType::MechanismCountUpdate,
8 => TransactionType::MechanismEmission,
9 => TransactionType::MaxUidsTrimming,
10 => TransactionType::SubnetBuyback,
10 => TransactionType::AddStakeBurn,
_ => TransactionType::Unknown,
}
}
Expand Down