Skip to content

Commit

Permalink
fix: clippy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Jan 7, 2023
1 parent 322493d commit d71e836
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 108 deletions.
128 changes: 83 additions & 45 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::{
journaled_state::{Account, JournaledState, State},
models::SelfDestructResult,
precompiles, return_ok, return_revert, AnalysisKind, CallContext, CallInputs, CallScheme,
CreateInputs, CreateScheme, Env, ExecutionResult, Gas, Inspector, Log, Return, Spec,
CreateInputs, CreateOutputs, CreateScheme, Env, ExecutionResult, Gas, Inspector, Log, Return,
Spec,
SpecId::{self, *},
TransactOut, TransactTo, Transfer, B160, B256, KECCAK_EMPTY, U256,
};
Expand Down Expand Up @@ -220,8 +221,13 @@ 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)?;
(exit, ret_gas, TransactOut::Create(bytes, address))
let CreateOutputs {
exit_reason,
address,
gas,
return_value,
} = self.create_inner(&mut create_input)?;
(exit_reason, gas, TransactOut::Create(return_value, address))
}
};

Expand Down Expand Up @@ -418,23 +424,19 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
}
}

#[allow(clippy::type_complexity)]
fn create_inner(
&mut self,
inputs: &mut CreateInputs,
) -> Result<(Return, Option<B160>, Gas, Bytes), TransactionError<DB::Error>> {
) -> Result<CreateOutputs, TransactionError<DB::Error>> {
// Call inspector
if INSPECT {
let (ret, address, gas, out) = self.inspector.create(&mut self.data, inputs);
if ret != Return::Continue {
return Ok(self.inspector.create_end(
&mut self.data,
inputs,
ret,
address,
gas,
out,
));
// let (ret, address, gas, out)
let mut outputs = self.inspector.create(&mut self.data, inputs);
if outputs.exit_reason != Return::Continue {
self.inspector
.create_end(&mut self.data, inputs, &mut outputs);

return Ok(outputs);
}
}

Expand All @@ -443,11 +445,23 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,

// Check depth of calls
if self.data.journaled_state.depth() > interpreter::CALL_STACK_LIMIT {
return Ok((Return::CallTooDeep, None, gas, Bytes::new()));
return Ok(CreateOutputs {
exit_reason: Return::CallTooDeep,
address: None,
gas,
return_value: Bytes::new(),
});
}
// Check balance of caller and value. Do this before increasing nonce
match self.balance(inputs.caller) {
Ok(i) if i.0 < inputs.value => return Ok((Return::OutOfFund, None, gas, Bytes::new())),
Ok(i) if i.0 < inputs.value => {
return Ok(CreateOutputs {
exit_reason: Return::OutOfFund,
address: None,
gas,
return_value: Bytes::new(),
})
}
Ok(_) => (),
Err(e) => return Err(TransactionError::DatabaseFailure(e)),
}
Expand All @@ -466,7 +480,7 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
CreateScheme::Create => create_address(inputs.caller, old_nonce),
CreateScheme::Create2 { salt } => create2_address(inputs.caller, code_hash, salt),
};
let ret = Some(created_address);
let address = Some(created_address);

// Load account so that it will be hot
self.load_account(created_address)?;
Expand Down Expand Up @@ -540,74 +554,99 @@ impl<'a, GSPEC: Spec, DB: Database, const INSPECT: bool> EVMImpl<'a, GSPEC, DB,
}
let exit_reason = interpreter.run::<Self, GSPEC>(self, INSPECT)?;

