Skip to content

Commit d01cd1c

Browse files
committed
unite alpha getters/setters & add extrinsic
Combines the getters and setters for alpha low/high Removes alpha high/low hyperparams adds an extrinsic to set alpha high/low adds alpha values to subnetinfo
1 parent de9c54a commit d01cd1c

File tree

9 files changed

+144
-298
lines changed

9 files changed

+144
-298
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -997,56 +997,6 @@ pub mod pallet {
997997
Ok(())
998998
}
999999

1000-
/// Sets the lower bound for the alpha parameter for a given subnet.
1001-
///
1002-
/// # Parameters
1003-
/// - `origin`: The origin of the call, which must be the root account or subnet owner.
1004-
/// - `netuid`: The unique identifier for the subnet.
1005-
/// - `alpha_low`: The new lower bound value for the alpha parameter.
1006-
///
1007-
/// # Weight
1008-
/// This function has a fixed weight of 0 and is classified as an operational transaction that does not incur any fees.
1009-
#[pallet::call_index(50)]
1010-
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
1011-
pub fn sudo_set_alpha_low(
1012-
origin: OriginFor<T>,
1013-
netuid: u16,
1014-
alpha_low: u16,
1015-
) -> DispatchResult {
1016-
T::Subtensor::ensure_subnet_owner_or_root(origin, netuid)?;
1017-
T::Subtensor::set_alpha_low(netuid, alpha_low)?;
1018-
log::info!(
1019-
"AlphaLowSet( netuid: {:?}, alpha_low: {:?} ) ",
1020-
netuid,
1021-
alpha_low
1022-
);
1023-
Ok(())
1024-
}
1025-
/// Sets the upper bound for the alpha parameter for a given subnet.
1026-
///
1027-
/// # Parameters
1028-
/// - `origin`: The origin of the call, which must be the root account or subnet owner.
1029-
/// - `netuid`: The unique identifier for the subnet.
1030-
/// - `alpha_high`: The new upper bound value for the alpha parameter.
1031-
///
1032-
/// # Weight
1033-
/// This function has a fixed weight of 0 and is classified as an operational transaction that does not incur any fees.
1034-
#[pallet::call_index(51)]
1035-
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
1036-
pub fn sudo_set_alpha_high(
1037-
origin: OriginFor<T>,
1038-
netuid: u16,
1039-
alpha_high: u16,
1040-
) -> DispatchResult {
1041-
T::Subtensor::ensure_subnet_owner_or_root(origin, netuid)?;
1042-
T::Subtensor::set_alpha_high(netuid, alpha_high)?;
1043-
log::info!(
1044-
"AlphaHighSet( netuid: {:?}, alpha_high: {:?} ) ",
1045-
netuid,
1046-
alpha_high
1047-
);
1048-
Ok(())
1049-
}
10501000
/// Enables or disables Liquid Alpha for a given subnet.
10511001
///
10521002
/// # Parameters
@@ -1056,7 +1006,7 @@ pub mod pallet {
10561006
///
10571007
/// # Weight
10581008
/// This function has a fixed weight of 0 and is classified as an operational transaction that does not incur any fees.
1059-
#[pallet::call_index(52)]
1009+
#[pallet::call_index(50)]
10601010
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
10611011
pub fn sudo_set_liquid_alpha_enabled(
10621012
origin: OriginFor<T>,
@@ -1167,7 +1117,5 @@ pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin> {
11671117
fn set_target_stakes_per_interval(target_stakes_per_interval: u64);
11681118
fn set_commit_reveal_weights_interval(netuid: u16, interval: u64);
11691119
fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool);
1170-
fn set_alpha_high(netuid: u16, alpha_high: u16) -> Result<(), DispatchError>;
1171-
fn set_alpha_low(netuid: u16, alpha_low: u16) -> Result<(), DispatchError>;
11721120
fn set_liquid_alpha_enabled(netuid: u16, enabled: bool);
11731121
}

pallets/admin-utils/tests/mock.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -468,14 +468,6 @@ impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> f
468468
SubtensorModule::set_commit_reveal_weights_enabled(netuid, enabled);
469469
}
470470

