From 0291efbb90ea29cd2cb426365a46e0221bf9589d Mon Sep 17 00:00:00 2001 From: Keith Date: Wed, 7 May 2025 10:16:38 +0800 Subject: [PATCH 1/4] Make setting AdjustmentAlpha root only --- .../subnet.precompile.hyperparameter.test.ts | 29 ++++----- pallets/admin-utils/src/lib.rs | 2 +- pallets/subtensor/src/macros/hooks.rs | 4 +- .../migrate_reset_adjustment_alpha.rs | 59 +++++++++++++++++++ pallets/subtensor/src/migrations/mod.rs | 1 + precompiles/src/subnet.rs | 17 ++---- 6 files changed, 84 insertions(+), 28 deletions(-) create mode 100644 pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs diff --git a/evm-tests/test/subnet.precompile.hyperparameter.test.ts b/evm-tests/test/subnet.precompile.hyperparameter.test.ts index e7b5a1ee0d..867761ab31 100644 --- a/evm-tests/test/subnet.precompile.hyperparameter.test.ts +++ b/evm-tests/test/subnet.precompile.hyperparameter.test.ts @@ -163,26 +163,27 @@ describe("Test the Subnet precompile contract", () => { // assert.equal(valueFromContract, onchainValue); // }) - it("Can set adjustmentAlpha parameter", async () => { + // need sudo as origin now + // it("Can set adjustmentAlpha parameter", async () => { - const totalNetwork = await api.query.SubtensorModule.TotalNetworks.getValue() - const contract = new ethers.Contract(ISUBNET_ADDRESS, ISubnetABI, wallet); - const netuid = totalNetwork - 1; + // const totalNetwork = await api.query.SubtensorModule.TotalNetworks.getValue() + // const contract = new ethers.Contract(ISUBNET_ADDRESS, ISubnetABI, wallet); + // const netuid = totalNetwork - 1; - const newValue = 105; - const tx = await contract.setAdjustmentAlpha(netuid, newValue); - await tx.wait(); + // const newValue = 105; + // const tx = await contract.setAdjustmentAlpha(netuid, newValue); + // await tx.wait(); - let onchainValue = await api.query.SubtensorModule.AdjustmentAlpha.getValue(netuid) + // let onchainValue = await api.query.SubtensorModule.AdjustmentAlpha.getValue(netuid) - let valueFromContract = Number( - await contract.getAdjustmentAlpha(netuid) - ); + // let valueFromContract = Number( + // await contract.getAdjustmentAlpha(netuid) + // ); - assert.equal(valueFromContract, newValue) - assert.equal(valueFromContract, onchainValue); - }) + // assert.equal(valueFromContract, newValue) + // assert.equal(valueFromContract, onchainValue); + // }) it("Can set maxWeightLimit parameter", async () => { diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index c2e391360a..92068c07d9 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -374,7 +374,7 @@ pub mod pallet { netuid: u16, adjustment_alpha: u64, ) -> DispatchResult { - pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin, netuid)?; + ensure_root(origin)?; ensure!( pallet_subtensor::Pallet::::if_subnet_exist(netuid), diff --git a/pallets/subtensor/src/macros/hooks.rs b/pallets/subtensor/src/macros/hooks.rs index 78de392218..50b1ecad2f 100644 --- a/pallets/subtensor/src/macros/hooks.rs +++ b/pallets/subtensor/src/macros/hooks.rs @@ -115,7 +115,9 @@ mod hooks { // Reset max burn .saturating_add(migrations::migrate_reset_max_burn::migrate_reset_max_burn::()) // Migrate ColdkeySwapScheduled structure to new format - .saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::()); + .saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::()) + // Reset adjustment alpha + .saturating_add(migrations::migrate_reset_adjustment_alpha::migrate_reset_adjustment_alpha::()); weight } diff --git a/pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs b/pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs new file mode 100644 index 0000000000..1f777c2b72 --- /dev/null +++ b/pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs @@ -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() -> 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::::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.5 if the value exceeds 0.5 + // ------------------------------ + + let mut reset_entries_count = 0u64; + + for netuid in AdjustmentAlpha::::iter_keys() { + AdjustmentAlpha::::mutate(netuid, |adjustment| { + *adjustment = (*adjustment).min(32768); // 0.5 + }); + 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::::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 +} diff --git a/pallets/subtensor/src/migrations/mod.rs b/pallets/subtensor/src/migrations/mod.rs index 5c6347034f..5f7f17ca68 100644 --- a/pallets/subtensor/src/migrations/mod.rs +++ b/pallets/subtensor/src/migrations/mod.rs @@ -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; diff --git a/precompiles/src/subnet.rs b/precompiles/src/subnet.rs index cf2b71bcd2..f49efb46b6 100644 --- a/precompiles/src/subnet.rs +++ b/precompiles/src/subnet.rs @@ -217,19 +217,12 @@ where #[precompile::public("setAdjustmentAlpha(uint16,uint64)")] #[precompile::payable] fn set_adjustment_alpha( - handle: &mut impl PrecompileHandle, - netuid: u16, - adjustment_alpha: u64, + _handle: &mut impl PrecompileHandle, + _netuid: u16, + _adjustment_alpha: u64, ) -> EvmResult<()> { - let call = pallet_admin_utils::Call::::sudo_set_adjustment_alpha { - netuid, - adjustment_alpha, - }; - - handle.try_dispatch_runtime_call::( - call, - RawOrigin::Signed(handle.caller_account_id::()), - ) + // DEPRECATED. Subnet owner cannot set adjustment alpha + Ok(()) } #[precompile::public("getMaxWeightLimit(uint16)")] From 4ba1b8fd3f46ecbf84061dc3106947f5fa47ced5 Mon Sep 17 00:00:00 2001 From: Keith Date: Wed, 7 May 2025 18:09:55 +0800 Subject: [PATCH 2/4] Allow owner to set AdjustmentAlpha iff the value is between 0 and 0.5 --- .../subnet.precompile.hyperparameter.test.ts | 29 +++++++++---------- pallets/admin-utils/src/lib.rs | 11 ++++++- .../migrate_reset_adjustment_alpha.rs | 2 +- precompiles/src/subnet.rs | 17 +++++++---- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/evm-tests/test/subnet.precompile.hyperparameter.test.ts b/evm-tests/test/subnet.precompile.hyperparameter.test.ts index 867761ab31..e7b5a1ee0d 100644 --- a/evm-tests/test/subnet.precompile.hyperparameter.test.ts +++ b/evm-tests/test/subnet.precompile.hyperparameter.test.ts @@ -163,27 +163,26 @@ describe("Test the Subnet precompile contract", () => { // assert.equal(valueFromContract, onchainValue); // }) - // need sudo as origin now - // it("Can set adjustmentAlpha parameter", async () => { + it("Can set adjustmentAlpha parameter", async () => { - // const totalNetwork = await api.query.SubtensorModule.TotalNetworks.getValue() - // const contract = new ethers.Contract(ISUBNET_ADDRESS, ISubnetABI, wallet); - // const netuid = totalNetwork - 1; + const totalNetwork = await api.query.SubtensorModule.TotalNetworks.getValue() + const contract = new ethers.Contract(ISUBNET_ADDRESS, ISubnetABI, wallet); + const netuid = totalNetwork - 1; - // const newValue = 105; - // const tx = await contract.setAdjustmentAlpha(netuid, newValue); - // await tx.wait(); + const newValue = 105; + const tx = await contract.setAdjustmentAlpha(netuid, newValue); + await tx.wait(); - // let onchainValue = await api.query.SubtensorModule.AdjustmentAlpha.getValue(netuid) + let onchainValue = await api.query.SubtensorModule.AdjustmentAlpha.getValue(netuid) - // let valueFromContract = Number( - // await contract.getAdjustmentAlpha(netuid) - // ); + let valueFromContract = Number( + await contract.getAdjustmentAlpha(netuid) + ); - // assert.equal(valueFromContract, newValue) - // assert.equal(valueFromContract, onchainValue); - // }) + assert.equal(valueFromContract, newValue) + assert.equal(valueFromContract, onchainValue); + }) it("Can set maxWeightLimit parameter", async () => { diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 92068c07d9..14ab7bc160 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -90,6 +90,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)] @@ -374,7 +376,14 @@ pub mod pallet { netuid: u16, adjustment_alpha: u64, ) -> DispatchResult { - ensure_root(origin)?; + pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin, netuid)?; + + if pallet_subtensor::Pallet::::ensure_subnet_owner(origin, netuid).is_ok() { + ensure!( + adjustment_alpha <= (u64::MAX.saturating_div(2)), + Error::::AdjustmentAlphaMaxReached + ) + } ensure!( pallet_subtensor::Pallet::::if_subnet_exist(netuid), diff --git a/pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs b/pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs index 1f777c2b72..856df851b8 100644 --- a/pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs +++ b/pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs @@ -31,7 +31,7 @@ pub fn migrate_reset_adjustment_alpha() -> Weight { for netuid in AdjustmentAlpha::::iter_keys() { AdjustmentAlpha::::mutate(netuid, |adjustment| { - *adjustment = (*adjustment).min(32768); // 0.5 + *adjustment = (*adjustment).min(u64::MAX.saturating_div(2)); // 0.5 }); reset_entries_count = reset_entries_count.saturating_add(1); } diff --git a/precompiles/src/subnet.rs b/precompiles/src/subnet.rs index f49efb46b6..cf2b71bcd2 100644 --- a/precompiles/src/subnet.rs +++ b/precompiles/src/subnet.rs @@ -217,12 +217,19 @@ where #[precompile::public("setAdjustmentAlpha(uint16,uint64)")] #[precompile::payable] fn set_adjustment_alpha( - _handle: &mut impl PrecompileHandle, - _netuid: u16, - _adjustment_alpha: u64, + handle: &mut impl PrecompileHandle, + netuid: u16, + adjustment_alpha: u64, ) -> EvmResult<()> { - // DEPRECATED. Subnet owner cannot set adjustment alpha - Ok(()) + let call = pallet_admin_utils::Call::::sudo_set_adjustment_alpha { + netuid, + adjustment_alpha, + }; + + handle.try_dispatch_runtime_call::( + call, + RawOrigin::Signed(handle.caller_account_id::()), + ) } #[precompile::public("getMaxWeightLimit(uint16)")] From 6e5dd5154f0901e90c0ce710be4606001bafd19b Mon Sep 17 00:00:00 2001 From: Keith Date: Wed, 7 May 2025 21:06:51 +0800 Subject: [PATCH 3/4] Add test --- pallets/admin-utils/src/lib.rs | 2 +- pallets/admin-utils/src/tests/mod.rs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 14ab7bc160..0d7f0079cf 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -376,7 +376,7 @@ pub mod pallet { netuid: u16, adjustment_alpha: u64, ) -> DispatchResult { - pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin, netuid)?; + pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin.clone(), netuid)?; if pallet_subtensor::Pallet::::ensure_subnet_owner(origin, netuid).is_ok() { ensure!( diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index bb813ce117..a2105b2c48 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -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::::insert(netuid, coldkey); assert_eq!( AdminUtils::sudo_set_adjustment_alpha( - <::RuntimeOrigin>::signed(U256::from(1)), + <::RuntimeOrigin>::signed(U256::from(100)), netuid, to_be_set ), @@ -358,9 +360,17 @@ fn test_sudo_set_adjustment_alpha() { ), Err(Error::::SubnetDoesNotExist.into()) ); + assert_eq!( + AdminUtils::sudo_set_adjustment_alpha( + <::RuntimeOrigin>::signed(coldkey), + netuid, + u64::MAX + ), + Err(Error::::AdjustmentAlphaMaxReached.into()) + ); assert_eq!(SubtensorModule::get_adjustment_alpha(netuid), init_value); assert_ok!(AdminUtils::sudo_set_adjustment_alpha( - <::RuntimeOrigin>::root(), + <::RuntimeOrigin>::signed(coldkey), netuid, to_be_set )); From 9fe8a9a569c9c40a84a7e2f20045deb2604076ec Mon Sep 17 00:00:00 2001 From: Keith Date: Mon, 12 May 2025 17:58:24 +0800 Subject: [PATCH 4/4] Change cap to 0.97 --- pallets/admin-utils/src/lib.rs | 2 +- .../src/migrations/migrate_reset_adjustment_alpha.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 0d7f0079cf..3822c966ad 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -380,7 +380,7 @@ pub mod pallet { if pallet_subtensor::Pallet::::ensure_subnet_owner(origin, netuid).is_ok() { ensure!( - adjustment_alpha <= (u64::MAX.saturating_div(2)), + adjustment_alpha <= (u64::MAX.saturating_div(100).saturating_mul(97)), // 0.97 Error::::AdjustmentAlphaMaxReached ) } diff --git a/pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs b/pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs index 856df851b8..ab4e5975e8 100644 --- a/pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs +++ b/pallets/subtensor/src/migrations/migrate_reset_adjustment_alpha.rs @@ -24,14 +24,14 @@ pub fn migrate_reset_adjustment_alpha() -> Weight { ); // ------------------------------ - // Step 1: Reset all subnet's adjustment alpha to 0.5 if the value exceeds 0.5 + // 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::::iter_keys() { AdjustmentAlpha::::mutate(netuid, |adjustment| { - *adjustment = (*adjustment).min(u64::MAX.saturating_div(2)); // 0.5 + *adjustment = (*adjustment).min(u64::MAX.saturating_div(100).saturating_mul(97)); // 0.97 }); reset_entries_count = reset_entries_count.saturating_add(1); }