// Host error if present on execution\
let (ret, address, gas, out) = match exit_reason {
// Host error if present on execution
let mut outputs = match exit_reason {
return_ok!() => {
// if ok, check contract creation limit and calculate gas deduction on output len.
let mut bytes = interpreter.return_value();
let mut return_value = interpreter.return_value();

// EIP-3541: Reject new contract code starting with the 0xEF byte
if GSPEC::enabled(LONDON) && !bytes.is_empty() && bytes.first() == Some(&0xEF) {
if GSPEC::enabled(LONDON)
&& !return_value.is_empty()
&& return_value.first() == Some(&0xEF)
{
self.data.journaled_state.checkpoint_revert(checkpoint);
return Ok((Return::CreateContractWithEF, ret, interpreter.gas, bytes));
return Ok(CreateOutputs {
exit_reason: Return::CreateContractWithEF,
address,
gas: interpreter.gas,
return_value,
});
}

// EIP-170: Contract code size limit
// By default limit is 0x6000 (~25kb)
if GSPEC::enabled(SPURIOUS_DRAGON)
&& bytes.len() > self.data.env.cfg.limit_contract_code_size.unwrap_or(0x6000)
&& return_value.len()
> self.data.env.cfg.limit_contract_code_size.unwrap_or(0x6000)
{
self.data.journaled_state.checkpoint_revert(checkpoint);
return Ok((Return::CreateContractLimit, ret, interpreter.gas, bytes));
return Ok(CreateOutputs {
exit_reason: Return::CreateContractLimit,
address,
gas: interpreter.gas,
return_value,
});
}
if crate::USE_GAS {
let gas_for_code = bytes.len() as u64 * crate::gas::CODEDEPOSIT;
let gas_for_code = return_value.len() as u64 * crate::gas::CODEDEPOSIT;
if !interpreter.gas.record_cost(gas_for_code) {
// record code deposit gas cost and check if we are out of gas.
// EIP-2 point 3: If contract creation does not have enough gas to pay for the
// final gas fee for adding the contract code to the state, the contract
// 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 Ok((Return::OutOfGas, ret, interpreter.gas, bytes));
return Ok(CreateOutputs {
exit_reason: Return::OutOfGas,
address,
gas: interpreter.gas,
return_value,
});
} else {
bytes = Bytes::new();
return_value = Bytes::new();
}
}
}
// if we have enought gas
self.data.journaled_state.checkpoint_commit();
// Do analasis of bytecode streight away.
let bytecode = match self.data.env.cfg.perf_analyse_created_bytecodes {
AnalysisKind::Raw => Bytecode::new_raw(bytes.clone()),
AnalysisKind::Check => Bytecode::new_raw(bytes.clone()).to_checked(),
AnalysisKind::Raw => Bytecode::new_raw(return_value.clone()),
AnalysisKind::Check => Bytecode::new_raw(return_value.clone()).to_checked(),
AnalysisKind::Analyse => {
Bytecode::new_raw(bytes.clone()).to_analysed::<GSPEC>()
Bytecode::new_raw(return_value.clone()).to_analysed::<GSPEC>()
}
};

self.data
.journaled_state
.set_code(created_address, bytecode);
(Return::Continue, ret, interpreter.gas, bytes)

CreateOutputs {
exit_reason: Return::Continue,
address,
gas: interpreter.gas,
return_value,
}
}
_ => {
self.data.journaled_state.checkpoint_revert(checkpoint);
(
CreateOutputs {
exit_reason,
ret,
interpreter.gas,
interpreter.return_value(),
)
address,
gas: interpreter.gas,
return_value: interpreter.return_value(),
}
}
};

Ok(if INSPECT {
if INSPECT {
self.inspector
.create_end(&mut self.data, inputs, ret, address, gas, out)
} else {
(ret, address, gas, out)
})
.create_end(&mut self.data, inputs, &mut outputs)
}

Ok(outputs)
}

