Skip to content

Commit 69fb210

Browse files
committed
Merge remote-tracking branch 'origin/hotfix/sn-to-emit-to' into devnet
2 parents 6684a27 + c01f1bb commit 69fb210

File tree

5 files changed

+44
-164
lines changed

5 files changed

+44
-164
lines changed

pallets/subtensor/src/migrations/migrate_reset_unactive_sn.rs

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
use super::*;
2-
use sp_std::collections::{btree_map::BTreeMap, btree_set::BTreeSet};
3-
4-
use subtensor_swap_interface::SwapHandler;
52

63
pub fn get_unactive_sn_netuids<T: Config>(
74
pool_initial_alpha: AlphaCurrency,
@@ -51,37 +48,6 @@ pub fn migrate_reset_unactive_sn<T: Config>() -> Weight {
5148
let (unactive_netuids, w) = get_unactive_sn_netuids::<T>(pool_initial_alpha);
5249
weight = weight.saturating_add(w);
5350

54-
// Collect the hotkeys to remove for each subnet
55-
let mut to_remove_alpha_hotkeys: BTreeMap<NetUid, Vec<T::AccountId>> = BTreeMap::new();
56-
let mut to_remove_alpha_coldkeys: BTreeMap<NetUid, Vec<(T::AccountId, T::AccountId)>> =
57-
BTreeMap::new();
58-
let mut all_hotkeys_set = BTreeSet::new();
59-
for (hotkey, netuid_i, _) in TotalHotkeyAlpha::<T>::iter() {
60-
weight = weight.saturating_add(T::DbWeight::get().reads(1));
61-
if unactive_netuids.contains(&netuid_i) {
62-
// Only for unactive subnets
63-
to_remove_alpha_hotkeys
64-
.entry(netuid_i)
65-
.or_insert(Vec::new())
66-
.push(hotkey.clone());
67-
all_hotkeys_set.insert(hotkey);
68-
}
69-
}
70-
71-
// Collect the coldkeys to remove for each subnet
72-
for hotkey in all_hotkeys_set.iter() {
73-
for ((coldkey, netuid_i), _) in Alpha::<T>::iter_prefix((&hotkey,)) {
74-
weight = weight.saturating_add(T::DbWeight::get().reads(1));
75-
if unactive_netuids.contains(&netuid_i) {
76-
// Only for unactive subnets
77-
to_remove_alpha_coldkeys
78-
.entry(netuid_i)
79-
.or_insert(Vec::new())
80-
.push((hotkey.clone(), coldkey));
81-
}
82-
}
83-
}
84-
8551
for netuid in unactive_netuids.iter() {
8652
// Reset the subnet as it shouldn't have any emissions
8753
PendingServerEmission::<T>::remove(*netuid);
@@ -92,91 +58,8 @@ pub fn migrate_reset_unactive_sn<T: Config>() -> Weight {
9258
SubnetAlphaInEmission::<T>::remove(*netuid);
9359
SubnetAlphaOutEmission::<T>::remove(*netuid);
9460
weight = weight.saturating_add(T::DbWeight::get().writes(7));
95-
96-
// Reset v3 pool
97-
let burned_tao = match T::SwapInterface::clear_protocol_liquidity(*netuid) {
98-
Ok((_tao, fee_tao, _alpha, _fee_alpha)) => fee_tao,
99-
Err(e) => {
100-
log::error!("Failed to clear protocol liquidity for netuid {netuid:?}: {e:?}");
101-
TaoCurrency::ZERO
102-
}
103-
};
104-
Pallet::<T>::recycle_tao(burned_tao);
105-
// might be based on ticks but this is a rough estimate
106-
weight = weight.saturating_add(T::DbWeight::get().reads_writes(6, 14));
107-
108-
// Recycle already emitted TAO
109-
// or mint 1 TAO to the pool
110-
let subnet_tao = SubnetTAO::<T>::get(*netuid);
111-
if subnet_tao > pool_initial_tao {
112-
let tao_to_recycle = subnet_tao.saturating_sub(pool_initial_tao);
113-
Pallet::<T>::recycle_tao(tao_to_recycle);
114-
TotalStake::<T>::mutate(|total| {
115-
*total = total.saturating_sub(tao_to_recycle);
116-
});
117-
SubnetTAO::<T>::mutate(*netuid, |amount| {
118-
*amount = amount.saturating_sub(tao_to_recycle);
119-
});
120-
weight = weight.saturating_add(T::DbWeight::get().reads_writes(3, 3));
121-
} else if subnet_tao < pool_initial_tao {
122-
let tao_to_add = pool_initial_tao.saturating_sub(subnet_tao);
123-
TotalStake::<T>::mutate(|total| {
124-
*total = total.saturating_add(tao_to_add);
125-
});
126-
SubnetTAO::<T>::mutate(*netuid, |amount| {
127-
*amount = amount.saturating_add(tao_to_add);
128-
});
129-
TotalIssuance::<T>::mutate(|total| {
130-
*total = total.saturating_add(tao_to_add);
131-
});
132-
weight = weight.saturating_add(T::DbWeight::get().reads_writes(3, 3));
133-
}
134-
135-
// Reset pool alpha
136-
SubnetAlphaIn::<T>::insert(*netuid, pool_initial_alpha);
137-
SubnetAlphaOut::<T>::insert(*netuid, AlphaCurrency::ZERO);
138-
// Reset volume
139-
SubnetVolume::<T>::insert(*netuid, 0u128);
140-
weight = weight.saturating_add(T::DbWeight::get().writes(4));
141-
142-
// Reset Alpha stake entries for this subnet
143-
let to_reset: Vec<T::AccountId> = match to_remove_alpha_hotkeys.get(netuid) {
144-
Some(hotkeys) => hotkeys.clone(),
145-
None => Vec::new(),
146-
};
147-
148-
for hotkey in to_reset {
149-
TotalHotkeyAlpha::<T>::remove(&hotkey, *netuid);
150-
TotalHotkeyShares::<T>::remove(&hotkey, *netuid);
151-
TotalHotkeyAlphaLastEpoch::<T>::remove(&hotkey, *netuid);
152-
weight = weight.saturating_add(T::DbWeight::get().writes(3));
153-
154-
// Reset root claimable and claimed
155-
RootClaimable::<T>::mutate(&hotkey, |claimable| {
156-
claimable.remove(netuid);
157-
});
158-
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
159-
let removal_result = RootClaimed::<T>::clear_prefix((*netuid, &hotkey), u32::MAX, None);
160-
weight = weight.saturating_add(
161-
T::DbWeight::get()
162-
.reads_writes(removal_result.loops as u64, removal_result.backend as u64),
163-
);
164-
165-
let to_reset_alpha: Vec<(T::AccountId, T::AccountId)> =
166-
match to_remove_alpha_coldkeys.get(netuid) {
167-
Some(coldkeys) => coldkeys.clone(),
168-
None => Vec::new(),
169-
};
170-
for (hotkey, coldkey) in to_reset_alpha {
171-
Alpha::<T>::remove((hotkey, coldkey, netuid));
172-
weight = weight.saturating_add(T::DbWeight::get().writes(1));
173-
}
174-
}
17561
}
17662

177-
// Run total issuance migration
178-
crate::migrations::migrate_init_total_issuance::migrate_init_total_issuance::<T>();
179-
18063
// Mark Migration as Completed
18164
HasMigrationRun::<T>::insert(&migration_name, true);
18265
weight = weight.saturating_add(T::DbWeight::get().writes(1));

pallets/subtensor/src/tests/migration.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2577,11 +2577,13 @@ fn test_migrate_reset_unactive_sn() {
25772577
RAORecycledForRegistration::<Test>::get(netuid),
25782578
actual_tao_lock_amount_less_pool_tao
25792579
);
2580-
assert!(!pallet_subtensor_swap::AlphaSqrtPrice::<Test>::contains_key(*netuid));
2580+
assert!(pallet_subtensor_swap::AlphaSqrtPrice::<Test>::contains_key(
2581+
*netuid
2582+
));
25812583
assert_eq!(PendingOwnerCut::<Test>::get(netuid), AlphaCurrency::ZERO);
2582-
assert_eq!(SubnetTAO::<Test>::get(netuid), initial_tao);
2583-
assert_eq!(SubnetAlphaIn::<Test>::get(netuid), initial_alpha);
2584-
assert_eq!(SubnetAlphaOut::<Test>::get(netuid), AlphaCurrency::ZERO);
2584+
assert_ne!(SubnetTAO::<Test>::get(netuid), initial_tao);
2585+
assert_ne!(SubnetAlphaIn::<Test>::get(netuid), initial_alpha);
2586+
assert_ne!(SubnetAlphaOut::<Test>::get(netuid), AlphaCurrency::ZERO);
25852587
assert_eq!(SubnetTaoInEmission::<Test>::get(netuid), TaoCurrency::ZERO);
25862588
assert_eq!(
25872589
SubnetAlphaInEmission::<Test>::get(netuid),
@@ -2591,26 +2593,26 @@ fn test_migrate_reset_unactive_sn() {
25912593
SubnetAlphaOutEmission::<Test>::get(netuid),
25922594
AlphaCurrency::ZERO
25932595
);
2594-
assert_eq!(SubnetVolume::<Test>::get(netuid), 0u128);
2596+
assert_ne!(SubnetVolume::<Test>::get(netuid), 0u128);
25952597
for hotkey in 0..10 {
25962598
let hk = U256::from(hotkey);
2597-
assert_eq!(
2599+
assert_ne!(
25982600
TotalHotkeyAlpha::<Test>::get(hk, netuid),
25992601
AlphaCurrency::ZERO
26002602
);
2601-
assert_eq!(
2603+
assert_ne!(
26022604
TotalHotkeyShares::<Test>::get(hk, netuid),
26032605
U64F64::from_num(0.0)
26042606
);
2605-
assert_eq!(
2607+
assert_ne!(
26062608
TotalHotkeyAlphaLastEpoch::<Test>::get(hk, netuid),
26072609
AlphaCurrency::ZERO
26082610
);
2609-
assert_eq!(RootClaimable::<Test>::get(hk).get(netuid), None);
2611+
assert_ne!(RootClaimable::<Test>::get(hk).get(netuid), None);
26102612
for coldkey in 0..10 {
26112613
let ck = U256::from(coldkey);
2612-
assert_eq!(Alpha::<Test>::get((hk, ck, netuid)), U64F64::from_num(0.0));
2613-
assert_eq!(RootClaimed::<Test>::get((netuid, hk, ck)), 0u128);
2614+
assert_ne!(Alpha::<Test>::get((hk, ck, netuid)), U64F64::from_num(0.0));
2615+
assert_ne!(RootClaimed::<Test>::get((netuid, hk, ck)), 0u128);
26142616
}
26152617
}
26162618

pallets/swap-interface/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ pub trait SwapHandler {
4949
fn is_user_liquidity_enabled(netuid: NetUid) -> bool;
5050
fn dissolve_all_liquidity_providers(netuid: NetUid) -> DispatchResult;
5151
fn toggle_user_liquidity(netuid: NetUid, enabled: bool);
52-
fn clear_protocol_liquidity(
53-
netuid: NetUid,
54-
) -> Result<(TaoCurrency, TaoCurrency, AlphaCurrency, AlphaCurrency), DispatchError>;
52+
fn clear_protocol_liquidity(netuid: NetUid) -> DispatchResult;
5553
}
5654

5755
pub trait DefaultPriceLimit<PaidIn, PaidOut>

pallets/swap/src/pallet/impls.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ impl<T: Config> Pallet<T> {
511511
Ok((position, tao, alpha))
512512
}
513513

514-
/// Remove liquidity and return the tao and alpha amounts.
514+
/// Remove liquidity and credit balances back to (coldkey_account_id, hotkey_account_id) stake.
515515
/// Removing is allowed even when user liquidity is enabled.
516516
///
517517
/// Account ID and Position ID identify position in the storage map
@@ -878,13 +878,12 @@ impl<T: Config> Pallet<T> {
878878
rm.alpha.saturating_add(rm.fee_alpha);
879879

880880
// ---------------- USER: refund τ and convert α → stake ----------------
881-
let tao_total_from_pool = rm.tao.saturating_add(rm.fee_tao);
881+
882882
// 1) Refund τ principal directly.
883-
if tao_total_from_pool > TaoCurrency::ZERO {
884-
T::BalanceOps::increase_balance(&owner, tao_total_from_pool);
885-
user_refunded_tao =
886-
user_refunded_tao.saturating_add(tao_total_from_pool);
887-
T::TaoReserve::decrease_provided(netuid, tao_total_from_pool);
883+
if rm.tao > TaoCurrency::ZERO {
884+
T::BalanceOps::increase_balance(&owner, rm.tao);
885+
user_refunded_tao = user_refunded_tao.saturating_add(rm.tao);
886+
T::TaoReserve::decrease_provided(netuid, rm.tao);
888887
}
889888

890889
// 2) Stake ALL withdrawn α (principal + fees) to the best permitted validator.
@@ -945,18 +944,12 @@ impl<T: Config> Pallet<T> {
945944
}
946945

947946
/// Clear **protocol-owned** liquidity and wipe all swap state for `netuid`.
948-
/// # Returns
949-
/// * `(TaoCurrency, AlphaCurrency)` - The amount of TAO and ALPHA burned
950-
pub fn do_clear_protocol_liquidity(
951-
netuid: NetUid,
952-
) -> Result<(TaoCurrency, TaoCurrency, AlphaCurrency, AlphaCurrency), DispatchError> {
947+
pub fn do_clear_protocol_liquidity(netuid: NetUid) -> DispatchResult {
953948
let protocol_account = Self::protocol_account_id();
954949

955950
// 1) Force-close only protocol positions, burning proceeds.
956-
let mut tao = TaoCurrency::ZERO;
957-
let mut fee_tao = TaoCurrency::ZERO;
958-
let mut alpha = AlphaCurrency::ZERO;
959-
let mut fee_alpha = AlphaCurrency::ZERO;
951+
let mut burned_tao = TaoCurrency::ZERO;
952+
let mut burned_alpha = AlphaCurrency::ZERO;
960953

961954
// Collect protocol position IDs first to avoid mutating while iterating.
962955
let protocol_pos_ids: sp_std::vec::Vec<PositionId> = Positions::<T>::iter_prefix((netuid,))
@@ -972,20 +965,27 @@ impl<T: Config> Pallet<T> {
972965
for pos_id in protocol_pos_ids {
973966
match Self::do_remove_liquidity(netuid, &protocol_account, pos_id) {
974967
Ok(rm) => {
975-
tao = tao.saturating_add(rm.tao);
976-
fee_tao = fee_tao.saturating_add(rm.fee_tao);
977-
alpha = alpha.saturating_add(rm.alpha);
978-
fee_alpha = fee_alpha.saturating_add(rm.fee_alpha);
968+
let alpha_total_from_pool: AlphaCurrency =
969+
rm.alpha.saturating_add(rm.fee_alpha);
970+
let tao = rm.tao;
971+
972+
if tao > TaoCurrency::ZERO {
973+
burned_tao = burned_tao.saturating_add(tao);
974+
}
975+
if alpha_total_from_pool > AlphaCurrency::ZERO {
976+
burned_alpha = burned_alpha.saturating_add(alpha_total_from_pool);
977+
}
979978

980979
log::debug!(
981-
"clear_protocol_liquidity: burned protocol pos: netuid={netuid:?}, \
982-
pos_id={pos_id:?}, τ={fee_tao:?}, α={fee_alpha:?}; \
983-
protocol liquidity: {tao:?}, {alpha:?}"
980+
"clear_protocol_liquidity: burned protocol pos: netuid={netuid:?}, pos_id={pos_id:?}, τ={tao:?}, α_total={alpha_total_from_pool:?}"
984981
);
985982
}
986-
Err(e) => log::debug!(
987-
"clear_protocol_liquidity: force-close failed: netuid={netuid:?}, pos_id={pos_id:?}, err={e:?}"
988-
),
983+
Err(e) => {
984+
log::debug!(
985+
"clear_protocol_liquidity: force-close failed: netuid={netuid:?}, pos_id={pos_id:?}, err={e:?}"
986+
);
987+
continue;
988+
}
989989
}
990990
}
991991

@@ -1011,11 +1011,10 @@ impl<T: Config> Pallet<T> {
10111011
EnabledUserLiquidity::<T>::remove(netuid);
10121012

10131013
log::debug!(
1014-
"clear_protocol_liquidity: netuid={netuid:?}, protocol_fees: τ={fee_tao:?}, α={fee_alpha:?}; \
1015-
protocol liquidity: {tao:?}, {alpha:?}; state cleared"
1014+
"clear_protocol_liquidity: netuid={netuid:?}, protocol_burned: τ={burned_tao:?}, α={burned_alpha:?}; state cleared"
10161015
);
10171016

1018-
Ok((tao, fee_tao, alpha, fee_alpha))
1017+
Ok(())
10191018
}
10201019
}
10211020

@@ -1138,9 +1137,7 @@ impl<T: Config> SwapHandler for Pallet<T> {
11381137
fn toggle_user_liquidity(netuid: NetUid, enabled: bool) {
11391138
EnabledUserLiquidity::<T>::insert(netuid, enabled)
11401139
}
1141-
fn clear_protocol_liquidity(
1142-
netuid: NetUid,
1143-
) -> Result<(TaoCurrency, TaoCurrency, AlphaCurrency, AlphaCurrency), DispatchError> {
1140+
fn clear_protocol_liquidity(netuid: NetUid) -> DispatchResult {
11441141
Self::do_clear_protocol_liquidity(netuid)
11451142
}
11461143
}

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
220220
// `spec_version`, and `authoring_version` are the same between Wasm and native.
221221
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
222222
// the compatible custom types.
223-
spec_version: 344,
223+
spec_version: 345,
224224
impl_version: 1,
225225
apis: RUNTIME_API_VERSIONS,
226226
transaction_version: 1,

0 commit comments

Comments
 (0)