Skip to content
Merged

Ckburn #1996

Show file tree
Hide file tree
Changes from 6 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
43 changes: 39 additions & 4 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ pub mod pallet {
BondsMovingAverageMaxReached,
/// Only root can set negative sigmoid steepness values
NegativeSigmoidSteepness,
/// Value not in allowed bounds.
ValueNotInBounds,
}
/// Enum for specifying the type of precompile operation.
#[derive(
Expand Down Expand Up @@ -665,12 +667,22 @@ pub mod pallet {
netuid: NetUid,
min_burn: TaoCurrency,
) -> DispatchResult {
ensure_root(origin)?;

pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin.clone(), netuid)?;
// Allow set min_burn but only up to 1 TAO for spamming.
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
// Min burn must be less than 1 TAO.
ensure!(
min_burn < TaoCurrency::from(1_000_000_000),
Error::<T>::ValueNotInBounds
);
// Min burn must be less than max burn
ensure!(
min_burn > pallet_subtensor::Pallet::<T>::get_max_burn(netuid),
Error::<T>::ValueNotInBounds
);
pallet_subtensor::Pallet::<T>::set_min_burn(netuid, min_burn);
log::debug!("MinBurnSet( netuid: {netuid:?} min_burn: {min_burn:?} ) ");
Ok(())
Expand All @@ -688,12 +700,21 @@ pub mod pallet {
netuid: NetUid,
max_burn: TaoCurrency,
) -> DispatchResult {
ensure_root(origin)?;

pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin.clone(), netuid)?;
ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
// Max burn must be greater than 0.1 TAO.
ensure!(
max_burn > TaoCurrency::from(100_000_000),
Error::<T>::ValueNotInBounds
);
// Max burn must be greater than min burn
ensure!(
max_burn > pallet_subtensor::Pallet::<T>::get_min_burn(netuid),
Error::<T>::ValueNotInBounds
);
pallet_subtensor::Pallet::<T>::set_max_burn(netuid, max_burn);
log::debug!("MaxBurnSet( netuid: {netuid:?} max_burn: {max_burn:?} ) ");
Ok(())
Expand Down Expand Up @@ -1682,6 +1703,20 @@ pub mod pallet {
pallet_subtensor::Pallet::<T>::set_owner_immune_neuron_limit(netuid, immune_neurons)?;
Ok(())
}

/// The extrinsic sets the difficulty for a subnet.
/// It is only callable by the root account or subnet owner.
/// The extrinsic will call the Subtensor pallet to set the difficulty.
#[pallet::call_index(73)]
#[pallet::weight(Weight::from_parts(15_650_000, 0)
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1_u64))
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)))]
pub fn sudo_set_ck_burn(origin: OriginFor<T>, burn: u64) -> DispatchResult {
ensure_root(origin)?;
pallet_subtensor::Pallet::<T>::set_ck_burn(burn);
log::debug!("CKBurnSet( burn: {burn:?} ) ");
Ok(())
}
}
}

