difftreelog
fix eth weights
in: master
3 files changed
pallets/balances-adapter/src/erc.rsdiffbeforeafterboth--- 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<U256> {
- // self.consume_store_reads(1)?;
+ consume_store_reads(self, 1)?;
let owner = T::CrossAccountId::from_eth(owner);
let balance = <T as Config>::Currency::free_balance(owner.as_sub());
Ok(balance.into())
@@ -62,7 +63,7 @@
}
fn total_supply(&self) -> Result<U256> {
- // self.consume_store_reads(1)?;
+ consume_store_reads(self, 1)?;
let total = <T as Config>::Currency::total_issuance();
Ok(total.into())
}
@@ -126,7 +127,7 @@
T::AccountId: From<[u8; 32]>,
{
fn balance_of_cross(&self, owner: CrossAddress) -> Result<U256> {
- // self.consume_store_reads(1)?;
+ consume_store_reads(self, 1)?;
let owner = owner.into_sub_cross_account::<T>()?;
let balance = <T as Config>::Currency::free_balance(owner.as_sub());
Ok(balance.into())
pallets/balances-adapter/src/lib.rsdiffbeforeafterboth1// #![doc = include_str!("../README.md")]2#![cfg_attr(not(feature = "std"), no_std)]34extern crate alloc;5use frame_support::sp_runtime::DispatchResult;6pub use pallet::*;7use pallet_evm_coder_substrate::{WithRecorder, SubstrateRecorder};89pub mod common;10pub mod erc;1112pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;1314/// Handle for native fungible collection15pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);16impl<T: Config> NativeFungibleHandle<T> {17 /// Creates a handle18 pub fn new() -> NativeFungibleHandle<T> {19 Self(SubstrateRecorder::new(u64::MAX))20 }2122 /// Check if the collection is internal23 pub fn check_is_internal(&self) -> DispatchResult {24 Ok(())25 }26}2728impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {29 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {30 &self.031 }32 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {33 self.034 }35}36#[frame_support::pallet]37pub mod pallet {38 use alloc::string::String;39 use frame_support::{traits::Get};40 use pallet_balances::WeightInfo;41 use sp_core::U256;4243 #[pallet::config]44 pub trait Config:45 frame_system::Config + pallet_evm_coder_substrate::Config + pallet_common::Config46 {47 /// Currency from `pallet_balances`48 type Currency: frame_support::traits::Currency<49 Self::AccountId,50 Balance = Self::CurrencyBalance,51 >;52 /// Balance type of chain53 type CurrencyBalance: Into<U256> + TryFrom<U256> + PartialEq<u128> + From<u128> + Into<u128>;5455 /// Decimals of balance56 type Decimals: Get<u8>;57 /// Collection name58 type Name: Get<String>;59 /// Collection symbol60 type Symbol: Get<String>;6162 /// Weight information63 type WeightInfo: WeightInfo;64 }65 #[pallet::pallet]66 pub struct Pallet<T>(_);67}1// #![doc = include_str!("../README.md")]2#![cfg_attr(not(feature = "std"), no_std)]34extern crate alloc;5use core::ops::Deref;67use frame_support::sp_runtime::DispatchResult;8pub use pallet::*;9use pallet_evm_coder_substrate::{WithRecorder, SubstrateRecorder};1011pub mod common;12pub mod erc;1314pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;1516/// Handle for native fungible collection17pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);18impl<T: Config> NativeFungibleHandle<T> {19 /// Creates a handle20 pub fn new() -> NativeFungibleHandle<T> {21 Self(SubstrateRecorder::new(u64::MAX))22 }2324 /// Check if the collection is internal25 pub fn check_is_internal(&self) -> DispatchResult {26 Ok(())27 }28}2930impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {31 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {32 &self.033 }34 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {35 self.036 }37}3839impl<T: Config> Deref for NativeFungibleHandle<T> {40 type Target = SubstrateRecorder<T>;4142 fn deref(&self) -> &Self::Target {43 &self.044 }45}46#[frame_support::pallet]47pub mod pallet {48 use alloc::string::String;49 use frame_support::{traits::Get};50 use pallet_balances::WeightInfo;51 use sp_core::U256;5253 #[pallet::config]54 pub trait Config:55 frame_system::Config + pallet_evm_coder_substrate::Config + pallet_common::Config56 {57 /// Currency from `pallet_balances`58 type Currency: frame_support::traits::Currency<59 Self::AccountId,60 Balance = Self::CurrencyBalance,61 >;62 /// Balance type of chain63 type CurrencyBalance: Into<U256> + TryFrom<U256> + PartialEq<u128> + From<u128> + Into<u128>;6465 /// Decimals of balance66 type Decimals: Get<u8>;67 /// Collection name68 type Name: Get<String>;69 /// Collection symbol70 type Symbol: Get<String>;7172 /// Weight information73 type WeightInfo: WeightInfo;74 }75 #[pallet::pallet]76 pub struct Pallet<T>(_);77}pallets/common/src/lib.rsdiffbeforeafterboth--- 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(
- <T as frame_system::Config>::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(
- <T as frame_system::Config>::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 = <T as frame_system::Config>::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<T: Config>(
+ recorder: &SubstrateRecorder<T>,
+ reads: u64,
+) -> pallet_evm_coder_substrate::execution::Result<()> {
+ recorder.consume_gas(T::GasWeightMapping::weight_to_gas(Weight::from_parts(
+ <T as frame_system::Config>::DbWeight::get()
+ .read
+ .saturating_mul(reads),
+ // TODO: measure proof
+ 0,
+ )))
+}
+
+/// Consume gas for writing.
+pub fn consume_store_writes<T: Config>(
+ recorder: &SubstrateRecorder<T>,
+ writes: u64,
+) -> pallet_evm_coder_substrate::execution::Result<()> {
+ recorder.consume_gas(T::GasWeightMapping::weight_to_gas(Weight::from_parts(
+ <T as frame_system::Config>::DbWeight::get()
+ .write
+ .saturating_mul(writes),
+ // TODO: measure proof
+ 0,
+ )))
+}
+
+/// Consume gas for reading and writing.
+pub fn consume_store_reads_and_writes<T: Config>(
+ recorder: &SubstrateRecorder<T>,
+ reads: u64,
+ writes: u64,
+) -> pallet_evm_coder_substrate::execution::Result<()> {
+ let weight = <T as frame_system::Config>::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,
+ )))
+}