Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,23 @@ impl<T: Config> Pallet<T> {
log::debug!("Subnets to emit to: {subnets_to_emit_to:?}");

// --- 2. Get sum of tao reserves ( in a later version we will switch to prices. )
let mut acc_total_moving_prices = U96F32::saturating_from_num(0.0);
// Only get price EMA for subnets that we emit to.
for netuid_i in subnets_to_emit_to.iter() {
// Get and update the moving price of each subnet adding the total together.
acc_total_moving_prices =
acc_total_moving_prices.saturating_add(Self::get_moving_alpha_price(*netuid_i));
}
let total_moving_prices = acc_total_moving_prices;
let total_moving_prices = subnets_to_emit_to
.iter()
.map(|netuid| Self::get_moving_alpha_price(*netuid))
.fold(U96F32::saturating_from_num(0.0), |acc, ema| {
acc.saturating_add(ema)
});
log::debug!("total_moving_prices: {total_moving_prices:?}");

let subsidy_mode = total_moving_prices < U96F32::saturating_from_num(1.0);
log::debug!("subsidy_mode: {subsidy_mode:?}");

// --- 3. Get subnet terms (tao_in, alpha_in, and alpha_out)
// Computation is described in detail in the dtao whitepaper.
let mut tao_in: BTreeMap<NetUid, U96F32> = BTreeMap::new();
let mut alpha_in: BTreeMap<NetUid, U96F32> = BTreeMap::new();
let mut alpha_out: BTreeMap<NetUid, U96F32> = BTreeMap::new();
let mut is_subsidized: BTreeMap<NetUid, bool> = BTreeMap::new();
// Only calculate for subnets that we are emitting to.
for netuid_i in subnets_to_emit_to.iter() {
// Get subnet price.
Expand All @@ -79,11 +80,7 @@ impl<T: Config> Pallet<T> {
// Get initial alpha_in
let mut alpha_in_i: U96F32;
let mut tao_in_i: U96F32;
let tao_in_ratio: U96F32 = default_tao_in_i.safe_div_or(
U96F32::saturating_from_num(block_emission),
U96F32::saturating_from_num(0.0),
);
if price_i < tao_in_ratio {
if subsidy_mode {
tao_in_i = price_i.saturating_mul(U96F32::saturating_from_num(block_emission));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tao_in_i should only be changed for subnets where ALPHA price < TAO emissions (EMA / sum of EMAs), otherwise it would inject more TAO, and would result in more than 1 TAO/block being emitted. The only reason to change the tao_in_i according to the ALPHA price * Block Emission is so that one can calculate how much TAO should be used to swap for ALPHA (to bring the ALPHA price back up).

Here a scenario:

SoS is 0.8 and there is a subnet A that is overpriced, so ALPHA price is 0.5 while TAO emissions are 0.25, the chain would inject 0.5 TAO into the subnet because it looks at ALPHA price * Block Emission, instead of the 0.25 TAO that he should get based on TAO emissions. The difference_tao_in would clamp to 0 because it would be otherwise negative (tao_in > default_tao_in). Then subnet B has 0.75 TAO emissions but an ALPHA price of 0.3, so the chain would use 0.3 TAO to inject, and 0.45 TAO to swap for ALPHA to push ALPHA price towards TAO emissions (still using 0.75 TAO in total).

Thus subnet A gets 0.5 TAO and subnet B gets 0.75 TAO, so the chain emits 1.25 TAO/block

If one were to use ALPHA prices only as commented at line 38 I think this wouldn't happen, because then there is no discrepancy between ALPHA price and EMA price, as only ALPHA price is used.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason to downscale TAO is if ALPHA needs to be downscaled, so that the injections can maintain the price, while the difference_tao_in is used to swap for ALPHA (increasing the ALPHA price towards TAO emissions), which it doesn't when ALPHA price > TAO emissions

alpha_in_i = block_emission;
let difference_tao: U96F32 = default_tao_in_i.saturating_sub(tao_in_i);
Expand All @@ -100,11 +97,9 @@ impl<T: Config> Pallet<T> {
*total = total.saturating_sub(bought_alpha);
});
}
is_subsidized.insert(*netuid_i, true);
} else {
tao_in_i = default_tao_in_i;
alpha_in_i = tao_in_i.safe_div_or(price_i, alpha_emission_i);
is_subsidized.insert(*netuid_i, false);
}
log::debug!("alpha_in_i: {alpha_in_i:?}");

Expand Down Expand Up @@ -215,8 +210,7 @@ impl<T: Config> Pallet<T> {
let pending_alpha: U96F32 = alpha_out_i.saturating_sub(root_alpha);
log::debug!("pending_alpha: {pending_alpha:?}");
// Sell root emission through the pool (do not pay fees)
let subsidized: bool = *is_subsidized.get(netuid_i).unwrap_or(&false);
if !subsidized {
if !subsidy_mode {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could technically stay so that the root proportion on "overpriced" subnets (ALPHA price > TAO emissions) is not countering the SoS to increase at/above 1

let swap_result = Self::swap_alpha_for_tao(
*netuid_i,
tou64!(root_alpha).into(),
Expand Down
83 changes: 79 additions & 4 deletions pallets/subtensor/src/tests/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,81 @@ fn test_coinbase_tao_issuance_different_prices() {
epsilon = 1.into(),
);

// EMA Prices are high => full emission
let tao_issued = TaoCurrency::from(emission);
assert_abs_diff_eq!(
TotalIssuance::<Test>::get(),
tao_issued,
epsilon = 10.into()
);
assert_abs_diff_eq!(
TotalStake::<Test>::get(),
emission.into(),
epsilon = 10.into()
);
});
}

// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_coinbase_subsidies --exact --show-output --nocapture
#[test]
fn test_coinbase_subsidies() {
new_test_ext(1).execute_with(|| {
let netuid1 = NetUid::from(1);
let netuid2 = NetUid::from(2);
let emission = 100_000_000;
add_network(netuid1, 1, 0);
add_network(netuid2, 1, 0);

// Setup prices 0.1 and 0.2
let initial_tao: u64 = 100_000_u64;
let initial_alpha1: u64 = initial_tao * 10;
let initial_alpha2: u64 = initial_tao * 5;
mock::setup_reserves(netuid1, initial_tao.into(), initial_alpha1.into());
mock::setup_reserves(netuid2, initial_tao.into(), initial_alpha2.into());

// Force the swap to initialize
SubtensorModule::swap_tao_for_alpha(
netuid1,
TaoCurrency::ZERO,
1_000_000_000_000.into(),
false,
)
.unwrap();
SubtensorModule::swap_tao_for_alpha(
netuid2,
TaoCurrency::ZERO,
1_000_000_000_000.into(),
false,
)
.unwrap();

// Make subnets dynamic.
SubnetMechanism::<Test>::insert(netuid1, 1);
SubnetMechanism::<Test>::insert(netuid2, 1);

// Set subnet EMA prices (sum < 1)
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(0.1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(0.2));

// Assert initial TAO reserves.
assert_eq!(SubnetTAO::<Test>::get(netuid1), initial_tao.into());
assert_eq!(SubnetTAO::<Test>::get(netuid2), initial_tao.into());

// Run the coinbase with the emission amount.
SubtensorModule::run_coinbase(U96F32::from_num(emission));

// Assert tao emission is split evenly.
assert_abs_diff_eq!(
SubnetTAO::<Test>::get(netuid1),
TaoCurrency::from(initial_tao + emission / 3),
epsilon = 1.into(),
);
assert_abs_diff_eq!(
SubnetTAO::<Test>::get(netuid2),
TaoCurrency::from(initial_tao + 2 * emission / 3),
epsilon = 1.into(),
);

// Prices are low => we limit tao issued (buy alpha with it)
let tao_issued = TaoCurrency::from(((0.1 + 0.2) * emission as f64) as u64);
assert_abs_diff_eq!(
Expand Down Expand Up @@ -430,8 +505,8 @@ fn test_coinbase_alpha_issuance_with_cap_trigger() {
SubnetTAO::<Test>::insert(netuid2, TaoCurrency::from(initial));
SubnetAlphaIn::<Test>::insert(netuid2, AlphaCurrency::from(initial_alpha)); // Make price extremely low.
// Set subnet prices.
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(0.1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(0.2));
// Run coinbase
SubtensorModule::run_coinbase(U96F32::from_num(emission));
// tao_in = 333_333
Expand Down Expand Up @@ -468,8 +543,8 @@ fn test_coinbase_alpha_issuance_with_cap_trigger_and_block_emission() {
// Enable emission
FirstEmissionBlockNumber::<Test>::insert(netuid1, 0);
FirstEmissionBlockNumber::<Test>::insert(netuid2, 0);
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(2));
SubnetMovingPrice::<Test>::insert(netuid1, I96F32::from_num(0.1));
SubnetMovingPrice::<Test>::insert(netuid2, I96F32::from_num(0.2));

// Force the swap to initialize
SubtensorModule::swap_tao_for_alpha(
Expand Down
Loading