Skip to content

Commit

Permalink
chore: apply suggestions by clippy (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksuss committed Sep 12, 2023
1 parent 5f74467 commit df6f494
Show file tree
Hide file tree
Showing 17 changed files with 179 additions and 164 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

env:
CARGO_TERM_COLOR: always
Expand All @@ -17,7 +16,7 @@ jobs:
- name: Rustfmt
run: cargo fmt --all -- --check
- name: Clippy
run: cargo clippy --all
run: cargo clippy --workspace --all-targets -- -D clippy::all -D clippy::nursery
build:
runs-on: ubuntu-latest
steps:
Expand Down
12 changes: 8 additions & 4 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,26 @@ pub enum ExitReason {

impl ExitReason {
/// Whether the exit is succeeded.
pub fn is_succeed(&self) -> bool {
#[must_use]
pub const fn is_succeed(&self) -> bool {
matches!(self, Self::Succeed(_))
}

/// Whether the exit is error.
pub fn is_error(&self) -> bool {
#[must_use]
pub const fn is_error(&self) -> bool {
matches!(self, Self::Error(_))
}

/// Whether the exit is revert.
pub fn is_revert(&self) -> bool {
#[must_use]
pub const fn is_revert(&self) -> bool {
matches!(self, Self::Revert(_))
}

/// Whether the exit is fatal.
pub fn is_fatal(&self) -> bool {
#[must_use]
pub const fn is_fatal(&self) -> bool {
matches!(self, Self::Fatal(_))
}
}
Expand Down
12 changes: 6 additions & 6 deletions core/src/eval/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ pub fn jumpi(state: &mut Machine) -> Control {
pop_u256!(state, dest);
pop_u256!(state, value);

if value != U256::zero() {
if value == U256::zero() {
Control::Continue(1)
} else {
let dest = as_usize_or_fail!(dest, ExitError::InvalidJump);
if state.valids.is_valid(dest) {
Control::Jump(dest)
} else {
Control::Exit(ExitError::InvalidJump.into())
}
} else {
Control::Continue(1)
}
}

Expand Down Expand Up @@ -186,7 +186,7 @@ pub fn push0(state: &mut Machine) -> Control {

#[inline]
pub fn push1(state: &mut Machine, position: usize) -> Control {
let b0 = *state.code.get(position + 1).unwrap_or(&0) as u64;
let b0 = u64::from(*state.code.get(position + 1).unwrap_or(&0));
let val = U256::from(b0);

push_u256!(state, val);
Expand All @@ -195,8 +195,8 @@ pub fn push1(state: &mut Machine, position: usize) -> Control {

#[inline]
pub fn push2(state: &mut Machine, position: usize) -> Control {
let b0 = *state.code.get(position + 1).unwrap_or(&0) as u64;
let b1 = *state.code.get(position + 2).unwrap_or(&0) as u64;
let b0 = u64::from(*state.code.get(position + 1).unwrap_or(&0));
let b1 = u64::from(*state.code.get(position + 2).unwrap_or(&0));
let val = U256::from((b0 << 8) | b1);

push_u256!(state, val);
Expand Down
11 changes: 5 additions & 6 deletions core/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,11 @@ fn eval_table<H: InterpreterHandler>(
let mut pc = position;
handler.before_eval();
loop {
let op = match state.code.get(pc) {
Some(v) => Opcode(*v),
None => {
state.position = Err(ExitSucceed::Stopped.into());
return Control::Exit(ExitSucceed::Stopped.into());
}
let op = if let Some(v) = state.code.get(pc) {
Opcode(*v)
} else {
state.position = Err(ExitSucceed::Stopped.into());
return Control::Exit(ExitSucceed::Stopped.into());
};
match handler.before_bytecode(op, pc, state, address) {
Ok(()) => (),
Expand Down
30 changes: 15 additions & 15 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,30 @@ pub trait InterpreterHandler {

impl Machine {
/// Reference of machine stack.
pub fn stack(&self) -> &Stack {
#[must_use]
pub const fn stack(&self) -> &Stack {
&self.stack
}
/// Mutable reference of machine stack.
pub fn stack_mut(&mut self) -> &mut Stack {
&mut self.stack
}
/// Reference of machine memory.
pub fn memory(&self) -> &Memory {
#[must_use]
pub const fn memory(&self) -> &Memory {
&self.memory
}
/// Mutable reference of machine memory.
pub fn memory_mut(&mut self) -> &mut Memory {
&mut self.memory
}
/// Return a reference of the program counter.
pub fn position(&self) -> &Result<usize, ExitReason> {
pub const fn position(&self) -> &Result<usize, ExitReason> {
&self.position
}

/// Create a new machine with given code and data.
#[must_use]
pub fn new(
code: Rc<Vec<u8>>,
data: Rc<Vec<u8>>,
Expand All @@ -110,23 +113,17 @@ impl Machine {
}

/// Inspect the machine's next opcode and current stack.
#[must_use]
pub fn inspect(&self) -> Option<(Opcode, &Stack)> {
let position = match self.position {
Ok(position) => position,
Err(_) => return None,
};
let Ok(position) = self.position else { return None };
self.code.get(position).map(|v| (Opcode(*v), &self.stack))
}

/// Copy and get the return value of the machine, if any.
#[must_use]
pub fn return_value(&self) -> Vec<u8> {
if self.return_range.start > U256::from(usize::MAX) {
let mut ret = Vec::new();
ret.resize(
(self.return_range.end - self.return_range.start).as_usize(),
0,
);
ret
vec![0; (self.return_range.end - self.return_range.start).as_usize()]
} else if self.return_range.end > U256::from(usize::MAX) {
let mut ret = self.memory.get(
self.return_range.start.as_usize(),
Expand Down Expand Up @@ -185,15 +182,18 @@ pub struct SimpleInterpreterHandler {
}

impl SimpleInterpreterHandler {
pub fn new(address: H160) -> Self {
#[must_use]
pub const fn new(address: H160) -> Self {
Self {
executed: 0,
profile: [0; 256],
address,
}
}
}

pub fn default() -> Self {
impl Default for SimpleInterpreterHandler {
fn default() -> Self {
Self {
executed: 0,
profile: [0; 256],
Expand Down
60 changes: 32 additions & 28 deletions core/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ pub struct Memory {

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

/// Memory limit.
pub fn limit(&self) -> usize {
#[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.
pub fn effective_len(&self) -> usize {
#[must_use]
pub const fn effective_len(&self) -> usize {
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.
pub fn data(&self) -> &Vec<u8> {
#[must_use]
pub const fn data(&self) -> &Vec<u8> {
&self.data
}

/// 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.
/// 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: usize, len: usize) -> Result<(), ExitError> {
if len == 0 {
return Ok(());
}

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

/// Resize the memory, making it cover to `end`, with 32 bytes as the step.
Expand All @@ -79,6 +82,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, mut offset: usize, size: usize) -> Vec<u8> {
if offset > self.data.len() {
offset = self.data.len();
Expand All @@ -95,6 +99,7 @@ impl Memory {
}

/// Get `H256` from a specific offset in memory.
#[must_use]
pub fn get_h256(&self, offset: usize) -> H256 {
let mut ret = [0; 32];

Expand Down Expand Up @@ -126,8 +131,7 @@ impl Memory {

if offset
.checked_add(target_size)
.map(|pos| pos > self.limit)
.unwrap_or(true)
.map_or(true, |pos| pos > self.limit)
{
return Err(ExitFatal::NotSupported);
}
Expand Down Expand Up @@ -165,22 +169,22 @@ impl Memory {
return Ok(());
}

let data = if let Some(end) = data_offset.checked_add(len.into()) {
if end > U256::from(usize::MAX) {
&[]
} else {
let data_offset = data_offset.as_usize();
let end = end.as_usize();

if data_offset > data.len() {
let data = data_offset
.checked_add(len.into())
.map_or(&[] as &[u8], |end| {
if end > U256::from(usize::MAX) {
&[]
} else {
&data[data_offset..min(end, data.len())]
let data_offset = data_offset.as_usize();
let end = end.as_usize();

if data_offset > data.len() {
&[]
} else {
&data[data_offset..min(end, data.len())]
}
}
}
} else {
&[]
};
});

self.set(memory_offset, data, Some(len))
}
Expand Down Expand Up @@ -211,7 +215,7 @@ mod tests {
continue;
}
let next_multiple = x + 32 - (x % 32);
assert_eq!(Some(next_multiple), next_multiple_of_32(x.into()));
assert_eq!(Some(next_multiple), next_multiple_of_32(x));
}

// next_multiple_of_32 returns None when the next multiple of 32 is too big
Expand Down
5 changes: 5 additions & 0 deletions core/src/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
pub struct Opcode(pub u8);

// Core opcodes.
#[allow(clippy::use_self)]
impl Opcode {
/// `STOP`
pub const STOP: Opcode = Opcode(0x00);
Expand Down Expand Up @@ -178,6 +179,7 @@ impl Opcode {
}

// External opcodes
#[allow(clippy::use_self)]
impl Opcode {
/// `SHA3`
pub const SHA3: Opcode = Opcode(0x20);
Expand Down Expand Up @@ -251,6 +253,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 +264,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
Loading

0 comments on commit df6f494

Please sign in to comment.