Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CANCUN hard fork impelemtation #39

Merged
merged 22 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "evm-tests/jsontests/res/ethtests"]
path = evm-tests/jsontests/res/ethtests
url = https://github.com/ethereum/tests
tag = v12.3
tag = v13.2
69 changes: 35 additions & 34 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,52 +35,53 @@ name = "loop"
harness = false

[features]
default = ["std"]
default = ["std", "force-debug"]
std = [
"ethereum/std",
"log/std",
"primitive-types/std",
"rlp/std",
"sha3/std",
"environmental/std",
"scale-codec/std",
"scale-info/std",
"serde/std",
"evm-core/std",
"evm-gasometer/std",
"evm-runtime/std",
"ethereum/std",
"log/std",
"primitive-types/std",
"rlp/std",
"sha3/std",
"environmental/std",
"scale-codec/std",
"scale-info/std",
"serde/std",
"evm-core/std",
"evm-gasometer/std",
"evm-runtime/std",
]
with-codec = [
"scale-codec",
"scale-info",
"primitive-types/codec",
"primitive-types/scale-info",
"ethereum/with-codec",
"evm-core/with-codec",
"scale-codec",
"scale-info",
"primitive-types/codec",
"primitive-types/scale-info",
"ethereum/with-codec",
"evm-core/with-codec",
]
with-serde = [
"serde",
"primitive-types/impl-serde",
"evm-core/with-serde",
"ethereum/with-serde",
"serde",
"primitive-types/impl-serde",
"evm-core/with-serde",
"ethereum/with-serde",
]
tracing = [
"environmental",
"evm-core/tracing",
"evm-gasometer/tracing",
"evm-runtime/tracing",
"environmental",
"evm-core/tracing",
"evm-gasometer/tracing",
"evm-runtime/tracing",
]
force-debug = [
"evm-core/force-debug",
"evm-gasometer/force-debug",
"evm-core/force-debug",
"evm-gasometer/force-debug",
]
create-fixed = []
print-debug = ["evm-gasometer/print-debug"]

