Skip to content

Commit

Permalink
Merge branch 'main' into reintroduce-alloy-rebased
Browse files Browse the repository at this point in the history
  • Loading branch information
Evalir committed Sep 20, 2023
2 parents 0439559 + 1f31756 commit 4dd6510
Show file tree
Hide file tree
Showing 29 changed files with 1,382 additions and 1,580 deletions.
47 changes: 35 additions & 12 deletions bins/revme/src/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ pub enum TestErrorKind {
StateRootMismatch { got: B256, expected: B256 },
#[error("Unknown private key: {0:?}")]
UnknownPrivateKey(B256),
#[error("Unexpected exception: {got_exception:?} but test expects:{expected_exception:?}")]
UnexpectedException {
expected_exception: Option<String>,
got_exception: Option<String>,
},
#[error(transparent)]
SerdeDeserialize(#[from] serde_json::Error),
}
Expand Down Expand Up @@ -94,12 +99,6 @@ fn skip_test(path: &Path) -> bool {
| "loopMul.json"
| "CALLBlake2f_MaxRounds.json"
| "shiftCombinations.json"

// TODO: These EIP-4844 all have exception specified.
| "emptyBlobhashList.json" // '>=Cancun': TR_EMPTYBLOB
| "wrongBlobhashVersion.json" // '>=Cancun': TR_BLOBVERSION_INVALID
| "createBlobhashTx.json" // '>=Cancun': TR_BLOBCREATE
| "blobhashListBounds7.json" // ">=Cancun": "TR_BLOBLIST_OVERSIZE"
) || path_str.contains("stEOF")
}

