Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: liquid alpha #552

Merged
merged 20 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 1 addition & 53 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,56 +997,6 @@ pub mod pallet {
Ok(())
}

/// Sets the lower bound for the alpha parameter for a given subnet.
///
/// # Parameters
/// - `origin`: The origin of the call, which must be the root account or subnet owner.
/// - `netuid`: The unique identifier for the subnet.
/// - `alpha_low`: The new lower bound value for the alpha parameter.
///
/// # Weight
/// This function has a fixed weight of 0 and is classified as an operational transaction that does not incur any fees.
#[pallet::call_index(50)]
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
pub fn sudo_set_alpha_low(
origin: OriginFor<T>,
netuid: u16,
alpha_low: u16,
) -> DispatchResult {
T::Subtensor::ensure_subnet_owner_or_root(origin, netuid)?;
T::Subtensor::set_alpha_low(netuid, alpha_low)?;
log::info!(
"AlphaLowSet( netuid: {:?}, alpha_low: {:?} ) ",
netuid,
alpha_low
);
Ok(())
}
/// Sets the upper bound for the alpha parameter for a given subnet.
///
/// # Parameters
/// - `origin`: The origin of the call, which must be the root account or subnet owner.
/// - `netuid`: The unique identifier for the subnet.
/// - `alpha_high`: The new upper bound value for the alpha parameter.
///
/// # Weight
/// This function has a fixed weight of 0 and is classified as an operational transaction that does not incur any fees.
#[pallet::call_index(51)]
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
pub fn sudo_set_alpha_high(
origin: OriginFor<T>,
netuid: u16,
alpha_high: u16,
) -> DispatchResult {
T::Subtensor::ensure_subnet_owner_or_root(origin, netuid)?;
T::Subtensor::set_alpha_high(netuid, alpha_high)?;
log::info!(
"AlphaHighSet( netuid: {:?}, alpha_high: {:?} ) ",
netuid,
alpha_high
);
Ok(())
}
/// Enables or disables Liquid Alpha for a given subnet.
///
/// # Parameters
Expand All @@ -1056,7 +1006,7 @@ pub mod pallet {
///
/// # Weight
/// This function has a fixed weight of 0 and is classified as an operational transaction that does not incur any fees.
#[pallet::call_index(52)]
#[pallet::call_index(50)]
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
pub fn sudo_set_liquid_alpha_enabled(
origin: OriginFor<T>,
Expand Down Expand Up @@ -1167,7 +1117,5 @@ pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin> {
fn set_target_stakes_per_interval(target_stakes_per_interval: u64);
fn set_commit_reveal_weights_interval(netuid: u16, interval: u64);
fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool);
fn set_alpha_high(netuid: u16, alpha_high: u16) -> Result<(), DispatchError>;
fn set_alpha_low(netuid: u16, alpha_low: u16) -> Result<(), DispatchError>;
fn set_liquid_alpha_enabled(netuid: u16, enabled: bool);
}
8 changes: 0 additions & 8 deletions pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,14 +468,6 @@ impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> f
SubtensorModule::set_commit_reveal_weights_enabled(netuid, enabled);
}

fn set_alpha_high(netuid: u16, alpha_high: u16) -> Result<(), DispatchError> {
SubtensorModule::set_alpha_high(netuid, alpha_high)
}

fn set_alpha_low(netuid: u16, alpha_low: u16) -> Result<(), DispatchError> {
SubtensorModule::set_alpha_low(netuid, alpha_low)
}

fn set_liquid_alpha_enabled(netuid: u16, enabled: bool) {
SubtensorModule::set_liquid_alpha_enabled(netuid, enabled);
}
Expand Down
148 changes: 0 additions & 148 deletions pallets/admin-utils/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,154 +1180,6 @@ fn test_sudo_set_target_stakes_per_interval() {
});
}

#[test]
fn alpha_low_can_only_be_called_by_admin() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let to_be_set: u16 = 52428; // 0.8 i.e. 0.8 x u16::MAX
assert_eq!(
AdminUtils::sudo_set_alpha_low(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
netuid,
to_be_set
),
Err(DispatchError::BadOrigin)
);
});
}

