Skip to content
Open
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
26 changes: 25 additions & 1 deletion crates/anvil/core/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ pub enum EthRequest {
EthCallBundle(EthCallBundle),

#[serde(rename = "eth_simulateV1")]
EthSimulateV1(SimulatePayload, #[serde(default)] Option<BlockId>),
EthSimulateV1(
SimulatePayload<WithOtherFields<TransactionRequest>>,
#[serde(default)] Option<BlockId>,
),

#[serde(rename = "eth_createAccessList")]
EthCreateAccessList(
Expand Down Expand Up @@ -1378,6 +1381,27 @@ mod tests {
let _req = serde_json::from_value::<EthRequest>(value).unwrap();
}

#[test]
fn test_simulate_preserves_transaction_extension_fields() {
let value = serde_json::json!({
"method": "eth_simulateV1",
"params": [{
"blockStateCalls": [{
"calls": [{
"to": "0x0000000000000000000000000000000000000001",
"tempoExtension": "preserved"
}]
}]
}]
});

let request = serde_json::from_value::<EthRequest>(value).unwrap();
let EthRequest::EthSimulateV1(payload, _) = request else { panic!() };
let transaction = &payload.block_state_calls[0].calls[0];

assert_eq!(transaction.other["tempoExtension"], serde_json::json!("preserved"));
}

#[test]
fn test_custom_set_code() {
let s = r#"{"method": "anvil_setCode", "params":
Expand Down
226 changes: 141 additions & 85 deletions crates/anvil/src/eth/api.rs

Large diffs are not rendered by default.

74 changes: 56 additions & 18 deletions crates/anvil/src/eth/backend/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
mem::inspector::{AnvilInspector, InspectorTxConfig},
};
use alloy_consensus::{
Eip658Value, Transaction, TransactionEnvelope, TxReceipt,
Eip658Value, Receipt, ReceiptWithBloom, Transaction, TransactionEnvelope, TxReceipt,
transaction::{Either, Recovered},
};
use alloy_eips::{
Expand All @@ -24,7 +24,7 @@ use alloy_evm::{
receipt_builder::{ReceiptBuilder, ReceiptBuilderCtx},
},
};
use alloy_primitives::{Address, B256, Bytes, U256};
use alloy_primitives::{Address, B256, Bytes, Log, U256};
use anvil_core::eth::transaction::{
MaybeImpersonatedTransaction, PendingTransaction, TransactionInfo,
};
Expand All @@ -45,36 +45,74 @@ use std::{fmt, fmt::Debug, mem::take, sync::Arc};
#[non_exhaustive]
pub struct FoundryReceiptBuilder;

impl ReceiptBuilder for FoundryReceiptBuilder {
type Transaction = FoundryTxEnvelope;
type Receipt = FoundryReceiptEnvelope;

fn build_receipt<E: Evm>(
&self,
ctx: ReceiptBuilderCtx<'_, FoundryTxType, E>,
impl FoundryReceiptBuilder {
fn wrap_receipt(
tx_type: FoundryTxType,
receipt: ReceiptWithBloom<Receipt>,
) -> FoundryReceiptEnvelope {
let receipt = alloy_consensus::Receipt {
status: Eip658Value::Eip658(ctx.result.is_success()),
cumulative_gas_used: ctx.cumulative_gas_used,
logs: ctx.result.into_logs(),
}
.with_bloom();

match ctx.tx_type {
match tx_type {
FoundryTxType::Legacy => FoundryReceiptEnvelope::Legacy(receipt),
FoundryTxType::Eip2930 => FoundryReceiptEnvelope::Eip2930(receipt),
FoundryTxType::Eip1559 => FoundryReceiptEnvelope::Eip1559(receipt),
FoundryTxType::Eip4844 => FoundryReceiptEnvelope::Eip4844(receipt),
FoundryTxType::Eip7702 => FoundryReceiptEnvelope::Eip7702(receipt),
#[cfg(feature = "optimism")]
FoundryTxType::Deposit => {
unreachable!("deposit receipts are built in commit_transaction")
unreachable!("deposit receipts require fork-specific metadata")
}
#[cfg(feature = "optimism")]
FoundryTxType::PostExec => FoundryReceiptEnvelope::PostExec(receipt),
FoundryTxType::Tempo => FoundryReceiptEnvelope::Tempo(receipt),
}
}

/// Builds a typed receipt for an RPC-simulated transaction.
pub(crate) fn build_simulated_receipt(
tx_type: FoundryTxType,
result: &ExecutionResult,
logs: Vec<Log>,
cumulative_gas_used: u64,
deposit_nonce: Option<u64>,
deposit_receipt_version: Option<u64>,
) -> FoundryReceiptEnvelope {
let receipt =
Receipt { status: Eip658Value::Eip658(result.is_success()), cumulative_gas_used, logs }
.with_bloom();
#[cfg(feature = "optimism")]
if tx_type == FoundryTxType::Deposit {
return FoundryReceiptEnvelope::Deposit(
op_alloy_consensus::OpDepositReceiptWithBloom {
receipt: op_alloy_consensus::OpDepositReceipt {
inner: receipt.receipt,
deposit_nonce,
deposit_receipt_version,
},
logs_bloom: receipt.logs_bloom,
},
);
}
#[cfg(not(feature = "optimism"))]
let _ = (deposit_nonce, deposit_receipt_version);
Self::wrap_receipt(tx_type, receipt)
}
}

impl ReceiptBuilder for FoundryReceiptBuilder {
type Transaction = FoundryTxEnvelope;
type Receipt = FoundryReceiptEnvelope;

fn build_receipt<E: Evm>(
&self,
ctx: ReceiptBuilderCtx<'_, FoundryTxType, E>,
) -> FoundryReceiptEnvelope {
let receipt = Receipt {
status: Eip658Value::Eip658(ctx.result.is_success()),
cumulative_gas_used: ctx.cumulative_gas_used,
logs: ctx.result.into_logs(),
}
.with_bloom();
Self::wrap_receipt(ctx.tx_type, receipt)
}
}

/// Result of executing a transaction in [`AnvilBlockExecutor`].
Expand Down
46 changes: 37 additions & 9 deletions crates/anvil/src/eth/backend/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,17 @@ impl<N: Network> ClientFork<N> {
Ok(res)
}

/// Sends `eth_call` with a network-specific request.
pub async fn call_raw(
&self,
request: &WithOtherFields<TransactionRequest>,
block: Option<BlockNumber>,
) -> Result<Bytes, TransportError> {
self.provider()
.raw_request("eth_call".into(), (request, block.unwrap_or(BlockNumber::Latest)))
.await
}

/// Sends `eth_callMany`
pub async fn call_many(
&self,
Expand All @@ -518,17 +529,10 @@ impl<N: Network> ClientFork<N> {
/// Sends `eth_simulateV1`
pub async fn simulate_v1(
&self,
request: &SimulatePayload,
request: &SimulatePayload<WithOtherFields<TransactionRequest>>,
block: Option<BlockId>,
) -> Result<Vec<SimulatedBlock<N::BlockResponse>>, TransportError> {
let mut simulate_call = self.provider().simulate(request);
if let Some(block) = block {
simulate_call = simulate_call.block_id(block);
}

let res = simulate_call.await?;

Ok(res)
self.provider().raw_request("eth_simulateV1".into(), (request, block)).await
}

/// Sends `eth_estimateGas`
Expand All @@ -543,6 +547,19 @@ impl<N: Network> ClientFork<N> {
Ok(res as u128)
}

/// Sends `eth_estimateGas` with a network-specific request.
pub async fn estimate_gas_raw(
&self,
request: &WithOtherFields<TransactionRequest>,
block: Option<BlockNumber>,
) -> Result<u128, TransportError> {
let gas: U256 = self
.provider()
.raw_request("eth_estimateGas".into(), (request, block.unwrap_or_default()))
.await?;
Ok(gas.saturating_to())
}

/// Sends `eth_createAccessList`
pub async fn create_access_list(
&self,
Expand All @@ -552,6 +569,17 @@ impl<N: Network> ClientFork<N> {
self.provider().create_access_list(request).block_id(block.unwrap_or_default().into()).await
}

/// Sends `eth_createAccessList` with a network-specific request.
pub async fn create_access_list_raw(
&self,
request: &WithOtherFields<TransactionRequest>,
block: Option<BlockNumber>,
) -> Result<AccessListResult, TransportError> {
self.provider()
.raw_request("eth_createAccessList".into(), (request, block.unwrap_or_default()))
.await
}

pub async fn transaction_by_block_number_and_index(
&self,
number: u64,
Expand Down
9 changes: 8 additions & 1 deletion crates/anvil/src/eth/backend/mem/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,16 @@ impl AnvilInspector {
pub fn take_simulation_logs(
&mut self,
canonical_logs: &[Log],
success: bool,
) -> Option<(Vec<(u64, Log)>, u64)> {
self.simulation_logs.take().map(|mut collector| {
collector.append_remaining_canonical_logs(canonical_logs);
if success {
collector.append_remaining_canonical_logs(canonical_logs);
} else {
// A top-level revert can discard logs without producing an enclosing call frame
// callback. Preserve the attempted count for subsequent log indices.
collector.logs.clear();
}
(
collector.logs.into_iter().map(|log| (log.index, log.log)).collect(),
collector.next_index,
Expand Down
Loading
Loading