Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/rpc/rpc-api/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc-builder/tests/it/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 16 additions & 5 deletions crates/rpc/rpc/src/eth/api/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -171,6 +177,7 @@ where
block: BlockEnv,
request: CallRequest,
state: S,
state_override: Option<StateOverride>,
) -> EthResult<U256>
where
S: StateProvider,
Expand All @@ -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 {
Expand Down Expand Up @@ -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 })
}
Expand Down
2 changes: 2 additions & 0 deletions crates/rpc/rpc/src/eth/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
}
Expand Down
1 change: 1 addition & 0 deletions crates/rpc/rpc/src/eth/api/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ where
max_fee_per_blob_gas: None,
},
BlockId::Number(BlockNumberOrTag::Pending),
None,
)
.await?;
let gas_limit = estimated_gas;
Expand Down
5 changes: 4 additions & 1 deletion crates/rpc/rpc/src/eth/revm_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
Loading