Skip to content

Commit d3211b7

Browse files
authored
Merge pull request #2171 from opentensor/feat/flow-emissions
Init flows instead of transition, set temperature = 1.0
2 parents c5e0161 + 9f84b02 commit d3211b7

File tree

7 files changed

+19
-245
lines changed

7 files changed

+19
-245
lines changed

pallets/subtensor/src/coinbase/subnet_emissions.rs

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

89
impl<T: Config> Pallet<T> {
910
pub fn get_subnet_block_emissions(
@@ -73,8 +74,11 @@ impl<T: Config> Pallet<T> {
7374
last_block_ema
7475
}
7576
} else {
76-
// Initialize EMA flow, set S(current_block) = 0
77-
let ema_flow = I64F64::saturating_from_num(0);
77+
// Initialize EMA flow, set S(current_block) = min(price, ema_price) * init_factor
78+
let moving_price = I64F64::saturating_from_num(Self::get_moving_alpha_price(netuid));
79+
let current_price =
80+
I64F64::saturating_from_num(T::SwapInterface::current_alpha_price(netuid));
81+
let ema_flow = moving_price.min(current_price);
7882
SubnetEmaTaoFlow::<T>::insert(netuid, (current_block, ema_flow));
7983
ema_flow
8084
}
@@ -206,88 +210,8 @@ impl<T: Config> Pallet<T> {
206210
offset_flows
207211
}
208212

209-
// DEPRECATED: Implementation of shares that uses EMA prices will be gradually deprecated
210-
fn get_shares_price_ema(subnets_to_emit_to: &[NetUid]) -> BTreeMap<NetUid, U64F64> {
211-
// Get sum of alpha moving prices
212-
let total_moving_prices = subnets_to_emit_to
213-
.iter()
214-
.map(|netuid| U64F64::saturating_from_num(Self::get_moving_alpha_price(*netuid)))
215-
.fold(U64F64::saturating_from_num(0.0), |acc, ema| {
216-
acc.saturating_add(ema)
217-
});
218-
log::debug!("total_moving_prices: {total_moving_prices:?}");
219-
220-
// Calculate shares.
221-
subnets_to_emit_to
222-
.iter()
223-
.map(|netuid| {
224-
let moving_price =
225-
U64F64::saturating_from_num(Self::get_moving_alpha_price(*netuid));
226-
log::debug!("moving_price_i: {moving_price:?}");
227-
228-
let share = moving_price
229-
.checked_div(total_moving_prices)
230-
.unwrap_or(U64F64::saturating_from_num(0));
231-
232-
(*netuid, share)
233-
})
234-
.collect::<BTreeMap<NetUid, U64F64>>()
235-
}
236-
237213
// Combines ema price method and tao flow method linearly over FlowHalfLife blocks
238214
pub(crate) fn get_shares(subnets_to_emit_to: &[NetUid]) -> BTreeMap<NetUid, U64F64> {
239-
let current_block: u64 = Self::get_current_block_as_u64();
240-
241-
// Weight of tao flow method
242-
let period = FlowHalfLife::<T>::get();
243-
let one = U64F64::saturating_from_num(1);
244-
let zero = U64F64::saturating_from_num(0);
245-
let tao_flow_weight = if let Some(start_block) = FlowFirstBlock::<T>::get() {
246-
if (current_block > start_block) && (current_block < start_block.saturating_add(period))
247-
{
248-
// Combination period in progress
249-
let start_fixed = U64F64::saturating_from_num(start_block);
250-
let current_fixed = U64F64::saturating_from_num(current_block);
251-
let period_fixed = U64F64::saturating_from_num(period);
252-
current_fixed
253-
.saturating_sub(start_fixed)
254-
.safe_div(period_fixed)
255-
} else if current_block >= start_block.saturating_add(period) {
256-
// Over combination period
257-
one
258-
} else {
259-
// Not yet in combination period
260-
zero
261-
}
262-
} else {
263-
zero
264-
};
265-
266-
// Get shares for each method as needed
267-
let shares_flow = if tao_flow_weight > zero {
268-
Self::get_shares_flow(subnets_to_emit_to)
269-
} else {
270-
BTreeMap::new()
271-
};
272-
273-
let shares_prices = if tao_flow_weight < one {
274-
Self::get_shares_price_ema(subnets_to_emit_to)
275-
} else {
276-
BTreeMap::new()
277-
};
278-
279-
// Combine
280-
let mut shares_combined = BTreeMap::new();
281-
for netuid in subnets_to_emit_to.iter() {
282-
let share_flow = shares_flow.get(netuid).unwrap_or(&zero);
283-
let share_prices = shares_prices.get(netuid).unwrap_or(&zero);
284-
shares_combined.insert(
285-
*netuid,
286-
share_flow.saturating_mul(tao_flow_weight).saturating_add(
287-
share_prices.saturating_mul(one.saturating_sub(tao_flow_weight)),
288-
),
289-
);
290-
}
291-
shares_combined
215+
Self::get_shares_flow(subnets_to_emit_to)
292216
}
293217
}

