Skip to content

Commit ccddd7b

Browse files
committed
ensure caller is subnet owner & tests
1 parent d01cd1c commit ccddd7b

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

pallets/subtensor/src/epoch.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,27 +1184,30 @@ impl<T: Config> Pallet<T> {
11841184

11851185
pub fn do_set_alpha_values(origin: T::RuntimeOrigin, netuid: u16, alpha_low: u16, alpha_high: u16) -> Result<(), DispatchError> {
11861186
// --- 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
1187+
ensure_signed(origin.clone())?;
1188+
1189+
// --- 2. Ensure the function caller is the subnet owner or root.
1190+
Self::ensure_subnet_owner_or_root(origin, netuid)?;
11911191

1192-
// --- 2. Ensure liquid alpha is enabled
1192+
// --- 3. Ensure liquid alpha is enabled
11931193
ensure!(
11941194
Self::get_liquid_alpha_enabled(netuid),
11951195
Error::<T>::LiquidAlphaDisabled
11961196
);
1197-
1198-
// --- 3. Ensure alpha high is greater than the minimum
1197+
1198+
let max_u16: u32 = u16::MAX as u32; // 65535
1199+
let min_alpha_high: u16 = (max_u16.saturating_mul(4).saturating_div(5)) as u16; // 52428
1200+
1201+
// --- 4. Ensure alpha high is greater than the minimum
11991202
ensure!(alpha_high >= min_alpha_high, Error::<T>::AlphaHighTooLow);
12001203

1201-
// -- 4. Ensure alpha low is within range
1204+
// -- 5. Ensure alpha low is within range
12021205
ensure!(
12031206
alpha_low > 0 && alpha_low < min_alpha_high,
12041207
Error::<T>::AlphaLowTooLow
12051208
);
1206-
AlphaValues::<T>::insert(netuid, (alpha_low, alpha_high));
12071209

1210+
AlphaValues::<T>::insert(netuid, (alpha_low, alpha_high));
12081211
Ok(())
12091212
}
12101213
}

pallets/subtensor/tests/epoch.rs

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use frame_system::Config;
44
use pallet_subtensor::*;
55
use rand::{distributions::Uniform, rngs::StdRng, seq::SliceRandom, thread_rng, Rng, SeedableRng};
66
use sp_core::U256;
7+
use sp_runtime::DispatchError;
78
use std::time::Instant;
89
use substrate_fixed::types::I32F32;
910
mod mock;
@@ -1481,9 +1482,30 @@ fn test_bonds_with_liquid_alpha() {
14811482
fn test_set_alpha_disabled() {
14821483
new_test_ext(1).execute_with(|| {
14831484
let netuid: u16 = 1;
1484-
let tempo: u16 = u16::MAX - 1;
1485-
let signer: RuntimeOrigin = RuntimeOrigin::signed(U256::from(1));
1486-
add_network(netuid, tempo, 0);
1485+
let hotkey: U256 = U256::from(1);
1486+
let coldkey: U256 = U256::from(1 + 456);
1487+
let signer = <<Test as Config>::RuntimeOrigin>::signed(coldkey.clone());
1488+
1489+
// Enable Liquid Alpha and setup
1490+
SubtensorModule::set_liquid_alpha_enabled(netuid, true);
1491+
migration::migrate_create_root_network::<Test>();
1492+
SubtensorModule::add_balance_to_coldkey_account(
1493+
&coldkey,
1494+
1_000_000_000_000_000,
1495+
);
1496+
assert_ok!(SubtensorModule::root_register(
1497+
signer.clone(),
1498+
hotkey,
1499+
));
1500+
assert_ok!(SubtensorModule::add_stake(
1501+
signer.clone(),
1502+
hotkey,
1503+
1000
1504+
));
1505+
// Only owner can set alpha values
1506+
assert_ok!(SubtensorModule::register_network(
1507+
signer.clone()
1508+
));
14871509

14881510
// Explicitly set to false
14891511
SubtensorModule::set_liquid_alpha_enabled(netuid, false);
@@ -2509,10 +2531,38 @@ fn test_get_set_alpha() {
25092531
let netuid: u16 = 1;
25102532
let alpha_low: u16 = 12_u16;
25112533
let alpha_high: u16 = u16::MAX - 10;
2512-
let signer: RuntimeOrigin = RuntimeOrigin::signed(U256::from(1));
25132534

2514-
// Enable liquid alpha and set alpha values
2535+
let hotkey: U256 = U256::from(1);
2536+
let coldkey: U256 = U256::from(1 + 456);
2537+
let signer = <<Test as Config>::RuntimeOrigin>::signed(coldkey.clone());
2538+
2539+
// Enable Liquid Alpha and setup
25152540
SubtensorModule::set_liquid_alpha_enabled(netuid, true);
2541+
migration::migrate_create_root_network::<Test>();
2542+
SubtensorModule::add_balance_to_coldkey_account(
2543+
&coldkey,
2544+
1_000_000_000_000_000,
2545+
);
2546+
assert_ok!(SubtensorModule::root_register(
2547+
signer.clone(),
2548+
hotkey,
2549+
));
2550+
assert_ok!(SubtensorModule::add_stake(
2551+
signer.clone(),
2552+
hotkey,
2553+
1000
2554+
));
2555+
2556+
// Should fail as signer does not own the subnet
2557+
assert_err!(
2558+
SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high),
2559+
DispatchError::BadOrigin
2560+
);
2561+
2562+
assert_ok!(SubtensorModule::register_network(
2563+
signer.clone()
2564+
));
2565+
25162566
assert_ok!(SubtensorModule::set_alpha_values(signer.clone(), netuid, alpha_low, alpha_high));
25172567
let (grabbed_alpha_low, grabbed_alpha_high): (u16, u16) = SubtensorModule::get_alpha_values(netuid);
25182568

@@ -2537,8 +2587,6 @@ fn test_get_set_alpha() {
25372587
assert!((alpha_low_32.to_num::<f32>() - alpha_low_decimal).abs() < tolerance, "alpha_low mismatch: {} != {}", alpha_low_32.to_num::<f32>(), alpha_low_decimal);
25382588
assert!((alpha_high_32.to_num::<f32>() - alpha_high_decimal).abs() < tolerance, "alpha_high mismatch: {} != {}", alpha_high_32.to_num::<f32>(), alpha_high_decimal);
25392589

2540-
// Test error cases
2541-
25422590
// 1. Liquid alpha disabled
25432591
SubtensorModule::set_liquid_alpha_enabled(netuid, false);
25442592
assert_err!(

0 commit comments

Comments
 (0)