Skip to content

Commit b836401

Browse files
author
Samuel Dare
committed
lints , use sat maths for 256 miner alpha test
1 parent 2e51ebd commit b836401

File tree

8 files changed

+209
-94
lines changed

8 files changed

+209
-94
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,12 @@ pub mod pallet {
10261026
/// Sets values for liquid alpha
10271027
#[pallet::call_index(51)]
10281028
#[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 {
1029+
pub fn sudo_set_alpha_values(
1030+
origin: OriginFor<T>,
1031+
netuid: u16,
1032+
alpha_low: u16,
1033+
alpha_high: u16,
1034+
) -> DispatchResult {
10301035
T::Subtensor::ensure_subnet_owner_or_root(origin.clone(), netuid)?;
10311036
T::Subtensor::do_set_alpha_values(origin, netuid, alpha_low, alpha_high)
10321037
}
@@ -1126,5 +1131,10 @@ pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin> {
11261131
fn set_commit_reveal_weights_interval(netuid: u16, interval: u64);
11271132
fn set_commit_reveal_weights_enabled(netuid: u16, enabled: bool);
11281133
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>;
1134+
fn do_set_alpha_values(
1135+
origin: RuntimeOrigin,
1136+
netuid: u16,
1137+
alpha_low: u16,
1138+
alpha_high: u16,
1139+
) -> Result<(), DispatchError>;
11301140
}

pallets/admin-utils/tests/mock.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,12 @@ 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> {
474+
fn do_set_alpha_values(
475+
origin: RuntimeOrigin,
476+
netuid: u16,
477+
alpha_low: u16,
478+
alpha_high: u16,
479+
) -> Result<(), DispatchError> {
475480
SubtensorModule::do_set_alpha_values(origin, netuid, alpha_low, alpha_high)
476481
}
477482
}

pallets/admin-utils/tests/tests.rs

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use frame_support::{assert_ok, assert_err, dispatch::{DispatchClass, GetDispatchInfo, Pays}};
21
use frame_support::sp_runtime::DispatchError;
2+
use frame_support::{
3+
assert_err, assert_ok,
4+
dispatch::{DispatchClass, GetDispatchInfo, Pays},
5+
};
36
use frame_system::Config;
47
use pallet_admin_utils::Error;
58
use pallet_subtensor::Error as SubtensorError;
@@ -1225,39 +1228,37 @@ fn test_sudo_get_set_alpha() {
12251228

12261229
let hotkey: U256 = U256::from(1);
12271230
let coldkey: U256 = U256::from(1 + 456);
1228-
let signer = <<Test as Config>::RuntimeOrigin>::signed(coldkey.clone());
1231+
let signer = <<Test as Config>::RuntimeOrigin>::signed(coldkey);
12291232

12301233
// Enable Liquid Alpha and setup
12311234
SubtensorModule::set_liquid_alpha_enabled(netuid, true);
12321235
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-
));
1236+
SubtensorModule::add_balance_to_coldkey_account(&coldkey, 1_000_000_000_000_000);
1237+
assert_ok!(SubtensorModule::root_register(signer.clone(), hotkey,));
1238+
assert_ok!(SubtensorModule::add_stake(signer.clone(), hotkey, 1000));
12461239

12471240
// Should fail as signer does not own the subnet
12481241
assert_err!(
12491242
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high),
12501243
DispatchError::BadOrigin
12511244
);
12521245

1253-
assert_ok!(SubtensorModule::register_network(
1254-
signer.clone()
1255-
));
1246+
assert_ok!(SubtensorModule::register_network(signer.clone()));
12561247

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);
1248+
assert_ok!(AdminUtils::sudo_set_alpha_values(
1249+
signer.clone(),
1250+
netuid,
1251+
alpha_low,
1252+
alpha_high
1253+
));
1254+
let (grabbed_alpha_low, grabbed_alpha_high): (u16, u16) =
1255+
SubtensorModule::get_alpha_values(netuid);
12591256

1260-
log::info!("alpha_low: {:?} alpha_high: {:?}", grabbed_alpha_low, grabbed_alpha_high);
1257+
log::info!(
1258+
"alpha_low: {:?} alpha_high: {:?}",
1259+
grabbed_alpha_low,
1260+
grabbed_alpha_high
1261+
);
12611262
assert_eq!(grabbed_alpha_low, alpha_low);
12621263
assert_eq!(grabbed_alpha_high, alpha_high);
12631264

@@ -1275,8 +1276,18 @@ fn test_sudo_get_set_alpha() {
12751276
let tolerance: f32 = 1e-6; // 0.000001
12761277

12771278
// 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);
1279+
assert!(
1280+
(alpha_low_32.to_num::<f32>() - alpha_low_decimal).abs() < tolerance,
1281+
"alpha_low mismatch: {} != {}",
1282+
alpha_low_32.to_num::<f32>(),
1283+
alpha_low_decimal
1284+
);
1285+
assert!(
1286+
(alpha_high_32.to_num::<f32>() - alpha_high_decimal).abs() < tolerance,
1287+
"alpha_high mismatch: {} != {}",
1288+
alpha_high_32.to_num::<f32>(),
1289+
alpha_high_decimal
1290+
);
12801291

12811292
// 1. Liquid alpha disabled
12821293
SubtensorModule::set_liquid_alpha_enabled(netuid, false);
@@ -1286,32 +1297,67 @@ fn test_sudo_get_set_alpha() {
12861297
);
12871298
// Correct scenario after error
12881299
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));
1300+
assert_ok!(AdminUtils::sudo_set_alpha_values(
1301+
signer.clone(),
1302+
netuid,
1303+
alpha_low,
1304+
alpha_high
1305+
));
12901306

