evm/lib.rs
1//! # Ethereum Virtual Machine in Rust
2//!
3//! Rust EVM is a flexible Ethereum Virtual Machine interpreter that can be
4//! easily customized.
5//!
6//! ## Basic usage
7//!
8//! If you want to simply run EVM against Ethereum mainnet, use the
9//! [evm-mainnet](https://docs.rs/evm-mainnet) crate.
10//!
11//! The entrypoint of a normal EVM execution is through the [transact] function.
12//! The [transact] function implements a hybrid (stack-based, and then
13//! heap-based) call stack.
14//!
15//! To use the [transact] function, you will need to first implement a
16//! backend. This is anything that implements
17//! [interpreter::runtime::RuntimeEnvironment], and
18//! [interpreter::runtime::RuntimeBackend] traits. You will also need to select
19//! a few other components to construct the `invoker` parameter needed for the
20//! function.
21//!
22//! * Select an [Invoker]. The invoker defines all details of the execution
23//!   environment except the external backend. [standard::Invoker] is
24//!   probably want you want if you are not extending EVM.
25//! * For the standard invoker, select a [standard::Config], which represents
26//!   different Ethereum hard forks.
27//! * Select the precompile set. You may want the `StandardPrecompileSet` in
28//!   `evm-precompile` crate.
29//! * Select a resolver. This defines how the interpreter machines are resolved
30//!   given a code address for call or an init code for create. You may want
31//!   [standard::EtableResolver], which accepts a precompile set.
32//!
33//! ## Debugging
34//!
35//! Rust EVM supports two different methods for debugging. You can either single
36//! step the execution, or you can trace the opcodes.
37//!
38//! ### Single stepping
39//!
40//! Single stepping allows you to examine the full machine internal state every
41//! time the interpreter finishes executing a single opcode. To do this, use the
42//! heap-only call stack [HeapTransact]. Parameters passed to [HeapTransact] are
43//! the same as [transact].
44//!
45//! ### Tracing
46//!
47//! The interpreter machine uses information from an
48//! [interpreter::etable::Etable] to decide how each opcode behaves. The default
49//! implementation is
50//! [interpreter::etable::DispatchEtable]. [interpreter::etable::DispatchEtable]
51//! is fully customizable and a helper function is also provided
52//! [interpreter::etable::DispatchEtable::wrap].
53//!
54//! If you also want to trace inside gasometers, simply create a wrapper struct
55//! of the gasometer you use, and pass that into the invoker.
56//!
57//! ## Customization
58//!
59//! All aspects of the interpreter can be customized individually.
60//!
61//! * New opcodes can be added or customized through [interpreter::etable::DispatchEtable].
62//! * Gas metering behavior can be customized by wrapping [standard::eval_gasometer] or
63//!   creating new ones.
64//! * Code resolution and precompiles can be customized by [standard::Resolver]. Async
65//!   precompile is also possible through the [evm-future](https://docs.rs/evm-future) crate.
66//! * Call invocation and transaction behavior can be customized via [standard::Invoker].
67
68#![deny(warnings)]
69#![forbid(unsafe_code, unused_variables)]
70#![warn(missing_docs)]
71#![cfg_attr(not(feature = "std"), no_std)]
72
73extern crate alloc;
74
75pub mod backend;
76pub mod standard;
77
78mod call_stack;
79mod gasometer;
80mod invoker;
81
82pub use evm_interpreter as interpreter;
83
84pub use crate::{
85	call_stack::{HeapTransact, transact},
86	gasometer::GasMutState,
87	invoker::{Invoker, InvokerControl},
88};
89
90/// Merge strategy of a backend substate layer or a call stack gasometer layer.
91#[derive(Clone, Debug, Copy)]
92pub enum MergeStrategy {
93	/// Fully commit the sub-layer into the parent. This happens if the sub-machine executes
94	/// successfully.
95	Commit,
96	/// Revert the state, but keep remaining gases. This happens with the `REVERT` opcode.
97	Revert,
98	/// Discard the state and gases. This happens in all situations where the machine encounters an
99	/// error.
100	Discard,
101}