Skip to content

Commit

Permalink
fix tests lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Dare committed Jun 21, 2024
1 parent 5487aeb commit c7dc69b
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 252 deletions.
276 changes: 136 additions & 140 deletions pallets/admin-utils/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use frame_support::assert_ok;
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 sp_core::U256;
use pallet_subtensor::Error as SubtensorError;


mod mock;
use mock::*;
Expand Down Expand Up @@ -1181,156 +1180,153 @@ 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 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 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]
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,
true,
));
assert_ok!(AdminUtils::sudo_set_alpha_low(
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
));
assert_eq!(SubtensorModule::get_alpha_low(netuid), to_be_set);
});
}
),
Err(SubtensorError::<Test>::AlphaLowTooLow.into())
);
});
}

#[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_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 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]
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,
true,
));
assert_eq!(
AdminUtils::sudo_set_alpha_low(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
),
Err(SubtensorError::<Test>::AlphaLowTooLow.into())
);
});
}
to_be_set
),
Err(SubtensorError::<Test>::LiquidAlphaDisabled.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 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() {
Expand Down
32 changes: 13 additions & 19 deletions pallets/subtensor/src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,27 +845,18 @@ impl<T: Config> Pallet<T> {

// Calculate the slope 'a' of the logistic function.
// a = (ln((1 / alpha_high - 1)) - ln((1 / alpha_low - 1))) / (consensus_low - consensus_high)
let a = (safe_ln(
I32F32::from_num(1.0)
.saturating_div(alpha_high)
.saturating_sub(I32F32::from_num(1.0)),
)
.saturating_sub(safe_ln(
I32F32::from_num(1.0)
.saturating_div(alpha_low)
.saturating_sub(I32F32::from_num(1.0)),
)))
.saturating_div(consensus_low.saturating_sub(consensus_high));
let a =
(safe_ln((I32F32::from_num(1.0) / alpha_high).saturating_sub(I32F32::from_num(1.0)))
.saturating_sub(safe_ln(
(I32F32::from_num(1.0) / alpha_low).saturating_sub(I32F32::from_num(1.0)),
)))
.saturating_div(consensus_low.saturating_sub(consensus_high));
log::trace!("a: {:?}", a);

// Calculate the intercept 'b' of the logistic function.
// b = ln((1 / alpha_low - 1)) + a * consensus_low
let b = safe_ln(
I32F32::from_num(1.0)
.saturating_div(alpha_low)
.saturating_sub(I32F32::from_num(1.0)),
)
.saturating_add(a.saturating_mul(consensus_low));
let b = safe_ln((I32F32::from_num(1.0) / alpha_low).saturating_sub(I32F32::from_num(1.0)))
.saturating_add(a.saturating_mul(consensus_low));
log::trace!("b: {:?}", b);

// Return the calculated slope 'a' and intercept 'b'.
Expand Down Expand Up @@ -1082,9 +1073,12 @@ impl<T: Config> Pallet<T> {
// Calculate the 75th percentile (high) and 25th percentile (low) of the consensus values.
let consensus_high = quantile(&consensus, 0.75);
let consensus_low = quantile(&consensus, 0.25);

// consensus_low < 0 ||
// consensus_high <= consensus_low ||
// alpha_low < 0 ||
// alpha_high <= alpha_low
// Further check if the high and low consensus values meet the required conditions.
if (consensus_high > consensus_low) && consensus_high != 0 && consensus_low != 0 {
if (consensus_high > consensus_low) || consensus_high != 0 || consensus_low < 0 {
// if (consensus_high > consensus_low) || consensus_high != 0) || consensus_low != 0 {
// if (consensus_high > consensus_low) || consensus_low != 0 {
log::trace!("Using Liquid Alpha");
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ pub mod pallet {
/// -- ITEM (switches liquid alpha on)
#[pallet::type_value]
pub fn DefaultLiquidAlpha<T: Config>() -> bool {
return false;
false
}
#[pallet::storage] // --- MAP ( netuid ) --> Whether or not Liquid Alpha is enabled
pub type LiquidAlphaOn<T> =
Expand Down
1 change: 0 additions & 1 deletion pallets/subtensor/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use sp_std::cmp::Ordering;
use sp_std::vec;
use substrate_fixed::transcendental::{exp, ln};
use substrate_fixed::types::{I32F32, I64F64};
use crate::alloc::borrow::ToOwned;

// TODO: figure out what cfg gate this needs to not be a warning in rustc
#[allow(unused)]
Expand Down
Loading

0 comments on commit c7dc69b

Please sign in to comment.