471-
fn set_alpha_high(netuid: u16, alpha_high: u16) -> Result<(), DispatchError> {
472-
SubtensorModule::set_alpha_high(netuid, alpha_high)
473-
}
474-
475-
fn set_alpha_low(netuid: u16, alpha_low: u16) -> Result<(), DispatchError> {
476-
SubtensorModule::set_alpha_low(netuid, alpha_low)
477-
}
478-
479471
fn set_liquid_alpha_enabled(netuid: u16, enabled: bool) {
480472
SubtensorModule::set_liquid_alpha_enabled(netuid, enabled);
481473
}

pallets/admin-utils/tests/tests.rs

Lines changed: 0 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,154 +1180,6 @@ fn test_sudo_set_target_stakes_per_interval() {
11801180
});
11811181
}
11821182

1183-
#[test]
1184-
fn alpha_low_can_only_be_called_by_admin() {
1185-
new_test_ext().execute_with(|| {
1186-
let netuid: u16 = 1;
1187-
let to_be_set: u16 = 52428; // 0.8 i.e. 0.8 x u16::MAX
1188-
assert_eq!(
1189-
AdminUtils::sudo_set_alpha_low(
1190-
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
1191-
netuid,
1192-
to_be_set
1193-
),
1194-
Err(DispatchError::BadOrigin)
1195-
);
1196-
});
1197-
}
1198-
1199-
#[test]
1200-
fn sets_alpha_low_valid_value() {
1201-
new_test_ext().execute_with(|| {
1202-
let netuid: u16 = 1;
1203-
let to_be_set: u16 = 52428; // 0.8 i.e. 0.8 x u16::MAX
1204-
let init_value = SubtensorModule::get_alpha_low(netuid);
1205-
assert_eq!(SubtensorModule::get_alpha_low(netuid), init_value);
1206-
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
1207-
<<Test as Config>::RuntimeOrigin>::root(),
1208-
netuid,
1209-
true,
1210-
));
1211-
assert_ok!(AdminUtils::sudo_set_alpha_low(
1212-
<<Test as Config>::RuntimeOrigin>::root(),
1213-
netuid,
1214-
to_be_set
1215-
));
1216-
assert_eq!(SubtensorModule::get_alpha_low(netuid), to_be_set);
1217-
});
1218-
}
1219-
1220-
#[test]
1221-
fn alpha_low_fails_if_liquid_alpha_disabled() {
1222-
new_test_ext().execute_with(|| {
1223-
let netuid: u16 = 1;
1224-
let to_be_set: u16 = 52428; // 0.8 i.e. 0.8 x u16::MAX
1225-
assert_eq!(
1226-
AdminUtils::sudo_set_alpha_low(
1227-
<<Test as Config>::RuntimeOrigin>::root(),
1228-
netuid,
1229-
to_be_set
1230-
),
1231-
Err(SubtensorError::<Test>::LiquidAlphaDisabled.into())
1232-
);
1233-
});
1234-
}
1235-
1236-
#[test]
1237-
fn alpha_low_fails_if_alpha_low_too_low() {
1238-
new_test_ext().execute_with(|| {
1239-
let netuid: u16 = 1;
1240-
let to_be_set: u16 = 0; // Invalid value
1241-
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
1242-
<<Test as Config>::RuntimeOrigin>::root(),
1243-
netuid,
1244-
true,
1245-
));
1246-
assert_eq!(
1247-
AdminUtils::sudo_set_alpha_low(
1248-
<<Test as Config>::RuntimeOrigin>::root(),
1249-
netuid,
1250-
to_be_set
1251-
),
1252-
Err(SubtensorError::<Test>::AlphaLowTooLow.into())
1253-
);
1254-
});
1255-
}
1256-
1257-
#[test]
1258-
fn alpha_high_can_only_be_called_by_admin() {
1259-
new_test_ext().execute_with(|| {
1260-
let netuid: u16 = 1;
1261-
let to_be_set: u16 = 60000; // Valid value
1262-
assert_eq!(
1263-
AdminUtils::sudo_set_alpha_high(
1264-
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
1265-
netuid,
1266-
to_be_set
1267-
),
1268-
Err(DispatchError::BadOrigin)
1269-
);
1270-
});
1271-
}
1272-
1273-
#[test]
1274-
fn sets_a_valid_value() {
1275-
new_test_ext().execute_with(|| {
1276-
let netuid: u16 = 1;
1277-
let to_be_set: u16 = 60000; // Valid value
1278-
let init_value = SubtensorModule::get_alpha_high(netuid);
1279-
assert_eq!(SubtensorModule::get_alpha_high(netuid), init_value);
1280-
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
1281-
<<Test as Config>::RuntimeOrigin>::root(),
1282-
netuid,
1283-
true,
1284-
));
1285-
assert_ok!(AdminUtils::sudo_set_alpha_high(
1286-
<<Test as Config>::RuntimeOrigin>::root(),
1287-
netuid,
1288-
to_be_set
1289-
));
1290-
assert_eq!(SubtensorModule::get_alpha_high(netuid), to_be_set);
1291-
});
1292-
}
1293-
1294-
#[test]
1295-
fn alpha_high_fails_if_liquid_alpha_disabled() {
1296-
new_test_ext().execute_with(|| {
1297-
let netuid: u16 = 1;
1298-
let to_be_set: u16 = 60000; // Valid value
1299-
assert_eq!(
1300-
AdminUtils::sudo_set_alpha_high(
1301-
<<Test as Config>::RuntimeOrigin>::root(),
1302-
netuid,
1303-
to_be_set
1304-
),
1305-
Err(SubtensorError::<Test>::LiquidAlphaDisabled.into())
1306-
);
1307-
});
1308-
}
1309-
1310-
#[test]
1311-
fn fails_if_alpha_high_too_low() {
1312-
new_test_ext().execute_with(|| {
1313-
let netuid: u16 = 1;
1314-
let to_be_set: u16 = 50000; // Invalid value, less than 52428
1315-
assert_ok!(AdminUtils::sudo_set_liquid_alpha_enabled(
1316-
<<Test as Config>::RuntimeOrigin>::root(),
1317-
netuid,
1318-
true,
1319-
));
1320-
assert_eq!(
1321-
AdminUtils::sudo_set_alpha_high(
1322-
<<Test as Config>::RuntimeOrigin>::root(),
1323-
netuid,
1324-
to_be_set
1325-
),
1326-
Err(SubtensorError::<Test>::AlphaHighTooLow.into())
1327-
);
1328-
});
1329-
}
1330-
13311183
#[test]
13321184
fn test_sudo_set_liquid_alpha_enabled() {
13331185
new_test_ext().execute_with(|| {

pallets/subtensor/src/epoch.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,10 +1082,8 @@ impl<T: Config> Pallet<T> {
10821082
log::trace!("Using Liquid Alpha");
10831083

10841084
// Get the high and low alpha values for the network.
1085-
let alpha_high = Self::get_alpha_high_32(netuid);
1086-
log::trace!("alpha_high: {:?}", alpha_high);
1087-
let alpha_low = Self::get_alpha_low_32(netuid);
1088-
log::trace!("alpha_low: {:?}", alpha_low);
1085+
let (alpha_low, alpha_high): (I32F32, I32F32) = Self::get_alpha_values_32(netuid);
1086+
log::trace!("alpha_low: {:?} alpha_high: {:?}", alpha_low, alpha_high);
10891087

10901088
// Calculate the logistic function parameters 'a' and 'b' based on alpha and consensus values.
10911089
let (a, b) = Self::calculate_logistic_params(
@@ -1151,10 +1149,8 @@ impl<T: Config> Pallet<T> {
11511149
log::trace!("Using Liquid Alpha");
11521150

11531151
// Get the high and low alpha values for the network.
1154-
let alpha_high = Self::get_alpha_high_32(netuid);
1155-
log::trace!("alpha_high: {:?}", alpha_high);
1156-
let alpha_low = Self::get_alpha_low_32(netuid);
1157-
log::trace!("alpha_low: {:?}", alpha_low);
1152+
let (alpha_low, alpha_high): (I32F32, I32F32) = Self::get_alpha_values_32(netuid);
1153+
log::trace!("alpha_low: {:?} alpha_high: {:?}", alpha_low, alpha_high);
11581154

11591155
// Calculate the logistic function parameters 'a' and 'b' based on alpha and consensus values.
11601156
let (a, b) = Self::calculate_logistic_params(
@@ -1185,4 +1181,30 @@ impl<T: Config> Pallet<T> {
11851181
Self::compute_ema_bonds_normal(&bonds_delta, &bonds, netuid)
11861182
}
11871183
}
1184+
1185+
pub fn do_set_alpha_values(origin: T::RuntimeOrigin, netuid: u16, alpha_low: u16, alpha_high: u16) -> Result<(), DispatchError> {
1186+
// --- 1. Ensure the function caller is a signed user.
1187+
let _coldkey = ensure_signed(origin)?;
1188+
1189+
let max_u16: u32 = u16::MAX as u32; // 65535
1190+
let min_alpha_high: u16 = (max_u16.saturating_mul(4).saturating_div(5)) as u16; // 52428
1191+
1192+
// --- 2. Ensure liquid alpha is enabled
1193+
ensure!(
1194+
Self::get_liquid_alpha_enabled(netuid),
1195+
Error::<T>::LiquidAlphaDisabled
1196+
);
1197+
1198+
// --- 3. Ensure alpha high is greater than the minimum
1199+
ensure!(alpha_high >= min_alpha_high, Error::<T>::AlphaHighTooLow);
1200+
1201+
// -- 4. Ensure alpha low is within range
1202+
ensure!(
1203+
alpha_low > 0 && alpha_low < min_alpha_high,
1204+
Error::<T>::AlphaLowTooLow
1205+
);
1206+
AlphaValues::<T>::insert(netuid, (alpha_low, alpha_high));
1207+
1208+
Ok(())
1209+
}
11881210
}

pallets/subtensor/src/lib.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -866,14 +866,9 @@ pub mod pallet {
866866
/// Provides the default value for the upper bound of the alpha parameter.
867867
868868
#[pallet::type_value]
869-
pub fn DefaultAlphaHigh<T: Config>() -> u16 {
870-
58982 // Represents 0.9 as per the production default
871-
}
872-
/// Provides the default value for the lower bound of the alpha parameter.
873-
#[pallet::type_value]
874-
pub fn DefaultAlphaLow<T: Config>() -> u16 {
875-
45875 // Represents 0.7 as per the production default
876-
}
869+
pub fn DefaultAlphaValues<T: Config>() -> (u16, u16) {
870+
(45875, 58982) // (alpha_low: 0.7, alpha_high: 0.9)
871+
}
877872

878873
#[pallet::storage] // ITEM( weights_min_stake )
879874
pub type WeightsMinStake<T> = StorageValue<_, u64, ValueQuery, DefaultWeightsMinStake<T>>;
@@ -944,12 +939,10 @@ pub mod pallet {
944939
#[pallet::storage] // --- DMAP ( netuid ) --> adjustment_alpha
945940
pub type AdjustmentAlpha<T: Config> =
946941
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultAdjustmentAlpha<T>>;
947-
// MAP ( netuid ) --> alpha_high
948-
#[pallet::storage]
949-
pub type AlphaHigh<T> = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultAlphaHigh<T>>;
950-
// MAP ( netuid ) --> alpha_low
942+
943+
// MAP ( netuid ) --> (alpha_low, alpha_high)
951944
#[pallet::storage]
952-
pub type AlphaLow<T> = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultAlphaLow<T>>;
945+
pub type AlphaValues<T> = StorageMap<_, Identity, u16, (u16, u16), ValueQuery, DefaultAlphaValues<T>>;
953946

954947
#[pallet::storage] // --- MAP (netuid, who) --> (hash, weight) | Returns the hash and weight committed by an account for a given netuid.
955948
pub type WeightCommits<T: Config> = StorageDoubleMap<
@@ -2085,6 +2078,15 @@ pub mod pallet {
20852078
pub fn dissolve_network(origin: OriginFor<T>, netuid: u16) -> DispatchResult {
20862079
Self::user_remove_network(origin, netuid)
20872080
}
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+
}
20882090
}
20892091

20902092
// ---- Subtensor helper functions.

0 commit comments

Comments
 (0)