Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement EIP-5656 for Cancun #851

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 36 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,36 @@ 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
destination = pop(evm.stack)
source = pop(evm.stack)
length = pop(evm.stack)

# GAS
words = ceil32(Uint(length)) // 32
copy_gas_cost = GAS_COPY * words

extend_memory = calculate_gas_extend_memory(
evm.memory, [(source, length), (destination, 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, source, length)
memory_write(evm.memory, destination, value)

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

mcopy

checkable
Loading