Skip to content

Commit 3dd0e0f

Browse files
authored
Merge pull request #2225 from opentensor/fix/clean-merge
Fix/clean merge
2 parents 11dbff3 + b4bfe39 commit 3dd0e0f

File tree

5 files changed

+50
-41
lines changed

5 files changed

+50
-41
lines changed

pallets/subtensor/src/coinbase/subnet_emissions.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use alloc::collections::BTreeMap;
33
use safe_math::FixedExt;
44
use substrate_fixed::transcendental::{exp, ln};
55
use substrate_fixed::types::{I32F32, I64F64, U64F64, U96F32};
6-
use subtensor_swap_interface::SwapHandler;
76

87
impl<T: Config> Pallet<T> {
98
pub fn get_subnets_to_emit_to(subnets: &[NetUid]) -> Vec<NetUid> {
@@ -64,32 +63,24 @@ impl<T: Config> Pallet<T> {
6463

6564
// Calculate net ema flow for the next block
6665
let block_flow = I64F64::saturating_from_num(SubnetTaoFlow::<T>::get(netuid));
67-
if let Some((last_block, last_block_ema)) = SubnetEmaTaoFlow::<T>::get(netuid) {
68-
// EMA flow already initialized
69-
if last_block != current_block {
70-
let flow_alpha = I64F64::saturating_from_num(FlowEmaSmoothingFactor::<T>::get())
71-
.safe_div(I64F64::saturating_from_num(i64::MAX));
72-
let one = I64F64::saturating_from_num(1);
73-
let ema_flow = (one.saturating_sub(flow_alpha))
74-
.saturating_mul(last_block_ema)
75-
.saturating_add(flow_alpha.saturating_mul(block_flow));
76-
SubnetEmaTaoFlow::<T>::insert(netuid, (current_block, ema_flow));
77-
78-
// Drop the accumulated flow in the last block
79-
Self::reset_tao_outflow(netuid);
80-
ema_flow
81-
} else {
82-
last_block_ema
83-
}
84-
} else {
85-
// Initialize EMA flow, set S(current_block) = min(price, ema_price) * init_factor
86-
let init_factor = I64F64::saturating_from_num(1_000_000_000);
87-
let moving_price = I64F64::saturating_from_num(Self::get_moving_alpha_price(netuid));
88-
let current_price =
89-
I64F64::saturating_from_num(T::SwapInterface::current_alpha_price(netuid));
90-
let ema_flow = init_factor.saturating_mul(moving_price.min(current_price));
66+
let (last_block, last_block_ema) =
67+
SubnetEmaTaoFlow::<T>::get(netuid).unwrap_or((0, I64F64::saturating_from_num(0)));
68+
69+
// EMA flow already initialized
70+
if last_block != current_block {
71+
let flow_alpha = I64F64::saturating_from_num(FlowEmaSmoothingFactor::<T>::get())
72+
.safe_div(I64F64::saturating_from_num(i64::MAX));
73+
let one = I64F64::saturating_from_num(1);
74+
let ema_flow = (one.saturating_sub(flow_alpha))
75+
.saturating_mul(last_block_ema)
76+
.saturating_add(flow_alpha.saturating_mul(block_flow));
9177
SubnetEmaTaoFlow::<T>::insert(netuid, (current_block, ema_flow));
78+
79+
// Drop the accumulated flow in the last block
80+
Self::reset_tao_outflow(netuid);
9281
ema_flow
82+
} else {
83+
last_block_ema
9384
}
9485
}
9586

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,8 @@ impl<T: Config> Pallet<T> {
707707
let protocol_tao_after = Self::get_protocol_tao(netuid);
708708
// This should decrease as we are removing TAO from the protocol.
709709
let protocol_tao_delta: TaoCurrency = protocol_tao.saturating_sub(protocol_tao_after);
710+
// Use max to overstate the TAO flow from the protocol.
711+
let tao_flow = protocol_tao_delta.max(swap_result.amount_paid_out.into());
710712

711713
// Refund the unused alpha (in case if limit price is hit)
712714
let refund = actual_alpha_decrease.saturating_sub(
@@ -734,7 +736,7 @@ impl<T: Config> Pallet<T> {
734736
// }
735737

736738
// Record TAO outflow
737-
Self::record_tao_outflow(netuid, protocol_tao_delta);
739+
Self::record_tao_outflow(netuid, tao_flow);
738740

739741
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());
740742

@@ -784,6 +786,8 @@ impl<T: Config> Pallet<T> {
784786

785787
// This should increase as we are adding TAO to the protocol.
786788
let protocol_tao_delta: TaoCurrency = protocol_tao_after.saturating_sub(protocol_tao);
789+
// Use min to understate the TAO flow into the protocol.
790+
let tao_flow = protocol_tao_delta.min(tao);
787791

788792
ensure!(
789793
!swap_result.amount_paid_out.is_zero(),
@@ -820,7 +824,7 @@ impl<T: Config> Pallet<T> {
820824
}
821825

822826
// Record TAO inflow
823-
Self::record_tao_inflow(netuid, protocol_tao_delta);
827+
Self::record_tao_inflow(netuid, tao_flow);
824828

825829
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());
826830

pallets/subtensor/src/tests/claim_root.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
DefaultMinRootClaimAmount, Error, MAX_NUM_ROOT_CLAIMS, MAX_ROOT_CLAIM_THRESHOLD, NetworksAdded,
88
NumRootClaim, NumStakingColdkeys, PendingRootAlphaDivs, RootClaimable, RootClaimableThreshold,
99
StakingColdkeys, StakingColdkeysByIndex, SubnetAlphaIn, SubnetMechanism, SubnetMovingPrice,
10-
SubnetTAO, SubtokenEnabled, Tempo, pallet,
10+
SubnetTAO, SubnetTaoFlow, SubtokenEnabled, Tempo, pallet,
1111
};
1212
use crate::{RootClaimType, RootClaimTypeEnum, RootClaimed};
1313
use approx::assert_abs_diff_eq;
@@ -1037,6 +1037,9 @@ fn test_claim_root_coinbase_distribution() {
10371037
let root_sell_flag = SubtensorModule::get_network_root_sell_flag(&[netuid]);
10381038
assert!(root_sell_flag, "Root sell flag should be true");
10391039

1040+
// Set TAOFlow > 0
1041+
SubnetTaoFlow::<Test>::insert(netuid, 2222_i64);
1042+
10401043
// Check total issuance (saved to pending alpha divs)
10411044
run_to_block(2);
10421045

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ fn test_coinbase_tao_issuance_base() {
9999
let subnet_owner_hk = U256::from(1002);
100100
let netuid = add_dynamic_network(&subnet_owner_hk, &subnet_owner_ck);
101101
let total_issuance_before = TotalIssuance::<Test>::get();
102-
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from(3141) / I96F32::from(1000));
102+
// Set subnet TAO flow to non-zero
103+
SubnetTaoFlow::<Test>::insert(netuid, 1234567_i64);
103104
let tao_in_before = SubnetTAO::<Test>::get(netuid);
104105
let total_stake_before = TotalStake::<Test>::get();
105106
SubtensorModule::run_coinbase(U96F32::from_num(emission));
@@ -120,6 +121,8 @@ fn test_coinbase_tao_issuance_base_low() {
120121
let emission = TaoCurrency::from(1);
121122
add_network(netuid, 1, 0);
122123
assert_eq!(SubnetTAO::<Test>::get(netuid), TaoCurrency::ZERO);
124+
// Set subnet flow to non-zero
125+
SubnetTaoFlow::<Test>::insert(netuid, 33433_i64);
123126
SubtensorModule::run_coinbase(U96F32::from_num(emission));
124127
assert_eq!(SubnetTAO::<Test>::get(netuid), emission);
125128
assert_eq!(TotalIssuance::<Test>::get(), emission);
@@ -171,6 +174,10 @@ fn test_coinbase_tao_issuance_multiple() {
171174
assert_eq!(SubnetTAO::<Test>::get(netuid1), TaoCurrency::ZERO);
172175
assert_eq!(SubnetTAO::<Test>::get(netuid2), TaoCurrency::ZERO);
173176
assert_eq!(SubnetTAO::<Test>::get(netuid3), TaoCurrency::ZERO);
177+
// Set Tao flows to equal and non-zero
178+
SubnetTaoFlow::<Test>::insert(netuid1, 100_000_000_i64);
179+
SubnetTaoFlow::<Test>::insert(netuid2, 100_000_000_i64);
180+
SubnetTaoFlow::<Test>::insert(netuid3, 100_000_000_i64);
174181
SubtensorModule::run_coinbase(U96F32::from_num(emission));
175182
assert_abs_diff_eq!(
176183
SubnetTAO::<Test>::get(netuid1),
@@ -234,9 +241,10 @@ fn test_coinbase_tao_issuance_different_prices() {
234241
SubnetMechanism::<Test>::insert(netuid1, 1);
235242
SubnetMechanism::<Test>::insert(netuid2, 1);
236243

237-
// Set subnet prices.
238-
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
239-
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
244+
// Set subnet flows
245+
// Subnet 2 has twice the flow of subnet 1.
246+
SubnetTaoFlow::<Test>::insert(netuid1, 100_000_000_i64);
247+
SubnetTaoFlow::<Test>::insert(netuid2, 200_000_000_i64);
240248

241249
// Assert initial TAO reserves.
242250
assert_eq!(SubnetTAO::<Test>::get(netuid1), initial_tao.into());
@@ -483,8 +491,9 @@ fn test_coinbase_alpha_issuance_base() {
483491
SubnetAlphaIn::<Test>::insert(netuid1, AlphaCurrency::from(initial));
484492
SubnetTAO::<Test>::insert(netuid2, TaoCurrency::from(initial));
485493
SubnetAlphaIn::<Test>::insert(netuid2, AlphaCurrency::from(initial));
486-
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from(1));
487-
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from(1));
494+
// Equal flow
495+
SubnetTaoFlow::<Test>::insert(netuid1, 100_000_000_i64);
496+
SubnetTaoFlow::<Test>::insert(netuid2, 100_000_000_i64);
488497
// Check initial
489498
SubtensorModule::run_coinbase(U96F32::from_num(emission));
490499
// tao_in = 500_000
@@ -523,9 +532,9 @@ fn test_coinbase_alpha_issuance_different() {
523532
SubnetAlphaIn::<Test>::insert(netuid1, AlphaCurrency::from(initial));
524533
SubnetTAO::<Test>::insert(netuid2, TaoCurrency::from(2 * initial));
525534
SubnetAlphaIn::<Test>::insert(netuid2, AlphaCurrency::from(initial));
526-
// Set subnet ema prices to 1 and 2
527-
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
528-
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
535+
// Set subnet TAO flows to non-zero and 1:2 ratio
536+
SubnetTaoFlow::<Test>::insert(netuid1, 100_000_000_i64);
537+
SubnetTaoFlow::<Test>::insert(netuid2, 200_000_000_i64);
529538
// Do NOT Set tao flow, let it initialize
530539
// Run coinbase
531540
SubtensorModule::run_coinbase(U96F32::from_num(emission));
@@ -602,8 +611,9 @@ fn test_coinbase_alpha_issuance_with_cap_trigger_and_block_emission() {
602611
// Enable emission
603612
FirstEmissionBlockNumber::<Test>::insert(netuid1, 0);
604613
FirstEmissionBlockNumber::<Test>::insert(netuid2, 0);
605-
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
606-
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
614+
// Set subnet TAO flows to non-zero and 1:2 ratio
615+
SubnetTaoFlow::<Test>::insert(netuid1, 100_000_000_i64);
616+
SubnetTaoFlow::<Test>::insert(netuid2, 200_000_000_i64);
607617

608618
// Force the swap to initialize
609619
SubtensorModule::swap_tao_for_alpha(
@@ -2750,8 +2760,9 @@ fn test_coinbase_v3_liquidity_update() {
27502760

27512761
// Enable emissions and run coinbase (which will increase position liquidity)
27522762
let emission: u64 = 1_234_567;
2763+
// Set the TAO flow to non-zero
2764+
SubnetTaoFlow::<Test>::insert(netuid, 8348383_i64);
27532765
FirstEmissionBlockNumber::<Test>::insert(netuid, 0);
2754-
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0.5));
27552766
SubtensorModule::run_coinbase(U96F32::from_num(emission));
27562767

27572768
let position_after = pallet_subtensor_swap::Positions::<Test>::get((

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: 348,
223+
spec_version: 349,
224224
impl_version: 1,
225225
apis: RUNTIME_API_VERSIONS,
226226
transaction_version: 1,

0 commit comments

Comments
 (0)