Skip to content

Commit

Permalink
Fix estimate used gas and estimation for WeightV2
Browse files Browse the repository at this point in the history
  • Loading branch information
tgmichel committed Jul 7, 2023
1 parent 3c75079 commit 3ffd1b7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
5 changes: 4 additions & 1 deletion frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,10 @@ impl<T: Config> Pallet<T> {
Ok(PostDispatchInfo {
actual_weight: {
let mut gas_to_weight = T::GasWeightMapping::gas_to_weight(
used_gas.standard.unique_saturated_into(),
sp_std::cmp::max(
used_gas.standard.unique_saturated_into(),
used_gas.effective.unique_saturated_into(),
),
true,
);
if let Some(weight_info) = weight_info {
Expand Down
86 changes: 79 additions & 7 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use fp_evm::weight_per_gas;
use fp_rpc::TransactionStatus;
use pallet_ethereum::{Call::transact, PostLogContent, Transaction as EthereumTransaction};
use pallet_evm::{
Account as EVMAccount, EnsureAccountId20, FeeCalculator, IdentityAddressMapping, Runner,
Account as EVMAccount, EnsureAccountId20, FeeCalculator, GasWeightMapping, IdentityAddressMapping, Runner,
};

// A few exports that help ease life for downstream crates.
Expand Down Expand Up @@ -689,6 +689,41 @@ impl_runtime_apis! {
let is_transactional = false;
let validate = true;
let evm_config = config.as_ref().unwrap_or(<Runtime as pallet_evm::Config>::config());

let mut estimated_transaction_len = data.len() +
// to: 20
// from: 20
// value: 32
// gas_limit: 32
// nonce: 32
// 1 byte transaction action variant
// chain id 8 bytes
// 65 bytes signature
210;
if max_fee_per_gas.is_some() {
estimated_transaction_len += 32;
}
if max_priority_fee_per_gas.is_some() {
estimated_transaction_len += 32;
}
if access_list.is_some() {
estimated_transaction_len += access_list.encoded_size();
}

let gas_limit = gas_limit.min(u64::MAX.into()).low_u64();
let without_base_extrinsic_weight = true;

let (weight_limit, proof_size_base_cost) =
match <Runtime as pallet_evm::Config>::GasWeightMapping::gas_to_weight(
gas_limit,
without_base_extrinsic_weight
) {
weight_limit if weight_limit.proof_size() > 0 => {
(Some(weight_limit), Some(estimated_transaction_len as u64))
}
_ => (None, None),
};

<Runtime as pallet_evm::Config>::Runner::call(
from,
to,
Expand All @@ -701,9 +736,8 @@ impl_runtime_apis! {
access_list.unwrap_or_default(),
is_transactional,
validate,
// TODO we probably want to support external cost recording in non-transactional calls
None,
None,
weight_limit,
proof_size_base_cost,
evm_config,
).map_err(|err| err.error.into())
}
Expand All @@ -730,6 +764,45 @@ impl_runtime_apis! {
let is_transactional = false;
let validate = true;
let evm_config = config.as_ref().unwrap_or(<Runtime as pallet_evm::Config>::config());

let mut estimated_transaction_len = data.len() +
// to: 20
// from: 20
// value: 32
// gas_limit: 32
// nonce: 32
// 1 byte transaction action variant
// chain id 8 bytes
// 65 bytes signature
210;
if max_fee_per_gas.is_some() {
estimated_transaction_len += 32;
}
if max_priority_fee_per_gas.is_some() {
estimated_transaction_len += 32;
}
if access_list.is_some() {
estimated_transaction_len += access_list.encoded_size();
}

let gas_limit = if gas_limit > U256::from(u64::MAX) {
u64::MAX
} else {
gas_limit.low_u64()
};
let without_base_extrinsic_weight = true;

let (weight_limit, proof_size_base_cost) =
match <Runtime as pallet_evm::Config>::GasWeightMapping::gas_to_weight(
gas_limit,
without_base_extrinsic_weight
) {
weight_limit if weight_limit.proof_size() > 0 => {
(Some(weight_limit), Some(estimated_transaction_len as u64))
}
_ => (None, None),
};

<Runtime as pallet_evm::Config>::Runner::create(
from,
data,
Expand All @@ -741,9 +814,8 @@ impl_runtime_apis! {
access_list.unwrap_or_default(),
is_transactional,
validate,
// TODO we probably want to support external cost recording in non-transactional calls
None,
None,
weight_limit,
proof_size_base_cost,
evm_config,
).map_err(|err| err.error.into())
}
Expand Down

0 comments on commit 3ffd1b7

Please sign in to comment.