diff --git a/crates/primitives/src/state.rs b/crates/primitives/src/state.rs index 6a69b93044..fd9362fcaf 100644 --- a/crates/primitives/src/state.rs +++ b/crates/primitives/src/state.rs @@ -20,11 +20,12 @@ bitflags! { #[cfg_attr(feature = "serde", serde(transparent))] pub struct AccountStatus: u8 { /// When account is loaded but not touched or interacted with. + /// This is the default state. const Loaded = 0b00000000; /// When account is newly created we will not access database /// to fetch storage values const Created = 0b00000001; - /// If account is marked for self destruct. + /// If account is marked for self destruction. const SelfDestructed = 0b00000010; /// Only when account is marked as touched we will save it to database. const Touched = 0b00000100; diff --git a/crates/revm/src/evm_impl.rs b/crates/revm/src/evm_impl.rs index 88d443a734..985997cd74 100644 --- a/crates/revm/src/evm_impl.rs +++ b/crates/revm/src/evm_impl.rs @@ -223,7 +223,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact context, is_static: false, }; - let (exit, gas, bytes) = self.call_inner(&mut call_input); + let (exit, gas, bytes) = self.call(&mut call_input); (exit, gas, Output::Call(bytes)) } else { ( @@ -241,7 +241,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> Transact init_code: data, gas_limit, }; - let (exit, address, ret_gas, bytes) = self.create_inner(&mut create_input); + let (exit, address, ret_gas, bytes) = self.create(&mut create_input); (exit, ret_gas, Output::Create(bytes, address)) } }; @@ -471,48 +471,27 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, } } + //fn call_with_inspector() + fn create_inner( &mut self, - inputs: &mut CreateInputs, + inputs: &CreateInputs, ) -> (InstructionResult, Option, Gas, Bytes) { - // Call inspector - if INSPECT { - let (ret, address, gas, out) = self.inspector.create(&mut self.data, inputs); - if ret != InstructionResult::Continue { - return self - .inspector - .create_end(&mut self.data, inputs, ret, address, gas, out); - } - } - let gas = Gas::new(inputs.gas_limit); self.load_account(inputs.caller); // Check depth of calls if self.data.journaled_state.depth() > CALL_STACK_LIMIT { - return self.create_end( - inputs, - InstructionResult::CallTooDeep, - None, - gas, - Bytes::new(), - ); + return (InstructionResult::CallTooDeep, None, gas, Bytes::new()); } // Check balance of caller and value. Do this before increasing nonce match self.balance(inputs.caller) { Some(i) if i.0 < inputs.value => { - return self.create_end( - inputs, - InstructionResult::OutOfFund, - None, - gas, - Bytes::new(), - ) + return (InstructionResult::OutOfFund, None, gas, Bytes::new()) } Some(_) => (), _ => { - return self.create_end( - inputs, + return ( InstructionResult::FatalExternalError, None, gas, @@ -526,7 +505,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, if let Some(nonce) = self.data.journaled_state.inc_nonce(inputs.caller) { old_nonce = nonce - 1; } else { - return self.create_end(inputs, InstructionResult::Return, None, gas, Bytes::new()); + return (InstructionResult::Return, None, gas, Bytes::new()); } // Create address @@ -546,13 +525,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, // Create contract account and check for collision if !self.data.journaled_state.create_account(created_address) { self.data.journaled_state.checkpoint_revert(checkpoint); - return self.create_end( - inputs, - InstructionResult::CreateCollision, - ret, - gas, - Bytes::new(), - ); + return (InstructionResult::CreateCollision, ret, gas, Bytes::new()); } // Transfer value to contract address @@ -563,7 +536,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, self.data.db, ) { self.data.journaled_state.checkpoint_revert(checkpoint); - return self.create_end(inputs, e, ret, gas, Bytes::new()); + return (e, ret, gas, Bytes::new()); } // EIP-161: State trie clearing (invariant-preserving alternative) @@ -576,7 +549,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, { // overflow self.data.journaled_state.checkpoint_revert(checkpoint); - return self.create_end(inputs, InstructionResult::Return, None, gas, Bytes::new()); + return (InstructionResult::Return, None, gas, Bytes::new()); } // Create new interpreter and execute initcode @@ -609,7 +582,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, interpreter.run::(self) }; // Host error if present on execution\ - let (ret, address, gas, out) = match exit_reason { + match exit_reason { return_ok!() => { // if ok, check contract creation limit and calculate gas deduction on output len. let mut bytes = interpreter.return_value(); @@ -617,8 +590,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, // EIP-3541: Reject new contract code starting with the 0xEF byte if GSPEC::enabled(LONDON) && !bytes.is_empty() && bytes.first() == Some(&0xEF) { self.data.journaled_state.checkpoint_revert(checkpoint); - return self.create_end( - inputs, + return ( InstructionResult::CreateContractStartingWithEF, ret, interpreter.gas, @@ -638,8 +610,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, .unwrap_or(MAX_CODE_SIZE) { self.data.journaled_state.checkpoint_revert(checkpoint); - return self.create_end( - inputs, + return ( InstructionResult::CreateContractSizeLimit, ret, interpreter.gas, @@ -655,13 +626,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, // creation fails (i.e. goes out-of-gas) rather than leaving an empty contract. if GSPEC::enabled(HOMESTEAD) { self.data.journaled_state.checkpoint_revert(checkpoint); - return self.create_end( - inputs, - InstructionResult::OutOfGas, - ret, - interpreter.gas, - bytes, - ); + return (InstructionResult::OutOfGas, ret, interpreter.gas, bytes); } else { bytes = Bytes::new(); } @@ -689,45 +654,10 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, interpreter.return_value(), ) } - }; - - self.create_end(inputs, ret, address, gas, out) - } - - fn create_end( - &mut self, - inputs: &CreateInputs, - ret: InstructionResult, - address: Option, - gas: Gas, - out: Bytes, - ) -> (InstructionResult, Option, Gas, Bytes) { - if INSPECT { - self.inspector - .create_end(&mut self.data, inputs, ret, address, gas, out) - } else { - (ret, address, gas, out) } } fn call_inner(&mut self, inputs: &mut CallInputs) -> (InstructionResult, Gas, Bytes) { - // Call the inspector - if INSPECT { - let (ret, gas, out) = self - .inspector - .call(&mut self.data, inputs, inputs.is_static); - if ret != InstructionResult::Continue { - return self.inspector.call_end( - &mut self.data, - inputs, - gas, - ret, - out, - inputs.is_static, - ); - } - } - let mut gas = Gas::new(inputs.gas_limit); // Load account and get code. Account is now hot. let bytecode = if let Some((bytecode, _)) = self.code(inputs.contract) { @@ -738,19 +668,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, // Check depth if self.data.journaled_state.depth() > CALL_STACK_LIMIT { - let (ret, gas, out) = (InstructionResult::CallTooDeep, gas, Bytes::new()); - if INSPECT { - return self.inspector.call_end( - &mut self.data, - inputs, - gas, - ret, - out, - inputs.is_static, - ); - } else { - return (ret, gas, out); - } + return (InstructionResult::CallTooDeep, gas, Bytes::new()); } // Create subroutine checkpoint @@ -770,23 +688,11 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, self.data.db, ) { self.data.journaled_state.checkpoint_revert(checkpoint); - let (ret, gas, out) = (e, gas, Bytes::new()); - if INSPECT { - return self.inspector.call_end( - &mut self.data, - inputs, - gas, - ret, - out, - inputs.is_static, - ); - } else { - return (ret, gas, out); - } + return (e, gas, Bytes::new()); } // Call precompiles - let (ret, gas, out) = if let Some(precompile) = self.precompiles.get(&inputs.contract) { + if let Some(precompile) = self.precompiles.get(&inputs.contract) { let out = match precompile { Precompile::Standard(fun) => fun(inputs.input.as_ref(), inputs.gas_limit), Precompile::Custom(fun) => fun(inputs.input.as_ref(), inputs.gas_limit), @@ -845,13 +751,6 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB, } (exit_reason, interpreter.gas, interpreter.return_value()) - }; - - if INSPECT { - self.inspector - .call_end(&mut self.data, inputs, gas, ret, out, inputs.is_static) - } else { - (ret, gas, out) } } } @@ -988,10 +887,35 @@ impl<'a, GSPEC: Spec, DB: Database + 'a, const INSPECT: bool> Host &mut self, inputs: &mut CreateInputs, ) -> (InstructionResult, Option, Gas, Bytes) { - self.create_inner(inputs) + // Call inspector + if INSPECT { + let (ret, address, gas, out) = self.inspector.create(&mut self.data, inputs); + if ret != InstructionResult::Continue { + return (ret, address, gas, out); + } + } + let ret = self.create_inner(inputs); + if INSPECT { + self.inspector + .create_end(&mut self.data, inputs, ret.0, ret.1, ret.2, ret.3) + } else { + ret + } } fn call(&mut self, inputs: &mut CallInputs) -> (InstructionResult, Gas, Bytes) { - self.call_inner(inputs) + if INSPECT { + let (ret, gas, out) = self.inspector.call(&mut self.data, inputs); + if ret != InstructionResult::Continue { + return (ret, gas, out); + } + } + let ret = self.call_inner(inputs); + if INSPECT { + self.inspector + .call_end(&mut self.data, inputs, ret.1, ret.0, ret.2) + } else { + ret + } } } diff --git a/crates/revm/src/inspector.rs b/crates/revm/src/inspector.rs index b800f52c80..b5e20561e1 100644 --- a/crates/revm/src/inspector.rs +++ b/crates/revm/src/inspector.rs @@ -83,7 +83,6 @@ pub trait Inspector { &mut self, _data: &mut EVMData<'_, DB>, _inputs: &mut CallInputs, - _is_static: bool, ) -> (InstructionResult, Gas, Bytes) { (InstructionResult::Continue, Gas::new(0), Bytes::new()) } @@ -99,7 +98,6 @@ pub trait Inspector { remaining_gas: Gas, ret: InstructionResult, out: Bytes, - _is_static: bool, ) -> (InstructionResult, Gas, Bytes) { (ret, remaining_gas, out) } diff --git a/crates/revm/src/inspector/customprinter.rs b/crates/revm/src/inspector/customprinter.rs index c16cf29826..3ef6754d43 100644 --- a/crates/revm/src/inspector/customprinter.rs +++ b/crates/revm/src/inspector/customprinter.rs @@ -71,10 +71,9 @@ impl Inspector for CustomPrintTracer { remaining_gas: Gas, ret: InstructionResult, out: Bytes, - is_static: bool, ) -> (InstructionResult, Gas, Bytes) { self.gas_inspector - .call_end(data, inputs, remaining_gas, ret, out.clone(), is_static); + .call_end(data, inputs, remaining_gas, ret, out.clone()); (ret, remaining_gas, out) } @@ -96,13 +95,12 @@ impl Inspector for CustomPrintTracer { &mut self, _data: &mut EVMData<'_, DB>, inputs: &mut CallInputs, - is_static: bool, ) -> (InstructionResult, Gas, Bytes) { println!( "SM CALL: {:?},context:{:?}, is_static:{:?}, transfer:{:?}, input_size:{:?}", inputs.contract, inputs.context, - is_static, + inputs.is_static, inputs.transfer, inputs.input.len(), ); diff --git a/crates/revm/src/inspector/gas.rs b/crates/revm/src/inspector/gas.rs index 8532574206..c1ac32b198 100644 --- a/crates/revm/src/inspector/gas.rs +++ b/crates/revm/src/inspector/gas.rs @@ -71,7 +71,6 @@ impl Inspector for GasInspector { remaining_gas: Gas, ret: InstructionResult, out: Bytes, - _is_static: bool, ) -> (InstructionResult, Gas, Bytes) { (ret, remaining_gas, out) } @@ -157,9 +156,8 @@ mod tests { &mut self, data: &mut EVMData<'_, DB>, call: &mut CallInputs, - is_static: bool, ) -> (InstructionResult, Gas, Bytes) { - self.gas_inspector.call(data, call, is_static); + self.gas_inspector.call(data, call); ( InstructionResult::Continue, @@ -175,10 +173,9 @@ mod tests { remaining_gas: Gas, ret: InstructionResult, out: Bytes, - is_static: bool, ) -> (InstructionResult, Gas, Bytes) { self.gas_inspector - .call_end(data, inputs, remaining_gas, ret, out.clone(), is_static); + .call_end(data, inputs, remaining_gas, ret, out.clone()); (ret, remaining_gas, out) } diff --git a/crates/revm/src/inspector/tracer_eip3155.rs b/crates/revm/src/inspector/tracer_eip3155.rs index a9b80c9a53..5c95ab4b55 100644 --- a/crates/revm/src/inspector/tracer_eip3155.rs +++ b/crates/revm/src/inspector/tracer_eip3155.rs @@ -97,7 +97,6 @@ impl Inspector for TracerEip3155 { &mut self, data: &mut EVMData<'_, DB>, _inputs: &mut CallInputs, - _is_static: bool, ) -> (InstructionResult, Gas, Bytes) { self.print_log_line(data.journaled_state.depth()); (InstructionResult::Continue, Gas::new(0), Bytes::new()) @@ -110,10 +109,9 @@ impl Inspector for TracerEip3155 { remaining_gas: Gas, ret: InstructionResult, out: Bytes, - is_static: bool, ) -> (InstructionResult, Gas, Bytes) { self.gas_inspector - .call_end(data, inputs, remaining_gas, ret, out.clone(), is_static); + .call_end(data, inputs, remaining_gas, ret, out.clone()); // self.log_step(interp, data, is_static, eval); self.skip = true; if data.journaled_state.depth() == 0 {