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

WIP: EVM Object Format Reference Tests #205

Closed
wants to merge 10 commits into from
Closed
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
5 changes: 5 additions & 0 deletions src/ethereum_test_forks/forks/forks.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,8 @@ def engine_new_payload_beacon_root(cls, block_number: int = 0, timestamp: int =
Starting at Cancun, payloads must have a parent beacon block root.
"""
return True

class Prague(Cancun):
"""
Prague fork
"""
2 changes: 2 additions & 0 deletions src/ethereum_test_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
add_kzg_version,
ceiling_division,
compute_create2_address,
compute_create3_address,
compute_create_address,
copy_opcode_cost,
cost_memory_bytes,
Expand Down Expand Up @@ -88,6 +89,7 @@
"ceiling_division",
"compute_create_address",
"compute_create2_address",
"compute_create3_address",
"copy_opcode_cost",
"cost_memory_bytes",
"eip_2028_transaction_data_cost",
Expand Down
2 changes: 2 additions & 0 deletions src/ethereum_test_tools/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
add_kzg_version,
ceiling_division,
compute_create2_address,
compute_create3_address,
compute_create_address,
copy_opcode_cost,
cost_memory_bytes,
Expand Down Expand Up @@ -92,6 +93,7 @@
"ceiling_division",
"compute_create_address",
"compute_create2_address",
"compute_create3_address",
"copy_opcode_cost",
"cost_memory_bytes",
"eip_2028_transaction_data_cost",
Expand Down
24 changes: 23 additions & 1 deletion src/ethereum_test_tools/common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,29 @@ def copy_opcode_cost(length: int) -> int:
return 3 + (ceiling_division(length, 32) * 3) + cost_memory_bytes(length, 0)


def eip_2028_transaction_data_cost(data: BytesConvertible) -> int:
def compute_create3_address(
address: str | int, salt: int, init_container: bytes
) -> str:
"""
Compute address of the resulting contract created using the `CREATE2`
opcode.
"""
ff = bytes([0xFF])
if type(address) is str:
if address.startswith("0x"):
address = address[2:]
address_bytes = bytes.fromhex(address)
elif type(address) is int:
address_bytes = address.to_bytes(length=20, byteorder="big")
salt_bytes = salt.to_bytes(length=32, byteorder="big")
initcode_hash = keccak256(init_container)
hash = keccak256(
ff + address_bytes + salt_bytes + initcode_hash
)
return "0x" + hash[-20:].hex()


def eip_2028_transaction_data_cost(data: bytes | str) -> int:
"""
Calculates the cost of a given data as part of a transaction, based on the
costs specified in EIP-2028: https://eips.ethereum.org/EIPS/eip-2028
Expand Down
6 changes: 5 additions & 1 deletion src/ethereum_test_tools/common/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,11 @@ def check_alloc(self: "Account", address: str, alloc: dict):
)

if self.code is not None:
expected_code = Bytes(self.code).hex()
expected_code = None
if not isinstance(self.code, Bytes):
expected_code = "0x" + self.code.assemble().hex()
else:
expected_code = Bytes(self.code).hex()
actual_code = str_or_none(alloc.get("code"), "0x")
if expected_code != actual_code:
raise Account.CodeMismatch(
Expand Down
6 changes: 6 additions & 0 deletions src/ethereum_test_tools/eof/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
EVM Object Format Libary to generate bytecode for testing purposes
"""
from .constants import LATEST_EOF_VERSION

__all__ = ("LATEST_EOF_VERSION",)
17 changes: 17 additions & 0 deletions src/ethereum_test_tools/eof/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
EVM Object Format generic constants.
Applicable to all EOF versions.
"""
EOF_MAGIC = bytes([0])
"""
The second byte found on every EOF formatted contract, which was chosen to
avoid clashes with three contracts which were deployed on Mainnet.
"""
EOF_HEADER_TERMINATOR = bytes([0])
"""
Byte that terminates the header of the EOF format.
"""
LATEST_EOF_VERSION = 1
"""
Latest existing EOF version.
"""
Loading