Skip to content

Commit 4a01966

Browse files
authored
Merge pull request #2142 from opentensor/chore/refactor-subnet-emissions
Refactor subnet emissions
2 parents 249fa62 + 9211c19 commit 4a01966

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

pallets/subtensor/src/coinbase/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pub mod block_step;
44
pub mod reveal_commits;
55
pub mod root;
66
pub mod run_coinbase;
7+
pub mod subnet_emissions;

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,10 @@ impl<T: Config> Pallet<T> {
3030
.filter(|netuid| *netuid != NetUid::ROOT)
3131
.collect();
3232
log::debug!("All subnet netuids: {subnets:?}");
33-
// Filter out subnets with no first emission block number.
34-
let subnets_to_emit_to: Vec<NetUid> = subnets
35-
.clone()
36-
.into_iter()
37-
.filter(|netuid| FirstEmissionBlockNumber::<T>::get(*netuid).is_some())
38-
.collect();
39-
log::debug!("Subnets to emit to: {subnets_to_emit_to:?}");
4033

41-
// --- 2. Get sum of tao reserves ( in a later version we will switch to prices. )
42-
let mut acc_total_moving_prices = U96F32::saturating_from_num(0.0);
43-
// Only get price EMA for subnets that we emit to.
44-
for netuid_i in subnets_to_emit_to.iter() {
45-
// Get and update the moving price of each subnet adding the total together.
46-
acc_total_moving_prices =
47-
acc_total_moving_prices.saturating_add(Self::get_moving_alpha_price(*netuid_i));
48-
}
49-
let total_moving_prices = acc_total_moving_prices;
50-
log::debug!("total_moving_prices: {total_moving_prices:?}");
34+
// 2. Get subnets to emit to and emissions
35+
let subnet_emissions = Self::get_subnet_block_emissions(&subnets, block_emission);
36+
let subnets_to_emit_to: Vec<NetUid> = subnet_emissions.keys().copied().collect();
5137

5238
// --- 3. Get subnet terms (tao_in, alpha_in, and alpha_out)
5339
// Computation is described in detail in the dtao whitepaper.
@@ -60,14 +46,11 @@ impl<T: Config> Pallet<T> {
6046
// Get subnet price.
6147
let price_i = T::SwapInterface::current_alpha_price((*netuid_i).into());
6248
log::debug!("price_i: {price_i:?}");
63-
// Get subnet TAO.
64-
let moving_price_i: U96F32 = Self::get_moving_alpha_price(*netuid_i);
65-
log::debug!("moving_price_i: {moving_price_i:?}");
6649
// Emission is price over total.
67-
let default_tao_in_i: U96F32 = block_emission
68-
.saturating_mul(moving_price_i)
69-
.checked_div(total_moving_prices)
70-
.unwrap_or(asfloat!(0.0));
50+
let default_tao_in_i: U96F32 = subnet_emissions
51+
.get(netuid_i)
52+
.copied()
53+
.unwrap_or(asfloat!(0));
7154
log::debug!("default_tao_in_i: {default_tao_in_i:?}");
7255
// Get alpha_emission total
7356
let alpha_emission_i: U96F32 = asfloat!(
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use super::*;
2+
use crate::alloc::borrow::ToOwned;
3+
use alloc::collections::BTreeMap;
4+
use substrate_fixed::types::U96F32;
5+
6+
impl<T: Config> Pallet<T> {
7+
pub fn get_subnet_block_emissions(
8+
subnets: &[NetUid],
9+
block_emission: U96F32,
10+
) -> BTreeMap<NetUid, U96F32> {
11+
// Filter out subnets with no first emission block number.
12+
let subnets_to_emit_to: Vec<NetUid> = subnets
13+
.to_owned()
14+
.clone()
15+
.into_iter()
16+
.filter(|netuid| FirstEmissionBlockNumber::<T>::get(*netuid).is_some())
17+
.collect();
18+
log::debug!("Subnets to emit to: {subnets_to_emit_to:?}");
19+
20+
// Get sum of alpha moving prices
21+
let total_moving_prices = subnets_to_emit_to
22+
.iter()
23+
.map(|netuid| Self::get_moving_alpha_price(*netuid))
24+
.fold(U96F32::saturating_from_num(0.0), |acc, ema| {
25+
acc.saturating_add(ema)
26+
});
27+
log::debug!("total_moving_prices: {total_moving_prices:?}");
28+
29+
// Get subnet TAO emissions.
30+
subnets_to_emit_to
31+
.into_iter()
32+
.map(|netuid| {
33+
let moving_price = Self::get_moving_alpha_price(netuid);
34+
log::debug!("moving_price_i: {moving_price:?}");
35+
36+
let share = block_emission
37+
.saturating_mul(moving_price)
38+
.checked_div(total_moving_prices)
39+
.unwrap_or(U96F32::from_num(0));
40+
41+
(netuid, share)
42+
})
43+
.collect::<BTreeMap<NetUid, U96F32>>()
44+
}
45+
}

0 commit comments

Comments
 (0)