Skip to content

Commit

Permalink
lints , use sat maths for 256 miner alpha test
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Dare committed Jun 23, 2024
1 parent 2e51ebd commit b836401
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 94 deletions.
14 changes: 12 additions & 2 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,12 @@ pub mod pallet {
/// 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 {
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 @@ -1126,5 +1131,10 @@ 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>;
fn do_set_alpha_values(
origin: RuntimeOrigin,
netuid: u16,
alpha_low: u16,
alpha_high: u16,
) -> Result<(), DispatchError>;
}
7 changes: 6 additions & 1 deletion pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,12 @@ 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> {
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)
}
}
Expand Down
106 changes: 76 additions & 30 deletions pallets/admin-utils/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use frame_support::{assert_ok, assert_err, dispatch::{DispatchClass, GetDispatchInfo, Pays}};
use frame_support::sp_runtime::DispatchError;
use frame_support::{
assert_err, assert_ok,
dispatch::{DispatchClass, GetDispatchInfo, Pays},
};
use frame_system::Config;
use pallet_admin_utils::Error;
use pallet_subtensor::Error as SubtensorError;
Expand Down Expand Up @@ -1225,39 +1228,37 @@ fn test_sudo_get_set_alpha() {

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

// 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
));
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!(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);
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);
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);

Expand All @@ -1275,8 +1276,18 @@ fn test_sudo_get_set_alpha() {
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);
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);
Expand All @@ -1286,32 +1297,67 @@ fn test_sudo_get_set_alpha() {
);
// 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));
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),
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));
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),
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));
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),
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));
assert_ok!(AdminUtils::sudo_set_alpha_values(
signer.clone(),
netuid,
alpha_low,
alpha_high
));
});
}
9 changes: 7 additions & 2 deletions pallets/subtensor/src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,13 +1182,18 @@ impl<T: Config> Pallet<T> {
}
}

pub fn do_set_alpha_values(origin: T::RuntimeOrigin, netuid: u16, alpha_low: u16, alpha_high: u16) -> Result<(), DispatchError> {
pub fn do_set_alpha_values(
origin: T::RuntimeOrigin,
netuid: u16,
alpha_low: u16,
alpha_high: u16,
) -> Result<(), DispatchError> {
// --- 1. Ensure the function caller is a signed user.
ensure_signed(origin.clone())?;

// --- 2. Ensure the function caller is the subnet owner or root.
Self::ensure_subnet_owner_or_root(origin, netuid)?;

// --- 3. Ensure liquid alpha is enabled
ensure!(
Self::get_liquid_alpha_enabled(netuid),
Expand Down
5 changes: 3 additions & 2 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ pub mod pallet {
#[pallet::type_value]
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 @@ -942,7 +942,8 @@ pub mod pallet {

// MAP ( netuid ) --> (alpha_low, alpha_high)
#[pallet::storage]
pub type AlphaValues<T> = StorageMap<_, Identity, u16, (u16, u16), ValueQuery, DefaultAlphaValues<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
2 changes: 1 addition & 1 deletion pallets/subtensor/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ impl<T: Config> Pallet<T> {
let converted_low = I32F32::from_num(alpha_low) / I32F32::from_num(u16::MAX);
let converted_high = I32F32::from_num(alpha_high) / I32F32::from_num(u16::MAX);

return (converted_low, converted_high);
(converted_low, converted_high)
}

pub fn set_liquid_alpha_enabled(netuid: u16, enabled: bool) {
Expand Down
Loading

0 comments on commit b836401

Please sign in to comment.