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

added arg and computing state in function of some overrides #6985

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion crates/anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ pub enum EthRequest {
),

#[cfg_attr(feature = "serde", serde(rename = "eth_estimateGas"))]
EthEstimateGas(CallRequest, #[cfg_attr(feature = "serde", serde(default))] Option<BlockId>),
EthEstimateGas(
CallRequest,
#[cfg_attr(feature = "serde", serde(default))] Option<BlockId>,
#[cfg_attr(feature = "serde", serde(default))] Option<StateOverride>,
),

#[cfg_attr(feature = "serde", serde(rename = "eth_getTransactionByHash", with = "sequence"))]
EthGetTransactionByHash(TxHash),
Expand Down
41 changes: 32 additions & 9 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use super::{backend::mem::BlockRequest, sign::build_typed_transaction};
use super::{
backend::mem::{state, BlockRequest},
sign::build_typed_transaction,
};
use crate::{
eth::{
backend,
Expand Down Expand Up @@ -228,8 +231,8 @@ impl EthApi {
EthRequest::EthCreateAccessList(call, block) => {
self.create_access_list(call, block).await.to_rpc_result()
}
EthRequest::EthEstimateGas(call, block) => {
self.estimate_gas(call, block).await.to_rpc_result()
EthRequest::EthEstimateGas(call, block, overrides) => {
self.estimate_gas(call, block, overrides).await.to_rpc_result()
}
EthRequest::EthGetTransactionByBlockHashAndIndex(hash, index) => {
self.transaction_by_block_hash_and_index(hash, index).await.to_rpc_result()
Expand Down Expand Up @@ -860,7 +863,9 @@ impl EthApi {

if request.gas.is_none() {
// estimate if not provided
if let Ok(gas) = self.estimate_gas(request.clone().into_call_request(), None).await {
if let Ok(gas) =
self.estimate_gas(request.clone().into_call_request(), None, None).await
{
request.gas = Some(gas);
}
}
Expand All @@ -886,7 +891,9 @@ impl EthApi {

if request.gas.is_none() {
// estimate if not provided
if let Ok(gas) = self.estimate_gas(request.clone().into_call_request(), None).await {
if let Ok(gas) =
self.estimate_gas(request.clone().into_call_request(), None, None).await
{
request.gas = Some(gas);
}
}
Expand Down Expand Up @@ -1081,10 +1088,15 @@ impl EthApi {
&self,
request: CallRequest,
block_number: Option<BlockId>,
overrides: Option<StateOverride>,
) -> Result<U256> {
node_info!("eth_estimateGas");
self.do_estimate_gas(request, block_number.or_else(|| Some(BlockNumber::Pending.into())))
.await
self.do_estimate_gas(
request,
block_number.or_else(|| Some(BlockNumber::Pending.into())),
overrides,
)
.await
}

/// Get transaction by its hash.
Expand Down Expand Up @@ -2144,12 +2156,18 @@ impl EthApi {
&self,
request: CallRequest,
block_number: Option<BlockId>,
overrides: Option<StateOverride>,
) -> Result<U256> {
let block_request = self.block_request(block_number).await?;
// check if the number predates the fork, if in fork mode
if let BlockRequest::Number(number) = block_request {
if let Some(fork) = self.get_fork() {
if fork.predates_fork(number) {
if overrides.is_some() {
return Err(BlockchainError::StateOverrideError(
"not available on past forked blocks".to_string(),
));
}
return fork
.estimate_gas(&request, Some(number.into()))
.await
Expand All @@ -2159,8 +2177,13 @@ impl EthApi {
}

self.backend
.with_database_at(Some(block_request), |state, block| {
self.do_estimate_gas_with_state(request, state, block)
.with_database_at(Some(block_request), |state, block| match overrides {
None => self.do_estimate_gas_with_state(request, state, block),
Some(overrides) => {
let state =
state::apply_state_override(overrides.into_iter().collect(), state)?;
self.do_estimate_gas_with_state(request, state, block)
}
})
.await?
}
Expand Down
2 changes: 1 addition & 1 deletion crates/anvil/tests/it/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ async fn estimates_gas_on_pending_by_default() {

let tx =
TransactionRequest::new().from(recipient).to(sender).value(1e10 as u64).data(vec![0x42]);
api.estimate_gas(to_call_request_from_tx_request(tx), None).await.unwrap();
api.estimate_gas(to_call_request_from_tx_request(tx), None, None).await.unwrap();
}

#[tokio::test(flavor = "multi_thread")]
Expand Down