Skip to content

Commit 47a5d54

Browse files
committed
Merge branch 'hotfix/vune/epoch-fn-inp' into feat/new-coinbase
2 parents ba0fae0 + 40b4ffc commit 47a5d54

File tree

3 files changed

+82
-88
lines changed

3 files changed

+82
-88
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,7 @@ impl<T: Config> Pallet<T> {
567567
incentive,
568568
});
569569
}
570-
log::debug!(
571-
"incentives: increasing stake for {hotkey:?} to {incentive:?} with owner {owner:?}"
572-
);
570+
573571
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
574572
&destination,
575573
&owner,

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -447,19 +447,13 @@ impl<T: Config> Pallet<T> {
447447
let should_refund_owner: bool = reg_at < start_block;
448448

449449
// 3) Compute owner's received emission in TAO at current price (ONLY if we may refund).
450-
// Emission::<T> is Vec<AlphaCurrency>. We:
451-
// - sum emitted α,
450+
// We:
451+
// - get the current alpha issuance,
452452
// - apply owner fraction to get owner α,
453453
// - price that α using a *simulated* AMM swap.
454454
let mut owner_emission_tao = TaoCurrency::ZERO;
455455
if should_refund_owner && !lock_cost.is_zero() {
456-
let total_emitted_alpha_u128: u128 =
457-
Emission::<T>::get(netuid)
458-
.into_iter()
459-
.fold(0u128, |acc, e_alpha| {
460-
let e_u64: u64 = Into::<u64>::into(e_alpha);
461-
acc.saturating_add(e_u64 as u128)
462-
});
456+
let total_emitted_alpha_u128: u128 = Self::get_alpha_issuance(netuid).to_u64() as u128;
463457

464458
if total_emitted_alpha_u128 > 0 {
465459
let owner_fraction: U96F32 = Self::get_float_subnet_owner_cut();
@@ -469,22 +463,12 @@ impl<T: Config> Pallet<T> {
469463
.saturating_to_num::<u64>();
470464

471465
owner_emission_tao = if owner_alpha_u64 > 0 {
472-
let order = GetTaoForAlpha::with_amount(owner_alpha_u64);
473-
match T::SwapInterface::sim_swap(netuid.into(), order) {
474-
Ok(sim) => TaoCurrency::from(sim.amount_paid_out),
475-
Err(e) => {
476-
log::debug!(
477-
"destroy_alpha_in_out_stakes: sim_swap owner α→τ failed (netuid={netuid:?}, alpha={owner_alpha_u64}, err={e:?}); falling back to price multiply.",
478-
);
479-
let cur_price: U96F32 =
480-
T::SwapInterface::current_alpha_price(netuid.into());
481-
let val_u64 = U96F32::from_num(owner_alpha_u64)
482-
.saturating_mul(cur_price)
483-
.floor()
484-
.saturating_to_num::<u64>();
485-
TaoCurrency::from(val_u64)
486-
}
487-
}
466+
let cur_price: U96F32 = T::SwapInterface::current_alpha_price(netuid.into());
467+
let val_u64 = U96F32::from_num(owner_alpha_u64)
468+
.saturating_mul(cur_price)
469+
.floor()
470+
.saturating_to_num::<u64>();
471+
TaoCurrency::from(val_u64)
488472
} else {
489473
TaoCurrency::ZERO
490474
};

pallets/subtensor/src/tests/networks.rs

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -216,43 +216,49 @@ fn dissolve_owner_cut_refund_logic() {
216216
// One staker and a TAO pot (not relevant to refund amount).
217217
let sh = U256::from(77);
218218
let sc = U256::from(88);
219-
Alpha::<Test>::insert((sh, sc, net), U64F64::from_num(100u128));
219+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
220+
&sh,
221+
&sc,
222+
net,
223+
AlphaCurrency::from(800u64),
224+
);
220225
SubnetTAO::<Test>::insert(net, TaoCurrency::from(1_000));
221226

222227
// Lock & emissions: total emitted α = 800.
223228
let lock: TaoCurrency = TaoCurrency::from(2_000);
224229
SubtensorModule::set_subnet_locked_balance(net, lock);
225-
Emission::<Test>::insert(
226-
net,
227-
vec![AlphaCurrency::from(200), AlphaCurrency::from(600)],
228-
);
230+
// ensure there was some Alpha issued
231+
assert!(SubtensorModule::get_alpha_issuance(net).to_u64() > 0);
229232

230233
// Owner cut = 11796 / 65535 (about 18%).
231234
SubnetOwnerCut::<Test>::put(11_796u16);
232235

233236
// Compute expected refund with the SAME math as the pallet.
234237
let frac: U96F32 = SubtensorModule::get_float_subnet_owner_cut();
235-
let total_emitted_alpha: u64 = 800;
238+
let total_emitted_alpha: u64 = SubtensorModule::get_alpha_issuance(net).to_u64();
236239
let owner_alpha_u64: u64 = U96F32::from_num(total_emitted_alpha)
237240
.saturating_mul(frac)
238241
.floor()
239242
.saturating_to_num::<u64>();
240243

241-
// Current α→τ price for this subnet.
242-
let price: U96F32 =
243-
<Test as pallet::Config>::SwapInterface::current_alpha_price(net.into());
244-
let owner_emission_tao_u64: u64 = U96F32::from_num(owner_alpha_u64)
245-
.saturating_mul(price)
246-
.floor()
247-
.saturating_to_num::<u64>();
244+
// Use the current alpha price to estimate the TAO equivalent.
245+
let owner_emission_tao = {
246+
let price: U96F32 =
247+
<Test as pallet::Config>::SwapInterface::current_alpha_price(net.into());
248+
U96F32::from_num(owner_alpha_u64)
249+
.saturating_mul(price)
250+
.floor()
251+
.saturating_to_num::<u64>()
252+
.into()
253+
};
248254

249-
let expected_refund: TaoCurrency =
250-
lock.saturating_sub(TaoCurrency::from(owner_emission_tao_u64));
255+
let expected_refund: TaoCurrency = lock.saturating_sub(owner_emission_tao);
251256

252257
let before = SubtensorModule::get_coldkey_balance(&oc);
253258
assert_ok!(SubtensorModule::do_dissolve_network(net));
254259
let after = SubtensorModule::get_coldkey_balance(&oc);
255260

261+
assert!(after > before); // some refund is expected
256262
assert_eq!(
257263
TaoCurrency::from(after),
258264
TaoCurrency::from(before) + expected_refund
@@ -841,15 +847,10 @@ fn destroy_alpha_out_many_stakers_complex_distribution() {
841847
SubnetTAO::<Test>::insert(netuid, TaoCurrency::from(tao_pot));
842848
SubtensorModule::set_subnet_locked_balance(netuid, TaoCurrency::from(lock));
843849

850+
// ensure there was some Alpha issued
851+
assert!(SubtensorModule::get_alpha_issuance(netuid).to_u64() > 0);
852+
844853
// Owner already earned some emission; owner-cut = 50 %
845-
Emission::<Test>::insert(
846-
netuid,
847-
vec![
848-
AlphaCurrency::from(1_000),
849-
AlphaCurrency::from(2_000),
850-
AlphaCurrency::from(1_500),
851-
],
852-
);
853854
SubnetOwnerCut::<Test>::put(32_768u16); // ~ 0.5 in fixed-point
854855

855856
// ── 4) balances before ──────────────────────────────────────────────
@@ -879,28 +880,23 @@ fn destroy_alpha_out_many_stakers_complex_distribution() {
879880

880881
// ── 5b) expected owner refund with price-aware emission deduction ───
881882
let frac: U96F32 = SubtensorModule::get_float_subnet_owner_cut();
882-
let total_emitted_alpha: u64 = 1_000 + 2_000 + 1_500; // 4500 α
883+
let total_emitted_alpha: u64 = SubtensorModule::get_alpha_issuance(netuid).to_u64();
883884
let owner_alpha_u64: u64 = U96F32::from_num(total_emitted_alpha)
884885
.saturating_mul(frac)
885886
.floor()
886887
.saturating_to_num::<u64>();
887888

888-
let order = GetTaoForAlpha::<Test>::with_amount(owner_alpha_u64);
889-
let owner_emission_tao =
890-
<Test as pallet::Config>::SwapInterface::sim_swap(netuid.into(), order)
891-
.map(|res| res.amount_paid_out)
892-
.unwrap_or_else(|_| {
893-
// Fallback matches the pallet's fallback
894-
let price: U96F32 =
895-
<Test as pallet::Config>::SwapInterface::current_alpha_price(netuid.into());
896-
U96F32::from_num(owner_alpha_u64)
897-
.saturating_mul(price)
898-
.floor()
899-
.saturating_to_num::<u64>()
900-
.into()
901-
});
902-
903-
let expected_refund = lock.saturating_sub(owner_emission_tao.to_u64());
889+
let owner_emission_tao: u64 = {
890+
// Fallback matches the pallet's fallback
891+
let price: U96F32 =
892+
<Test as pallet::Config>::SwapInterface::current_alpha_price(netuid.into());
893+
U96F32::from_num(owner_alpha_u64)
894+
.saturating_mul(price)
895+
.floor()
896+
.saturating_to_num::<u64>()
897+
};
898+
899+
let expected_refund = lock.saturating_sub(owner_emission_tao);
904900

905901
// ── 6) run distribution (credits τ to coldkeys, wipes α state) ─────
906902
assert_ok!(SubtensorModule::destroy_alpha_in_out_stakes(netuid));
@@ -947,34 +943,38 @@ fn destroy_alpha_out_refund_gating_by_registration_block() {
947943
// Lock and (nonzero) emissions
948944
let lock_u64: u64 = 50_000;
949945
SubtensorModule::set_subnet_locked_balance(netuid, TaoCurrency::from(lock_u64));
950-
Emission::<Test>::insert(
951-
netuid,
952-
vec![AlphaCurrency::from(1_500u64), AlphaCurrency::from(3_000u64)], // total 4_500 α
953-
);
954946
// Owner cut ≈ 50%
955947
SubnetOwnerCut::<Test>::put(32_768u16);
956948

949+
// give some stake to other key
950+
let other_cold = U256::from(1_234);
951+
let other_hot = U256::from(2_345);
952+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
953+
&other_hot,
954+
&other_cold,
955+
netuid,
956+
AlphaCurrency::from(30u64), // not nearly enough to cover the lock
957+
);
958+
959+
// ensure there was some Alpha issued
960+
assert!(SubtensorModule::get_alpha_issuance(netuid).to_u64() > 0);
961+
957962
// Compute expected refund using the same math as the pallet
958963
let frac: U96F32 = SubtensorModule::get_float_subnet_owner_cut();
959-
let total_emitted_alpha: u64 = 1_500 + 3_000; // 4_500 α
964+
let total_emitted_alpha: u64 = SubtensorModule::get_alpha_issuance(netuid).to_u64();
960965
let owner_alpha_u64: u64 = U96F32::from_num(total_emitted_alpha)
961966
.saturating_mul(frac)
962967
.floor()
963968
.saturating_to_num::<u64>();
964969

965-
// Prefer sim_swap; fall back to current price if unavailable.
966-
let order = GetTaoForAlpha::<Test>::with_amount(owner_alpha_u64);
967-
let owner_emission_tao_u64 =
968-
<Test as pallet::Config>::SwapInterface::sim_swap(netuid.into(), order)
969-
.map(|res| res.amount_paid_out.to_u64())
970-
.unwrap_or_else(|_| {
971-
let price: U96F32 =
972-
<Test as pallet::Config>::SwapInterface::current_alpha_price(netuid.into());
973-
U96F32::from_num(owner_alpha_u64)
974-
.saturating_mul(price)
975-
.floor()
976-
.saturating_to_num::<u64>()
977-
});
970+
let owner_emission_tao_u64 = {
971+
let price: U96F32 =
972+
<Test as pallet::Config>::SwapInterface::current_alpha_price(netuid.into());
973+
U96F32::from_num(owner_alpha_u64)
974+
.saturating_mul(price)
975+
.floor()
976+
.saturating_to_num::<u64>()
977+
};
978978

979979
let expected_refund: u64 = lock_u64.saturating_sub(owner_emission_tao_u64);
980980

@@ -1011,7 +1011,17 @@ fn destroy_alpha_out_refund_gating_by_registration_block() {
10111011
// Lock and emissions present (should be ignored for refund)
10121012
let lock_u64: u64 = 42_000;
10131013
SubtensorModule::set_subnet_locked_balance(netuid, TaoCurrency::from(lock_u64));
1014-
Emission::<Test>::insert(netuid, vec![AlphaCurrency::from(5_000u64)]);
1014+
// give some stake to other key
1015+
let other_cold = U256::from(1_234);
1016+
let other_hot = U256::from(2_345);
1017+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
1018+
&other_hot,
1019+
&other_cold,
1020+
netuid,
1021+
AlphaCurrency::from(300u64), // not nearly enough to cover the lock
1022+
);
1023+
// ensure there was some Alpha issued
1024+
assert!(SubtensorModule::get_alpha_issuance(netuid).to_u64() > 0);
10151025
SubnetOwnerCut::<Test>::put(32_768u16); // ~50%
10161026

10171027
// Balances before
@@ -1046,7 +1056,9 @@ fn destroy_alpha_out_refund_gating_by_registration_block() {
10461056

10471057
// lock = 0; emissions present (must not matter)
10481058
SubtensorModule::set_subnet_locked_balance(netuid, TaoCurrency::from(0u64));
1049-
Emission::<Test>::insert(netuid, vec![AlphaCurrency::from(10_000u64)]);
1059+
SubnetAlphaOut::<Test>::insert(netuid, AlphaCurrency::from(10_000));
1060+
// ensure there was some Alpha issued
1061+
assert!(SubtensorModule::get_alpha_issuance(netuid).to_u64() > 0);
10501062
SubnetOwnerCut::<Test>::put(32_768u16); // ~50%
10511063

10521064
let owner_before = SubtensorModule::get_coldkey_balance(&owner_cold);

0 commit comments

Comments
 (0)