From 6b2a268590f9390f1b0522ec5108a89747ba14e0 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Fri, 23 May 2025 07:44:30 +0200 Subject: [PATCH 1/6] Change Stack type to use U256 --- interpreter/src/machine/stack.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/interpreter/src/machine/stack.rs b/interpreter/src/machine/stack.rs index 8798d5897..12cdea62d 100644 --- a/interpreter/src/machine/stack.rs +++ b/interpreter/src/machine/stack.rs @@ -1,13 +1,13 @@ use alloc::vec::Vec; -use primitive_types::H256; +use primitive_types::U256; use crate::error::{ExitError, ExitException}; /// EVM stack. #[derive(Clone, Debug)] pub struct Stack { - data: Vec, + data: Vec, limit: usize, } @@ -27,8 +27,8 @@ macro_rules! impl_perform_popn_pushn { #[allow(unused_parens)] pub fn $name(&mut self, f: F) -> Result where F: FnOnce( - $(impl_perform_popn_pushn!(INTERNAL_TYPE_RH256, $peek_pop)),* - ) -> Result<(($(impl_perform_popn_pushn!(INTERNAL_TYPE_H256, $peek_push)),*), R), ExitError> + $(impl_perform_popn_pushn!(INTERNAL_TYPE_RU256, $peek_pop)),* + ) -> Result<(($(impl_perform_popn_pushn!(INTERNAL_TYPE_U256, $peek_push)),*), R), ExitError> { match self.check_pop_push($pop_len, $push_len) { Ok(()) => (), @@ -44,8 +44,8 @@ macro_rules! impl_perform_popn_pushn { Ok(ret) } }; - (INTERNAL_TYPE_RH256, $e:expr) => { &H256 }; - (INTERNAL_TYPE_H256, $e:expr) => { H256 }; + (INTERNAL_TYPE_RU256, $e:expr) => { &U256 }; + (INTERNAL_TYPE_U256, $e:expr) => { U256 }; } impl Stack { @@ -82,7 +82,7 @@ impl Stack { /// Stack data. #[inline] #[must_use] - pub const fn data(&self) -> &Vec { + pub const fn data(&self) -> &Vec { &self.data } @@ -94,7 +94,7 @@ impl Stack { /// Pop a value from the stack. /// If the stack is already empty, returns the `StackUnderflow` error. #[inline] - pub fn pop(&mut self) -> Result { + pub fn pop(&mut self) -> Result { self.data.pop().ok_or(ExitException::StackUnderflow) } @@ -102,7 +102,7 @@ impl Stack { /// If it exceeds the stack limit, returns `StackOverflow` error and /// leaves the stack unchanged. #[inline] - pub fn push(&mut self, value: H256) -> Result<(), ExitException> { + pub fn push(&mut self, value: U256) -> Result<(), ExitException> { if self.data.len() + 1 > self.limit { return Err(ExitException::StackOverflow); } @@ -121,11 +121,11 @@ impl Stack { Ok(()) } - fn unchecked_peek(&self, no_from_top: usize) -> &H256 { + fn unchecked_peek(&self, no_from_top: usize) -> &U256 { &self.data[self.data.len() - no_from_top - 1] } - fn unchecked_pop_push1(&mut self, pop: usize, p1: H256) { + fn unchecked_pop_push1(&mut self, pop: usize, p1: U256) { for _ in 0..pop { self.data.pop(); } @@ -142,7 +142,7 @@ impl Stack { /// the stack is at index `0`. If the index is too large, /// `StackError::Underflow` is returned. #[inline] - pub fn peek(&self, no_from_top: usize) -> Result { + pub fn peek(&self, no_from_top: usize) -> Result { if self.data.len() > no_from_top { Ok(self.data[self.data.len() - no_from_top - 1]) } else { @@ -154,7 +154,7 @@ impl Stack { /// stack is at index `0`. If the index is too large, /// `StackError::Underflow` is returned. #[inline] - pub fn set(&mut self, no_from_top: usize, val: H256) -> Result<(), ExitException> { + pub fn set(&mut self, no_from_top: usize, val: U256) -> Result<(), ExitException> { if self.data.len() > no_from_top { let len = self.data.len(); self.data[len - no_from_top - 1] = val; From 7b69eea3147aec94d79ed015f2dc726c78395a6c Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Fri, 23 May 2025 07:55:17 +0200 Subject: [PATCH 2/6] Bump rust version in rust-blockchain.toml --- rust-toolchain.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 9465ad536..770a47c91 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "1.75.0" +channel = "1.87.0" profile = "minimal" components = [ "rustfmt", "clippy" ] From 7d6cb51ff98285844afbd7a046c4fdc685bc46d6 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Fri, 23 May 2025 08:29:57 +0200 Subject: [PATCH 3/6] Finish change everything in interpreter --- interpreter/src/error/trap.rs | 100 ++++++++++++++++----------------- interpreter/src/eval/macros.rs | 18 +++--- interpreter/src/eval/misc.rs | 32 +++++------ interpreter/src/eval/mod.rs | 2 +- interpreter/src/eval/system.rs | 48 ++++++++-------- 5 files changed, 100 insertions(+), 100 deletions(-) diff --git a/interpreter/src/error/trap.rs b/interpreter/src/error/trap.rs index 66764b3fb..cd1f77400 100644 --- a/interpreter/src/error/trap.rs +++ b/interpreter/src/error/trap.rs @@ -14,7 +14,7 @@ use crate::{ interpreter::Interpreter, machine::{Machine, Memory}, runtime::{Context, RuntimeBackend, RuntimeState, Transfer}, - utils::{h256_to_u256, u256_to_usize}, + utils::{h256_to_u256, u256_to_h256, u256_to_usize}, }; pub trait TrapConstruct { @@ -132,20 +132,15 @@ impl CallTrapData { scheme: CallScheme, memory: &mut Memory, state: &mut S, - gas: &H256, - to: &H256, - value: Option<&H256>, - in_offset: &H256, - in_len: &H256, - out_offset: &H256, - out_len: &H256, + gas: U256, + to: H256, + value: Option, + in_offset: U256, + in_len: U256, + out_offset: U256, + out_len: U256, ) -> Result<((), Self), ExitError> { - let gas = h256_to_u256(*gas); - let value = value.map_or(U256::zero(), |v| h256_to_u256(*v)); - let in_offset = h256_to_u256(*in_offset); - let in_len = h256_to_u256(*in_len); - let out_offset = h256_to_u256(*out_offset); - let out_len = h256_to_u256(*out_len); + let value = value.unwrap_or(U256::zero()); let in_end = in_offset .checked_add(in_len) @@ -168,7 +163,7 @@ impl CallTrapData { let context = match scheme { CallScheme::Call | CallScheme::StaticCall => Context { - address: (*to).into(), + address: to.into(), caller: state.as_ref().context.address, apparent_value: value, }, @@ -187,7 +182,7 @@ impl CallTrapData { let transfer = if scheme == CallScheme::Call { Some(Transfer { source: state.as_ref().context.address, - target: (*to).into(), + target: to.into(), value, }) } else if scheme == CallScheme::CallCode { @@ -205,7 +200,7 @@ impl CallTrapData { Ok(( (), Self { - target: (*to).into(), + target: to.into(), transfer, input, gas, @@ -232,21 +227,29 @@ impl CallTrapData { scheme, memory, state, - gas, - to, - Some(value), - in_offset, - in_len, - out_offset, - out_len, + *gas, + u256_to_h256(*to), + Some(*value), + *in_offset, + *in_len, + *out_offset, + *out_len, ) }, ), CallScheme::DelegateCall | CallScheme::StaticCall => { stack.perform_pop6_push0(|gas, to, in_offset, in_len, out_offset, out_len| { Self::new_from_params( - scheme, memory, state, gas, to, None, in_offset, in_len, out_offset, - out_len, + scheme, + memory, + state, + *gas, + u256_to_h256(*to), + None, + *in_offset, + *in_len, + *out_offset, + *out_len, ) }) } @@ -274,21 +277,19 @@ impl CallTrapData { &retbuf[..], ) { Ok(()) => { - let mut value = H256::default(); - U256::one().to_big_endian(&mut value[..]); - interpreter.machine_mut().stack.push(value)?; + interpreter.machine_mut().stack.push(U256::one())?; Ok(()) } Err(_) => { - interpreter.machine_mut().stack.push(H256::default())?; + interpreter.machine_mut().stack.push(U256::zero())?; Ok(()) } } } Err(ExitError::Reverted) => { - interpreter.machine_mut().stack.push(H256::default())?; + interpreter.machine_mut().stack.push(U256::zero())?; let _ = interpreter.machine_mut().memory.copy_large( out_offset, @@ -300,12 +301,12 @@ impl CallTrapData { Ok(()) } Err(ExitError::Exception(_)) => { - interpreter.machine_mut().stack.push(H256::default())?; + interpreter.machine_mut().stack.push(U256::zero())?; Ok(()) } Err(ExitError::Fatal(e)) => { - interpreter.machine_mut().stack.push(H256::default())?; + interpreter.machine_mut().stack.push(U256::zero())?; Err(e.into()) } @@ -399,14 +400,10 @@ impl CreateTrapData { let state = &mut machine.state; stack.perform_pop3_push0(|value, code_offset, code_len| { - let value = h256_to_u256(*value); - let code_offset = h256_to_u256(*code_offset); - let code_len = h256_to_u256(*code_len); - - let code_offset_len = if code_len == U256::zero() { + let code_offset_len = if code_len == &U256::zero() { None } else { - Some((u256_to_usize(code_offset)?, u256_to_usize(code_len)?)) + Some((u256_to_usize(*code_offset)?, u256_to_usize(*code_len)?)) }; let code = code_offset_len @@ -423,7 +420,7 @@ impl CreateTrapData { (), Self { scheme, - value, + value: *value, code, }, )) @@ -438,14 +435,10 @@ impl CreateTrapData { let state = &mut machine.state; stack.perform_pop4_push0(|value, code_offset, code_len, salt| { - let value = h256_to_u256(*value); - let code_offset = h256_to_u256(*code_offset); - let code_len = h256_to_u256(*code_len); - - let code_offset_len = if code_len == U256::zero() { + let code_offset_len = if code_len == &U256::zero() { None } else { - Some((u256_to_usize(code_offset)?, u256_to_usize(code_len)?)) + Some((u256_to_usize(*code_offset)?, u256_to_usize(*code_len)?)) }; let code = code_offset_len @@ -456,7 +449,7 @@ impl CreateTrapData { let scheme = CreateScheme::Create2 { caller: state.as_ref().context.address, - salt: *salt, + salt: u256_to_h256(*salt), code_hash, }; @@ -466,7 +459,7 @@ impl CreateTrapData { (), Self { scheme, - value, + value: *value, code, }, )) @@ -484,19 +477,22 @@ impl CreateTrapData { { let ret = match reason { Ok(address) => { - interpreter.machine_mut().stack.push(address.into())?; + interpreter + .machine_mut() + .stack + .push(h256_to_u256(address.into()))?; Ok(()) } Err(ExitError::Reverted) => { - interpreter.machine_mut().stack.push(H256::default())?; + interpreter.machine_mut().stack.push(U256::zero())?; Ok(()) } Err(ExitError::Exception(_)) => { - interpreter.machine_mut().stack.push(H256::default())?; + interpreter.machine_mut().stack.push(U256::zero())?; Ok(()) } Err(ExitError::Fatal(e)) => { - interpreter.machine_mut().stack.push(H256::default())?; + interpreter.machine_mut().stack.push(U256::zero())?; Err(e.into()) } }; diff --git a/interpreter/src/eval/macros.rs b/interpreter/src/eval/macros.rs index ded57e6db..538e36c38 100644 --- a/interpreter/src/eval/macros.rs +++ b/interpreter/src/eval/macros.rs @@ -7,11 +7,15 @@ macro_rules! try_or_fail { }; } -macro_rules! pop { +macro_rules! pop_h256 { ( $machine:expr, $( $x:ident ),* ) => ( $( let $x = match $machine.stack.pop() { - Ok(value) => value, + Ok(value) => { + let mut hvalue = H256::default(); + value.to_big_endian(&mut hvalue[..]); + hvalue + }, Err(e) => return Control::Exit(e.into()), }; )* @@ -22,17 +26,17 @@ macro_rules! pop_u256 { ( $machine:expr, $( $x:ident ),* ) => ( $( let $x = match $machine.stack.pop() { - Ok(value) => U256::from_big_endian(&value[..]), + Ok(value) => value, Err(e) => return Control::Exit(e.into()), }; )* ); } -macro_rules! push { +macro_rules! push_h256 { ( $machine:expr, $( $x:expr ),* ) => ( $( - match $machine.stack.push($x) { + match $machine.stack.push(U256::from_big_endian(H256::as_ref(&$x))) { Ok(()) => (), Err(e) => return Control::Exit(e.into()), } @@ -43,9 +47,7 @@ macro_rules! push { macro_rules! push_u256 { ( $machine:expr, $( $x:expr ),* ) => ( $( - let mut value = H256::default(); - $x.to_big_endian(&mut value[..]); - match $machine.stack.push(value) { + match $machine.stack.push($x) { Ok(()) => (), Err(e) => return Control::Exit(e.into()), } diff --git a/interpreter/src/eval/misc.rs b/interpreter/src/eval/misc.rs index 0fd3f4ad3..05202ea72 100644 --- a/interpreter/src/eval/misc.rs +++ b/interpreter/src/eval/misc.rs @@ -6,7 +6,6 @@ use crate::{ error::{ExitError, ExitException, ExitFatal, ExitSucceed}, etable::Control, machine::Machine, - utils::u256_to_h256, }; #[inline] @@ -16,7 +15,7 @@ pub fn codesize(state: &mut Machine) -> Control { match stack.perform_pop0_push1(|| { let size = U256::from(code.len()); - Ok((u256_to_h256(size), ())) + Ok((size, ())) }) { Ok(()) => Control::Continue, Err(e) => Control::Exit(Err(e)), @@ -25,15 +24,16 @@ pub fn codesize(state: &mut Machine) -> Control { #[inline] pub fn codecopy(state: &mut Machine) -> Control { - pop_u256!(state, memory_offset, code_offset, len); + let stack = &mut state.stack; + let memory = &mut state.memory; + let code = &state.code; - try_or_fail!(state.memory.resize_offset(memory_offset, len)); - match state - .memory - .copy_large(memory_offset, code_offset, len, &state.code) - { + match stack.perform_pop3_push0(|memory_offset, code_offset, len| { + memory.copy_large(*memory_offset, *code_offset, *len, &code)?; + Ok(((), ())) + }) { Ok(()) => Control::Continue, - Err(e) => Control::Exit(e.into()), + Err(e) => Control::Exit(Err(e)), } } @@ -54,7 +54,7 @@ pub fn calldataload(state: &mut Machine) -> Control { } } - push!(state, H256::from(load)); + push_h256!(state, H256::from(load)); Control::Continue } @@ -85,7 +85,7 @@ pub fn calldatacopy(state: &mut Machine) -> Control { #[inline] pub fn pop(state: &mut Machine) -> Control { - pop!(state, _val); + pop_h256!(state, _val); Control::Continue } @@ -95,7 +95,7 @@ pub fn mload(state: &mut Machine) -> Control { try_or_fail!(state.memory.resize_offset(index, U256::from(32))); let index = as_usize_or_fail!(index); let value = H256::from_slice(&state.memory.get(index, 32)[..]); - push!(state, value); + push_h256!(state, value); Control::Continue } @@ -119,7 +119,7 @@ pub fn mcopy(state: &mut Machine) -> Control { #[inline] pub fn mstore(state: &mut Machine) -> Control { pop_u256!(state, index); - pop!(state, value); + pop_h256!(state, value); try_or_fail!(state.memory.resize_offset(index, U256::from(32))); let index = as_usize_or_fail!(index); match state.memory.set(index, &value[..], Some(32)) { @@ -151,7 +151,7 @@ pub fn jump(state: &mut Machine) -> Control { #[inline] pub fn jumpi(state: &mut Machine) -> Control { pop_u256!(state, dest); - pop!(state, value); + pop_h256!(state, value); if value == H256::zero() { Control::Continue @@ -181,7 +181,7 @@ pub fn push(state: &mut Machine, n: usize, position: usize) -> Control val[(32 - n)..(32 - n + slice.len())].copy_from_slice(slice); let result = H256(val); - push!(state, result); + push_h256!(state, result); Control::ContinueN(1 + n) } @@ -191,7 +191,7 @@ pub fn dup(state: &mut Machine, n: usize) -> Control { Ok(value) => value, Err(e) => return Control::Exit(e.into()), }; - push!(state, value); + push_u256!(state, value); Control::Continue } diff --git a/interpreter/src/eval/mod.rs b/interpreter/src/eval/mod.rs index 241bcf96e..5ba880368 100644 --- a/interpreter/src/eval/mod.rs +++ b/interpreter/src/eval/mod.rs @@ -7,7 +7,7 @@ mod system; use core::ops::{BitAnd, BitOr, BitXor}; -use primitive_types::{H256, U256}; +use primitive_types::U256; use crate::{ error::{CallCreateTrap, ExitException, ExitSucceed, TrapConstruct}, diff --git a/interpreter/src/eval/system.rs b/interpreter/src/eval/system.rs index a9b7ce71f..c5df36229 100644 --- a/interpreter/src/eval/system.rs +++ b/interpreter/src/eval/system.rs @@ -3,6 +3,8 @@ use alloc::vec::Vec; use primitive_types::{H256, U256}; use sha3::{Digest, Keccak256}; +use crate::utils::u256_to_h256; + use crate::{ error::{ExitException, ExitFatal, ExitSucceed}, etable::Control, @@ -24,7 +26,7 @@ pub fn sha3, Tr>(machine: &mut Machine) -> Control }; let ret = Keccak256::digest(data.as_slice()); - push!(machine, H256::from_slice(ret.as_slice())); + push_h256!(machine, H256::from_slice(ret.as_slice())); Control::Continue } @@ -40,7 +42,7 @@ pub fn chainid, H: RuntimeEnvironment + RuntimeBackend, T pub fn address, Tr>(machine: &mut Machine) -> Control { let ret = H256::from(machine.state.as_ref().context.address); - push!(machine, ret); + push_h256!(machine, ret); Control::Continue } @@ -49,7 +51,7 @@ pub fn balance, H: RuntimeEnvironment + RuntimeBackend, T machine: &mut Machine, handler: &mut H, ) -> Control { - pop!(machine, address); + pop_h256!(machine, address); push_u256!(machine, handler.balance(address.into())); Control::Continue @@ -72,14 +74,14 @@ pub fn origin, H: RuntimeEnvironment + RuntimeBackend, Tr _handler: &H, ) -> Control { let ret = H256::from(machine.state.as_ref().transaction_context.origin); - push!(machine, ret); + push_h256!(machine, ret); Control::Continue } pub fn caller, Tr>(machine: &mut Machine) -> Control { let ret = H256::from(machine.state.as_ref().context.caller); - push!(machine, ret); + push_h256!(machine, ret); Control::Continue } @@ -92,7 +94,7 @@ pub fn callvalue, Tr>(machine: &mut Machine) -> Contro .context .apparent_value .to_big_endian(&mut ret[..]); - push!(machine, ret); + push_h256!(machine, ret); Control::Continue } @@ -108,7 +110,7 @@ pub fn gasprice, H: RuntimeEnvironment + RuntimeBackend, .transaction_context .gas_price .to_big_endian(&mut ret[..]); - push!(machine, ret); + push_h256!(machine, ret); Control::Continue } @@ -119,7 +121,7 @@ pub fn basefee, H: RuntimeEnvironment + RuntimeBackend, T ) -> Control { let mut ret = H256::default(); handler.block_base_fee_per_gas().to_big_endian(&mut ret[..]); - push!(machine, ret); + push_h256!(machine, ret); Control::Continue } @@ -128,7 +130,7 @@ pub fn extcodesize, H: RuntimeEnvironment + RuntimeBacken machine: &mut Machine, handler: &mut H, ) -> Control { - pop!(machine, address); + pop_h256!(machine, address); let code_size = handler.code_size(address.into()); push_u256!(machine, code_size); @@ -139,9 +141,9 @@ pub fn extcodehash, H: RuntimeEnvironment + RuntimeBacken machine: &mut Machine, handler: &mut H, ) -> Control { - pop!(machine, address); + pop_h256!(machine, address); let code_hash = handler.code_hash(address.into()); - push!(machine, code_hash); + push_h256!(machine, code_hash); Control::Continue } @@ -150,7 +152,7 @@ pub fn extcodecopy, H: RuntimeEnvironment + RuntimeBacken machine: &mut Machine, handler: &mut H, ) -> Control { - pop!(machine, address); + pop_h256!(machine, address); pop_u256!(machine, memory_offset, code_offset, len); try_or_fail!(machine.memory.resize_offset(memory_offset, len)); @@ -199,7 +201,7 @@ pub fn blockhash, H: RuntimeEnvironment + RuntimeBackend, handler: &H, ) -> Control { pop_u256!(machine, number); - push!(machine, handler.block_hash(number)); + push_h256!(machine, handler.block_hash(number)); Control::Continue } @@ -208,7 +210,7 @@ pub fn coinbase, H: RuntimeEnvironment + RuntimeBackend, machine: &mut Machine, handler: &H, ) -> Control { - push!(machine, handler.block_coinbase().into()); + push_h256!(machine, handler.block_coinbase().into()); Control::Continue } @@ -241,7 +243,7 @@ pub fn prevrandao, H: RuntimeEnvironment + RuntimeBackend handler: &H, ) -> Control { if let Some(rand) = handler.block_randomness() { - push!(machine, rand); + push_h256!(machine, rand); Control::Continue } else { difficulty(machine, handler) @@ -260,9 +262,9 @@ pub fn sload, H: RuntimeEnvironment + RuntimeBackend, Tr> machine: &mut Machine, handler: &mut H, ) -> Control { - pop!(machine, index); + pop_h256!(machine, index); let value = handler.storage(machine.state.as_ref().context.address, index); - push!(machine, value); + push_h256!(machine, value); Control::Continue } @@ -271,7 +273,7 @@ pub fn sstore, H: RuntimeEnvironment + RuntimeBackend, Tr machine: &mut Machine, handler: &mut H, ) -> Control { - pop!(machine, index, value); + pop_h256!(machine, index, value); match handler.set_storage(machine.state.as_ref().context.address, index, value) { Ok(()) => Control::Continue, @@ -292,9 +294,9 @@ pub fn tload, H: RuntimeEnvironment + RuntimeBackend, Tr> machine: &mut Machine, handler: &mut H, ) -> Control { - pop!(machine, index); + pop_h256!(machine, index); let value = handler.transient_storage(machine.state.as_ref().context.address, index); - push!(machine, value); + push_h256!(machine, value); Control::Continue } @@ -303,7 +305,7 @@ pub fn tstore, H: RuntimeEnvironment + RuntimeBackend, Tr machine: &mut Machine, handler: &mut H, ) -> Control { - pop!(machine, index, value); + pop_h256!(machine, index, value); match handler.set_transient_storage(machine.state.as_ref().context.address, index, value) { Ok(()) => Control::Continue, Err(e) => Control::Exit(e.into()), @@ -331,7 +333,7 @@ pub fn log, H: RuntimeEnvironment + RuntimeBackend, Tr>( for _ in 0..(n as usize) { match machine.stack.pop() { Ok(value) => { - topics.push(value); + topics.push(u256_to_h256(value)); } Err(e) => return Control::Exit(e.into()), } @@ -358,7 +360,7 @@ pub fn suicide, H: RuntimeEnvironment + RuntimeBackend, T handler.transfer(Transfer { source: address, - target: (*target).into(), + target: u256_to_h256(*target).into(), value: balance, })?; From acd6bde722e52ca4aaa14f366a434422177d6dd5 Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Fri, 23 May 2025 08:41:49 +0200 Subject: [PATCH 4/6] Finish changing the top-level crate --- interpreter/src/utils.rs | 8 ++- src/standard/gasometer/mod.rs | 108 +++++++++++++++++----------------- src/standard/mod.rs | 4 +- 3 files changed, 62 insertions(+), 58 deletions(-) diff --git a/interpreter/src/utils.rs b/interpreter/src/utils.rs index 9809a2774..5fbeac8b6 100644 --- a/interpreter/src/utils.rs +++ b/interpreter/src/utils.rs @@ -5,7 +5,7 @@ use core::{ ops::{Div, Rem}, }; -use primitive_types::{H256, U256}; +use primitive_types::{H160, H256, U256}; use crate::error::{ExitError, ExitFatal}; @@ -23,6 +23,12 @@ pub fn h256_to_u256(v: H256) -> U256 { U256::from_big_endian(&v[..]) } +/// Convert [U256] into [H160] +#[must_use] +pub fn u256_to_h160(v: U256) -> H160 { + u256_to_h256(v).into() +} + /// Convert [U256] to [usize]. pub fn u256_to_usize(v: U256) -> Result { if v > U256::from(usize::MAX) { diff --git a/src/standard/gasometer/mod.rs b/src/standard/gasometer/mod.rs index 12eeea477..fa6eb5999 100644 --- a/src/standard/gasometer/mod.rs +++ b/src/standard/gasometer/mod.rs @@ -11,6 +11,7 @@ use evm_interpreter::{ machine::{Machine, Stack}, opcode::Opcode, runtime::{RuntimeBackend, RuntimeState}, + utils::{u256_to_h160, u256_to_h256}, }; use primitive_types::{H160, H256, U256}; @@ -306,7 +307,7 @@ fn dynamic_opcode_cost( Opcode::BASEFEE => GasCost::Invalid(opcode), Opcode::EXTCODESIZE => { - let target = stack.peek(0)?.into(); + let target = u256_to_h160(stack.peek(0)?); // https://eips.ethereum.org/EIPS/eip-2929 let target_is_cold = handler.is_cold(target, None); @@ -315,7 +316,7 @@ fn dynamic_opcode_cost( GasCost::ExtCodeSize { target_is_cold } } Opcode::BALANCE => { - let target = stack.peek(0)?.into(); + let target = u256_to_h160(stack.peek(0)?); // https://eips.ethereum.org/EIPS/eip-2929 let target_is_cold = handler.is_cold(target, None); @@ -326,7 +327,7 @@ fn dynamic_opcode_cost( Opcode::BLOCKHASH => GasCost::BlockHash, Opcode::EXTCODEHASH if config.has_ext_code_hash => { - let target = stack.peek(0)?.into(); + let target = u256_to_h160(stack.peek(0)?); // https://eips.ethereum.org/EIPS/eip-2929 let target_is_cold = handler.is_cold(target, None); @@ -337,37 +338,37 @@ fn dynamic_opcode_cost( Opcode::EXTCODEHASH => GasCost::Invalid(opcode), Opcode::CALLCODE => { - let target = stack.peek(1)?.into(); + let target = u256_to_h160(stack.peek(1)?); // https://eips.ethereum.org/EIPS/eip-2929 let target_is_cold = handler.is_cold(target, None); handler.mark_hot(target, None); GasCost::CallCode { - value: U256::from_big_endian(&stack.peek(2)?[..]), - gas: U256::from_big_endian(&stack.peek(0)?[..]), + value: stack.peek(2)?, + gas: stack.peek(0)?, target_is_cold, target_exists: { handler.exists(target) }, } } Opcode::STATICCALL => { - let target = stack.peek(1)?.into(); + let target = u256_to_h160(stack.peek(1)?); // https://eips.ethereum.org/EIPS/eip-2929 let target_is_cold = handler.is_cold(target, None); handler.mark_hot(target, None); GasCost::StaticCall { - gas: U256::from_big_endian(&stack.peek(0)?[..]), + gas: stack.peek(0)?, target_is_cold, target_exists: { handler.exists(target) }, } } Opcode::SHA3 => GasCost::Sha3 { - len: U256::from_big_endian(&stack.peek(1)?[..]), + len: stack.peek(1)?, }, Opcode::EXTCODECOPY => { - let target = stack.peek(0)?.into(); + let target = u256_to_h160(stack.peek(0)?); // https://eips.ethereum.org/EIPS/eip-2929 let target_is_cold = handler.is_cold(target, None); @@ -375,20 +376,20 @@ fn dynamic_opcode_cost( GasCost::ExtCodeCopy { target_is_cold, - len: U256::from_big_endian(&stack.peek(3)?[..]), + len: stack.peek(3)?, } } Opcode::MCOPY if config.eip_5656_enabled => GasCost::VeryLowCopy { - len: U256::from_big_endian(&stack.peek(2)?[..]), + len: stack.peek(2)?, }, Opcode::CALLDATACOPY | Opcode::CODECOPY => GasCost::VeryLowCopy { - len: U256::from_big_endian(&stack.peek(2)?[..]), + len: stack.peek(2)?, }, Opcode::EXP => GasCost::Exp { - power: U256::from_big_endian(&stack.peek(1)?[..]), + power: stack.peek(1)?, }, Opcode::SLOAD => { - let index = stack.peek(0)?; + let index = u256_to_h256(stack.peek(0)?); // https://eips.ethereum.org/EIPS/eip-2929 let target_is_cold = handler.is_cold(address, Some(index)); @@ -399,14 +400,14 @@ fn dynamic_opcode_cost( Opcode::TLOAD if config.eip_1153_enabled => GasCost::TLoad, Opcode::DELEGATECALL if config.has_delegate_call => { - let target = stack.peek(1)?.into(); + let target = u256_to_h160(stack.peek(1)?); // https://eips.ethereum.org/EIPS/eip-2929 let target_is_cold = handler.is_cold(target, None); handler.mark_hot(target, None); GasCost::DelegateCall { - gas: U256::from_big_endian(&stack.peek(0)?[..]), + gas: stack.peek(0)?, target_is_cold, target_exists: { handler.exists(target) }, } @@ -415,13 +416,13 @@ fn dynamic_opcode_cost( Opcode::RETURNDATASIZE if config.has_return_data => GasCost::Base, Opcode::RETURNDATACOPY if config.has_return_data => GasCost::VeryLowCopy { - len: U256::from_big_endian(&stack.peek(2)?[..]), + len: stack.peek(2)?, }, Opcode::RETURNDATASIZE | Opcode::RETURNDATACOPY => GasCost::Invalid(opcode), Opcode::SSTORE if !is_static => { - let index = stack.peek(0)?; - let value = stack.peek(1)?; + let index = u256_to_h256(stack.peek(0)?); + let value = u256_to_h256(stack.peek(1)?); // https://eips.ethereum.org/EIPS/eip-2929 let target_is_cold = handler.is_cold(address, Some(index)); @@ -437,30 +438,30 @@ fn dynamic_opcode_cost( Opcode::TSTORE if !is_static && config.eip_1153_enabled => GasCost::TStore, Opcode::LOG0 if !is_static => GasCost::Log { n: 0, - len: U256::from_big_endian(&stack.peek(1)?[..]), + len: stack.peek(1)?, }, Opcode::LOG1 if !is_static => GasCost::Log { n: 1, - len: U256::from_big_endian(&stack.peek(1)?[..]), + len: stack.peek(1)?, }, Opcode::LOG2 if !is_static => GasCost::Log { n: 2, - len: U256::from_big_endian(&stack.peek(1)?[..]), + len: stack.peek(1)?, }, Opcode::LOG3 if !is_static => GasCost::Log { n: 3, - len: U256::from_big_endian(&stack.peek(1)?[..]), + len: stack.peek(1)?, }, Opcode::LOG4 if !is_static => GasCost::Log { n: 4, - len: U256::from_big_endian(&stack.peek(1)?[..]), + len: stack.peek(1)?, }, Opcode::CREATE if !is_static => GasCost::Create, Opcode::CREATE2 if !is_static && config.has_create2 => GasCost::Create2 { - len: U256::from_big_endian(&stack.peek(2)?[..]), + len: stack.peek(2)?, }, Opcode::SUICIDE if !is_static => { - let target = stack.peek(0)?.into(); + let target = u256_to_h160(stack.peek(0)?); // https://eips.ethereum.org/EIPS/eip-2929 let target_is_cold = handler.is_cold(target, None); @@ -473,19 +474,16 @@ fn dynamic_opcode_cost( already_removed: handler.deleted(address), } } - Opcode::CALL - if !is_static - || (is_static && U256::from_big_endian(&stack.peek(2)?[..]) == U256::zero()) => - { - let target = stack.peek(1)?.into(); + Opcode::CALL if !is_static || (is_static && stack.peek(2)? == U256::zero()) => { + let target = u256_to_h160(stack.peek(1)?); // https://eips.ethereum.org/EIPS/eip-2929 let target_is_cold = handler.is_cold(target, None); handler.mark_hot(target, None); GasCost::Call { - value: U256::from_big_endian(&stack.peek(2)?[..]), - gas: U256::from_big_endian(&stack.peek(0)?[..]), + value: stack.peek(2)?, + gas: stack.peek(0)?, target_is_cold, target_exists: { handler.exists(target) }, } @@ -505,64 +503,64 @@ fn dynamic_opcode_cost( | Opcode::LOG2 | Opcode::LOG3 | Opcode::LOG4 => Some(MemoryCost { - offset: U256::from_big_endian(&stack.peek(0)?[..]), - len: U256::from_big_endian(&stack.peek(1)?[..]), + offset: stack.peek(0)?, + len: stack.peek(1)?, }), Opcode::MCOPY => { - let top0 = U256::from_big_endian(&stack.peek(0)?[..]); - let top1 = U256::from_big_endian(&stack.peek(1)?[..]); + let top0 = stack.peek(0)?; + let top1 = stack.peek(1)?; let offset = top0.max(top1); Some(MemoryCost { offset, - len: U256::from_big_endian(&stack.peek(2)?[..]), + len: stack.peek(2)?, }) } Opcode::CODECOPY | Opcode::CALLDATACOPY | Opcode::RETURNDATACOPY => Some(MemoryCost { - offset: U256::from_big_endian(&stack.peek(0)?[..]), - len: U256::from_big_endian(&stack.peek(2)?[..]), + offset: stack.peek(0)?, + len: stack.peek(2)?, }), Opcode::EXTCODECOPY => Some(MemoryCost { - offset: U256::from_big_endian(&stack.peek(1)?[..]), - len: U256::from_big_endian(&stack.peek(3)?[..]), + offset: stack.peek(1)?, + len: stack.peek(3)?, }), Opcode::MLOAD | Opcode::MSTORE => Some(MemoryCost { - offset: U256::from_big_endian(&stack.peek(0)?[..]), + offset: stack.peek(0)?, len: U256::from(32), }), Opcode::MSTORE8 => Some(MemoryCost { - offset: U256::from_big_endian(&stack.peek(0)?[..]), + offset: stack.peek(0)?, len: U256::from(1), }), Opcode::CREATE | Opcode::CREATE2 => Some(MemoryCost { - offset: U256::from_big_endian(&stack.peek(1)?[..]), - len: U256::from_big_endian(&stack.peek(2)?[..]), + offset: stack.peek(1)?, + len: stack.peek(2)?, }), Opcode::CALL | Opcode::CALLCODE => Some( MemoryCost { - offset: U256::from_big_endian(&stack.peek(3)?[..]), - len: U256::from_big_endian(&stack.peek(4)?[..]), + offset: stack.peek(3)?, + len: stack.peek(4)?, } .join(MemoryCost { - offset: U256::from_big_endian(&stack.peek(5)?[..]), - len: U256::from_big_endian(&stack.peek(6)?[..]), + offset: stack.peek(5)?, + len: stack.peek(6)?, }), ), Opcode::DELEGATECALL | Opcode::STATICCALL => Some( MemoryCost { - offset: U256::from_big_endian(&stack.peek(2)?[..]), - len: U256::from_big_endian(&stack.peek(3)?[..]), + offset: stack.peek(2)?, + len: stack.peek(3)?, } .join(MemoryCost { - offset: U256::from_big_endian(&stack.peek(4)?[..]), - len: U256::from_big_endian(&stack.peek(5)?[..]), + offset: stack.peek(4)?, + len: stack.peek(5)?, }), ), diff --git a/src/standard/mod.rs b/src/standard/mod.rs index 7b9b1a9f3..68bb904c4 100644 --- a/src/standard/mod.rs +++ b/src/standard/mod.rs @@ -21,8 +21,8 @@ pub use self::{ config::Config, gasometer::{eval as eval_gasometer, GasometerState}, invoker::{ - routines, EtableResolver, Invoker, InvokerState, PrecompileSet, Resolver, SubstackInvoke, - TransactArgs, TransactInvoke, TransactValue, + routines, EtableResolver, IntoCallCreateTrap, Invoker, InvokerState, PrecompileSet, + Resolver, SubstackInvoke, TransactArgs, TransactInvoke, TransactValue, }, }; use crate::{gasometer::GasMutState, MergeStrategy}; From f58de6c18af013b8fcad932cbab03b3ab717c13b Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Fri, 23 May 2025 08:49:35 +0200 Subject: [PATCH 5/6] Fix all clippy warnings --- Cargo.toml | 2 +- interpreter/src/error/trap.rs | 4 ++-- interpreter/src/eval/misc.rs | 2 +- interpreter/src/machine/memory.rs | 4 ++-- jsontests/src/in_memory.rs | 3 ++- src/call_stack.rs | 2 +- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 10f8b177e..e24ee2c0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ resolver = "2" [workspace.package] edition = "2021" -rust-version = "1.60.0" +rust-version = "1.70.0" license = "Apache-2.0" authors = ["rust-evm Developers "] repository = "https://github.com/rust-ethereum/evm" diff --git a/interpreter/src/error/trap.rs b/interpreter/src/error/trap.rs index cd1f77400..05959ea19 100644 --- a/interpreter/src/error/trap.rs +++ b/interpreter/src/error/trap.rs @@ -327,7 +327,7 @@ impl CallTrapData { pub fn has_value(&self) -> bool { self.transfer .as_ref() - .map_or(false, |t| t.value != U256::zero()) + .is_some_and(|t| t.value != U256::zero()) } } @@ -370,7 +370,7 @@ impl CreateScheme { let mut stream = rlp::RlpStream::new_list(2); stream.append(caller); stream.append(&nonce); - H256::from_slice(Keccak256::digest(&stream.out()).as_slice()).into() + H256::from_slice(Keccak256::digest(stream.out()).as_slice()).into() } } } diff --git a/interpreter/src/eval/misc.rs b/interpreter/src/eval/misc.rs index 05202ea72..7693ff6e1 100644 --- a/interpreter/src/eval/misc.rs +++ b/interpreter/src/eval/misc.rs @@ -29,7 +29,7 @@ pub fn codecopy(state: &mut Machine) -> Control { let code = &state.code; match stack.perform_pop3_push0(|memory_offset, code_offset, len| { - memory.copy_large(*memory_offset, *code_offset, *len, &code)?; + memory.copy_large(*memory_offset, *code_offset, *len, code)?; Ok(((), ())) }) { Ok(()) => Control::Continue, diff --git a/interpreter/src/machine/memory.rs b/interpreter/src/machine/memory.rs index 04d93ecf2..0d796ab90 100644 --- a/interpreter/src/machine/memory.rs +++ b/interpreter/src/machine/memory.rs @@ -65,8 +65,8 @@ impl Memory { self.effective_len = U256::zero(); } - /// Resize the memory, making it cover the memory region of `offset..(offset - /// + len)`, with 32 bytes as the step. If the length is zero, this function + /// Resize the memory, making it cover the memory region of `offset..(offset + len)`, + /// with 32 bytes as the step. If the length is zero, this function /// does nothing. pub fn resize_offset(&mut self, offset: U256, len: U256) -> Result<(), ExitException> { if len == U256::zero() { diff --git a/jsontests/src/in_memory.rs b/jsontests/src/in_memory.rs index 63c84ef87..3dc4b895a 100644 --- a/jsontests/src/in_memory.rs +++ b/jsontests/src/in_memory.rs @@ -29,6 +29,7 @@ pub struct InMemoryAccount { } #[derive(Clone, Debug)] +#[allow(dead_code)] pub struct InMemorySuicideInfo { pub address: H160, } @@ -143,7 +144,7 @@ impl RuntimeBaseBackend for InMemoryBackend { } fn exists(&self, address: H160) -> bool { - self.state.get(&address).is_some() + self.state.contains_key(&address) } fn storage(&self, address: H160, index: H256) -> H256 { diff --git a/src/call_stack.rs b/src/call_stack.rs index 09a28f847..2c76eaf27 100644 --- a/src/call_stack.rs +++ b/src/call_stack.rs @@ -254,7 +254,7 @@ where match invoker.enter_substack(trap, &mut machine, backend, initial_depth + 1) { Capture::Exit(Ok((trap_data, InvokerControl::Enter(sub_machine)))) => { let (sub_result, sub_machine) = if heap_depth - .map_or(false, |hd| initial_depth + 1 >= hd) + .is_some_and(|hd| initial_depth + 1 >= hd) { match CallStack::new(sub_machine, initial_depth + 1, backend, invoker) .run() From 24b4b6096c6111fbbc64698f71e53e1db3b7299b Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Fri, 23 May 2025 08:53:08 +0200 Subject: [PATCH 6/6] Fix test clippy --- tests/mock.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mock.rs b/tests/mock.rs index 30829e5b2..2449740a7 100644 --- a/tests/mock.rs +++ b/tests/mock.rs @@ -108,7 +108,7 @@ impl RuntimeBaseBackend for MockBackend { } fn exists(&self, address: H160) -> bool { - self.state.get(&address).is_some() + self.state.contains_key(&address) } fn storage(&self, address: H160, index: H256) -> H256 {