diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index 2b41539816..5ea88eb76f 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -84,6 +84,13 @@ pub mod pallet { /// Indicates if the Yuma3 enable was enabled or disabled. enabled: bool, }, + /// Event emitted when Bonds Reset is toggled. + BondsResetToggled { + /// The network identifier. + netuid: u16, + /// Indicates if the Bonds Reset was enabled or disabled. + enabled: bool, + }, } // Errors inform users that something went wrong. @@ -1602,6 +1609,34 @@ pub mod pallet { Ok(()) } + /// Enables or disables Bonds Reset for a given subnet. + /// + /// # Parameters + /// - `origin`: The origin of the call, which must be the root account or subnet owner. + /// - `netuid`: The unique identifier for the subnet. + /// - `enabled`: A boolean flag to enable or disable Bonds Reset. + /// + /// # Weight + /// This function has a fixed weight of 0 and is classified as an operational transaction that does not incur any fees. + #[pallet::call_index(70)] + #[pallet::weight((0, DispatchClass::Operational, Pays::No))] + pub fn sudo_set_bonds_reset_enabled( + origin: OriginFor, + netuid: u16, + enabled: bool, + ) -> DispatchResult { + pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin, netuid)?; + pallet_subtensor::Pallet::::set_bonds_reset(netuid, enabled); + + Self::deposit_event(Event::BondsResetToggled { netuid, enabled }); + log::debug!( + "BondsResetToggled( netuid: {:?} bonds_reset: {:?} ) ", + netuid, + enabled + ); + Ok(()) + } + /// Sets or updates the hotkey account associated with the owner of a specific subnet. /// /// This function allows either the root origin or the current subnet owner to set or update diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index bb813ce117..62fdac223d 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -1780,3 +1780,77 @@ fn test_set_sn_owner_hotkey_root() { assert_eq!(actual_hotkey, hotkey); }); } + +#[test] +fn test_sudo_set_bonds_reset_enabled() { + new_test_ext().execute_with(|| { + let netuid: u16 = 1; + let to_be_set: bool = true; + let sn_owner = U256::from(1); + add_network(netuid, 10); + let init_value: bool = SubtensorModule::get_bonds_reset(netuid); + + assert_eq!( + AdminUtils::sudo_set_bonds_reset_enabled( + <::RuntimeOrigin>::signed(U256::from(1)), + netuid, + to_be_set + ), + Err(DispatchError::BadOrigin) + ); + + assert_ok!(AdminUtils::sudo_set_bonds_reset_enabled( + <::RuntimeOrigin>::root(), + netuid, + to_be_set + )); + assert_eq!(SubtensorModule::get_bonds_reset(netuid), to_be_set); + assert_ne!(SubtensorModule::get_bonds_reset(netuid), init_value); + + pallet_subtensor::SubnetOwner::::insert(netuid, sn_owner); + + assert_ok!(AdminUtils::sudo_set_bonds_reset_enabled( + <::RuntimeOrigin>::signed(sn_owner), + netuid, + !to_be_set + )); + assert_eq!(SubtensorModule::get_bonds_reset(netuid), !to_be_set); + }); +} + +#[test] +fn test_sudo_set_yuma3_enabled() { + new_test_ext().execute_with(|| { + let netuid: u16 = 1; + let to_be_set: bool = true; + let sn_owner = U256::from(1); + add_network(netuid, 10); + let init_value: bool = SubtensorModule::get_yuma3_enabled(netuid); + + assert_eq!( + AdminUtils::sudo_set_yuma3_enabled( + <::RuntimeOrigin>::signed(U256::from(1)), + netuid, + to_be_set + ), + Err(DispatchError::BadOrigin) + ); + + assert_ok!(AdminUtils::sudo_set_yuma3_enabled( + <::RuntimeOrigin>::root(), + netuid, + to_be_set + )); + assert_eq!(SubtensorModule::get_yuma3_enabled(netuid), to_be_set); + assert_ne!(SubtensorModule::get_yuma3_enabled(netuid), init_value); + + pallet_subtensor::SubnetOwner::::insert(netuid, sn_owner); + + assert_ok!(AdminUtils::sudo_set_yuma3_enabled( + <::RuntimeOrigin>::signed(sn_owner), + netuid, + !to_be_set + )); + assert_eq!(SubtensorModule::get_yuma3_enabled(netuid), !to_be_set); + }); +} diff --git a/precompiles/src/solidity/subnet.abi b/precompiles/src/solidity/subnet.abi index a2849a0cbe..35f8a06571 100644 --- a/precompiles/src/solidity/subnet.abi +++ b/precompiles/src/solidity/subnet.abi @@ -80,6 +80,25 @@ "stateMutability": "view", "type": "function" }, + { + "inputs": [ + { + "internalType": "uint16", + "name": "netuid", + "type": "uint16" + } + ], + "name": "getBondsResetEnabled", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, { "inputs": [ { @@ -579,6 +598,24 @@ "stateMutability": "payable", "type": "function" }, + { + "inputs": [ + { + "internalType": "uint16", + "name": "netuid", + "type": "uint16" + }, + { + "internalType": "bool", + "name": "bondsResetEnabled", + "type": "bool" + } + ], + "name": "setBondsResetEnabled", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, { "inputs": [ { diff --git a/precompiles/src/solidity/subnet.sol b/precompiles/src/solidity/subnet.sol index 2fa9d3f550..b1da53944e 100644 --- a/precompiles/src/solidity/subnet.sol +++ b/precompiles/src/solidity/subnet.sol @@ -159,6 +159,14 @@ interface ISubnet { bool yuma3Enabled ) external payable; + function getBondsResetEnabled(uint16 netuid) external view returns (bool); + + function setBondsResetEnabled( + uint16 netuid, + bool bondsResetEnabled + ) external payable; + + function getAlphaValues( uint16 netuid ) external view returns (uint16, uint16); diff --git a/precompiles/src/subnet.rs b/precompiles/src/subnet.rs index 7d4dd175e3..20abe48b40 100644 --- a/precompiles/src/subnet.rs +++ b/precompiles/src/subnet.rs @@ -577,6 +577,12 @@ where Ok(pallet_subtensor::Yuma3On::::get(netuid)) } + #[precompile::public("getBondsResetEnabled(uint16)")] + #[precompile::view] + fn get_bonds_reset_enabled(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + Ok(pallet_subtensor::BondsResetOn::::get(netuid)) + } + #[precompile::public("setYuma3Enabled(uint16,bool)")] #[precompile::payable] fn set_yuma3_enabled( @@ -592,6 +598,21 @@ where ) } + #[precompile::public("setBondsResetEnabled(uint16,bool)")] + #[precompile::payable] + fn set_bonds_reset_enabled( + handle: &mut impl PrecompileHandle, + netuid: u16, + enabled: bool, + ) -> EvmResult<()> { + let call = pallet_admin_utils::Call::::sudo_set_bonds_reset_enabled { netuid, enabled }; + + handle.try_dispatch_runtime_call::( + call, + RawOrigin::Signed(handle.caller_account_id::()), + ) + } + #[precompile::public("getAlphaValues(uint16)")] #[precompile::view] fn get_alpha_values(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<(u16, u16)> {