Skip to content

Commit

Permalink
move set_alpha extrinsic to admin-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnReedV committed Jun 21, 2024
1 parent ccddd7b commit 39b4fae
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 48 deletions.
9 changes: 9 additions & 0 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,14 @@ pub mod pallet {
);
Ok(())
}

/// Sets values for liquid alpha
#[pallet::call_index(51)]
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
pub fn sudo_set_alpha_values(origin: OriginFor<T>, netuid: u16, alpha_low: u16, alpha_high: u16) -> DispatchResult {
T::Subtensor::ensure_subnet_owner_or_root(origin.clone(), netuid)?;
T::Subtensor::do_set_alpha_values(origin, netuid, alpha_low, alpha_high)
}
}
}

Expand Down Expand Up @@ -1118,4 +1126,5 @@ pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin> {
fn set_commit_reveal_weights_interval(netuid: u16, interval: u64);
fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool);
fn set_liquid_alpha_enabled(netuid: u16, enabled: bool);
fn do_set_alpha_values(origin: RuntimeOrigin, netuid: u16, alpha_low: u16, alpha_high: u16) -> Result<(), DispatchError>;
}
3 changes: 3 additions & 0 deletions pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> f
fn set_liquid_alpha_enabled(netuid: u16, enabled: bool) {
SubtensorModule::set_liquid_alpha_enabled(netuid, enabled);
}
fn do_set_alpha_values(origin: RuntimeOrigin, netuid: u16, alpha_low: u16, alpha_high: u16) -> Result<(), DispatchError> {
SubtensorModule::do_set_alpha_values(origin, netuid, alpha_low, alpha_high)
}
}

impl pallet_admin_utils::Config for Test {
Expand Down
123 changes: 121 additions & 2 deletions pallets/admin-utils/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use frame_support::assert_ok;
use frame_support::{assert_ok, assert_err, dispatch::{DispatchClass, GetDispatchInfo, Pays}};
use frame_support::sp_runtime::DispatchError;
use frame_system::Config;
use pallet_admin_utils::Error;
use pallet_subtensor::Error as SubtensorError;
use pallet_subtensor::Event;
use pallet_subtensor::{migration, Event};
use sp_core::U256;

mod mock;
Expand Down Expand Up @@ -1196,3 +1196,122 @@ fn test_sudo_set_liquid_alpha_enabled() {
assert_eq!(enabled, SubtensorModule::get_liquid_alpha_enabled(netuid));
});
}

#[test]
fn test_set_alpha_values_dispatch_info_ok() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let alpha_low: u16 = 12_u16;
let alpha_high: u16 = u16::MAX - 10;
let call = RuntimeCall::AdminUtils(pallet_admin_utils::Call::sudo_set_alpha_values {
netuid,
alpha_low,
alpha_high,
});

let dispatch_info = call.get_dispatch_info();

assert_eq!(dispatch_info.class, DispatchClass::Operational);
assert_eq!(dispatch_info.pays_fee, Pays::No);
});
}

#[test]
fn test_sudo_get_set_alpha() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let alpha_low: u16 = 12_u16;
let alpha_high: u16 = u16::MAX - 10;

let hotkey: U256 = U256::from(1);
let coldkey: U256 = U256::from(1 + 456);
let signer = <<Test as Config>::RuntimeOrigin>::signed(coldkey.clone());

// Enable Liquid Alpha and setup
SubtensorModule::set_liquid_alpha_enabled(netuid, true);
migration::migrate_create_root_network::<Test>();
SubtensorModule::add_balance_to_coldkey_account(
&coldkey,
1_000_000_000_000_000,
);
assert_ok!(SubtensorModule::root_register(
signer.clone(),
hotkey,
));
assert_ok!(SubtensorModule::add_stake(
signer.clone(),
hotkey,
1000
));

// Should fail as signer does not own the subnet
assert_err!(
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high),
DispatchError::BadOrigin
);

assert_ok!(SubtensorModule::register_network(
signer.clone()
));

assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
let (grabbed_alpha_low, grabbed_alpha_high): (u16, u16) = SubtensorModule::get_alpha_values(netuid);

