evm_mainnet/
lib.rs

1//! Mainnet convenience functions for [evm](https://docs.rs/evm).
2
3#![deny(warnings)]
4#![forbid(unused_variables, unused_imports)]
5#![warn(missing_docs)]
6#![cfg_attr(not(feature = "std"), no_std)]
7
8extern crate alloc;
9
10use evm::{
11	backend::TransactionalBackend,
12	interpreter::{
13		ExitError, etable,
14		runtime::{RuntimeBackend, RuntimeEnvironment},
15	},
16	standard::{
17		Config, EtableResolver, ExecutionEtable, GasometerEtable, Invoker, TransactArgs,
18		TransactValue,
19	},
20};
21use evm_precompile::StandardPrecompileSet;
22
23#[doc(hidden)]
24pub use evm;
25#[doc(hidden)]
26pub use evm_precompile;
27
28/// Mainnet [evm::interpreter::etable::Etable].
29pub static MAINNET_ETABLE: etable::Chained<GasometerEtable<'static>, ExecutionEtable<'static>> =
30	etable::Chained(GasometerEtable::new(), ExecutionEtable::new());
31/// PrecompileSet for mainnet.
32pub static MAINNET_PRECOMPILE_SET: StandardPrecompileSet = StandardPrecompileSet;
33/// Mainnet [EtableResolver].
34pub static MAINNET_RESOLVER: EtableResolver<
35	'static,
36	'static,
37	StandardPrecompileSet,
38	etable::Chained<GasometerEtable<'static>, ExecutionEtable<'static>>,
39> = EtableResolver::new(&MAINNET_PRECOMPILE_SET, &MAINNET_ETABLE);
40/// Mainnet [Invoker].
41pub static MAINNET_INVOKER: Invoker<
42	'static,
43	'static,
44	EtableResolver<
45		'static,
46		'static,
47		StandardPrecompileSet,
48		etable::Chained<GasometerEtable<'static>, ExecutionEtable<'static>>,
49	>,
50> = Invoker::new(&MAINNET_RESOLVER);
51
52/// Config for the Frontier hard fork.
53pub static FRONTIER_CONFIG: Config = Config::frontier();
54/// Config for the Homestead hard fork.
55pub static HOMESTEAD_CONFIG: Config = Config::homestead();
56/// Config for the TangerineWhistle hard fork.
57pub static TAGERINE_WHISTLE_CONFIG: Config = Config::tangerine_whistle();
58/// Config for the SpuriousDragon hard fork.
59pub static SPURIOUS_DRAGON_CONFIG: Config = Config::spurious_dragon();
60/// Config for the Byzantium hard fork.
61pub static BYZANTIUM_CONFIG: Config = Config::byzantium();
62/// Config for the Frontier hard fork.
63pub static PETERSBURG_CONFIG: Config = Config::petersburg();
64/// Config for the Istanbul hard fork.
65pub static ISTANBUL_CONFIG: Config = Config::istanbul();
66/// Config for the Berlin hard fork.
67pub static BERLIN_CONFIG: Config = Config::berlin();
68/// Config for the London hard fork.
69pub static LONDON_CONFIG: Config = Config::london();
70/// Config for the Shanghai hard fork.
71pub static SHANGHAI_CONFIG: Config = Config::shanghai();
72/// Config for the Cancun hard fork.
73pub static CANCUN_CONFIG: Config = Config::cancun();
74
75const TRANSACT_MAINNET_HEAP_DEPTH: Option<usize> = Some(4);
76/// Same as [transact], but use all `'static` lifetime to avoid a few stack allocations.
77pub fn transact_static<H>(
78	args: TransactArgs<'static>,
79	backend: &mut H,
80) -> Result<TransactValue, ExitError>
81where
82	H: TransactionalBackend + RuntimeEnvironment + RuntimeBackend,
83{
84	evm::transact(args, TRANSACT_MAINNET_HEAP_DEPTH, backend, &MAINNET_INVOKER)
85}
86
87/// Create a mainnet invoker.
88#[macro_export]
89macro_rules! with_mainnet_invoker {
90	// This can technically be written as a normal function as well, but we then must explicitly write the complex type.
91	( |$invoker:ident| $body:expr ) => {{
92		let precompiles = $crate::evm_precompile::StandardPrecompileSet;
93		let gas_etable =
94			$crate::evm::interpreter::etable::Single::new($crate::evm::standard::eval_gasometer);
95		let exec_etable = $crate::evm::standard::DispatchEtable::runtime();
96		let etable = $crate::evm::interpreter::etable::Chained(gas_etable, exec_etable);
97		let resolver = $crate::evm::standard::EtableResolver::new(&precompiles, &etable);
98		let $invoker = $crate::evm::standard::Invoker::new(&resolver);
99		$body
100	}};
101}
102
103/// Invoke a transaction on mainnet.
104pub fn transact<'config, H>(
105	args: TransactArgs<'config>,
106	backend: &mut H,
107) -> Result<TransactValue, ExitError>
108where
109	H: TransactionalBackend + RuntimeEnvironment + RuntimeBackend,
110{
111	with_mainnet_invoker!(|invoker| {
112		evm::transact(args, TRANSACT_MAINNET_HEAP_DEPTH, backend, &invoker)
113	})
114}