Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 65 additions & 26 deletions precompiles/src/alpha.rs
Original file line number Diff line number Diff line change
@@ -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<R>(PhantomData<R>);

impl<R> PrecompileExt<R::AccountId> for AlphaPrecompile<R>
Expand All @@ -34,7 +34,9 @@ where
{
#[precompile::public("getAlphaPrice(uint16)")]
#[precompile::view]
fn get_alpha_price(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<U256> {
fn get_alpha_price(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<U256> {
// SubnetMechanism + SubnetAlphaIn + SubnetTAO + SwapBalancer reads
handle.record_db_reads::<R>(4)?;
let current_alpha_price =
<pallet_subtensor_swap::Pallet<R> as SwapHandler>::current_alpha_price(netuid.into());
let price = current_alpha_price.saturating_mul(U64F64::from_num(1_000_000_000));
Expand All @@ -48,7 +50,9 @@ where

#[precompile::public("getMovingAlphaPrice(uint16)")]
#[precompile::view]
fn get_moving_alpha_price(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<U256> {
fn get_moving_alpha_price(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<U256> {
// SubnetMechanism + SubnetMovingPrice reads
handle.record_db_reads::<R>(2)?;
let moving_alpha_price: U64F64 =
pallet_subtensor::Pallet::<R>::get_moving_alpha_price(netuid.into());
let price = moving_alpha_price.saturating_mul(U64F64::from_num(1_000_000_000));
Expand All @@ -62,50 +66,61 @@ where

#[precompile::public("getTaoInPool(uint16)")]
#[precompile::view]
fn get_tao_in_pool(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<u64> {
fn get_tao_in_pool(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<u64> {
handle.record_db_reads::<R>(1)?;
Ok(pallet_subtensor::SubnetTAO::<R>::get(NetUid::from(netuid)).to_u64())
}

#[precompile::public("getAlphaInPool(uint16)")]
#[precompile::view]
fn get_alpha_in_pool(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<u64> {
fn get_alpha_in_pool(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<u64> {
handle.record_db_reads::<R>(1)?;
Ok(pallet_subtensor::SubnetAlphaIn::<R>::get(NetUid::from(netuid)).into())
}

#[precompile::public("getAlphaOutPool(uint16)")]
#[precompile::view]
fn get_alpha_out_pool(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<u64> {
fn get_alpha_out_pool(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<u64> {
handle.record_db_reads::<R>(1)?;
Ok(pallet_subtensor::SubnetAlphaOut::<R>::get(NetUid::from(netuid)).into())
}

#[precompile::public("getAlphaIssuance(uint16)")]
#[precompile::view]
fn get_alpha_issuance(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<u64> {
fn get_alpha_issuance(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<u64> {
// SubnetAlphaIn + SubnetAlphaOut reads
handle.record_db_reads::<R>(2)?;
Ok(pallet_subtensor::Pallet::<R>::get_alpha_issuance(netuid.into()).into())
}

#[precompile::public("getTaoWeight()")]
#[precompile::view]
fn get_tao_weight(_handle: &mut impl PrecompileHandle) -> EvmResult<U256> {
fn get_tao_weight(handle: &mut impl PrecompileHandle) -> EvmResult<U256> {
handle.record_db_reads::<R>(1)?;
let tao_weight = pallet_subtensor::TaoWeight::<R>::get();
Ok(U256::from(tao_weight))
}

#[precompile::public("getCKBurn()")]
#[precompile::view]
fn get_ck_burn(_handle: &mut impl PrecompileHandle) -> EvmResult<U256> {
fn get_ck_burn(handle: &mut impl PrecompileHandle) -> EvmResult<U256> {
handle.record_db_reads::<R>(1)?;
let ck_burn = pallet_subtensor::CKBurn::<R>::get();
Ok(U256::from(ck_burn))
}

#[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<U256> {
// SubnetMechanism + swap simulation reads
handle.record_db_reads::<R>(1)?;
let order = pallet_subtensor::GetAlphaForTao::<R>::with_amount(tao);

handle.record_db_reads::<R>(8)?;
let swap_result =
<pallet_subtensor_swap::Pallet<R> as SwapHandler>::sim_swap(netuid.into(), order)
.map_err(|e| PrecompileFailure::Error {
Expand All @@ -117,10 +132,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<U256> {
// SubnetMechanism + swap simulation reads
handle.record_db_reads::<R>(2)?;
Comment thread
open-junius marked this conversation as resolved.
Outdated

let order = pallet_subtensor::GetTaoForAlpha::<R>::with_amount(alpha);
let swap_result =
<pallet_subtensor_swap::Pallet<R> as SwapHandler>::sim_swap(netuid.into(), order)
Expand All @@ -132,7 +150,8 @@ where

#[precompile::public("getSubnetMechanism(uint16)")]
#[precompile::view]
fn get_subnet_mechanism(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<u16> {
fn get_subnet_mechanism(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<u16> {
handle.record_db_reads::<R>(1)?;
Ok(pallet_subtensor::SubnetMechanism::<R>::get(NetUid::from(
netuid,
)))
Expand All @@ -147,58 +166,78 @@ where
#[precompile::public("getEMAPriceHalvingBlocks(uint16)")]
#[precompile::view]
fn get_ema_price_halving_blocks(
_handle: &mut impl PrecompileHandle,
handle: &mut impl PrecompileHandle,
netuid: u16,
) -> EvmResult<u64> {
handle.record_db_reads::<R>(1)?;
Ok(pallet_subtensor::EMAPriceHalvingBlocks::<R>::get(
NetUid::from(netuid),
))
}

#[precompile::public("getSubnetVolume(uint16)")]
#[precompile::view]
fn get_subnet_volume(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<U256> {
fn get_subnet_volume(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<U256> {
handle.record_db_reads::<R>(1)?;
Ok(U256::from(pallet_subtensor::SubnetVolume::<R>::get(
NetUid::from(netuid),
)))
}

#[precompile::public("getTaoInEmission(uint16)")]
#[precompile::view]
fn get_tao_in_emission(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<U256> {
fn get_tao_in_emission(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<U256> {
handle.record_db_reads::<R>(1)?;
Ok(U256::from(
pallet_subtensor::SubnetTaoInEmission::<R>::get(NetUid::from(netuid)).to_u64(),
))
}

#[precompile::public("getAlphaInEmission(uint16)")]
#[precompile::view]
fn get_alpha_in_emission(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<U256> {
fn get_alpha_in_emission(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<U256> {
handle.record_db_reads::<R>(1)?;
Ok(U256::from(
pallet_subtensor::SubnetAlphaInEmission::<R>::get(NetUid::from(netuid)).to_u64(),
))
}

#[precompile::public("getAlphaOutEmission(uint16)")]
#[precompile::view]
fn get_alpha_out_emission(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<U256> {
fn get_alpha_out_emission(handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult<U256> {
handle.record_db_reads::<R>(1)?;
Ok(U256::from(
pallet_subtensor::SubnetAlphaOutEmission::<R>::get(NetUid::from(netuid)).to_u64(),
))
}

#[precompile::public("getSumAlphaPrice()")]
#[precompile::view]
fn get_sum_alpha_price(_handle: &mut impl PrecompileHandle) -> EvmResult<U256> {
fn get_sum_alpha_price(handle: &mut impl PrecompileHandle) -> EvmResult<U256> {
// NetworksAdded iteration + current_alpha_price reads
handle.record_db_reads::<R>(1)?;
let subnet_limit = pallet_subtensor::SubnetLimit::<R>::get().saturated_into::<u64>();

handle.record_db_reads::<R>(subnet_limit)?;

let mut sum_alpha_price: U64F64 = U64F64::from_num(0);
let netuids = pallet_subtensor::NetworksAdded::<R>::iter()
.filter(|(netuid, _)| *netuid != NetUid::ROOT)
.map(|(netuid, _)| netuid)
.collect::<Vec<_>>();

let mut sum_alpha_price: U64F64 = U64F64::from_num(0);
for (netuid, _) in netuids {
let price = <pallet_subtensor_swap::Pallet<R> as SwapHandler>::current_alpha_price(
netuid.into(),
);
// NetworksAdded entry + current_alpha_price reads
handle.record_db_reads::<R>(
netuids
.len()
.saturated_into::<u64>()
.saturating_mul(5)
.saturating_sub(subnet_limit),
)?;

for netuid in netuids.iter() {
let price =
<pallet_subtensor_swap::Pallet<R> as SwapHandler>::current_alpha_price(*netuid);

if price < U64F64::from_num(1) {
sum_alpha_price = sum_alpha_price.saturating_add(price);
Expand Down
21 changes: 21 additions & 0 deletions precompiles/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -34,6 +35,26 @@ pub(crate) trait PrecompileHandleExt: PrecompileHandle {
<R as pallet_evm::Config>::AddressMapping::into_account_id(self.context().caller)
}

fn record_db_reads<R>(&mut self, reads: u64) -> EvmResult<()>
where
R: frame_system::Config + pallet_evm::Config,
{
for _ in 0..reads {
self.record_cost(RuntimeHelper::<R>::db_read_gas_cost())?;
}
Ok(())
Comment thread
open-junius marked this conversation as resolved.
Outdated
}

fn record_db_writes<R>(&mut self, writes: u64) -> EvmResult<()>
where
R: frame_system::Config + pallet_evm::Config,
{
for _ in 0..writes {
self.record_cost(RuntimeHelper::<R>::db_write_gas_cost())?;
}
Comment thread
open-junius marked this conversation as resolved.
Outdated
Ok(())
}

fn try_convert_apparent_value<R>(&self) -> EvmResult<U256>
where
R: pallet_evm::Config,
Expand Down
26 changes: 12 additions & 14 deletions precompiles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down
24 changes: 10 additions & 14 deletions precompiles/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -496,8 +496,8 @@ where
amount_alpha: U256,
) -> EvmResult<()> {
// AllowancesStorage write + RegisteredSubnetCounter read
handle.record_cost(RuntimeHelper::<R>::db_read_gas_cost())?;
handle.record_cost(RuntimeHelper::<R>::db_write_gas_cost())?;
handle.record_db_reads::<R>(1)?;
handle.record_db_writes::<R>(1)?;

let approver = handle.context().caller;
let spender = spender_address.0;
Expand All @@ -522,8 +522,7 @@ where
origin_netuid: U256,
) -> EvmResult<U256> {
// AllowancesStorage read + RegisteredSubnetCounter read
handle.record_cost(RuntimeHelper::<R>::db_read_gas_cost())?;
handle.record_cost(RuntimeHelper::<R>::db_read_gas_cost())?;
handle.record_db_reads::<R>(2)?;

let spender = spender_address.0;
let netuid = try_u16_from_u256(origin_netuid)?;
Expand All @@ -547,9 +546,8 @@ where
}

// AllowancesStorage read + write + RegisteredSubnetCounter read
handle.record_cost(RuntimeHelper::<R>::db_read_gas_cost())?;
handle.record_cost(RuntimeHelper::<R>::db_read_gas_cost())?;
handle.record_cost(RuntimeHelper::<R>::db_write_gas_cost())?;
handle.record_db_reads::<R>(2)?;
handle.record_db_writes::<R>(1)?;

let approver = handle.context().caller;
let spender = spender_address.0;
Expand Down Expand Up @@ -578,9 +576,8 @@ where
}

// AllowancesStorage read + write + RegisteredSubnetCounter read
handle.record_cost(RuntimeHelper::<R>::db_read_gas_cost())?;
handle.record_cost(RuntimeHelper::<R>::db_read_gas_cost())?;
handle.record_cost(RuntimeHelper::<R>::db_write_gas_cost())?;
handle.record_db_reads::<R>(2)?;
handle.record_db_writes::<R>(1)?;

let approver = handle.context().caller;
let spender = spender_address.0;
Expand Down Expand Up @@ -613,9 +610,8 @@ where
}

// AllowancesStorage read + write + RegisteredSubnetCounter read
handle.record_cost(RuntimeHelper::<R>::db_read_gas_cost())?;
handle.record_cost(RuntimeHelper::<R>::db_read_gas_cost())?;
handle.record_cost(RuntimeHelper::<R>::db_write_gas_cost())?;
handle.record_db_reads::<R>(2)?;
handle.record_db_writes::<R>(1)?;

let counter = Self::current_subnet_counter(netuid);
let approval_key = (spender, netuid, counter);
Expand Down
Loading