Skip to content

Commit 5487aeb

Browse files
author
Samuel Dare
committed
feat: use u16:MAX norm , update tests
1 parent ed87610 commit 5487aeb

File tree

10 files changed

+183
-152
lines changed

10 files changed

+183
-152
lines changed

pallets/admin-utils/tests/mock.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ frame_support::construct_runtime!(
2222
System: frame_system,
2323
Balances: pallet_balances,
2424
AdminUtils: pallet_admin_utils,
25-
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>},
25+
SubtensorModule: pallet_subtensor::{Pallet, Call, Storage, Event<T>, Error<T>},
2626
}
2727
);
2828

@@ -108,8 +108,8 @@ parameter_types! {
108108
pub const InitialSubnetLimit: u16 = 10; // Max 10 subnets.
109109
pub const InitialNetworkRateLimit: u64 = 0;
110110
pub const InitialTargetStakesPerInterval: u16 = 1;
111-
pub const InitialAlphaHigh: u16 = 900; // Represents 0.9 as per the production default
112-
pub const InitialAlphaLow: u16 = 700; // Represents 0.7 as per the production default
111+
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
112+
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
113113
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
114114
}
115115

pallets/admin-utils/tests/tests.rs

Lines changed: 148 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use frame_system::Config;
44
use pallet_admin_utils::Error;
55
use pallet_subtensor::Event;
66
use sp_core::U256;
7-
use substrate_fixed::types::I32F32;
7+
use pallet_subtensor::Error as SubtensorError;
8+
89

910
mod mock;
1011
use mock::*;
@@ -1180,65 +1181,156 @@ fn test_sudo_set_target_stakes_per_interval() {
11801181
});
11811182
}
11821183

1183-
#[test]
1184-
fn test_sudo_set_alpha_high() {
1185-
new_test_ext().execute_with(|| {
1186-
let netuid: u16 = 1;
1187-
let to_be_set: u16 = 10;
1188-
let init_value = SubtensorModule::get_alpha_high(netuid);
1189-
assert_eq!(
1190-
AdminUtils::sudo_set_alpha_high(
1191-
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
1184+
1185+
#[test]
1186+
fn alpha_low_can_only_be_called_by_admin() {
1187+
new_test_ext().execute_with(|| {
1188+
let netuid: u16 = 1;
1189+
let to_be_set: u16 = 52428; // 0.8 i.e. 0.8 x u16::MAX
1190+
assert_eq!(
1191+
AdminUtils::sudo_set_alpha_low(
1192+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
1193+
netuid,
1194+
to_be_set
1195+
),
1196+
Err(DispatchError::BadOrigin)
1197+
);
1198+
});
1199+
}
1200+
1201+
#[test]
1202+
fn sets_alpha_low_valid_value() {
1203+
new_test_ext().execute_with(|| {
1204+
let netuid: u16 = 1;
1205+
let to_be_set: u16 = 52428; // 0.8 i.e. 0.8 x u16::MAX
1206+
let init_value = SubtensorModule::get_alpha_low(netuid);
1207+
assert_eq!(SubtensorModule::get_alpha_low(netuid), init_value);
1208+
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
1209+
<<Test as Config>::RuntimeOrigin>::root(),
1210+
netuid,
1211+
true,
1212+
));
1213+
assert_ok!(AdminUtils::sudo_set_alpha_low(
1214+
<<Test as Config>::RuntimeOrigin>::root(),
11921215
netuid,
11931216
to_be_set
1194-
),
1195-
Err(DispatchError::BadOrigin)
1196-
);
1197-
assert_eq!(SubtensorModule::get_alpha_high(netuid), init_value);
1198-
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
1199-
<<Test as Config>::RuntimeOrigin>::root(),
1200-
netuid,
1201-
true,
1202-
));
1203-
assert_ok!(AdminUtils::sudo_set_alpha_high(
1204-
<<Test as Config>::RuntimeOrigin>::root(),
1205-
netuid,
1206-
to_be_set
1207-
));
1208-
let expected_value: I32F32 = I32F32::from_num(to_be_set as f64 / 1000.0);
1209-
assert_eq!(SubtensorModule::get_alpha_high(netuid), expected_value);
1210-
});
1211-
}
1217+
));
1218+
assert_eq!(SubtensorModule::get_alpha_low(netuid), to_be_set);
1219+
});
1220+
}
12121221