[workspace]
members = [
"core",
"gasometer",
"runtime",
"fuzzer",
"evm-tests/jsontests"
"core",
"gasometer",
"runtime",
"fuzzer",
"evm-tests/jsontests"
]
13 changes: 8 additions & 5 deletions benches/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn run_loop_contract() {

let vicinity = MemoryVicinity {
gas_price: U256::zero(),
effective_gas_price: U256::zero(),
origin: H160::default(),
block_hashes: Vec::new(),
block_number: Default::default(),
Expand All @@ -20,17 +21,19 @@ fn run_loop_contract() {
chain_id: U256::one(),
block_base_fee_per_gas: U256::zero(),
block_randomness: None,
blob_gas_price: None,
blob_hashes: Vec::new(),
};

let mut state = BTreeMap::new();
state.insert(
H160::from_str("0x1000000000000000000000000000000000000000").unwrap(),
MemoryAccount {
nonce: U256::one(),
balance: U256::from(10000000),
storage: BTreeMap::new(),
code: hex::decode("6080604052348015600f57600080fd5b506004361060285760003560e01c80630f14a40614602d575b600080fd5b605660048036036020811015604157600080fd5b8101908080359060200190929190505050606c565b6040518082815260200191505060405180910390f35b6000806000905060005b83811015608f5760018201915080806001019150506076565b508091505091905056fea26469706673582212202bc9ec597249a9700278fe4ce78da83273cb236e76d4d6797b441454784f901d64736f6c63430007040033").unwrap(),
}
nonce: U256::one(),
balance: U256::from(10000000),
storage: BTreeMap::new(),
code: hex::decode("6080604052348015600f57600080fd5b506004361060285760003560e01c80630f14a40614602d575b600080fd5b605660048036036020811015604157600080fd5b8101908080359060200190929190505050606c565b6040518082815260200191505060405180910390f35b6000806000905060005b83811015608f5760018201915080806001019150506076565b508091505091905056fea26469706673582212202bc9ec597249a9700278fe4ce78da83273cb236e76d4d6797b441454784f901d64736f6c63430007040033").unwrap(),
},
);
state.insert(
H160::from_str("0xf000000000000000000000000000000000000000").unwrap(),
Expand Down
4 changes: 2 additions & 2 deletions core/src/eval/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ macro_rules! op3_u256_fn {

macro_rules! as_usize_or_fail {
( $v:expr ) => {{
if $v > U256::from(usize::MAX) {
if $v > crate::utils::USIZE_MAX {
return Control::Exit(ExitFatal::NotSupported.into());
}

$v.as_usize()
}};

( $v:expr, $reason:expr ) => {{
if $v > U256::from(usize::MAX) {
if $v > crate::utils::USIZE_MAX {
return Control::Exit($reason.into());
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod external;
mod memory;
mod opcode;
mod stack;
mod utils;
pub mod utils;
mod valids;

pub use crate::error::{Capture, ExitError, ExitFatal, ExitReason, ExitRevert, ExitSucceed, Trap};
Expand Down
32 changes: 32 additions & 0 deletions core/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,38 @@ impl Memory {
Ok(())
}

/// Copy memory region form `src` to `dst` with length.
/// `copy_within` uses `memmove` to avoid DoS attacks.
pub fn copy(
&mut self,
src_offset: usize,
dst_offset: usize,
length: usize,
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<(), ExitFatal> {
// If length is zero - do nothing
if length == 0 {
return Ok(());
}

// Get maximum offset
let offset = core::cmp::max(src_offset, dst_offset);
let offset_length = offset
.checked_add(length)
.ok_or_else(|| ExitFatal::Other(Cow::from("OverflowOnCopy")))?;
if offset_length > self.limit {
return Err(ExitFatal::Other(Cow::from("OutOfGasOnCopy")));
}

// Resize data memory
if self.data.len() < offset_length {
self.data.resize(offset_length, 0);
}

self.data
.copy_within(src_offset..src_offset + length, dst_offset);
Ok(())
}

/// Copy `data` into the memory, of given `len`.
pub fn copy_large(
&mut self,
Expand Down
172 changes: 172 additions & 0 deletions core/src/opcode.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt::{Display, Formatter};

/// Opcode enum. One-to-one corresponding to an `u8` value.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(
Expand Down Expand Up @@ -95,6 +97,13 @@ impl Opcode {
/// `JUMPDEST`
pub const JUMPDEST: Opcode = Opcode(0x5b);

/// `TLOAD` - EIP-1153
pub const TLOAD: Opcode = Opcode(0x5c);
/// `TSTORE` - EIP-1153
pub const TSTORE: Opcode = Opcode(0x5d);
/// `MCOPY` - EIP-5656
pub const MCOPY: Opcode = Opcode(0x5e);

/// `PUSHn`
pub const PUSH0: Opcode = Opcode(0x5f);
pub const PUSH1: Opcode = Opcode(0x60);
Expand Down Expand Up @@ -191,6 +200,10 @@ impl Opcode {
pub const SELFBALANCE: Opcode = Opcode(0x47);
/// `BASEFEE`
pub const BASEFEE: Opcode = Opcode(0x48);
/// `BLOBHASH` - EIP-4844
pub const BLOBHASH: Opcode = Opcode(0x49);
/// `BLOBBASEFEE` - EIP-7516
pub const BLOBBASEFEE: Opcode = Opcode(0x4a);
/// `ORIGIN`
pub const ORIGIN: Opcode = Opcode(0x32);
/// `CALLER`
Expand Down Expand Up @@ -275,3 +288,162 @@ impl Opcode {
self.0 as usize
}
}

impl Display for Opcode {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
let name = match *self {
Self::STOP => "STOP",
Self::ADD => "ADD",
Self::MUL => "MUL",
Self::SUB => "SUB",
Self::DIV => "DIV",
Self::SDIV => "SDIV",
Self::MOD => "MOD",
Self::SMOD => "SMOD",
Self::ADDMOD => "ADDMOD",
Self::MULMOD => "MULMOD",
Self::EXP => "EXP",
Self::SIGNEXTEND => "SIGNEXTEND",
Self::LT => "LT",
Self::GT => "GT",
Self::SLT => "SLT",
Self::SGT => "SGT",
Self::EQ => "EQ",
Self::ISZERO => "ISZERO",
Self::AND => "AND",
Self::OR => "OR",
Self::XOR => "XOR",
Self::NOT => "NOT",
Self::BYTE => "BYTE",
Self::CALLDATALOAD => "CALLDATALOAD",
Self::CALLDATASIZE => "CALLDATASIZE",
Self::CALLDATACOPY => "CALLDATACOPY",
Self::CODESIZE => "CODESIZE",
Self::CODECOPY => "CODECOPY",
Self::SHL => "SHL",
Self::SHR => "SHR",
Self::SAR => "SAR",
Self::POP => "POP",
Self::MLOAD => "MLOAD",
Self::MSTORE => "MSTORE",
Self::MSTORE8 => "MSTORE8",
Self::JUMP => "JUMP",
Self::JUMPI => "JUMPI",
Self::PC => "PC",
Self::MSIZE => "MSIZE",
Self::JUMPDEST => "JUMPDEST",
Self::TLOAD => "TLOAD",
Self::TSTORE => "TSTORE",
Self::MCOPY => "MCOPY",
Self::PUSH0 => "PUSH0",
Self::PUSH1 => "PUSH1",
Self::PUSH2 => "PUSH2",
Self::PUSH3 => "PUSH3",
Self::PUSH4 => "PUSH4",
Self::PUSH5 => "PUSH5",
Self::PUSH6 => "PUSH6",
Self::PUSH7 => "PUSH7",
Self::PUSH8 => "PUSH8",
Self::PUSH9 => "PUSH9",
Self::PUSH10 => "PUSH10",
Self::PUSH11 => "PUSH11",
Self::PUSH12 => "PUSH12",
Self::PUSH13 => "PUSH13",
Self::PUSH14 => "PUSH14",
Self::PUSH15 => "PUSH15",
Self::PUSH16 => "PUSH16",
Self::PUSH17 => "PUSH17",
Self::PUSH18 => "PUSH18",
Self::PUSH19 => "PUSH19",
Self::PUSH20 => "PUSH20",
Self::PUSH21 => "PUSH21",
Self::PUSH22 => "PUSH22",
Self::PUSH23 => "PUSH23",
Self::PUSH24 => "PUSH24",
Self::PUSH25 => "PUSH25",
Self::PUSH26 => "PUSH26",
Self::PUSH27 => "PUSH27",
Self::PUSH28 => "PUSH28",
Self::PUSH29 => "PUSH29",
Self::PUSH30 => "PUSH30",
Self::PUSH31 => "PUSH31",
Self::PUSH32 => "PUSH32",
Self::DUP1 => "DUP1",
Self::DUP2 => "DUP2",
Self::DUP3 => "DUP3",
Self::DUP4 => "DUP4",
Self::DUP5 => "DUP5",
Self::DUP6 => "DUP6",
Self::DUP7 => "DUP7",
Self::DUP8 => "DUP8",
Self::DUP9 => "DUP9",
Self::DUP10 => "DUP10",
Self::DUP11 => "DUP11",
Self::DUP12 => "DUP12",
Self::DUP13 => "DUP13",
Self::DUP14 => "DUP14",
Self::DUP15 => "DUP15",
Self::DUP16 => "DUP16",
Self::SWAP1 => "SWAP1",
Self::SWAP2 => "SWAP2",
Self::SWAP3 => "SWAP3",
Self::SWAP4 => "SWAP4",
Self::SWAP5 => "SWAP5",
Self::SWAP6 => "SWAP6",
Self::SWAP7 => "SWAP7",
Self::SWAP8 => "SWAP8",
Self::SWAP9 => "SWAP9",
Self::SWAP10 => "SWAP10",
Self::SWAP11 => "SWAP11",
Self::SWAP12 => "SWAP12",
Self::SWAP13 => "SWAP13",
Self::SWAP14 => "SWAP14",
Self::SWAP15 => "SWAP15",
Self::SWAP16 => "SWAP16",
Self::RETURN => "RETURN",
Self::REVERT => "REVERT",
Self::INVALID => "INVALID",
Self::EOFMAGIC => "EOFMAGIC",
Self::SHA3 => "SHA3",
Self::ADDRESS => "ADDRESS",
Self::BALANCE => "BALANCE",
Self::SELFBALANCE => "SELFBALANCE",
Self::BASEFEE => "BASEFEE",
Self::BLOBHASH => "BLOBHASH",
Self::BLOBBASEFEE => "BLOBBASEFEE",
Self::ORIGIN => "ORIGIN",
Self::CALLER => "CALLER",
Self::CALLVALUE => "CALLVALUE",
Self::GASPRICE => "GASPRICE",
Self::EXTCODESIZE => "EXTCODESIZE",
Self::EXTCODECOPY => "EXTCODECOPY",
Self::EXTCODEHASH => "EXTCODEHASH",
Self::RETURNDATASIZE => "RETURNDATASIZE",
Self::RETURNDATACOPY => "RETURNDATACOPY",
Self::BLOCKHASH => "BLOCKHASH",
Self::COINBASE => "COINBASE",
Self::TIMESTAMP => "TIMESTAMP",
Self::NUMBER => "NUMBER",
Self::DIFFICULTY => "DIFFICULTY",
Self::GASLIMIT => "GASLIMIT",
Self::SLOAD => "SLOAD",
Self::SSTORE => "SSTORE",
Self::GAS => "GAS",
Self::LOG0 => "LOG0",
Self::LOG1 => "LOG1",
Self::LOG2 => "LOG2",
Self::LOG3 => "LOG3",
Self::LOG4 => "LOG4",
Self::CREATE => "CREATE",
Self::CREATE2 => "CREATE2",
Self::CALL => "CALL",
Self::CALLCODE => "CALLCODE",
Self::DELEGATECALL => "DELEGATECALL",
Self::STATICCALL => "STATICCALL",
Self::SUICIDE => "SUICIDE",
Self::CHAINID => "CHAINID",
_ => "UNKNOWN",
};
write!(f, "{name} [{}]", self.0)
}
}
3 changes: 3 additions & 0 deletions core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use core::cmp::Ordering;
use core::ops::{Div, Rem};
use primitive_types::U256;

/// Precalculated `usize::MAX` for `U256`
pub const USIZE_MAX: U256 = U256([usize::MAX as u64, 0, 0, 0]);

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Sign {
Plus,
Expand Down
Loading