Skip to content

Commit

Permalink
feat: record weights for erc20 precompiles
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Aug 29, 2023
1 parent 4c8ba8c commit 4eee5f0
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 48 deletions.
1 change: 1 addition & 0 deletions crates/dex-stable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ use sp_std::{ops::Sub, vec, vec::Vec};

pub use default_weights::WeightInfo;
pub use pallet::*;
pub use primitives::Pool;
use primitives::*;
use traits::{StablePoolLpCurrencyIdGenerate, ValidateCurrency};

Expand Down
2 changes: 2 additions & 0 deletions parachain/runtime/common/evm/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ fn parse_event_enum(input: DeriveInput) -> syn::Result<TokenStream> {
let mut writer = ::evm_utils::Writer::new();
let data = writer.write(data).build();

handle.record_cost(::evm_utils::log_cost(topics.len(), data.len())?)?;

handle.log(
handle.context().address,
topics,
Expand Down
9 changes: 7 additions & 2 deletions parachain/runtime/common/evm/macro/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sp_core::{H160, H256, U256};
struct MockPrecompileHandle {
context: fp_evm::Context,
logs: Vec<Log>,
gas_used: u64,
}

impl fp_evm::PrecompileHandle for MockPrecompileHandle {
Expand All @@ -21,8 +22,9 @@ impl fp_evm::PrecompileHandle for MockPrecompileHandle {
unimplemented!()
}

fn record_cost(&mut self, _: u64) -> Result<(), fp_evm::ExitError> {
unimplemented!()
fn record_cost(&mut self, cost: u64) -> Result<(), fp_evm::ExitError> {
self.gas_used += cost;
Ok(())
}

fn remaining_gas(&self) -> u64 {
Expand Down Expand Up @@ -150,6 +152,7 @@ fn generate_event() {
apparent_value: Default::default(),
},
logs: Default::default(),
gas_used: Default::default(),
};

assert_ok!(Event::Transfer {
Expand All @@ -159,6 +162,8 @@ fn generate_event() {
}
.log(&mut precompile_handle));

assert_eq!(precompile_handle.gas_used, 1756);

let mut data = [0u8; 32];
U256::one().to_big_endian(&mut data);

Expand Down
18 changes: 17 additions & 1 deletion parachain/runtime/common/evm/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]

use pallet_evm::{ExitSucceed, PrecompileFailure, PrecompileOutput};
use pallet_evm::{ExitError, ExitSucceed, PrecompileFailure, PrecompileOutput};
use sp_std::prelude::*;

mod evm_codec;
Expand All @@ -24,3 +24,19 @@ pub fn new_precompile_output<T: EvmCodec>(value: T) -> PrecompileOutput {
output: encode_return_data(value),
}
}

// https://github.com/rust-blockchain/evm/blob/a33ac87ad7462b7e7029d12c385492b2a8311d1c/gasometer/src/costs.rs#L147-L163
fn log_cost_inner(topics: usize, data_len: usize) -> Option<u64> {
const G_LOG: u64 = 375;
const G_LOGDATA: u64 = 8;
const G_LOGTOPIC: u64 = 375;
let topic_cost = G_LOGTOPIC.checked_mul(topics as u64)?;
let data_cost = G_LOGDATA.checked_mul(data_len as u64)?;
G_LOG.checked_add(topic_cost)?.checked_add(data_cost)
}

pub fn log_cost(topics: usize, data_len: usize) -> EvmResult<u64> {
log_cost_inner(topics, data_len).ok_or(PrecompileFailure::Error {
exit_status: ExitError::OutOfGas,
})
}
11 changes: 10 additions & 1 deletion parachain/runtime/common/evm/utils/src/revert_reason.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ extern crate alloc;

use crate::{EvmString, Writer};
use alloc::string::{String, ToString};
use frame_support::dispatch::DispatchError;
use frame_support::{
dispatch::{DispatchError, PostDispatchInfo},
sp_runtime::DispatchErrorWithPostInfo,
};
use pallet_evm::{ExitRevert, PrecompileFailure};
use sp_std::prelude::*;

Expand Down Expand Up @@ -75,3 +78,9 @@ impl From<DispatchError> for RevertReason {
RevertReason::custom(alloc::format!("Runtime error: {err:?}"))
}
}

impl From<DispatchErrorWithPostInfo<PostDispatchInfo>> for RevertReason {
fn from(error_and_info: DispatchErrorWithPostInfo<PostDispatchInfo>) -> Self {
RevertReason::custom(alloc::format!("Runtime error: {:?}", error_and_info.error))
}
}
Loading

0 comments on commit 4eee5f0

Please sign in to comment.