Skip to content

Commit

Permalink
Chore: Refactoring with simplification and more idiomatic clippy-way (r…
Browse files Browse the repository at this point in the history
…ust-ethereum#260)

* Refactoring with simplification and more idiomatic clippy-way

* Restore work() func for new_transact
  • Loading branch information
mrLSD authored Dec 8, 2023
1 parent 1e1aac7 commit fdd7408
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 50 deletions.
2 changes: 1 addition & 1 deletion interpreter/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl std::error::Error for ExitError {
#[cfg(feature = "std")]
impl std::fmt::Display for ExitError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
write!(f, "{self:?}")
}
}

Expand Down
4 changes: 4 additions & 0 deletions interpreter/src/etable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,18 @@ where
}

impl<S, H, Tr> Etable<S, H, Tr> {
#[must_use]
pub const fn none() -> Self {
Self([eval_unknown as _; 256], PhantomData)
}

#[must_use]
pub const fn pass() -> Self {
Self([eval_pass as _; 256], PhantomData)
}

/// Default core value for Etable.
#[must_use]
pub const fn core() -> Self {
let mut table = [eval_unknown as _; 256];

Expand Down Expand Up @@ -253,6 +256,7 @@ where
S: AsRef<RuntimeState> + GasState,
{
/// Runtime Etable.
#[must_use]
pub const fn runtime() -> Self {
let mut table = Self::core();

Expand Down
6 changes: 3 additions & 3 deletions interpreter/src/eval/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ pub fn jumpi<S, Tr>(state: &mut Machine<S>) -> Control<Tr> {
pop_u256!(state, dest);
pop!(state, value);

if value != H256::zero() {
if value == H256::zero() {
Control::Continue
} else {
let dest = as_usize_or_fail!(dest, ExitException::InvalidJump);
Control::Jump(dest)
} else {
Control::Continue
}
}

Expand Down
8 changes: 3 additions & 5 deletions interpreter/src/eval/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,9 @@ pub fn returndatacopy<S: AsRef<RuntimeState>, Tr>(machine: &mut Machine<S>) -> C
pop_u256!(machine, memory_offset, data_offset, len);

try_or_fail!(machine.memory.resize_offset(memory_offset, len));
if data_offset
.checked_add(len)
.map(|l| l > U256::from(machine.state.as_ref().retbuf.len()))
.unwrap_or(true)
{
if data_offset.checked_add(len).map_or(true, |l| {
l > U256::from(machine.state.as_ref().retbuf.len())
}) {
return Control::Exit(ExitException::OutOfOffset.into());
}

Expand Down
1 change: 1 addition & 0 deletions interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl<S> Machine<S> {
}

/// Whether the machine has empty code.
#[must_use]
pub fn is_empty(&self) -> bool {
self.code.is_empty()
}
Expand Down
21 changes: 12 additions & 9 deletions interpreter/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Memory {

impl Memory {
/// Create a new memory with the given limit.
#[must_use]
pub fn new(limit: usize) -> Self {
Self {
data: Vec::new(),
Expand All @@ -25,26 +26,31 @@ impl Memory {
}

/// Memory limit.
#[must_use]
pub const fn limit(&self) -> usize {
self.limit
}

/// Get the length of the current memory range.
#[must_use]
pub fn len(&self) -> usize {
self.data.len()
}

/// Get the effective length.
#[must_use]
pub const fn effective_len(&self) -> U256 {
self.effective_len
}

/// Return true if current effective memory range is zero.
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Return the full memory.
#[must_use]
pub const fn data(&self) -> &Vec<u8> {
&self.data
}
Expand All @@ -63,11 +69,9 @@ impl Memory {
return Ok(());
}

if let Some(end) = offset.checked_add(len) {
self.resize_end(end)
} else {
Err(ExitException::InvalidRange)
}
offset
.checked_add(len)
.map_or(Err(ExitException::InvalidRange), |end| self.resize_end(end))
}

/// Resize the memory, making it cover to `end`, with 32 bytes as the step.
Expand Down Expand Up @@ -109,6 +113,7 @@ impl Memory {
///
/// Value of `size` is considered trusted. If they're too large,
/// the program can run out of memory, or it can overflow.
#[must_use]
pub fn get(&self, offset: usize, size: usize) -> Vec<u8> {
let mut ret = vec![0; size];

Expand Down Expand Up @@ -196,7 +201,7 @@ impl Memory {
len.as_usize()
};

let data = if let Some(end) = data_offset.checked_add(len) {
let data: &[u8] = data_offset.checked_add(len).map_or(&[], |end| {
if end > U256::from(usize::MAX) {
&[]
} else {
Expand All @@ -209,9 +214,7 @@ impl Memory {
&data[data_offset..min(end, data.len())]
}
}
} else {
&[]
};
});

self.set(memory_offset, data, Some(ulen))
}
Expand Down
3 changes: 3 additions & 0 deletions interpreter/src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ impl Opcode {

impl Opcode {
/// Whether the opcode is a push opcode.
#[must_use]
pub fn is_push(&self) -> Option<u8> {
let value = self.0;
if (0x60..=0x7f).contains(&value) {
Expand All @@ -261,11 +262,13 @@ impl Opcode {
}

#[inline]
#[must_use]
pub const fn as_u8(&self) -> u8 {
self.0
}

#[inline]
#[must_use]
pub const fn as_usize(&self) -> usize {
self.0 as usize
}
Expand Down
9 changes: 7 additions & 2 deletions interpreter/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ macro_rules! impl_perform_popn_pushn {

impl Stack {
/// Create a new stack with given limit.
#[must_use]
pub const fn new(limit: usize) -> Self {
Self {
data: Vec::new(),
Expand All @@ -57,31 +58,35 @@ impl Stack {

#[inline]
/// Stack limit.
#[must_use]
pub const fn limit(&self) -> usize {
self.limit
}

#[inline]
/// Stack length.
#[must_use]
pub fn len(&self) -> usize {
self.data.len()
}

#[inline]
/// Whether the stack is empty.
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}

#[inline]
/// Stack data.
#[must_use]
pub const fn data(&self) -> &Vec<H256> {
&self.data
}

/// Clear the stack.
pub fn clear(&mut self) {
self.data.clear()
self.data.clear();
}

#[inline]
Expand Down
8 changes: 5 additions & 3 deletions interpreter/src/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl CreateScheme {
}
}

#[must_use]
pub const fn caller(&self) -> H160 {
match self {
Self::Create2 { caller, .. } => *caller,
Expand Down Expand Up @@ -118,6 +119,7 @@ pub enum CallCreateTrapData {
}

impl CallCreateTrapData {
#[must_use]
pub const fn target_gas(&self) -> Option<U256> {
match self {
Self::Call(CallTrapData { gas, .. }) => Some(*gas),
Expand Down Expand Up @@ -185,7 +187,7 @@ impl CallTrapData {
out_len: &H256,
) -> Result<((), Self), ExitError> {
let gas = h256_to_u256(*gas);
let value = value.map(|v| h256_to_u256(*v)).unwrap_or(U256::zero());
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);
Expand Down Expand Up @@ -366,11 +368,11 @@ impl CallTrapData {
}
}

#[must_use]
pub fn has_value(&self) -> bool {
self.transfer
.as_ref()
.map(|t| t.value != U256::zero())
.unwrap_or(false)
.map_or(false, |t| t.value != U256::zero())
}
}

Expand Down
12 changes: 8 additions & 4 deletions interpreter/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use core::ops::{Div, Rem};
use primitive_types::{H256, U256};

/// Convert [U256] into [H256].
#[must_use]
pub fn u256_to_h256(v: U256) -> H256 {
let mut r = H256::default();
v.to_big_endian(&mut r[..]);
r
}

/// Convert [H256] to [U256].
#[must_use]
pub fn h256_to_u256(v: H256) -> U256 {
U256::from_big_endian(&v[..])
}
Expand All @@ -34,10 +36,10 @@ pub enum Sign {
}

const SIGN_BIT_MASK: U256 = U256([
0xffffffffffffffff,
0xffffffffffffffff,
0xffffffffffffffff,
0x7fffffffffffffff,
0xffff_ffff_ffff_ffff,
0xffff_ffff_ffff_ffff,
0xffff_ffff_ffff_ffff,
0x7fff_ffff_ffff_ffff,
]);

/// Signed 256-bit integer.
Expand All @@ -46,10 +48,12 @@ pub struct I256(pub Sign, pub U256);

impl I256 {
/// Zero value of I256.
#[must_use]
pub const fn zero() -> I256 {
I256(Sign::Zero, U256::zero())
}
/// Minimum value of I256.
#[must_use]
pub fn min_value() -> I256 {
I256(Sign::Minus, (U256::MAX & SIGN_BIT_MASK) + U256::from(1u64))
}
Expand Down
4 changes: 4 additions & 0 deletions interpreter/src/valids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Valids(Vec<bool>);

impl Valids {
/// Create a new valid mapping from given code bytes.
#[must_use]
pub fn new(code: &[u8]) -> Self {
let mut valids: Vec<bool> = Vec::with_capacity(code.len());
valids.resize(code.len(), false);
Expand All @@ -30,18 +31,21 @@ impl Valids {
/// Get the length of the valid mapping. This is the same as the
/// code bytes.
#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}

/// Returns true if the valids list is empty
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// Returns `true` if the position is a valid jump destination. If
/// not, returns `false`.
#[must_use]
pub fn is_valid(&self, position: usize) -> bool {
if position >= self.0.len() {
return false;
Expand Down
10 changes: 4 additions & 6 deletions src/call_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,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(|hd| initial_depth + 1 >= hd)
.unwrap_or(false)
.map_or(false, |hd| initial_depth + 1 >= hd)
{
match CallStack::new(sub_machine, initial_depth + 1, backend, invoker)
.run()
Expand Down Expand Up @@ -381,10 +380,9 @@ where
invoker,
backend,
}) => {
let (transact_invoke, control) = match invoker.new_transact(args, backend) {
Ok((transact_invoke, control)) => (transact_invoke, control),
Err(err) => return Err(Capture::Exit(Err(err))),
};
let (transact_invoke, control) = invoker
.new_transact(args, backend)
.map_err(|err| Capture::Exit(Err(err)))?;

match control {
InvokerControl::Enter(machine) => {
Expand Down
Loading

0 comments on commit fdd7408

Please sign in to comment.