Skip to main content

foundry_evm_core/evm/
op.rs

1//! OP Stack on evm2, using `op-evm2`.
2
3use super::{
4    EvmEnv, FoundryEvmExt, FoundryEvmFactory, FoundryEvmNetwork, FoundryExecutionHooks,
5    FoundryTxConversion, FoundryTxResultExt, SendableExt, TxPayload, with_eip7702_delegations,
6};
7use crate::edgecov;
8use alloy_consensus::transaction::Recovered;
9use alloy_eips::Encodable2718;
10use alloy_network::AnyRpcTransaction;
11use alloy_primitives::{Address, Bytes};
12use evm2::{
13    ExecutionConfig, TxResult, Version, evm::precompile::PrecompileProvider, registry::TxRegistry,
14};
15use op_alloy_consensus::OpTxEnvelope as OpConsensusTxEnvelope;
16use op_alloy_network::Optimism;
17use op_evm2::{
18    OpEvmExt, OpEvmFactory, OpEvmTypes, OpPrecompiles, OpSpecId, OpTxEnvelope, OpTxResultExt,
19    OpTypedTx, TxDeposit, op_tx_registry, op_version,
20};
21
22/// OP Stack EVM network.
23#[derive(Clone, Copy, Debug, Default)]
24pub struct OpEvmNetwork;
25
26// SAFETY: `OpEvmExt` is `Send + Sync`, as asserted below, so clones share only thread-safe state.
27unsafe impl SendableExt for OpEvmExt {}
28
29const _: fn() = || {
30    const fn assert_send_sync<T: Send + Sync>() {}
31    assert_send_sync::<OpEvmExt>();
32};
33
34impl FoundryEvmExt<OpTxEnvelope> for OpEvmExt {
35    fn replace_while_executing(&mut self, ext: Self) {
36        // Only the block-level L1 info comes from `ext`; everything else, including the cached L1
37        // cost, describes the running transaction.
38        let tx_l1_cost = self.l1_block_info.tx_l1_cost;
39        self.l1_block_info = ext.l1_block_info;
40        self.l1_block_info.tx_l1_cost = tx_l1_cost;
41    }
42}
43
44impl FoundryTxResultExt for OpTxResultExt {
45    fn is_failed_deposit(&self) -> bool {
46        self.failed_deposit
47    }
48}
49
50impl FoundryEvmNetwork for OpEvmNetwork {
51    type Network = Optimism;
52    type EvmFactory = OpEvmFactory;
53}
54
55impl FoundryExecutionHooks for OpEvmTypes {}
56
57impl FoundryTxConversion<Optimism> for OpEvmTypes {
58    fn evm_tx(
59        envelope: OpConsensusTxEnvelope,
60        sender: Address,
61        payload: TxPayload,
62    ) -> eyre::Result<Recovered<OpTxEnvelope>> {
63        // The L1 data fee is priced over the signed encoding; simulations of unsigned requests
64        // price it at zero, like `eth_call`.
65        let enveloped = Some(if payload.is_simulated() {
66            Bytes::new()
67        } else {
68            envelope.encoded_2718().into()
69        });
70        let typed = match envelope {
71            OpConsensusTxEnvelope::Legacy(tx) => OpTypedTx::Legacy(tx.strip_signature()),
72            OpConsensusTxEnvelope::Eip2930(tx) => OpTypedTx::Eip2930(tx.strip_signature()),
73            OpConsensusTxEnvelope::Eip1559(tx) => OpTypedTx::Eip1559(tx.strip_signature()),
74            OpConsensusTxEnvelope::Eip7702(tx) => OpTypedTx::Eip7702(
75                evm2::ethereum::LazyTxEip7702::from_recovered_authorizations(tx.strip_signature()),
76            ),
77            OpConsensusTxEnvelope::Deposit(tx) => {
78                let tx = tx.into_inner();
79                return Ok(Recovered::new_unchecked(
80                    OpTxEnvelope::deposit(TxDeposit {
81                        source_hash: tx.source_hash,
82                        from: tx.from,
83                        to: tx.to,
84                        mint: tx.mint,
85                        value: tx.value,
86                        gas_limit: tx.gas_limit,
87                        is_system_transaction: tx.is_system_transaction,
88                        input: tx.input,
89                    }),
90                    sender,
91                ));
92            }
93            OpConsensusTxEnvelope::PostExec(_) => {
94                eyre::bail!("OP post-execution transactions are not supported by op-evm2")
95            }
96        };
97        Ok(Recovered::new_unchecked(OpTxEnvelope::new(typed, enveloped), sender))
98    }
99
100    fn envelope_from_any_rpc(tx: &AnyRpcTransaction) -> eyre::Result<OpConsensusTxEnvelope> {
101        OpConsensusTxEnvelope::try_from(tx.clone())
102            .map_err(|err| super::unknown_transaction_type(tx, "OpTransaction").wrap_err(err))
103    }
104}
105
106impl FoundryEvmFactory for OpEvmFactory {
107    type Types = OpEvmTypes;
108
109    fn version(&self, spec: OpSpecId, chain_id: u64) -> Version {
110        with_eip7702_delegations(op_version(spec, chain_id))
111    }
112
113    fn execution_config(&self, spec: OpSpecId, version: Version) -> ExecutionConfig<OpEvmTypes> {
114        edgecov::execution_config(spec, version)
115    }
116
117    fn tx_registry(&self, spec: OpSpecId) -> TxRegistry<OpEvmTypes, TxResult<OpEvmTypes>> {
118        op_tx_registry(spec)
119    }
120
121    fn precompiles(
122        &self,
123        env: &EvmEnv<OpEvmTypes>,
124        _ext: &OpEvmExt,
125    ) -> Box<dyn PrecompileProvider<OpEvmTypes>> {
126        Box::new(OpPrecompiles::new(env.spec))
127    }
128}