revm_handler/
mainnet_builder.rs1use crate::{instructions::EthInstructions, EthPrecompiles};
2use context::{BlockEnv, Cfg, CfgEnv, Context, Evm, Journal, TxEnv};
3use context_interface::{Block, Database, JournalTr, Transaction};
4use database_interface::EmptyDB;
5use interpreter::interpreter::EthInterpreter;
6use primitives::hardfork::SpecId;
7
8pub type MainnetEvm<CTX, INSP = ()> =
9 Evm<CTX, INSP, EthInstructions<EthInterpreter, CTX>, EthPrecompiles>;
10
11pub type MainnetContext<DB> = Context<BlockEnv, TxEnv, CfgEnv, DB, Journal<DB>, ()>;
12
13pub trait MainBuilder: Sized {
14 type Context;
15
16 fn build_mainnet(self) -> MainnetEvm<Self::Context>;
17
18 fn build_mainnet_with_inspector<INSP>(self, inspector: INSP)
19 -> MainnetEvm<Self::Context, INSP>;
20}
21
22impl<BLOCK, TX, CFG, DB, JOURNAL, CHAIN> MainBuilder for Context<BLOCK, TX, CFG, DB, JOURNAL, CHAIN>
23where
24 BLOCK: Block,
25 TX: Transaction,
26 CFG: Cfg,
27 DB: Database,
28 JOURNAL: JournalTr<Database = DB>,
29{
30 type Context = Self;
31
32 fn build_mainnet(self) -> MainnetEvm<Self::Context> {
33 Evm {
34 ctx: self,
35 inspector: (),
36 instruction: EthInstructions::default(),
37 precompiles: EthPrecompiles::default(),
38 }
39 }
40
41 fn build_mainnet_with_inspector<INSP>(
42 self,
43 inspector: INSP,
44 ) -> MainnetEvm<Self::Context, INSP> {
45 Evm {
46 ctx: self,
47 inspector,
48 instruction: EthInstructions::default(),
49 precompiles: EthPrecompiles::default(),
50 }
51 }
52}
53
54pub trait MainContext {
56 fn mainnet() -> Self;
57}
58
59impl MainContext for Context<BlockEnv, TxEnv, CfgEnv, EmptyDB, Journal<EmptyDB>, ()> {
60 fn mainnet() -> Self {
61 Context::new(EmptyDB::new(), SpecId::default())
62 }
63}
64
65#[cfg(test)]
66mod test {
67 use crate::ExecuteEvm;
68 use crate::{MainBuilder, MainContext};
69 use alloy_signer::{Either, SignerSync};
70 use alloy_signer_local::PrivateKeySigner;
71 use bytecode::{
72 opcode::{PUSH1, SSTORE},
73 Bytecode,
74 };
75 use context::{Context, TxEnv};
76 use context_interface::{transaction::Authorization, TransactionType};
77 use database::{BenchmarkDB, EEADDRESS, FFADDRESS};
78 use primitives::{hardfork::SpecId, TxKind, U256};
79 use primitives::{StorageKey, StorageValue};
80
81 #[test]
82 fn sanity_eip7702_tx() {
83 let signer = PrivateKeySigner::random();
84 let auth = Authorization {
85 chain_id: U256::ZERO,
86 nonce: 0,
87 address: FFADDRESS,
88 };
89 let signature = signer.sign_hash_sync(&auth.signature_hash()).unwrap();
90 let auth = auth.into_signed(signature);
91
92 let bytecode = Bytecode::new_legacy([PUSH1, 0x01, PUSH1, 0x01, SSTORE].into());
93
94 let ctx = Context::mainnet()
95 .modify_cfg_chained(|cfg| cfg.spec = SpecId::PRAGUE)
96 .with_db(BenchmarkDB::new_bytecode(bytecode));
97
98 let mut evm = ctx.build_mainnet();
99
100 let state = evm
101 .transact_finalize(TxEnv {
102 tx_type: TransactionType::Eip7702.into(),
103 gas_limit: 100_000,
104 authorization_list: vec![Either::Left(auth)],
105 caller: EEADDRESS,
106 kind: TxKind::Call(signer.address()),
107 ..Default::default()
108 })
109 .unwrap()
110 .state;
111
112 let auth_acc = state.get(&signer.address()).unwrap();
113 assert_eq!(auth_acc.info.code, Some(Bytecode::new_eip7702(FFADDRESS)));
114 assert_eq!(auth_acc.info.nonce, 1);
115 assert_eq!(
116 auth_acc
117 .storage
118 .get(&StorageKey::from(1))
119 .unwrap()
120 .present_value,
121 StorageValue::from(1)
122 );
123 }
124}