1213-
#[test]
1214-
fn test_sudo_set_alpha_low() {
1215-
new_test_ext().execute_with(|| {
1216-
let netuid: u16 = 1;
1217-
let to_be_set: u16 = 10;
1218-
let init_value = SubtensorModule::get_alpha_low(netuid);
1219-
assert_eq!(
1220-
AdminUtils::sudo_set_alpha_low(
1221-
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
1222+
#[test]
1223+
fn alpha_low_fails_if_liquid_alpha_disabled() {
1224+
new_test_ext().execute_with(|| {
1225+
let netuid: u16 = 1;
1226+
let to_be_set: u16 = 52428; // 0.8 i.e. 0.8 x u16::MAX
1227+
assert_eq!(
1228+
AdminUtils::sudo_set_alpha_low(
1229+
<<Test as Config>::RuntimeOrigin>::root(),
1230+
netuid,
1231+
to_be_set
1232+
),
1233+
Err(SubtensorError::<Test>::LiquidAlphaDisabled.into())
1234+
);
1235+
});
1236+
}
1237+
1238+
#[test]
1239+
fn alpha_low_fails_if_alpha_low_too_low() {
1240+
new_test_ext().execute_with(|| {
1241+
let netuid: u16 = 1;
1242+
let to_be_set: u16 = 0; // Invalid value
1243+
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
1244+
<<Test as Config>::RuntimeOrigin>::root(),
12221245
netuid,
1223-
to_be_set
1224-
),
1225-
Err(DispatchError::BadOrigin)
1226-
);
1227-
assert_eq!(SubtensorModule::get_alpha_low(netuid), init_value);
1228-
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
1229-
<<Test as Config>::RuntimeOrigin>::root(),
1230-
netuid,
1231-
true,
1232-
));
1233-
assert_ok!(AdminUtils::sudo_set_alpha_low(
1234-
<<Test as Config>::RuntimeOrigin>::root(),
1235-
netuid,
1236-
to_be_set
1237-
));
1238-
let expected_value: I32F32 = I32F32::from_num(to_be_set as f64 / 1000.0);
1239-
assert_eq!(SubtensorModule::get_alpha_low(netuid), expected_value);
1240-
});
1241-
}
1246+
true,
1247+
));
1248+
assert_eq!(
1249+
AdminUtils::sudo_set_alpha_low(
1250+
<<Test as Config>::RuntimeOrigin>::root(),
1251+
netuid,
1252+
to_be_set
1253+
),
1254+
Err(SubtensorError::<Test>::AlphaLowTooLow.into())
1255+
);
1256+
});
1257+
}
1258+
1259+
1260+
#[test]
1261+
fn alpha_high_can_only_be_called_by_admin() {
1262+
new_test_ext().execute_with(|| {
1263+
let netuid: u16 = 1;
1264+
let to_be_set: u16 = 60000; // Valid value
1265+
assert_eq!(
1266+
AdminUtils::sudo_set_alpha_high(
1267+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
1268+
netuid,
1269+
to_be_set
1270+
),
1271+
Err(DispatchError::BadOrigin)
1272+
);
1273+
});
1274+
}
1275+
1276+
#[test]
1277+
fn sets_a_valid_value() {
1278+
new_test_ext().execute_with(|| {
1279+
let netuid: u16 = 1;
1280+
let to_be_set: u16 = 60000; // Valid value
1281+
let init_value = SubtensorModule::get_alpha_high(netuid);
1282+
assert_eq!(SubtensorModule::get_alpha_high(netuid), init_value);
1283+
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
1284+
<<Test as Config>::RuntimeOrigin>::root(),
1285+
netuid,
1286+
true,
1287+
));
1288+
assert_ok!(AdminUtils::sudo_set_alpha_high(
1289+
<<Test as Config>::RuntimeOrigin>::root(),
1290+
netuid,
1291+
to_be_set
1292+
));
1293+
assert_eq!(SubtensorModule::get_alpha_high(netuid), to_be_set);
1294+
});
1295+
}
1296+
1297+
#[test]
1298+
fn alpha_high_fails_if_liquid_alpha_disabled() {
1299+
new_test_ext().execute_with(|| {
1300+
let netuid: u16 = 1;
1301+
let to_be_set: u16 = 60000; // Valid value
1302+
assert_eq!(
1303+
AdminUtils::sudo_set_alpha_high(
1304+
<<Test as Config>::RuntimeOrigin>::root(),
1305+
netuid,
1306+
to_be_set
1307+
),
1308+
Err(SubtensorError::<Test>::LiquidAlphaDisabled.into())
1309+
);
1310+
});
1311+
}
1312+
1313+
#[test]
1314+
fn fails_if_alpha_high_too_low() {
1315+
new_test_ext().execute_with(|| {
1316+
let netuid: u16 = 1;
1317+
let to_be_set: u16 = 50000; // Invalid value, less than 52428
1318+
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
1319+
<<Test as Config>::RuntimeOrigin>::root(),
1320+
netuid,
1321+
true,
1322+
));
1323+
assert_eq!(
1324+
AdminUtils::sudo_set_alpha_high(
1325+
<<Test as Config>::RuntimeOrigin>::root(),
1326+
netuid,
1327+
to_be_set
1328+
),
1329+
Err(SubtensorError::<Test>::AlphaHighTooLow.into())
1330+
);
1331+
});
1332+
}
1333+
12421334