#[test]
fn sets_alpha_low_valid_value() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let to_be_set: u16 = 52428; // 0.8 i.e. 0.8 x u16::MAX
let init_value = SubtensorModule::get_alpha_low(netuid);
assert_eq!(SubtensorModule::get_alpha_low(netuid), init_value);
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
true,
));
assert_ok!(AdminUtils::sudo_set_alpha_low(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
));
assert_eq!(SubtensorModule::get_alpha_low(netuid), to_be_set);
});
}

#[test]
fn alpha_low_fails_if_liquid_alpha_disabled() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let to_be_set: u16 = 52428; // 0.8 i.e. 0.8 x u16::MAX
assert_eq!(
AdminUtils::sudo_set_alpha_low(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
),
Err(SubtensorError::<Test>::LiquidAlphaDisabled.into())
);
});
}

#[test]
fn alpha_low_fails_if_alpha_low_too_low() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let to_be_set: u16 = 0; // Invalid value
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
true,
));
assert_eq!(
AdminUtils::sudo_set_alpha_low(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
),
Err(SubtensorError::<Test>::AlphaLowTooLow.into())
);
});
}

#[test]
fn alpha_high_can_only_be_called_by_admin() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let to_be_set: u16 = 60000; // Valid value
assert_eq!(
AdminUtils::sudo_set_alpha_high(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
netuid,
to_be_set
),
Err(DispatchError::BadOrigin)
);
});
}

#[test]
fn sets_a_valid_value() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let to_be_set: u16 = 60000; // Valid value
let init_value = SubtensorModule::get_alpha_high(netuid);
assert_eq!(SubtensorModule::get_alpha_high(netuid), init_value);
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
true,
));
assert_ok!(AdminUtils::sudo_set_alpha_high(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
));
assert_eq!(SubtensorModule::get_alpha_high(netuid), to_be_set);
});
}

#[test]
fn alpha_high_fails_if_liquid_alpha_disabled() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let to_be_set: u16 = 60000; // Valid value
assert_eq!(
AdminUtils::sudo_set_alpha_high(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
),
Err(SubtensorError::<Test>::LiquidAlphaDisabled.into())
);
});
}

#[test]
fn fails_if_alpha_high_too_low() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let to_be_set: u16 = 50000; // Invalid value, less than 52428
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
true,
));
assert_eq!(
AdminUtils::sudo_set_alpha_high(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
),
Err(SubtensorError::<Test>::AlphaHighTooLow.into())
);
});
}

