Skip to content

Swich interpreter stack from H256 to U256 #317

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

Merged
merged 6 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <contact-rust-evm@pacna.org>"]
repository = "https://github.com/rust-ethereum/evm"
Expand Down
104 changes: 50 additions & 54 deletions interpreter/src/error/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand Down Expand Up @@ -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<U256>,
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)
Expand All @@ -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,
},
Expand All @@ -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 {
Expand All @@ -205,7 +200,7 @@ impl CallTrapData {
Ok((
(),
Self {
target: (*to).into(),
target: to.into(),
transfer,
input,
gas,
Expand All @@ -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,
)
})
}
Expand Down Expand Up @@ -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,
Expand All @@ -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())
}
Expand All @@ -326,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())
}
}

Expand Down Expand Up @@ -369,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()
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -423,7 +420,7 @@ impl CreateTrapData {
(),
Self {
scheme,
value,
value: *value,
code,
},
))
Expand All @@ -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
Expand All @@ -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,
};

Expand All @@ -466,7 +459,7 @@ impl CreateTrapData {
(),
Self {
scheme,
value,
value: *value,
code,
},
))
Expand All @@ -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())
}
};
Expand Down
18 changes: 10 additions & 8 deletions interpreter/src/eval/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
};
)*
Expand All @@ -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()),
}
Expand All @@ -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()),
}
Expand Down
Loading