--- a/pallets/balances-adapter/src/erc.rs +++ b/pallets/balances-adapter/src/erc.rs @@ -3,6 +3,7 @@ use frame_support::traits::{Currency, ExistenceRequirement}; use pallet_balances::WeightInfo; use pallet_common::{ + consume_store_reads, erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult}, eth::CrossAddress, }; @@ -43,7 +44,7 @@ } fn balance_of(&self, owner: Address) -> Result { - // self.consume_store_reads(1)?; + consume_store_reads(self, 1)?; let owner = T::CrossAccountId::from_eth(owner); let balance = ::Currency::free_balance(owner.as_sub()); Ok(balance.into()) @@ -62,7 +63,7 @@ } fn total_supply(&self) -> Result { - // self.consume_store_reads(1)?; + consume_store_reads(self, 1)?; let total = ::Currency::total_issuance(); Ok(total.into()) } @@ -126,7 +127,7 @@ T::AccountId: From<[u8; 32]>, { fn balance_of_cross(&self, owner: CrossAddress) -> Result { - // self.consume_store_reads(1)?; + consume_store_reads(self, 1)?; let owner = owner.into_sub_cross_account::()?; let balance = ::Currency::free_balance(owner.as_sub()); Ok(balance.into()) --- a/pallets/balances-adapter/src/lib.rs +++ b/pallets/balances-adapter/src/lib.rs @@ -2,6 +2,8 @@ #![cfg_attr(not(feature = "std"), no_std)] extern crate alloc; +use core::ops::Deref; + use frame_support::sp_runtime::DispatchResult; pub use pallet::*; use pallet_evm_coder_substrate::{WithRecorder, SubstrateRecorder}; @@ -33,6 +35,14 @@ self.0 } } + +impl Deref for NativeFungibleHandle { + type Target = SubstrateRecorder; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} #[frame_support::pallet] pub mod pallet { use alloc::string::String; --- a/pallets/common/src/lib.rs +++ b/pallets/common/src/lib.rs @@ -153,14 +153,7 @@ &self, reads: u64, ) -> pallet_evm_coder_substrate::execution::Result<()> { - self.recorder - .consume_gas(T::GasWeightMapping::weight_to_gas(Weight::from_parts( - ::DbWeight::get() - .read - .saturating_mul(reads), - // TODO: measure proof - 0, - ))) + consume_store_reads(self.recorder(), reads) } /// Consume gas for writing. @@ -168,14 +161,7 @@ &self, writes: u64, ) -> pallet_evm_coder_substrate::execution::Result<()> { - self.recorder - .consume_gas(T::GasWeightMapping::weight_to_gas(Weight::from_parts( - ::DbWeight::get() - .write - .saturating_mul(writes), - // TODO: measure proof - 0, - ))) + consume_store_writes(self.recorder(), writes) } /// Consume gas for reading and writing. @@ -184,15 +170,7 @@ reads: u64, writes: u64, ) -> pallet_evm_coder_substrate::execution::Result<()> { - let weight = ::DbWeight::get(); - let reads = weight.read.saturating_mul(reads); - let writes = weight.read.saturating_mul(writes); - self.recorder - .consume_gas(T::GasWeightMapping::weight_to_gas(Weight::from_parts( - reads.saturating_add(writes), - // TODO: measure proof - 0, - ))) + consume_store_reads_and_writes(self.recorder(), reads, writes) } /// Save collection to storage. @@ -2345,3 +2323,47 @@ } } } + +/// Consume gas for reading. +pub fn consume_store_reads( + recorder: &SubstrateRecorder, + reads: u64, +) -> pallet_evm_coder_substrate::execution::Result<()> { + recorder.consume_gas(T::GasWeightMapping::weight_to_gas(Weight::from_parts( + ::DbWeight::get() + .read + .saturating_mul(reads), + // TODO: measure proof + 0, + ))) +} + +/// Consume gas for writing. +pub fn consume_store_writes( + recorder: &SubstrateRecorder, + writes: u64, +) -> pallet_evm_coder_substrate::execution::Result<()> { + recorder.consume_gas(T::GasWeightMapping::weight_to_gas(Weight::from_parts( + ::DbWeight::get() + .write + .saturating_mul(writes), + // TODO: measure proof + 0, + ))) +} + +/// Consume gas for reading and writing. +pub fn consume_store_reads_and_writes( + recorder: &SubstrateRecorder, + reads: u64, + writes: u64, +) -> pallet_evm_coder_substrate::execution::Result<()> { + let weight = ::DbWeight::get(); + let reads = weight.read.saturating_mul(reads); + let writes = weight.read.saturating_mul(writes); + recorder.consume_gas(T::GasWeightMapping::weight_to_gas(Weight::from_parts( + reads.saturating_add(writes), + // TODO: measure proof + 0, + ))) +}