fn call_inner(
Expand Down Expand Up @@ -857,7 +896,7 @@ impl<'a, GSPEC: Spec, DB: Database + 'a, const INSPECT: bool> Host
fn create(
&mut self,
inputs: &mut CreateInputs,
) -> Result<(Return, Option<B160>, Gas, Bytes), TransactionError<Self::DatabaseError>> {
) -> Result<CreateOutputs, TransactionError<Self::DatabaseError>> {
self.create_inner(inputs)
}

Expand Down Expand Up @@ -927,12 +966,11 @@ pub trait Host {
target: B160,
) -> Result<SelfDestructResult, Self::DatabaseError>;

#[allow(clippy::type_complexity)]
/// Invoke a create operation.
fn create(
&mut self,
inputs: &mut CreateInputs,
) -> Result<(Return, Option<B160>, Gas, Bytes), TransactionError<Self::DatabaseError>>;
) -> Result<CreateOutputs, TransactionError<Self::DatabaseError>>;
/// Invoke a call operation.
fn call(
&mut self,
Expand Down
21 changes: 5 additions & 16 deletions crates/revm/src/inspector.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
bits::{B160, B256},
evm_impl::EVMData,
CallInputs, CreateInputs, Database, Gas, Interpreter, Return,
CallInputs, CreateInputs, CreateOutputs, Database, Gas, Interpreter, Return,
};
use auto_impl::auto_impl;
use bytes::Bytes;
Expand Down Expand Up @@ -104,28 +104,17 @@ pub trait Inspector<DB: Database> {
/// Called when a contract is about to be created.
///
/// Returning anything other than [Return::Continue] overrides the result of the creation.
fn create(
&mut self,
_data: &mut EVMData<'_, DB>,
_inputs: &mut CreateInputs,
) -> (Return, Option<B160>, Gas, Bytes) {
(Return::Continue, None, Gas::new(0), Bytes::default())
fn create(&mut self, _data: &mut EVMData<'_, DB>, _inputs: &mut CreateInputs) -> CreateOutputs {
CreateOutputs::default()
}

/// Called when a contract has been created.
///
/// Returning anything other than the values passed to this function (`(ret, remaining_gas,
/// address, out)`) will alter the result of the create.
fn create_end(
&mut self,
_data: &mut EVMData<'_, DB>,
_inputs: &CreateInputs,
ret: Return,
address: Option<B160>,
remaining_gas: Gas,
out: Bytes,
) -> (Return, Option<B160>, Gas, Bytes) {
(ret, address, remaining_gas, out)
_outputs: &mut CreateOutputs,
) {
}

/// Called when a contract has been self-destructed.
Expand Down
23 changes: 8 additions & 15 deletions crates/revm/src/inspector/customprinter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//! It is great tool if some debugging is needed.
//!
use crate::{
bits::B160,
inspectors::GasInspector,
opcode::{self},
Bytes, CallInputs, CreateInputs, Database, EVMData, Gas, Inspector, Interpreter, Return,
Bytes, CallInputs, CreateInputs, CreateOutputs, Database, EVMData, Gas, Inspector, Interpreter,
Return,
};
use hex;
#[derive(Clone, Default)]
Expand Down Expand Up @@ -86,14 +86,11 @@ impl<DB: Database> Inspector<DB> for CustomPrintTracer {
&mut self,
data: &mut EVMData<'_, DB>,
inputs: &CreateInputs,
ret: Return,
address: Option<B160>,
remaining_gas: Gas,
out: Bytes,
) -> (Return, Option<B160>, Gas, Bytes) {
outputs: &mut CreateOutputs,
) {
let mut cloned_outputs = outputs.clone();
self.gas_inspector
.create_end(data, inputs, ret, address, remaining_gas, out.clone());
(ret, address, remaining_gas, out)
.create_end(data, inputs, &mut cloned_outputs);
}

fn call(
Expand All @@ -113,11 +110,7 @@ impl<DB: Database> Inspector<DB> for CustomPrintTracer {
(Return::Continue, Gas::new(0), Bytes::new())
}

fn create(
&mut self,
_data: &mut EVMData<'_, DB>,
inputs: &mut CreateInputs,
) -> (Return, Option<B160>, Gas, Bytes) {
fn create(&mut self, _data: &mut EVMData<'_, DB>, inputs: &mut CreateInputs) -> CreateOutputs {
println!(
"CREATE CALL: caller:{:?}, scheme:{:?}, value:{:?}, init_code:{:?}, gas:{:?}",
inputs.caller,
Expand All @@ -126,7 +119,7 @@ impl<DB: Database> Inspector<DB> for CustomPrintTracer {
hex::encode(&inputs.init_code),
inputs.gas_limit
);
(Return::Continue, None, Gas::new(0), Bytes::new())
CreateOutputs::default()
}

fn selfdestruct(&mut self) {
Expand Down
Loading

0 comments on commit d71e836

Please sign in to comment.