Skip to content

Commit bac8246

Browse files
authored
Merge pull request #2220 from opentensor/hotfix/vune/ema-flow-init-from-zero
[hotfix] EMA flow init
2 parents 11c6330 + 9e3b8c5 commit bac8246

File tree

4 files changed

+44
-39
lines changed

4 files changed

+44
-39
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> {
@@ -56,32 +55,24 @@ impl<T: Config> Pallet<T> {
5655

5756
// Calculate net ema flow for the next block
5857
let block_flow = I64F64::saturating_from_num(SubnetTaoFlow::<T>::get(netuid));
59-
if let Some((last_block, last_block_ema)) = SubnetEmaTaoFlow::<T>::get(netuid) {
60-
// EMA flow already initialized
61-
if last_block != current_block {
62-
let flow_alpha = I64F64::saturating_from_num(FlowEmaSmoothingFactor::<T>::get())
63-
.safe_div(I64F64::saturating_from_num(i64::MAX));
64-
let one = I64F64::saturating_from_num(1);
65-
let ema_flow = (one.saturating_sub(flow_alpha))
66-
.saturating_mul(last_block_ema)
67-
.saturating_add(flow_alpha.saturating_mul(block_flow));
68-
SubnetEmaTaoFlow::<T>::insert(netuid, (current_block, ema_flow));
69-
70-
// Drop the accumulated flow in the last block
71-
Self::reset_tao_outflow(netuid);
72-
ema_flow
73-
} else {
74-
last_block_ema
75-
}
76-
} else {
77-
// Initialize EMA flow, set S(current_block) = min(price, ema_price) * init_factor
78-
let init_factor = I64F64::saturating_from_num(1_000_000_000);
79-
let moving_price = I64F64::saturating_from_num(Self::get_moving_alpha_price(netuid));
80-
let current_price =
81-
I64F64::saturating_from_num(T::SwapInterface::current_alpha_price(netuid));
82-
let ema_flow = init_factor.saturating_mul(moving_price.min(current_price));
58+
let (last_block, last_block_ema) =
59+
SubnetEmaTaoFlow::<T>::get(netuid).unwrap_or((0, I64F64::saturating_from_num(0)));
60+
61+
// EMA flow already initialized
62+
if last_block != current_block {
63+
let flow_alpha = I64F64::saturating_from_num(FlowEmaSmoothingFactor::<T>::get())
64+
.safe_div(I64F64::saturating_from_num(i64::MAX));
65+
let one = I64F64::saturating_from_num(1);
66+
let ema_flow = (one.saturating_sub(flow_alpha))
67+
.saturating_mul(last_block_ema)
68+
.saturating_add(flow_alpha.saturating_mul(block_flow));
8369
SubnetEmaTaoFlow::<T>::insert(netuid, (current_block, ema_flow));
70+
71+
// Drop the accumulated flow in the last block
72+
Self::reset_tao_outflow(netuid);
8473
ema_flow
74+
} else {
75+
last_block_ema
8576
}
8677
}
8778

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
@@ -90,7 +90,8 @@ fn test_coinbase_tao_issuance_base() {
9090
let subnet_owner_hk = U256::from(1002);
9191
let netuid = add_dynamic_network(&subnet_owner_hk, &subnet_owner_ck);
9292
let total_issuance_before = TotalIssuance::<Test>::get();
93-
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from(3141) / I96F32::from(1000));
93+
// Set subnet TAO flow to non-zero
94+
SubnetTaoFlow::<Test>::insert(netuid, 1234567_i64);
9495
let tao_in_before = SubnetTAO::<Test>::get(netuid);
9596
let total_stake_before = TotalStake::<Test>::get();
9697
SubtensorModule::run_coinbase(U96F32::from_num(emission));
@@ -111,6 +112,8 @@ fn test_coinbase_tao_issuance_base_low() {
111112
let emission = TaoCurrency::from(1);
112113
add_network(netuid, 1, 0);
113114
assert_eq!(SubnetTAO::<Test>::get(netuid), TaoCurrency::ZERO);
115+
// Set subnet flow to non-zero
116+
SubnetTaoFlow::<Test>::insert(netuid, 33433_i64);
114117
SubtensorModule::run_coinbase(U96F32::from_num(emission));
115118
assert_eq!(SubnetTAO::<Test>::get(netuid), emission);
116119
assert_eq!(TotalIssuance::<Test>::get(), emission);
@@ -162,6 +165,10 @@ fn test_coinbase_tao_issuance_multiple() {
162165
assert_eq!(SubnetTAO::<Test>::get(netuid1), TaoCurrency::ZERO);
163166
assert_eq!(SubnetTAO::<Test>::get(netuid2), TaoCurrency::ZERO);
164167
assert_eq!(SubnetTAO::<Test>::get(netuid3), TaoCurrency::ZERO);
168+
// Set Tao flows to equal and non-zero
169+
SubnetTaoFlow::<Test>::insert(netuid1, 100_000_000_i64);
170+
SubnetTaoFlow::<Test>::insert(netuid2, 100_000_000_i64);
171+
SubnetTaoFlow::<Test>::insert(netuid3, 100_000_000_i64);
165172
SubtensorModule::run_coinbase(U96F32::from_num(emission));
166173
assert_abs_diff_eq!(
167174
SubnetTAO::<Test>::get(netuid1),
@@ -225,9 +232,10 @@ fn test_coinbase_tao_issuance_different_prices() {
225232
SubnetMechanism::<Test>::insert(netuid1, 1);
226233
SubnetMechanism::<Test>::insert(netuid2, 1);
227234

228-
// Set subnet prices.
229-
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
230-
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
235+
// Set subnet flows
236+
// Subnet 2 has twice the flow of subnet 1.
237+
SubnetTaoFlow::<Test>::insert(netuid1, 100_000_000_i64);
238+
SubnetTaoFlow::<Test>::insert(netuid2, 200_000_000_i64);
231239

232240
// Assert initial TAO reserves.
233241
assert_eq!(SubnetTAO::<Test>::get(netuid1), initial_tao.into());
@@ -474,8 +482,9 @@ fn test_coinbase_alpha_issuance_base() {
474482
SubnetAlphaIn::<Test>::insert(netuid1, AlphaCurrency::from(initial));
475483
SubnetTAO::<Test>::insert(netuid2, TaoCurrency::from(initial));
476484
SubnetAlphaIn::<Test>::insert(netuid2, AlphaCurrency::from(initial));
477-
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from(1));
478-
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from(1));
485+
// Equal flow
486+
SubnetTaoFlow::<Test>::insert(netuid1, 100_000_000_i64);
487+
SubnetTaoFlow::<Test>::insert(netuid2, 100_000_000_i64);
479488
// Check initial
480489
SubtensorModule::run_coinbase(U96F32::from_num(emission));
481490
// tao_in = 500_000
@@ -514,9 +523,9 @@ fn test_coinbase_alpha_issuance_different() {
514523
SubnetAlphaIn::<Test>::insert(netuid1, AlphaCurrency::from(initial));
515524
SubnetTAO::<Test>::insert(netuid2, TaoCurrency::from(2 * initial));
516525
SubnetAlphaIn::<Test>::insert(netuid2, AlphaCurrency::from(initial));
517-
// Set subnet ema prices to 1 and 2
518-
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
519-
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
526+
// Set subnet TAO flows to non-zero and 1:2 ratio
527+
SubnetTaoFlow::<Test>::insert(netuid1, 100_000_000_i64);
528+
SubnetTaoFlow::<Test>::insert(netuid2, 200_000_000_i64);
520529
// Do NOT Set tao flow, let it initialize
521530
// Run coinbase
522531
SubtensorModule::run_coinbase(U96F32::from_num(emission));
@@ -593,8 +602,9 @@ fn test_coinbase_alpha_issuance_with_cap_trigger_and_block_emission() {
593602
// Enable emission
594603
FirstEmissionBlockNumber::<Test>::insert(netuid1, 0);
595604
FirstEmissionBlockNumber::<Test>::insert(netuid2, 0);
596-
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
597-
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
605+
// Set subnet TAO flows to non-zero and 1:2 ratio
606+
SubnetTaoFlow::<Test>::insert(netuid1, 100_000_000_i64);
607+
SubnetTaoFlow::<Test>::insert(netuid2, 200_000_000_i64);
598608

599609
// Force the swap to initialize
600610
SubtensorModule::swap_tao_for_alpha(
@@ -2741,8 +2751,9 @@ fn test_coinbase_v3_liquidity_update() {
27412751

27422752
// Enable emissions and run coinbase (which will increase position liquidity)
27432753
let emission: u64 = 1_234_567;
2754+
// Set the TAO flow to non-zero
2755+
SubnetTaoFlow::<Test>::insert(netuid, 8348383_i64);
27442756
FirstEmissionBlockNumber::<Test>::insert(netuid, 0);
2745-
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0.5));
27462757
SubtensorModule::run_coinbase(U96F32::from_num(emission));
27472758

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

0 commit comments

Comments
 (0)