Skip to content

Commit 39b4fae

Browse files
committed
move set_alpha extrinsic to admin-utils
1 parent ccddd7b commit 39b4fae

File tree

8 files changed

+163
-48
lines changed

8 files changed

+163
-48
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,14 @@ pub mod pallet {
10221022
);
10231023
Ok(())
10241024
}
1025+
1026+
/// Sets values for liquid alpha
1027+
#[pallet::call_index(51)]
1028+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
1029+
pub fn sudo_set_alpha_values(origin: OriginFor<T>, netuid: u16, alpha_low: u16, alpha_high: u16) -> DispatchResult {
1030+
T::Subtensor::ensure_subnet_owner_or_root(origin.clone(), netuid)?;
1031+
T::Subtensor::do_set_alpha_values(origin, netuid, alpha_low, alpha_high)
1032+
}
10251033
}
10261034
}
10271035

@@ -1118,4 +1126,5 @@ pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin> {
11181126
fn set_commit_reveal_weights_interval(netuid: u16, interval: u64);
11191127
fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool);
11201128
fn set_liquid_alpha_enabled(netuid: u16, enabled: bool);
1129+
fn do_set_alpha_values(origin: RuntimeOrigin, netuid: u16, alpha_low: u16, alpha_high: u16) -> Result<(), DispatchError>;
11211130
}

pallets/admin-utils/tests/mock.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,9 @@ impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> f
471471
fn set_liquid_alpha_enabled(netuid: u16, enabled: bool) {
472472
SubtensorModule::set_liquid_alpha_enabled(netuid, enabled);
473473
}
474+
fn do_set_alpha_values(origin: RuntimeOrigin, netuid: u16, alpha_low: u16, alpha_high: u16) -> Result<(), DispatchError> {
475+
SubtensorModule::do_set_alpha_values(origin, netuid, alpha_low, alpha_high)
476+
}
474477
}
475478