log::info!("alpha_low: {:?} alpha_high: {:?}", grabbed_alpha_low, grabbed_alpha_high);
assert_eq!(grabbed_alpha_low, alpha_low);
assert_eq!(grabbed_alpha_high, alpha_high);

// Convert the u16 values to decimal values
fn unnormalize_u16_to_float(normalized_value: u16) -> f32 {
const MAX_U16: u16 = 65535;
normalized_value as f32 / MAX_U16 as f32
}

let alpha_low_decimal = unnormalize_u16_to_float(alpha_low);
let alpha_high_decimal = unnormalize_u16_to_float(alpha_high);

let (alpha_low_32, alpha_high_32) = SubtensorModule::get_alpha_values_32(netuid);

let tolerance: f32 = 1e-6; // 0.000001

// Check if the values are equal to the sixth decimal
assert!((alpha_low_32.to_num::<f32>() - alpha_low_decimal).abs() < tolerance, "alpha_low mismatch: {} != {}", alpha_low_32.to_num::<f32>(), alpha_low_decimal);
assert!((alpha_high_32.to_num::<f32>() - alpha_high_decimal).abs() < tolerance, "alpha_high mismatch: {} != {}", alpha_high_32.to_num::<f32>(), alpha_high_decimal);

// 1. Liquid alpha disabled
SubtensorModule::set_liquid_alpha_enabled(netuid, false);
assert_err!(
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high),
SubtensorError::<Test>::LiquidAlphaDisabled
);
// Correct scenario after error
SubtensorModule::set_liquid_alpha_enabled(netuid, true); // Re-enable for further tests
assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));

// 2. Alpha high too low
let alpha_high_too_low = (u16::MAX as u32 * 4 / 5) as u16 - 1; // One less than the minimum acceptable value
assert_err!(
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high_too_low),
SubtensorError::<Test>::AlphaHighTooLow
);
// Correct scenario after error
assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));

// 3. Alpha low too low or too high
let alpha_low_too_low = 0_u16;
assert_err!(
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low_too_low, alpha_high),
SubtensorError::<Test>::AlphaLowOutOfRange
);
// Correct scenario after error
assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));

let alpha_low_too_high = (u16::MAX as u32 * 4 / 5) as u16 + 1; // One more than the maximum acceptable value
assert_err!(
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low_too_high, alpha_high),
SubtensorError::<Test>::AlphaLowOutOfRange
);
// Correct scenario after error
assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
});
}
9 changes: 8 additions & 1 deletion pallets/subtensor/src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,10 +1204,17 @@ impl<T: Config> Pallet<T> {
// -- 5. Ensure alpha low is within range
ensure!(
alpha_low > 0 && alpha_low < min_alpha_high,
Error::<T>::AlphaLowTooLow
Error::<T>::AlphaLowOutOfRange
);

AlphaValues::<T>::insert(netuid, (alpha_low, alpha_high));

log::info!(
"AlphaValuesSet( netuid: {:?}, AlphaLow: {:?}, AlphaHigh: {:?} ) ",
netuid,
alpha_low,
alpha_high,
);
Ok(())
}
}
6 changes: 3 additions & 3 deletions pallets/subtensor/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ mod errors {
CommitRevealDisabled,
/// Attempting to set alpha high/low while disabled
LiquidAlphaDisabled,
/// Alpha high is too low
/// Alpha high is too low: alpha_high > 0.8
AlphaHighTooLow,
/// Alpha low is too low
AlphaLowTooLow,
/// Alpha low is out of range: alpha_low > 0 && alpha_low < 0.8
AlphaLowOutOfRange,
}
}
9 changes: 0 additions & 9 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2078,15 +2078,6 @@ 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
48 changes: 15 additions & 33 deletions pallets/subtensor/tests/epoch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::mock::*;
use frame_support::{dispatch::{DispatchClass, GetDispatchInfo, Pays}, assert_err, assert_ok};
use frame_support::{assert_err, assert_ok};
use frame_system::Config;
use pallet_subtensor::*;
use rand::{distributions::Uniform, rngs::StdRng, seq::SliceRandom, thread_rng, Rng, SeedableRng};
Expand Down Expand Up @@ -1510,12 +1510,12 @@ fn test_set_alpha_disabled() {
// Explicitly set to false
SubtensorModule::set_liquid_alpha_enabled(netuid, false);
assert_err!(
SubtensorModule::set_alpha_values(signer.clone(), netuid, 12_u16, u16::MAX),
SubtensorModule::do_set_alpha_values(signer.clone(), netuid, 12_u16, u16::MAX),
Error::<Test>::LiquidAlphaDisabled
);

SubtensorModule::set_liquid_alpha_enabled(netuid, true);
assert_ok!(SubtensorModule::set_alpha_values(signer.clone(), netuid, 12_u16, u16::MAX));
assert_ok!(SubtensorModule::do_set_alpha_values(signer.clone(), netuid, 12_u16, u16::MAX));
});
}

