Skip to content

Commit

Permalink
Fix eth_call and estimate_gas for older runtimes (#1546)
Browse files Browse the repository at this point in the history
* Use biggiest proof_size_base_cost

* Upgrade frontier locked commit
  • Loading branch information
boundless-forest authored and aurexav committed Jul 25, 2024
1 parent 81ced2b commit 8e54ed9
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 125 deletions.
48 changes: 24 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 75 additions & 33 deletions runtime/crab/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,9 @@ sp_api::impl_runtime_apis! {
access_list: Option<Vec<(H160, Vec<H256>)>>,
) -> Result<pallet_evm::CallInfo, sp_runtime::DispatchError> {
// frontier
use pallet_evm::Runner;
use pallet_ethereum::{TransactionData, TransactionAction};
use pallet_evm::{Runner, GasWeightMapping as _};
// polkadot-sdk
use sp_runtime::traits::{UniqueSaturatedInto, Get};
use sp_runtime::traits::UniqueSaturatedInto;

let config = if estimate {
let mut config = <Runtime as pallet_evm::Config>::config().clone();
Expand All @@ -484,20 +483,39 @@ sp_api::impl_runtime_apis! {
None
};

let gas_limit = gas_limit.min(u64::MAX.into());
let transaction_data = TransactionData::new(
TransactionAction::Call(to),
data.clone(),
nonce.unwrap_or_default(),
gas_limit,
None,
max_fee_per_gas,
max_priority_fee_per_gas,
value,
Some(<Runtime as pallet_evm::Config>::ChainId::get()),
access_list.clone().unwrap_or_default(),
);
let (weight_limit, proof_size_base_cost) = pallet_ethereum::Pallet::<Runtime>::transaction_weight(&transaction_data);
// Estimated encoded transaction size must be based on the heaviest transaction
// type (EIP1559Transaction) to be compatible with all transaction types.
let mut estimated_transaction_len = data.len() +
// pallet ethereum index: 1
// transact call index: 1
// Transaction enum variant: 1
// chain_id 8 bytes
// nonce: 32
// max_priority_fee_per_gas: 32
// max_fee_per_gas: 32
// gas_limit: 32
// action: 21 (enum varianrt + call address)
// value: 32
// access_list: 1 (empty vec size)
// 65 bytes signature
258;

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,
Expand Down Expand Up @@ -529,10 +547,9 @@ sp_api::impl_runtime_apis! {
access_list: Option<Vec<(H160, Vec<H256>)>>,
) -> Result<pallet_evm::CreateInfo, sp_runtime::DispatchError> {
// frontier
use pallet_evm::Runner;
use pallet_ethereum::{TransactionData, TransactionAction};
use pallet_evm::{Runner, GasWeightMapping as _};
// polkadot-sdk
use sp_runtime::traits::{UniqueSaturatedInto, Get};
use sp_runtime::traits::UniqueSaturatedInto;

let config = if estimate {
let mut config = <Runtime as pallet_evm::Config>::config().clone();
Expand All @@ -542,19 +559,44 @@ sp_api::impl_runtime_apis! {
None
};

let transaction_data = TransactionData::new(
TransactionAction::Create,
data.clone(),
nonce.unwrap_or_default(),
gas_limit,
None,
max_fee_per_gas,
max_priority_fee_per_gas,
value,
Some(<Runtime as pallet_evm::Config>::ChainId::get()),
access_list.clone().unwrap_or_default(),
);
let (weight_limit, proof_size_base_cost) = pallet_ethereum::Pallet::<Runtime>::transaction_weight(&transaction_data);
let mut estimated_transaction_len = data.len() +
// from: 20
// value: 32
// gas_limit: 32
// nonce: 32
// 1 byte transaction action variant
// chain id 8 bytes
// 65 bytes signature
190;

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 Down
Loading

0 comments on commit 8e54ed9

Please sign in to comment.