476479
impl pallet_admin_utils::Config for Test {

pallets/admin-utils/tests/tests.rs

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use frame_support::assert_ok;
1+
use frame_support::{assert_ok, assert_err, dispatch::{DispatchClass, GetDispatchInfo, Pays}};
22
use frame_support::sp_runtime::DispatchError;
33
use frame_system::Config;
44
use pallet_admin_utils::Error;
55
use pallet_subtensor::Error as SubtensorError;
6-
use pallet_subtensor::Event;
6+
use pallet_subtensor::{migration, Event};
77
use sp_core::U256;
88

99
mod mock;
@@ -1196,3 +1196,122 @@ fn test_sudo_set_liquid_alpha_enabled() {
11961196
assert_eq!(enabled, SubtensorModule::get_liquid_alpha_enabled(netuid));
11971197
});
11981198
}
1199+
1200+
#[test]
1201+
fn test_set_alpha_values_dispatch_info_ok() {
1202+
new_test_ext().execute_with(|| {
1203+
let netuid: u16 = 1;
1204+
let alpha_low: u16 = 12_u16;
1205+
let alpha_high: u16 = u16::MAX - 10;
1206+
let call = RuntimeCall::AdminUtils(pallet_admin_utils::Call::sudo_set_alpha_values {
1207+
netuid,
1208+
alpha_low,
1209+
alpha_high,
1210+
});
1211+
1212+
let dispatch_info = call.get_dispatch_info();
1213+
1214+
assert_eq!(dispatch_info.class, DispatchClass::Operational);
1215+
assert_eq!(dispatch_info.pays_fee, Pays::No);
1216+
});
1217+
}
1218+
1219+
#[test]
1220+
fn test_sudo_get_set_alpha() {
1221+
new_test_ext().execute_with(|| {
1222+
let netuid: u16 = 1;
1223+
let alpha_low: u16 = 12_u16;
1224+
let alpha_high: u16 = u16::MAX - 10;
1225+
1226+
let hotkey: U256 = U256::from(1);
1227+
let coldkey: U256 = U256::from(1 + 456);
1228+
let signer = <<Test as Config>::RuntimeOrigin>::signed(coldkey.clone());
1229+
1230+
// Enable Liquid Alpha and setup
1231+
SubtensorModule::set_liquid_alpha_enabled(netuid, true);
1232+
migration::migrate_create_root_network::<Test>();
1233+
SubtensorModule::add_balance_to_coldkey_account(
1234+
&coldkey,
1235+
1_000_000_000_000_000,
1236+
);
1237+
assert_ok!(SubtensorModule::root_register(
1238+
signer.clone(),
1239+
hotkey,
1240+
));
1241+
assert_ok!(SubtensorModule::add_stake(
1242+
signer.clone(),
1243+
hotkey,
1244+
1000
1245+
));
1246+
1247+
// Should fail as signer does not own the subnet
1248+
assert_err!(
1249+
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high),
1250+
DispatchError::BadOrigin
1251+
);
1252+
1253+
assert_ok!(SubtensorModule::register_network(
1254+
signer.clone()
1255+
));
1256+
1257+
assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
1258+
let (grabbed_alpha_low, grabbed_alpha_high): (u16, u16) = SubtensorModule::get_alpha_values(netuid);
1259+
1260+
log::info!("alpha_low: {:?} alpha_high: {:?}", grabbed_alpha_low, grabbed_alpha_high);
1261+
assert_eq!(grabbed_alpha_low, alpha_low);
1262+
assert_eq!(grabbed_alpha_high, alpha_high);
1263+
1264+
// Convert the u16 values to decimal values
1265+
fn unnormalize_u16_to_float(normalized_value: u16) -> f32 {
1266+
const MAX_U16: u16 = 65535;
1267+
normalized_value as f32 / MAX_U16 as f32
1268+
}
1269+
1270+
let alpha_low_decimal = unnormalize_u16_to_float(alpha_low);
1271+
let alpha_high_decimal = unnormalize_u16_to_float(alpha_high);
1272+
1273+
let (alpha_low_32, alpha_high_32) = SubtensorModule::get_alpha_values_32(netuid);
1274+
1275+
let tolerance: f32 = 1e-6; // 0.000001
1276+
1277+
// Check if the values are equal to the sixth decimal
1278+
assert!((alpha_low_32.to_num::<f32>() - alpha_low_decimal).abs() < tolerance, "alpha_low mismatch: {} != {}", alpha_low_32.to_num::<f32>(), alpha_low_decimal);
1279+
assert!((alpha_high_32.to_num::<f32>() - alpha_high_decimal).abs() < tolerance, "alpha_high mismatch: {} != {}", alpha_high_32.to_num::<f32>(), alpha_high_decimal);
1280+
1281+
// 1. Liquid alpha disabled
1282+
SubtensorModule::set_liquid_alpha_enabled(netuid, false);
1283+
assert_err!(
1284+
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high),
1285+
SubtensorError::<Test>::LiquidAlphaDisabled
1286+
);
1287+
// Correct scenario after error
1288+
SubtensorModule::set_liquid_alpha_enabled(netuid, true); // Re-enable for further tests
1289+
assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
1290+
1291+
// 2. Alpha high too low
1292+
let alpha_high_too_low = (u16::MAX as u32 * 4 / 5) as u16 - 1; // One less than the minimum acceptable value
1293+
assert_err!(
1294+
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high_too_low),
1295+
SubtensorError::<Test>::AlphaHighTooLow
1296+
);
1297+
// Correct scenario after error
1298+
assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
1299+
1300+
// 3. Alpha low too low or too high
1301+
let alpha_low_too_low = 0_u16;
1302+
assert_err!(
1303+
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low_too_low, alpha_high),
1304+
SubtensorError::<Test>::AlphaLowOutOfRange
1305+
);
1306+
// Correct scenario after error
1307+
assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
1308+
1309+
let alpha_low_too_high = (u16::MAX as u32 * 4 / 5) as u16 + 1; // One more than the maximum acceptable value
1310+
assert_err!(
1311+
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low_too_high, alpha_high),
1312+
SubtensorError::<Test>::AlphaLowOutOfRange
1313+
);
1314+
// Correct scenario after error
1315+
assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
1316+
});
1317+
}

pallets/subtensor/src/epoch.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1204,10 +1204,17 @@ impl<T: Config> Pallet<T> {
12041204
// -- 5. Ensure alpha low is within range
12051205
ensure!(
12061206
alpha_low > 0 && alpha_low < min_alpha_high,
1207-
Error::<T>::AlphaLowTooLow
1207+
Error::<T>::AlphaLowOutOfRange
12081208
);
12091209

12101210
AlphaValues::<T>::insert(netuid, (alpha_low, alpha_high));
1211+
1212+
log::info!(
1213+
"AlphaValuesSet( netuid: {:?}, AlphaLow: {:?}, AlphaHigh: {:?} ) ",
1214+
netuid,
1215+
alpha_low,
1216+
alpha_high,
1217+
);
12111218
Ok(())
12121219
}
12131220
}

pallets/subtensor/src/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ mod errors {
128128
CommitRevealDisabled,
129129
/// Attempting to set alpha high/low while disabled
130130
LiquidAlphaDisabled,
131-
/// Alpha high is too low
131+
/// Alpha high is too low: alpha_high > 0.8
132132
AlphaHighTooLow,
133-
/// Alpha low is too low
134-
AlphaLowTooLow,
133+
/// Alpha low is out of range: alpha_low > 0 && alpha_low < 0.8
134+
AlphaLowOutOfRange,
135135
}
136136
}

