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

Commit

Permalink
feat: begin_tx support EIP-3860, extra init_code gas required
Browse files Browse the repository at this point in the history
  • Loading branch information
KimiWu123 committed Aug 7, 2023
1 parent 1f2dd4d commit f642772
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
10 changes: 9 additions & 1 deletion src/zkevm_specs/evm_circuit/execution/begin_tx.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from zkevm_specs.util.param import GAS_COST_INITCODE_WORD
from ...util import (
GAS_COST_TX,
GAS_COST_CREATION_TX,
EMPTY_CODE_HASH,
N_BYTES_U64,
FQ,
Word,
WordOrValue,
Expand Down Expand Up @@ -67,7 +69,13 @@ def begin_tx(instruction: Instruction):
# G_transaction +
# sum([G_accesslistaddress + G_accessliststorage * len(TA[j]) for j in len(TA)])
tx_calldata_gas_cost = instruction.tx_context_lookup(tx_id, TxContextFieldTag.CallDataGasCost)
tx_cost_gas = GAS_COST_CREATION_TX if tx_is_create == 1 else GAS_COST_TX
tx_cost_gas = GAS_COST_TX
if tx_is_create.expr() == FQ(1):
len_words, _ = instruction.constant_divmod(
tx_call_data_length + FQ(31), FQ(32), N_BYTES_U64
)
tx_cost_gas = GAS_COST_CREATION_TX + len_words * GAS_COST_INITCODE_WORD

# TODO: Handle gas cost of tx level access list (EIP 2930)
tx_accesslist_gas = instruction.tx_context_lookup(tx_id, TxContextFieldTag.AccessListGasCost)
tx_intrinsic_gas = tx_calldata_gas_cost.expr() + tx_cost_gas + tx_accesslist_gas.expr()
Expand Down
5 changes: 2 additions & 3 deletions src/zkevm_specs/evm_circuit/execution/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ def create(instruction: Instruction):
# CREATE2 = gas cost of CREATE + GAS_COST_COPY_SHA3 * word_len
# byte_code is only available in `return_revert`,
# so the last part (GAS_COST_CODE_DEPOSIT * len(byte_code)) won't be calculated here
gas_left = instruction.curr.gas_left
gas_cost = GAS_COST_CREATE + memory_expansion_gas_cost
word_len, _ = instruction.constant_divmod(size + FQ(31), FQ(32), N_BYTES_MEMORY_WORD_SIZE)
gas_cost += word_len * GAS_COST_INITCODE_WORD
gas_left = instruction.curr.gas_left
gas_cost = GAS_COST_CREATE + memory_expansion_gas_cost + word_len * GAS_COST_INITCODE_WORD
if is_create2 == FQ(1):
gas_cost += GAS_COST_COPY_SHA3 * word_len
gas_available = gas_left - gas_cost
Expand Down
10 changes: 5 additions & 5 deletions tests/evm/test_begin_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def gen_bytecode(is_return: bool, offset: int, has_init_code: bool) -> Bytecode:
Transaction(
caller_address=0xFE,
callee_address=None,
gas=100000, # TODO(amb) make more precise gas
gas=53000,
),
CALLEE_WITH_NOTHING,
True,
Expand All @@ -196,7 +196,7 @@ def gen_bytecode(is_return: bool, offset: int, has_init_code: bool) -> Bytecode:
Transaction(
caller_address=0xFE,
callee_address=None,
gas=100000, # TODO(amb) make more precise gas
gas=53000,
value=1,
),
CALLEE_WITH_NOTHING,
Expand All @@ -207,7 +207,7 @@ def gen_bytecode(is_return: bool, offset: int, has_init_code: bool) -> Bytecode:
Transaction(
caller_address=0xFE,
callee_address=None,
gas=53580, # TODO(amb) make more precise gas
gas=53584,
call_data=bytes(gen_bytecode(True, 0, True).code),
),
CALLEE_WITH_NOTHING,
Expand All @@ -218,7 +218,7 @@ def gen_bytecode(is_return: bool, offset: int, has_init_code: bool) -> Bytecode:
Transaction(
caller_address=0xFE,
callee_address=None,
gas=53580,
gas=53584,
value=1,
call_data=bytes(gen_bytecode(True, 0, True).code),
),
Expand All @@ -230,7 +230,7 @@ def gen_bytecode(is_return: bool, offset: int, has_init_code: bool) -> Bytecode:
Transaction(
caller_address=0xFE,
callee_address=None,
gas=53580,
gas=53584,
value=1,
call_data=bytes(gen_bytecode(False, 0, True).code),
),
Expand Down

0 comments on commit f642772

Please sign in to comment.