Skip to content

Fix alpha_low, alpha_high min value checks #1729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: devnet-ready
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pallets/admin-utils/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ fn test_sudo_get_set_alpha() {
));

// 2. Alpha high too low
let alpha_high_too_low = (u16::MAX as u32 * 4 / 5) as u16 - 1; // One less than the minimum acceptable value
let alpha_high_too_low = (u16::MAX as u32 / 40) as u16 - 1; // One less than the minimum acceptable value
assert_err!(
AdminUtils::sudo_set_alpha_values(
signer.clone(),
Expand Down Expand Up @@ -1288,7 +1288,7 @@ fn test_sudo_get_set_alpha() {
alpha_high
));

let alpha_low_too_high = (u16::MAX as u32 * 4 / 5) as u16 + 1; // One more than the maximum acceptable value
let alpha_low_too_high = (u16::MAX as u32 / 40) as u16 + 1; // One more than the maximum acceptable value
assert_err!(
AdminUtils::sudo_set_alpha_values(
signer.clone(),
Expand Down
5 changes: 3 additions & 2 deletions pallets/subtensor/src/epoch/run_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,14 +1332,15 @@ impl<T: Config> Pallet<T> {
);

let max_u16: u32 = u16::MAX as u32; // 65535
let min_alpha_high: u16 = (max_u16.saturating_mul(4).safe_div(5)) as u16; // 52428
let min_alpha_low: u16 = (max_u16.safe_div(40)) as u16; // 1638
let min_alpha_high: u16 = min_alpha_low;

// --- 4. Ensure alpha high is greater than the minimum
ensure!(alpha_high >= min_alpha_high, Error::<T>::AlphaHighTooLow);

// -- 5. Ensure alpha low is within range
ensure!(
alpha_low > 0 && alpha_low < min_alpha_high,
alpha_low >= min_alpha_low && alpha_low <= alpha_high,
Error::<T>::AlphaLowOutOfRange
);

Expand Down
53 changes: 48 additions & 5 deletions pallets/subtensor/src/tests/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use super::mock::*;
use crate::epoch::math::{fixed, u16_proportion_to_fixed};
use crate::tests::math::{assert_mat_compare, vec_to_fixed, vec_to_mat_fixed};
use crate::*;

use approx::assert_abs_diff_eq;
Expand Down Expand Up @@ -1312,15 +1313,15 @@ fn test_set_alpha_disabled() {
// Explicitly set to false
SubtensorModule::set_liquid_alpha_enabled(netuid, false);
assert_err!(
SubtensorModule::do_set_alpha_values(signer.clone(), netuid, 12_u16, u16::MAX),
SubtensorModule::do_set_alpha_values(signer.clone(), netuid, 1638_u16, u16::MAX),
Error::<Test>::LiquidAlphaDisabled
);

SubtensorModule::set_liquid_alpha_enabled(netuid, true);
assert_ok!(SubtensorModule::do_set_alpha_values(
signer.clone(),
netuid,
12_u16,
1638_u16,
u16::MAX
));
});
Expand Down Expand Up @@ -2227,7 +2228,7 @@ fn test_validator_permits() {
fn test_get_set_alpha() {
new_test_ext(1).execute_with(|| {
let netuid = 1;
let alpha_low: u16 = 12_u16;
let alpha_low: u16 = 1638_u16;
let alpha_high: u16 = u16::MAX - 10;

let hotkey: U256 = U256::from(1);
Expand Down Expand Up @@ -2315,7 +2316,7 @@ fn test_get_set_alpha() {
));

// 2. Alpha high too low
let alpha_high_too_low = (u16::MAX as u32 * 4 / 5) as u16 - 1; // One less than the minimum acceptable value
let alpha_high_too_low = (u16::MAX as u32 / 40) as u16 - 1; // One less than the minimum acceptable value
assert_err!(
SubtensorModule::do_set_alpha_values(
signer.clone(),
Expand Down Expand Up @@ -2352,7 +2353,7 @@ fn test_get_set_alpha() {
alpha_high
));

let alpha_low_too_high = (u16::MAX as u32 * 4 / 5) as u16 + 1; // One more than the maximum acceptable value
let alpha_low_too_high = alpha_high + 1; // alpha_low should be <= alpha_high
assert_err!(
SubtensorModule::do_set_alpha_values(
signer.clone(),
Expand Down Expand Up @@ -3480,3 +3481,45 @@ fn test_yuma_3_bonds_reset() {
}
})
}

#[test]
fn test_liquid_alpha_equal_values_against_itself() {
new_test_ext(1).execute_with(|| {
// check Liquid alpha disabled against Liquid Alpha enabled with alpha_low == alpha_high
let netuid: u16 = 1;
let alpha_low = u16::MAX / 10;
let alpha_high = u16::MAX / 10;
let epsilon = I32F32::from_num(1e-3);
let weights: Vec<Vec<I32F32>> = vec_to_mat_fixed(
&[0., 0.1, 0., 0., 0.2, 0.4, 0., 0.3, 0.1, 0., 0.4, 0.5],
4,
false,
);
let bonds: Vec<Vec<I32F32>> = vec_to_mat_fixed(
&[0.1, 0.1, 0.5, 0., 0., 0.4, 0.5, 0.1, 0.1, 0., 0.4, 0.2],
4,
false,
);
let consensus: Vec<I32F32> = vec_to_fixed(&[0.3, 0.2, 0.1, 0.4]);

// set both alpha values to 0.1 and bonds moving average to 0.9
AlphaValues::<Test>::insert(netuid, (alpha_low, alpha_high));
SubtensorModule::set_bonds_moving_average(netuid, 900_000);

// compute bonds with liquid alpha enabled
SubtensorModule::set_liquid_alpha_enabled(netuid, true);
let new_bonds_liquid_alpha_on =
SubtensorModule::compute_bonds(netuid, &weights, &bonds, &consensus);

// compute bonds with liquid alpha disabled
SubtensorModule::set_liquid_alpha_enabled(netuid, false);
let new_bonds_liquid_alpha_off =
SubtensorModule::compute_bonds(netuid, &weights, &bonds, &consensus);

assert_mat_compare(
&new_bonds_liquid_alpha_on,
&new_bonds_liquid_alpha_off,
epsilon,
);
});
}
6 changes: 3 additions & 3 deletions pallets/subtensor/src/tests/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn assert_vec_compare_u16(va: &[u16], vb: &[u16]) {
}
}

fn assert_mat_compare(ma: &[Vec<I32F32>], mb: &[Vec<I32F32>], epsilon: I32F32) {
pub fn assert_mat_compare(ma: &[Vec<I32F32>], mb: &[Vec<I32F32>], epsilon: I32F32) {
assert!(ma.len() == mb.len());
for row in 0..ma.len() {
assert!(ma[row].len() == mb[row].len());
Expand Down Expand Up @@ -72,7 +72,7 @@ fn assert_sparse_mat_compare(
}
}

fn vec_to_fixed(vector: &[f32]) -> Vec<I32F32> {
pub fn vec_to_fixed(vector: &[f32]) -> Vec<I32F32> {
vector.iter().map(|x| I32F32::from_num(*x)).collect()
}

Expand Down Expand Up @@ -365,7 +365,7 @@ fn test_math_vec_to_fixed() {
}

// Reshape vector to matrix with specified number of rows, cast to I32F32.
fn vec_to_mat_fixed(vector: &[f32], rows: usize, transpose: bool) -> Vec<Vec<I32F32>> {
pub fn vec_to_mat_fixed(vector: &[f32], rows: usize, transpose: bool) -> Vec<Vec<I32F32>> {
assert!(
vector.len() % rows == 0,
"Vector of len {:?} cannot reshape to {rows} rows.",
Expand Down
Loading