Add stateOverrides-to-estimate-gas-call (#5604)

This commit is contained in:
Aditya Pandey
2023-11-30 17:16:44 +05:30
committed by GitHub
parent a7f474cb0b
commit 00804191ff
6 changed files with 25 additions and 7 deletions

View File

@ -190,6 +190,7 @@ pub trait EthApi {
&self,
request: CallRequest,
block_number: Option<BlockId>,
state_override: Option<StateOverride>,
) -> RpcResult<U256>;
/// Returns the current price per gas in wei.

View File

@ -160,7 +160,7 @@ where
EthApiClient::create_access_list(client, call_request.clone(), Some(block_number.into()))
.await
.unwrap();
EthApiClient::estimate_gas(client, call_request.clone(), Some(block_number.into()))
EthApiClient::estimate_gas(client, call_request.clone(), Some(block_number.into()), None)
.await
.unwrap();
EthApiClient::call(client, call_request.clone(), Some(block_number.into()), None, None)

View File

@ -4,8 +4,9 @@ use crate::{
eth::{
error::{ensure_success, EthApiError, EthResult, RevertError, RpcInvalidTransactionError},
revm_utils::{
build_call_evm_env, caller_gas_allowance, cap_tx_gas_limit_with_caller_allowance,
get_precompiles, inspect, prepare_call_env, transact, EvmOverrides,
apply_state_overrides, build_call_evm_env, caller_gas_allowance,
cap_tx_gas_limit_with_caller_allowance, get_precompiles, inspect, prepare_call_env,
transact, EvmOverrides,
},
EthTransactions,
},
@ -41,12 +42,17 @@ where
Network: NetworkInfo + Send + Sync + 'static,
{
/// Estimate gas needed for execution of the `request` at the [BlockId].
pub async fn estimate_gas_at(&self, request: CallRequest, at: BlockId) -> EthResult<U256> {
pub async fn estimate_gas_at(
&self,
request: CallRequest,
at: BlockId,
state_override: Option<StateOverride>,
) -> EthResult<U256> {
let (cfg, block_env, at) = self.evm_env_at(at).await?;
self.on_blocking_task(|this| async move {
let state = this.state_at(at)?;
this.estimate_gas_with(cfg, block_env, request, state)
this.estimate_gas_with(cfg, block_env, request, state, state_override)
})
.await
}
@ -171,6 +177,7 @@ where
block: BlockEnv,
request: CallRequest,
state: S,
state_override: Option<StateOverride>,
) -> EthResult<U256>
where
S: StateProvider,
@ -197,6 +204,10 @@ where
let mut env = build_call_evm_env(cfg, block, request)?;
let mut db = CacheDB::new(StateProviderDatabase::new(state));
if let Some(state_override) = state_override {
// apply state overrides
apply_state_overrides(state_override, &mut db)?;
}
// if the request is a simple transfer we can optimize
if env.tx.data.is_empty() {
if let TransactTo::Call(to) = env.tx.transact_to {
@ -409,7 +420,7 @@ where
// calculate the gas used using the access list
request.access_list = Some(access_list.clone());
let gas_used = self.estimate_gas_with(env.cfg, env.block, request, db.db.state())?;
let gas_used = self.estimate_gas_with(env.cfg, env.block, request, db.db.state(), None)?;
Ok(AccessListWithGasUsed { access_list, gas_used })
}

View File

@ -266,12 +266,14 @@ where
&self,
request: CallRequest,
block_number: Option<BlockId>,
state_override: Option<StateOverride>,
) -> Result<U256> {
trace!(target: "rpc::eth", ?request, ?block_number, "Serving eth_estimateGas");
Ok(self
.estimate_gas_at(
request,
block_number.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest)),
state_override,
)
.await?)
}

View File

@ -530,6 +530,7 @@ where
max_fee_per_blob_gas: None,
},
BlockId::Number(BlockNumberOrTag::Pending),
None,
)
.await?;
let gas_limit = estimated_gas;

View File

@ -520,7 +520,10 @@ fn apply_block_overrides(overrides: BlockOverrides, env: &mut BlockEnv) {
}
/// Applies the given state overrides (a set of [AccountOverride]) to the [CacheDB].
fn apply_state_overrides<DB>(overrides: StateOverride, db: &mut CacheDB<DB>) -> EthResult<()>
pub(crate) fn apply_state_overrides<DB>(
overrides: StateOverride,
db: &mut CacheDB<DB>,
) -> EthResult<()>
where
DB: DatabaseRef,
EthApiError: From<<DB as DatabaseRef>::Error>,