Expand Down
42 changes: 22 additions & 20 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,18 +743,19 @@ impl<T: Config> Pallet<T> {
// Calculate the hotkey's share of the validator emission based on its childkey take
let validating_emission: U96F32 = U96F32::saturating_from_num(dividends);
let mut remaining_emission: U96F32 = validating_emission;
let childkey_take_proportion: U96F32 =
let burn_take_proportion: U96F32 = Self::get_ck_burn();
let child_take_proportion: U96F32 =
U96F32::saturating_from_num(Self::get_childkey_take(hotkey, netuid))
.safe_div(U96F32::saturating_from_num(u16::MAX));
log::debug!("Childkey take proportion: {childkey_take_proportion:?} for hotkey {hotkey:?}");
log::debug!("Childkey take proportion: {child_take_proportion:?} for hotkey {hotkey:?}");
// NOTE: Only the validation emission should be split amongst parents.

// Grab the owner of the childkey.
let childkey_owner = Self::get_owning_coldkey_for_hotkey(hotkey);

// Initialize variables to track emission distribution
let mut to_parents: u64 = 0;
let mut total_child_emission_take: U96F32 = U96F32::saturating_from_num(0);
let mut total_child_take: U96F32 = U96F32::saturating_from_num(0);

// Initialize variables to calculate total stakes from parents
let mut total_contribution: U96F32 = U96F32::saturating_from_num(0);
Expand Down Expand Up @@ -818,23 +819,25 @@ impl<T: Config> Pallet<T> {
remaining_emission = remaining_emission.saturating_sub(parent_emission);

// Get the childkey take for this parent.
let child_emission_take: U96F32 = if parent_owner == childkey_owner {
// The parent is from the same coldkey, so we don't remove any childkey take.
U96F32::saturating_from_num(0)
} else {
childkey_take_proportion
.saturating_mul(U96F32::saturating_from_num(parent_emission))
let mut burn_take: U96F32 = U96F32::saturating_from_num(0);
let mut child_take: U96F32 = U96F32::saturating_from_num(0);
if parent_owner != childkey_owner {
// The parent is from a different coldkey, we burn some proportion
burn_take = burn_take_proportion.saturating_mul(parent_emission);
child_take = child_take_proportion.saturating_mul(parent_emission);
parent_emission = parent_emission.saturating_sub(burn_take);
parent_emission = parent_emission.saturating_sub(child_take);
total_child_take = total_child_take.saturating_add(child_take);
SubnetAlphaOut::<T>::mutate(netuid, |total| {
*total = total
.saturating_sub(AlphaCurrency::from(burn_take.saturating_to_num::<u64>()));
});
};
log::debug!("burn_takee: {burn_take:?} for hotkey {hotkey:?}");
log::debug!("child_take: {child_take:?} for hotkey {hotkey:?}");
log::debug!("parent_emission: {parent_emission:?} for hotkey {hotkey:?}");
log::debug!("total_child_take: {total_child_take:?} for hotkey {hotkey:?}");

// Remove the childkey take from the parent's emission.
parent_emission = parent_emission.saturating_sub(child_emission_take);

// Add the childkey take to the total childkey take tracker.
total_child_emission_take =
total_child_emission_take.saturating_add(child_emission_take);

log::debug!("Child emission take: {child_emission_take:?} for hotkey {hotkey:?}");
log::debug!("Parent emission: {parent_emission:?} for hotkey {hotkey:?}");
log::debug!("remaining emission: {remaining_emission:?}");

// Add the parent's emission to the distribution list
Expand All @@ -852,13 +855,12 @@ impl<T: Config> Pallet<T> {
// Calculate the final emission for the hotkey itself.
// This includes the take left from the parents and the self contribution.
let child_emission = remaining_emission
.saturating_add(total_child_emission_take)
.saturating_add(total_child_take)
.saturating_to_num::<u64>()
.into();

// Add the hotkey's own emission to the distribution list
dividend_tuples.push((hotkey.clone(), child_emission));

dividend_tuples
}

Expand Down
3 changes: 3 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,9 @@ pub mod pallet {
/// --- ITEM --> Global weight
pub type TaoWeight<T> = StorageValue<_, u64, ValueQuery, DefaultTaoWeight<T>>;
#[pallet::storage]
/// --- ITEM --> CK burn
pub type CKBurn<T> = StorageValue<_, u64, ValueQuery, DefaultZeroU64<T>>;
#[pallet::storage]
/// --- ITEM ( default_delegate_take )
pub type MaxDelegateTake<T> = StorageValue<_, u16, ValueQuery, DefaultDelegateTake<T>>;
#[pallet::storage]
Expand Down
10 changes: 10 additions & 0 deletions pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ impl<T: Config> Pallet<T> {
// This ensures the result is always between 0 and 1
weight_fixed.safe_div(U96F32::saturating_from_num(u64::MAX))
}
pub fn get_ck_burn() -> U96F32 {
let stored_weight = CKBurn::<T>::get();
let weight_fixed = U96F32::saturating_from_num(stored_weight);
weight_fixed.safe_div(U96F32::saturating_from_num(u64::MAX))
}

/// Sets the global global weight in storage.
///
Expand All @@ -117,6 +122,11 @@ impl<T: Config> Pallet<T> {
// Update the TaoWeight storage with the new weight value
TaoWeight::<T>::set(weight);
}
// Set the amount burned on non owned CK
pub fn set_ck_burn(weight: u64) {
// Update the ck burn value.
CKBurn::<T>::set(weight);
}

/// Calculates the weighted combination of alpha and global tao for a single hotkey onet a subnet.
///
Expand Down
7 changes: 7 additions & 0 deletions precompiles/src/alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ where
Ok(U256::from(tao_weight))
}

#[precompile::public("getCKBurn()")]
#[precompile::view]
fn get_ck_burn(_handle: &mut impl PrecompileHandle) -> EvmResult<U256> {
let ck_burn = pallet_subtensor::CKBurn::<R>::get();
Ok(U256::from(ck_burn))
}

#[precompile::public("simSwapTaoForAlpha(uint16,uint64)")]
#[precompile::view]
fn sim_swap_tao_for_alpha(
Expand Down
Loading