12911307
// 2. Alpha high too low
12921308
let alpha_high_too_low = (u16::MAX as u32 * 4 / 5) as u16 - 1; // One less than the minimum acceptable value
12931309
assert_err!(
1294-
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high_too_low),
1310+
AdminUtils::sudo_set_alpha_values(
1311+
signer.clone(),
1312+
netuid,
1313+
alpha_low,
1314+
alpha_high_too_low
1315+
),
12951316
SubtensorError::<Test>::AlphaHighTooLow
12961317
);
12971318
// Correct scenario after error
1298-
assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
1319+
assert_ok!(AdminUtils::sudo_set_alpha_values(
1320+
signer.clone(),
1321+
netuid,
1322+
alpha_low,
1323+
alpha_high
1324+
));
12991325

13001326
// 3. Alpha low too low or too high
13011327
let alpha_low_too_low = 0_u16;
13021328
assert_err!(
1303-
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low_too_low, alpha_high),
1329+
AdminUtils::sudo_set_alpha_values(
1330+
signer.clone(),
1331+
netuid,
1332+
alpha_low_too_low,
1333+
alpha_high
1334+
),
13041335
SubtensorError::<Test>::AlphaLowOutOfRange
13051336
);
13061337
// Correct scenario after error
1307-
assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
1338+
assert_ok!(AdminUtils::sudo_set_alpha_values(
1339+
signer.clone(),
1340+
netuid,
1341+
alpha_low,
1342+
alpha_high
1343+
));
13081344

13091345
let alpha_low_too_high = (u16::MAX as u32 * 4 / 5) as u16 + 1; // One more than the maximum acceptable value
13101346
assert_err!(
1311-
AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low_too_high, alpha_high),
1347+
AdminUtils::sudo_set_alpha_values(
1348+
signer.clone(),
1349+
netuid,
1350+
alpha_low_too_high,
1351+
alpha_high
1352+
),
13121353
SubtensorError::<Test>::AlphaLowOutOfRange
13131354
);
13141355
// Correct scenario after error
1315-
assert_ok!(AdminUtils::sudo_set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
1356+
assert_ok!(AdminUtils::sudo_set_alpha_values(
1357+
signer.clone(),
1358+
netuid,
1359+
alpha_low,
1360+
alpha_high
1361+
));
13161362
});
13171363
}

pallets/subtensor/src/epoch.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,13 +1182,18 @@ impl<T: Config> Pallet<T> {
11821182
}
11831183
}
11841184

1185-
pub fn do_set_alpha_values(origin: T::RuntimeOrigin, netuid: u16, alpha_low: u16, alpha_high: u16) -> Result<(), DispatchError> {
1185+
pub fn do_set_alpha_values(
1186+
origin: T::RuntimeOrigin,
1187+
netuid: u16,
1188+
alpha_low: u16,
1189+
alpha_high: u16,
1190+
) -> Result<(), DispatchError> {
11861191
// --- 1. Ensure the function caller is a signed user.
11871192
ensure_signed(origin.clone())?;
11881193

11891194
// --- 2. Ensure the function caller is the subnet owner or root.
11901195
Self::ensure_subnet_owner_or_root(origin, netuid)?;
1191-
1196+
11921197
// --- 3. Ensure liquid alpha is enabled
11931198
ensure!(
11941199
Self::get_liquid_alpha_enabled(netuid),

pallets/subtensor/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ pub mod pallet {
868868
#[pallet::type_value]
869869
pub fn DefaultAlphaValues<T: Config>() -> (u16, u16) {
870870
(45875, 58982) // (alpha_low: 0.7, alpha_high: 0.9)
871-
}
871+
}
872872

873873
#[pallet::storage] // ITEM( weights_min_stake )
874874
pub type WeightsMinStake<T> = StorageValue<_, u64, ValueQuery, DefaultWeightsMinStake<T>>;
@@ -942,7 +942,8 @@ pub mod pallet {
942942

943943
// MAP ( netuid ) --> (alpha_low, alpha_high)
944944
#[pallet::storage]
945-
pub type AlphaValues<T> = StorageMap<_, Identity, u16, (u16, u16), ValueQuery, DefaultAlphaValues<T>>;
945+
pub type AlphaValues<T> =
946+
StorageMap<_, Identity, u16, (u16, u16), ValueQuery, DefaultAlphaValues<T>>;
946947

947948
#[pallet::storage] // --- MAP (netuid, who) --> (hash, weight) | Returns the hash and weight committed by an account for a given netuid.
948949
pub type WeightCommits<T: Config> = StorageDoubleMap<

pallets/subtensor/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ impl<T: Config> Pallet<T> {
672672
let converted_low = I32F32::from_num(alpha_low) / I32F32::from_num(u16::MAX);
673673
let converted_high = I32F32::from_num(alpha_high) / I32F32::from_num(u16::MAX);
674674

675-
return (converted_low, converted_high);
675+
(converted_low, converted_high)
676676
}
677677

678678
pub fn set_liquid_alpha_enabled(netuid: u16, enabled: bool) {

0 commit comments

Comments
 (0)