Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
unconst committed Jan 7, 2025
1 parent d54e929 commit f96edc1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 45 deletions.
62 changes: 62 additions & 0 deletions pallets/subtensor/src/coinbase/block_emission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,68 @@ use substrate_fixed::{

impl<T: Config> Pallet<T> {

/// Calculates the dynamic TAO emission for a given subnet.
///
/// This function determines the three terms tao_in, alpha_in, alpha_out
/// which are consequetively, 1) the amount of tao injected into the pool
/// 2) the amount of alpha injected into the pool and 3) the amount of alpha
/// left to be distributed towards miners/validators/owners per block.
///
/// # Arguments
/// * `netuid` - The unique identifier of the subnet.
/// * `tao_emission` - The amount of tao to distribute for this subnet.
/// * `alpha_block_emission` - The maximum alpha emission allowed for the block.
///
/// # Returns
/// * `(u64, u64, u64)` - A tuple containing:
/// - `tao_in_emission`: The adjusted TAO emission always lower or equalt to tao_emission
/// - `alpha_in_emission`: The adjusted alpha emission amount to be added into the pool.
/// - `alpha_out_emission`: The remaining alpha emission after adjustments to be distributed to miners/validatods.
///
/// The algorithm ensures that the pool injection of tao_in_emission, alpha_in_emission does not effect the pool price
/// It also ensures that the total amount of alpha_in_emission + alpha_out_emission sum to 2 * alpha_block_emission
/// It also ensure that 1 < alpha_out_emission < 2 * alpha_block_emission and 0 < alpha_in_emission < alpha_block_emission.
pub fn get_dynamic_tao_emission(netuid: u16, tao_emission: u64, alpha_block_emission: u64) -> (u64, u64, u64) {
// Init terms.
let mut tao_in_emission: I96F32 = I96F32::from_num(tao_emission);
let float_alpha_block_emission: I96F32 = I96F32::from_num(alpha_block_emission);

// Get alpha price for subnet.
let alpha_price: I96F32 = Self::get_alpha_price(netuid);
log::debug!("{:?} - alpha_price: {:?}", netuid, alpha_price);

// Get initial alpha_in
let mut alpha_in_emission: I96F32 = I96F32::from_num(tao_emission).checked_div(alpha_price).unwrap_or(float_alpha_block_emission);

// Check if we are emitting too much alpha_in
if alpha_in_emission >= float_alpha_block_emission {
log::debug!("{:?} - alpha_in_emission: {:?} > alpha_block_emission: {:?}", netuid, alpha_in_emission, float_alpha_block_emission);

// Scale down tao_in
tao_in_emission = alpha_price.saturating_mul(float_alpha_block_emission);

// Set to max alpha_block_emission
alpha_in_emission = float_alpha_block_emission;
}

// Avoid rounding errors.
if tao_in_emission < I96F32::from_num(1) || alpha_in_emission < I96F32::from_num(1) {
alpha_in_emission = I96F32::from_num(0);
tao_in_emission = I96F32::from_num(0);
}

// Set Alpha in emission.
let alpha_out_emission = I96F32::from_num(2).saturating_mul(float_alpha_block_emission).saturating_sub(alpha_in_emission);

// Log results.
log::debug!("{:?} - tao_in_emission: {:?}", netuid, tao_in_emission);
log::debug!("{:?} - alpha_in_emission: {:?}", netuid, alpha_in_emission);
log::debug!("{:?} - alpha_out_emission: {:?}", netuid, alpha_out_emission);

// Return result.
(tao_in_emission.to_num::<u64>(), alpha_in_emission.to_num::<u64>(), alpha_out_emission.to_num::<u64>())
}

/// Calculates the block emission based on the total issuance.
///
/// This function computes the block emission by applying a logarithmic function
Expand Down
45 changes: 0 additions & 45 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,6 @@ pub struct WeightsTlockPayload {
impl<T: Config> Pallet<T> {


pub fn get_dynamic_tao_emission( netuid: u16, tao_emission: u64, alpha_block_emission: u64 ) -> (u64, u64, u64) {

// Init terms.
let mut tao_in_emission: I96F32 = I96F32::from_num( tao_emission );
let float_alpha_block_emission: I96F32 = I96F32::from_num( alpha_block_emission );

// Get alpha price for subnet.
let alpha_price: I96F32 = Self::get_alpha_price( netuid );
log::debug!("{:?} - alpha_price: {:?}", netuid, alpha_price);

// Get initial alpha_in
let mut alpha_in_emission: I96F32 = I96F32::from_num( tao_emission ).checked_div( alpha_price ).unwrap_or( float_alpha_block_emission );

// Check if we are emitting too much alpha_in
if alpha_in_emission >= float_alpha_block_emission {
log::debug!("{:?} - alpha_in_emission: {:?} > alpha_block_emission: {:?}", netuid, alpha_in_emission, float_alpha_block_emission);

// Scale down tao_in
tao_in_emission = alpha_price.saturating_mul( float_alpha_block_emission);

// Set to max alpha_block_emission
alpha_in_emission = float_alpha_block_emission;

}

// Avoid rounding errors.
if tao_in_emission < I96F32::from_num(1) || alpha_in_emission < I96F32::from_num(1) {
alpha_in_emission = I96F32::from_num(0);
tao_in_emission = I96F32::from_num(0);
}

// Set Alpha in emission.
let alpha_out_emission = I96F32::from_num(2).saturating_mul( float_alpha_block_emission ).saturating_sub( alpha_in_emission );

// Log results.
log::debug!("{:?} - tao_in_emission: {:?}", netuid, tao_in_emission);
log::debug!("{:?} - alpha_in_emission: {:?}", netuid, alpha_in_emission);
log::debug!("{:?} - alpha_out_emission: {:?}", netuid, alpha_out_emission);

// Return result.
(tao_in_emission.to_num::<u64>(), alpha_in_emission.to_num::<u64>(), alpha_out_emission.to_num::<u64>())
}



pub fn get_root_divs_in_alpha( netuid: u16, alpha_out_emission: I96F32 ) -> I96F32 {
// Get total TAO on root.
let total_root_tao: I96F32 = I96F32::from_num( SubnetTAO::<T>::get( 0 ) );
Expand Down
1 change: 1 addition & 0 deletions pallets/subtensor/src/rpc_info/show_subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::epoch::math::*;
use frame_support::pallet_prelude::{Decode, Encode};
use substrate_fixed::types::I64F64;

#[freeze_struct("1af112d561741563")]
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)]
pub struct SubnetState<T: Config> {
netuid: Compact<u16>,
Expand Down

0 comments on commit f96edc1

Please sign in to comment.