Expand Down Expand Up @@ -215,6 +214,7 @@ pub fn execute_test_suite(

for (id, test) in tests.into_iter().enumerate() {
env.tx.gas_limit = unit.transaction.gas_limit[test.indexes.gas].saturating_to();

env.tx.data = unit
.transaction
.data
Expand Down Expand Up @@ -272,11 +272,34 @@ pub fn execute_test_suite(
// validate results
// this is in a closure so we can have a common printing routine for errors
let check = || {
let logs = match &exec_result {
Ok(ExecutionResult::Success { logs, .. }) => logs.clone(),
_ => Vec::new(),
};
let logs_root = log_rlp_hash(&logs);
// if we expect exception revm should return error from execution.
// So we do not check logs and state root.
//
// Note that some tests that have exception and run tests from before state clear
// would touch the caller account and make it appear in state root calculation.
// This is not something that we would expect as invalid tx should not touch state.
// but as this is a cleanup of invalid tx it is not properly defined and in the end
// it does not matter.
// Test where this happens: `tests/GeneralStateTests/stTransactionTest/NoSrcAccountCreate.json`
// and you can check that we have only two "hash" values for before and after state clear.
match (&test.expect_exception, &exec_result) {
// do nothing
(None, Ok(_)) => (),
// return okay, exception is expected.
(Some(_), Err(_)) => return Ok(()),
_ => {
return Err(TestError {
name: name.clone(),
kind: TestErrorKind::UnexpectedException {
expected_exception: test.expect_exception.clone(),
got_exception: exec_result.clone().err().map(|e| e.to_string()),
},
});
}
}

let logs_root =
log_rlp_hash(exec_result.as_ref().map(|r| r.logs()).unwrap_or_default());

if logs_root != test.logs {
return Err(TestError {
Expand Down Expand Up @@ -325,7 +348,7 @@ pub fn execute_test_suite(
evm.database(&mut state);

let path = path.display();
println!("Test {name:?} (id: {id}, path: {path}) failed:\n{e}");
println!("Test {name:?} (index: {index}, path: {path}) failed:\n{e}");

println!("\nTraces:");
let _ = evm.inspect_commit(TracerEip3155::new(Box::new(stdout()), false, false));
Expand Down
8 changes: 4 additions & 4 deletions crates/interpreter/src/instruction_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub enum InstructionResult {

// error codes
OutOfGas = 0x50,
MemoryOOG = 0x51,
MemoryLimitOOG = 0x52,
PrecompileOOG = 0x53,
InvalidOperandOOG = 0x54,
MemoryOOG,
MemoryLimitOOG,
PrecompileOOG,
InvalidOperandOOG,
OpcodeNotFound,
CallNotAllowedInsideStatic,
StateChangeDuringStaticCall,
Expand Down
194 changes: 12 additions & 182 deletions crates/interpreter/src/instructions.rs
Original file line number Diff line number Diff line change
@@ -1,185 +1,15 @@
#[macro_use]
mod macros;

mod arithmetic;
mod bitwise;
mod control;
mod host;
mod host_env;
mod i256;
mod memory;
pub mod macros;

pub mod arithmetic;
pub mod bitwise;
pub mod control;
pub mod host;
pub mod host_env;
pub mod i256;
pub mod memory;
pub mod opcode;
mod stack;
mod system;

use crate::{interpreter::Interpreter, primitives::Spec, Host};
pub use opcode::{OpCode, OPCODE_JUMPMAP};

pub use crate::{return_ok, return_revert, InstructionResult};
pub fn return_stop(interpreter: &mut Interpreter, _host: &mut dyn Host) {
interpreter.instruction_result = InstructionResult::Stop;
}
pub fn return_invalid(interpreter: &mut Interpreter, _host: &mut dyn Host) {
interpreter.instruction_result = InstructionResult::InvalidFEOpcode;
}

pub fn return_not_found(interpreter: &mut Interpreter, _host: &mut dyn Host) {
interpreter.instruction_result = InstructionResult::OpcodeNotFound;
}

#[inline(always)]
pub fn eval<H: Host, S: Spec>(opcode: u8, interp: &mut Interpreter, host: &mut H) {
match opcode {
opcode::STOP => return_stop(interp, host),
opcode::ADD => arithmetic::wrapped_add(interp, host),
opcode::MUL => arithmetic::wrapping_mul(interp, host),
opcode::SUB => arithmetic::wrapping_sub(interp, host),
opcode::DIV => arithmetic::div(interp, host),
opcode::SDIV => arithmetic::sdiv(interp, host),
opcode::MOD => arithmetic::rem(interp, host),
opcode::SMOD => arithmetic::smod(interp, host),
opcode::ADDMOD => arithmetic::addmod(interp, host),
opcode::MULMOD => arithmetic::mulmod(interp, host),
opcode::EXP => arithmetic::eval_exp::<S>(interp, host),
opcode::SIGNEXTEND => arithmetic::signextend(interp, host),
opcode::LT => bitwise::lt(interp, host),
opcode::GT => bitwise::gt(interp, host),
opcode::SLT => bitwise::slt(interp, host),
opcode::SGT => bitwise::sgt(interp, host),
opcode::EQ => bitwise::eq(interp, host),
opcode::ISZERO => bitwise::iszero(interp, host),
opcode::AND => bitwise::bitand(interp, host),
opcode::OR => bitwise::bitor(interp, host),
opcode::XOR => bitwise::bitxor(interp, host),
opcode::NOT => bitwise::not(interp, host),
opcode::BYTE => bitwise::byte(interp, host),
opcode::SHL => bitwise::shl::<S>(interp, host),
opcode::SHR => bitwise::shr::<S>(interp, host),
opcode::SAR => bitwise::sar::<S>(interp, host),
opcode::KECCAK256 => system::calculate_keccak256(interp, host),
opcode::ADDRESS => system::address(interp, host),
opcode::BALANCE => host::balance::<S>(interp, host),
opcode::SELFBALANCE => host::selfbalance::<S>(interp, host),
opcode::BLOBHASH => host_env::blob_hash::<S>(interp, host),
opcode::CODESIZE => system::codesize(interp, host),
opcode::CODECOPY => system::codecopy(interp, host),
opcode::CALLDATALOAD => system::calldataload(interp, host),
opcode::CALLDATASIZE => system::calldatasize(interp, host),
opcode::CALLDATACOPY => system::calldatacopy(interp, host),
opcode::POP => stack::pop(interp, host),
opcode::MLOAD => memory::mload(interp, host),
opcode::MSTORE => memory::mstore(interp, host),
opcode::MSTORE8 => memory::mstore8(interp, host),
opcode::JUMP => control::jump(interp, host),
opcode::JUMPI => control::jumpi(interp, host),
opcode::PC => control::pc(interp, host),
opcode::MSIZE => memory::msize(interp, host),
opcode::JUMPDEST => control::jumpdest(interp, host),
opcode::PUSH0 => stack::push0::<S>(interp, host),
opcode::PUSH1 => stack::push::<1>(interp, host),
opcode::PUSH2 => stack::push::<2>(interp, host),
opcode::PUSH3 => stack::push::<3>(interp, host),
opcode::PUSH4 => stack::push::<4>(interp, host),
opcode::PUSH5 => stack::push::<5>(interp, host),
opcode::PUSH6 => stack::push::<6>(interp, host),
opcode::PUSH7 => stack::push::<7>(interp, host),
opcode::PUSH8 => stack::push::<8>(interp, host),
opcode::PUSH9 => stack::push::<9>(interp, host),
opcode::PUSH10 => stack::push::<10>(interp, host),
opcode::PUSH11 => stack::push::<11>(interp, host),
opcode::PUSH12 => stack::push::<12>(interp, host),
opcode::PUSH13 => stack::push::<13>(interp, host),
opcode::PUSH14 => stack::push::<14>(interp, host),
opcode::PUSH15 => stack::push::<15>(interp, host),
opcode::PUSH16 => stack::push::<16>(interp, host),
opcode::PUSH17 => stack::push::<17>(interp, host),
opcode::PUSH18 => stack::push::<18>(interp, host),
opcode::PUSH19 => stack::push::<19>(interp, host),
opcode::PUSH20 => stack::push::<20>(interp, host),
opcode::PUSH21 => stack::push::<21>(interp, host),
opcode::PUSH22 => stack::push::<22>(interp, host),
opcode::PUSH23 => stack::push::<23>(interp, host),
opcode::PUSH24 => stack::push::<24>(interp, host),
opcode::PUSH25 => stack::push::<25>(interp, host),
opcode::PUSH26 => stack::push::<26>(interp, host),
opcode::PUSH27 => stack::push::<27>(interp, host),
opcode::PUSH28 => stack::push::<28>(interp, host),
opcode::PUSH29 => stack::push::<29>(interp, host),
opcode::PUSH30 => stack::push::<30>(interp, host),
opcode::PUSH31 => stack::push::<31>(interp, host),
opcode::PUSH32 => stack::push::<32>(interp, host),
opcode::DUP1 => stack::dup::<1>(interp, host),
opcode::DUP2 => stack::dup::<2>(interp, host),
opcode::DUP3 => stack::dup::<3>(interp, host),
opcode::DUP4 => stack::dup::<4>(interp, host),
opcode::DUP5 => stack::dup::<5>(interp, host),
opcode::DUP6 => stack::dup::<6>(interp, host),
opcode::DUP7 => stack::dup::<7>(interp, host),
opcode::DUP8 => stack::dup::<8>(interp, host),
opcode::DUP9 => stack::dup::<9>(interp, host),
opcode::DUP10 => stack::dup::<10>(interp, host),
opcode::DUP11 => stack::dup::<11>(interp, host),
opcode::DUP12 => stack::dup::<12>(interp, host),
opcode::DUP13 => stack::dup::<13>(interp, host),
opcode::DUP14 => stack::dup::<14>(interp, host),
opcode::DUP15 => stack::dup::<15>(interp, host),
opcode::DUP16 => stack::dup::<16>(interp, host),

opcode::SWAP1 => stack::swap::<1>(interp, host),
opcode::SWAP2 => stack::swap::<2>(interp, host),
opcode::SWAP3 => stack::swap::<3>(interp, host),
opcode::SWAP4 => stack::swap::<4>(interp, host),
opcode::SWAP5 => stack::swap::<5>(interp, host),
opcode::SWAP6 => stack::swap::<6>(interp, host),
opcode::SWAP7 => stack::swap::<7>(interp, host),
opcode::SWAP8 => stack::swap::<8>(interp, host),
opcode::SWAP9 => stack::swap::<9>(interp, host),
opcode::SWAP10 => stack::swap::<10>(interp, host),
opcode::SWAP11 => stack::swap::<11>(interp, host),
opcode::SWAP12 => stack::swap::<12>(interp, host),
opcode::SWAP13 => stack::swap::<13>(interp, host),
opcode::SWAP14 => stack::swap::<14>(interp, host),
opcode::SWAP15 => stack::swap::<15>(interp, host),
opcode::SWAP16 => stack::swap::<16>(interp, host),
pub mod stack;
pub mod system;

opcode::RETURN => control::ret(interp, host),
opcode::REVERT => control::revert::<S>(interp, host),
opcode::INVALID => return_invalid(interp, host),
opcode::BASEFEE => host_env::basefee::<S>(interp, host),
opcode::ORIGIN => host_env::origin(interp, host),
opcode::CALLER => system::caller(interp, host),
opcode::CALLVALUE => system::callvalue(interp, host),
opcode::GASPRICE => host_env::gasprice(interp, host),
opcode::EXTCODESIZE => host::extcodesize::<S>(interp, host),
opcode::EXTCODEHASH => host::extcodehash::<S>(interp, host),
opcode::EXTCODECOPY => host::extcodecopy::<S>(interp, host),
opcode::RETURNDATASIZE => system::returndatasize::<S>(interp, host),
opcode::RETURNDATACOPY => system::returndatacopy::<S>(interp, host),
opcode::BLOCKHASH => host::blockhash(interp, host),
opcode::COINBASE => host_env::coinbase(interp, host),
opcode::TIMESTAMP => host_env::timestamp(interp, host),
opcode::NUMBER => host_env::number(interp, host),
opcode::DIFFICULTY => host_env::difficulty::<H, S>(interp, host),
opcode::GASLIMIT => host_env::gaslimit(interp, host),
opcode::SLOAD => host::sload::<S>(interp, host),
opcode::SSTORE => host::sstore::<S>(interp, host),
opcode::TSTORE => host::tstore::<H, S>(interp, host),
opcode::TLOAD => host::tload::<H, S>(interp, host),
opcode::GAS => system::gas(interp, host),
opcode::LOG0 => host::log::<0>(interp, host),
opcode::LOG1 => host::log::<1>(interp, host),
opcode::LOG2 => host::log::<2>(interp, host),
opcode::LOG3 => host::log::<3>(interp, host),
opcode::LOG4 => host::log::<4>(interp, host),
opcode::SELFDESTRUCT => host::selfdestruct::<S>(interp, host),
opcode::CREATE => host::create::<false, S>(interp, host), //check
opcode::CREATE2 => host::create::<true, S>(interp, host), //check
opcode::CALL => host::call::<S>(interp, host), //check
opcode::CALLCODE => host::call_code::<S>(interp, host), //check
opcode::DELEGATECALL => host::delegate_call::<S>(interp, host), //check
opcode::STATICCALL => host::static_call::<S>(interp, host), //check
opcode::CHAINID => host_env::chainid::<S>(interp, host),
opcode::MCOPY => memory::mcopy::<S>(interp, host),
_ => return_not_found(interp, host),
}
}
pub use opcode::{Instruction, OpCode, OPCODE_JUMPMAP};
12 changes: 8 additions & 4 deletions crates/interpreter/src/instructions/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ pub fn wrapping_sub(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn div(interpreter: &mut Interpreter, _host: &mut dyn Host) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
*op2 = op1.checked_div(*op2).unwrap_or_default()
if *op2 != U256::ZERO {
*op2 = op1.wrapping_div(*op2);
}
}

pub fn sdiv(interpreter: &mut Interpreter, _host: &mut dyn Host) {
Expand All @@ -38,15 +40,17 @@ pub fn sdiv(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn rem(interpreter: &mut Interpreter, _host: &mut dyn Host) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
*op2 = op1.checked_rem(*op2).unwrap_or_default()
if *op2 != U256::ZERO {
*op2 = op1.wrapping_rem(*op2);
}
}

pub fn smod(interpreter: &mut Interpreter, _host: &mut dyn Host) {
gas!(interpreter, gas::LOW);
pop_top!(interpreter, op1, op2);
if *op2 != U256::ZERO {
*op2 = i256_mod(op1, *op2)
};
}
}

pub fn addmod(interpreter: &mut Interpreter, _host: &mut dyn Host) {
Expand All @@ -61,7 +65,7 @@ pub fn mulmod(interpreter: &mut Interpreter, _host: &mut dyn Host) {
*op3 = op1.mul_mod(op2, *op3)
}

pub fn eval_exp<SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pub fn exp<SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut dyn Host) {
pop_top!(interpreter, op1, op2);
gas_or_fail!(interpreter, gas::exp_cost::<SPEC>(*op2));
*op2 = op1.pow(*op2);
Expand Down
Loading

0 comments on commit 4dd6510

Please sign in to comment.