Skip to content

Commit

Permalink
refactor Env for prague and cancun
Browse files Browse the repository at this point in the history
  • Loading branch information
gurukamath committed Dec 12, 2024
1 parent 1ea4a8d commit 8e37d2f
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 576 deletions.
264 changes: 83 additions & 181 deletions src/ethereum/cancun/fork.py

Large diffs are not rendered by default.

25 changes: 17 additions & 8 deletions src/ethereum/cancun/utils/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ethereum.base_types import U256, Bytes, Bytes0, Bytes32, Uint

from ..fork_types import Address
from ..state import get_account
from ..state import TransientStorage, get_account
from ..vm import Environment, Message
from ..vm.precompiled_contracts.mapping import PRE_COMPILED_CONTRACTS
from .address import compute_contract_address
Expand All @@ -30,13 +30,13 @@ def prepare_message(
data: Bytes,
gas: Uint,
env: Environment,
code_address: Optional[Address] = None,
should_transfer_value: bool = True,
is_static: bool = False,
preaccessed_addresses: FrozenSet[Address] = frozenset(),
preaccessed_storage_keys: FrozenSet[
Tuple[(Address, Bytes32)]
] = frozenset(),
code_address: Optional[Address],
should_transfer_value: bool,
is_static: bool,
preaccessed_addresses: FrozenSet[Address],
preaccessed_storage_keys: FrozenSet[Tuple[(Address, Bytes32)]],
gas_price: Uint,
blob_versioned_hashes: Tuple[Bytes32, ...],
) -> Message:
"""
Execute a transaction against the provided environment.
Expand Down Expand Up @@ -69,6 +69,10 @@ def prepare_message(
preaccessed_storage_keys:
Storage keys that should be marked as accessed prior to the message
call
gas_price:
Gas price for the transaction.
blob_versioned_hashes:
Hashes of the blobs that are being accessed.
Returns
-------
Expand Down Expand Up @@ -99,6 +103,7 @@ def prepare_message(

return Message(
caller=caller,
origin=caller,
target=target,
gas=gas,
value=value,
Expand All @@ -111,5 +116,9 @@ def prepare_message(
is_static=is_static,
accessed_addresses=accessed_addresses,
accessed_storage_keys=set(preaccessed_storage_keys),
gas_price=gas_price,
transient_storage=TransientStorage(),
blob_versioned_hashes=blob_versioned_hashes,
parent_evm=None,
traces=[],
)
17 changes: 9 additions & 8 deletions src/ethereum/cancun/vm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,18 @@ class Environment:
Items external to the virtual machine itself, provided by the environment.
"""

caller: Address
chain_id: U64
state: State
block_gas_limit: Uint
block_hashes: List[Hash32]
origin: Address
coinbase: Address
number: Uint
base_fee_per_gas: Uint
gas_limit: Uint
gas_price: Uint
time: U256
prev_randao: Bytes32
state: State
chain_id: U64
traces: List[dict]
excess_blob_gas: U64
blob_versioned_hashes: Tuple[VersionedHash, ...]
transient_storage: TransientStorage
parent_beacon_block_root: Hash32


