Skip to content

Commit

Permalink
clippy, docs, fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Nov 6, 2023
1 parent 256e639 commit 5501a9b
Show file tree
Hide file tree
Showing 28 changed files with 140 additions and 213 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ default-members = ["crates/revm"]
[profile.release]
lto = true
codegen-units = 1
debug = true

[profile.ethtests]
inherits = "test"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ cargo run -p revm --features ethersdb --example fork_ref_transact
* [Foundry](https://github.com/foundry-rs/foundry) is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
* [Helios](https://github.com/a16z/helios) is a fully trustless, efficient, and portable Ethereum light client written in Rust.
* [Reth](https://github.com/paradigmxyz/reth) Modular, contributor-friendly and blazing-fast implementation of the Ethereum protocol
* [Arbiter](https://github.com/primitivefinance/arbiter) is a CallStackFramework for stateful Ethereum smart-contract simulation
* [Arbiter](https://github.com/primitivefinance/arbiter) is a framework for stateful Ethereum smart-contract simulation
* [Zeth](https://github.com/risc0/zeth) is an open-source ZK block prover for Ethereum built on the RISC Zero zkVM.
* ...

Expand Down
1 change: 0 additions & 1 deletion crates/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ dev = [
"optional_no_base_fee",
]
memory_limit = ["revm-primitives/memory_limit"]
no_gas_measuring = ["revm-primitives/no_gas_measuring"]
optional_balance_check = ["revm-primitives/optional_balance_check"]
optional_block_gas_limit = ["revm-primitives/optional_block_gas_limit"]
optional_eip3607 = ["revm-primitives/optional_eip3607"]
Expand Down
3 changes: 0 additions & 3 deletions crates/interpreter/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ impl Gas {
/// Records an explicit cost.
///
/// Returns `false` if the gas limit is exceeded.
///
/// This function is called on every instruction in the interpreter if the feature
/// `no_gas_measuring` is not enabled.
#[inline(always)]
pub fn record_cost(&mut self, cost: u64) -> bool {
let all_used_gas = self.all_used_gas.saturating_add(cost);
Expand Down
4 changes: 1 addition & 3 deletions crates/interpreter/src/gas/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,12 @@ pub fn selfdestruct_cost<SPEC: Spec>(res: SelfDestructResult) -> u64 {
}

pub fn call_cost<SPEC: Spec>(
value: U256,
transfers_value: bool,
is_new: bool,
is_cold: bool,
is_call_or_callcode: bool,
is_call_or_staticcall: bool,
) -> u64 {
let transfers_value = value != U256::default();

let call_gas = if SPEC::enabled(BERLIN) {
if is_cold {
COLD_ACCOUNT_ACCESS_COST
Expand Down
10 changes: 4 additions & 6 deletions crates/interpreter/src/instruction_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,10 @@ pub enum SuccessOrHalt {
Revert,
Halt(Halt),
FatalExternalError,
/// Internal instruction.
/// Internal instruction that signals Interpreter should continue running.
InternalContinue,
/// Internal call opcode.
InternalCall,
/// Internal create opcode.
InternalCreate,
/// Internal instruction that signals subcall.
InternalCallOrCreate,
}

impl SuccessOrHalt {
Expand Down Expand Up @@ -152,7 +150,7 @@ impl From<InstructionResult> for SuccessOrHalt {
InstructionResult::Return => Self::Success(Eval::Return),
InstructionResult::SelfDestruct => Self::Success(Eval::SelfDestruct),
InstructionResult::Revert => Self::Revert,
InstructionResult::CallOrCreate => Self::InternalCall, // used only in interpreter loop
InstructionResult::CallOrCreate => Self::InternalCallOrCreate, // used only in interpreter loop
InstructionResult::CallTooDeep => Self::Halt(Halt::CallTooDeep), // not gonna happen for first call
InstructionResult::OutOfFund => Self::Halt(Halt::OutOfFund), // Check for first call is done separately.
InstructionResult::OutOfGas => Self::Halt(Halt::OutOfGas(
Expand Down
8 changes: 4 additions & 4 deletions crates/interpreter/src/instructions/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ pub fn revert<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut H)
return_inner(interpreter, InstructionResult::Revert);
}

/// Stop opcode. This opcode halts the execution.
pub fn stop<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
interpreter.return_data_buffer = Bytes::default();
interpreter.instruction_result = InstructionResult::Stop;
}

/// Invalid opcode. This opcode halts the execution.
pub fn invalid<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
interpreter.return_data_buffer = Bytes::default();
interpreter.instruction_result = InstructionResult::InvalidFEOpcode;
}

pub fn not_found<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
interpreter.return_data_buffer = Bytes::default();
/// Unknown opcode. This opcode halts the execution.
pub fn unknown<H: Host>(interpreter: &mut Interpreter, _host: &mut H) {
interpreter.instruction_result = InstructionResult::OpcodeNotFound;
}
145 changes: 51 additions & 94 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,9 @@ pub fn selfdestruct<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &m
interpreter.instruction_result = InstructionResult::SelfDestruct;
}

#[inline(never)]
pub fn prepare_create_inputs<H: Host, const IS_CREATE2: bool, SPEC: Spec>(
pub fn create<const IS_CREATE2: bool, H: Host, SPEC: Spec>(
interpreter: &mut Interpreter,
host: &mut H,
create_inputs: &mut Option<Box<CreateInputs>>,
) {
check_staticcall!(interpreter);

Expand All @@ -249,14 +247,11 @@ pub fn prepare_create_inputs<H: Host, const IS_CREATE2: bool, SPEC: Spec>(
check!(interpreter, PETERSBURG);
}

interpreter.return_data_buffer = Bytes::new();

pop!(interpreter, value, code_offset, len);
let len = as_usize_or_fail!(interpreter, len);

let code = if len == 0 {
Bytes::new()
} else {
let mut code = Bytes::new();
if len != 0 {
// EIP-3860: Limit and meter initcode
if SPEC::enabled(SHANGHAI) {
// Limit is set as double of max contract bytecode size
Expand All @@ -275,9 +270,10 @@ pub fn prepare_create_inputs<H: Host, const IS_CREATE2: bool, SPEC: Spec>(

let code_offset = as_usize_or_fail!(interpreter, code_offset);
shared_memory_resize!(interpreter, code_offset, len);
Bytes::copy_from_slice(interpreter.shared_memory.slice(code_offset, len))
};
code = Bytes::copy_from_slice(interpreter.shared_memory.slice(code_offset, len));
}

// EIP-1014: Skinny CREATE2
let scheme = if IS_CREATE2 {
pop!(interpreter, salt);
gas_or_fail!(interpreter, gas::create2_cost(len));
Expand All @@ -296,29 +292,15 @@ pub fn prepare_create_inputs<H: Host, const IS_CREATE2: bool, SPEC: Spec>(
}
gas!(interpreter, gas_limit);

*create_inputs = Some(Box::new(CreateInputs {
caller: interpreter.contract.address,
scheme,
value,
init_code: code,
gas_limit,
}));
}

pub fn create<const IS_CREATE2: bool, H: Host, SPEC: Spec>(
interpreter: &mut Interpreter,
host: &mut H,
) {
let mut create_input: Option<Box<CreateInputs>> = None;
prepare_create_inputs::<H, IS_CREATE2, SPEC>(interpreter, host, &mut create_input);

let Some(create_input) = create_input else {
return;
};

// Call host to interact with target contract
interpreter.next_action = Some(InterpreterAction::Create {
inputs: create_input,
inputs: Box::new(CreateInputs {
caller: interpreter.contract.address,
scheme,
value,
init_code: code,
gas_limit,
}),
});
interpreter.instruction_result = InstructionResult::CallOrCreate;
}
Expand All @@ -339,17 +321,25 @@ pub fn static_call<H: Host, SPEC: Spec>(interpreter: &mut Interpreter, host: &mu
call_inner::<SPEC, H>(CallScheme::StaticCall, interpreter, host);
}

#[inline(never)]
fn prepare_call_inputs<H: Host, SPEC: Spec>(
interpreter: &mut Interpreter,
pub fn call_inner<SPEC: Spec, H: Host>(
scheme: CallScheme,
interpreter: &mut Interpreter,
host: &mut H,
result_len: &mut usize,
result_offset: &mut usize,
result_call_inputs: &mut Option<Box<CallInputs>>,
) {
match scheme {
// EIP-7: DELEGATECALL
CallScheme::DelegateCall => check!(interpreter, HOMESTEAD),
// EIP-214: New opcode STATICCALL
CallScheme::StaticCall => check!(interpreter, BYZANTIUM),
_ => (),
}

pop!(interpreter, local_gas_limit);
pop_address!(interpreter, to);

// max gas limit is not possible in real ethereum situation.
// But for tests we would not like to fail on this.
// Gas limit for subcall is taken as min of this value and current gas limit.
let local_gas_limit = u64::try_from(local_gas_limit).unwrap_or(u64::MAX);

let value = match scheme {
Expand Down Expand Up @@ -379,10 +369,10 @@ fn prepare_call_inputs<H: Host, SPEC: Spec>(
Bytes::new()
};

*result_len = as_usize_or_fail!(interpreter, out_len);
*result_offset = if *result_len != 0 {
let out_len = as_usize_or_fail!(interpreter, out_len);
let out_offset = if out_len != 0 {
let out_offset = as_usize_or_fail!(interpreter, out_offset);
shared_memory_resize!(interpreter, out_offset, *result_len);
shared_memory_resize!(interpreter, out_offset, out_len);
out_offset
} else {
usize::MAX //unrealistic value so we are sure it is not used
Expand Down Expand Up @@ -412,24 +402,24 @@ fn prepare_call_inputs<H: Host, SPEC: Spec>(
},
};

let transfer = if scheme == CallScheme::Call {
Transfer {
let transfer = match scheme {
CallScheme::Call => Transfer {
source: interpreter.contract.address,
target: to,
value,
}
} else if scheme == CallScheme::CallCode {
Transfer {
},
CallScheme::CallCode => Transfer {
source: interpreter.contract.address,
target: interpreter.contract.address,
value,
}
} else {
//this is dummy send for StaticCall and DelegateCall, it should do nothing and dont touch anything.
Transfer {
source: interpreter.contract.address,
target: interpreter.contract.address,
value: U256::ZERO,
},
_ => {
//this is dummy send for StaticCall and DelegateCall, it should do nothing and dont touch anything.
Transfer {
source: interpreter.contract.address,
target: interpreter.contract.address,
value: U256::ZERO,
}
}
};

Expand All @@ -443,7 +433,7 @@ fn prepare_call_inputs<H: Host, SPEC: Spec>(
gas!(
interpreter,
gas::call_cost::<SPEC>(
value,
value != U256::ZERO,
is_new,
is_cold,
matches!(scheme, CallScheme::Call | CallScheme::CallCode),
Expand All @@ -468,49 +458,16 @@ fn prepare_call_inputs<H: Host, SPEC: Spec>(
}
let is_static = matches!(scheme, CallScheme::StaticCall) || interpreter.is_static;

*result_call_inputs = Some(Box::new(CallInputs {
contract: to,
transfer,
input,
gas_limit,
context,
is_static,
}));
}

pub fn call_inner<SPEC: Spec, H: Host>(
scheme: CallScheme,
interpreter: &mut Interpreter,
host: &mut H,
) {
match scheme {
// EIP-7: DELEGATECALL
CallScheme::DelegateCall => check!(interpreter, HOMESTEAD),
// EIP-214: New opcode STATICCALL
CallScheme::StaticCall => check!(interpreter, BYZANTIUM),
_ => (),
}
interpreter.return_data_buffer = Bytes::new();

let mut out_offset: usize = 0;
let mut out_len: usize = 0;
let mut call_input: Option<Box<CallInputs>> = None;
prepare_call_inputs::<H, SPEC>(
interpreter,
scheme,
host,
&mut out_len,
&mut out_offset,
&mut call_input,
);

let Some(call_input) = call_input else {
return;
};

// Call host to interact with target contract
interpreter.next_action = Some(InterpreterAction::SubCall {
inputs: call_input,
inputs: Box::new(CallInputs {
contract: to,
transfer,
input,
gas_limit,
context,
is_static,
}),
return_memory_offset: out_offset..out_offset + out_len,
});
interpreter.instruction_result = InstructionResult::CallOrCreate;
Expand Down
6 changes: 3 additions & 3 deletions crates/interpreter/src/instructions/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub enum InstructionTables<'a, H> {
impl<'a, H> Clone for InstructionTables<'a, H> {
fn clone(&self) -> Self {
match self {
Self::Plain(arg0) => Self::Plain(arg0.clone()),
Self::Boxed(arg0) => Self::Boxed(arg0.clone()),
Self::Plain(table) => Self::Plain(table.clone()),
Self::Boxed(table) => Self::Boxed(table.clone()),
}
}
}
Expand Down Expand Up @@ -75,7 +75,7 @@ macro_rules! opcodes {
pub fn instruction<H: Host, SPEC: Spec>(opcode: u8) -> Instruction<H> {
match opcode {
$($name => $f,)*
_ => control::not_found,
_ => control::unknown,
}
}
};
Expand Down
Loading

0 comments on commit 5501a9b

Please sign in to comment.