Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
feat: remove CREATE opcode from this oog_dynamic_memory
Browse files Browse the repository at this point in the history
  • Loading branch information
KimiWu123 committed Aug 16, 2023
1 parent a05e4f4 commit 3d23a0e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 274 deletions.
49 changes: 28 additions & 21 deletions bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ impl<'a> CircuitInputStateRef<'a> {
let (offset, length) = match step.op {
OpcodeId::RETURN | OpcodeId::REVERT => {
let (offset, length) = if step.error.is_some()
|| (self.call()?.is_create() && self.call()?.is_success())
|| (self.call()?.is_create() && self.call()?.is_success)
{
(0, 0)
} else {
Expand Down Expand Up @@ -1072,8 +1072,9 @@ impl<'a> CircuitInputStateRef<'a> {
let geth_step = steps
.get(0)
.ok_or(Error::InternalError("invalid index 0"))?;
let is_revert_or_return_call_success = geth_step.op == OpcodeId::REVERT
|| geth_step.op == OpcodeId::RETURN && exec_step.error.is_none();
let is_revert_or_return_call_success = (geth_step.op == OpcodeId::REVERT
|| geth_step.op == OpcodeId::RETURN)
&& exec_step.error.is_none();

if !is_revert_or_return_call_success && !call.is_success {
// add call failure ops for exception cases
Expand Down Expand Up @@ -1127,25 +1128,29 @@ impl<'a> CircuitInputStateRef<'a> {
_ => [Word::zero(), Word::zero()],
};

let curr_memory_word_size = (exec_step.memory_size as u64) / 32;
let next_memory_word_size = if !last_callee_return_data_length.is_zero() {
std::cmp::max(
(last_callee_return_data_offset + last_callee_return_data_length + 31).as_u64()
/ 32,
curr_memory_word_size,
)
let gas_refund = if exec_step.error.is_some() {
0
} else {
curr_memory_word_size
};
let curr_memory_word_size = (exec_step.memory_size as u64) / 32;
let next_memory_word_size = if !last_callee_return_data_length.is_zero() {
std::cmp::max(
(last_callee_return_data_offset + last_callee_return_data_length + 31).as_u64()
/ 32,
curr_memory_word_size,
)
} else {
curr_memory_word_size
};

let memory_expansion_gas_cost =
memory_expansion_gas_cost(curr_memory_word_size, next_memory_word_size);
let code_deposit_cost = if call.is_create() && call.is_success {
GasCost::CODE_DEPOSIT_BYTE_COST * last_callee_return_data_length.as_u64()
} else {
0
let memory_expansion_gas_cost =
memory_expansion_gas_cost(curr_memory_word_size, next_memory_word_size);
let code_deposit_cost = if call.is_create() && call.is_success {
GasCost::CODE_DEPOSIT_BYTE_COST * last_callee_return_data_length.as_u64()
} else {
0
};
geth_step.gas - memory_expansion_gas_cost - code_deposit_cost
};
let gas_refund = geth_step.gas - memory_expansion_gas_cost - code_deposit_cost;

let caller_gas_left = if is_revert_or_return_call_success || call.is_success {
geth_step_next.gas - gas_refund
Expand Down Expand Up @@ -1179,19 +1184,21 @@ impl<'a> CircuitInputStateRef<'a> {
}

// EIP-211: CREATE/CREATE2 call successful case should set RETURNDATASIZE = 0
let discard_return_data =
call.is_create() && geth_step.op == OpcodeId::RETURN || exec_step.error.is_some();
for (field, value) in [
(CallContextField::LastCalleeId, call.call_id.into()),
(
CallContextField::LastCalleeReturnDataOffset,
if call.is_create() && call.is_success {
if discard_return_data {
U256::zero()
} else {
last_callee_return_data_offset
},
),
(
CallContextField::LastCalleeReturnDataLength,
if call.is_create() && call.is_success {
if discard_return_data {
U256::zero()
} else {
last_callee_return_data_length
Expand Down
58 changes: 19 additions & 39 deletions bus-mapping/src/evm/opcodes/error_oog_dynamic_memory.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::circuit_input_builder::{CircuitInputStateRef, ExecStep};
use crate::error::{ExecError, OogError};
use crate::evm::Opcode;
use crate::Error;
use eth_types::evm_types::OpcodeId;
use eth_types::GethExecStep;
use crate::{
circuit_input_builder::{CircuitInputStateRef, ExecStep},
error::{ExecError, OogError},
evm::Opcode,
Error,
};
use eth_types::{evm_types::OpcodeId, GethExecStep};

#[derive(Clone, Copy, Debug)]
pub(crate) struct OOGDynamicMemory {}
Expand All @@ -14,44 +15,23 @@ impl Opcode for OOGDynamicMemory {
geth_steps: &[GethExecStep],
) -> Result<Vec<ExecStep>, Error> {
let geth_step = &geth_steps[0];
debug_assert!(
[OpcodeId::CREATE, OpcodeId::RETURN, OpcodeId::REVERT].contains(&geth_step.op)
);
debug_assert!([OpcodeId::RETURN, OpcodeId::REVERT].contains(&geth_step.op));

let mut exec_step = state.new_step(geth_step)?;
exec_step.error = Some(ExecError::OutOfGas(OogError::DynamicMemoryExpansion));

if geth_step.op == OpcodeId::CREATE {
state.stack_read(
&mut exec_step,
geth_step.stack.last_filled(),
geth_step.stack.last()?,
)?;
state.stack_read(
&mut exec_step,
geth_step.stack.nth_last_filled(1),
geth_step.stack.nth_last(1)?,
)?;
state.stack_read(
&mut exec_step,
geth_step.stack.nth_last_filled(2),
geth_step.stack.nth_last(2)?,
)?;
} else {
state.stack_read(
&mut exec_step,
geth_step.stack.last_filled(),
geth_step.stack.last()?,
)?;
state.stack_read(
&mut exec_step,
geth_step.stack.nth_last_filled(1),
geth_step.stack.nth_last(1)?,
)?;
}
state.stack_read(
&mut exec_step,
geth_step.stack.last_filled(),
geth_step.stack.last()?,
)?;
state.stack_read(
&mut exec_step,
geth_step.stack.nth_last_filled(1),
geth_step.stack.nth_last(1)?,
)?;

state.gen_restore_context_ops(&mut exec_step, geth_steps)?;
state.handle_return(geth_step)?;
state.handle_return(&mut exec_step, geth_steps, true)?;

Ok(vec![exec_step])
}
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ pub struct ExecutionConfig<F> {
Box<DummyGadget<F, 0, 0, { ExecutionState::ErrorOutOfGasStaticMemoryExpansion }>>,
error_stack: Box<ErrorStackGadget<F>>,
error_write_protection: Box<ErrorWriteProtectionGadget<F>>,
error_oog_dynamic_memory_gadget:Box<ErrorOOGDynamicMemoryGadget<F>>,
error_oog_dynamic_memory_gadget: Box<ErrorOOGDynamicMemoryGadget<F>>,
error_oog_log: Box<ErrorOOGLogGadget<F>>,
error_oog_sha3: Box<ErrorOOGSha3Gadget<F>>,
error_oog_account_access: Box<ErrorOOGAccountAccessGadget<F>>,
Expand Down
Loading

0 comments on commit 3d23a0e

Please sign in to comment.