Skip to content

Disallow subnet owners to set AdjustmentAlpha greater than 0.97 #1606

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 6 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
11 changes: 10 additions & 1 deletion pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ pub mod pallet {
MaxAllowedUIdsLessThanCurrentUIds,
/// The maximum value for bonds moving average is reached
BondsMovingAverageMaxReached,
/// The maximum value for adjustment alpha is reached
AdjustmentAlphaMaxReached,
}
/// Enum for specifying the type of precompile operation.
#[derive(Encode, Decode, TypeInfo, Clone, PartialEq, Eq, Debug, Copy)]
Expand Down Expand Up @@ -381,7 +383,14 @@ pub mod pallet {
netuid: u16,
adjustment_alpha: u64,
) -> DispatchResult {
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin.clone(), netuid)?;

if pallet_subtensor::Pallet::<T>::ensure_subnet_owner(origin, netuid).is_ok() {
ensure!(
adjustment_alpha <= (u64::MAX.saturating_div(100).saturating_mul(97)), // 0.97
Error::<T>::AdjustmentAlphaMaxReached
)
}

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Expand Down
16 changes: 13 additions & 3 deletions pallets/admin-utils/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,14 @@ fn test_sudo_set_adjustment_interval() {
fn test_sudo_set_adjustment_alpha() {
new_test_ext().execute_with(|| {
let netuid: u16 = 1;
let to_be_set: u64 = 10;
let coldkey: U256 = U256::from(1);
let to_be_set: u64 = u64::MAX / 3;
add_network(netuid, 10);
let init_value: u64 = SubtensorModule::get_adjustment_alpha(netuid);
pallet_subtensor::SubnetOwner::<Test>::insert(netuid, coldkey);
assert_eq!(
AdminUtils::sudo_set_adjustment_alpha(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
<<Test as Config>::RuntimeOrigin>::signed(U256::from(100)),
netuid,
to_be_set
),
Expand All @@ -358,9 +360,17 @@ fn test_sudo_set_adjustment_alpha() {
),
Err(Error::<Test>::SubnetDoesNotExist.into())
);
assert_eq!(
AdminUtils::sudo_set_adjustment_alpha(
<<Test as Config>::RuntimeOrigin>::signed(coldkey),
netuid,
u64::MAX
),
Err(Error::<Test>::AdjustmentAlphaMaxReached.into())
);
assert_eq!(SubtensorModule::get_adjustment_alpha(netuid), init_value);
assert_ok!(AdminUtils::sudo_set_adjustment_alpha(
<<Test as Config>::RuntimeOrigin>::root(),
<<Test as Config>::RuntimeOrigin>::signed(coldkey),
netuid,
to_be_set
));
Expand Down
4 changes: 3 additions & 1 deletion pallets/subtensor/src/macros/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ mod hooks {
// Reset max burn
.saturating_add(migrations::migrate_reset_max_burn::migrate_reset_max_burn::<T>())
// Migrate ColdkeySwapScheduled structure to new format
.saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::<T>());
.saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::<T>())
// Reset adjustment alpha
.saturating_add(migrations::migrate_reset_adjustment_alpha::migrate_reset_adjustment_alpha::<T>());
weight
}

Expand Down
59 changes: 59 additions & 0 deletions pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use super::*;
use frame_support::{traits::Get, weights::Weight};
use log;
use scale_info::prelude::string::String;

pub fn migrate_reset_adjustment_alpha<T: Config>() -> Weight {
let migration_name = b"migrate_reset_adjustment_alpha".to_vec();
let mut weight = T::DbWeight::get().reads(1);

// ------------------------------
// Step 0: Check if already run
// ------------------------------
if HasMigrationRun::<T>::get(&migration_name) {
log::info!(
"Migration '{:?}' has already run. Skipping.",
migration_name
);
return weight;
}

log::info!(
"Running migration '{}'",
String::from_utf8_lossy(&migration_name)
);

// ------------------------------
// Step 1: Reset all subnet's adjustment alpha to 0.97 if the value exceeds 0.97
// ------------------------------

let mut reset_entries_count = 0u64;

for netuid in AdjustmentAlpha::<T>::iter_keys() {
AdjustmentAlpha::<T>::mutate(netuid, |adjustment| {
*adjustment = (*adjustment).min(u64::MAX.saturating_div(100).saturating_mul(97)); // 0.97
});
reset_entries_count = reset_entries_count.saturating_add(1);
}

weight = weight
.saturating_add(T::DbWeight::get().reads_writes(reset_entries_count, reset_entries_count));

log::info!(
"Reset {} subnets from AdjustmentAlpha.",
reset_entries_count
);

// ------------------------------
// Step 2: Mark Migration as Completed
// ------------------------------
HasMigrationRun::<T>::insert(&migration_name, true);
weight = weight.saturating_add(T::DbWeight::get().writes(1));

log::info!(
"Migration '{:?}' completed successfully.",
String::from_utf8_lossy(&migration_name)
);

weight
}
1 change: 1 addition & 0 deletions pallets/subtensor/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod migrate_remove_stake_map;
pub mod migrate_remove_total_hotkey_coldkey_stakes_this_interval;
pub mod migrate_remove_unused_maps_and_values;
pub mod migrate_remove_zero_total_hotkey_alpha;
pub mod migrate_reset_adjustment_alpha;
pub mod migrate_reset_bonds_moving_average;
pub mod migrate_reset_max_burn;
pub mod migrate_set_first_emission_block_number;
Expand Down
Loading