pallets/subtensor/src/lib.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,15 +2078,6 @@ pub mod pallet {
20782078
pub fn dissolve_network(origin: OriginFor<T>, netuid: u16) -> DispatchResult {
20792079
Self::user_remove_network(origin, netuid)
20802080
}
2081-
2082-
/// Sets values for liquid alpha
2083-
#[pallet::call_index(88)]
2084-
#[pallet::weight((Weight::from_parts(119_000_000, 0)
2085-
.saturating_add(T::DbWeight::get().reads(0))
2086-
.saturating_add(T::DbWeight::get().writes(0)), DispatchClass::Operational, Pays::No))]
2087-
pub fn set_alpha_values(origin: OriginFor<T>, netuid: u16, alpha_low: u16, alpha_high: u16) -> DispatchResult {
2088-
Self::do_set_alpha_values(origin, netuid, alpha_low, alpha_high)
2089-
}
20902081
}
20912082

20922083
// ---- Subtensor helper functions.

pallets/subtensor/tests/epoch.rs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::mock::*;
2-
use frame_support::{dispatch::{DispatchClass, GetDispatchInfo, Pays}, assert_err, assert_ok};
2+
use frame_support::{assert_err, assert_ok};
33
use frame_system::Config;
44
use pallet_subtensor::*;
55
use rand::{distributions::Uniform, rngs::StdRng, seq::SliceRandom, thread_rng, Rng, SeedableRng};
@@ -1510,12 +1510,12 @@ fn test_set_alpha_disabled() {
15101510
// Explicitly set to false
15111511
SubtensorModule::set_liquid_alpha_enabled(netuid, false);
15121512
assert_err!(
1513-
SubtensorModule::set_alpha_values(signer.clone(), netuid, 12_u16, u16::MAX),
1513+
SubtensorModule::do_set_alpha_values(signer.clone(), netuid, 12_u16, u16::MAX),
15141514
Error::<Test>::LiquidAlphaDisabled
15151515
);
15161516

15171517
SubtensorModule::set_liquid_alpha_enabled(netuid, true);
1518-
assert_ok!(SubtensorModule::set_alpha_values(signer.clone(), netuid, 12_u16, u16::MAX));
1518+
assert_ok!(SubtensorModule::do_set_alpha_values(signer.clone(), netuid, 12_u16, u16::MAX));
15191519
});
15201520
}
15211521

@@ -2555,15 +2555,15 @@ fn test_get_set_alpha() {
25552555

25562556
// Should fail as signer does not own the subnet
25572557
assert_err!(
2558-
SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high),
2558+
SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high),
25592559
DispatchError::BadOrigin
25602560
);
25612561

25622562
assert_ok!(SubtensorModule::register_network(
25632563
signer.clone()
25642564
));
25652565

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

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

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

26092609
// 3. Alpha low too low or too high
26102610
let alpha_low_too_low = 0_u16;
26112611
assert_err!(
2612-
SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low_too_low, alpha_high),
2613-
Error::<Test>::AlphaLowTooLow
2612+
SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low_too_low, alpha_high),
2613+
Error::<Test>::AlphaLowOutOfRange
26142614
);
26152615
// Correct scenario after error
2616-
assert_ok!(SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
2616+
assert_ok!(SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
26172617

26182618
let alpha_low_too_high = (u16::MAX as u32 * 4 / 5) as u16 + 1; // One more than the maximum acceptable value
26192619
assert_err!(
2620-
SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low_too_high, alpha_high),
2621-
Error::<Test>::AlphaLowTooLow
2620+
SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low_too_high, alpha_high),
2621+
Error::<Test>::AlphaLowOutOfRange
26222622
);
26232623
// Correct scenario after error
2624-
assert_ok!(SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
2625-
});
2626-
}
2627-
2628-
#[test]
2629-
fn test_set_alpha_values_dispatch_info_ok() {
2630-
new_test_ext(0).execute_with(|| {
2631-
let netuid: u16 = 1;
2632-
let alpha_low: u16 = 12_u16;
2633-
let alpha_high: u16 = u16::MAX - 10;
2634-
let call = RuntimeCall::SubtensorModule(SubtensorCall::set_alpha_values {
2635-
netuid,
2636-
alpha_low,
2637-
alpha_high,
2638-
});
2639-
let dispatch_info = call.get_dispatch_info();
2640-
2641-
assert_eq!(dispatch_info.class, DispatchClass::Operational);
2642-
assert_eq!(dispatch_info.pays_fee, Pays::No);
2624+
assert_ok!(SubtensorModule::do_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
26432625
});
26442626
}
26452627

runtime/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,10 @@ impl
11521152
fn set_liquid_alpha_enabled(netuid: u16, enabled: bool) {
11531153
SubtensorModule::set_liquid_alpha_enabled(netuid, enabled);
11541154
}
1155+
1156+
fn do_set_alpha_values(origin: RuntimeOrigin, netuid: u16, alpha_low: u16, alpha_high: u16) -> Result<(), DispatchError> {
1157+
SubtensorModule::do_set_alpha_values(origin, netuid, alpha_low, alpha_high)
1158+
}
11551159
}
11561160

11571161
impl pallet_admin_utils::Config for Runtime {

0 commit comments

Comments
 (0)