12431335
#[test]
12441336
fn test_sudo_set_liquid_alpha_enabled() {

pallets/subtensor/src/epoch.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,9 +1090,9 @@ impl<T: Config> Pallet<T> {
10901090
log::trace!("Using Liquid Alpha");
10911091

10921092
// Get the high and low alpha values for the network.
1093-
let alpha_high = Self::get_alpha_high(netuid);
1093+
let alpha_high = Self::get_alpha_high_32(netuid);
10941094
log::trace!("alpha_high: {:?}", alpha_high);
1095-
let alpha_low = Self::get_alpha_low(netuid);
1095+
let alpha_low = Self::get_alpha_low_32(netuid);
10961096
log::trace!("alpha_low: {:?}", alpha_low);
10971097

10981098
// Calculate the logistic function parameters 'a' and 'b' based on alpha and consensus values.
@@ -1159,9 +1159,9 @@ impl<T: Config> Pallet<T> {
11591159
log::trace!("Using Liquid Alpha");
11601160

11611161
// Get the high and low alpha values for the network.
1162-
let alpha_high = Self::get_alpha_high(netuid);
1162+
let alpha_high = Self::get_alpha_high_32(netuid);
11631163
log::trace!("alpha_high: {:?}", alpha_high);
1164-
let alpha_low = Self::get_alpha_low(netuid);
1164+
let alpha_low = Self::get_alpha_low_32(netuid);
11651165
log::trace!("alpha_low: {:?}", alpha_low);
11661166

11671167
// Calculate the logistic function parameters 'a' and 'b' based on alpha and consensus values.

pallets/subtensor/src/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,9 @@ mod errors {
128128
CommitRevealDisabled,
129129
/// Attempting to set alpha high/low while disabled
130130
LiquidAlphaDisabled,
131+
/// Alpha high is too low
132+
AlphaHighTooLow,
133+
/// Alpha low is too low
134+
AlphaLowTooLow,
131135
}
132136
}

pallets/subtensor/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ pub mod pallet {
376376
/// -- ITEM (switches liquid alpha on)
377377
#[pallet::type_value]
378378
pub fn DefaultLiquidAlpha<T: Config>() -> bool {
379-
false
379+
return false;
380380
}
381381
#[pallet::storage] // --- MAP ( netuid ) --> Whether or not Liquid Alpha is enabled
382382
pub type LiquidAlphaOn<T> =
@@ -867,12 +867,12 @@ pub mod pallet {
867867
868868
#[pallet::type_value]
869869
pub fn DefaultAlphaHigh<T: Config>() -> u16 {
870-
900 // Represents 0.9
870+
58982 // Represents 0.9 as per the production default
871871
}
872872
/// Provides the default value for the lower bound of the alpha parameter.
873873
#[pallet::type_value]
874874
pub fn DefaultAlphaLow<T: Config>() -> u16 {
875-
700 // Represents 0.7
875+
45875 // Represents 0.7 as per the production default
876876
}
877877

878878
#[pallet::storage] // ITEM( weights_min_stake )

pallets/subtensor/src/math.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// we get a compiler warning for this , even though the trait is used in the
22
// quantile function.
3+
use crate::alloc::borrow::ToOwned;
34
#[allow(unused)]
45
use num_traits::float::Float;
56
use sp_runtime::traits::CheckedAdd;

pallets/subtensor/src/subnet_info.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ impl<T: Config> Pallet<T> {
158158
let difficulty = Self::get_difficulty_as_u64(netuid);
159159
let commit_reveal_weights_interval = Self::get_commit_reveal_weights_interval(netuid);
160160
let commit_reveal_weights_enabled = Self::get_commit_reveal_weights_enabled(netuid);
161-
let alpha_high = AlphaHigh::<T>::get(netuid);
162-
let alpha_low = AlphaLow::<T>::get(netuid);
163-
let liquid_alpha_enabled = LiquidAlphaOn::<T>::get(netuid);
161+
let alpha_high = Self::get_alpha_high(netuid);
162+
let alpha_low = Self::get_alpha_low(netuid);
163+
let liquid_alpha_enabled = Self::get_liquid_alpha_enabled(netuid);
164164

165165
Some(SubnetHyperparams {
166166
rho: rho.into(),

pallets/subtensor/src/utils.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,29 +663,41 @@ impl<T: Config> Pallet<T> {
663663
NominatorMinRequiredStake::<T>::put(min_stake);
664664
}
665665

666-
pub fn get_alpha_high(netuid: u16) -> I32F32 {
666+
pub fn get_alpha_high(netuid: u16) -> u16 {
667+
AlphaHigh::<T>::get(netuid)
668+
}
669+
670+
pub fn get_alpha_high_32(netuid: u16) -> I32F32 {
667671
I32F32::from_num(AlphaHigh::<T>::get(netuid) as f64 / 1000.0)
668672
}
669673

670674
pub fn set_alpha_high(netuid: u16, alpha_high: u16) -> Result<(), DispatchError> {
675+
// Ensure liquid alpha is enabled
671676
ensure!(
672677
Self::get_liquid_alpha_enabled(netuid),
673678
Error::<T>::LiquidAlphaDisabled
674679
);
680+
// Ensure alpha high is greater than the minimum
681+
ensure!(alpha_high >= 52428, Error::<T>::AlphaHighTooLow);
675682
AlphaHigh::<T>::insert(netuid, alpha_high);
676683

677684
Ok(())
678685
}
679686

680-
pub fn get_alpha_low(netuid: u16) -> I32F32 {
681-
I32F32::from_num(AlphaLow::<T>::get(netuid) as f64 / 1000.0)
687+
pub fn get_alpha_low(netuid: u16) -> u16 {
688+
AlphaLow::<T>::get(netuid)
689+
}
690+
691+
pub fn get_alpha_low_32(netuid: u16) -> I32F32 {
692+
I32F32::from_num(AlphaLow::<T>::get(netuid))
682693
}
683694

684695
pub fn set_alpha_low(netuid: u16, alpha_low: u16) -> Result<(), DispatchError> {
685696
ensure!(
686697
Self::get_liquid_alpha_enabled(netuid),
687698
Error::<T>::LiquidAlphaDisabled
688699
);
700+
ensure!(alpha_low > 0 || alpha_low == 52428, Error::<T>::AlphaLowTooLow);
689701
AlphaLow::<T>::insert(netuid, alpha_low);
690702

691703
Ok(())

0 commit comments

Comments
 (0)