From 784fe737d1827da259759d8af3263b81f04764c4 Mon Sep 17 00:00:00 2001 From: open-junius Date: Wed, 2 Jul 2025 21:25:00 +0800 Subject: [PATCH 1/5] fix owner disable registration --- pallets/admin-utils/src/lib.rs | 5 +- pallets/subtensor/src/lib.rs | 2 +- .../migrate_set_registration_enable.rs | 59 +++++++++++++++++++ pallets/subtensor/src/migrations/mod.rs | 1 + 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 pallets/subtensor/src/migrations/migrate_set_registration_enable.rs diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 2968bd7728..752b834c48 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -605,8 +605,7 @@ pub mod pallet { netuid: NetUid, registration_allowed: bool, ) -> DispatchResult { - pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin, netuid)?; - + ensure_root(origin)?; pallet_subtensor::Pallet::::set_network_registration_allowed( netuid, registration_allowed, @@ -633,7 +632,7 @@ pub mod pallet { netuid: NetUid, registration_allowed: bool, ) -> DispatchResult { - pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin, netuid)?; + ensure_root(origin)?; pallet_subtensor::Pallet::::set_network_pow_registration_allowed( netuid, diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index dbd91fe25e..e3d29175fa 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -502,7 +502,7 @@ pub mod pallet { #[pallet::type_value] /// Default value for registration allowed. pub fn DefaultRegistrationAllowed() -> bool { - false + true } #[pallet::type_value] /// Default value for network registered at. diff --git a/pallets/subtensor/src/migrations/migrate_set_registration_enable.rs b/pallets/subtensor/src/migrations/migrate_set_registration_enable.rs new file mode 100644 index 0000000000..d066ed6b74 --- /dev/null +++ b/pallets/subtensor/src/migrations/migrate_set_registration_enable.rs @@ -0,0 +1,59 @@ +use alloc::string::String; + +use frame_support::IterableStorageMap; +use frame_support::{traits::Get, weights::Weight}; + +use super::*; + +pub fn migrate_set_registration_enable() -> Weight { + let migration_name = b"migrate_set_registration_enable".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.", + String::from_utf8_lossy(&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() { + if netuid.is_root() { + continue; + } + + if !Pallet::::get_network_pow_registration_allowed(*netuid) { + Pallet::::set_network_pow_registration_allowed(*netuid, true); + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + } + + if !Pallet::::get_network_registration_allowed(*netuid) { + Pallet::::set_network_registration_allowed(*netuid, true); + 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 35da283800..5a6b1094e2 100644 --- a/pallets/subtensor/src/migrations/mod.rs +++ b/pallets/subtensor/src/migrations/mod.rs @@ -27,6 +27,7 @@ pub mod migrate_reset_max_burn; pub mod migrate_set_first_emission_block_number; pub mod migrate_set_min_burn; pub mod migrate_set_min_difficulty; +pub mod migrate_set_registration_enable; pub mod migrate_set_subtoken_enabled; pub mod migrate_stake_threshold; pub mod migrate_subnet_identities_to_v3; From f7b5cb7f82428457ae6d911344c625d35b5cc9bc Mon Sep 17 00:00:00 2001 From: open-junius Date: Wed, 2 Jul 2025 21:38:40 +0800 Subject: [PATCH 2/5] udpate runtime version --- pallets/subtensor/src/macros/hooks.rs | 4 ++- pallets/subtensor/src/subnets/subnet.rs | 8 +++-- pallets/subtensor/src/tests/migration.rs | 46 ++++++++++++++++++++++++ runtime/src/lib.rs | 2 +- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/pallets/subtensor/src/macros/hooks.rs b/pallets/subtensor/src/macros/hooks.rs index 1f75abc01f..f067497efb 100644 --- a/pallets/subtensor/src/macros/hooks.rs +++ b/pallets/subtensor/src/macros/hooks.rs @@ -121,7 +121,9 @@ mod hooks { // Migrate ColdkeySwapScheduled structure to new format .saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::()) // Fix the root subnet TAO storage value - .saturating_add(migrations::migrate_fix_root_subnet_tao::migrate_fix_root_subnet_tao::()); + .saturating_add(migrations::migrate_fix_root_subnet_tao::migrate_fix_root_subnet_tao::()) + // Fix the owner disable the registration + .saturating_add(migrations::migrate_set_registration_enable::migrate_set_registration_enable::()); weight } diff --git a/pallets/subtensor/src/subnets/subnet.rs b/pallets/subtensor/src/subnets/subnet.rs index 70855c69a1..dd8eeddcb0 100644 --- a/pallets/subtensor/src/subnets/subnet.rs +++ b/pallets/subtensor/src/subnets/subnet.rs @@ -220,7 +220,11 @@ impl Pallet { Self::deposit_event(Event::SubnetIdentitySet(netuid_to_register)); } - // --- 16. Emit the NetworkAdded event. + // --- 16. Enable registration for new subnet + NetworkRegistrationAllowed::::set(netuid_to_register, true); + NetworkPowRegistrationAllowed::::set(netuid_to_register, true); + + // --- 17. Emit the NetworkAdded event. log::info!( "NetworkAdded( netuid:{:?}, mechanism:{:?} )", netuid_to_register, @@ -228,7 +232,7 @@ impl Pallet { ); Self::deposit_event(Event::NetworkAdded(netuid_to_register, mechid)); - // --- 17. Return success. + // --- 18. Return success. Ok(()) } diff --git a/pallets/subtensor/src/tests/migration.rs b/pallets/subtensor/src/tests/migration.rs index c5cc8689cf..0e75a7cc92 100644 --- a/pallets/subtensor/src/tests/migration.rs +++ b/pallets/subtensor/src/tests/migration.rs @@ -907,3 +907,49 @@ fn test_migrate_subnet_symbols() { assert!(!weight.is_zero(), "Migration weight should be non-zero"); }); } + +#[test] +fn test_migrate_set_registration_enable() { + new_test_ext(1).execute_with(|| { + const MIGRATION_NAME: &str = "migrate_set_registration_enable"; + + // Create 3 subnets + let netuids: [NetUid; 3] = [1.into(), 2.into(), 3.into()]; + for netuid in netuids.iter() { + add_network(*netuid, 1, 0); + // Set registration to false to simulate the need for migration + SubtensorModule::set_network_registration_allowed(*netuid, false); + SubtensorModule::set_network_pow_registration_allowed(*netuid, false); + } + + // Sanity check: registration is disabled before migration + for netuid in netuids.iter() { + assert!(!SubtensorModule::get_network_registration_allowed(*netuid)); + assert!(!SubtensorModule::get_network_pow_registration_allowed( + *netuid + )); + } + + // Run the migration + let weight = + crate::migrations::migrate_set_registration_enable::migrate_set_registration_enable::< + Test, + >(); + + // After migration, registration should be enabled for all subnets except root + for netuid in netuids.iter() { + assert!(SubtensorModule::get_network_registration_allowed(*netuid)); + assert!(SubtensorModule::get_network_pow_registration_allowed( + *netuid + )); + } + + // Migration should be marked as run + assert!(HasMigrationRun::::get( + MIGRATION_NAME.as_bytes().to_vec() + )); + + // Weight should be non-zero + assert!(!weight.is_zero(), "Migration weight should be non-zero"); + }); +} diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 161287fdef..0d5d5fd04e 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -218,7 +218,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: 286, + spec_version: 287, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From ff2bc607b2da35cc9da8a6f480028dcbbe5f708e Mon Sep 17 00:00:00 2001 From: open-junius Date: Wed, 2 Jul 2025 21:47:28 +0800 Subject: [PATCH 3/5] commit Cargo.lock --- pallets/admin-utils/src/tests/mod.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index 260dc9f7c3..73b4fc16a0 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -983,6 +983,8 @@ fn test_sudo_set_network_pow_registration_allowed() { let to_be_set: bool = true; add_network(netuid, 10); + let owner = SubtensorModule::get_subnet_owner(netuid); + let init_value: bool = SubtensorModule::get_network_pow_registration_allowed(netuid); assert_eq!( AdminUtils::sudo_set_network_pow_registration_allowed( @@ -996,6 +998,20 @@ fn test_sudo_set_network_pow_registration_allowed() { SubtensorModule::get_network_pow_registration_allowed(netuid), init_value ); + + assert_eq!( + AdminUtils::sudo_set_network_pow_registration_allowed( + <::RuntimeOrigin>::signed(owner), + netuid, + to_be_set + ), + Err(DispatchError::BadOrigin) + ); + assert_eq!( + SubtensorModule::get_network_pow_registration_allowed(netuid), + init_value + ); + assert_ok!(AdminUtils::sudo_set_network_pow_registration_allowed( <::RuntimeOrigin>::root(), netuid, From d67790045dccfc07f06e2b7eaa008acd6e97b4ec Mon Sep 17 00:00:00 2001 From: open-junius Date: Wed, 2 Jul 2025 21:49:43 +0800 Subject: [PATCH 4/5] cargo clippy --- pallets/admin-utils/src/tests/mod.rs | 48 ++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index 73b4fc16a0..cfac6310bf 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -1024,6 +1024,54 @@ fn test_sudo_set_network_pow_registration_allowed() { }); } +#[test] +fn test_sudo_set_network_registration_allowed() { + new_test_ext().execute_with(|| { + let netuid = NetUid::from(1); + let to_be_set: bool = true; + add_network(netuid, 10); + + let owner = SubtensorModule::get_subnet_owner(netuid); + + let init_value: bool = SubtensorModule::get_network_registration_allowed(netuid); + assert_eq!( + AdminUtils::sudo_set_network_registration_allowed( + <::RuntimeOrigin>::signed(U256::from(1)), + netuid, + to_be_set + ), + Err(DispatchError::BadOrigin) + ); + assert_eq!( + SubtensorModule::get_network_registration_allowed(netuid), + init_value + ); + + assert_eq!( + AdminUtils::sudo_set_network_registration_allowed( + <::RuntimeOrigin>::signed(owner), + netuid, + to_be_set + ), + Err(DispatchError::BadOrigin) + ); + assert_eq!( + SubtensorModule::get_network_registration_allowed(netuid), + init_value + ); + + assert_ok!(AdminUtils::sudo_set_network_registration_allowed( + <::RuntimeOrigin>::root(), + netuid, + to_be_set + )); + assert_eq!( + SubtensorModule::get_network_registration_allowed(netuid), + to_be_set + ); + }); +} + mod sudo_set_nominator_min_required_stake { use super::*; From dacaac70bb15b276579c6b8138fb42a00844c1f4 Mon Sep 17 00:00:00 2001 From: open-junius Date: Wed, 2 Jul 2025 22:02:12 +0800 Subject: [PATCH 5/5] commit Cargo.lock --- pallets/subtensor/src/tests/subnet.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pallets/subtensor/src/tests/subnet.rs b/pallets/subtensor/src/tests/subnet.rs index e9a29864fc..e29018d80d 100644 --- a/pallets/subtensor/src/tests/subnet.rs +++ b/pallets/subtensor/src/tests/subnet.rs @@ -220,6 +220,10 @@ fn test_register_network_min_burn_at_default() { // Check min burn is set to default assert_eq!(MinBurn::::get(netuid), InitialMinBurn::get()); + + // Check registration allowed + assert!(NetworkRegistrationAllowed::::get(netuid)); + assert!(NetworkPowRegistrationAllowed::::get(netuid)); }); } @@ -245,6 +249,10 @@ fn test_register_network_use_symbol_for_subnet_if_available() { // Ensure the symbol correspond to the netuid has been set let expected_symbol = SYMBOLS.get(usize::from(u16::from(netuid))).unwrap(); assert_eq!(TokenSymbol::::get(netuid), *expected_symbol); + + // Check registration allowed + assert!(NetworkRegistrationAllowed::::get(netuid)); + assert!(NetworkPowRegistrationAllowed::::get(netuid)); } }); } @@ -272,6 +280,10 @@ fn test_register_network_use_next_available_symbol_if_symbol_for_subnet_is_taken // Ensure the symbol correspond to the netuid has been set let expected_symbol = SYMBOLS.get(usize::from(u16::from(netuid))).unwrap(); assert_eq!(TokenSymbol::::get(netuid), *expected_symbol); + + // Check registration allowed + assert!(NetworkRegistrationAllowed::::get(netuid)); + assert!(NetworkPowRegistrationAllowed::::get(netuid)); } // Swap some of the network symbol for the network 25 to network 51 symbol (not registered yet) @@ -336,6 +348,10 @@ fn test_register_network_use_default_symbol_if_all_symbols_are_taken() { // We expect the symbol to be the default symbol assert_eq!(TokenSymbol::::get(netuid), *DEFAULT_SYMBOL); + + // Check registration allowed + assert!(NetworkRegistrationAllowed::::get(netuid)); + assert!(NetworkPowRegistrationAllowed::::get(netuid)); }); } // cargo test --package pallet-subtensor --lib -- tests::subnet::test_subtoken_enable --exact --show-output