From 5580be4f2656a639fde52d3059b5d0fd455a6d91 Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Wed, 4 Jan 2023 13:54:26 -0800 Subject: [PATCH 1/6] nitpicks --- core/src/lib.rs | 8 ++------ core/src/memory.rs | 5 ++--- core/src/stack.rs | 2 +- core/src/valids.rs | 3 +-- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/core/src/lib.rs b/core/src/lib.rs index 5bda37eea..5c3380b2d 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -104,12 +104,8 @@ impl Machine { /// Copy and get the return value of the machine, if any. pub fn return_value(&self) -> Vec { 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 + let len = (self.return_range.end - self.return_range.start).as_usize(); + vec![0; len] } else if self.return_range.end > U256::from(usize::MAX) { let mut ret = self.memory.get( self.return_range.start.as_usize(), diff --git a/core/src/memory.rs b/core/src/memory.rs index 7bb09629b..fb938b7c8 100644 --- a/core/src/memory.rs +++ b/core/src/memory.rs @@ -44,7 +44,7 @@ impl Memory { } /// Return the full memory. - pub fn data(&self) -> &Vec { + pub fn data(&self) -> &[u8] { &self.data } @@ -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 { - 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 { diff --git a/core/src/stack.rs b/core/src/stack.rs index 893af517c..2175d55b7 100644 --- a/core/src/stack.rs +++ b/core/src/stack.rs @@ -38,7 +38,7 @@ impl Stack { #[inline] /// Stack data. - pub fn data(&self) -> &Vec { + pub fn data(&self) -> &[H256] { &self.data } diff --git a/core/src/valids.rs b/core/src/valids.rs index 22a082104..f7c3bfa06 100644 --- a/core/src/valids.rs +++ b/core/src/valids.rs @@ -8,8 +8,7 @@ pub struct Valids(Vec); impl Valids { /// Create a new valid mapping from given code bytes. pub fn new(code: &[u8]) -> Self { - let mut valids: Vec = Vec::with_capacity(code.len()); - valids.resize(code.len(), false); + let mut valids = vec![false; code.len()]; let mut i = 0; while i < code.len() { From 3567b3424c0685bffd21de430265f7e99c4453d6 Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Wed, 4 Jan 2023 13:57:53 -0800 Subject: [PATCH 2/6] use vec![] instead of Vec::new() --- benches/loop.rs | 6 ++-- core/src/memory.rs | 2 +- core/src/stack.rs | 2 +- core/src/valids.rs | 2 +- fuzzer/src/main.rs | 6 ++-- runtime/src/eval/system.rs | 2 +- runtime/src/lib.rs | 2 +- src/backend/memory.rs | 2 +- src/executor/stack/executor.rs | 61 ++++++++++++++++------------------ src/executor/stack/memory.rs | 6 ++-- 10 files changed, 43 insertions(+), 48 deletions(-) diff --git a/benches/loop.rs b/benches/loop.rs index 1d338c86d..94061c73f 100644 --- a/benches/loop.rs +++ b/benches/loop.rs @@ -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(), @@ -37,7 +37,7 @@ fn run_loop_contract() { nonce: U256::one(), balance: U256::from(10000000), storage: BTreeMap::new(), - code: Vec::new(), + code: vec![], }, ); @@ -55,7 +55,7 @@ fn run_loop_contract() { .unwrap(), // hex::decode("0f14a4060000000000000000000000000000000000000000000000000000000000002ee0").unwrap(), u64::MAX, - Vec::new(), + vec![], ); } diff --git a/core/src/memory.rs b/core/src/memory.rs index fb938b7c8..4d9c674a2 100644 --- a/core/src/memory.rs +++ b/core/src/memory.rs @@ -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, } diff --git a/core/src/stack.rs b/core/src/stack.rs index 2175d55b7..73ed5a464 100644 --- a/core/src/stack.rs +++ b/core/src/stack.rs @@ -13,7 +13,7 @@ impl Stack { /// Create a new stack with given limit. pub fn new(limit: usize) -> Self { Self { - data: Vec::new(), + data: vec![], limit, } } diff --git a/core/src/valids.rs b/core/src/valids.rs index f7c3bfa06..1ad1e6bee 100644 --- a/core/src/valids.rs +++ b/core/src/valids.rs @@ -23,7 +23,7 @@ impl Valids { } } - Valids(valids) + Self(valids) } /// Get the length of the valid mapping. This is the same as the diff --git a/fuzzer/src/main.rs b/fuzzer/src/main.rs index 5d9caccb8..af959abef 100644 --- a/fuzzer/src/main.rs +++ b/fuzzer/src/main.rs @@ -8,7 +8,7 @@ fn find_subsequence(haystack: &[u8], needle: &[u8]) -> Option { } fn split_at_delim(sequence: &[u8], delim: &[u8]) -> (Vec, Vec) { - let mut res_vec = Vec::new(); + let mut res_vec = vec![]; let mut current_pos = 0; if let Some(index) = find_subsequence(&sequence[current_pos..], delim) { let found_index = index + current_pos; @@ -16,7 +16,7 @@ fn split_at_delim(sequence: &[u8], delim: &[u8]) -> (Vec, Vec) { current_pos = found_index + delim.len(); } if current_pos == 0 { - (sequence.to_vec(), Vec::new()) + (sequence.to_vec(), vec![]) } else { res_vec.push(sequence[current_pos..].to_vec()); (res_vec[0].to_owned(), res_vec[1].to_owned()) @@ -63,7 +63,7 @@ fn main() { }; for argument in all_files { println!("Now doing file {:?}", argument); - let mut buffer: Vec = Vec::new(); + let mut buffer: Vec = vec![]; let mut f = File::open(argument).unwrap(); f.read_to_end(&mut buffer).unwrap(); handle_data(buffer.as_slice()); diff --git a/runtime/src/eval/system.rs b/runtime/src/eval/system.rs index 574355437..6aeba6e0f 100644 --- a/runtime/src/eval/system.rs +++ b/runtime/src/eval/system.rs @@ -12,7 +12,7 @@ pub fn sha3(runtime: &mut Runtime) -> Control { try_or_fail!(runtime.machine.memory_mut().resize_offset(from, len)); let data = if len == U256::zero() { - Vec::new() + vec![] } else { let from = as_usize_or_fail!(from); let len = as_usize_or_fail!(len); diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 3cf482715..5aff95e24 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -128,7 +128,7 @@ impl<'config> Runtime<'config> { Self { machine: Machine::new(code, data, config.stack_limit, config.memory_limit), status: Ok(()), - return_data_buffer: Vec::new(), + return_data_buffer: vec![], return_data_len: U256::zero(), return_data_offset: U256::zero(), context, diff --git a/src/backend/memory.rs b/src/backend/memory.rs index 7382a5e22..f4538b481 100644 --- a/src/backend/memory.rs +++ b/src/backend/memory.rs @@ -65,7 +65,7 @@ impl<'vicinity> MemoryBackend<'vicinity> { Self { vicinity, state, - logs: Vec::new(), + logs: vec![], } } diff --git a/src/executor/stack/executor.rs b/src/executor/stack/executor.rs index 1ddda5b18..558cfbb25 100644 --- a/src/executor/stack/executor.rs +++ b/src/executor/stack/executor.rs @@ -22,7 +22,7 @@ macro_rules! emit_exit { let reason = $reason; event!(Exit { reason: &reason, - return_value: &Vec::new(), + return_value: &vec![], }); reason }}; @@ -456,7 +456,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> return ( ExitReason::Fatal(ExitFatal::UnhandledInterrupt), None, - Vec::new(), + vec![], ); } }; @@ -512,7 +512,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> }; // Early exit if passing on the result caused an error if let Err(e) = maybe_error { - return (e, None, Vec::new()); + return (e, None, vec![]); } } } @@ -550,7 +550,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> }); if let Err(e) = self.record_create_transaction_cost(&init_code, &access_list) { - return emit_exit!(e.into(), Vec::new()); + return emit_exit!(e.into(), vec![]); } self.initialize_with_access_list(access_list); @@ -597,7 +597,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> }); if let Err(e) = self.record_create_transaction_cost(&init_code, &access_list) { - return emit_exit!(e.into(), Vec::new()); + return emit_exit!(e.into(), vec![]); } self.initialize_with_access_list(access_list); @@ -650,7 +650,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> let gasometer = &mut self.state.metadata_mut().gasometer; match gasometer.record_transaction(transaction_cost) { Ok(()) => (), - Err(e) => return emit_exit!(e.into(), Vec::new()), + Err(e) => return emit_exit!(e.into(), vec![]), } // Initialize initial addresses for EIP-2929 @@ -762,7 +762,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> ( $e:expr ) => { match $e { Ok(v) => v, - Err(e) => return Capture::Exit((e.into(), None, Vec::new())), + Err(e) => return Capture::Exit((e.into(), None, vec![])), } }; } @@ -787,12 +787,12 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> if let Some(depth) = self.state.metadata().depth { if depth > self.config.call_stack_limit { - return Capture::Exit((ExitError::CallTooDeep.into(), None, Vec::new())); + return Capture::Exit((ExitError::CallTooDeep.into(), None, vec![])); } } if self.balance(caller) < value { - return Capture::Exit((ExitError::OutOfFund.into(), None, Vec::new())); + return Capture::Exit((ExitError::OutOfFund.into(), None, vec![])); } let after_gas = if take_l64 && self.config.call_l64_after_gas { @@ -820,12 +820,12 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> { if self.code_size(address) != U256::zero() { let _ = self.exit_substate(StackExitKind::Failed); - return Capture::Exit((ExitError::CreateCollision.into(), None, Vec::new())); + return Capture::Exit((ExitError::CreateCollision.into(), None, vec![])); } if self.nonce(address) > U256::zero() { let _ = self.exit_substate(StackExitKind::Failed); - return Capture::Exit((ExitError::CreateCollision.into(), None, Vec::new())); + return Capture::Exit((ExitError::CreateCollision.into(), None, vec![])); } self.state.reset_storage(address); @@ -845,7 +845,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Ok(()) => (), Err(e) => { let _ = self.exit_substate(StackExitKind::Reverted); - return Capture::Exit((ExitReason::Error(e), None, Vec::new())); + return Capture::Exit((ExitReason::Error(e), None, vec![])); } } @@ -853,12 +853,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> self.state.inc_nonce(address); } - let runtime = Runtime::new( - Rc::new(init_code), - Rc::new(Vec::new()), - context, - self.config, - ); + let runtime = Runtime::new(Rc::new(init_code), Rc::new(vec![]), context, self.config); Capture::Trap(StackExecutorCreateInterrupt(TaggedRuntime { kind: RuntimeKind::Create(address), @@ -882,7 +877,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> ( $e:expr ) => { match $e { Ok(v) => v, - Err(e) => return Capture::Exit((e.into(), Vec::new())), + Err(e) => return Capture::Exit((e.into(), vec![])), } }; } @@ -932,7 +927,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> if let Some(depth) = self.state.metadata().depth { if depth > self.config.call_stack_limit { let _ = self.exit_substate(StackExitKind::Reverted); - return Capture::Exit((ExitError::CallTooDeep.into(), Vec::new())); + return Capture::Exit((ExitError::CallTooDeep.into(), vec![])); } } @@ -941,7 +936,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Ok(()) => (), Err(e) => { let _ = self.exit_substate(StackExitKind::Reverted); - return Capture::Exit((ExitReason::Error(e), Vec::new())); + return Capture::Exit((ExitReason::Error(e), vec![])); } } } @@ -968,7 +963,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> } Err(PrecompileFailure::Error { exit_status }) => { let _ = self.exit_substate(StackExitKind::Failed); - Capture::Exit((ExitReason::Error(exit_status), Vec::new())) + Capture::Exit((ExitReason::Error(exit_status), vec![])) } Err(PrecompileFailure::Revert { exit_status, @@ -980,7 +975,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Err(PrecompileFailure::Fatal { exit_status }) => { self.state.metadata_mut().gasometer.fail(); let _ = self.exit_substate(StackExitKind::Failed); - Capture::Exit((ExitReason::Fatal(exit_status), Vec::new())) + Capture::Exit((ExitReason::Fatal(exit_status), vec![])) } }; } @@ -1017,14 +1012,14 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> if let Err(e) = check_first_byte(self.config, &out) { self.state.metadata_mut().gasometer.fail(); let _ = self.exit_substate(StackExitKind::Failed); - return (e.into(), None, Vec::new()); + return (e.into(), None, vec![]); } if let Some(limit) = self.config.create_contract_limit { if out.len() > limit { self.state.metadata_mut().gasometer.fail(); let _ = self.exit_substate(StackExitKind::Failed); - return (ExitError::CreateContractLimit.into(), None, Vec::new()); + return (ExitError::CreateContractLimit.into(), None, vec![]); } } @@ -1038,20 +1033,20 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> let exit_result = self.exit_substate(StackExitKind::Succeeded); self.state.set_code(address, out); if let Err(e) = exit_result { - return (e.into(), None, Vec::new()); + return (e.into(), None, vec![]); } - (ExitReason::Succeed(s), Some(address), Vec::new()) + (ExitReason::Succeed(s), Some(address), vec![]) } Err(e) => { let _ = self.exit_substate(StackExitKind::Failed); - (ExitReason::Error(e), None, Vec::new()) + (ExitReason::Error(e), None, vec![]) } } } ExitReason::Error(e) => { self.state.metadata_mut().gasometer.fail(); let _ = self.exit_substate(StackExitKind::Failed); - (ExitReason::Error(e), None, Vec::new()) + (ExitReason::Error(e), None, vec![]) } ExitReason::Revert(e) => { let _ = self.exit_substate(StackExitKind::Reverted); @@ -1060,7 +1055,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> ExitReason::Fatal(e) => { self.state.metadata_mut().gasometer.fail(); let _ = self.exit_substate(StackExitKind::Failed); - (ExitReason::Fatal(e), None, Vec::new()) + (ExitReason::Fatal(e), None, vec![]) } } } @@ -1079,7 +1074,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> } ExitReason::Error(_) => { let _ = self.exit_substate(StackExitKind::Failed); - Vec::new() + vec![] } ExitReason::Revert(_) => { let _ = self.exit_substate(StackExitKind::Reverted); @@ -1088,7 +1083,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> ExitReason::Fatal(_) => { self.state.metadata_mut().gasometer.fail(); let _ = self.exit_substate(StackExitKind::Failed); - Vec::new() + vec![] } } } @@ -1386,7 +1381,7 @@ impl<'inner, 'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Pr .gasometer .record_dynamic_cost(gas_cost, memory_cost) { - return (ExitReason::Error(error), Vec::new()); + return (ExitReason::Error(error), vec![]); } event!(PrecompileSubcall { diff --git a/src/executor/stack/memory.rs b/src/executor/stack/memory.rs index 0ec07eabf..ed0a90b3a 100644 --- a/src/executor/stack/memory.rs +++ b/src/executor/stack/memory.rs @@ -31,7 +31,7 @@ impl<'config> MemoryStackSubstate<'config> { Self { metadata, parent: None, - logs: Vec::new(), + logs: vec![], accounts: BTreeMap::new(), storages: BTreeMap::new(), deletes: BTreeSet::new(), @@ -116,7 +116,7 @@ impl<'config> MemoryStackSubstate<'config> { let mut entering = Self { metadata: self.metadata.spit_child(gas_limit, is_static), parent: None, - logs: Vec::new(), + logs: vec![], accounts: BTreeMap::new(), storages: BTreeMap::new(), deletes: BTreeSet::new(), @@ -310,7 +310,7 @@ impl<'config> MemoryStackSubstate<'config> { } pub fn reset_storage(&mut self, address: H160, backend: &B) { - let mut removing = Vec::new(); + let mut removing = vec![]; for (oa, ok) in self.storages.keys() { if *oa == address { From c61527bb7f719acf4c1dbd3e48357973f21e3f5f Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Wed, 4 Jan 2023 14:07:37 -0800 Subject: [PATCH 3/6] use Self where possible --- core/src/opcode.rs | 288 ++++++++++++++++++++++----------------------- core/src/utils.rs | 50 ++++---- runtime/src/lib.rs | 16 +-- 3 files changed, 177 insertions(+), 177 deletions(-) diff --git a/core/src/opcode.rs b/core/src/opcode.rs index c35a6ec5e..6b4e46479 100644 --- a/core/src/opcode.rs +++ b/core/src/opcode.rs @@ -10,242 +10,242 @@ pub struct Opcode(pub u8); // Core opcodes. impl Opcode { /// `STOP` - pub const STOP: Opcode = Opcode(0x00); + pub const STOP: Self = Self(0x00); /// `ADD` - pub const ADD: Opcode = Opcode(0x01); + pub const ADD: Self = Self(0x01); /// `MUL` - pub const MUL: Opcode = Opcode(0x02); + pub const MUL: Self = Self(0x02); /// `SUB` - pub const SUB: Opcode = Opcode(0x03); + pub const SUB: Self = Self(0x03); /// `DIV` - pub const DIV: Opcode = Opcode(0x04); + pub const DIV: Self = Self(0x04); /// `SDIV` - pub const SDIV: Opcode = Opcode(0x05); + pub const SDIV: Self = Self(0x05); /// `MOD` - pub const MOD: Opcode = Opcode(0x06); + pub const MOD: Self = Self(0x06); /// `SMOD` - pub const SMOD: Opcode = Opcode(0x07); + pub const SMOD: Self = Self(0x07); /// `ADDMOD` - pub const ADDMOD: Opcode = Opcode(0x08); + pub const ADDMOD: Self = Self(0x08); /// `MULMOD` - pub const MULMOD: Opcode = Opcode(0x09); + pub const MULMOD: Self = Self(0x09); /// `EXP` - pub const EXP: Opcode = Opcode(0x0a); + pub const EXP: Self = Self(0x0a); /// `SIGNEXTEND` - pub const SIGNEXTEND: Opcode = Opcode(0x0b); + pub const SIGNEXTEND: Self = Self(0x0b); /// `LT` - pub const LT: Opcode = Opcode(0x10); + pub const LT: Self = Self(0x10); /// `GT` - pub const GT: Opcode = Opcode(0x11); + pub const GT: Self = Self(0x11); /// `SLT` - pub const SLT: Opcode = Opcode(0x12); + pub const SLT: Self = Self(0x12); /// `SGT` - pub const SGT: Opcode = Opcode(0x13); + pub const SGT: Self = Self(0x13); /// `EQ` - pub const EQ: Opcode = Opcode(0x14); + pub const EQ: Self = Self(0x14); /// `ISZERO` - pub const ISZERO: Opcode = Opcode(0x15); + pub const ISZERO: Self = Self(0x15); /// `AND` - pub const AND: Opcode = Opcode(0x16); + pub const AND: Self = Self(0x16); /// `OR` - pub const OR: Opcode = Opcode(0x17); + pub const OR: Self = Self(0x17); /// `XOR` - pub const XOR: Opcode = Opcode(0x18); + pub const XOR: Self = Self(0x18); /// `NOT` - pub const NOT: Opcode = Opcode(0x19); + pub const NOT: Self = Self(0x19); /// `BYTE` - pub const BYTE: Opcode = Opcode(0x1a); + pub const BYTE: Self = Self(0x1a); /// `CALLDATALOAD` - pub const CALLDATALOAD: Opcode = Opcode(0x35); + pub const CALLDATALOAD: Self = Self(0x35); /// `CALLDATASIZE` - pub const CALLDATASIZE: Opcode = Opcode(0x36); + pub const CALLDATASIZE: Self = Self(0x36); /// `CALLDATACOPY` - pub const CALLDATACOPY: Opcode = Opcode(0x37); + pub const CALLDATACOPY: Self = Self(0x37); /// `CODESIZE` - pub const CODESIZE: Opcode = Opcode(0x38); + pub const CODESIZE: Self = Self(0x38); /// `CODECOPY` - pub const CODECOPY: Opcode = Opcode(0x39); + pub const CODECOPY: Self = Self(0x39); /// `SHL` - pub const SHL: Opcode = Opcode(0x1b); + pub const SHL: Self = Self(0x1b); /// `SHR` - pub const SHR: Opcode = Opcode(0x1c); + pub const SHR: Self = Self(0x1c); /// `SAR` - pub const SAR: Opcode = Opcode(0x1d); + pub const SAR: Self = Self(0x1d); /// `POP` - pub const POP: Opcode = Opcode(0x50); + pub const POP: Self = Self(0x50); /// `MLOAD` - pub const MLOAD: Opcode = Opcode(0x51); + pub const MLOAD: Self = Self(0x51); /// `MSTORE` - pub const MSTORE: Opcode = Opcode(0x52); + pub const MSTORE: Self = Self(0x52); /// `MSTORE8` - pub const MSTORE8: Opcode = Opcode(0x53); + pub const MSTORE8: Self = Self(0x53); /// `JUMP` - pub const JUMP: Opcode = Opcode(0x56); + pub const JUMP: Self = Self(0x56); /// `JUMPI` - pub const JUMPI: Opcode = Opcode(0x57); + pub const JUMPI: Self = Self(0x57); /// `PC` - pub const PC: Opcode = Opcode(0x58); + pub const PC: Self = Self(0x58); /// `MSIZE` - pub const MSIZE: Opcode = Opcode(0x59); + pub const MSIZE: Self = Self(0x59); /// `JUMPDEST` - pub const JUMPDEST: Opcode = Opcode(0x5b); + pub const JUMPDEST: Self = Self(0x5b); /// `PUSHn` - pub const PUSH1: Opcode = Opcode(0x60); - pub const PUSH2: Opcode = Opcode(0x61); - pub const PUSH3: Opcode = Opcode(0x62); - pub const PUSH4: Opcode = Opcode(0x63); - pub const PUSH5: Opcode = Opcode(0x64); - pub const PUSH6: Opcode = Opcode(0x65); - pub const PUSH7: Opcode = Opcode(0x66); - pub const PUSH8: Opcode = Opcode(0x67); - pub const PUSH9: Opcode = Opcode(0x68); - pub const PUSH10: Opcode = Opcode(0x69); - pub const PUSH11: Opcode = Opcode(0x6a); - pub const PUSH12: Opcode = Opcode(0x6b); - pub const PUSH13: Opcode = Opcode(0x6c); - pub const PUSH14: Opcode = Opcode(0x6d); - pub const PUSH15: Opcode = Opcode(0x6e); - pub const PUSH16: Opcode = Opcode(0x6f); - pub const PUSH17: Opcode = Opcode(0x70); - pub const PUSH18: Opcode = Opcode(0x71); - pub const PUSH19: Opcode = Opcode(0x72); - pub const PUSH20: Opcode = Opcode(0x73); - pub const PUSH21: Opcode = Opcode(0x74); - pub const PUSH22: Opcode = Opcode(0x75); - pub const PUSH23: Opcode = Opcode(0x76); - pub const PUSH24: Opcode = Opcode(0x77); - pub const PUSH25: Opcode = Opcode(0x78); - pub const PUSH26: Opcode = Opcode(0x79); - pub const PUSH27: Opcode = Opcode(0x7a); - pub const PUSH28: Opcode = Opcode(0x7b); - pub const PUSH29: Opcode = Opcode(0x7c); - pub const PUSH30: Opcode = Opcode(0x7d); - pub const PUSH31: Opcode = Opcode(0x7e); - pub const PUSH32: Opcode = Opcode(0x7f); + pub const PUSH1: Self = Self(0x60); + pub const PUSH2: Self = Self(0x61); + pub const PUSH3: Self = Self(0x62); + pub const PUSH4: Self = Self(0x63); + pub const PUSH5: Self = Self(0x64); + pub const PUSH6: Self = Self(0x65); + pub const PUSH7: Self = Self(0x66); + pub const PUSH8: Self = Self(0x67); + pub const PUSH9: Self = Self(0x68); + pub const PUSH10: Self = Self(0x69); + pub const PUSH11: Self = Self(0x6a); + pub const PUSH12: Self = Self(0x6b); + pub const PUSH13: Self = Self(0x6c); + pub const PUSH14: Self = Self(0x6d); + pub const PUSH15: Self = Self(0x6e); + pub const PUSH16: Self = Self(0x6f); + pub const PUSH17: Self = Self(0x70); + pub const PUSH18: Self = Self(0x71); + pub const PUSH19: Self = Self(0x72); + pub const PUSH20: Self = Self(0x73); + pub const PUSH21: Self = Self(0x74); + pub const PUSH22: Self = Self(0x75); + pub const PUSH23: Self = Self(0x76); + pub const PUSH24: Self = Self(0x77); + pub const PUSH25: Self = Self(0x78); + pub const PUSH26: Self = Self(0x79); + pub const PUSH27: Self = Self(0x7a); + pub const PUSH28: Self = Self(0x7b); + pub const PUSH29: Self = Self(0x7c); + pub const PUSH30: Self = Self(0x7d); + pub const PUSH31: Self = Self(0x7e); + pub const PUSH32: Self = Self(0x7f); /// `DUPn` - pub const DUP1: Opcode = Opcode(0x80); - pub const DUP2: Opcode = Opcode(0x81); - pub const DUP3: Opcode = Opcode(0x82); - pub const DUP4: Opcode = Opcode(0x83); - pub const DUP5: Opcode = Opcode(0x84); - pub const DUP6: Opcode = Opcode(0x85); - pub const DUP7: Opcode = Opcode(0x86); - pub const DUP8: Opcode = Opcode(0x87); - pub const DUP9: Opcode = Opcode(0x88); - pub const DUP10: Opcode = Opcode(0x89); - pub const DUP11: Opcode = Opcode(0x8a); - pub const DUP12: Opcode = Opcode(0x8b); - pub const DUP13: Opcode = Opcode(0x8c); - pub const DUP14: Opcode = Opcode(0x8d); - pub const DUP15: Opcode = Opcode(0x8e); - pub const DUP16: Opcode = Opcode(0x8f); + pub const DUP1: Self = Self(0x80); + pub const DUP2: Self = Self(0x81); + pub const DUP3: Self = Self(0x82); + pub const DUP4: Self = Self(0x83); + pub const DUP5: Self = Self(0x84); + pub const DUP6: Self = Self(0x85); + pub const DUP7: Self = Self(0x86); + pub const DUP8: Self = Self(0x87); + pub const DUP9: Self = Self(0x88); + pub const DUP10: Self = Self(0x89); + pub const DUP11: Self = Self(0x8a); + pub const DUP12: Self = Self(0x8b); + pub const DUP13: Self = Self(0x8c); + pub const DUP14: Self = Self(0x8d); + pub const DUP15: Self = Self(0x8e); + pub const DUP16: Self = Self(0x8f); /// `SWAPn` - pub const SWAP1: Opcode = Opcode(0x90); - pub const SWAP2: Opcode = Opcode(0x91); - pub const SWAP3: Opcode = Opcode(0x92); - pub const SWAP4: Opcode = Opcode(0x93); - pub const SWAP5: Opcode = Opcode(0x94); - pub const SWAP6: Opcode = Opcode(0x95); - pub const SWAP7: Opcode = Opcode(0x96); - pub const SWAP8: Opcode = Opcode(0x97); - pub const SWAP9: Opcode = Opcode(0x98); - pub const SWAP10: Opcode = Opcode(0x99); - pub const SWAP11: Opcode = Opcode(0x9a); - pub const SWAP12: Opcode = Opcode(0x9b); - pub const SWAP13: Opcode = Opcode(0x9c); - pub const SWAP14: Opcode = Opcode(0x9d); - pub const SWAP15: Opcode = Opcode(0x9e); - pub const SWAP16: Opcode = Opcode(0x9f); + pub const SWAP1: Self = Self(0x90); + pub const SWAP2: Self = Self(0x91); + pub const SWAP3: Self = Self(0x92); + pub const SWAP4: Self = Self(0x93); + pub const SWAP5: Self = Self(0x94); + pub const SWAP6: Self = Self(0x95); + pub const SWAP7: Self = Self(0x96); + pub const SWAP8: Self = Self(0x97); + pub const SWAP9: Self = Self(0x98); + pub const SWAP10: Self = Self(0x99); + pub const SWAP11: Self = Self(0x9a); + pub const SWAP12: Self = Self(0x9b); + pub const SWAP13: Self = Self(0x9c); + pub const SWAP14: Self = Self(0x9d); + pub const SWAP15: Self = Self(0x9e); + pub const SWAP16: Self = Self(0x9f); /// `RETURN` - pub const RETURN: Opcode = Opcode(0xf3); + pub const RETURN: Self = Self(0xf3); /// `REVERT` - pub const REVERT: Opcode = Opcode(0xfd); + pub const REVERT: Self = Self(0xfd); /// `INVALID` - pub const INVALID: Opcode = Opcode(0xfe); + pub const INVALID: Self = Self(0xfe); /// See [EIP-3541](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3541.md) - pub const EOFMAGIC: Opcode = Opcode(0xef); + pub const EOFMAGIC: Self = Self(0xef); } // External opcodes impl Opcode { /// `SHA3` - pub const SHA3: Opcode = Opcode(0x20); + pub const SHA3: Self = Self(0x20); /// `ADDRESS` - pub const ADDRESS: Opcode = Opcode(0x30); + pub const ADDRESS: Self = Self(0x30); /// `BALANCE` - pub const BALANCE: Opcode = Opcode(0x31); + pub const BALANCE: Self = Self(0x31); /// `SELFBALANCE` - pub const SELFBALANCE: Opcode = Opcode(0x47); + pub const SELFBALANCE: Self = Self(0x47); /// `BASEFEE` - pub const BASEFEE: Opcode = Opcode(0x48); + pub const BASEFEE: Self = Self(0x48); /// `ORIGIN` - pub const ORIGIN: Opcode = Opcode(0x32); + pub const ORIGIN: Self = Self(0x32); /// `CALLER` - pub const CALLER: Opcode = Opcode(0x33); + pub const CALLER: Self = Self(0x33); /// `CALLVALUE` - pub const CALLVALUE: Opcode = Opcode(0x34); + pub const CALLVALUE: Self = Self(0x34); /// `GASPRICE` - pub const GASPRICE: Opcode = Opcode(0x3a); + pub const GASPRICE: Self = Self(0x3a); /// `EXTCODESIZE` - pub const EXTCODESIZE: Opcode = Opcode(0x3b); + pub const EXTCODESIZE: Self = Self(0x3b); /// `EXTCODECOPY` - pub const EXTCODECOPY: Opcode = Opcode(0x3c); + pub const EXTCODECOPY: Self = Self(0x3c); /// `EXTCODEHASH` - pub const EXTCODEHASH: Opcode = Opcode(0x3f); + pub const EXTCODEHASH: Self = Self(0x3f); /// `RETURNDATASIZE` - pub const RETURNDATASIZE: Opcode = Opcode(0x3d); + pub const RETURNDATASIZE: Self = Self(0x3d); /// `RETURNDATACOPY` - pub const RETURNDATACOPY: Opcode = Opcode(0x3e); + pub const RETURNDATACOPY: Self = Self(0x3e); /// `BLOCKHASH` - pub const BLOCKHASH: Opcode = Opcode(0x40); + pub const BLOCKHASH: Self = Self(0x40); /// `COINBASE` - pub const COINBASE: Opcode = Opcode(0x41); + pub const COINBASE: Self = Self(0x41); /// `TIMESTAMP` - pub const TIMESTAMP: Opcode = Opcode(0x42); + pub const TIMESTAMP: Self = Self(0x42); /// `NUMBER` - pub const NUMBER: Opcode = Opcode(0x43); + pub const NUMBER: Self = Self(0x43); /// `DIFFICULTY` - pub const DIFFICULTY: Opcode = Opcode(0x44); + pub const DIFFICULTY: Self = Self(0x44); /// `GASLIMIT` - pub const GASLIMIT: Opcode = Opcode(0x45); + pub const GASLIMIT: Self = Self(0x45); /// `SLOAD` - pub const SLOAD: Opcode = Opcode(0x54); + pub const SLOAD: Self = Self(0x54); /// `SSTORE` - pub const SSTORE: Opcode = Opcode(0x55); + pub const SSTORE: Self = Self(0x55); /// `GAS` - pub const GAS: Opcode = Opcode(0x5a); + pub const GAS: Self = Self(0x5a); /// `LOGn` - pub const LOG0: Opcode = Opcode(0xa0); - pub const LOG1: Opcode = Opcode(0xa1); - pub const LOG2: Opcode = Opcode(0xa2); - pub const LOG3: Opcode = Opcode(0xa3); - pub const LOG4: Opcode = Opcode(0xa4); + pub const LOG0: Self = Self(0xa0); + pub const LOG1: Self = Self(0xa1); + pub const LOG2: Self = Self(0xa2); + pub const LOG3: Self = Self(0xa3); + pub const LOG4: Self = Self(0xa4); /// `CREATE` - pub const CREATE: Opcode = Opcode(0xf0); + pub const CREATE: Self = Self(0xf0); /// `CREATE2` - pub const CREATE2: Opcode = Opcode(0xf5); + pub const CREATE2: Self = Self(0xf5); /// `CALL` - pub const CALL: Opcode = Opcode(0xf1); + pub const CALL: Self = Self(0xf1); /// `CALLCODE` - pub const CALLCODE: Opcode = Opcode(0xf2); + pub const CALLCODE: Self = Self(0xf2); /// `DELEGATECALL` - pub const DELEGATECALL: Opcode = Opcode(0xf4); + pub const DELEGATECALL: Self = Self(0xf4); /// `STATICCALL` - pub const STATICCALL: Opcode = Opcode(0xfa); + pub const STATICCALL: Self = Self(0xfa); /// `SUICIDE` - pub const SUICIDE: Opcode = Opcode(0xff); + pub const SUICIDE: Self = Self(0xff); /// `CHAINID` - pub const CHAINID: Opcode = Opcode(0x46); + pub const CHAINID: Self = Self(0x46); } impl Opcode { diff --git a/core/src/utils.rs b/core/src/utils.rs index d260ccf47..d28b6af28 100644 --- a/core/src/utils.rs +++ b/core/src/utils.rs @@ -21,17 +21,17 @@ pub struct I256(pub Sign, pub U256); impl I256 { /// Zero value of I256. - pub fn zero() -> I256 { - I256(Sign::Zero, U256::zero()) + pub fn zero() -> Self { + Self(Sign::Zero, U256::zero()) } /// Minimum value of I256. - pub fn min_value() -> I256 { - I256(Sign::Minus, (U256::MAX & SIGN_BIT_MASK) + U256::from(1u64)) + pub fn min_value() -> Self { + Self(Sign::Minus, (U256::MAX & SIGN_BIT_MASK) + U256::from(1u64)) } } impl Ord for I256 { - fn cmp(&self, other: &I256) -> Ordering { + fn cmp(&self, other: &Self) -> Ordering { match (self.0, other.0) { (Sign::Zero, Sign::Zero) => Ordering::Equal, (Sign::Zero, Sign::Plus) => Ordering::Less, @@ -47,25 +47,25 @@ impl Ord for I256 { } impl PartialOrd for I256 { - fn partial_cmp(&self, other: &I256) -> Option { + fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Default for I256 { - fn default() -> I256 { - I256::zero() + fn default() -> Self { + Self::zero() } } impl From for I256 { - fn from(val: U256) -> I256 { + fn from(val: U256) -> Self { if val == U256::zero() { - I256::zero() + Self::zero() } else if val & SIGN_BIT_MASK == val { - I256(Sign::Plus, val) + Self(Sign::Plus, val) } else { - I256(Sign::Minus, !val + U256::from(1u64)) + Self(Sign::Minus, !val + U256::from(1u64)) } } } @@ -84,21 +84,21 @@ impl From for U256 { } impl Div for I256 { - type Output = I256; + type Output = Self; - fn div(self, other: I256) -> I256 { - if other == I256::zero() { - return I256::zero(); + fn div(self, other: Self) -> Self { + if other == Self::zero() { + return Self::zero(); } - if self == I256::min_value() && other.1 == U256::from(1u64) { - return I256::min_value(); + if self == Self::min_value() && other.1 == U256::from(1u64) { + return Self::min_value(); } let d = (self.1 / other.1) & SIGN_BIT_MASK; if d == U256::zero() { - return I256::zero(); + return Self::zero(); } match (self.0, other.0) { @@ -106,26 +106,26 @@ impl Div for I256 { | (Sign::Plus, Sign::Zero) | (Sign::Zero, Sign::Zero) | (Sign::Plus, Sign::Plus) - | (Sign::Minus, Sign::Minus) => I256(Sign::Plus, d), + | (Sign::Minus, Sign::Minus) => Self(Sign::Plus, d), (Sign::Zero, Sign::Minus) | (Sign::Plus, Sign::Minus) | (Sign::Minus, Sign::Zero) - | (Sign::Minus, Sign::Plus) => I256(Sign::Minus, d), + | (Sign::Minus, Sign::Plus) => Self(Sign::Minus, d), } } } impl Rem for I256 { - type Output = I256; + type Output = Self; - fn rem(self, other: I256) -> I256 { + fn rem(self, other: Self) -> Self { let r = (self.1 % other.1) & SIGN_BIT_MASK; if r == U256::zero() { - return I256::zero(); + return Self::zero(); } - I256(self.0, r) + Self(self.0, r) } } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 5aff95e24..918e90393 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -287,8 +287,8 @@ pub struct Config { impl Config { /// Frontier hard fork configuration. - pub const fn frontier() -> Config { - Config { + pub const fn frontier() -> Self { + Self { gas_ext_code: 20, gas_ext_code_hash: 20, gas_balance: 20, @@ -338,8 +338,8 @@ impl Config { } /// Istanbul hard fork configuration. - pub const fn istanbul() -> Config { - Config { + pub const fn istanbul() -> Self { + Self { gas_ext_code: 700, gas_ext_code_hash: 700, gas_balance: 700, @@ -389,16 +389,16 @@ impl Config { } /// Berlin hard fork configuration. - pub const fn berlin() -> Config { + pub const fn berlin() -> Self { Self::config_with_derived_values(DerivedConfigInputs::berlin()) } /// london hard fork configuration. - pub const fn london() -> Config { + pub const fn london() -> Self { Self::config_with_derived_values(DerivedConfigInputs::london()) } - const fn config_with_derived_values(inputs: DerivedConfigInputs) -> Config { + const fn config_with_derived_values(inputs: DerivedConfigInputs) -> Self { let DerivedConfigInputs { gas_storage_read_warm, gas_sload_cold, @@ -420,7 +420,7 @@ impl Config { }; let max_refund_quotient = if decrease_clears_refund { 5 } else { 2 }; - Config { + Self { gas_ext_code: 0, gas_ext_code_hash: 0, gas_balance: 0, From 708a4ec2932efcbb5e55d6cee354092883984f97 Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Wed, 4 Jan 2023 14:11:38 -0800 Subject: [PATCH 4/6] use into where possible --- core/src/eval/macros.rs | 4 ++-- core/src/eval/misc.rs | 2 +- core/src/lib.rs | 4 ++-- core/src/memory.rs | 11 ++++------- gasometer/src/lib.rs | 2 +- runtime/src/eval/macros.rs | 4 ++-- 6 files changed, 12 insertions(+), 15 deletions(-) diff --git a/core/src/eval/macros.rs b/core/src/eval/macros.rs index 3fd7a9f6e..430042e5a 100644 --- a/core/src/eval/macros.rs +++ b/core/src/eval/macros.rs @@ -115,7 +115,7 @@ 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()); } @@ -123,7 +123,7 @@ macro_rules! as_usize_or_fail { }}; ( $v:expr, $reason:expr ) => {{ - if $v > U256::from(usize::MAX) { + if $v > usize::MAX.into() { return Control::Exit($reason.into()); } diff --git a/core/src/eval/misc.rs b/core/src/eval/misc.rs index 8271d5f6c..b198dbdce 100644 --- a/core/src/eval/misc.rs +++ b/core/src/eval/misc.rs @@ -32,7 +32,7 @@ pub fn calldataload(state: &mut Machine) -> Control { #[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 p <= usize::MAX.into() { let p = p.as_usize(); if p < state.data.len() { load[i] = state.data[p]; diff --git a/core/src/lib.rs b/core/src/lib.rs index 5c3380b2d..3c153d85c 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -103,10 +103,10 @@ impl Machine { /// Copy and get the return value of the machine, if any. pub fn return_value(&self) -> Vec { - if self.return_range.start > 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 > U256::from(usize::MAX) { + } 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(), diff --git a/core/src/memory.rs b/core/src/memory.rs index 4d9c674a2..7952b7952 100644 --- a/core/src/memory.rs +++ b/core/src/memory.rs @@ -149,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(); @@ -207,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 diff --git a/gasometer/src/lib.rs b/gasometer/src/lib.rs index 564a2d3e0..2d9de9b15 100644 --- a/gasometer/src/lib.rs +++ b/gasometer/src/lib.rs @@ -699,7 +699,7 @@ impl<'config> Inner<'config> { let end = from.checked_add(len).ok_or(ExitError::OutOfGas)?; - if end > U256::from(usize::MAX) { + if end > usize::MAX.into() { return Err(ExitError::OutOfGas); } let end = end.as_usize(); diff --git a/runtime/src/eval/macros.rs b/runtime/src/eval/macros.rs index 076866fba..33107f3db 100644 --- a/runtime/src/eval/macros.rs +++ b/runtime/src/eval/macros.rs @@ -55,7 +55,7 @@ macro_rules! push_u256 { 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()); } @@ -63,7 +63,7 @@ macro_rules! as_usize_or_fail { }}; ( $v:expr, $reason:expr ) => {{ - if $v > U256::from(usize::MAX) { + if $v > usize::MAX.into() { return Control::Exit($reason.into()); } From d4e09ff16704eaee67797ddd8b186df12d577506 Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Wed, 4 Jan 2023 14:13:59 -0800 Subject: [PATCH 5/6] use is_zero where possible --- core/src/eval/arithmetic.rs | 6 +++--- core/src/eval/bitwise.rs | 6 +++--- core/src/eval/misc.rs | 2 +- core/src/memory.rs | 2 +- core/src/utils.rs | 6 +++--- gasometer/src/costs.rs | 10 +++++----- gasometer/src/lib.rs | 8 ++++---- runtime/src/eval/system.rs | 8 ++++---- src/backend/memory.rs | 5 ++--- src/executor/stack/memory.rs | 8 ++++---- 10 files changed, 30 insertions(+), 31 deletions(-) diff --git a/core/src/eval/arithmetic.rs b/core/src/eval/arithmetic.rs index ae6e9a15c..4bcd7f2f6 100644 --- a/core/src/eval/arithmetic.rs +++ b/core/src/eval/arithmetic.rs @@ -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 @@ -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) @@ -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(); diff --git a/core/src/eval/bitwise.rs b/core/src/eval/bitwise.rs index 49883fd1c..dc1dbc0ab 100644 --- a/core/src/eval/bitwise.rs +++ b/core/src/eval/bitwise.rs @@ -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() @@ -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 >= U256::from(256) { U256::zero() } else { let shift: u64 = shift.as_u64(); @@ -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 >= U256::from(256) { U256::zero() } else { let shift: u64 = shift.as_u64(); diff --git a/core/src/eval/misc.rs b/core/src/eval/misc.rs index b198dbdce..9c2c08a17 100644 --- a/core/src/eval/misc.rs +++ b/core/src/eval/misc.rs @@ -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); } diff --git a/core/src/memory.rs b/core/src/memory.rs index 7952b7952..a9ce1d214 100644 --- a/core/src/memory.rs +++ b/core/src/memory.rs @@ -52,7 +52,7 @@ impl Memory { /// + 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(()); } diff --git a/core/src/utils.rs b/core/src/utils.rs index d28b6af28..01dd2f474 100644 --- a/core/src/utils.rs +++ b/core/src/utils.rs @@ -60,7 +60,7 @@ impl Default for I256 { impl From for I256 { fn from(val: U256) -> Self { - if val == U256::zero() { + if val.is_zero() { Self::zero() } else if val & SIGN_BIT_MASK == val { Self(Sign::Plus, val) @@ -97,7 +97,7 @@ impl Div for I256 { let d = (self.1 / other.1) & SIGN_BIT_MASK; - if d == U256::zero() { + if d.is_zero() { return Self::zero(); } @@ -121,7 +121,7 @@ impl Rem for I256 { fn rem(self, other: Self) -> Self { let r = (self.1 % other.1) & SIGN_BIT_MASK; - if r == U256::zero() { + if r.is_zero() { return Self::zero(); } diff --git a/gasometer/src/costs.rs b/gasometer/src/costs.rs index c25f5db9c..ef5bfa622 100644 --- a/gasometer/src/costs.rs +++ b/gasometer/src/costs.rs @@ -62,7 +62,7 @@ pub fn create2_cost(len: U256) -> Result { let base = U256::from(G_CREATE); // ceil(len / 32.0) let sha_addup_base = len / U256::from(32) - + if len % U256::from(32) == U256::zero() { + + if (len % U256::from(32)).is_zero() { U256::zero() } else { U256::one() @@ -80,7 +80,7 @@ pub fn create2_cost(len: U256) -> Result { } pub fn exp_cost(power: U256, config: &Config) -> Result { - if power == U256::zero() { + if power.is_zero() { Ok(G_EXP) } else { let gas = U256::from(G_EXP) @@ -106,7 +106,7 @@ pub fn verylowcopy_cost(len: U256) -> Result { let gas = U256::from(G_VERYLOW) .checked_add( U256::from(G_COPY) - .checked_mul(if wordr == U256::zero() { + .checked_mul(if wordr.is_zero() { wordd } else { wordd + U256::one() @@ -128,7 +128,7 @@ pub fn extcodecopy_cost(len: U256, is_cold: bool, config: &Config) -> Result Result { let gas = U256::from(G_SHA3) .checked_add( U256::from(G_SHA3WORD) - .checked_mul(if wordr == U256::zero() { + .checked_mul(if wordr.is_zero() { wordd } else { wordd + U256::one() diff --git a/gasometer/src/lib.rs b/gasometer/src/lib.rs index 2d9de9b15..d32834985 100644 --- a/gasometer/src/lib.rs +++ b/gasometer/src/lib.rs @@ -598,7 +598,7 @@ pub fn dynamic_opcode_cost( } Opcode::CALL if !is_static - || (is_static && U256::from_big_endian(&stack.peek(2)?[..]) == U256::zero()) => + || (is_static && U256::from_big_endian(&stack.peek(2)?[..]).is_zero()) => { let target = stack.peek(1)?.into(); storage_target = StorageTarget::Address(target); @@ -693,7 +693,7 @@ impl<'config> Inner<'config> { let from = memory.offset; let len = memory.len; - if len == U256::zero() { + if len.is_zero() { return Ok(self.memory_gas); } @@ -1027,11 +1027,11 @@ pub enum TransactionCost { impl MemoryCost { /// Join two memory cost together. pub fn join(self, other: MemoryCost) -> MemoryCost { - if self.len == U256::zero() { + if self.len.is_zero() { return other; } - if other.len == U256::zero() { + if other.len.is_zero() { return self; } diff --git a/runtime/src/eval/system.rs b/runtime/src/eval/system.rs index 6aeba6e0f..1d686fe13 100644 --- a/runtime/src/eval/system.rs +++ b/runtime/src/eval/system.rs @@ -11,7 +11,7 @@ pub fn sha3(runtime: &mut Runtime) -> Control { pop_u256!(runtime, from, len); try_or_fail!(runtime.machine.memory_mut().resize_offset(from, len)); - let data = if len == U256::zero() { + let data = if len.is_zero() { vec![] } else { let from = as_usize_or_fail!(from); @@ -229,7 +229,7 @@ pub fn log(runtime: &mut Runtime, n: u8, handler: &mut H) -> Control pop_u256!(runtime, offset, len); try_or_fail!(runtime.machine.memory_mut().resize_offset(offset, len)); - let data = if len == U256::zero() { + let data = if len.is_zero() { Vec::new() } else { let offset = as_usize_or_fail!(offset); @@ -271,7 +271,7 @@ pub fn create(runtime: &mut Runtime, is_create2: bool, handler: &mut pop_u256!(runtime, value, code_offset, len); try_or_fail!(runtime.machine.memory_mut().resize_offset(code_offset, len)); - let code = if len == U256::zero() { + let code = if len.is_zero() { Vec::new() } else { let code_offset = as_usize_or_fail!(code_offset); @@ -335,7 +335,7 @@ pub fn call(runtime: &mut Runtime, scheme: CallScheme, handler: &mut .memory_mut() .resize_offset(out_offset, out_len)); - let input = if in_len == U256::zero() { + let input = if in_len.is_zero() { Vec::new() } else { let in_offset = as_usize_or_fail!(in_offset); diff --git a/src/backend/memory.rs b/src/backend/memory.rs index f4538b481..677ad4ce1 100644 --- a/src/backend/memory.rs +++ b/src/backend/memory.rs @@ -201,9 +201,8 @@ impl<'vicinity> ApplyBackend for MemoryBackend<'vicinity> { } } - account.balance == U256::zero() - && account.nonce == U256::zero() - && account.code.is_empty() + account.balance.is_zero() + && account.nonce.is_zero() && account.code.is_empty() }; if is_empty && delete_empty { diff --git a/src/executor/stack/memory.rs b/src/executor/stack/memory.rs index ed0a90b3a..f8e455547 100644 --- a/src/executor/stack/memory.rs +++ b/src/executor/stack/memory.rs @@ -204,8 +204,8 @@ impl<'config> MemoryStackSubstate<'config> { if let Some(code) = &account.code { return Some( - account.basic.balance == U256::zero() - && account.basic.nonce == U256::zero() + account.basic.balance.is_zero() + && account.basic.nonce.is_zero() && code.is_empty(), ); } @@ -493,8 +493,8 @@ impl<'backend, 'config, B: Backend> StackState<'config> for MemoryStackState<'ba return known_empty; } - self.backend.basic(address).balance == U256::zero() - && self.backend.basic(address).nonce == U256::zero() + self.backend.basic(address).balance.is_zero() + && self.backend.basic(address).nonce.is_zero() && self.backend.code(address).len() == 0 } From 02cb7b39fb0ba2a69f7c90303e4282389951e930 Mon Sep 17 00:00:00 2001 From: adamnemecek Date: Wed, 4 Jan 2023 14:19:10 -0800 Subject: [PATCH 6/6] refactoring --- core/src/eval/bitwise.rs | 6 +++--- core/src/eval/misc.rs | 8 ++++---- gasometer/src/costs.rs | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/eval/bitwise.rs b/core/src/eval/bitwise.rs index dc1dbc0ab..500da2230 100644 --- a/core/src/eval/bitwise.rs +++ b/core/src/eval/bitwise.rs @@ -58,7 +58,7 @@ pub fn byte(op1: U256, op2: U256) -> U256 { #[inline] pub fn shl(shift: U256, value: U256) -> U256 { - if value.is_zero() || shift >= U256::from(256) { + if value.is_zero() || shift >= 256.into() { U256::zero() } else { let shift: u64 = shift.as_u64(); @@ -68,7 +68,7 @@ pub fn shl(shift: U256, value: U256) -> U256 { #[inline] pub fn shr(shift: U256, value: U256) -> U256 { - if value.is_zero() || shift >= U256::from(256) { + if value.is_zero() || shift >= 256.into() { U256::zero() } else { let shift: u64 = shift.as_u64(); @@ -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 diff --git a/core/src/eval/misc.rs b/core/src/eval/misc.rs index 9c2c08a17..6791e21be 100644 --- a/core/src/eval/misc.rs +++ b/core/src/eval/misc.rs @@ -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(); push_u256!(state, size); Control::Continue(1) } @@ -31,7 +31,7 @@ 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 let Some(p) = index.checked_add(i.into()) { if p <= usize::MAX.into() { let p = p.as_usize(); if p < state.data.len() { @@ -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) } diff --git a/gasometer/src/costs.rs b/gasometer/src/costs.rs index ef5bfa622..5f279d144 100644 --- a/gasometer/src/costs.rs +++ b/gasometer/src/costs.rs @@ -72,7 +72,7 @@ pub fn create2_cost(len: U256) -> Result { .ok_or(ExitError::OutOfGas)?; let gas = base.checked_add(sha_addup).ok_or(ExitError::OutOfGas)?; - if gas > U256::from(u64::MAX) { + if gas > u64::MAX.into() { return Err(ExitError::OutOfGas); }