diff --git a/precompiles/src/alpha.rs b/precompiles/src/alpha.rs index 9ea04497ac..9840c42575 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, Vec}; + +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::(9)?; + 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::(9)?; + 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,16 +212,31 @@ 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 { + // 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) + .map(|(netuid, _)| netuid) .collect::>(); - let mut sum_alpha_price: U64F64 = U64F64::from_num(0); - for (netuid, _) in netuids { - let price = as SwapHandler>::current_alpha_price( - netuid.into(), - ); + // NetworksAdded entry + current_alpha_price reads + 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(*netuid); if price < U64F64::from_num(1) { sum_alpha_price = sum_alpha_price.saturating_add(price); 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/extensions.rs b/precompiles/src/extensions.rs index 4a7c418c86..b98fcfb515 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,23 @@ 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, + { + self.record_cost(RuntimeHelper::::db_read_gas_cost().saturating_mul(reads))?; + Ok(()) + } + + fn record_db_writes(&mut self, writes: u64) -> EvmResult<()> + where + R: frame_system::Config + pallet_evm::Config, + { + self.record_cost(RuntimeHelper::::db_write_gas_cost().saturating_mul(writes))?; + + Ok(()) + } + fn try_convert_apparent_value(&self) -> EvmResult where R: pallet_evm::Config, 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/lib.rs b/precompiles/src/lib.rs index 39815a6946..cf54934d95 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; diff --git a/precompiles/src/metagraph.rs b/precompiles/src/metagraph.rs index 4cffb76a4f..ec8086a87e 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,12 @@ 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 +139,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 +149,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 +162,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 +173,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 f13619058c..554115ddf0 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; @@ -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); @@ -307,10 +309,9 @@ 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); let stake = pallet_subtensor::Pallet::::get_total_stake_for_hotkey(&hotkey); @@ -320,11 +321,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 +343,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 +353,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 +366,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 +381,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 +474,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( @@ -496,8 +505,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 +531,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 +555,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 +585,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 +619,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); @@ -780,9 +785,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 @@ -799,10 +806,9 @@ 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); // get total stake of hotkey @@ -820,11 +826,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..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, RuntimeHelper}, -}; +use precompile_utils::{EvmResult, prelude::BoundedString}; use sp_core::H256; use sp_runtime::traits::{AsSystemOriginSigner, Dispatchable}; use sp_std::vec; @@ -170,7 +167,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 +175,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 +202,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 +229,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 +256,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 +283,11 @@ 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,18 @@ 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 +465,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 +493,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 +523,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 +552,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 +570,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 +588,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 +613,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 +641,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 +670,11 @@ 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 +700,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 +752,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 +782,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..4cad7fcb89 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,11 @@ 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 +130,14 @@ 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)) } } 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,