Skip to content
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

Minor refactorings #151

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions benches/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn run_loop_contract() {
let vicinity = MemoryVicinity {
gas_price: U256::zero(),
origin: H160::default(),
block_hashes: Vec::new(),
block_hashes: vec![],
block_number: Default::default(),
block_coinbase: Default::default(),
block_timestamp: Default::default(),
Expand All @@ -37,7 +37,7 @@ fn run_loop_contract() {
nonce: U256::one(),
balance: U256::from(10000000),
storage: BTreeMap::new(),
code: Vec::new(),
code: vec![],
},
);

Expand All @@ -55,7 +55,7 @@ fn run_loop_contract() {
.unwrap(),
// hex::decode("0f14a4060000000000000000000000000000000000000000000000000000000000002ee0").unwrap(),
u64::MAX,
Vec::new(),
vec![],
);
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/eval/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use primitive_types::{U256, U512};

#[inline]
pub fn div(op1: U256, op2: U256) -> U256 {
if op2 == U256::zero() {
if op2.is_zero() {
U256::zero()
} else {
op1 / op2
Expand All @@ -22,7 +22,7 @@ pub fn sdiv(op1: U256, op2: U256) -> U256 {

#[inline]
pub fn rem(op1: U256, op2: U256) -> U256 {
if op2 == U256::zero() {
if op2.is_zero() {
U256::zero()
} else {
op1.rem(op2)
Expand All @@ -31,7 +31,7 @@ pub fn rem(op1: U256, op2: U256) -> U256 {

#[inline]
pub fn srem(op1: U256, op2: U256) -> U256 {
if op2 == U256::zero() {
if op2.is_zero() {
U256::zero()
} else {
let op1: I256 = op1.into();
Expand Down
8 changes: 4 additions & 4 deletions core/src/eval/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn sgt(op1: U256, op2: U256) -> U256 {

#[inline]
pub fn iszero(op1: U256) -> U256 {
if op1 == U256::zero() {
if op1.is_zero() {
U256::one()
} else {
U256::zero()
Expand Down Expand Up @@ -58,7 +58,7 @@ pub fn byte(op1: U256, op2: U256) -> U256 {

#[inline]
pub fn shl(shift: U256, value: U256) -> U256 {
if value == U256::zero() || shift >= U256::from(256) {
if value.is_zero() || shift >= 256.into() {
U256::zero()
} else {
let shift: u64 = shift.as_u64();
Expand All @@ -68,7 +68,7 @@ pub fn shl(shift: U256, value: U256) -> U256 {

#[inline]
pub fn shr(shift: U256, value: U256) -> U256 {
if value == U256::zero() || shift >= U256::from(256) {
if value.is_zero() || shift >= 256.into() {
U256::zero()
} else {
let shift: u64 = shift.as_u64();
Expand All @@ -80,7 +80,7 @@ pub fn shr(shift: U256, value: U256) -> U256 {
pub fn sar(shift: U256, value: U256) -> U256 {
let value = I256::from(value);

if value == I256::zero() || shift >= U256::from(256) {
if value == I256::zero() || shift >= 256.into() {
let I256(sign, _) = value;
match sign {
// value is 0 or >=1, pushing 0
Expand Down
4 changes: 2 additions & 2 deletions core/src/eval/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ macro_rules! op3_u256_fn {

macro_rules! as_usize_or_fail {
( $v:expr ) => {{
if $v > U256::from(usize::MAX) {
if $v > usize::MAX.into() {
return Control::Exit(ExitFatal::NotSupported.into());
}

$v.as_usize()
}};

( $v:expr, $reason:expr ) => {{
if $v > U256::from(usize::MAX) {
if $v > usize::MAX.into() {
return Control::Exit($reason.into());
}

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 @@ -5,7 +5,7 @@ use primitive_types::{H256, U256};

#[inline]
pub fn codesize(state: &mut Machine) -> Control {
let size = U256::from(state.code.len());
let size: U256 = state.code.len().into();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the reasoning behind those changes? Why do we want to prefer Into than From?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s more idiomatic.

push_u256!(state, size);
Control::Continue(1)
}
Expand All @@ -31,8 +31,8 @@ pub fn calldataload(state: &mut Machine) -> Control {
let mut load = [0u8; 32];
#[allow(clippy::needless_range_loop)]
for i in 0..32 {
if let Some(p) = index.checked_add(U256::from(i)) {
if p <= U256::from(usize::MAX) {
if let Some(p) = index.checked_add(i.into()) {
if p <= usize::MAX.into() {
let p = p.as_usize();
if p < state.data.len() {
load[i] = state.data[p];
Expand All @@ -41,13 +41,13 @@ pub fn calldataload(state: &mut Machine) -> Control {
}
}

push!(state, H256::from(load));
push!(state, load.into());
Control::Continue(1)
}

#[inline]
pub fn calldatasize(state: &mut Machine) -> Control {
let len = U256::from(state.data.len());
let len: U256 = state.data.len().into();
push_u256!(state, len);
Control::Continue(1)
}
Expand All @@ -57,7 +57,7 @@ pub fn calldatacopy(state: &mut Machine) -> Control {
pop_u256!(state, memory_offset, data_offset, len);

try_or_fail!(state.memory.resize_offset(memory_offset, len));
if len == U256::zero() {
if len.is_zero() {
return Control::Continue(1);
}

Expand Down
12 changes: 4 additions & 8 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,10 @@ impl Machine {

/// Copy and get the return value of the machine, if any.
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
} else if self.return_range.end > U256::from(usize::MAX) {
if self.return_range.start > usize::MAX.into() {
let len = (self.return_range.end - self.return_range.start).as_usize();
vec![0; len]
} else if self.return_range.end > usize::MAX.into() {
let mut ret = self.memory.get(
self.return_range.start.as_usize(),
usize::MAX - self.return_range.start.as_usize(),
Expand Down
20 changes: 8 additions & 12 deletions core/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Memory {
/// Create a new memory with the given limit.
pub fn new(limit: usize) -> Self {
Self {
data: Vec::new(),
data: vec![],
effective_len: U256::zero(),
limit,
}
Expand All @@ -44,15 +44,15 @@ impl Memory {
}

/// Return the full memory.
pub fn data(&self) -> &Vec<u8> {
pub fn data(&self) -> &[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.
pub fn resize_offset(&mut self, offset: U256, len: U256) -> Result<(), ExitError> {
if len == U256::zero() {
if len.is_zero() {
return Ok(());
}

Expand Down Expand Up @@ -80,8 +80,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.
pub fn get(&self, offset: usize, size: usize) -> Vec<u8> {
let mut ret = Vec::new();
ret.resize(size, 0);
let mut ret = vec![0; size];

#[allow(clippy::needless_range_loop)]
for index in 0..size {
Expand Down Expand Up @@ -150,20 +149,20 @@ impl Memory {
return Ok(());
}

let memory_offset = if memory_offset > U256::from(usize::MAX) {
let memory_offset = if memory_offset > usize::MAX.into() {
return Err(ExitFatal::NotSupported);
} else {
memory_offset.as_usize()
};

let ulen = if len > U256::from(usize::MAX) {
let ulen = if len > usize::MAX.into() {
return Err(ExitFatal::NotSupported);
} else {
len.as_usize()
};

let data = if let Some(end) = data_offset.checked_add(len) {
if end > U256::from(usize::MAX) {
if end > usize::MAX.into() {
&[]
} else {
let data_offset = data_offset.as_usize();
Expand Down Expand Up @@ -208,10 +207,7 @@ mod tests {
continue;
}
let next_multiple = x + 32 - (x % 32);
assert_eq!(
Some(U256::from(next_multiple)),
next_multiple_of_32(x.into())
);
assert_eq!(Some(next_multiple.into()), next_multiple_of_32(x.into()));
}

// next_multiple_of_32 returns None when the next multiple of 32 is too big
Expand Down
Loading