Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
94 changes: 88 additions & 6 deletions pallets/subtensor/src/epoch/run_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,9 +1274,21 @@ impl<T: Config> Pallet<T> {
.iter()
.any(|&c| c != I32F32::saturating_from_num(0))
{
// Liquid Alpha is enabled, compute the liquid alphas matrix.
let alphas: Vec<Vec<I32F32>> =
Self::compute_liquid_alpha_values(netuid, weights, bonds, consensus);
// Liquid Alpha is enabled, compute the appropriate consensus for liquid alpha based on mode
let consensus_for_liquid_alpha =
Self::compute_consensus_for_liquid_alpha(netuid, consensus);
log::trace!(
"consensus_for_liquid_alpha: {:?}",
&consensus_for_liquid_alpha
);

// Compute the liquid alphas matrix.
let alphas: Vec<Vec<I32F32>> = Self::compute_liquid_alpha_values(
netuid,
weights,
bonds,
&consensus_for_liquid_alpha,
);
log::trace!("alphas: {:?}", &alphas);

// Compute the Exponential Moving Average (EMA) of bonds using the provided clamped alpha values.
Expand Down Expand Up @@ -1316,9 +1328,21 @@ impl<T: Config> Pallet<T> {
.iter()
.any(|&c| c != I32F32::saturating_from_num(0))
{
// Liquid Alpha is enabled, compute the liquid alphas matrix.
let alphas: Vec<Vec<I32F32>> =
Self::compute_liquid_alpha_values_sparse(netuid, weights, bonds, consensus);
// Liquid Alpha is enabled, compute the appropriate consensus for liquid alpha based on mode
let consensus_for_liquid_alpha =
Self::compute_consensus_for_liquid_alpha(netuid, consensus);
log::trace!(
"consensus_for_liquid_alpha: {:?}",
&consensus_for_liquid_alpha
);

// Compute the liquid alphas matrix.
let alphas: Vec<Vec<I32F32>> = Self::compute_liquid_alpha_values_sparse(
netuid,
weights,
bonds,
&consensus_for_liquid_alpha,
);
log::trace!("alphas: {:?}", &alphas);

// Compute the Exponential Moving Average (EMA) of bonds using the provided clamped alpha values.
Expand All @@ -1332,6 +1356,51 @@ impl<T: Config> Pallet<T> {
}
}

/// Compute the consensus to use for liquid alpha calculation based on the configured mode
///
/// # Args:
/// * `netuid` - The network ID.
/// * `current_consensus` - The current in-memory consensus values.
///
/// # Returns:
/// A vector of consensus values to use for liquid alpha calculation
pub fn compute_consensus_for_liquid_alpha(
netuid: NetUid,
current_consensus: &[I32F32],
) -> Vec<I32F32> {
let mode = Self::get_liquid_alpha_consensus_mode(netuid);

match mode {
ConsensusMode::Current => {
// Use the in-memory consensus (current behavior)
current_consensus.to_vec()
}
ConsensusMode::Previous => {
// Use consensus from storage
Self::get_previous_consensus_as_i32f32(netuid)
}
ConsensusMode::Auto => {
// Auto mode: Previous if bond_penalty == 1, otherwise Current
let bonds_penalty = Self::get_float_bonds_penalty(netuid);
(bonds_penalty == I32F32::from_num(1))
.then(|| Self::get_previous_consensus_as_i32f32(netuid))
.unwrap_or_else(|| current_consensus.to_vec())
}
}
}

/// Convert stored consensus (u16 values) to I32F32 format
/// Used by consensus modes that need to read from storage
fn get_previous_consensus_as_i32f32(netuid: NetUid) -> Vec<I32F32> {
let previous_consensus_u16 = Consensus::<T>::get(netuid);
previous_consensus_u16
.iter()
.map(|&c| {
I32F32::saturating_from_num(c).safe_div(I32F32::saturating_from_num(u16::MAX))
})
.collect()
}

/// Compute liquid alphas matrix
/// There is a separate alpha param for each validator-miner binding
///
Expand Down Expand Up @@ -1539,6 +1608,19 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub fn do_set_liquid_alpha_consensus_mode(
origin: T::RuntimeOrigin,
netuid: NetUid,
mode: ConsensusMode,
) -> Result<(), DispatchError> {
Self::ensure_subnet_owner_or_root(origin, netuid)?;

Self::set_liquid_alpha_consensus_mode_storage(netuid, mode.clone());

log::debug!("LiquidAlphaConsensusModeSet( netuid: {netuid:?}, mode: {mode:?} )",);
Ok(())
}

pub fn do_reset_bonds(
netuid_index: NetUidStorageIndex,
account_id: &T::AccountId,
Expand Down
26 changes: 25 additions & 1 deletion pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ pub mod pallet {
},
}

/// Enum for consensus mode used in liquid alpha calculation
#[derive(
Encode, Decode, DecodeWithMemTracking, Default, TypeInfo, Clone, PartialEq, Eq, Debug,
)]
pub enum ConsensusMode {
/// Use current in-memory consensus (current behavior)
Current,
/// Use previous consensus from storage
Previous,
/// Auto mode: Previous if bond_penalty == 1, otherwise Current
#[default]
Auto,
}
/// Default minimum root claim amount.
/// This is the minimum amount of root claim that can be made.
/// Any amount less than this will not be claimed.
Expand Down Expand Up @@ -947,6 +960,12 @@ pub mod pallet {
(45875, 58982)
}

#[pallet::type_value]
/// Default consensus mode for liquid alpha calculation
pub fn DefaultConsensusMode<T: Config>() -> ConsensusMode {
ConsensusMode::default()
}

/// Default value for coldkey swap announcement delay.
#[pallet::type_value]
pub fn DefaultColdkeySwapAnnouncementDelay<T: Config>() -> BlockNumberFor<T> {
Expand Down Expand Up @@ -1884,8 +1903,13 @@ pub mod pallet {
pub type AlphaValues<T> =
StorageMap<_, Identity, NetUid, (u16, u16), ValueQuery, DefaultAlphaValues<T>>;

/// --- MAP ( netuid ) --> If subtoken trading enabled
#[pallet::storage]
/// MAP ( netuid ) --> consensus mode for liquid alpha calculation
pub type LiquidAlphaConsensusMode<T> =
StorageMap<_, Identity, NetUid, ConsensusMode, ValueQuery, DefaultConsensusMode<T>>;

#[pallet::storage]
/// --- MAP ( netuid ) --> If subtoken trading enabled
pub type SubtokenEnabled<T> =
StorageMap<_, Identity, NetUid, bool, ValueQuery, DefaultFalse<T>>;

Expand Down
31 changes: 31 additions & 0 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2599,5 +2599,36 @@ mod dispatches {
) -> DispatchResult {
Self::do_subnet_buyback(origin, hotkey, netuid, amount, limit)
}

/// Sets the consensus mode for liquid alpha calculation on a subnet.
///
/// This function can only be called by the subnet owner or root.
/// The consensus mode determines which consensus values are used for liquid alpha calculation:
/// - `Current`: Use current in-memory consensus
/// - `Previous`: Use previous consensus from storage
/// - `Auto`: Use Previous if bond_penalty == 1, otherwise Current (default)
///
/// # Arguments:
/// * `origin` - The origin of the call, must be subnet owner or root.
/// * `netuid` - The subnet to set the mode for.
/// * `mode` - The consensus mode to use.
///
/// # Errors:
/// * `BadOrigin` - If the origin is not the subnet owner or root.
#[pallet::call_index(133)]
#[pallet::weight((
Weight::from_parts(10_000, 0)
.saturating_add(T::DbWeight::get().reads(2))
.saturating_add(T::DbWeight::get().writes(1)),
DispatchClass::Operational,
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should be normal

Pays::Yes
))]
pub fn set_liquid_alpha_consensus_mode(
origin: OriginFor<T>,
netuid: NetUid,
mode: ConsensusMode,
) -> DispatchResult {
Self::do_set_liquid_alpha_consensus_mode(origin, netuid, mode)
}
}
}
Loading
Loading