Skip to content

Commit

Permalink
Merge branch 'master' into ahmad-cancun-eip-6780
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadkaouk committed Jun 11, 2024
2 parents 7560c6b + 1218997 commit 2b6b4b6
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 710 deletions.
30 changes: 17 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
[workspace]
members = [
"interpreter",
"jsontests",
"precompile",
"tracer",
]
resolver = "2"

[workspace.package]
edition = "2021"
rust-version = "1.60.0"
license = "Apache-2.0"
authors = ["rust-evm Developers <[email protected]>"]
repository = "https://github.com/rust-ethereum/evm"
Expand All @@ -7,7 +18,8 @@ keywords = ["no_std", "ethereum", "evm"]
[package]
name = "evm"
version = "1.0.0-dev"
edition = "2021"
edition = { workspace = true }
rust-version = { workspace = true }
license = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
Expand All @@ -30,20 +42,12 @@ std = [
"sha3/std",
"evm-interpreter/std",
]
with-codec = [
scale = [
"primitive-types/codec",
"primitive-types/scale-info",
"evm-interpreter/with-codec",
"evm-interpreter/scale",
]
with-serde = [
serde = [
"primitive-types/impl-serde",
"evm-interpreter/with-serde",
]

[workspace]
members = [
"interpreter",
"jsontests",
"precompile",
"tracer",
"evm-interpreter/serde",
]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ interpreter that can be easily customized.
## Status

The Rust EVM project has a long history dating back to the initial
implementation in 2017 (when it was called SputnikVM). It has went through
multiple rewrites over the years to accomodate for different requirements, when
we successfully tested one integrating Geth to sync the mainnet.
implementation in 2017 (when it was called SputnikVM). It has gone through
multiple rewrites over the years to accommodate for different requirements,
when we successfully tested one integrating Geth to sync the mainnet.

The current rewrite is used in production for the Frontier project (the
Ethereum-compatibility layer for Polkadot). However, we have not yet fully
Expand Down
14 changes: 8 additions & 6 deletions interpreter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
[package]
name = "evm-interpreter"
version = "1.0.0-dev"
edition = "2021"
edition = { workspace = true }
rust-version = { workspace = true }
license = { workspace = true }
authors = { workspace = true }
repository = { workspace = true }
keywords = { workspace = true }
description = "The interpreter part of Ethereum Virtual Machine"

[dependencies]
auto_impl = "1.2"
paste = "1.0"
primitive-types = { version = "0.12", default-features = false, features = ["rlp"] }
rlp = { version = "0.5", default-features = false }
scale-codec = { package = "parity-scale-codec", version = "3.2", default-features = false, features = ["derive", "full"], optional = true }
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
sha3 = { version = "0.10", default-features = false }
auto_impl = "1.1"

[dev-dependencies]
hex = "0.4"
Expand All @@ -25,17 +27,17 @@ default = ["std"]
std = [
"primitive-types/std",
"rlp/std",
"serde/std",
"scale-codec/std",
"scale-info/std",
"serde/std",
"sha3/std",
]
with-codec = [
scale = [
"scale-codec",
"scale-info",
"primitive-types/impl-codec",
]
with-serde = [
"serde",
serde = [
"dep:serde",
"primitive-types/impl-serde",
]
68 changes: 32 additions & 36 deletions interpreter/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::Opcode;
use alloc::borrow::Cow;
use core::fmt;

/// Capture represents the result of execution.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -33,10 +34,10 @@ pub type ExitResult = Result<ExitSucceed, ExitError>;
/// Exit reason.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "with-codec",
feature = "scale",
derive(scale_codec::Encode, scale_codec::Decode, scale_info::TypeInfo)
)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ExitError {
/// Machine returns a normal EVM error.
Exception(ExitException),
Expand All @@ -54,30 +55,25 @@ impl From<ExitError> for ExitResult {
}

#[cfg(feature = "std")]
impl std::error::Error for ExitError {
fn description(&self) -> &str {
impl std::error::Error for ExitError {}

impl fmt::Display for ExitError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Exception(_) => "EVM exit exception",
Self::Reverted => "EVM internal revert",
Self::Fatal(_) => "EVM fatal error",
Self::Exception(_) => f.write_str("EVM exit exception"),
Self::Reverted => f.write_str("EVM internal revert"),
Self::Fatal(_) => f.write_str("EVM fatal error"),
}
}
}

#[cfg(feature = "std")]
impl std::fmt::Display for ExitError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}

/// Exit succeed reason.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "with-codec",
feature = "scale",
derive(scale_codec::Encode, scale_codec::Decode, scale_info::TypeInfo)
)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ExitSucceed {
/// Machine encountered an explicit stop.
Stopped,
Expand All @@ -96,67 +92,67 @@ impl From<ExitSucceed> for ExitResult {
/// Exit error reason.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "with-codec",
feature = "scale",
derive(scale_codec::Encode, scale_codec::Decode, scale_info::TypeInfo)
)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ExitException {
/// Trying to pop from an empty stack.
#[cfg_attr(feature = "with-codec", codec(index = 0))]
#[cfg_attr(feature = "scale", codec(index = 0))]
StackUnderflow,
/// Trying to push into a stack over stack limit.
#[cfg_attr(feature = "with-codec", codec(index = 1))]
#[cfg_attr(feature = "scale", codec(index = 1))]
StackOverflow,
/// Jump destination is invalid.
#[cfg_attr(feature = "with-codec", codec(index = 2))]
#[cfg_attr(feature = "scale", codec(index = 2))]
InvalidJump,
/// An opcode accesses memory region, but the region is invalid.
#[cfg_attr(feature = "with-codec", codec(index = 3))]
#[cfg_attr(feature = "scale", codec(index = 3))]
InvalidRange,
/// Encountered the designated invalid opcode.
#[cfg_attr(feature = "with-codec", codec(index = 4))]
#[cfg_attr(feature = "scale", codec(index = 4))]
DesignatedInvalid,
/// Call stack is too deep (runtime).
#[cfg_attr(feature = "with-codec", codec(index = 5))]
#[cfg_attr(feature = "scale", codec(index = 5))]
CallTooDeep,
/// Create opcode encountered collision (runtime).
#[cfg_attr(feature = "with-codec", codec(index = 6))]
#[cfg_attr(feature = "scale", codec(index = 6))]
CreateCollision,
/// Create init code exceeds limit (runtime).
#[cfg_attr(feature = "with-codec", codec(index = 7))]
#[cfg_attr(feature = "scale", codec(index = 7))]
CreateContractLimit,

/// Invalid opcode during execution or starting byte is 0xef ([EIP-3541](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3541.md)).
#[cfg_attr(feature = "with-codec", codec(index = 15))]
#[cfg_attr(feature = "scale", codec(index = 15))]
InvalidOpcode(Opcode),

/// An opcode accesses external information, but the request is off offset
/// limit (runtime).
#[cfg_attr(feature = "with-codec", codec(index = 8))]
#[cfg_attr(feature = "scale", codec(index = 8))]
OutOfOffset,
/// Execution runs out of gas (runtime).
#[cfg_attr(feature = "with-codec", codec(index = 9))]
#[cfg_attr(feature = "scale", codec(index = 9))]
OutOfGas,
/// Not enough fund to start the execution (runtime).
#[cfg_attr(feature = "with-codec", codec(index = 10))]
#[cfg_attr(feature = "scale", codec(index = 10))]
OutOfFund,

/// PC underflowed (unused).
#[allow(clippy::upper_case_acronyms)]
#[cfg_attr(feature = "with-codec", codec(index = 11))]
#[cfg_attr(feature = "scale", codec(index = 11))]
PCUnderflow,

/// Attempt to create an empty account (runtime, unused).
#[cfg_attr(feature = "with-codec", codec(index = 12))]
#[cfg_attr(feature = "scale", codec(index = 12))]
CreateEmpty,

/// Nonce reached maximum value of 2^64-1
/// https://eips.ethereum.org/EIPS/eip-2681
#[cfg_attr(feature = "with-codec", codec(index = 14))]
#[cfg_attr(feature = "scale", codec(index = 14))]
MaxNonce,

/// Other normal errors.
#[cfg_attr(feature = "with-codec", codec(index = 13))]
#[cfg_attr(feature = "scale", codec(index = 13))]
Other(Cow<'static, str>),
}

Expand All @@ -175,10 +171,10 @@ impl From<ExitException> for ExitError {
/// Exit fatal reason.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "with-codec",
feature = "scale",
derive(scale_codec::Encode, scale_codec::Decode, scale_info::TypeInfo)
)]
#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ExitFatal {
/// The operation is not supported.
NotSupported,
Expand Down
33 changes: 25 additions & 8 deletions interpreter/src/etable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl<S, H, Tr> Etable<S, H, Tr> {
table[Opcode::MULMOD.as_usize()] = eval_mulmod as _;
table[Opcode::EXP.as_usize()] = eval_exp as _;
table[Opcode::SIGNEXTEND.as_usize()] = eval_signextend as _;

table[Opcode::LT.as_usize()] = eval_lt as _;
table[Opcode::GT.as_usize()] = eval_gt as _;
table[Opcode::SLT.as_usize()] = eval_slt as _;
Expand All @@ -157,22 +158,27 @@ impl<S, H, Tr> Etable<S, H, Tr> {
table[Opcode::XOR.as_usize()] = eval_xor as _;
table[Opcode::NOT.as_usize()] = eval_not as _;
table[Opcode::BYTE.as_usize()] = eval_byte as _;

table[Opcode::SHL.as_usize()] = eval_shl as _;
table[Opcode::SHR.as_usize()] = eval_shr as _;
table[Opcode::SAR.as_usize()] = eval_sar as _;
table[Opcode::CODESIZE.as_usize()] = eval_codesize as _;
table[Opcode::CODECOPY.as_usize()] = eval_codecopy as _;

table[Opcode::CALLDATALOAD.as_usize()] = eval_calldataload as _;
table[Opcode::CALLDATASIZE.as_usize()] = eval_calldatasize as _;
table[Opcode::CALLDATACOPY.as_usize()] = eval_calldatacopy as _;
table[Opcode::CODESIZE.as_usize()] = eval_codesize as _;
table[Opcode::CODECOPY.as_usize()] = eval_codecopy as _;

table[Opcode::POP.as_usize()] = eval_pop as _;
table[Opcode::MLOAD.as_usize()] = eval_mload as _;
table[Opcode::MSTORE.as_usize()] = eval_mstore as _;
table[Opcode::MSTORE8.as_usize()] = eval_mstore8 as _;

table[Opcode::JUMP.as_usize()] = eval_jump as _;
table[Opcode::JUMPI.as_usize()] = eval_jumpi as _;
table[Opcode::PC.as_usize()] = eval_pc as _;
table[Opcode::MSIZE.as_usize()] = eval_msize as _;

table[Opcode::JUMPDEST.as_usize()] = eval_jumpdest as _;

table[Opcode::PUSH0.as_usize()] = eval_push0 as _;
Expand Down Expand Up @@ -244,7 +250,9 @@ impl<S, H, Tr> Etable<S, H, Tr> {
table[Opcode::SWAP16.as_usize()] = eval_swap16 as _;

table[Opcode::RETURN.as_usize()] = eval_return as _;

table[Opcode::REVERT.as_usize()] = eval_revert as _;

table[Opcode::INVALID.as_usize()] = eval_invalid as _;

Self(table, PhantomData)
Expand All @@ -261,43 +269,52 @@ where
let mut table = Self::core();

table.0[Opcode::SHA3.as_usize()] = eval_sha3 as _;

table.0[Opcode::ADDRESS.as_usize()] = eval_address as _;
table.0[Opcode::BALANCE.as_usize()] = eval_balance as _;
table.0[Opcode::SELFBALANCE.as_usize()] = eval_selfbalance as _;
table.0[Opcode::ORIGIN.as_usize()] = eval_origin as _;
table.0[Opcode::CALLER.as_usize()] = eval_caller as _;
table.0[Opcode::CALLVALUE.as_usize()] = eval_callvalue as _;

table.0[Opcode::GASPRICE.as_usize()] = eval_gasprice as _;
table.0[Opcode::EXTCODESIZE.as_usize()] = eval_extcodesize as _;
table.0[Opcode::EXTCODEHASH.as_usize()] = eval_extcodehash as _;
table.0[Opcode::EXTCODECOPY.as_usize()] = eval_extcodecopy as _;
table.0[Opcode::RETURNDATASIZE.as_usize()] = eval_returndatasize as _;
table.0[Opcode::RETURNDATACOPY.as_usize()] = eval_returndatacopy as _;
table.0[Opcode::EXTCODEHASH.as_usize()] = eval_extcodehash as _;

table.0[Opcode::BLOCKHASH.as_usize()] = eval_blockhash as _;
table.0[Opcode::COINBASE.as_usize()] = eval_coinbase as _;
table.0[Opcode::TIMESTAMP.as_usize()] = eval_timestamp as _;
table.0[Opcode::NUMBER.as_usize()] = eval_number as _;
table.0[Opcode::DIFFICULTY.as_usize()] = eval_difficulty as _;
table.0[Opcode::GASLIMIT.as_usize()] = eval_gaslimit as _;
table.0[Opcode::CHAINID.as_usize()] = eval_chainid as _;
table.0[Opcode::SELFBALANCE.as_usize()] = eval_selfbalance as _;
table.0[Opcode::BASEFEE.as_usize()] = eval_basefee as _;

table.0[Opcode::SLOAD.as_usize()] = eval_sload as _;
table.0[Opcode::SSTORE.as_usize()] = eval_sstore as _;

table.0[Opcode::GAS.as_usize()] = eval_gas as _;

table.0[Opcode::LOG0.as_usize()] = eval_log0 as _;
table.0[Opcode::LOG1.as_usize()] = eval_log1 as _;
table.0[Opcode::LOG2.as_usize()] = eval_log2 as _;
table.0[Opcode::LOG3.as_usize()] = eval_log3 as _;
table.0[Opcode::LOG4.as_usize()] = eval_log4 as _;
table.0[Opcode::SUICIDE.as_usize()] = eval_suicide as _;
table.0[Opcode::CHAINID.as_usize()] = eval_chainid as _;
table.0[Opcode::BASEFEE.as_usize()] = eval_basefee as _;

table.0[Opcode::CREATE.as_usize()] = eval_call_create_trap as _;
table.0[Opcode::CREATE2.as_usize()] = eval_call_create_trap as _;
table.0[Opcode::CALL.as_usize()] = eval_call_create_trap as _;
table.0[Opcode::CALLCODE.as_usize()] = eval_call_create_trap as _;

table.0[Opcode::DELEGATECALL.as_usize()] = eval_call_create_trap as _;
table.0[Opcode::CREATE2.as_usize()] = eval_call_create_trap as _;

table.0[Opcode::STATICCALL.as_usize()] = eval_call_create_trap as _;

table.0[Opcode::SUICIDE.as_usize()] = eval_suicide as _;

table
}
}
Expand Down
Loading

0 comments on commit 2b6b4b6

Please sign in to comment.