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

feat(anvil): add eth_simulateV1 rpc call #10030

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
Prev Previous commit
Next Next commit
changes to get status and error properly populated
and also some structural fixes
dbeal-eth committed Mar 7, 2025

Verified

This commit was signed with the committer’s verified signature.
dbeal-eth dbeal
commit d0b921f2e1c41a283093e7134bd8d31848ffef44
6 changes: 3 additions & 3 deletions crates/anvil/src/eth/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
backend::{db::SerializableBlock, mem::{state, BlockRequest, State}},
backend::mem::{state, BlockRequest, State},
sign::build_typed_transaction,
};
use crate::{
@@ -30,7 +30,7 @@ use crate::{
revm::primitives::{BlobExcessGasAndPrice, Output},
ClientFork, LoggingManager, Miner, MiningMode, StorageInfo,
};
use alloy_consensus::{transaction::eip4844::TxEip4844Variant, Account};
use alloy_consensus::{transaction::eip4844::TxEip4844Variant, Account, Header};
use alloy_dyn_abi::TypedData;
use alloy_eips::eip2718::Encodable2718;
use alloy_network::{
@@ -100,7 +100,7 @@ pub const CLIENT_VERSION: &str = concat!("anvil/v", env!("CARGO_PKG_VERSION"));
#[derive(Serialize)]
#[serde(untagged)]
pub enum SimulatedBlockResponse {
AnvilInternal(Vec<SimulatedBlock<alloy_consensus::Header>>),
AnvilInternal(Vec<SimulatedBlock<alloy_rpc_types::Block<alloy_rpc_types::Transaction, Header>>>),
Forked(Vec<SimulatedBlock<WithOtherFields<alloy_rpc_types::Block<WithOtherFields<alloy_rpc_types::Transaction<alloy_network::AnyTxEnvelope>>, alloy_rpc_types::Header<alloy_network::AnyHeader>>>>>)
}

34 changes: 23 additions & 11 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! In-memory blockchain backend.
use self::state::trie_storage;
use super::{db::SerializableBlock, executor::new_evm_with_inspector_ref};
use super::executor::new_evm_with_inspector_ref;
use super::super::sign::build_typed_transaction;
use crate::{
config::PruneStateHistoryConfig,
@@ -1459,7 +1459,7 @@ impl Backend {
&self,
request: SimulatePayload,
block_request: Option<BlockRequest>,
) -> Result<Vec<SimulatedBlock<Header>>, BlockchainError> {
) -> Result<Vec<SimulatedBlock<alloy_rpc_types::Block<Transaction, alloy_consensus::Header>>>, BlockchainError> {
// first, snapshot the current state
let state_id = self.db.write().await.snapshot_state();

@@ -1509,15 +1509,19 @@ impl Backend {
}
}

let pool_transactions = block.calls.iter().enumerate().map(|(i, t)| Arc::new(PoolTransaction {
let pool_transactions = block.calls.iter().map(|t| Arc::new(PoolTransaction {
pending_transaction: PendingTransaction::with_impersonated(
build_typed_transaction(TypedTransactionRequest::EIP1559(TxEip1559 {
nonce: t.nonce.unwrap_or_default(),
// TODO: how to do gwei conversion? the number below
// for now should be 100 gwei
max_fee_per_gas: t.max_fee_per_gas.unwrap_or(100000000000),
max_priority_fee_per_gas: t.max_priority_fee_per_gas.unwrap_or_default(),
gas_limit: t.gas.unwrap_or(30000000),
// TODO: seems like it would be best to estimate the
// amount of gas needed here if not provided? or
// otherwise, not bound the block by the total required
// gas. for now I set to 1 million for testing
gas_limit: t.gas.unwrap_or(1000000),
value: t.value.unwrap_or_default(),
input: t.input.clone().into_input().unwrap_or_default(),
to: t.to.unwrap_or_default(),
@@ -1532,7 +1536,9 @@ impl Backend {
),
requires: Vec::new(),
provides: Vec::new(),
priority: TransactionPriority(i.try_into().unwrap())
priority: TransactionPriority(0)
// TODO: priority is by highest first, so just doing a sort of reverse here
//priority: TransactionPriority((1000000000 - i).try_into().unwrap())
})).collect();

// TODO: it may be possible to be more lightweight with our handling of blot.ck mining
@@ -1542,19 +1548,25 @@ impl Backend {
return Err(RpcError::internal_error_with("txn was invalid").into());
}

let executed_block = self.get_block(outcome.block_number).unwrap();

let mut simulated_block = SimulatedBlock {
inner: self.get_block(outcome.block_number).unwrap().header,
inner: alloy_rpc_types::Block { header: executed_block.header, transactions: BlockTransactions::Hashes(outcome.included.iter().map(|t| t.hash()).collect()), uncles: Vec::new(), withdrawals: Some(alloy_rpc_types::Withdrawals(Vec::new())) },
calls: Vec::new()
};

for tx in outcome.included {
let receipt = self.mined_transaction_receipt(tx.hash()).unwrap();
let status = receipt.out.is_some();
let MinedTransaction { info, receipt: raw_receipt, .. } =
self.blockchain.get_transaction_by_hash(&tx.hash()).unwrap();
let receipt = raw_receipt.as_receipt_with_bloom().receipt.clone();

let status = match receipt.status { alloy_consensus::Eip658Value::Eip658(true) => true, _ => false};

simulated_block.calls.push(SimCallResult {
return_data: receipt.out.unwrap_or_default(),
return_data: info.out.unwrap_or_default(),
// TODO: fill actual error
error: None,
gas_used: receipt.inner.gas_used,
error: match status { false => Some(alloy_rpc_types::simulate::SimulateError { code: -3200, message: "execution failed".to_string() }), true => None },
gas_used: receipt.cumulative_gas_used,
// TODO: fill actual logs
logs: Vec::new(),
status