pallets/subtensor/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ pub mod pallet {
8888
use frame_system::pallet_prelude::*;
8989
use pallet_drand::types::RoundNumber;
9090
use runtime_common::prod_or_fast;
91-
use safe_math::FixedExt;
9291
use sp_core::{ConstU32, H160, H256};
9392
use sp_runtime::traits::{Dispatchable, TrailingZeroInput};
9493
use sp_std::collections::btree_map::BTreeMap;
@@ -1292,7 +1291,7 @@ pub mod pallet {
12921291
#[pallet::type_value]
12931292
/// Default value for flow normalization exponent.
12941293
pub fn DefaultFlowNormExponent<T: Config>() -> U64F64 {
1295-
U64F64::saturating_from_num(15).safe_div(U64F64::saturating_from_num(10))
1294+
U64F64::saturating_from_num(1)
12961295
}
12971296
#[pallet::storage]
12981297
/// --- ITEM --> Flow Normalization Exponent (p)
@@ -1302,7 +1301,7 @@ pub mod pallet {
13021301
/// Default value for flow EMA smoothing.
13031302
pub fn DefaultFlowEmaSmoothingFactor<T: Config>() -> u64 {
13041303
// Example values:
1305-
// half-life factor value i64 normalized
1304+
// half-life factor value i64 normalized (x 2^63)
13061305
// 216000 (1 month) --> 0.000003209009576 ( 29_597_889_189_277)
13071306
// 50400 (1 week) --> 0.000013752825678 (126_847_427_788_335)
13081307
29_597_889_189_277
@@ -1316,9 +1315,6 @@ pub mod pallet {
13161315
/// --- ITEM --> Flow EMA smoothing factor (flow alpha), u64 normalized
13171316
pub type FlowEmaSmoothingFactor<T: Config> =
13181317
StorageValue<_, u64, ValueQuery, DefaultFlowEmaSmoothingFactor<T>>;
1319-
#[pallet::storage]
1320-
/// --- ITEM --> Block when TAO flow calculation starts(ed)
1321-
pub type FlowFirstBlock<T: Config> = StorageValue<_, u64, OptionQuery>;
13221318

13231319
/// ============================
13241320
/// ==== Global Parameters =====

pallets/subtensor/src/macros/hooks.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ mod hooks {
158158
.saturating_add(migrations::migrate_auto_stake_destination::migrate_auto_stake_destination::<T>())
159159
// Migrate Kappa to default (0.5)
160160
.saturating_add(migrations::migrate_kappa_map_to_default::migrate_kappa_map_to_default::<T>())
161-
// Set the first block of tao flow
162-
.saturating_add(migrations::migrate_set_first_tao_flow_block::migrate_set_first_tao_flow_block::<T>())
163161
// Remove obsolete map entries
164162
.saturating_add(migrations::migrate_remove_tao_dividends::migrate_remove_tao_dividends::<T>());
165163
weight

pallets/subtensor/src/migrations/migrate_set_first_tao_flow_block.rs

Lines changed: 0 additions & 38 deletions
This file was deleted.

pallets/subtensor/src/migrations/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ pub mod migrate_remove_zero_total_hotkey_alpha;
4040
pub mod migrate_reset_bonds_moving_average;
4141
pub mod migrate_reset_max_burn;
4242
pub mod migrate_set_first_emission_block_number;
43-
pub mod migrate_set_first_tao_flow_block;
4443
pub mod migrate_set_min_burn;
4544
pub mod migrate_set_min_difficulty;
4645
pub mod migrate_set_nominator_min_stake;

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ fn test_coinbase_tao_issuance_base_low_flow() {
127127
// 100% tao flow method
128128
let block_num = FlowHalfLife::<Test>::get();
129129
SubnetEmaTaoFlow::<Test>::insert(netuid, (block_num, I64F64::from_num(1_000_000_000)));
130-
FlowFirstBlock::<Test>::set(Some(0_u64));
131130
System::set_block_number(block_num);
132131

133132
let tao_in_before = SubnetTAO::<Test>::get(netuid);
@@ -237,12 +236,12 @@ fn test_coinbase_tao_issuance_different_prices() {
237236
assert_abs_diff_eq!(
238237
SubnetTAO::<Test>::get(netuid1),
239238
TaoCurrency::from(initial_tao + emission / 3),
240-
epsilon = 1.into(),
239+
epsilon = 10.into(),
241240
);
242241
assert_abs_diff_eq!(
243242
SubnetTAO::<Test>::get(netuid2),
244243
TaoCurrency::from(initial_tao + 2 * emission / 3),
245-
epsilon = 1.into(),
244+
epsilon = 10.into(),
246245
);
247246

248247
// Prices are low => we limit tao issued (buy alpha with it)
@@ -298,11 +297,9 @@ fn test_coinbase_tao_issuance_different_flows() {
298297
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(1));
299298

300299
// Set subnet tao flow ema.
301-
// 100% tao flow method
302300
let block_num = FlowHalfLife::<Test>::get();
303301
SubnetEmaTaoFlow::<Test>::insert(netuid1, (block_num, I64F64::from_num(1)));
304302
SubnetEmaTaoFlow::<Test>::insert(netuid2, (block_num, I64F64::from_num(2)));
305-
FlowFirstBlock::<Test>::set(Some(0_u64));
306303
System::set_block_number(block_num);
307304

308305
// Set normalization exponent to 1 for simplicity
@@ -490,9 +487,9 @@ fn test_coinbase_alpha_issuance_base() {
490487
});
491488
}
492489

493-
// Test alpha issuance with different subnet prices.
490+
// Test alpha issuance with different subnet flows.
494491
// This test verifies that:
495-
// - Alpha issuance is proportional to subnet prices
492+
// - Alpha issuance is proportional to subnet flows
496493
// - Higher priced subnets receive more TAO emission
497494
// - Alpha issuance is correctly calculated based on price ratios
498495
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_alpha_issuance_different --exact --show-output --nocapture
@@ -507,18 +504,16 @@ fn test_coinbase_alpha_issuance_different() {
507504
// Make subnets dynamic.
508505
SubnetMechanism::<Test>::insert(netuid1, 1);
509506
SubnetMechanism::<Test>::insert(netuid2, 1);
510-
// Setup prices 1 and 1
507+
// Setup prices 1 and 2
511508
let initial: u64 = 1_000_000;
512509
SubnetTAO::<Test>::insert(netuid1, TaoCurrency::from(initial));
513510
SubnetAlphaIn::<Test>::insert(netuid1, AlphaCurrency::from(initial));
514-
SubnetTAO::<Test>::insert(netuid2, TaoCurrency::from(initial));
511+
SubnetTAO::<Test>::insert(netuid2, TaoCurrency::from(2 * initial));
515512
SubnetAlphaIn::<Test>::insert(netuid2, AlphaCurrency::from(initial));
516-
// Set subnet prices.
513+
// Set subnet ema prices to 1 and 2
517514
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
518515
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
519-
// Set tao flow
520-
SubnetEmaTaoFlow::<Test>::insert(netuid1, (1u64, I64F64::from_num(1)));
521-
SubnetEmaTaoFlow::<Test>::insert(netuid2, (1u64, I64F64::from_num(2)));
516+
// Do NOT Set tao flow, let it initialize
522517
// Run coinbase
523518
SubtensorModule::run_coinbase(U96F32::from_num(emission));
524519
// tao_in = 333_333
@@ -528,10 +523,10 @@ fn test_coinbase_alpha_issuance_different() {
528523
(initial + emission / 3).into()
529524
);
530525
// tao_in = 666_666
531-
// alpha_in = 666_666/price = 666_666 + initial
526+
// alpha_in = 666_666/price = 333_333 + initial
532527
assert_eq!(
533528
SubnetAlphaIn::<Test>::get(netuid2),
534-
(initial + emission / 3 + emission / 3).into()
529+
(initial + (emission * 2 / 3) / 2).into()
535530
);
536531
});
537532
}

0 commit comments

Comments
 (0)