@dataclass
Expand All @@ -58,6 +54,8 @@ class Message:
"""

caller: Address
origin: Address
gas_price: Uint
target: Union[Bytes0, Address]
current_target: Address
gas: Uint
Expand All @@ -70,7 +68,10 @@ class Message:
is_static: bool
accessed_addresses: Set[Address]
accessed_storage_keys: Set[Tuple[Address, Bytes32]]
transient_storage: TransientStorage
blob_versioned_hashes: Tuple[VersionedHash, ...]
parent_evm: Optional["Evm"]
traces: List[dict]


@dataclass
Expand Down
8 changes: 4 additions & 4 deletions src/ethereum/cancun/vm/instructions/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def origin(evm: Evm) -> None:
charge_gas(evm, GAS_BASE)

# OPERATION
push(evm.stack, U256.from_be_bytes(evm.env.origin))
push(evm.stack, U256.from_be_bytes(evm.message.origin))

# PROGRAM COUNTER
evm.pc += 1
Expand Down Expand Up @@ -320,7 +320,7 @@ def gasprice(evm: Evm) -> None:
charge_gas(evm, GAS_BASE)

# OPERATION
push(evm.stack, U256(evm.env.gas_price))
push(evm.stack, U256(evm.message.gas_price))

# PROGRAM COUNTER
evm.pc += 1
Expand Down Expand Up @@ -551,8 +551,8 @@ def blob_hash(evm: Evm) -> None:
charge_gas(evm, GAS_BLOBHASH_OPCODE)

# OPERATION
if index < len(evm.env.blob_versioned_hashes):
blob_hash = evm.env.blob_versioned_hashes[index]
if index < len(evm.message.blob_versioned_hashes):
blob_hash = evm.message.blob_versioned_hashes[index]
else:
blob_hash = Bytes32(b"\x00" * 32)
push(evm.stack, U256.from_be_bytes(blob_hash))
Expand Down
7 changes: 5 additions & 2 deletions src/ethereum/cancun/vm/instructions/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def tload(evm: Evm) -> None:

# OPERATION
value = get_transient_storage(
evm.env.transient_storage, evm.message.current_target, key
evm.message.transient_storage, evm.message.current_target, key
)
push(evm.stack, value)

Expand All @@ -171,7 +171,10 @@ def tstore(evm: Evm) -> None:
if evm.message.is_static:
raise WriteInStaticContext
set_transient_storage(
evm.env.transient_storage, evm.message.current_target, key, new_value
evm.message.transient_storage,
evm.message.current_target,
key,
new_value,
)

# PROGRAM COUNTER
Expand Down
10 changes: 10 additions & 0 deletions src/ethereum/cancun/vm/instructions/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def generic_create(

child_message = Message(
caller=evm.message.current_target,
origin=evm.message.origin,
gas_price=evm.message.gas_price,
target=Bytes0(),
gas=create_message_gas,
value=endowment,
Expand All @@ -125,7 +127,10 @@ def generic_create(
is_static=False,
accessed_addresses=evm.accessed_addresses.copy(),
accessed_storage_keys=evm.accessed_storage_keys.copy(),
transient_storage=evm.message.transient_storage,
blob_versioned_hashes=evm.message.blob_versioned_hashes,
parent_evm=evm,
traces=evm.message.traces,
)
child_evm = process_create_message(child_message, evm.env)

Expand Down Expand Up @@ -297,7 +302,9 @@ def generic_call(
)
code = get_account(evm.env.state, code_address).code
child_message = Message(
origin=evm.message.origin,
caller=caller,
gas_price=evm.message.gas_price,
target=to,
gas=gas,
value=value,
Expand All @@ -310,7 +317,10 @@ def generic_call(
is_static=True if is_staticcall else evm.message.is_static,
accessed_addresses=evm.accessed_addresses.copy(),
accessed_storage_keys=evm.accessed_storage_keys.copy(),
transient_storage=evm.message.transient_storage,
blob_versioned_hashes=evm.message.blob_versioned_hashes,
parent_evm=evm,
traces=evm.message.traces,
)
child_evm = process_message(child_message, evm.env)

Expand Down
14 changes: 7 additions & 7 deletions src/ethereum/cancun/vm/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def process_create_message(message: Message, env: Environment) -> Evm:
Items containing execution specific objects.
"""
# take snapshot of state before processing the message
begin_transaction(env.state, env.transient_storage)
begin_transaction(env.state, message.transient_storage)

# If the address where the account is being created has storage, it is
# destroyed. This can only happen in the following highly unlikely
Expand Down Expand Up @@ -190,15 +190,15 @@ def process_create_message(message: Message, env: Environment) -> Evm:
if len(contract_code) > MAX_CODE_SIZE:
raise OutOfGasError
except ExceptionalHalt as error:
rollback_transaction(env.state, env.transient_storage)
rollback_transaction(env.state, message.transient_storage)
evm.gas_left = Uint(0)
evm.output = b""
evm.error = error
else:
set_code(env.state, message.current_target, contract_code)
commit_transaction(env.state, env.transient_storage)
commit_transaction(env.state, message.transient_storage)
else:
rollback_transaction(env.state, env.transient_storage)
rollback_transaction(env.state, message.transient_storage)
return evm


Expand All @@ -222,7 +222,7 @@ def process_message(message: Message, env: Environment) -> Evm:
raise StackDepthLimitError("Stack depth limit reached")

# take snapshot of state before processing the message
begin_transaction(env.state, env.transient_storage)
begin_transaction(env.state, message.transient_storage)

touch_account(env.state, message.current_target)

Expand All @@ -235,9 +235,9 @@ def process_message(message: Message, env: Environment) -> Evm:
if evm.error:
# revert state to the last saved checkpoint
# since the message call resulted in an error
rollback_transaction(env.state, env.transient_storage)
rollback_transaction(env.state, message.transient_storage)
else:
commit_transaction(env.state, env.transient_storage)
commit_transaction(env.state, message.transient_storage)
return evm


Expand Down
Loading

0 comments on commit 8e37d2f

Please sign in to comment.