#[test]
fn test_sudo_set_liquid_alpha_enabled() {
new_test_ext().execute_with(|| {
Expand Down
38 changes: 30 additions & 8 deletions pallets/subtensor/src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,10 +1082,8 @@ impl<T: Config> Pallet<T> {
log::trace!("Using Liquid Alpha");

// Get the high and low alpha values for the network.
let alpha_high = Self::get_alpha_high_32(netuid);
log::trace!("alpha_high: {:?}", alpha_high);
let alpha_low = Self::get_alpha_low_32(netuid);
log::trace!("alpha_low: {:?}", alpha_low);
let (alpha_low, alpha_high): (I32F32, I32F32) = Self::get_alpha_values_32(netuid);
log::trace!("alpha_low: {:?} alpha_high: {:?}", alpha_low, alpha_high);

// Calculate the logistic function parameters 'a' and 'b' based on alpha and consensus values.
let (a, b) = Self::calculate_logistic_params(
Expand Down Expand Up @@ -1151,10 +1149,8 @@ impl<T: Config> Pallet<T> {
log::trace!("Using Liquid Alpha");

// Get the high and low alpha values for the network.
let alpha_high = Self::get_alpha_high_32(netuid);
log::trace!("alpha_high: {:?}", alpha_high);
let alpha_low = Self::get_alpha_low_32(netuid);
log::trace!("alpha_low: {:?}", alpha_low);
let (alpha_low, alpha_high): (I32F32, I32F32) = Self::get_alpha_values_32(netuid);
log::trace!("alpha_low: {:?} alpha_high: {:?}", alpha_low, alpha_high);

// Calculate the logistic function parameters 'a' and 'b' based on alpha and consensus values.
let (a, b) = Self::calculate_logistic_params(
Expand Down Expand Up @@ -1185,4 +1181,30 @@ impl<T: Config> Pallet<T> {
Self::compute_ema_bonds_normal(&bonds_delta, &bonds, netuid)
}
}

pub fn do_set_alpha_values(origin: T::RuntimeOrigin, netuid: u16, alpha_low: u16, alpha_high: u16) -> Result<(), DispatchError> {
JohnReedV marked this conversation as resolved.
Show resolved Hide resolved
// --- 1. Ensure the function caller is a signed user.
let _coldkey = ensure_signed(origin)?;
JohnReedV marked this conversation as resolved.
Show resolved Hide resolved

let max_u16: u32 = u16::MAX as u32; // 65535
let min_alpha_high: u16 = (max_u16.saturating_mul(4).saturating_div(5)) as u16; // 52428

// --- 2. Ensure liquid alpha is enabled
ensure!(
Self::get_liquid_alpha_enabled(netuid),
Error::<T>::LiquidAlphaDisabled
);

// --- 3. Ensure alpha high is greater than the minimum
ensure!(alpha_high >= min_alpha_high, Error::<T>::AlphaHighTooLow);

// -- 4. Ensure alpha low is within range
ensure!(
alpha_low > 0 && alpha_low < min_alpha_high,
Error::<T>::AlphaLowTooLow
);
AlphaValues::<T>::insert(netuid, (alpha_low, alpha_high));

Ok(())
}
}
28 changes: 15 additions & 13 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,14 +866,9 @@ pub mod pallet {
/// Provides the default value for the upper bound of the alpha parameter.

#[pallet::type_value]
pub fn DefaultAlphaHigh<T: Config>() -> u16 {
58982 // Represents 0.9 as per the production default
}
/// Provides the default value for the lower bound of the alpha parameter.
#[pallet::type_value]
pub fn DefaultAlphaLow<T: Config>() -> u16 {
45875 // Represents 0.7 as per the production default
}
pub fn DefaultAlphaValues<T: Config>() -> (u16, u16) {
(45875, 58982) // (alpha_low: 0.7, alpha_high: 0.9)
}

#[pallet::storage] // ITEM( weights_min_stake )
pub type WeightsMinStake<T> = StorageValue<_, u64, ValueQuery, DefaultWeightsMinStake<T>>;
Expand Down Expand Up @@ -944,12 +939,10 @@ pub mod pallet {
#[pallet::storage] // --- DMAP ( netuid ) --> adjustment_alpha
pub type AdjustmentAlpha<T: Config> =
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultAdjustmentAlpha<T>>;
// MAP ( netuid ) --> alpha_high
#[pallet::storage]
pub type AlphaHigh<T> = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultAlphaHigh<T>>;
// MAP ( netuid ) --> alpha_low

// MAP ( netuid ) --> (alpha_low, alpha_high)
#[pallet::storage]
pub type AlphaLow<T> = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultAlphaLow<T>>;
pub type AlphaValues<T> = StorageMap<_, Identity, u16, (u16, u16), ValueQuery, DefaultAlphaValues<T>>;

#[pallet::storage] // --- MAP (netuid, who) --> (hash, weight) | Returns the hash and weight committed by an account for a given netuid.
pub type WeightCommits<T: Config> = StorageDoubleMap<
Expand Down Expand Up @@ -2085,6 +2078,15 @@ pub mod pallet {
pub fn dissolve_network(origin: OriginFor<T>, netuid: u16) -> DispatchResult {
Self::user_remove_network(origin, netuid)
}

/// Sets values for liquid alpha
#[pallet::call_index(88)]
#[pallet::weight((Weight::from_parts(119_000_000, 0)
.saturating_add(T::DbWeight::get().reads(0))
.saturating_add(T::DbWeight::get().writes(0)), DispatchClass::Operational, Pays::No))]
pub fn set_alpha_values(origin: OriginFor<T>, netuid: u16, alpha_low: u16, alpha_high: u16) -> DispatchResult {
Self::do_set_alpha_values(origin, netuid, alpha_low, alpha_high)
}
}

// ---- Subtensor helper functions.
Expand Down
Loading
Loading