From e1184990aac96a34b0e063cb835bd83e388f8111 Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Thu, 19 Dec 2024 18:06:11 -0500 Subject: [PATCH 1/5] Remove check for minimum stake for revoking childkeys --- .../subtensor/src/coinbase/run_coinbase.rs | 24 +- pallets/subtensor/src/staking/set_children.rs | 4 +- pallets/subtensor/src/tests/children.rs | 109 ++ runtime/src/precompiles/subnet.rs | 987 ++++++++++++++++++ 4 files changed, 1111 insertions(+), 13 deletions(-) create mode 100644 runtime/src/precompiles/subnet.rs diff --git a/pallets/subtensor/src/coinbase/run_coinbase.rs b/pallets/subtensor/src/coinbase/run_coinbase.rs index 04574733a..f2b1cea99 100644 --- a/pallets/subtensor/src/coinbase/run_coinbase.rs +++ b/pallets/subtensor/src/coinbase/run_coinbase.rs @@ -105,7 +105,10 @@ impl Pallet { }; } - // --- 4.3 Drain the subnet emission. + // 4.3 Apply pending childkeys of this subnet for the next epoch + Self::do_set_pending_children(*netuid); + + // --- 4.4 Drain the subnet emission. let mut subnet_emission: u64 = PendingEmission::::get(*netuid); PendingEmission::::insert(*netuid, 0); log::debug!( @@ -114,7 +117,7 @@ impl Pallet { subnet_emission ); - // --- 4.4 Set last step counter. + // --- 4.5 Set last step counter. Self::set_blocks_since_last_step(*netuid, 0); Self::set_last_mechanism_step_block(*netuid, current_block); @@ -123,30 +126,30 @@ impl Pallet { continue; } - // --- 4.5 Distribute owner take. + // --- 4.6 Distribute owner take. if SubnetOwner::::contains_key(netuid) { // Does the subnet have an owner? - // --- 4.5.1 Compute the subnet owner cut. + // --- 4.6.1 Compute the subnet owner cut. let owner_cut: I96F32 = I96F32::from_num(subnet_emission).saturating_mul( I96F32::from_num(Self::get_subnet_owner_cut()) .saturating_div(I96F32::from_num(u16::MAX)), ); - // --- 4.5.2 Remove the cut from the subnet emission + // --- 4.6.2 Remove the cut from the subnet emission subnet_emission = subnet_emission.saturating_sub(owner_cut.to_num::()); - // --- 4.5.3 Add the cut to the balance of the owner + // --- 4.6.3 Add the cut to the balance of the owner Self::add_balance_to_coldkey_account( &Self::get_subnet_owner(*netuid), owner_cut.to_num::(), ); - // --- 4.5.4 Increase total issuance on the chain. + // --- 4.6.4 Increase total issuance on the chain. Self::coinbase(owner_cut.to_num::()); } - // 4.6 Pass emission through epoch() --> hotkey emission. + // 4.7 Pass emission through epoch() --> hotkey emission. let hotkey_emission: Vec<(T::AccountId, u64, u64)> = Self::epoch(*netuid, subnet_emission); log::debug!( @@ -155,7 +158,7 @@ impl Pallet { hotkey_emission ); - // 4.7 Accumulate the tuples on hotkeys: + // 4.8 Accumulate the tuples on hotkeys: for (hotkey, mining_emission, validator_emission) in hotkey_emission { // 4.8 Accumulate the emission on the hotkey and parent hotkeys. Self::accumulate_hotkey_emission( @@ -166,9 +169,6 @@ impl Pallet { ); log::debug!("Accumulated emissions on hotkey {:?} for netuid {:?}: mining {:?}, validator {:?}", hotkey, *netuid, mining_emission, validator_emission); } - - // 4.5 Apply pending childkeys of this subnet for the next epoch - Self::do_set_pending_children(*netuid); } else { // No epoch, increase blocks since last step and continue Self::set_blocks_since_last_step( diff --git a/pallets/subtensor/src/staking/set_children.rs b/pallets/subtensor/src/staking/set_children.rs index 682475abe..c47dfa0ca 100644 --- a/pallets/subtensor/src/staking/set_children.rs +++ b/pallets/subtensor/src/staking/set_children.rs @@ -110,10 +110,12 @@ impl Pallet { } // Check that the parent key has at least the minimum own stake + // if children vector is not empty // (checking with check_weights_min_stake wouldn't work because it considers // grandparent stake in this case) ensure!( - Self::get_total_stake_for_hotkey(&hotkey) >= StakeThreshold::::get(), + children.is_empty() + || Self::get_total_stake_for_hotkey(&hotkey) >= StakeThreshold::::get(), Error::::NotEnoughStakeToSetChildkeys ); diff --git a/pallets/subtensor/src/tests/children.rs b/pallets/subtensor/src/tests/children.rs index 10087d7b2..4c833b00e 100644 --- a/pallets/subtensor/src/tests/children.rs +++ b/pallets/subtensor/src/tests/children.rs @@ -3747,3 +3747,112 @@ fn test_do_set_child_cooldown_period() { assert_eq!(children_after, vec![(proportion, child)]); }); } + +// Test that setting childkeys requires minimum stake +#[test] +fn test_do_set_child_min_stake_check() { + new_test_ext(1).execute_with(|| { + let coldkey = U256::from(1); + let parent = U256::from(2); + let child = U256::from(3); + let netuid: u16 = 1; + let proportion: u64 = 1000; + + // Add network and register hotkey + add_network(netuid, 13, 0); + register_ok_neuron(netuid, parent, coldkey, 0); + + // Set below minimum stake for setting children + let parent_total_stake_original = TotalHotkeyStake::::get(parent); + StakeThreshold::::put(1_000_000_000_000); + TotalHotkeyStake::::insert(parent, StakeThreshold::::get() - 1); + + // Schedule parent-child relationship + assert_err!( + SubtensorModule::do_schedule_children( + RuntimeOrigin::signed(coldkey), + parent, + netuid, + vec![(proportion, child)], + ), + Error::::NotEnoughStakeToSetChildkeys + ); + + // Ensure the childkeys are not yet applied + let children_before = SubtensorModule::get_children(&parent, netuid); + assert_eq!(children_before, vec![]); + + wait_and_set_pending_children(netuid); + TotalHotkeyStake::::insert(parent, parent_total_stake_original); + + // Ensure the childkeys are still not applied + let children_after = SubtensorModule::get_children(&parent, netuid); + assert_eq!(children_after, vec![]); + }); +} + +// Test that revoking childkeys does not require minimum stake +#[test] +fn test_revoke_child_no_min_stake_check() { + new_test_ext(1).execute_with(|| { + let coldkey = U256::from(1); + let parent = U256::from(2); + let child = U256::from(3); + let netuid: u16 = 1; + let proportion: u64 = 1000; + + // Add network and register hotkey + add_network(netuid, 13, 0); + register_ok_neuron(netuid, parent, coldkey, 0); + + // Set below minimum stake for setting children + let parent_total_stake_original = TotalHotkeyStake::::get(parent); + StakeThreshold::::put(1_000_000_000_000); + TotalHotkeyStake::::insert(parent, StakeThreshold::::get()); + + // Schedule parent-child relationship + assert_ok!(SubtensorModule::do_schedule_children( + RuntimeOrigin::signed(coldkey), + parent, + netuid, + vec![(proportion, child)], + )); + + // Ensure the childkeys are not yet applied + let children_before = SubtensorModule::get_children(&parent, netuid); + assert_eq!(children_before, vec![]); + + wait_and_set_pending_children(netuid); + TotalHotkeyStake::::insert(parent, parent_total_stake_original); + + // Ensure the childkeys are applied + let children_after = SubtensorModule::get_children(&parent, netuid); + assert_eq!(children_after, vec![(proportion, child)]); + + // Reduce the stake below required threshold + TotalHotkeyStake::::insert(parent, StakeThreshold::::get() - 1); + + // Bypass tx rate limit + SubtensorModule::set_last_transaction_block_on_subnet( + &parent, + netuid, + &TransactionType::SetChildren, + 0, + ); + + // Schedule parent-child relationship revokation + assert_ok!(SubtensorModule::do_schedule_children( + RuntimeOrigin::signed(coldkey), + parent, + netuid, + vec![], + )); + + wait_and_set_pending_children(netuid); + TotalHotkeyStake::::insert(parent, parent_total_stake_original); + + // Ensure the childkeys are revoked + let children_after = SubtensorModule::get_children(&parent, netuid); + assert_eq!(children_after, vec![]); + }); +} diff --git a/runtime/src/precompiles/subnet.rs b/runtime/src/precompiles/subnet.rs new file mode 100644 index 000000000..04fe171fb --- /dev/null +++ b/runtime/src/precompiles/subnet.rs @@ -0,0 +1,987 @@ +use crate::precompiles::{dispatch, get_method_id, get_slice}; +use crate::{Runtime, RuntimeCall}; +use pallet_evm::{ + ExitError, ExitSucceed, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, +}; +use sp_core::U256; +use sp_std::vec; + +pub const SUBNET_PRECOMPILE_INDEX: u64 = 2051; +// three bytes with max lenght 1K +pub const MAX_PARAMETER_SIZE: usize = 3 * 1024; + +// this is staking smart contract's(0x0000000000000000000000000000000000000803) sr25519 address +pub const SUBNET_CONTRACT_ADDRESS: &str = "5DPSUCb5mZFfizvBDSnRoAqmxV5Bmov2CS3xV773qU6VP1w2"; + +pub struct SubnetPrecompile; + +impl SubnetPrecompile { + pub fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult { + let txdata = handle.input(); + if txdata.len() > MAX_PARAMETER_SIZE { + log::error!("the length of subnet call is {} ", txdata.len()); + return Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + }); + } + let method_id = get_slice(txdata, 0, 4)?; + let method_input = txdata + .get(4..) + .map_or_else(vec::Vec::new, |slice| slice.to_vec()); // Avoiding borrowing conflicts + + log::warn!("========== precompile execute: method id = {:?}", method_id); + + match method_id { + id if id == get_method_id("registerNetwork(bytes,bytes,bytes)") => { + Self::register_network(handle, &method_input) + } + id if id == get_method_id("registerNetwork()") => { + Self::register_network(handle, &[0_u8; 0]) + } + + id if id == get_method_id("getServingRateLimit(uint16)") => { + Self::get_serving_rate_limit(&method_input) + } + id if id == get_method_id("setServingRateLimit(uint256,uint256)") => { + Self::set_serving_rate_limit(handle, &method_input) + } + + id if id == get_method_id("getMinDifficulty(uint16)") => { + Self::get_min_difficulty(&method_input) + } + id if id == get_method_id("setMinDifficulty(uint16,uint64)") => { + Self::set_min_difficulty(handle, &method_input) + } + + id if id == get_method_id("getMaxDifficulty(uint16)") => { + Self::get_max_difficulty(&method_input) + } + id if id == get_method_id("setMaxDifficulty(uint16,uint64)") => { + Self::set_max_difficulty(handle, &method_input) + } + + id if id == get_method_id("getWeightsVersionKey(uint16)") => { + Self::get_weights_version_key(&method_input) + } + id if id == get_method_id("setWeightsVersionKey(uint16,uint64)") => { + Self::set_weights_version_key(handle, &method_input) + } + + id if id == get_method_id("getWeightsSetRateLimit(uint16)") => { + Self::get_weights_set_rate_limit(&method_input) + } + id if id == get_method_id("setWeightsSetRateLimit(uint16,uint64)") => { + Self::set_weights_set_rate_limit(handle, &method_input) + } + + id if id == get_method_id("getAdjustmentAlpha(uint16)") => { + Self::get_adjustment_alpha(&method_input) + } + id if id == get_method_id("setAdjustmentAlpha(uint16,uint64)") => { + Self::set_adjustment_alpha(handle, &method_input) + } + + id if id == get_method_id("getMaxWeightLimit(uint16)") => { + Self::get_max_weight_limit(&method_input) + } + id if id == get_method_id("setMaxWeightLimit(uint16,uint64)") => { + Self::set_max_weight_limit(handle, &method_input) + } + + id if id == get_method_id("getImmunityPeriod(uint16)") => { + Self::get_immunity_period(&method_input) + } + id if id == get_method_id("setImmunityPeriod(uint16,uint64)") => { + Self::set_immunity_period(handle, &method_input) + } + + id if id == get_method_id("getMinAllowedWeights(uint16)") => { + Self::get_min_allowed_weights(&method_input) + } + id if id == get_method_id("setMinAllowedWeights(uint16,uint16)") => { + Self::set_min_allowed_weights(handle, &method_input) + } + + id if id == get_method_id("getKappa(uint16)") => Self::get_kappa(&method_input), + id if id == get_method_id("setKappa(uint16,uint16)") => { + Self::set_kappa(handle, &method_input) + } + + id if id == get_method_id("getRho(uint16)") => Self::get_rho(&method_input), + id if id == get_method_id("setRho(uint16,uint16)") => { + Self::set_rho(handle, &method_input) + } + + id if id == get_method_id("getActivityCutoff(uint16)") => { + Self::get_activity_cutoff(&method_input) + } + id if id == get_method_id("setActivityCutoff(uint16,uint16)") => { + Self::set_activity_cutoff(handle, &method_input) + } + + id if id == get_method_id("getNetworkRegistrationAllowed(uint16)") => { + Self::get_network_registration_allowed(&method_input) + } + id if id == get_method_id("setNetworkRegistrationAllowed(uint16,bool)") => { + Self::set_network_registration_allowed(handle, &method_input) + } + + id if id == get_method_id("getNetworkPowRegistrationAllowed(uint16)") => { + Self::get_network_pow_registration_allowed(&method_input) + } + id if id == get_method_id("setNetworkPowRegistrationAllowed(uint16,bool)") => { + Self::set_network_pow_registration_allowed(handle, &method_input) + } + + id if id == get_method_id("getMinBurn(uint16)") => Self::get_min_burn(&method_input), + id if id == get_method_id("setMinBurn(uint16,uint64)") => { + Self::set_min_burn(handle, &method_input) + } + + id if id == get_method_id("getMaxBurn(uint16)") => Self::get_max_burn(&method_input), + id if id == get_method_id("setMaxBurn(uint16,uint64)") => { + Self::set_max_burn(handle, &method_input) + } + + id if id == get_method_id("getDifficulty(uint16)") => { + Self::get_difficulty(&method_input) + } + id if id == get_method_id("setDifficulty(uint16,uint64)") => { + Self::set_difficulty(handle, &method_input) + } + + id if id == get_method_id("getBondsMovingAverage(uint16)") => { + Self::get_bonds_moving_average(&method_input) + } + id if id == get_method_id("setBondsMovingAverage(uint16,uint64)") => { + Self::set_bonds_moving_average(handle, &method_input) + } + + id if id == get_method_id("getCommitRevealWeightsEnabled(uint16)") => { + Self::get_commit_reveal_weights_enabled(&method_input) + } + id if id == get_method_id("setCommitRevealWeightsEnabled(uint16,bool)") => { + Self::set_commit_reveal_weights_enabled(handle, &method_input) + } + + id if id == get_method_id("getLiquidAlphaEnabled(uint16)") => { + Self::get_liquid_alpha_enabled(&method_input) + } + id if id == get_method_id("setLiquidAlphaEnabled(uint16,bool)") => { + Self::set_liquid_alpha_enabled(handle, &method_input) + } + + id if id == get_method_id("getAlphaValues(uint16)") => { + Self::get_alpha_values(&method_input) + } + id if id == get_method_id("setAlphaValues(uint16,uint16,uint16)") => { + Self::set_alpha_values(handle, &method_input) + } + + id if id == get_method_id("getCommitRevealWeightsInterval(uint16)") => { + Self::get_commit_reveal_weights_interval(&method_input) + } + id if id == get_method_id("setCommitRevealWeightsInterval(uint16,uint64)") => { + Self::set_commit_reveal_weights_interval(handle, &method_input) + } + _ => Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + }), + } + } + + fn register_network(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let call = if data.is_empty() { + RuntimeCall::SubtensorModule( + pallet_subtensor::Call::::register_network_with_identity { + identity: None, + }, + ) + } else { + let (subnet_name, github_repo, subnet_contact) = + Self::parse_register_network_parameters(data)?; + + let identity: pallet_subtensor::SubnetIdentityOf = pallet_subtensor::SubnetIdentityOf { + subnet_name, + github_repo, + subnet_contact, + }; + + // Create the register_network callcle + RuntimeCall::SubtensorModule( + pallet_subtensor::Call::::register_network_with_identity { + identity: Some(identity), + }, + ) + }; + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_serving_rate_limit(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::ServingRateLimit::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_serving_rate_limit(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, serving_rate_limit) = Self::parse_netuid_u64_parameter(data)?; + + log::warn!("==========set_serving_rate_limit: netuid = {:?}, serving_rate_limit = {:?}", netuid, serving_rate_limit); + + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_serving_rate_limit { + netuid, + serving_rate_limit, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_min_difficulty(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::MinDifficulty::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_min_difficulty(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, min_difficulty) = Self::parse_netuid_u64_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_min_difficulty { + netuid, + min_difficulty, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_max_difficulty(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::MaxDifficulty::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_max_difficulty(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, max_difficulty) = Self::parse_netuid_u64_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_max_difficulty { + netuid, + max_difficulty, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_weights_version_key(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::WeightsVersionKey::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_weights_version_key( + handle: &mut impl PrecompileHandle, + data: &[u8], + ) -> PrecompileResult { + let (netuid, weights_version_key) = Self::parse_netuid_u64_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_weights_version_key { + netuid, + weights_version_key, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_weights_set_rate_limit(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::WeightsSetRateLimit::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_weights_set_rate_limit( + handle: &mut impl PrecompileHandle, + data: &[u8], + ) -> PrecompileResult { + let (netuid, weights_set_rate_limit) = Self::parse_netuid_u64_parameter(data)?; + + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_weights_set_rate_limit { + netuid, + weights_set_rate_limit, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_adjustment_alpha(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::AdjustmentAlpha::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_adjustment_alpha(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, adjustment_alpha) = Self::parse_netuid_u64_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_adjustment_alpha { + netuid, + adjustment_alpha, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_max_weight_limit(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::MaxWeightsLimit::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_max_weight_limit(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, max_weight_limit) = Self::parse_netuid_u16_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_max_weight_limit { + netuid, + max_weight_limit, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_immunity_period(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::ImmunityPeriod::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_immunity_period(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, immunity_period) = Self::parse_netuid_u16_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_immunity_period { + netuid, + immunity_period, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_min_allowed_weights(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::MinAllowedWeights::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_min_allowed_weights( + handle: &mut impl PrecompileHandle, + data: &[u8], + ) -> PrecompileResult { + let (netuid, min_allowed_weights) = Self::parse_netuid_u16_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_min_allowed_weights { + netuid, + min_allowed_weights, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_kappa(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::Kappa::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_kappa(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, kappa) = Self::parse_netuid_u16_parameter(data)?; + let call = RuntimeCall::AdminUtils(pallet_admin_utils::Call::::sudo_set_kappa { + netuid, + kappa, + }); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_rho(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::Rho::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_rho(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, rho) = Self::parse_netuid_u16_parameter(data)?; + let call = RuntimeCall::AdminUtils(pallet_admin_utils::Call::::sudo_set_rho { + netuid, + rho, + }); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_activity_cutoff(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::ActivityCutoff::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_activity_cutoff(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, activity_cutoff) = Self::parse_netuid_u16_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_activity_cutoff { + netuid, + activity_cutoff, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_network_registration_allowed(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::NetworkRegistrationAllowed::::get(netuid); + + let value_u256 = if value { U256::from(1) } else { U256::from(0) }; + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_network_registration_allowed( + handle: &mut impl PrecompileHandle, + data: &[u8], + ) -> PrecompileResult { + let (netuid, registration_allowed) = Self::parse_netuid_bool_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_network_registration_allowed { + netuid, + registration_allowed, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_network_pow_registration_allowed(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::NetworkPowRegistrationAllowed::::get(netuid); + + let value_u256 = if value { U256::from(1) } else { U256::from(0) }; + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_network_pow_registration_allowed( + handle: &mut impl PrecompileHandle, + data: &[u8], + ) -> PrecompileResult { + let (netuid, registration_allowed) = Self::parse_netuid_bool_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_network_pow_registration_allowed { + netuid, + registration_allowed, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_min_burn(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::MinBurn::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_min_burn(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, min_burn) = Self::parse_netuid_u64_parameter(data)?; + let call = + RuntimeCall::AdminUtils(pallet_admin_utils::Call::::sudo_set_min_burn { + netuid, + min_burn, + }); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_max_burn(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::MaxBurn::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_max_burn(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, max_burn) = Self::parse_netuid_u64_parameter(data)?; + let call = + RuntimeCall::AdminUtils(pallet_admin_utils::Call::::sudo_set_max_burn { + netuid, + max_burn, + }); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_difficulty(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::Difficulty::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_difficulty(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, difficulty) = Self::parse_netuid_u64_parameter(data)?; + let call = + RuntimeCall::AdminUtils(pallet_admin_utils::Call::::sudo_set_difficulty { + netuid, + difficulty, + }); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_bonds_moving_average(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::BondsMovingAverage::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_bonds_moving_average( + handle: &mut impl PrecompileHandle, + data: &[u8], + ) -> PrecompileResult { + let (netuid, bonds_moving_average) = Self::parse_netuid_u64_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_bonds_moving_average { + netuid, + bonds_moving_average, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_commit_reveal_weights_enabled(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::CommitRevealWeightsEnabled::::get(netuid); + + let value_u256 = if value { U256::from(1) } else { U256::from(0) }; + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_commit_reveal_weights_enabled( + handle: &mut impl PrecompileHandle, + data: &[u8], + ) -> PrecompileResult { + let (netuid, enabled) = Self::parse_netuid_bool_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_commit_reveal_weights_enabled { + netuid, + enabled, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_liquid_alpha_enabled(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::LiquidAlphaOn::::get(netuid); + + let value_u256 = if value { U256::from(1) } else { U256::from(0) }; + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_liquid_alpha_enabled( + handle: &mut impl PrecompileHandle, + data: &[u8], + ) -> PrecompileResult { + let (netuid, enabled) = Self::parse_netuid_bool_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_liquid_alpha_enabled { netuid, enabled }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_alpha_values(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let (alpha_low, alpha_high) = pallet_subtensor::AlphaValues::::get(netuid); + + let mut value_u256 = U256::from(alpha_low); + let mut result = [0_u8; 64]; + U256::to_big_endian(&value_u256, &mut result[0..]); + + value_u256 = U256::from(alpha_high); + U256::to_big_endian(&value_u256, &mut result[32..]); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_alpha_values(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { + let (netuid, alpha_low, alpha_high) = Self::parse_netuid_u16_u16_parameter(data)?; + let call = + RuntimeCall::AdminUtils(pallet_admin_utils::Call::::sudo_set_alpha_values { + netuid, + alpha_low, + alpha_high, + }); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn get_commit_reveal_weights_interval(data: &[u8]) -> PrecompileResult { + let netuid = Self::parse_netuid(data)?; + + let value = pallet_subtensor::RevealPeriodEpochs::::get(netuid); + + let value_u256 = U256::from(value); + let mut result = [0_u8; 32]; + U256::to_big_endian(&value_u256, &mut result); + + Ok(PrecompileOutput { + exit_status: ExitSucceed::Returned, + output: result.into(), + }) + } + + fn set_commit_reveal_weights_interval( + handle: &mut impl PrecompileHandle, + data: &[u8], + ) -> PrecompileResult { + let (netuid, interval) = Self::parse_netuid_u64_parameter(data)?; + let call = RuntimeCall::AdminUtils( + pallet_admin_utils::Call::::sudo_set_commit_reveal_weights_interval { + netuid, + interval, + }, + ); + + dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) + } + + fn parse_register_network_parameters( + data: &[u8], + ) -> Result<(vec::Vec, vec::Vec, vec::Vec), PrecompileFailure> { + let mut buf = [0_u8; 4]; + + // get all start point for three data items: name, repo and contact + buf.copy_from_slice(get_slice(data, 28, 32)?); + let subnet_name_start: usize = u32::from_be_bytes(buf) as usize; + + buf.copy_from_slice(get_slice(data, 60, 64)?); + let github_repo_start: usize = u32::from_be_bytes(buf) as usize; + + buf.copy_from_slice(get_slice(data, 92, 96)?); + let subnet_contact_start: usize = u32::from_be_bytes(buf) as usize; + + // get name + buf.copy_from_slice(get_slice( + data, + subnet_name_start + 28, + subnet_name_start + 32, + )?); + let subnet_name_len: usize = u32::from_be_bytes(buf) as usize; + + let mut name_vec = vec![0; subnet_name_len]; + name_vec.copy_from_slice(get_slice( + data, + subnet_name_start + 32, + subnet_name_start + subnet_name_len + 32, + )?); + + // get repo data + buf.copy_from_slice(get_slice( + data, + github_repo_start + 28, + github_repo_start + 32, + )?); + let github_repo_len: usize = u32::from_be_bytes(buf) as usize; + + let mut repo_vec = vec![0; github_repo_len]; + repo_vec.copy_from_slice(get_slice( + data, + github_repo_start + 32, + github_repo_start + github_repo_len + 32, + )?); + + // get contact data + buf.copy_from_slice(get_slice( + data, + subnet_contact_start + 28, + subnet_contact_start + 32, + )?); + let subnet_contact_len: usize = u32::from_be_bytes(buf) as usize; + + let mut contact_vec = vec![0; subnet_contact_len]; + contact_vec.copy_from_slice(get_slice( + data, + subnet_contact_start + 32, + subnet_contact_start + subnet_contact_len + 32, + )?); + + Ok((name_vec, repo_vec, contact_vec)) + } + + fn parse_netuid(data: &[u8]) -> Result { + if data.len() < 32 { + return Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + }); + } + let mut netuid_vec = [0u8; 2]; + netuid_vec.copy_from_slice(get_slice(data, 30, 32)?); + let netuid = u16::from_be_bytes(netuid_vec); + + Ok(netuid) + } + + fn parse_netuid_u64_parameter(data: &[u8]) -> Result<(u16, u64), PrecompileFailure> { + if data.len() < 64 { + return Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + }); + } + let mut netuid_vec = [0u8; 2]; + netuid_vec.copy_from_slice(get_slice(data, 30, 32)?); + let netuid = u16::from_be_bytes(netuid_vec); + + let mut parameter_vec = [0u8; 8]; + parameter_vec.copy_from_slice(get_slice(data, 56, 64)?); + let parameter = u64::from_be_bytes(parameter_vec); + + Ok((netuid, parameter)) + } + + fn parse_netuid_u16_parameter(data: &[u8]) -> Result<(u16, u16), PrecompileFailure> { + if data.len() < 64 { + return Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + }); + } + let mut netuid_vec = [0u8; 2]; + netuid_vec.copy_from_slice(get_slice(data, 30, 32)?); + let netuid = u16::from_be_bytes(netuid_vec); + + let mut parameter_vec = [0u8; 2]; + parameter_vec.copy_from_slice(get_slice(data, 62, 64)?); + let parameter = u16::from_be_bytes(parameter_vec); + + Ok((netuid, parameter)) + } + + fn parse_netuid_u16_u16_parameter(data: &[u8]) -> Result<(u16, u16, u16), PrecompileFailure> { + if data.len() < 64 { + return Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + }); + } + let mut netuid_vec = [0u8; 2]; + netuid_vec.copy_from_slice(get_slice(data, 30, 32)?); + let netuid = u16::from_be_bytes(netuid_vec); + + let mut parameter_1_vec = [0u8; 2]; + parameter_1_vec.copy_from_slice(get_slice(data, 62, 64)?); + let parameter_1 = u16::from_be_bytes(parameter_1_vec); + + let mut parameter_2_vec = [0u8; 2]; + parameter_2_vec.copy_from_slice(get_slice(data, 94, 96)?); + let parameter_2 = u16::from_be_bytes(parameter_2_vec); + + Ok((netuid, parameter_1, parameter_2)) + } + + fn parse_netuid_bool_parameter(data: &[u8]) -> Result<(u16, bool), PrecompileFailure> { + if data.len() < 64 { + return Err(PrecompileFailure::Error { + exit_status: ExitError::InvalidRange, + }); + } + let mut netuid_vec = [0u8; 2]; + netuid_vec.copy_from_slice(get_slice(data, 30, 32)?); + let netuid = u16::from_be_bytes(netuid_vec); + + let mut parameter_vec = [0_u8]; + parameter_vec.copy_from_slice(get_slice(data, 63, 64)?); + + let parameter = parameter_vec[0] != 0; + + Ok((netuid, parameter)) + } +} From ed8ebee52001be51dbf81b8ae8a6410c9707a390 Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Thu, 19 Dec 2024 18:15:23 -0500 Subject: [PATCH 2/5] Apply childkeys even if sn registration is disabled --- .../subtensor/src/coinbase/run_coinbase.rs | 4 +- pallets/subtensor/src/tests/children.rs | 41 ++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/pallets/subtensor/src/coinbase/run_coinbase.rs b/pallets/subtensor/src/coinbase/run_coinbase.rs index f2b1cea99..1b69c1972 100644 --- a/pallets/subtensor/src/coinbase/run_coinbase.rs +++ b/pallets/subtensor/src/coinbase/run_coinbase.rs @@ -65,7 +65,7 @@ impl Pallet { // --- 3. Drain the subnet block emission and accumulate it as subnet emission, which increases until the tempo is reached in #4. // subnet_blockwise_emission -> subnet_pending_emission for netuid in subnets.clone().iter() { - if *netuid == 0 || !Self::is_registration_allowed(*netuid) { + if *netuid == 0 || !Self::get_network_registration_allowed(*netuid) { continue; } // --- 3.1 Get the network's block-wise emission amount. @@ -121,7 +121,7 @@ impl Pallet { Self::set_blocks_since_last_step(*netuid, 0); Self::set_last_mechanism_step_block(*netuid, current_block); - if *netuid == 0 || !Self::is_registration_allowed(*netuid) { + if *netuid == 0 || !Self::get_network_registration_allowed(*netuid) { // Skip netuid 0 payouts continue; } diff --git a/pallets/subtensor/src/tests/children.rs b/pallets/subtensor/src/tests/children.rs index 4c833b00e..45bbbeb80 100644 --- a/pallets/subtensor/src/tests/children.rs +++ b/pallets/subtensor/src/tests/children.rs @@ -3805,7 +3805,7 @@ fn test_revoke_child_no_min_stake_check() { add_network(netuid, 13, 0); register_ok_neuron(netuid, parent, coldkey, 0); - // Set below minimum stake for setting children + // Set minimum stake for setting children let parent_total_stake_original = TotalHotkeyStake::::get(parent); StakeThreshold::::put(1_000_000_000_000); TotalHotkeyStake::::insert(parent, StakeThreshold::::get()); @@ -3856,3 +3856,42 @@ fn test_revoke_child_no_min_stake_check() { assert_eq!(children_after, vec![]); }); } + +// Test that setting childkeys works even if subnet registration is disabled +#[test] +fn test_do_set_child_registration_disabled() { + new_test_ext(1).execute_with(|| { + let coldkey = U256::from(1); + let parent = U256::from(2); + let child = U256::from(3); + let netuid: u16 = 1; + let proportion: u64 = 1000; + + // Add network and register hotkey + add_network(netuid, 13, 0); + register_ok_neuron(netuid, parent, coldkey, 0); + + // Set minimum stake for setting children + let parent_total_stake_original = TotalHotkeyStake::::get(parent); + StakeThreshold::::put(1_000_000_000_000); + TotalHotkeyStake::::insert(parent, StakeThreshold::::get()); + + // Disable subnet registrations + NetworkRegistrationAllowed::::insert(netuid, false); + + // Schedule parent-child relationship + assert_ok!(SubtensorModule::do_schedule_children( + RuntimeOrigin::signed(coldkey), + parent, + netuid, + vec![(proportion, child)], + )); + + wait_and_set_pending_children(netuid); + TotalHotkeyStake::::insert(parent, parent_total_stake_original); + + // Ensure the childkeys are applied + let children_after = SubtensorModule::get_children(&parent, netuid); + assert_eq!(children_after, vec![(proportion, child)]); + }); +} \ No newline at end of file From 1ae3870f9af8e8a33f02585db4b603411d47e8d0 Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Thu, 19 Dec 2024 18:16:02 -0500 Subject: [PATCH 3/5] Fmt --- pallets/subtensor/src/tests/children.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/tests/children.rs b/pallets/subtensor/src/tests/children.rs index 45bbbeb80..ea336126d 100644 --- a/pallets/subtensor/src/tests/children.rs +++ b/pallets/subtensor/src/tests/children.rs @@ -3894,4 +3894,4 @@ fn test_do_set_child_registration_disabled() { let children_after = SubtensorModule::get_children(&parent, netuid); assert_eq!(children_after, vec![(proportion, child)]); }); -} \ No newline at end of file +} From 8df905a670d1389f821db67acf6aad6298394e43 Mon Sep 17 00:00:00 2001 From: gztensor <166415444+gztensor@users.noreply.github.com> Date: Thu, 19 Dec 2024 15:23:55 -0800 Subject: [PATCH 4/5] Delete runtime/src/precompiles/subnet.rs --- runtime/src/precompiles/subnet.rs | 987 ------------------------------ 1 file changed, 987 deletions(-) delete mode 100644 runtime/src/precompiles/subnet.rs diff --git a/runtime/src/precompiles/subnet.rs b/runtime/src/precompiles/subnet.rs deleted file mode 100644 index 04fe171fb..000000000 --- a/runtime/src/precompiles/subnet.rs +++ /dev/null @@ -1,987 +0,0 @@ -use crate::precompiles::{dispatch, get_method_id, get_slice}; -use crate::{Runtime, RuntimeCall}; -use pallet_evm::{ - ExitError, ExitSucceed, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, -}; -use sp_core::U256; -use sp_std::vec; - -pub const SUBNET_PRECOMPILE_INDEX: u64 = 2051; -// three bytes with max lenght 1K -pub const MAX_PARAMETER_SIZE: usize = 3 * 1024; - -// this is staking smart contract's(0x0000000000000000000000000000000000000803) sr25519 address -pub const SUBNET_CONTRACT_ADDRESS: &str = "5DPSUCb5mZFfizvBDSnRoAqmxV5Bmov2CS3xV773qU6VP1w2"; - -pub struct SubnetPrecompile; - -impl SubnetPrecompile { - pub fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult { - let txdata = handle.input(); - if txdata.len() > MAX_PARAMETER_SIZE { - log::error!("the length of subnet call is {} ", txdata.len()); - return Err(PrecompileFailure::Error { - exit_status: ExitError::InvalidRange, - }); - } - let method_id = get_slice(txdata, 0, 4)?; - let method_input = txdata - .get(4..) - .map_or_else(vec::Vec::new, |slice| slice.to_vec()); // Avoiding borrowing conflicts - - log::warn!("========== precompile execute: method id = {:?}", method_id); - - match method_id { - id if id == get_method_id("registerNetwork(bytes,bytes,bytes)") => { - Self::register_network(handle, &method_input) - } - id if id == get_method_id("registerNetwork()") => { - Self::register_network(handle, &[0_u8; 0]) - } - - id if id == get_method_id("getServingRateLimit(uint16)") => { - Self::get_serving_rate_limit(&method_input) - } - id if id == get_method_id("setServingRateLimit(uint256,uint256)") => { - Self::set_serving_rate_limit(handle, &method_input) - } - - id if id == get_method_id("getMinDifficulty(uint16)") => { - Self::get_min_difficulty(&method_input) - } - id if id == get_method_id("setMinDifficulty(uint16,uint64)") => { - Self::set_min_difficulty(handle, &method_input) - } - - id if id == get_method_id("getMaxDifficulty(uint16)") => { - Self::get_max_difficulty(&method_input) - } - id if id == get_method_id("setMaxDifficulty(uint16,uint64)") => { - Self::set_max_difficulty(handle, &method_input) - } - - id if id == get_method_id("getWeightsVersionKey(uint16)") => { - Self::get_weights_version_key(&method_input) - } - id if id == get_method_id("setWeightsVersionKey(uint16,uint64)") => { - Self::set_weights_version_key(handle, &method_input) - } - - id if id == get_method_id("getWeightsSetRateLimit(uint16)") => { - Self::get_weights_set_rate_limit(&method_input) - } - id if id == get_method_id("setWeightsSetRateLimit(uint16,uint64)") => { - Self::set_weights_set_rate_limit(handle, &method_input) - } - - id if id == get_method_id("getAdjustmentAlpha(uint16)") => { - Self::get_adjustment_alpha(&method_input) - } - id if id == get_method_id("setAdjustmentAlpha(uint16,uint64)") => { - Self::set_adjustment_alpha(handle, &method_input) - } - - id if id == get_method_id("getMaxWeightLimit(uint16)") => { - Self::get_max_weight_limit(&method_input) - } - id if id == get_method_id("setMaxWeightLimit(uint16,uint64)") => { - Self::set_max_weight_limit(handle, &method_input) - } - - id if id == get_method_id("getImmunityPeriod(uint16)") => { - Self::get_immunity_period(&method_input) - } - id if id == get_method_id("setImmunityPeriod(uint16,uint64)") => { - Self::set_immunity_period(handle, &method_input) - } - - id if id == get_method_id("getMinAllowedWeights(uint16)") => { - Self::get_min_allowed_weights(&method_input) - } - id if id == get_method_id("setMinAllowedWeights(uint16,uint16)") => { - Self::set_min_allowed_weights(handle, &method_input) - } - - id if id == get_method_id("getKappa(uint16)") => Self::get_kappa(&method_input), - id if id == get_method_id("setKappa(uint16,uint16)") => { - Self::set_kappa(handle, &method_input) - } - - id if id == get_method_id("getRho(uint16)") => Self::get_rho(&method_input), - id if id == get_method_id("setRho(uint16,uint16)") => { - Self::set_rho(handle, &method_input) - } - - id if id == get_method_id("getActivityCutoff(uint16)") => { - Self::get_activity_cutoff(&method_input) - } - id if id == get_method_id("setActivityCutoff(uint16,uint16)") => { - Self::set_activity_cutoff(handle, &method_input) - } - - id if id == get_method_id("getNetworkRegistrationAllowed(uint16)") => { - Self::get_network_registration_allowed(&method_input) - } - id if id == get_method_id("setNetworkRegistrationAllowed(uint16,bool)") => { - Self::set_network_registration_allowed(handle, &method_input) - } - - id if id == get_method_id("getNetworkPowRegistrationAllowed(uint16)") => { - Self::get_network_pow_registration_allowed(&method_input) - } - id if id == get_method_id("setNetworkPowRegistrationAllowed(uint16,bool)") => { - Self::set_network_pow_registration_allowed(handle, &method_input) - } - - id if id == get_method_id("getMinBurn(uint16)") => Self::get_min_burn(&method_input), - id if id == get_method_id("setMinBurn(uint16,uint64)") => { - Self::set_min_burn(handle, &method_input) - } - - id if id == get_method_id("getMaxBurn(uint16)") => Self::get_max_burn(&method_input), - id if id == get_method_id("setMaxBurn(uint16,uint64)") => { - Self::set_max_burn(handle, &method_input) - } - - id if id == get_method_id("getDifficulty(uint16)") => { - Self::get_difficulty(&method_input) - } - id if id == get_method_id("setDifficulty(uint16,uint64)") => { - Self::set_difficulty(handle, &method_input) - } - - id if id == get_method_id("getBondsMovingAverage(uint16)") => { - Self::get_bonds_moving_average(&method_input) - } - id if id == get_method_id("setBondsMovingAverage(uint16,uint64)") => { - Self::set_bonds_moving_average(handle, &method_input) - } - - id if id == get_method_id("getCommitRevealWeightsEnabled(uint16)") => { - Self::get_commit_reveal_weights_enabled(&method_input) - } - id if id == get_method_id("setCommitRevealWeightsEnabled(uint16,bool)") => { - Self::set_commit_reveal_weights_enabled(handle, &method_input) - } - - id if id == get_method_id("getLiquidAlphaEnabled(uint16)") => { - Self::get_liquid_alpha_enabled(&method_input) - } - id if id == get_method_id("setLiquidAlphaEnabled(uint16,bool)") => { - Self::set_liquid_alpha_enabled(handle, &method_input) - } - - id if id == get_method_id("getAlphaValues(uint16)") => { - Self::get_alpha_values(&method_input) - } - id if id == get_method_id("setAlphaValues(uint16,uint16,uint16)") => { - Self::set_alpha_values(handle, &method_input) - } - - id if id == get_method_id("getCommitRevealWeightsInterval(uint16)") => { - Self::get_commit_reveal_weights_interval(&method_input) - } - id if id == get_method_id("setCommitRevealWeightsInterval(uint16,uint64)") => { - Self::set_commit_reveal_weights_interval(handle, &method_input) - } - _ => Err(PrecompileFailure::Error { - exit_status: ExitError::InvalidRange, - }), - } - } - - fn register_network(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let call = if data.is_empty() { - RuntimeCall::SubtensorModule( - pallet_subtensor::Call::::register_network_with_identity { - identity: None, - }, - ) - } else { - let (subnet_name, github_repo, subnet_contact) = - Self::parse_register_network_parameters(data)?; - - let identity: pallet_subtensor::SubnetIdentityOf = pallet_subtensor::SubnetIdentityOf { - subnet_name, - github_repo, - subnet_contact, - }; - - // Create the register_network callcle - RuntimeCall::SubtensorModule( - pallet_subtensor::Call::::register_network_with_identity { - identity: Some(identity), - }, - ) - }; - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_serving_rate_limit(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::ServingRateLimit::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_serving_rate_limit(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, serving_rate_limit) = Self::parse_netuid_u64_parameter(data)?; - - log::warn!("==========set_serving_rate_limit: netuid = {:?}, serving_rate_limit = {:?}", netuid, serving_rate_limit); - - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_serving_rate_limit { - netuid, - serving_rate_limit, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_min_difficulty(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::MinDifficulty::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_min_difficulty(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, min_difficulty) = Self::parse_netuid_u64_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_min_difficulty { - netuid, - min_difficulty, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_max_difficulty(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::MaxDifficulty::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_max_difficulty(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, max_difficulty) = Self::parse_netuid_u64_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_max_difficulty { - netuid, - max_difficulty, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_weights_version_key(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::WeightsVersionKey::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_weights_version_key( - handle: &mut impl PrecompileHandle, - data: &[u8], - ) -> PrecompileResult { - let (netuid, weights_version_key) = Self::parse_netuid_u64_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_weights_version_key { - netuid, - weights_version_key, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_weights_set_rate_limit(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::WeightsSetRateLimit::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_weights_set_rate_limit( - handle: &mut impl PrecompileHandle, - data: &[u8], - ) -> PrecompileResult { - let (netuid, weights_set_rate_limit) = Self::parse_netuid_u64_parameter(data)?; - - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_weights_set_rate_limit { - netuid, - weights_set_rate_limit, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_adjustment_alpha(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::AdjustmentAlpha::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_adjustment_alpha(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, adjustment_alpha) = Self::parse_netuid_u64_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_adjustment_alpha { - netuid, - adjustment_alpha, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_max_weight_limit(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::MaxWeightsLimit::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_max_weight_limit(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, max_weight_limit) = Self::parse_netuid_u16_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_max_weight_limit { - netuid, - max_weight_limit, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_immunity_period(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::ImmunityPeriod::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_immunity_period(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, immunity_period) = Self::parse_netuid_u16_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_immunity_period { - netuid, - immunity_period, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_min_allowed_weights(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::MinAllowedWeights::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_min_allowed_weights( - handle: &mut impl PrecompileHandle, - data: &[u8], - ) -> PrecompileResult { - let (netuid, min_allowed_weights) = Self::parse_netuid_u16_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_min_allowed_weights { - netuid, - min_allowed_weights, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_kappa(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::Kappa::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_kappa(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, kappa) = Self::parse_netuid_u16_parameter(data)?; - let call = RuntimeCall::AdminUtils(pallet_admin_utils::Call::::sudo_set_kappa { - netuid, - kappa, - }); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_rho(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::Rho::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_rho(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, rho) = Self::parse_netuid_u16_parameter(data)?; - let call = RuntimeCall::AdminUtils(pallet_admin_utils::Call::::sudo_set_rho { - netuid, - rho, - }); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_activity_cutoff(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::ActivityCutoff::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_activity_cutoff(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, activity_cutoff) = Self::parse_netuid_u16_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_activity_cutoff { - netuid, - activity_cutoff, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_network_registration_allowed(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::NetworkRegistrationAllowed::::get(netuid); - - let value_u256 = if value { U256::from(1) } else { U256::from(0) }; - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_network_registration_allowed( - handle: &mut impl PrecompileHandle, - data: &[u8], - ) -> PrecompileResult { - let (netuid, registration_allowed) = Self::parse_netuid_bool_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_network_registration_allowed { - netuid, - registration_allowed, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_network_pow_registration_allowed(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::NetworkPowRegistrationAllowed::::get(netuid); - - let value_u256 = if value { U256::from(1) } else { U256::from(0) }; - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_network_pow_registration_allowed( - handle: &mut impl PrecompileHandle, - data: &[u8], - ) -> PrecompileResult { - let (netuid, registration_allowed) = Self::parse_netuid_bool_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_network_pow_registration_allowed { - netuid, - registration_allowed, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_min_burn(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::MinBurn::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_min_burn(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, min_burn) = Self::parse_netuid_u64_parameter(data)?; - let call = - RuntimeCall::AdminUtils(pallet_admin_utils::Call::::sudo_set_min_burn { - netuid, - min_burn, - }); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_max_burn(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::MaxBurn::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_max_burn(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, max_burn) = Self::parse_netuid_u64_parameter(data)?; - let call = - RuntimeCall::AdminUtils(pallet_admin_utils::Call::::sudo_set_max_burn { - netuid, - max_burn, - }); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_difficulty(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::Difficulty::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_difficulty(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, difficulty) = Self::parse_netuid_u64_parameter(data)?; - let call = - RuntimeCall::AdminUtils(pallet_admin_utils::Call::::sudo_set_difficulty { - netuid, - difficulty, - }); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_bonds_moving_average(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::BondsMovingAverage::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_bonds_moving_average( - handle: &mut impl PrecompileHandle, - data: &[u8], - ) -> PrecompileResult { - let (netuid, bonds_moving_average) = Self::parse_netuid_u64_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_bonds_moving_average { - netuid, - bonds_moving_average, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_commit_reveal_weights_enabled(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::CommitRevealWeightsEnabled::::get(netuid); - - let value_u256 = if value { U256::from(1) } else { U256::from(0) }; - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_commit_reveal_weights_enabled( - handle: &mut impl PrecompileHandle, - data: &[u8], - ) -> PrecompileResult { - let (netuid, enabled) = Self::parse_netuid_bool_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_commit_reveal_weights_enabled { - netuid, - enabled, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_liquid_alpha_enabled(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::LiquidAlphaOn::::get(netuid); - - let value_u256 = if value { U256::from(1) } else { U256::from(0) }; - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_liquid_alpha_enabled( - handle: &mut impl PrecompileHandle, - data: &[u8], - ) -> PrecompileResult { - let (netuid, enabled) = Self::parse_netuid_bool_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_liquid_alpha_enabled { netuid, enabled }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_alpha_values(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let (alpha_low, alpha_high) = pallet_subtensor::AlphaValues::::get(netuid); - - let mut value_u256 = U256::from(alpha_low); - let mut result = [0_u8; 64]; - U256::to_big_endian(&value_u256, &mut result[0..]); - - value_u256 = U256::from(alpha_high); - U256::to_big_endian(&value_u256, &mut result[32..]); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_alpha_values(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { - let (netuid, alpha_low, alpha_high) = Self::parse_netuid_u16_u16_parameter(data)?; - let call = - RuntimeCall::AdminUtils(pallet_admin_utils::Call::::sudo_set_alpha_values { - netuid, - alpha_low, - alpha_high, - }); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn get_commit_reveal_weights_interval(data: &[u8]) -> PrecompileResult { - let netuid = Self::parse_netuid(data)?; - - let value = pallet_subtensor::RevealPeriodEpochs::::get(netuid); - - let value_u256 = U256::from(value); - let mut result = [0_u8; 32]; - U256::to_big_endian(&value_u256, &mut result); - - Ok(PrecompileOutput { - exit_status: ExitSucceed::Returned, - output: result.into(), - }) - } - - fn set_commit_reveal_weights_interval( - handle: &mut impl PrecompileHandle, - data: &[u8], - ) -> PrecompileResult { - let (netuid, interval) = Self::parse_netuid_u64_parameter(data)?; - let call = RuntimeCall::AdminUtils( - pallet_admin_utils::Call::::sudo_set_commit_reveal_weights_interval { - netuid, - interval, - }, - ); - - dispatch(handle, call, SUBNET_CONTRACT_ADDRESS) - } - - fn parse_register_network_parameters( - data: &[u8], - ) -> Result<(vec::Vec, vec::Vec, vec::Vec), PrecompileFailure> { - let mut buf = [0_u8; 4]; - - // get all start point for three data items: name, repo and contact - buf.copy_from_slice(get_slice(data, 28, 32)?); - let subnet_name_start: usize = u32::from_be_bytes(buf) as usize; - - buf.copy_from_slice(get_slice(data, 60, 64)?); - let github_repo_start: usize = u32::from_be_bytes(buf) as usize; - - buf.copy_from_slice(get_slice(data, 92, 96)?); - let subnet_contact_start: usize = u32::from_be_bytes(buf) as usize; - - // get name - buf.copy_from_slice(get_slice( - data, - subnet_name_start + 28, - subnet_name_start + 32, - )?); - let subnet_name_len: usize = u32::from_be_bytes(buf) as usize; - - let mut name_vec = vec![0; subnet_name_len]; - name_vec.copy_from_slice(get_slice( - data, - subnet_name_start + 32, - subnet_name_start + subnet_name_len + 32, - )?); - - // get repo data - buf.copy_from_slice(get_slice( - data, - github_repo_start + 28, - github_repo_start + 32, - )?); - let github_repo_len: usize = u32::from_be_bytes(buf) as usize; - - let mut repo_vec = vec![0; github_repo_len]; - repo_vec.copy_from_slice(get_slice( - data, - github_repo_start + 32, - github_repo_start + github_repo_len + 32, - )?); - - // get contact data - buf.copy_from_slice(get_slice( - data, - subnet_contact_start + 28, - subnet_contact_start + 32, - )?); - let subnet_contact_len: usize = u32::from_be_bytes(buf) as usize; - - let mut contact_vec = vec![0; subnet_contact_len]; - contact_vec.copy_from_slice(get_slice( - data, - subnet_contact_start + 32, - subnet_contact_start + subnet_contact_len + 32, - )?); - - Ok((name_vec, repo_vec, contact_vec)) - } - - fn parse_netuid(data: &[u8]) -> Result { - if data.len() < 32 { - return Err(PrecompileFailure::Error { - exit_status: ExitError::InvalidRange, - }); - } - let mut netuid_vec = [0u8; 2]; - netuid_vec.copy_from_slice(get_slice(data, 30, 32)?); - let netuid = u16::from_be_bytes(netuid_vec); - - Ok(netuid) - } - - fn parse_netuid_u64_parameter(data: &[u8]) -> Result<(u16, u64), PrecompileFailure> { - if data.len() < 64 { - return Err(PrecompileFailure::Error { - exit_status: ExitError::InvalidRange, - }); - } - let mut netuid_vec = [0u8; 2]; - netuid_vec.copy_from_slice(get_slice(data, 30, 32)?); - let netuid = u16::from_be_bytes(netuid_vec); - - let mut parameter_vec = [0u8; 8]; - parameter_vec.copy_from_slice(get_slice(data, 56, 64)?); - let parameter = u64::from_be_bytes(parameter_vec); - - Ok((netuid, parameter)) - } - - fn parse_netuid_u16_parameter(data: &[u8]) -> Result<(u16, u16), PrecompileFailure> { - if data.len() < 64 { - return Err(PrecompileFailure::Error { - exit_status: ExitError::InvalidRange, - }); - } - let mut netuid_vec = [0u8; 2]; - netuid_vec.copy_from_slice(get_slice(data, 30, 32)?); - let netuid = u16::from_be_bytes(netuid_vec); - - let mut parameter_vec = [0u8; 2]; - parameter_vec.copy_from_slice(get_slice(data, 62, 64)?); - let parameter = u16::from_be_bytes(parameter_vec); - - Ok((netuid, parameter)) - } - - fn parse_netuid_u16_u16_parameter(data: &[u8]) -> Result<(u16, u16, u16), PrecompileFailure> { - if data.len() < 64 { - return Err(PrecompileFailure::Error { - exit_status: ExitError::InvalidRange, - }); - } - let mut netuid_vec = [0u8; 2]; - netuid_vec.copy_from_slice(get_slice(data, 30, 32)?); - let netuid = u16::from_be_bytes(netuid_vec); - - let mut parameter_1_vec = [0u8; 2]; - parameter_1_vec.copy_from_slice(get_slice(data, 62, 64)?); - let parameter_1 = u16::from_be_bytes(parameter_1_vec); - - let mut parameter_2_vec = [0u8; 2]; - parameter_2_vec.copy_from_slice(get_slice(data, 94, 96)?); - let parameter_2 = u16::from_be_bytes(parameter_2_vec); - - Ok((netuid, parameter_1, parameter_2)) - } - - fn parse_netuid_bool_parameter(data: &[u8]) -> Result<(u16, bool), PrecompileFailure> { - if data.len() < 64 { - return Err(PrecompileFailure::Error { - exit_status: ExitError::InvalidRange, - }); - } - let mut netuid_vec = [0u8; 2]; - netuid_vec.copy_from_slice(get_slice(data, 30, 32)?); - let netuid = u16::from_be_bytes(netuid_vec); - - let mut parameter_vec = [0_u8]; - parameter_vec.copy_from_slice(get_slice(data, 63, 64)?); - - let parameter = parameter_vec[0] != 0; - - Ok((netuid, parameter)) - } -} From ff7d88b22828aade0a6c1da8ea3e18e671d6a581 Mon Sep 17 00:00:00 2001 From: gztensor <166415444+gztensor@users.noreply.github.com> Date: Thu, 19 Dec 2024 16:37:22 -0800 Subject: [PATCH 5/5] Update pallets/subtensor/src/tests/children.rs Co-authored-by: Cameron Fairchild --- pallets/subtensor/src/tests/children.rs | 43 ------------------------- 1 file changed, 43 deletions(-) diff --git a/pallets/subtensor/src/tests/children.rs b/pallets/subtensor/src/tests/children.rs index ea336126d..ea89e4aae 100644 --- a/pallets/subtensor/src/tests/children.rs +++ b/pallets/subtensor/src/tests/children.rs @@ -3748,49 +3748,6 @@ fn test_do_set_child_cooldown_period() { }); } -// Test that setting childkeys requires minimum stake -#[test] -fn test_do_set_child_min_stake_check() { - new_test_ext(1).execute_with(|| { - let coldkey = U256::from(1); - let parent = U256::from(2); - let child = U256::from(3); - let netuid: u16 = 1; - let proportion: u64 = 1000; - - // Add network and register hotkey - add_network(netuid, 13, 0); - register_ok_neuron(netuid, parent, coldkey, 0); - - // Set below minimum stake for setting children - let parent_total_stake_original = TotalHotkeyStake::::get(parent); - StakeThreshold::::put(1_000_000_000_000); - TotalHotkeyStake::::insert(parent, StakeThreshold::::get() - 1); - - // Schedule parent-child relationship - assert_err!( - SubtensorModule::do_schedule_children( - RuntimeOrigin::signed(coldkey), - parent, - netuid, - vec![(proportion, child)], - ), - Error::::NotEnoughStakeToSetChildkeys - ); - - // Ensure the childkeys are not yet applied - let children_before = SubtensorModule::get_children(&parent, netuid); - assert_eq!(children_before, vec![]); - - wait_and_set_pending_children(netuid); - TotalHotkeyStake::::insert(parent, parent_total_stake_original); - - // Ensure the childkeys are still not applied - let children_after = SubtensorModule::get_children(&parent, netuid); - assert_eq!(children_after, vec![]); - }); -} - // Test that revoking childkeys does not require minimum stake #[test] fn test_revoke_child_no_min_stake_check() {