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

DRAFT: EIP-7708 #1105

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
42 changes: 41 additions & 1 deletion src/ethereum/cancun/vm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
from ethereum_types.bytes import Bytes, Bytes0, Bytes32
from ethereum_types.numeric import U64, U256, Uint

from ethereum.crypto.hash import Hash32
from ethereum.crypto.hash import Hash32, keccak256
from ethereum.exceptions import EthereumException
from ethereum.utils.byte import right_pad_zero_bytes

from ..blocks import Log
from ..fork_types import Address, VersionedHash
from ..state import State, TransientStorage, account_exists_and_is_empty
from .precompiled_contracts import RIPEMD160_ADDRESS

__all__ = ("Environment", "Evm", "Message")
# Magic number for transaction logs converted to Keccak Hash
MAGIC_LOG_KHASH = keccak256(b"42")


@dataclass
Expand Down Expand Up @@ -150,3 +153,40 @@ def incorporate_child_on_error(evm: Evm, child_evm: Evm) -> None:
):
evm.touched_accounts.add(RIPEMD160_ADDRESS)
evm.gas_left += child_evm.gas_left


def transfer_log(
evm: Evm,
sender: Address,
recipient: Address,
transfer_amount: U256,
) -> None:
"""
Main functional unit satisfying EIP-7708

Parameters
----------
evm :
The state of the ethereum virtual machine
sender :
The account address sending the transfer
recipient :
The address of the transfer recipient account
transfer_amount :
The amount of ETH transacted
"""
# We need to pre-pad the Address correctly before converting,
# otherwise it will throw because the lengths are not the same
padded_sender = right_pad_zero_bytes(sender, 12)
padded_recipient = right_pad_zero_bytes(recipient, 12)
log_entry = Log(
address=evm.message.current_target,
topics=(
MAGIC_LOG_KHASH, # noqa: SC200
Hash32(padded_sender),
Hash32(padded_recipient),
),
data=transfer_amount.to_be_bytes(),
)

evm.logs = evm.logs + (log_entry,)
17 changes: 17 additions & 0 deletions src/ethereum/cancun/vm/instructions/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

Implementations of the EVM system related instructions.
"""

from ethereum_types.bytes import Bytes0
from ethereum_types.numeric import U256, Uint

Expand All @@ -37,6 +38,7 @@
Message,
incorporate_child_on_error,
incorporate_child_on_success,
transfer_log,
)
from ..exceptions import OutOfGasError, Revert, WriteInStaticContext
from ..gas import (
Expand Down Expand Up @@ -380,9 +382,11 @@ def call(evm: Evm) -> None:
access_gas_cost + create_gas_cost + transfer_gas_cost,
)
charge_gas(evm, message_call_gas.cost + extend_memory.cost)

if evm.message.is_static and value != U256(0):
raise WriteInStaticContext
evm.memory += b"\x00" * extend_memory.expand_by

sender_balance = get_account(
evm.env.state, evm.message.current_target
).balance
Expand All @@ -405,6 +409,12 @@ def call(evm: Evm) -> None:
memory_output_start_position,
memory_output_size,
)
transfer_log(
evm,
evm.message.current_target,
to,
value,
)

# PROGRAM COUNTER
evm.pc += Uint(1)
Expand Down Expand Up @@ -522,6 +532,13 @@ def selfdestruct(evm: Evm) -> None:
originator_balance,
)

transfer_log(
evm,
evm.message.current_target,
beneficiary,
originator_balance,
)

# register account for deletion only if it was created
# in the same transaction
if originator in evm.env.state.created_accounts:
Expand Down
9 changes: 8 additions & 1 deletion src/ethereum/cancun/vm/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from ..vm import Message
from ..vm.gas import GAS_CODE_DEPOSIT, charge_gas
from ..vm.precompiled_contracts.mapping import PRE_COMPILED_CONTRACTS
from . import Environment, Evm
from . import Environment, Evm, transfer_log
from .exceptions import (
AddressCollision,
ExceptionalHalt,
Expand Down Expand Up @@ -286,6 +286,13 @@ def execute_code(message: Message, env: Environment) -> Evm:
accessed_addresses=message.accessed_addresses,
accessed_storage_keys=message.accessed_storage_keys,
)
transfer_log(
evm,
message.current_target,
message.caller,
message.value,
)

try:
if evm.message.code_address in PRE_COMPILED_CONTRACTS:
evm_trace(evm, PrecompileStart(evm.message.code_address))
Expand Down