Expand Down Expand Up @@ -2555,15 +2555,15 @@ fn test_get_set_alpha() {

// Should fail as signer does not own the subnet
assert_err!(
SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high),
SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high),
DispatchError::BadOrigin
);

assert_ok!(SubtensorModule::register_network(
signer.clone()
));

assert_ok!(SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
assert_ok!(SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
let (grabbed_alpha_low, grabbed_alpha_high): (u16, u16) = SubtensorModule::get_alpha_values(netuid);

log::info!("alpha_low: {:?} alpha_high: {:?}", grabbed_alpha_low, grabbed_alpha_high);
Expand All @@ -2590,56 +2590,38 @@ fn test_get_set_alpha() {
// 1. Liquid alpha disabled
SubtensorModule::set_liquid_alpha_enabled(netuid, false);
assert_err!(
SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high),
SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high),
Error::<Test>::LiquidAlphaDisabled
);
// Correct scenario after error
SubtensorModule::set_liquid_alpha_enabled(netuid, true); // Re-enable for further tests
assert_ok!(SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
assert_ok!(SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));

// 2. Alpha high too low
let alpha_high_too_low = (u16::MAX as u32 * 4 / 5) as u16 - 1; // One less than the minimum acceptable value
assert_err!(
SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high_too_low),
SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high_too_low),
Error::<Test>::AlphaHighTooLow
);
// Correct scenario after error
assert_ok!(SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
assert_ok!(SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));

// 3. Alpha low too low or too high
let alpha_low_too_low = 0_u16;
assert_err!(
SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low_too_low, alpha_high),
Error::<Test>::AlphaLowTooLow
SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low_too_low, alpha_high),
Error::<Test>::AlphaLowOutOfRange
);
// Correct scenario after error
assert_ok!(SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
assert_ok!(SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));

let alpha_low_too_high = (u16::MAX as u32 * 4 / 5) as u16 + 1; // One more than the maximum acceptable value
assert_err!(
SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low_too_high, alpha_high),
Error::<Test>::AlphaLowTooLow
SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low_too_high, alpha_high),
Error::<Test>::AlphaLowOutOfRange
);
// Correct scenario after error
assert_ok!(SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
});
}

#[test]
fn test_set_alpha_values_dispatch_info_ok() {
new_test_ext(0).execute_with(|| {
let netuid: u16 = 1;
let alpha_low: u16 = 12_u16;
let alpha_high: u16 = u16::MAX - 10;
let call = RuntimeCall::SubtensorModule(SubtensorCall::set_alpha_values {
netuid,
alpha_low,
alpha_high,
});
let dispatch_info = call.get_dispatch_info();

assert_eq!(dispatch_info.class, DispatchClass::Operational);
assert_eq!(dispatch_info.pays_fee, Pays::No);
assert_ok!(SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
});
}

Expand Down
4 changes: 4 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,10 @@ impl
fn set_liquid_alpha_enabled(netuid: u16, enabled: bool) {
SubtensorModule::set_liquid_alpha_enabled(netuid, enabled);
}

fn do_set_alpha_values(origin: RuntimeOrigin, netuid: u16, alpha_low: u16, alpha_high: u16) -> Result<(), DispatchError> {
SubtensorModule::do_set_alpha_values(origin, netuid, alpha_low, alpha_high)
}
}

impl pallet_admin_utils::Config for Runtime {
Expand Down

0 comments on commit 39b4fae

Please sign in to comment.