Skip to content

Commit

Permalink
implement EIP-5656
Browse files Browse the repository at this point in the history
  • Loading branch information
gurukamath committed Oct 30, 2023
1 parent fcf2cd6 commit f44404b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/ethereum/cancun/vm/instructions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class Ops(enum.Enum):
MSTORE = 0x52
MSTORE8 = 0x53
MSIZE = 0x59
MCOPY = 0x5E

# Log Operations
LOG0 = 0xA0
Expand Down Expand Up @@ -249,6 +250,7 @@ class Ops(enum.Enum):
Ops.MSTORE: memory_instructions.mstore,
Ops.MSTORE8: memory_instructions.mstore8,
Ops.MSIZE: memory_instructions.msize,
Ops.MCOPY: memory_instructions.mcopy,
Ops.ADDRESS: environment_instructions.address,
Ops.BALANCE: environment_instructions.balance,
Ops.ORIGIN: environment_instructions.origin,
Expand Down
40 changes: 39 additions & 1 deletion src/ethereum/cancun/vm/instructions/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
Implementations of the EVM Memory instructions.
"""
from ethereum.base_types import U8_MAX_VALUE, U256, Bytes
from ethereum.base_types import U8_MAX_VALUE, U256, Bytes, Uint
from ethereum.utils.numeric import ceil32

from .. import Evm
from ..gas import (
GAS_BASE,
GAS_COPY,
GAS_VERY_LOW,
calculate_gas_extend_memory,
charge_gas,
Expand Down Expand Up @@ -138,3 +140,39 @@ def msize(evm: Evm) -> None:

# PROGRAM COUNTER
evm.pc += 1


def mcopy(evm: Evm) -> None:
"""
Copy the bytes in memory from one location to another.
Parameters
----------
evm :
The current EVM frame.
"""
# STACK
dst = pop(evm.stack)
src = pop(evm.stack)
length = pop(evm.stack)

# GAS
if length > 0:
words = ceil32(Uint(length)) // 32
copy_gas_cost = GAS_COPY * words
else:
copy_gas_cost = Uint(0)

extend_memory = calculate_gas_extend_memory(
evm.memory, [(src, length), (dst, length)]
)
charge_gas(evm, GAS_VERY_LOW + copy_gas_cost + extend_memory.cost)

# OPERATION
evm.memory += b"\x00" * extend_memory.expand_by
value = memory_read_bytes(evm.memory, src, length)
memory_write(evm.memory, dst, value)

# PROGRAM COUNTER
evm.pc += 1
3 changes: 3 additions & 0 deletions whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,7 @@ rlps
jsons
mem

mcopy
src

checkable

0 comments on commit f44404b

Please sign in to comment.