From a00c53c2b8787750c3d9782e2943e61c43e6cfb1 Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 15 Jun 2026 11:56:46 +0800 Subject: [PATCH 01/12] record db cost for precompile view functions --- precompiles/src/alpha.rs | 75 ++++++++++++++++++++++++----------- precompiles/src/extensions.rs | 21 ++++++++++ precompiles/src/lib.rs | 27 ++++++------- precompiles/src/staking.rs | 24 +++++------ 4 files changed, 95 insertions(+), 52 deletions(-) diff --git a/precompiles/src/alpha.rs b/precompiles/src/alpha.rs index 9ea04497ac..d9b80886fc 100644 --- a/precompiles/src/alpha.rs +++ b/precompiles/src/alpha.rs @@ -1,16 +1,16 @@ use core::marker::PhantomData; +use crate::PrecompileExt; use fp_evm::{ExitError, PrecompileFailure}; use pallet_evm::{BalanceConverter, PrecompileHandle, SubstrateBalance}; use precompile_utils::EvmResult; +use sp_runtime::SaturatedConversion; + +use crate::PrecompileHandleExt; use sp_core::U256; -use sp_std::vec::Vec; use substrate_fixed::types::U64F64; use subtensor_runtime_common::{NetUid, Token}; use subtensor_swap_interface::{Order, SwapHandler}; - -use crate::PrecompileExt; - pub struct AlphaPrecompile(PhantomData); impl PrecompileExt for AlphaPrecompile @@ -34,7 +34,9 @@ where { #[precompile::public("getAlphaPrice(uint16)")] #[precompile::view] - fn get_alpha_price(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_alpha_price(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + // SubnetMechanism + SubnetAlphaIn + SubnetTAO + SwapBalancer reads + handle.record_db_reads::(4)?; let current_alpha_price = as SwapHandler>::current_alpha_price(netuid.into()); let price = current_alpha_price.saturating_mul(U64F64::from_num(1_000_000_000)); @@ -48,7 +50,9 @@ where #[precompile::public("getMovingAlphaPrice(uint16)")] #[precompile::view] - fn get_moving_alpha_price(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_moving_alpha_price(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + // SubnetMechanism + SubnetMovingPrice reads + handle.record_db_reads::(2)?; let moving_alpha_price: U64F64 = pallet_subtensor::Pallet::::get_moving_alpha_price(netuid.into()); let price = moving_alpha_price.saturating_mul(U64F64::from_num(1_000_000_000)); @@ -62,38 +66,45 @@ where #[precompile::public("getTaoInPool(uint16)")] #[precompile::view] - fn get_tao_in_pool(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_tao_in_pool(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::SubnetTAO::::get(NetUid::from(netuid)).to_u64()) } #[precompile::public("getAlphaInPool(uint16)")] #[precompile::view] - fn get_alpha_in_pool(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_alpha_in_pool(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::SubnetAlphaIn::::get(NetUid::from(netuid)).into()) } #[precompile::public("getAlphaOutPool(uint16)")] #[precompile::view] - fn get_alpha_out_pool(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_alpha_out_pool(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::SubnetAlphaOut::::get(NetUid::from(netuid)).into()) } #[precompile::public("getAlphaIssuance(uint16)")] #[precompile::view] - fn get_alpha_issuance(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_alpha_issuance(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + // SubnetAlphaIn + SubnetAlphaOut reads + handle.record_db_reads::(2)?; Ok(pallet_subtensor::Pallet::::get_alpha_issuance(netuid.into()).into()) } #[precompile::public("getTaoWeight()")] #[precompile::view] - fn get_tao_weight(_handle: &mut impl PrecompileHandle) -> EvmResult { + fn get_tao_weight(handle: &mut impl PrecompileHandle) -> EvmResult { + handle.record_db_reads::(1)?; let tao_weight = pallet_subtensor::TaoWeight::::get(); Ok(U256::from(tao_weight)) } #[precompile::public("getCKBurn()")] #[precompile::view] - fn get_ck_burn(_handle: &mut impl PrecompileHandle) -> EvmResult { + fn get_ck_burn(handle: &mut impl PrecompileHandle) -> EvmResult { + handle.record_db_reads::(1)?; let ck_burn = pallet_subtensor::CKBurn::::get(); Ok(U256::from(ck_burn)) } @@ -101,10 +112,13 @@ where #[precompile::public("simSwapTaoForAlpha(uint16,uint64)")] #[precompile::view] fn sim_swap_tao_for_alpha( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, netuid: u16, tao: u64, ) -> EvmResult { + // SubnetMechanism + swap simulation reads + handle.record_db_reads::(2)?; + let order = pallet_subtensor::GetAlphaForTao::::with_amount(tao); let swap_result = as SwapHandler>::sim_swap(netuid.into(), order) @@ -117,10 +131,13 @@ where #[precompile::public("simSwapAlphaForTao(uint16,uint64)")] #[precompile::view] fn sim_swap_alpha_for_tao( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, netuid: u16, alpha: u64, ) -> EvmResult { + // SubnetMechanism + swap simulation reads + handle.record_db_reads::(2)?; + let order = pallet_subtensor::GetTaoForAlpha::::with_amount(alpha); let swap_result = as SwapHandler>::sim_swap(netuid.into(), order) @@ -132,7 +149,8 @@ where #[precompile::public("getSubnetMechanism(uint16)")] #[precompile::view] - fn get_subnet_mechanism(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_subnet_mechanism(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::SubnetMechanism::::get(NetUid::from( netuid, ))) @@ -147,9 +165,10 @@ where #[precompile::public("getEMAPriceHalvingBlocks(uint16)")] #[precompile::view] fn get_ema_price_halving_blocks( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, netuid: u16, ) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::EMAPriceHalvingBlocks::::get( NetUid::from(netuid), )) @@ -157,7 +176,8 @@ where #[precompile::public("getSubnetVolume(uint16)")] #[precompile::view] - fn get_subnet_volume(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_subnet_volume(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(U256::from(pallet_subtensor::SubnetVolume::::get( NetUid::from(netuid), ))) @@ -165,7 +185,8 @@ where #[precompile::public("getTaoInEmission(uint16)")] #[precompile::view] - fn get_tao_in_emission(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_tao_in_emission(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(U256::from( pallet_subtensor::SubnetTaoInEmission::::get(NetUid::from(netuid)).to_u64(), )) @@ -173,7 +194,8 @@ where #[precompile::public("getAlphaInEmission(uint16)")] #[precompile::view] - fn get_alpha_in_emission(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_alpha_in_emission(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(U256::from( pallet_subtensor::SubnetAlphaInEmission::::get(NetUid::from(netuid)).to_u64(), )) @@ -181,7 +203,8 @@ where #[precompile::public("getAlphaOutEmission(uint16)")] #[precompile::view] - fn get_alpha_out_emission(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_alpha_out_emission(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(U256::from( pallet_subtensor::SubnetAlphaOutEmission::::get(NetUid::from(netuid)).to_u64(), )) @@ -189,15 +212,19 @@ where #[precompile::public("getSumAlphaPrice()")] #[precompile::view] - fn get_sum_alpha_price(_handle: &mut impl PrecompileHandle) -> EvmResult { + fn get_sum_alpha_price(handle: &mut impl PrecompileHandle) -> EvmResult { + let mut sum_alpha_price: U64F64 = U64F64::from_num(0); let netuids = pallet_subtensor::NetworksAdded::::iter() .filter(|(netuid, _)| *netuid != NetUid::ROOT) + .map(|(netuid, _)| netuid) .collect::>(); - let mut sum_alpha_price: U64F64 = U64F64::from_num(0); - for (netuid, _) in netuids { + // NetworksAdded entry + current_alpha_price reads + handle.record_db_reads::(netuids.len().saturated_into::().saturating_mul(5))?; + + for netuid in netuids.iter() { let price = as SwapHandler>::current_alpha_price( - netuid.into(), + netuid.clone(), ); if price < U64F64::from_num(1) { diff --git a/precompiles/src/extensions.rs b/precompiles/src/extensions.rs index 4a7c418c86..af00a0cccb 100644 --- a/precompiles/src/extensions.rs +++ b/precompiles/src/extensions.rs @@ -12,6 +12,7 @@ use pallet_evm::{ }; use pallet_subtensor::SubtensorTransactionExtension; use precompile_utils::EvmResult; +use precompile_utils::prelude::RuntimeHelper; use scale_info::TypeInfo; use sp_core::{H160, U256, blake2_256}; use sp_runtime::{ @@ -34,6 +35,26 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle { ::AddressMapping::into_account_id(self.context().caller) } + fn record_db_reads(&mut self, reads: u64) -> EvmResult<()> + where + R: frame_system::Config + pallet_evm::Config, + { + for _ in 0..reads { + self.record_cost(RuntimeHelper::::db_read_gas_cost())?; + } + Ok(()) + } + + fn record_db_writes(&mut self, writes: u64) -> EvmResult<()> + where + R: frame_system::Config + pallet_evm::Config, + { + for _ in 0..writes { + self.record_cost(RuntimeHelper::::db_write_gas_cost())?; + } + Ok(()) + } + fn try_convert_apparent_value(&self) -> EvmResult where R: pallet_evm::Config, diff --git a/precompiles/src/lib.rs b/precompiles/src/lib.rs index 39815a6946..0b24a95704 100644 --- a/precompiles/src/lib.rs +++ b/precompiles/src/lib.rs @@ -4,12 +4,22 @@ extern crate alloc; use core::marker::PhantomData; +use crate::extensions::*; +pub use address_mapping::AddressMappingPrecompile; +pub use alpha::AlphaPrecompile; +pub use balance_transfer::BalanceTransferPrecompile; +pub use crowdloan::CrowdloanPrecompile; +pub use ed25519::Ed25519Verify; +pub use extensions::PrecompileExt; use fp_evm::{ExitError, PrecompileFailure}; use frame_support::traits::IsSubType; use frame_support::{ dispatch::{DispatchInfo, GetDispatchInfo, PostDispatchInfo}, pallet_prelude::Decode, }; +pub use leasing::LeasingPrecompile; +pub use metagraph::MetagraphPrecompile; +pub use neuron::NeuronPrecompile; use pallet_admin_utils::PrecompileEnum; use pallet_evm::{ AddressMapping, IsPrecompileResult, Precompile, PrecompileHandle, PrecompileResult, @@ -21,26 +31,14 @@ use pallet_evm_precompile_modexp::Modexp; use pallet_evm_precompile_sha3fips::Sha3FIPS256; use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256}; use pallet_subtensor_proxy as pallet_proxy; +pub use proxy::ProxyPrecompile; use sp_core::{H160, U256, crypto::ByteArray}; use sp_runtime::traits::{AsSystemOriginSigner, Dispatchable, StaticLookup}; -use subtensor_runtime_common::ProxyType; - -use crate::extensions::*; - -pub use address_mapping::AddressMappingPrecompile; -pub use alpha::AlphaPrecompile; -pub use balance_transfer::BalanceTransferPrecompile; -pub use crowdloan::CrowdloanPrecompile; -pub use ed25519::Ed25519Verify; -pub use extensions::PrecompileExt; -pub use leasing::LeasingPrecompile; -pub use metagraph::MetagraphPrecompile; -pub use neuron::NeuronPrecompile; -pub use proxy::ProxyPrecompile; pub use sr25519::Sr25519Verify; pub use staking::{StakingPrecompile, StakingPrecompileV2}; pub use storage_query::StorageQueryPrecompile; pub use subnet::SubnetPrecompile; +use subtensor_runtime_common::ProxyType; pub use uid_lookup::UidLookupPrecompile; pub use voting_power::VotingPowerPrecompile; @@ -170,6 +168,7 @@ where hash(AddressMappingPrecompile::::INDEX), ] } + } impl PrecompileSet for Precompiles where diff --git a/precompiles/src/staking.rs b/precompiles/src/staking.rs index f13619058c..6e39c0b850 100644 --- a/precompiles/src/staking.rs +++ b/precompiles/src/staking.rs @@ -43,7 +43,7 @@ use pallet_evm::{ }; use pallet_subtensor_proxy as pallet_proxy; use precompile_utils::EvmResult; -use precompile_utils::prelude::{Address, RuntimeHelper, revert}; +use precompile_utils::prelude::{Address, revert}; use sp_core::{H160, H256, U256}; use sp_runtime::traits::{AsSystemOriginSigner, Dispatchable, StaticLookup, UniqueSaturatedInto}; use sp_std::vec; @@ -496,8 +496,8 @@ where amount_alpha: U256, ) -> EvmResult<()> { // AllowancesStorage write + RegisteredSubnetCounter read - handle.record_cost(RuntimeHelper::::db_read_gas_cost())?; - handle.record_cost(RuntimeHelper::::db_write_gas_cost())?; + handle.record_db_reads::(1)?; + handle.record_db_writes::(1)?; let approver = handle.context().caller; let spender = spender_address.0; @@ -522,8 +522,7 @@ where origin_netuid: U256, ) -> EvmResult { // AllowancesStorage read + RegisteredSubnetCounter read - handle.record_cost(RuntimeHelper::::db_read_gas_cost())?; - handle.record_cost(RuntimeHelper::::db_read_gas_cost())?; + handle.record_db_reads::(2)?; let spender = spender_address.0; let netuid = try_u16_from_u256(origin_netuid)?; @@ -547,9 +546,8 @@ where } // AllowancesStorage read + write + RegisteredSubnetCounter read - handle.record_cost(RuntimeHelper::::db_read_gas_cost())?; - handle.record_cost(RuntimeHelper::::db_read_gas_cost())?; - handle.record_cost(RuntimeHelper::::db_write_gas_cost())?; + handle.record_db_reads::(2)?; + handle.record_db_writes::(1)?; let approver = handle.context().caller; let spender = spender_address.0; @@ -578,9 +576,8 @@ where } // AllowancesStorage read + write + RegisteredSubnetCounter read - handle.record_cost(RuntimeHelper::::db_read_gas_cost())?; - handle.record_cost(RuntimeHelper::::db_read_gas_cost())?; - handle.record_cost(RuntimeHelper::::db_write_gas_cost())?; + handle.record_db_reads::(2)?; + handle.record_db_writes::(1)?; let approver = handle.context().caller; let spender = spender_address.0; @@ -613,9 +610,8 @@ where } // AllowancesStorage read + write + RegisteredSubnetCounter read - handle.record_cost(RuntimeHelper::::db_read_gas_cost())?; - handle.record_cost(RuntimeHelper::::db_read_gas_cost())?; - handle.record_cost(RuntimeHelper::::db_write_gas_cost())?; + handle.record_db_reads::(2)?; + handle.record_db_writes::(1)?; let counter = Self::current_subnet_counter(netuid); let approval_key = (spender, netuid, counter); From b36e1be6e32d38254c40cd0d3a9e0c497427e346 Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 15 Jun 2026 12:48:54 +0800 Subject: [PATCH 02/12] fix ai comment --- precompiles/src/alpha.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/precompiles/src/alpha.rs b/precompiles/src/alpha.rs index d9b80886fc..25f472f5ee 100644 --- a/precompiles/src/alpha.rs +++ b/precompiles/src/alpha.rs @@ -213,6 +213,12 @@ where #[precompile::public("getSumAlphaPrice()")] #[precompile::view] fn get_sum_alpha_price(handle: &mut impl PrecompileHandle) -> EvmResult { + // NetworksAdded iteration + current_alpha_price reads + handle.record_db_reads::(1)?; + let subnet_limit = pallet_subtensor::SubnetLimit::::get().saturated_into::(); + + handle.record_db_reads::(subnet_limit)?; + let mut sum_alpha_price: U64F64 = U64F64::from_num(0); let netuids = pallet_subtensor::NetworksAdded::::iter() .filter(|(netuid, _)| *netuid != NetUid::ROOT) @@ -220,7 +226,13 @@ where .collect::>(); // NetworksAdded entry + current_alpha_price reads - handle.record_db_reads::(netuids.len().saturated_into::().saturating_mul(5))?; + handle.record_db_reads::( + netuids + .len() + .saturated_into::() + .saturating_mul(5) + .saturating_sub(subnet_limit), + )?; for netuid in netuids.iter() { let price = as SwapHandler>::current_alpha_price( From c4caf92b5a2e76d9baaf7efb59f6de4f88bc38fc Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 15 Jun 2026 12:54:37 +0800 Subject: [PATCH 03/12] add missed import --- precompiles/src/alpha.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/precompiles/src/alpha.rs b/precompiles/src/alpha.rs index 25f472f5ee..bcb5555d9d 100644 --- a/precompiles/src/alpha.rs +++ b/precompiles/src/alpha.rs @@ -4,7 +4,7 @@ use crate::PrecompileExt; use fp_evm::{ExitError, PrecompileFailure}; use pallet_evm::{BalanceConverter, PrecompileHandle, SubstrateBalance}; use precompile_utils::EvmResult; -use sp_runtime::SaturatedConversion; +use sp_runtime::{SaturatedConversion, Vec}; use crate::PrecompileHandleExt; use sp_core::U256; From dc2aaa1b66f716cb01cd641a2a84b65cc55d591f Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 15 Jun 2026 12:55:54 +0800 Subject: [PATCH 04/12] cargo clippy --- precompiles/src/alpha.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/precompiles/src/alpha.rs b/precompiles/src/alpha.rs index bcb5555d9d..1295fb4974 100644 --- a/precompiles/src/alpha.rs +++ b/precompiles/src/alpha.rs @@ -236,7 +236,7 @@ where for netuid in netuids.iter() { let price = as SwapHandler>::current_alpha_price( - netuid.clone(), + *netuid, ); if price < U64F64::from_num(1) { From b158bdcc15fcceefa838f1bde3129e000f7b2d24 Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 15 Jun 2026 12:56:38 +0800 Subject: [PATCH 05/12] cargo fmt --- precompiles/src/alpha.rs | 5 ++--- precompiles/src/lib.rs | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/precompiles/src/alpha.rs b/precompiles/src/alpha.rs index 1295fb4974..d172042b0f 100644 --- a/precompiles/src/alpha.rs +++ b/precompiles/src/alpha.rs @@ -235,9 +235,8 @@ where )?; for netuid in netuids.iter() { - let price = as SwapHandler>::current_alpha_price( - *netuid, - ); + let price = + as SwapHandler>::current_alpha_price(*netuid); if price < U64F64::from_num(1) { sum_alpha_price = sum_alpha_price.saturating_add(price); diff --git a/precompiles/src/lib.rs b/precompiles/src/lib.rs index 0b24a95704..cf54934d95 100644 --- a/precompiles/src/lib.rs +++ b/precompiles/src/lib.rs @@ -168,7 +168,6 @@ where hash(AddressMappingPrecompile::::INDEX), ] } - } impl PrecompileSet for Precompiles where From 8c5f395a7d4a395beebd44e487a3a6d876f5fdd2 Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 15 Jun 2026 13:17:29 +0800 Subject: [PATCH 06/12] add db cost for sim swap --- precompiles/src/alpha.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/precompiles/src/alpha.rs b/precompiles/src/alpha.rs index d172042b0f..e1162f091d 100644 --- a/precompiles/src/alpha.rs +++ b/precompiles/src/alpha.rs @@ -117,9 +117,10 @@ where tao: u64, ) -> EvmResult { // SubnetMechanism + swap simulation reads - handle.record_db_reads::(2)?; - + handle.record_db_reads::(1)?; let order = pallet_subtensor::GetAlphaForTao::::with_amount(tao); + + handle.record_db_reads::(8)?; let swap_result = as SwapHandler>::sim_swap(netuid.into(), order) .map_err(|e| PrecompileFailure::Error { From b42d8d26249017778e79248a685a04607e45d857 Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 15 Jun 2026 17:41:14 +0800 Subject: [PATCH 07/12] update reads --- precompiles/src/alpha.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/precompiles/src/alpha.rs b/precompiles/src/alpha.rs index e1162f091d..9840c42575 100644 --- a/precompiles/src/alpha.rs +++ b/precompiles/src/alpha.rs @@ -117,10 +117,9 @@ where tao: u64, ) -> EvmResult { // SubnetMechanism + swap simulation reads - handle.record_db_reads::(1)?; - let order = pallet_subtensor::GetAlphaForTao::::with_amount(tao); + handle.record_db_reads::(9)?; - handle.record_db_reads::(8)?; + let order = pallet_subtensor::GetAlphaForTao::::with_amount(tao); let swap_result = as SwapHandler>::sim_swap(netuid.into(), order) .map_err(|e| PrecompileFailure::Error { @@ -137,7 +136,7 @@ where alpha: u64, ) -> EvmResult { // SubnetMechanism + swap simulation reads - handle.record_db_reads::(2)?; + handle.record_db_reads::(9)?; let order = pallet_subtensor::GetTaoForAlpha::::with_amount(alpha); let swap_result = From cb0f223d479f24a83f2446e8ac432cbcadd3e7ac Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 15 Jun 2026 21:20:48 +0800 Subject: [PATCH 08/12] apply db reads to all views --- precompiles/src/crowdloan.rs | 6 ++- precompiles/src/leasing.rs | 9 ++-- precompiles/src/metagraph.rs | 47 +++++++++++++------- precompiles/src/proxy.rs | 3 +- precompiles/src/staking.rs | 38 ++++++++++++----- precompiles/src/subnet.rs | 76 ++++++++++++++++++++++----------- precompiles/src/uid_lookup.rs | 5 ++- precompiles/src/voting_power.rs | 27 +++++++----- 8 files changed, 142 insertions(+), 69 deletions(-) diff --git a/precompiles/src/crowdloan.rs b/precompiles/src/crowdloan.rs index c474ab9405..1c66d941ca 100644 --- a/precompiles/src/crowdloan.rs +++ b/precompiles/src/crowdloan.rs @@ -75,9 +75,10 @@ where #[precompile::public("getCrowdloan(uint32)")] #[precompile::view] fn get_crowdloan( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, crowdloan_id: u32, ) -> EvmResult { + handle.record_db_reads::(1)?; let crowdloan = pallet_crowdloan::Crowdloans::::get(crowdloan_id).ok_or( PrecompileFailure::Error { exit_status: ExitError::Other("Crowdloan not found".into()), @@ -105,10 +106,11 @@ where #[precompile::public("getContribution(uint32,bytes32)")] #[precompile::view] fn get_contribution( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, crowdloan_id: u32, coldkey: H256, ) -> EvmResult { + handle.record_db_reads::(1)?; let coldkey = R::AccountId::from(coldkey.0); let contribution = pallet_crowdloan::Contributions::::get(crowdloan_id, coldkey).ok_or( PrecompileFailure::Error { diff --git a/precompiles/src/leasing.rs b/precompiles/src/leasing.rs index 005782c776..5ebf03cb3c 100644 --- a/precompiles/src/leasing.rs +++ b/precompiles/src/leasing.rs @@ -73,7 +73,8 @@ where { #[precompile::public("getLease(uint32)")] #[precompile::view] - fn get_lease(_handle: &mut impl PrecompileHandle, lease_id: u32) -> EvmResult { + fn get_lease(handle: &mut impl PrecompileHandle, lease_id: u32) -> EvmResult { + handle.record_db_reads::(1)?; let lease = pallet_subtensor::SubnetLeases::::get(lease_id).ok_or(PrecompileFailure::Error { exit_status: ExitError::Other("Lease not found".into()), @@ -97,10 +98,11 @@ where #[precompile::public("getContributorShare(uint32,bytes32)")] #[precompile::view] fn get_contributor_share( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, lease_id: u32, contributor: H256, ) -> EvmResult<(u128, u128)> { + handle.record_db_reads::(1)?; let contributor = R::AccountId::from(contributor.0); let share = pallet_subtensor::SubnetLeaseShares::::get(lease_id, contributor); @@ -109,7 +111,8 @@ where #[precompile::public("getLeaseIdForSubnet(uint16)")] #[precompile::view] - fn get_lease_id_for_subnet(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_lease_id_for_subnet(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; let lease_id = pallet_subtensor::SubnetUidToLeaseId::::get(NetUid::from(netuid)).ok_or( PrecompileFailure::Error { exit_status: ExitError::Other("Lease not found for netuid".into()), diff --git a/precompiles/src/metagraph.rs b/precompiles/src/metagraph.rs index 4cffb76a4f..438a7730c7 100644 --- a/precompiles/src/metagraph.rs +++ b/precompiles/src/metagraph.rs @@ -8,12 +8,13 @@ use sp_core::{ByteArray, H256}; use subtensor_runtime_common::{NetUid, Token}; use crate::PrecompileExt; +use crate::PrecompileHandleExt; pub struct MetagraphPrecompile(PhantomData); impl PrecompileExt for MetagraphPrecompile where - R: frame_system::Config + pallet_subtensor::Config, + R: frame_system::Config + pallet_subtensor::Config + pallet_evm::Config, R::AccountId: From<[u8; 32]> + ByteArray, { const INDEX: u64 = 2050; @@ -22,12 +23,13 @@ where #[precompile_utils::precompile] impl MetagraphPrecompile where - R: frame_system::Config + pallet_subtensor::Config, + R: frame_system::Config + pallet_subtensor::Config + pallet_evm::Config, R::AccountId: ByteArray, { #[precompile::public("getUidCount(uint16)")] #[precompile::view] - fn get_uid_count(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_uid_count(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::SubnetworkN::::get(NetUid::from( netuid, ))) @@ -35,7 +37,9 @@ where #[precompile::public("getStake(uint16,uint16)")] #[precompile::view] - fn get_stake(_: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + fn get_stake(handle: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + // Keys + TotalHotkeyAlpha reads + handle.record_db_reads::(2)?; let hotkey = pallet_subtensor::Pallet::::get_hotkey_for_net_and_uid(netuid.into(), uid) .map_err(|_| PrecompileFailure::Error { exit_status: ExitError::InvalidRange, @@ -60,7 +64,8 @@ where #[precompile::public("getConsensus(uint16,uint16)")] #[precompile::view] - fn get_consensus(_: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + fn get_consensus(handle: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::Pallet::::get_consensus_for_uid( netuid.into(), uid, @@ -69,7 +74,8 @@ where #[precompile::public("getIncentive(uint16,uint16)")] #[precompile::view] - fn get_incentive(_: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + fn get_incentive(handle: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::Pallet::::get_incentive_for_uid( netuid.into(), uid, @@ -78,7 +84,8 @@ where #[precompile::public("getDividends(uint16,uint16)")] #[precompile::view] - fn get_dividends(_: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + fn get_dividends(handle: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::Pallet::::get_dividends_for_uid( netuid.into(), uid, @@ -87,13 +94,15 @@ where #[precompile::public("getEmission(uint16,uint16)")] #[precompile::view] - fn get_emission(_: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + fn get_emission(handle: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::Pallet::::get_emission_for_uid(netuid.into(), uid).into()) } #[precompile::public("getVtrust(uint16,uint16)")] #[precompile::view] - fn get_vtrust(_: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + fn get_vtrust(handle: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::Pallet::::get_validator_trust_for_uid( netuid.into(), uid, @@ -103,10 +112,11 @@ where #[precompile::public("getValidatorStatus(uint16,uint16)")] #[precompile::view] fn get_validator_status( - _: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, netuid: u16, uid: u16, ) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::Pallet::::get_validator_permit_for_uid( netuid.into(), uid, @@ -115,7 +125,8 @@ where #[precompile::public("getLastUpdate(uint16,uint16)")] #[precompile::view] - fn get_last_update(_: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + fn get_last_update(handle: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::Pallet::::get_last_update_for_uid( netuid.into(), uid, @@ -124,7 +135,8 @@ where #[precompile::public("getIsActive(uint16,uint16)")] #[precompile::view] - fn get_is_active(_: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + fn get_is_active(handle: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::Pallet::::get_active_for_uid( netuid.into(), uid, @@ -133,7 +145,9 @@ where #[precompile::public("getAxon(uint16,uint16)")] #[precompile::view] - fn get_axon(_: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + fn get_axon(handle: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + // Keys + Axons reads + handle.record_db_reads::(2)?; let hotkey = pallet_subtensor::Pallet::::get_hotkey_for_net_and_uid(netuid.into(), uid) .map_err(|_| PrecompileFailure::Error { exit_status: ExitError::Other("hotkey not found".into()), @@ -144,7 +158,8 @@ where #[precompile::public("getHotkey(uint16,uint16)")] #[precompile::view] - fn get_hotkey(_: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + fn get_hotkey(handle: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + handle.record_db_reads::(1)?; pallet_subtensor::Pallet::::get_hotkey_for_net_and_uid(netuid.into(), uid) .map(|acc| H256::from_slice(acc.as_slice())) .map_err(|_| PrecompileFailure::Error { @@ -154,7 +169,9 @@ where #[precompile::public("getColdkey(uint16,uint16)")] #[precompile::view] - fn get_coldkey(_: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + fn get_coldkey(handle: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + // Keys + Owner reads + handle.record_db_reads::(2)?; let hotkey = pallet_subtensor::Pallet::::get_hotkey_for_net_and_uid(netuid.into(), uid) .map_err(|_| PrecompileFailure::Error { exit_status: ExitError::InvalidRange, diff --git a/precompiles/src/proxy.rs b/precompiles/src/proxy.rs index 3312b67194..78d59f5ce2 100644 --- a/precompiles/src/proxy.rs +++ b/precompiles/src/proxy.rs @@ -268,9 +268,10 @@ where #[precompile::public("getProxies(bytes32)")] #[precompile::view] pub fn get_proxies( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, account_id: H256, ) -> EvmResult> { + handle.record_db_reads::(1)?; let account_id = R::AccountId::from(account_id.0.into()); let proxies = pallet_proxy::pallet::Pallet::::proxies(account_id); diff --git a/precompiles/src/staking.rs b/precompiles/src/staking.rs index 6e39c0b850..d570ffeb6e 100644 --- a/precompiles/src/staking.rs +++ b/precompiles/src/staking.rs @@ -296,9 +296,11 @@ where #[precompile::public("getTotalColdkeyStake(bytes32)")] #[precompile::view] fn get_total_coldkey_stake( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, coldkey: H256, ) -> EvmResult { + // StakingHotkeys + per-hotkey stake reads + handle.record_db_reads::(2)?; let coldkey = R::AccountId::from(coldkey.0); let stake = pallet_subtensor::Pallet::::get_total_stake_for_coldkey(&coldkey); @@ -308,9 +310,11 @@ where #[precompile::public("getTotalHotkeyStake(bytes32)")] #[precompile::view] fn get_total_hotkey_stake( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, hotkey: H256, ) -> EvmResult { + // Per-subnet stake + alpha price reads + handle.record_db_reads::(2)?; let hotkey = R::AccountId::from(hotkey.0); let stake = pallet_subtensor::Pallet::::get_total_stake_for_hotkey(&hotkey); @@ -320,11 +324,13 @@ where #[precompile::public("getStake(bytes32,bytes32,uint256)")] #[precompile::view] fn get_stake( - _: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, hotkey: H256, coldkey: H256, netuid: U256, ) -> EvmResult { + // Alpha share pool reads + handle.record_db_reads::(2)?; let hotkey = R::AccountId::from(hotkey.0); let coldkey = R::AccountId::from(coldkey.0); let netuid = try_u16_from_u256(netuid)?; @@ -340,7 +346,7 @@ where #[precompile::public("getAlphaStakedValidators(bytes32,uint256)")] #[precompile::view] fn get_alpha_staked_validators( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, hotkey: H256, netuid: U256, ) -> EvmResult> { @@ -350,6 +356,7 @@ where for (coldkey, netuid_in_alpha, _) in pallet_subtensor::Pallet::::alpha_iter_single_prefix(&hotkey) { + handle.record_db_reads::(1)?; if netuid == netuid_in_alpha { let key: [u8; 32] = coldkey.into(); coldkeys.push(key.into()); @@ -362,10 +369,11 @@ where #[precompile::public("getTotalAlphaStaked(bytes32,uint256)")] #[precompile::view] fn get_total_alpha_staked( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, hotkey: H256, netuid: U256, ) -> EvmResult { + handle.record_db_reads::(2)?; let hotkey = R::AccountId::from(hotkey.0); let netuid = try_u16_from_u256(netuid)?; let stake = @@ -376,7 +384,9 @@ where #[precompile::public("getNominatorMinRequiredStake()")] #[precompile::view] - fn get_nominator_min_required_stake(_handle: &mut impl PrecompileHandle) -> EvmResult { + fn get_nominator_min_required_stake(handle: &mut impl PrecompileHandle) -> EvmResult { + // NominatorMinRequiredStake + DefaultMinStake reads + handle.record_db_reads::(2)?; let stake = pallet_subtensor::Pallet::::get_nominator_min_required_stake(); Ok(stake.into()) @@ -467,10 +477,12 @@ where #[precompile::public("getTotalColdkeyStakeOnSubnet(bytes32,uint256)")] #[precompile::view] fn get_total_coldkey_stake_on_subnet( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, coldkey: H256, netuid: U256, ) -> EvmResult { + // StakingHotkeys + per-hotkey stake reads + handle.record_db_reads::(2)?; let coldkey = R::AccountId::from(coldkey.0); let netuid = try_u16_from_u256(netuid)?; let stake = pallet_subtensor::Pallet::::get_total_stake_for_coldkey_on_subnet( @@ -776,9 +788,11 @@ where #[precompile::public("getTotalColdkeyStake(bytes32)")] #[precompile::view] fn get_total_coldkey_stake( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, coldkey: H256, ) -> EvmResult { + // StakingHotkeys + per-hotkey stake reads + handle.record_db_reads::(2)?; let coldkey = R::AccountId::from(coldkey.0); // get total stake of coldkey @@ -796,9 +810,11 @@ where #[precompile::public("getTotalHotkeyStake(bytes32)")] #[precompile::view] fn get_total_hotkey_stake( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, hotkey: H256, ) -> EvmResult { + // Per-subnet stake + alpha price reads + handle.record_db_reads::(2)?; let hotkey = R::AccountId::from(hotkey.0); // get total stake of hotkey @@ -816,11 +832,13 @@ where #[precompile::public("getStake(bytes32,bytes32,uint256)")] #[precompile::view] fn get_stake( - _: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, hotkey: H256, coldkey: H256, netuid: U256, ) -> EvmResult { + // Alpha share pool reads + handle.record_db_reads::(2)?; let hotkey = R::AccountId::from(hotkey.0); let coldkey = R::AccountId::from(coldkey.0); let netuid = try_u16_from_u256(netuid)?; diff --git a/precompiles/src/subnet.rs b/precompiles/src/subnet.rs index da9ff4c79b..b6f3281258 100644 --- a/precompiles/src/subnet.rs +++ b/precompiles/src/subnet.rs @@ -7,7 +7,7 @@ use frame_system::RawOrigin; use pallet_evm::{AddressMapping, PrecompileHandle}; use precompile_utils::{ EvmResult, - prelude::{BoundedString, RuntimeHelper}, + prelude::BoundedString, }; use sp_core::H256; use sp_runtime::traits::{AsSystemOriginSigner, Dispatchable}; @@ -170,7 +170,7 @@ where handle: &mut impl PrecompileHandle, netuid: u16, ) -> EvmResult { - handle.record_cost(RuntimeHelper::::db_read_gas_cost())?; + handle.record_db_reads::(1)?; Ok(pallet_subtensor::NetworkRegisteredAt::::get( NetUid::from(netuid), )) @@ -178,7 +178,8 @@ where #[precompile::public("getServingRateLimit(uint16)")] #[precompile::view] - fn get_serving_rate_limit(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_serving_rate_limit(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::ServingRateLimit::::get(NetUid::from( netuid, ))) @@ -204,7 +205,8 @@ where #[precompile::public("getMinDifficulty(uint16)")] #[precompile::view] - fn get_min_difficulty(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_min_difficulty(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::MinDifficulty::::get(NetUid::from( netuid, ))) @@ -230,7 +232,8 @@ where #[precompile::public("getMaxDifficulty(uint16)")] #[precompile::view] - fn get_max_difficulty(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_max_difficulty(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::MaxDifficulty::::get(NetUid::from( netuid, ))) @@ -256,7 +259,8 @@ where #[precompile::public("getWeightsVersionKey(uint16)")] #[precompile::view] - fn get_weights_version_key(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_weights_version_key(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::WeightsVersionKey::::get(NetUid::from( netuid, ))) @@ -282,7 +286,8 @@ where #[precompile::public("getWeightsSetRateLimit(uint16)")] #[precompile::view] - fn get_weights_set_rate_limit(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_weights_set_rate_limit(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::WeightsSetRateLimit::::get( NetUid::from(netuid), )) @@ -301,7 +306,8 @@ where #[precompile::public("getAdjustmentAlpha(uint16)")] #[precompile::view] - fn get_adjustment_alpha(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_adjustment_alpha(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::AdjustmentAlpha::::get(NetUid::from( netuid, ))) @@ -335,7 +341,8 @@ where #[precompile::public("getImmunityPeriod(uint16)")] #[precompile::view] - fn get_immunity_period(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_immunity_period(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::ImmunityPeriod::::get(NetUid::from( netuid, ))) @@ -361,7 +368,8 @@ where #[precompile::public("getMinAllowedWeights(uint16)")] #[precompile::view] - fn get_min_allowed_weights(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_min_allowed_weights(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::MinAllowedWeights::::get(NetUid::from( netuid, ))) @@ -387,7 +395,8 @@ where #[precompile::public("getKappa(uint16)")] #[precompile::view] - fn get_kappa(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_kappa(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::Kappa::::get(NetUid::from(netuid))) } @@ -407,13 +416,15 @@ where #[precompile::public("getRho(uint16)")] #[precompile::view] - fn get_rho(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_rho(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::Rho::::get(NetUid::from(netuid))) } #[precompile::public("getAlphaSigmoidSteepness(uint16)")] #[precompile::view] - fn get_alpha_sigmoid_steepness(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_alpha_sigmoid_steepness(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::AlphaSigmoidSteepness::::get(NetUid::from(netuid)) as u16) } @@ -451,7 +462,8 @@ where #[precompile::public("getActivityCutoff(uint16)")] #[precompile::view] - fn get_activity_cutoff(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_activity_cutoff(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::ActivityCutoff::::get(NetUid::from( netuid, ))) @@ -478,9 +490,10 @@ where #[precompile::public("getNetworkRegistrationAllowed(uint16)")] #[precompile::view] fn get_network_registration_allowed( - _: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, netuid: u16, ) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::NetworkRegistrationAllowed::::get( NetUid::from(netuid), )) @@ -507,9 +520,10 @@ where #[precompile::public("getNetworkPowRegistrationAllowed(uint16)")] #[precompile::view] fn get_network_pow_registration_allowed( - _: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, netuid: u16, ) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::NetworkPowRegistrationAllowed::::get( NetUid::from(netuid), )) @@ -535,7 +549,8 @@ where #[precompile::public("getMinBurn(uint16)")] #[precompile::view] - fn get_min_burn(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_min_burn(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::MinBurn::::get(NetUid::from(netuid)).to_u64()) } @@ -552,7 +567,8 @@ where #[precompile::public("getMaxBurn(uint16)")] #[precompile::view] - fn get_max_burn(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_max_burn(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::MaxBurn::::get(NetUid::from(netuid)).to_u64()) } @@ -569,7 +585,8 @@ where #[precompile::public("getDifficulty(uint16)")] #[precompile::view] - fn get_difficulty(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_difficulty(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::Difficulty::::get(NetUid::from(netuid))) } @@ -593,7 +610,8 @@ where #[precompile::public("getBondsMovingAverage(uint16)")] #[precompile::view] - fn get_bonds_moving_average(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_bonds_moving_average(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::BondsMovingAverage::::get( NetUid::from(netuid), )) @@ -620,9 +638,10 @@ where #[precompile::public("getCommitRevealWeightsEnabled(uint16)")] #[precompile::view] fn get_commit_reveal_weights_enabled( - _: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, netuid: u16, ) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::CommitRevealWeightsEnabled::::get( NetUid::from(netuid), )) @@ -648,7 +667,8 @@ where #[precompile::public("getLiquidAlphaEnabled(uint16)")] #[precompile::view] - fn get_liquid_alpha_enabled(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_liquid_alpha_enabled(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::LiquidAlphaOn::::get(NetUid::from( netuid, ))) @@ -674,13 +694,15 @@ where #[precompile::public("getYuma3Enabled(uint16)")] #[precompile::view] - fn get_yuma3_enabled(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_yuma3_enabled(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::Yuma3On::::get(NetUid::from(netuid))) } #[precompile::public("getBondsResetEnabled(uint16)")] #[precompile::view] - fn get_bonds_reset_enabled(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_bonds_reset_enabled(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::BondsResetOn::::get(NetUid::from( netuid, ))) @@ -724,7 +746,8 @@ where #[precompile::public("getAlphaValues(uint16)")] #[precompile::view] - fn get_alpha_values(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<(u16, u16)> { + fn get_alpha_values(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<(u16, u16)> { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::AlphaValues::::get(NetUid::from( netuid, ))) @@ -753,9 +776,10 @@ where #[precompile::public("getCommitRevealWeightsInterval(uint16)")] #[precompile::view] fn get_commit_reveal_weights_interval( - _: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, netuid: u16, ) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::RevealPeriodEpochs::::get( NetUid::from(netuid), )) diff --git a/precompiles/src/uid_lookup.rs b/precompiles/src/uid_lookup.rs index 5d87973368..dc65501ba1 100644 --- a/precompiles/src/uid_lookup.rs +++ b/precompiles/src/uid_lookup.rs @@ -6,7 +6,7 @@ use precompile_utils::{EvmResult, prelude::Address}; use sp_runtime::traits::{Dispatchable, StaticLookup}; use sp_std::vec::Vec; -use crate::PrecompileExt; +use crate::{PrecompileExt, PrecompileHandleExt}; pub struct UidLookupPrecompile(PhantomData); @@ -39,11 +39,12 @@ where #[precompile::public("uidLookup(uint16,address,uint16)")] #[precompile::view] fn uid_lookup( - _handle: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, netuid: u16, evm_address: Address, limit: u16, ) -> EvmResult> { + handle.record_db_reads::(u64::from(limit))?; Ok(pallet_subtensor::Pallet::::uid_lookup( netuid.into(), evm_address.0, diff --git a/precompiles/src/voting_power.rs b/precompiles/src/voting_power.rs index af7896dac1..1db4b31bb1 100644 --- a/precompiles/src/voting_power.rs +++ b/precompiles/src/voting_power.rs @@ -6,6 +6,7 @@ use sp_core::{ByteArray, H256, U256}; use subtensor_runtime_common::NetUid; use crate::PrecompileExt; +use crate::PrecompileHandleExt; /// VotingPower precompile for smart contract access to validator voting power. /// @@ -15,7 +16,7 @@ pub struct VotingPowerPrecompile(PhantomData); impl PrecompileExt for VotingPowerPrecompile where - R: frame_system::Config + pallet_subtensor::Config, + R: frame_system::Config + pallet_subtensor::Config + pallet_evm::Config, R::AccountId: From<[u8; 32]> + ByteArray, { const INDEX: u64 = 2061; @@ -24,7 +25,7 @@ where #[precompile_utils::precompile] impl VotingPowerPrecompile where - R: frame_system::Config + pallet_subtensor::Config, + R: frame_system::Config + pallet_subtensor::Config + pallet_evm::Config, R::AccountId: From<[u8; 32]>, { /// Get voting power for a hotkey on a subnet. @@ -44,10 +45,11 @@ where #[precompile::public("getVotingPower(uint16,bytes32)")] #[precompile::view] fn get_voting_power( - _: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, netuid: u16, hotkey: H256, ) -> EvmResult { + handle.record_db_reads::(1)?; let hotkey = R::AccountId::from(hotkey.0); let voting_power = pallet_subtensor::VotingPower::::get(NetUid::from(netuid), &hotkey); Ok(U256::from(voting_power)) @@ -63,9 +65,10 @@ where #[precompile::public("isVotingPowerTrackingEnabled(uint16)")] #[precompile::view] fn is_voting_power_tracking_enabled( - _: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, netuid: u16, ) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::VotingPowerTrackingEnabled::::get( NetUid::from(netuid), )) @@ -84,9 +87,10 @@ where #[precompile::public("getVotingPowerDisableAtBlock(uint16)")] #[precompile::view] fn get_voting_power_disable_at_block( - _: &mut impl PrecompileHandle, + handle: &mut impl PrecompileHandle, netuid: u16, ) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::VotingPowerDisableAtBlock::::get( NetUid::from(netuid), )) @@ -104,7 +108,8 @@ where /// * `u64` - The alpha value (with 18 decimal precision) #[precompile::public("getVotingPowerEmaAlpha(uint16)")] #[precompile::view] - fn get_voting_power_ema_alpha(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_voting_power_ema_alpha(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + handle.record_db_reads::(1)?; Ok(pallet_subtensor::VotingPowerEmaAlpha::::get( NetUid::from(netuid), )) @@ -122,10 +127,12 @@ where /// * `u256` - The total voting power across all validators #[precompile::public("getTotalVotingPower(uint16)")] #[precompile::view] - fn get_total_voting_power(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { - let total: u64 = pallet_subtensor::VotingPower::::iter_prefix(NetUid::from(netuid)) - .map(|(_, voting_power)| voting_power) - .fold(0u64, |acc, vp| acc.saturating_add(vp)); + fn get_total_voting_power(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + let mut total: u64 = 0; + for (_, voting_power) in pallet_subtensor::VotingPower::::iter_prefix(NetUid::from(netuid)) { + handle.record_db_reads::(1)?; + total = total.saturating_add(voting_power); + } Ok(U256::from(total)) } } From 95ea327281ce78bf9e72144aa4e78a005fba52c9 Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 15 Jun 2026 21:21:49 +0800 Subject: [PATCH 09/12] cargo fmt --- precompiles/src/metagraph.rs | 6 +++++- precompiles/src/staking.rs | 10 ++-------- precompiles/src/subnet.rs | 20 +++++++++++++------- precompiles/src/voting_power.rs | 9 +++++++-- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/precompiles/src/metagraph.rs b/precompiles/src/metagraph.rs index 438a7730c7..ec8086a87e 100644 --- a/precompiles/src/metagraph.rs +++ b/precompiles/src/metagraph.rs @@ -125,7 +125,11 @@ where #[precompile::public("getLastUpdate(uint16,uint16)")] #[precompile::view] - fn get_last_update(handle: &mut impl PrecompileHandle, netuid: u16, uid: u16) -> EvmResult { + fn get_last_update( + handle: &mut impl PrecompileHandle, + netuid: u16, + uid: u16, + ) -> EvmResult { handle.record_db_reads::(1)?; Ok(pallet_subtensor::Pallet::::get_last_update_for_uid( netuid.into(), diff --git a/precompiles/src/staking.rs b/precompiles/src/staking.rs index d570ffeb6e..554115ddf0 100644 --- a/precompiles/src/staking.rs +++ b/precompiles/src/staking.rs @@ -309,10 +309,7 @@ where #[precompile::public("getTotalHotkeyStake(bytes32)")] #[precompile::view] - fn get_total_hotkey_stake( - handle: &mut impl PrecompileHandle, - hotkey: H256, - ) -> EvmResult { + fn get_total_hotkey_stake(handle: &mut impl PrecompileHandle, hotkey: H256) -> EvmResult { // Per-subnet stake + alpha price reads handle.record_db_reads::(2)?; let hotkey = R::AccountId::from(hotkey.0); @@ -809,10 +806,7 @@ where #[precompile::public("getTotalHotkeyStake(bytes32)")] #[precompile::view] - fn get_total_hotkey_stake( - handle: &mut impl PrecompileHandle, - hotkey: H256, - ) -> EvmResult { + fn get_total_hotkey_stake(handle: &mut impl PrecompileHandle, hotkey: H256) -> EvmResult { // Per-subnet stake + alpha price reads handle.record_db_reads::(2)?; let hotkey = R::AccountId::from(hotkey.0); diff --git a/precompiles/src/subnet.rs b/precompiles/src/subnet.rs index b6f3281258..e02dedcb9d 100644 --- a/precompiles/src/subnet.rs +++ b/precompiles/src/subnet.rs @@ -5,10 +5,7 @@ use frame_support::traits::ConstU32; use frame_support::traits::IsSubType; use frame_system::RawOrigin; use pallet_evm::{AddressMapping, PrecompileHandle}; -use precompile_utils::{ - EvmResult, - prelude::BoundedString, -}; +use precompile_utils::{EvmResult, prelude::BoundedString}; use sp_core::H256; use sp_runtime::traits::{AsSystemOriginSigner, Dispatchable}; use sp_std::vec; @@ -286,7 +283,10 @@ where #[precompile::public("getWeightsSetRateLimit(uint16)")] #[precompile::view] - fn get_weights_set_rate_limit(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_weights_set_rate_limit( + handle: &mut impl PrecompileHandle, + netuid: u16, + ) -> EvmResult { handle.record_db_reads::(1)?; Ok(pallet_subtensor::WeightsSetRateLimit::::get( NetUid::from(netuid), @@ -423,7 +423,10 @@ where #[precompile::public("getAlphaSigmoidSteepness(uint16)")] #[precompile::view] - fn get_alpha_sigmoid_steepness(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_alpha_sigmoid_steepness( + handle: &mut impl PrecompileHandle, + netuid: u16, + ) -> EvmResult { handle.record_db_reads::(1)?; Ok(pallet_subtensor::AlphaSigmoidSteepness::::get(NetUid::from(netuid)) as u16) } @@ -667,7 +670,10 @@ where #[precompile::public("getLiquidAlphaEnabled(uint16)")] #[precompile::view] - fn get_liquid_alpha_enabled(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_liquid_alpha_enabled( + handle: &mut impl PrecompileHandle, + netuid: u16, + ) -> EvmResult { handle.record_db_reads::(1)?; Ok(pallet_subtensor::LiquidAlphaOn::::get(NetUid::from( netuid, diff --git a/precompiles/src/voting_power.rs b/precompiles/src/voting_power.rs index 1db4b31bb1..4cad7fcb89 100644 --- a/precompiles/src/voting_power.rs +++ b/precompiles/src/voting_power.rs @@ -108,7 +108,10 @@ where /// * `u64` - The alpha value (with 18 decimal precision) #[precompile::public("getVotingPowerEmaAlpha(uint16)")] #[precompile::view] - fn get_voting_power_ema_alpha(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { + fn get_voting_power_ema_alpha( + handle: &mut impl PrecompileHandle, + netuid: u16, + ) -> EvmResult { handle.record_db_reads::(1)?; Ok(pallet_subtensor::VotingPowerEmaAlpha::::get( NetUid::from(netuid), @@ -129,7 +132,9 @@ where #[precompile::view] fn get_total_voting_power(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { let mut total: u64 = 0; - for (_, voting_power) in pallet_subtensor::VotingPower::::iter_prefix(NetUid::from(netuid)) { + for (_, voting_power) in + pallet_subtensor::VotingPower::::iter_prefix(NetUid::from(netuid)) + { handle.record_db_reads::(1)?; total = total.saturating_add(voting_power); } From ba7c2452fc6160a572492f2bc7a7eb2d4e775a97 Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 15 Jun 2026 23:37:57 +0800 Subject: [PATCH 10/12] remove the loop --- precompiles/src/extensions.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/precompiles/src/extensions.rs b/precompiles/src/extensions.rs index af00a0cccb..f7ce7a46b5 100644 --- a/precompiles/src/extensions.rs +++ b/precompiles/src/extensions.rs @@ -39,9 +39,7 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle { where R: frame_system::Config + pallet_evm::Config, { - for _ in 0..reads { - self.record_cost(RuntimeHelper::::db_read_gas_cost())?; - } + self.record_cost(RuntimeHelper::::db_read_gas_cost().saturating_mul(reads as u64))?; Ok(()) } @@ -49,9 +47,8 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle { where R: frame_system::Config + pallet_evm::Config, { - for _ in 0..writes { - self.record_cost(RuntimeHelper::::db_write_gas_cost())?; - } + self.record_cost(RuntimeHelper::::db_write_gas_cost().saturating_mul(writes as u64))?; + Ok(()) } From f92d0917ba5acc09c21e1f4396c74e83575519ee Mon Sep 17 00:00:00 2001 From: open-junius Date: Mon, 15 Jun 2026 23:38:59 +0800 Subject: [PATCH 11/12] cargo clippy --- precompiles/src/extensions.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/precompiles/src/extensions.rs b/precompiles/src/extensions.rs index f7ce7a46b5..b98fcfb515 100644 --- a/precompiles/src/extensions.rs +++ b/precompiles/src/extensions.rs @@ -39,7 +39,7 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle { where R: frame_system::Config + pallet_evm::Config, { - self.record_cost(RuntimeHelper::::db_read_gas_cost().saturating_mul(reads as u64))?; + self.record_cost(RuntimeHelper::::db_read_gas_cost().saturating_mul(reads))?; Ok(()) } @@ -47,7 +47,7 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle { where R: frame_system::Config + pallet_evm::Config, { - self.record_cost(RuntimeHelper::::db_write_gas_cost().saturating_mul(writes as u64))?; + self.record_cost(RuntimeHelper::::db_write_gas_cost().saturating_mul(writes))?; Ok(()) } From 35185dafd77e5cb378830dc28ae7de53d69f736d Mon Sep 17 00:00:00 2001 From: open-junius Date: Tue, 16 Jun 2026 10:38:16 +0800 Subject: [PATCH 12/12] bump version --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 15607d1e09..44a990d765 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -235,7 +235,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 418, + spec_version: 419, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1,