@@ -4,7 +4,8 @@ use frame_system::Config;
44use pallet_admin_utils:: Error ;
55use pallet_subtensor:: Event ;
66use sp_core:: U256 ;
7- use substrate_fixed:: types:: I32F32 ;
7+ use pallet_subtensor:: Error as SubtensorError ;
8+
89
910mod mock;
1011use 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]
12441336fn test_sudo_set_liquid_alpha_enabled ( ) {
0 commit comments