From f0268893511e496ae56234f8fddda33c50e77b04 Mon Sep 17 00:00:00 2001 From: Maxim Andreev Date: Mon, 28 Oct 2024 09:51:42 +0000 Subject: [PATCH] make CallDataImpl non clonable --- src/arguments/calldata.rs | 1 - src/arguments/mod.rs | 8 ++------ src/evm/vm.rs | 25 ++++++++++++++++++------- src/selectors/calldata.rs | 1 - src/selectors/mod.rs | 2 +- src/state_mutability/calldata.rs | 1 - src/state_mutability/mod.rs | 12 ++++-------- src/utils.rs | 2 +- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/arguments/calldata.rs b/src/arguments/calldata.rs index 624f2a0..23cd8d7 100644 --- a/src/arguments/calldata.rs +++ b/src/arguments/calldata.rs @@ -2,7 +2,6 @@ use super::Label; use crate::evm::{calldata::CallData, element::Element, U256, VAL_4, VAL_131072}; use std::error; -#[derive(Clone)] pub(super) struct CallDataImpl { pub selector: [u8; 4], } diff --git a/src/arguments/mod.rs b/src/arguments/mod.rs index d2f110e..7e5e54d 100644 --- a/src/arguments/mod.rs +++ b/src/arguments/mod.rs @@ -561,12 +561,8 @@ pub fn function_arguments_alloy( selector[0], selector[1], selector[2], selector[3] ); } - let mut vm = Vm::new( - code, - CallDataImpl { - selector: *selector, - }, - ); + let calldata = CallDataImpl { selector: *selector }; + let mut vm = Vm::new(code, &calldata); let mut args = ArgsResult::new(); let mut gas_used = 0; let real_gas_limit = if gas_limit == 0 { diff --git a/src/evm/vm.rs b/src/evm/vm.rs index 389d137..9161796 100644 --- a/src/evm/vm.rs +++ b/src/evm/vm.rs @@ -34,24 +34,23 @@ impl StepResult { } } -#[derive(Clone)] pub struct Vm<'a, T, U> where T: Clone + std::fmt::Debug, - U: Clone + CallData, + U: CallData, { pub code: &'a [u8], pub pc: usize, pub stack: Stack, pub memory: Memory, pub stopped: bool, - pub calldata: U, + pub calldata: &'a U, } impl<'a, T, U> fmt::Debug for Vm<'a, T, U> where T: Clone + std::fmt::Debug, - U: Clone + CallData, + U: CallData, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( @@ -72,9 +71,9 @@ where impl<'a, T, U> Vm<'a, T, U> where T: std::fmt::Debug + Clone + Eq, - U: Clone + CallData, + U: CallData, { - pub fn new(code: &'a [u8], calldata: U) -> Self { + pub fn new(code: &'a [u8], calldata: &'a U) -> Self { Self { code, pc: 0, @@ -85,6 +84,18 @@ where } } + // not Clone trait because Cow experiments + pub fn clone(&'a self) -> Self { + Vm { + code: self.code, + pc: self.pc, + stack: self.stack.clone(), + memory: self.memory.clone(), + stopped: self.stopped, + calldata: self.calldata, + } + } + pub fn step(&mut self) -> Result, Box> { let op = self.code[self.pc]; let ret = self.exec_opcode(op)?; @@ -647,7 +658,7 @@ mod tests { #[test] fn test_arithmetic() { - let mut vm = Vm::new(&[], DummyCallData {}); + let mut vm = Vm::new(&[], &DummyCallData {}); let cases = [ ( I256::unchecked_from(-1).into_raw(), diff --git a/src/selectors/calldata.rs b/src/selectors/calldata.rs index e24893d..2d493c9 100644 --- a/src/selectors/calldata.rs +++ b/src/selectors/calldata.rs @@ -2,7 +2,6 @@ use super::Label; use crate::evm::{calldata::CallData, element::Element, U256, VAL_4}; use std::error; -#[derive(Clone)] pub(super) struct CallDataImpl {} impl CallData