diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 28d1c9a7de..0d3232d894 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -653,7 +653,7 @@ pub mod pallet { netuid: u16, difficulty: u64, ) -> DispatchResult { - pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin, netuid)?; + ensure_root(origin)?; ensure!( pallet_subtensor::Pallet::::if_subnet_exist(netuid), Error::::SubnetDoesNotExist diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index 03fb1063e7..1bf2209ec1 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -643,6 +643,18 @@ fn test_sudo_set_difficulty() { to_be_set )); assert_eq!(SubtensorModule::get_difficulty_as_u64(netuid), to_be_set); + + // Test that SN owner can't set difficulty + pallet_subtensor::SubnetOwner::::insert(netuid, U256::from(1)); + assert_eq!( + AdminUtils::sudo_set_difficulty( + <::RuntimeOrigin>::signed(U256::from(1)), + netuid, + init_value + ), + Err(DispatchError::BadOrigin) + ); + assert_eq!(SubtensorModule::get_difficulty_as_u64(netuid), to_be_set); // no change }); } diff --git a/pallets/subtensor/src/macros/hooks.rs b/pallets/subtensor/src/macros/hooks.rs index 106f64456a..3a0a0ecb16 100644 --- a/pallets/subtensor/src/macros/hooks.rs +++ b/pallets/subtensor/src/macros/hooks.rs @@ -81,7 +81,9 @@ mod hooks { // Upgrade identities to V2 .saturating_add(migrations::migrate_identities_v2::migrate_identities_to_v2::()) // Set the min burn across all subnets to a new minimum - .saturating_add(migrations::migrate_set_min_burn::migrate_set_min_burn::()); + .saturating_add(migrations::migrate_set_min_burn::migrate_set_min_burn::()) + // Set the min difficulty across all subnets to a new minimum + .saturating_add(migrations::migrate_set_min_difficulty::migrate_set_min_difficulty::()); weight } diff --git a/pallets/subtensor/src/migrations/migrate_set_min_difficulty.rs b/pallets/subtensor/src/migrations/migrate_set_min_difficulty.rs new file mode 100644 index 0000000000..6d859925ae --- /dev/null +++ b/pallets/subtensor/src/migrations/migrate_set_min_difficulty.rs @@ -0,0 +1,52 @@ +use alloc::string::String; + +use frame_support::IterableStorageMap; +use frame_support::{traits::Get, weights::Weight}; + +use super::*; + +pub fn migrate_set_min_difficulty() -> Weight { + let migration_name = b"migrate_set_min_difficulty".to_vec(); + + // Initialize the weight with one read operation. + let mut weight = T::DbWeight::get().reads(1); + + // Check if the migration has already run + if HasMigrationRun::::get(&migration_name) { + log::info!( + "Migration '{:?}' has already run. Skipping.", + migration_name + ); + return weight; + } + log::info!( + "Running migration '{}'", + String::from_utf8_lossy(&migration_name) + ); + + let netuids: Vec = as IterableStorageMap>::iter() + .map(|(netuid, _)| netuid) + .collect(); + weight = weight.saturating_add(T::DbWeight::get().reads(netuids.len() as u64)); + + for netuid in netuids.iter().clone() { + if *netuid == 0 { + continue; + } + // Set min difficulty to 10 million for all subnets + Pallet::::set_min_difficulty(*netuid, 10_000_000); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + } + + // Mark the migration as completed + HasMigrationRun::::insert(&migration_name, true); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + + log::info!( + "Migration '{:?}' completed.", + String::from_utf8_lossy(&migration_name) + ); + + // Return the migration weight. + weight +} diff --git a/pallets/subtensor/src/migrations/mod.rs b/pallets/subtensor/src/migrations/mod.rs index 8185226e4b..e16adf46aa 100644 --- a/pallets/subtensor/src/migrations/mod.rs +++ b/pallets/subtensor/src/migrations/mod.rs @@ -12,6 +12,7 @@ pub mod migrate_populate_owned_hotkeys; pub mod migrate_populate_staking_hotkeys; pub mod migrate_rao; pub mod migrate_set_min_burn; +pub mod migrate_set_min_difficulty; pub mod migrate_stake_threshold; pub mod migrate_subnet_volume; pub mod migrate_to_v1_separate_emission; diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 28f4fabc54..1b86426bb2 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -229,7 +229,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 236, + spec_version: 237, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1,