diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index b9af2b942f..561eb3313e 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -12,6 +12,7 @@ Test fixtures for use by clients are available for each release on the [Github r - ✨ Improve handling of the argument passed to `solc --evm-version` when compiling Yul code ([#418](https://github.com/ethereum/execution-spec-tests/pull/418)). - 🐞 Fix `fill -m yul_test` which failed to filter tests that are (dynamically) marked as a yul test ([#418](https://github.com/ethereum/execution-spec-tests/pull/418)). +- 🔀 Helper methods `to_address`, `to_hash` and `to_hash_bytes` have been deprecated in favor of `Address` and `Hash`, which are automatically detected as opcode parameters and pushed to the stack in the resulting bytecode ([#422](https://github.com/ethereum/execution-spec-tests/pull/422)). ### 🔧 EVM Tools diff --git a/src/ethereum_test_tools/__init__.py b/src/ethereum_test_tools/__init__.py index c5965f64b9..7d6d144f74 100644 --- a/src/ethereum_test_tools/__init__.py +++ b/src/ethereum_test_tools/__init__.py @@ -17,10 +17,11 @@ from .common import ( AccessList, Account, + Address, Auto, EngineAPIError, Environment, - HistoryStorageAddress, + Hash, JSONEncoder, Removable, Storage, @@ -38,9 +39,6 @@ copy_opcode_cost, cost_memory_bytes, eip_2028_transaction_data_cost, - to_address, - to_hash, - to_hash_bytes, transaction_list_root, ) from .exceptions import BlockException, ExceptionList, ExceptionType, TransactionException @@ -63,6 +61,7 @@ "SPEC_TYPES", "AccessList", "Account", + "Address", "Auto", "BaseFixture", "BaseTest", @@ -80,8 +79,8 @@ "ExceptionList", "ExceptionType", "FixtureCollector", + "Hash", "Header", - "HistoryStorageAddress", "Initcode", "JSONEncoder", "Opcode", @@ -113,8 +112,5 @@ "cost_memory_bytes", "eip_2028_transaction_data_cost", "eip_2028_transaction_data_cost", - "to_address", - "to_hash_bytes", - "to_hash", "transaction_list_root", ) diff --git a/src/ethereum_test_tools/common/__init__.py b/src/ethereum_test_tools/common/__init__.py index 6a09c4b59c..e8d750d403 100644 --- a/src/ethereum_test_tools/common/__init__.py +++ b/src/ethereum_test_tools/common/__init__.py @@ -1,12 +1,21 @@ """ Common definitions and types. """ +from .base_types import ( + Address, + Bloom, + Bytes, + Hash, + HeaderNonce, + HexNumber, + Number, + ZeroPaddedHexNumber, +) from .constants import ( AddrAA, AddrBB, EmptyTrieRoot, EngineAPIError, - HistoryStorageAddress, TestAddress, TestAddress2, TestPrivateKey, @@ -21,29 +30,19 @@ copy_opcode_cost, cost_memory_bytes, eip_2028_transaction_data_cost, - to_address, - to_hash, - to_hash_bytes, ) from .json import to_json from .types import ( AccessList, Account, - Address, Alloc, Auto, - Bloom, - Bytes, Environment, - Hash, - HeaderNonce, JSONEncoder, - Number, Removable, Storage, Transaction, Withdrawal, - ZeroPaddedHexNumber, alloc_to_accounts, serialize_transactions, str_or_none, @@ -66,7 +65,7 @@ "Environment", "Hash", "HeaderNonce", - "HistoryStorageAddress", + "HexNumber", "JSONEncoder", "Number", "Removable", @@ -89,9 +88,6 @@ "eip_2028_transaction_data_cost", "serialize_transactions", "str_or_none", - "to_address", - "to_hash_bytes", - "to_hash", "to_json", "transaction_list_root", "withdrawals_root", diff --git a/src/ethereum_test_tools/common/base_types.py b/src/ethereum_test_tools/common/base_types.py new file mode 100644 index 0000000000..897dcc4797 --- /dev/null +++ b/src/ethereum_test_tools/common/base_types.py @@ -0,0 +1,219 @@ +""" +Basic type primitives used to define other types. +""" + + +from typing import ClassVar, SupportsBytes, Type, TypeVar + +from .conversions import ( + BytesConvertible, + FixedSizeBytesConvertible, + NumberConvertible, + to_bytes, + to_fixed_size_bytes, + to_number, +) +from .json import JSONEncoder, SupportsJSON + +N = TypeVar("N", bound="Number") + + +class Number(int, SupportsJSON): + """ + Class that helps represent numbers in tests. + """ + + def __new__(cls, input: NumberConvertible | N): + """ + Creates a new Number object. + """ + return super(Number, cls).__new__(cls, to_number(input)) + + def __str__(self) -> str: + """ + Returns the string representation of the number. + """ + return str(int(self)) + + def __json__(self, encoder: JSONEncoder) -> str: + """ + Returns the JSON representation of the number. + """ + return str(self) + + def hex(self) -> str: + """ + Returns the hexadecimal representation of the number. + """ + return hex(self) + + @classmethod + def or_none(cls: Type[N], input: N | NumberConvertible | None) -> N | None: + """ + Converts the input to a Number while accepting None. + """ + if input is None: + return input + return cls(input) + + +class HexNumber(Number): + """ + Class that helps represent an hexadecimal numbers in tests. + """ + + def __str__(self) -> str: + """ + Returns the string representation of the number. + """ + return self.hex() + + +class ZeroPaddedHexNumber(HexNumber): + """ + Class that helps represent zero padded hexadecimal numbers in tests. + """ + + def hex(self) -> str: + """ + Returns the hexadecimal representation of the number. + """ + if self == 0: + return "0x00" + hex_str = hex(self)[2:] + if len(hex_str) % 2 == 1: + return "0x0" + hex_str + return "0x" + hex_str + + +class Bytes(bytes, SupportsJSON): + """ + Class that helps represent bytes of variable length in tests. + """ + + def __new__(cls, input: BytesConvertible): + """ + Creates a new Bytes object. + """ + return super(Bytes, cls).__new__(cls, to_bytes(input)) + + def __hash__(self) -> int: + """ + Returns the hash of the bytes. + """ + return super(Bytes, self).__hash__() + + def __str__(self) -> str: + """ + Returns the hexadecimal representation of the bytes. + """ + return self.hex() + + def __json__(self, encoder: JSONEncoder) -> str: + """ + Returns the JSON representation of the bytes. + """ + return str(self) + + def hex(self, *args, **kwargs) -> str: + """ + Returns the hexadecimal representation of the bytes. + """ + return "0x" + super().hex(*args, **kwargs) + + @classmethod + def or_none(cls, input: "Bytes | BytesConvertible | None") -> "Bytes | None": + """ + Converts the input to a Bytes while accepting None. + """ + if input is None: + return input + return cls(input) + + +T = TypeVar("T", bound="FixedSizeBytes") + + +class FixedSizeBytes(Bytes): + """ + Class that helps represent bytes of fixed length in tests. + """ + + byte_length: ClassVar[int] + + def __class_getitem__(cls, length: int) -> Type["FixedSizeBytes"]: + """ + Creates a new FixedSizeBytes class with the given length. + """ + + class Sized(cls): # type: ignore + byte_length = length + + return Sized + + def __new__(cls, input: FixedSizeBytesConvertible | T): + """ + Creates a new FixedSizeBytes object. + """ + return super(FixedSizeBytes, cls).__new__(cls, to_fixed_size_bytes(input, cls.byte_length)) + + def __hash__(self) -> int: + """ + Returns the hash of the bytes. + """ + return super(FixedSizeBytes, self).__hash__() + + @classmethod + def or_none(cls: Type[T], input: T | FixedSizeBytesConvertible | None) -> T | None: + """ + Converts the input to a Fixed Size Bytes while accepting None. + """ + if input is None: + return input + return cls(input) + + def __eq__(self, other: object) -> bool: + """ + Compares two FixedSizeBytes objects. + """ + if not isinstance(other, FixedSizeBytes): + assert ( + isinstance(other, str) + or isinstance(other, int) + or isinstance(other, bytes) + or isinstance(other, SupportsBytes) + ) + other = self.__class__(other) + return super().__eq__(other) + + +class Address(FixedSizeBytes[20]): # type: ignore + """ + Class that helps represent Ethereum addresses in tests. + """ + + pass + + +class Hash(FixedSizeBytes[32]): # type: ignore + """ + Class that helps represent hashes in tests. + """ + + pass + + +class Bloom(FixedSizeBytes[256]): # type: ignore + """ + Class that helps represent blooms in tests. + """ + + pass + + +class HeaderNonce(FixedSizeBytes[8]): # type: ignore + """ + Class that helps represent the header nonce in tests. + """ + + pass diff --git a/src/ethereum_test_tools/common/constants.py b/src/ethereum_test_tools/common/constants.py index 06d06da0f3..14f3b0364e 100644 --- a/src/ethereum_test_tools/common/constants.py +++ b/src/ethereum_test_tools/common/constants.py @@ -4,23 +4,23 @@ from enum import IntEnum -TestAddress = "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b" -TestAddress2 = "0x8a0a19589531694250d570040a0c4b74576919b8" +from .base_types import Address + +TestAddress = Address("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b") +TestAddress2 = Address("0x8a0a19589531694250d570040a0c4b74576919b8") TestPrivateKey = "0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8" TestPrivateKey2 = "0x9e7645d0cfd9c3a04eb7a9db59a4eb7d359f2e75c9164a9d6b9a7d54e1b6a36f" -AddrAA = "0x00000000000000000000000000000000000000aa" -AddrBB = "0x00000000000000000000000000000000000000bb" +AddrAA = Address(0xAA) +AddrBB = Address(0xBB) EmptyBloom = bytes([0] * 256) EmptyOmmersRoot = bytes.fromhex("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347") EmptyTrieRoot = bytes.fromhex("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") EmptyHash = bytes([0] * 32) EmptyNonce = bytes([0] * 8) -ZeroAddress = bytes([0] * 20) - -HistoryStorageAddress = "0x000000000000000000000000000000000000000b" +ZeroAddress = Address(0x00) class EngineAPIError(IntEnum): diff --git a/src/ethereum_test_tools/common/helpers.py b/src/ethereum_test_tools/common/helpers.py index 71acecb920..f25207cd06 100644 --- a/src/ethereum_test_tools/common/helpers.py +++ b/src/ethereum_test_tools/common/helpers.py @@ -8,8 +8,8 @@ from ethereum.crypto.hash import keccak256 from ethereum.rlp import encode +from .base_types import Address, Bytes, Hash from .conversions import BytesConvertible, FixedSizeBytesConvertible -from .types import Address, Bytes, Hash """ Helper functions @@ -24,25 +24,25 @@ def ceiling_division(a: int, b: int) -> int: return -(a // -b) -def compute_create_address(address: FixedSizeBytesConvertible, nonce: int) -> str: +def compute_create_address(address: FixedSizeBytesConvertible, nonce: int) -> Address: """ Compute address of the resulting contract created using a transaction or the `CREATE` opcode. """ nonce_bytes = bytes() if nonce == 0 else nonce.to_bytes(length=1, byteorder="big") hash = keccak256(encode([Address(address), nonce_bytes])) - return "0x" + hash[-20:].hex() + return Address(hash[-20:]) def compute_create2_address( address: FixedSizeBytesConvertible, salt: FixedSizeBytesConvertible, initcode: BytesConvertible -) -> str: +) -> Address: """ Compute address of the resulting contract created using the `CREATE2` opcode. """ hash = keccak256(b"\xff" + Address(address) + Hash(salt) + keccak256(Bytes(initcode))) - return "0x" + hash[-20:].hex() + return Address(hash[-20:]) def cost_memory_bytes(new_bytes: int, previous_bytes: int) -> int: @@ -85,27 +85,6 @@ def eip_2028_transaction_data_cost(data: BytesConvertible) -> int: return cost -def to_address(input: FixedSizeBytesConvertible) -> str: - """ - Converts an int or str into proper address 20-byte hex string. - """ - return str(Address(input)) - - -def to_hash_bytes(input: FixedSizeBytesConvertible) -> bytes: - """ - Converts an int or str into proper 32-byte hash. - """ - return bytes(Hash(input)) - - -def to_hash(input: FixedSizeBytesConvertible) -> str: - """ - Converts an int or str into proper 32-byte hash hex string. - """ - return str(Hash(input)) - - def add_kzg_version( b_hashes: List[bytes | SupportsBytes | int | str], kzg_version: int ) -> List[bytes]: @@ -116,8 +95,9 @@ def add_kzg_version( kzg_versioned_hashes = [] for hash in b_hashes: + hash = bytes(Hash(hash)) if isinstance(hash, int) or isinstance(hash, str): - kzg_versioned_hashes.append(kzg_version_hex + to_hash_bytes(hash)[1:]) + kzg_versioned_hashes.append(kzg_version_hex + hash[1:]) elif isinstance(hash, bytes) or isinstance(hash, SupportsBytes): if isinstance(hash, SupportsBytes): hash = bytes(hash) diff --git a/src/ethereum_test_tools/common/types.py b/src/ethereum_test_tools/common/types.py index dfcce4e783..113ed8fe22 100644 --- a/src/ethereum_test_tools/common/types.py +++ b/src/ethereum_test_tools/common/types.py @@ -16,7 +16,6 @@ SupportsBytes, Type, TypeAlias, - TypeVar, ) from coincurve.keys import PrivateKey, PublicKey @@ -28,16 +27,14 @@ from ethereum_test_forks import Fork from ..exceptions import ExceptionList, TransactionException -from .constants import AddrAA, TestPrivateKey +from .base_types import Address, Bytes, Hash, HexNumber, Number, ZeroPaddedHexNumber +from .constants import TestPrivateKey from .conversions import ( BytesConvertible, FixedSizeBytesConvertible, NumberConvertible, int_or_none, str_or_none, - to_bytes, - to_fixed_size_bytes, - to_number, ) from .json import JSONEncoder, SupportsJSON, field @@ -63,187 +60,6 @@ def __repr__(self) -> str: return "auto" -# Basic Types - - -N = TypeVar("N", bound="Number") - - -class Number(int, SupportsJSON): - """ - Class that helps represent numbers in tests. - """ - - def __new__(cls, input: NumberConvertible | N): - """ - Creates a new Number object. - """ - return super(Number, cls).__new__(cls, to_number(input)) - - def __str__(self) -> str: - """ - Returns the string representation of the number. - """ - return str(int(self)) - - def __json__(self, encoder: JSONEncoder) -> str: - """ - Returns the JSON representation of the number. - """ - return str(self) - - def hex(self) -> str: - """ - Returns the hexadecimal representation of the number. - """ - return hex(self) - - @classmethod - def or_none(cls: Type[N], input: N | NumberConvertible | None) -> N | None: - """ - Converts the input to a Number while accepting None. - """ - if input is None: - return input - return cls(input) - - -class HexNumber(Number): - """ - Class that helps represent an hexadecimal numbers in tests. - """ - - def __str__(self) -> str: - """ - Returns the string representation of the number. - """ - return self.hex() - - -class ZeroPaddedHexNumber(HexNumber): - """ - Class that helps represent zero padded hexadecimal numbers in tests. - """ - - def hex(self) -> str: - """ - Returns the hexadecimal representation of the number. - """ - if self == 0: - return "0x00" - hex_str = hex(self)[2:] - if len(hex_str) % 2 == 1: - return "0x0" + hex_str - return "0x" + hex_str - - -class Bytes(bytes, SupportsJSON): - """ - Class that helps represent bytes of variable length in tests. - """ - - def __new__(cls, input: BytesConvertible): - """ - Creates a new Bytes object. - """ - return super(Bytes, cls).__new__(cls, to_bytes(input)) - - def __str__(self) -> str: - """ - Returns the hexadecimal representation of the bytes. - """ - return self.hex() - - def __json__(self, encoder: JSONEncoder) -> str: - """ - Returns the JSON representation of the bytes. - """ - return str(self) - - def hex(self, *args, **kwargs) -> str: - """ - Returns the hexadecimal representation of the bytes. - """ - return "0x" + super().hex(*args, **kwargs) - - @classmethod - def or_none(cls, input: "Bytes | BytesConvertible | None") -> "Bytes | None": - """ - Converts the input to a Bytes while accepting None. - """ - if input is None: - return input - return cls(input) - - -T = TypeVar("T", bound="FixedSizeBytes") - - -class FixedSizeBytes(Bytes): - """ - Class that helps represent bytes of fixed length in tests. - """ - - byte_length: ClassVar[int] - - def __class_getitem__(cls, length: int) -> Type["FixedSizeBytes"]: - """ - Creates a new FixedSizeBytes class with the given length. - """ - - class Sized(cls): # type: ignore - byte_length = length - - return Sized - - def __new__(cls, input: FixedSizeBytesConvertible | T): - """ - Creates a new FixedSizeBytes object. - """ - return super(FixedSizeBytes, cls).__new__(cls, to_fixed_size_bytes(input, cls.byte_length)) - - @classmethod - def or_none(cls: Type[T], input: T | FixedSizeBytesConvertible | None) -> T | None: - """ - Converts the input to a Fixed Size Bytes while accepting None. - """ - if input is None: - return input - return cls(input) - - -class Address(FixedSizeBytes[20]): # type: ignore - """ - Class that helps represent Ethereum addresses in tests. - """ - - pass - - -class Hash(FixedSizeBytes[32]): # type: ignore - """ - Class that helps represent hashes in tests. - """ - - pass - - -class Bloom(FixedSizeBytes[256]): # type: ignore - """ - Class that helps represent blooms in tests. - """ - - pass - - -class HeaderNonce(FixedSizeBytes[8]): # type: ignore - """ - Class that helps represent the header nonce in tests. - """ - - pass - - MAX_STORAGE_KEY_VALUE = 2**256 - 1 MIN_STORAGE_KEY_VALUE = -(2**255) @@ -353,12 +169,12 @@ class KeyValueMismatch(Exception): was different. """ - address: str + address: Address key: int want: int got: int - def __init__(self, address: str, key: int, want: int, got: int, *args): + def __init__(self, address: Address, key: int, want: int, got: int, *args): super().__init__(args) self.address = address self.key = key @@ -486,7 +302,7 @@ def contains(self, other: "Storage") -> bool: return False return True - def must_contain(self, address: str, other: "Storage"): + def must_contain(self, address: Address, other: "Storage"): """ Succeeds only if self contains all keys with equal value as contained by second storage. @@ -504,7 +320,7 @@ def must_contain(self, address: str, other: "Storage"): address=address, key=key, want=self.data[key], got=other.data[key] ) - def must_be_equal(self, address: str, other: "Storage"): + def must_be_equal(self, address: Address, other: "Storage"): """ Succeeds only if "self" is equal to "other" storage. """ @@ -596,11 +412,11 @@ class NonceMismatch(Exception): value was found. """ - address: str + address: Address want: int | None got: int | None - def __init__(self, address: str, want: int | None, got: int | None, *args): + def __init__(self, address: Address, want: int | None, got: int | None, *args): super().__init__(args) self.address = address self.want = want @@ -620,11 +436,11 @@ class BalanceMismatch(Exception): value was found. """ - address: str + address: Address want: int | None got: int | None - def __init__(self, address: str, want: int | None, got: int | None, *args): + def __init__(self, address: Address, want: int | None, got: int | None, *args): super().__init__(args) self.address = address self.want = want @@ -644,11 +460,11 @@ class CodeMismatch(Exception): one was found. """ - address: str + address: Address want: str | None got: str | None - def __init__(self, address: str, want: str | None, got: str | None, *args): + def __init__(self, address: Address, want: str | None, got: str | None, *args): super().__init__(args) self.address = address self.want = want @@ -661,7 +477,7 @@ def __str__(self): + f"want {self.want}, got {self.got}" ) - def check_alloc(self: "Account", address: str, alloc: dict): + def check_alloc(self: "Account", address: Address, alloc: dict): """ Checks the returned alloc against an expected account in post state. Raises exception on failure. @@ -1163,8 +979,8 @@ class Transaction: cast_type=HexNumber, ), ) - to: Optional[FixedSizeBytesConvertible] = field( - default=AddrAA, + to: Optional[FixedSizeBytesConvertible | Address] = field( + default=Address(0xAA), json_encoder=JSONEncoder.Field( cast_type=Address, ), diff --git a/src/ethereum_test_tools/spec/base/base_test.py b/src/ethereum_test_tools/spec/base/base_test.py index 641aeb2638..cd436de46c 100644 --- a/src/ethereum_test_tools/spec/base/base_test.py +++ b/src/ethereum_test_tools/spec/base/base_test.py @@ -48,11 +48,11 @@ def verify_post_alloc(expected_post: Mapping, got_alloc: Mapping): Verify that an allocation matches the expected post in the test. Raises exception on unexpected values. """ - got_alloc_normalized: Dict[str, Any] = { - Address(address).hex(): got_alloc[address] for address in got_alloc + got_alloc_normalized: Dict[Address, Any] = { + Address(address): got_alloc[address] for address in got_alloc } for address, account in expected_post.items(): - address = Address(address).hex() + address = Address(address) if account is not None: if account == Account.NONEXISTENT: if address in got_alloc_normalized: diff --git a/src/ethereum_test_tools/spec/blockchain/types.py b/src/ethereum_test_tools/spec/blockchain/types.py index 1d849d333f..61b1932d83 100644 --- a/src/ethereum_test_tools/spec/blockchain/types.py +++ b/src/ethereum_test_tools/spec/blockchain/types.py @@ -14,25 +14,26 @@ from ethereum_test_forks import Fork from evm_transition_tool import FixtureFormats -from ...common.constants import EmptyOmmersRoot, EngineAPIError -from ...common.conversions import BytesConvertible, FixedSizeBytesConvertible, NumberConvertible -from ...common.json import JSONEncoder, field, to_json -from ...common.types import ( - Account, - AddrAA, +from ...common.base_types import ( Address, - Alloc, Bloom, Bytes, - Environment, Hash, HeaderNonce, HexNumber, Number, + ZeroPaddedHexNumber, +) +from ...common.constants import AddrAA, EmptyOmmersRoot, EngineAPIError +from ...common.conversions import BytesConvertible, FixedSizeBytesConvertible, NumberConvertible +from ...common.json import JSONEncoder, field, to_json +from ...common.types import ( + Account, + Alloc, + Environment, Removable, Transaction, Withdrawal, - ZeroPaddedHexNumber, blob_versioned_hashes_from_transactions, transaction_list_to_serializable_list, ) diff --git a/src/ethereum_test_tools/spec/state/types.py b/src/ethereum_test_tools/spec/state/types.py index f2085827cb..88e0d9fa13 100644 --- a/src/ethereum_test_tools/spec/state/types.py +++ b/src/ethereum_test_tools/spec/state/types.py @@ -8,20 +8,10 @@ from evm_transition_tool import FixtureFormats -from ...common.conversions import BytesConvertible, FixedSizeBytesConvertible +from ...common.base_types import Address, Bytes, Hash, HexNumber, ZeroPaddedHexNumber +from ...common.conversions import BytesConvertible, FixedSizeBytesConvertible, NumberConvertible from ...common.json import JSONEncoder, field, to_json -from ...common.types import ( - AccessList, - Address, - Alloc, - Bytes, - Environment, - Hash, - HexNumber, - NumberConvertible, - Transaction, - ZeroPaddedHexNumber, -) +from ...common.types import AccessList, Alloc, Environment, Transaction from ...exceptions import ExceptionList, TransactionException from ..base.base_test import BaseFixture diff --git a/src/ethereum_test_tools/tests/test_code.py b/src/ethereum_test_tools/tests/test_code.py index 39277574b2..ceceefb52b 100644 --- a/src/ethereum_test_tools/tests/test_code.py +++ b/src/ethereum_test_tools/tests/test_code.py @@ -12,7 +12,7 @@ from evm_transition_tool import FixtureFormats, GethTransitionTool from ..code import CalldataCase, Case, Code, Conditional, Initcode, Switch, Yul -from ..common import Account, Environment, TestAddress, Transaction, to_hash_bytes +from ..common import Account, Environment, Hash, TestAddress, Transaction from ..spec import StateTest from ..vm.opcode import Opcodes as Op from .conftest import SOLC_PADDING_VERSION @@ -317,7 +317,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): "tx_data,switch_bytecode,expected_storage", [ pytest.param( - to_hash_bytes(1), + Hash(1), Switch( cases=[ Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1)), @@ -329,7 +329,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="no-default-action-condition-met", ), pytest.param( - to_hash_bytes(1), + Hash(1), Switch( cases=[ CalldataCase(value=1, action=Op.SSTORE(0, 1)), @@ -341,7 +341,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="no-default-action-condition-met-calldata", ), pytest.param( - to_hash_bytes(0), + Hash(0), Switch( cases=[ Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1)), @@ -353,7 +353,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="no-default-action-no-condition-met", ), pytest.param( - to_hash_bytes(1), + Hash(1), Switch( cases=[], default_action=Op.SSTORE(0, 3), @@ -362,7 +362,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="no-cases", ), pytest.param( - to_hash_bytes(1), + Hash(1), Switch( cases=[Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1))], default_action=Op.SSTORE(0, 3), @@ -371,7 +371,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="one-case-condition-met", ), pytest.param( - to_hash_bytes(0), + Hash(0), Switch( cases=[Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1))], default_action=Op.SSTORE(0, 3), @@ -380,7 +380,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="one-case-condition-not-met", ), pytest.param( - to_hash_bytes(0), + Hash(0), Switch( cases=[ Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1)), @@ -392,7 +392,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="two-cases-no-condition-met", ), pytest.param( - to_hash_bytes(1), + Hash(1), Switch( cases=[ Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1)), @@ -404,7 +404,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="two-cases-first-condition-met", ), pytest.param( - to_hash_bytes(2), + Hash(2), Switch( cases=[ Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1)), @@ -416,7 +416,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="two-cases-second-condition-met", ), pytest.param( - to_hash_bytes(1), + Hash(1), Switch( cases=[ Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1)), @@ -431,7 +431,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="five-cases-first-condition-met", ), pytest.param( - to_hash_bytes(1), + Hash(1), Switch( cases=[ CalldataCase(value=1, action=Op.SSTORE(0, 1)), @@ -446,7 +446,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="five-cases-first-condition-met-calldata", ), pytest.param( - to_hash_bytes(3), + Hash(3), Switch( cases=[ Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1)), @@ -461,7 +461,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="five-cases-third-condition-met", ), pytest.param( - to_hash_bytes(3), + Hash(3), Switch( cases=[ CalldataCase(value=1, action=Op.SSTORE(0, 1)), @@ -476,7 +476,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="five-cases-third-condition-met-calldata", ), pytest.param( - to_hash_bytes(5), + Hash(5), Switch( cases=[ Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1)), @@ -491,7 +491,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="five-cases-last-met", ), pytest.param( - to_hash_bytes(3), + Hash(3), Switch( cases=[ Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1)), @@ -506,7 +506,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="five-cases-multiple-conditions-met", # first in list should be evaluated ), pytest.param( - to_hash_bytes(9), + Hash(9), Switch( cases=[ Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1)), @@ -521,7 +521,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="five-cases-no-condition-met", ), pytest.param( - to_hash_bytes(0), + Hash(0), Switch( cases=[ Case(condition=Op.EQ(1, 2), action=Op.SSTORE(0, 1)), @@ -536,7 +536,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="no-calldataload-condition-met", ), pytest.param( - to_hash_bytes(0), + Hash(0), Switch( cases=[ Case(condition=Op.EQ(1, 2), action=Op.SSTORE(0, 1)), @@ -554,7 +554,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="no-calldataload-condition-met-different-length-actions", ), pytest.param( - to_hash_bytes(0), + Hash(0), Switch( cases=[ Case( @@ -584,7 +584,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="different-length-conditions-condition-met-different-length-actions", ), pytest.param( - to_hash_bytes(0), + Hash(0), Op.SSTORE(0x10, 1) + Switch( cases=[ @@ -616,7 +616,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="nested-within-bytecode", ), pytest.param( - to_hash_bytes(1), + Hash(1), Switch( cases=[Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1))], default_action=Op.PUSH32(2**256 - 1) * 8, @@ -625,7 +625,7 @@ def test_opcodes_if(conditional_bytecode: bytes, expected: bytes): id="jumpi-larger-than-1-byte", ), pytest.param( - to_hash_bytes(1), + Hash(1), Switch( cases=[Case(condition=Op.EQ(Op.CALLDATALOAD(0), 1), action=Op.SSTORE(0, 1))], default_action=Op.PUSH32(2**256 - 1) * 2048, diff --git a/src/ethereum_test_tools/tests/test_filling/test_expect.py b/src/ethereum_test_tools/tests/test_filling/test_expect.py index 02b0a3ee0c..9456a09cf1 100644 --- a/src/ethereum_test_tools/tests/test_filling/test_expect.py +++ b/src/ethereum_test_tools/tests/test_filling/test_expect.py @@ -8,11 +8,11 @@ from ethereum_test_forks import Fork, get_deployed_forks from evm_transition_tool import FixtureFormats, GethTransitionTool -from ...common import Account, Environment, Transaction, to_address +from ...common import Account, Address, Environment, Transaction from ...common.types import Storage from ...spec import StateTest -ADDRESS_UNDER_TEST = to_address(0x01) +ADDRESS_UNDER_TEST = Address(0x01) @pytest.fixture @@ -206,7 +206,7 @@ def test_post_balance_value_mismatch(pre, post, state_test, t8n, fork): ), ( {ADDRESS_UNDER_TEST: Account(balance=1)}, - {ADDRESS_UNDER_TEST: Account(balance=1), to_address(0x02): Account(balance=1)}, + {ADDRESS_UNDER_TEST: Account(balance=1), Address(0x02): Account(balance=1)}, "expected account not found", ), ( diff --git a/src/ethereum_test_tools/tests/test_helpers.py b/src/ethereum_test_tools/tests/test_helpers.py index b8227174b4..bf65d4e81d 100644 --- a/src/ethereum_test_tools/tests/test_helpers.py +++ b/src/ethereum_test_tools/tests/test_helpers.py @@ -4,19 +4,21 @@ import pytest -from ..common import compute_create2_address, compute_create_address, to_address +from ..common import Address, compute_create2_address, compute_create_address -def test_to_address(): +def test_address(): """ - Test `ethereum_test.helpers.to_address`. + Test `ethereum_test.base_types.Address`. """ - assert to_address("0x0") == "0x0000000000000000000000000000000000000000" - assert to_address(0) == "0x0000000000000000000000000000000000000000" - assert to_address(1) == "0x0000000000000000000000000000000000000001" - assert to_address(10) == "0x000000000000000000000000000000000000000a" - assert to_address("0x10") == "0x0000000000000000000000000000000000000010" - assert to_address(2 ** (20 * 8) - 1) == "0xffffffffffffffffffffffffffffffffffffffff" + assert Address("0x0") == "0x0000000000000000000000000000000000000000" + assert Address(0) == "0x0000000000000000000000000000000000000000" + assert Address(1) == "0x0000000000000000000000000000000000000001" + assert Address(10) == "0x000000000000000000000000000000000000000a" + assert Address("0x10") == "0x0000000000000000000000000000000000000010" + assert Address(2 ** (20 * 8) - 1) == "0xffffffffffffffffffffffffffffffffffffffff" + assert Address(0) == Address(0) + assert Address(0) != Address(1) @pytest.mark.parametrize( diff --git a/src/ethereum_test_tools/tests/test_types.py b/src/ethereum_test_tools/tests/test_types.py index 427c108a6e..54b130eac7 100644 --- a/src/ethereum_test_tools/tests/test_types.py +++ b/src/ethereum_test_tools/tests/test_types.py @@ -17,8 +17,9 @@ to_json, withdrawals_root, ) +from ..common.base_types import Address, Bloom, Bytes, Hash, HeaderNonce, ZeroPaddedHexNumber from ..common.constants import TestPrivateKey -from ..common.types import Address, Alloc, Bloom, Bytes, Hash, HeaderNonce, ZeroPaddedHexNumber +from ..common.types import Alloc from ..exceptions import BlockException, TransactionException from ..spec.blockchain.types import ( FixtureEngineNewPayload, @@ -277,10 +278,10 @@ def test_storage(): ) def test_account_check_alloc(account: Account, alloc: Dict[Any, Any], should_pass: bool): if should_pass: - account.check_alloc("test", alloc) + account.check_alloc(Address(1), alloc) else: with pytest.raises(Exception) as _: - account.check_alloc("test", alloc) + account.check_alloc(Address(1), alloc) @pytest.mark.parametrize( diff --git a/src/ethereum_test_tools/tests/test_types_blockchain_test.py b/src/ethereum_test_tools/tests/test_types_blockchain_test.py index 7637ac2c00..1f809ca0e9 100644 --- a/src/ethereum_test_tools/tests/test_types_blockchain_test.py +++ b/src/ethereum_test_tools/tests/test_types_blockchain_test.py @@ -5,7 +5,7 @@ import pytest -from ..common.types import Address, Bloom, Bytes, Hash, HeaderNonce +from ..common.base_types import Address, Bloom, Bytes, Hash, HeaderNonce from ..spec.blockchain.types import FixtureHeader, Header fixture_header_ones = FixtureHeader( diff --git a/src/ethereum_test_tools/tests/test_vm.py b/src/ethereum_test_tools/tests/test_vm.py index 18d18b19e4..a459880937 100644 --- a/src/ethereum_test_tools/tests/test_vm.py +++ b/src/ethereum_test_tools/tests/test_vm.py @@ -4,6 +4,7 @@ import pytest +from ..common.base_types import Address from ..vm.opcode import Opcodes as Op @@ -106,6 +107,16 @@ + [0x55] ), ), + ( + Op.CALL(Op.GAS, Op.PUSH20(0x1234), 0, 0, 0, 0, 32), + b"\x60\x20\x60\x00\x60\x00\x60\x00\x60\x00\x73\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + + b"\x00\x00\x00\x00\x00\x00\x00\x00\x12\x34\x5A\xF1", + ), + ( + Op.CALL(Op.GAS, Address(0x1234), 0, 0, 0, 0, 32), + b"\x60\x20\x60\x00\x60\x00\x60\x00\x60\x00\x73\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + + b"\x00\x00\x00\x00\x00\x00\x00\x00\x12\x34\x5A\xF1", + ), ], ) def test_opcodes(opcodes: bytes, expected: bytes): diff --git a/src/ethereum_test_tools/vm/opcode.py b/src/ethereum_test_tools/vm/opcode.py index 2cf78c0623..c0a3a8f691 100644 --- a/src/ethereum_test_tools/vm/opcode.py +++ b/src/ethereum_test_tools/vm/opcode.py @@ -4,6 +4,8 @@ from enum import Enum from typing import List, Union +from ..common.base_types import FixedSizeBytes + def _get_int_size(n: int) -> int: """ @@ -67,7 +69,7 @@ def __new__( obj.data_portion_length = data_portion_length return obj - def __call__(self, *args_t: Union[int, bytes, str, "Opcode"]) -> bytes: + def __call__(self, *args_t: Union[int, bytes, str, "Opcode", FixedSizeBytes]) -> bytes: """ Makes all opcode instances callable to return formatted bytecode, which constitutes a data portion, that is located after the opcode @@ -94,10 +96,14 @@ def __call__(self, *args_t: Union[int, bytes, str, "Opcode"]) -> bytes: automatically converted to PUSH operations, and negative numbers always use a PUSH32 operation. + `FixedSizeBytes` can also be used as stack elements, which includes + `Address` and `Hash` types, for each of which a PUSH operation is + automatically generated, `PUSH20` and `PUSH32` respectively. + Hex-strings will automatically be converted to bytes. """ - args: List[Union[int, bytes, str, "Opcode"]] = list(args_t) + args: List[Union[int, bytes, str, "Opcode", FixedSizeBytes]] = list(args_t) pre_opcode_bytecode = bytes() data_portion = bytes() @@ -127,29 +133,36 @@ def __call__(self, *args_t: Union[int, bytes, str, "Opcode"]) -> bytes: # The rest of the arguments conform the stack. while len(args) > 0: data = args.pop() - if isinstance(data, bytes) or isinstance(data, str): + if isinstance(data, int) or isinstance(data, FixedSizeBytes): + # We are going to push a constant to the stack. + data_size = 0 + if isinstance(data, int): + signed = data < 0 + data_size = _get_int_size(data) + if data_size > 32: + raise ValueError("Opcode stack data must be less than 32 bytes") + elif data_size == 0: + # Pushing 0 is done with the PUSH1 opcode for compatibility + # reasons. + data_size = 1 + data = data.to_bytes( + length=data_size, + byteorder="big", + signed=signed, + ) + elif isinstance(data, FixedSizeBytes): + data_size = data.byte_length + + assert isinstance(data, bytes) + assert data_size > 0 + pre_opcode_bytecode += _push_opcodes_byte_list[data_size] + pre_opcode_bytecode += data + elif isinstance(data, bytes) or isinstance(data, str): if isinstance(data, str): if data.startswith("0x"): data = data[2:] data = bytes.fromhex(data) pre_opcode_bytecode += data - elif isinstance(data, int): - # We are going to push a constant to the stack. - signed = data < 0 - data_size = _get_int_size(data) - if data_size > 32: - raise ValueError("Opcode stack data must be less than 32 bytes") - elif data_size == 0: - # Pushing 0 is done with the PUSH1 opcode for compatibility - # reasons. - data_size = 1 - - pre_opcode_bytecode += _push_opcodes_byte_list[data_size] - pre_opcode_bytecode += data.to_bytes( - length=data_size, - byteorder="big", - signed=signed, - ) else: raise TypeError("Opcode stack data must be either an int or a bytes/hex string") diff --git a/tests/byzantium/eip198_modexp_precompile/test_modexp.py b/tests/byzantium/eip198_modexp_precompile/test_modexp.py index 9b63e54704..239d9139f0 100644 --- a/tests/byzantium/eip198_modexp_precompile/test_modexp.py +++ b/tests/byzantium/eip198_modexp_precompile/test_modexp.py @@ -9,13 +9,13 @@ from ethereum_test_tools import ( Account, + Address, Environment, StateTestFiller, TestAddress, TestParameterGroup, Transaction, compute_create_address, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -213,7 +213,7 @@ def test_modexp(state_test: StateTestFiller, input: ModExpInput, output: Expecte env = Environment() pre = {TestAddress: Account(balance=1000000000000000000000)} - account = to_address(0x100) + account = Address(0x100) pre[account] = Account( code=( diff --git a/tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py b/tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py index 11aae7b041..6d730178f7 100644 --- a/tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py +++ b/tests/cancun/eip1153_tstore/test_tstorage_create_contexts.py @@ -8,7 +8,7 @@ import pytest -from ethereum_test_tools import Account, Environment, Initcode +from ethereum_test_tools import Account, Address, Environment, Initcode from ethereum_test_tools import Opcodes as Op from ethereum_test_tools import ( StateTestFiller, @@ -150,7 +150,7 @@ def creator_contract_code( # noqa: D102 self, opcode: Op, create2_salt: int, - created_contract_address: str, + created_contract_address: Address, ) -> bytes: if opcode == Op.CREATE: create_call = Op.CREATE(0, 0, Op.CALLDATASIZE) @@ -158,9 +158,7 @@ def creator_contract_code( # noqa: D102 create_call = Op.CREATE2(0, 0, Op.CALLDATASIZE, create2_salt) else: raise Exception("Invalid opcode specified for test.") - contract_call = Op.SSTORE( - 4, Op.CALL(Op.GAS(), Op.PUSH20(created_contract_address), 0, 0, 0, 0, 0) - ) + contract_call = Op.SSTORE(4, Op.CALL(Op.GAS(), created_contract_address, 0, 0, 0, 0, 0)) return ( Op.TSTORE(0, 0x0100) + Op.TSTORE(1, 0x0200) @@ -182,7 +180,7 @@ def expected_creator_storage(self) -> dict: # noqa: D102 @pytest.fixture() def created_contract_address( # noqa: D102 self, opcode: Op, create2_salt: int, initcode: bytes - ) -> str: + ) -> Address: if opcode == Op.CREATE: return compute_create_address(address=creator_address, nonce=1) if opcode == Op.CREATE2: @@ -194,8 +192,8 @@ def created_contract_address( # noqa: D102 def test_contract_creation( self, state_test: StateTestFiller, - creator_contract_code: str, - created_contract_address: str, + creator_contract_code: bytes, + created_contract_address: Address, initcode: bytes, deploy_code: bytes, expected_creator_storage: dict, diff --git a/tests/cancun/eip1153_tstore/test_tstorage_reentrancy_contexts.py b/tests/cancun/eip1153_tstore/test_tstorage_reentrancy_contexts.py index 7b7d58926b..3938609e47 100644 --- a/tests/cancun/eip1153_tstore/test_tstorage_reentrancy_contexts.py +++ b/tests/cancun/eip1153_tstore/test_tstorage_reentrancy_contexts.py @@ -7,9 +7,9 @@ import pytest -from ethereum_test_tools import Account, CalldataCase, Conditional, Environment +from ethereum_test_tools import Account, CalldataCase, Conditional, Environment, Hash from ethereum_test_tools import Opcodes as Op -from ethereum_test_tools import StateTestFiller, Switch, TestAddress, Transaction, to_hash_bytes +from ethereum_test_tools import StateTestFiller, Switch, TestAddress, Transaction from . import PytestParameterEnum from .spec import ref_spec_1153 @@ -272,7 +272,7 @@ def test_reentrant_call(state_test: StateTestFiller, bytecode, expected_storage) tx = Transaction( to=callee_address, - data=to_hash_bytes(1), + data=Hash(1), gas_limit=10_000_000, ) diff --git a/tests/cancun/eip1153_tstore/test_tstorage_selfdestruct.py b/tests/cancun/eip1153_tstore/test_tstorage_selfdestruct.py index 3709366c6a..5bd4f7964f 100644 --- a/tests/cancun/eip1153_tstore/test_tstorage_selfdestruct.py +++ b/tests/cancun/eip1153_tstore/test_tstorage_selfdestruct.py @@ -41,9 +41,7 @@ def call_option(option_number: int) -> bytes: """ Return the bytecode for a call to the callee contract with the given option number. """ - return Op.MSTORE(0, option_number) + Op.CALL( - Op.GAS, Op.PUSH20(callee_address), 0, 0, 32, 0, 32 - ) + return Op.MSTORE(0, option_number) + Op.CALL(Op.GAS, callee_address, 0, 0, 32, 0, 32) @unique diff --git a/tests/cancun/eip4788_beacon_root/conftest.py b/tests/cancun/eip4788_beacon_root/conftest.py index b6fb6d4255..e97f075914 100644 --- a/tests/cancun/eip4788_beacon_root/conftest.py +++ b/tests/cancun/eip4788_beacon_root/conftest.py @@ -10,13 +10,13 @@ from ethereum_test_tools import ( AccessList, Account, + Address, Environment, + Hash, Storage, TestAddress, Transaction, add_kzg_version, - to_address, - to_hash_bytes, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -51,7 +51,7 @@ def __next__(self) -> bytes: @pytest.fixture def beacon_root(request, beacon_roots: Iterator[bytes]) -> bytes: # noqa: D103 - return to_hash_bytes(request.param) if hasattr(request, "param") else next(beacon_roots) + return Hash(request.param) if hasattr(request, "param") else next(beacon_roots) @pytest.fixture @@ -86,8 +86,8 @@ def call_gas() -> int: # noqa: D103 @pytest.fixture -def caller_address() -> str: # noqa: D103 - return to_address(0x100) +def caller_address() -> Address: # noqa: D103 + return Address(0x100) @pytest.fixture @@ -177,7 +177,7 @@ def system_address_balance() -> int: def pre( contract_call_account: Account, system_address_balance: int, - caller_address: str, + caller_address: Address, ) -> Dict: """ Prepares the pre state of all test cases, by setting the balance of the @@ -191,7 +191,7 @@ def pre( caller_address: contract_call_account, } if system_address_balance > 0: - pre_alloc[to_address(Spec.SYSTEM_ADDRESS)] = Account( + pre_alloc[Address(Spec.SYSTEM_ADDRESS)] = Account( nonce=0, balance=system_address_balance, ) @@ -199,7 +199,7 @@ def pre( @pytest.fixture -def tx_to_address(request, caller_address: Account) -> bytes: # noqa: D103 +def tx_to_address(request, caller_address: Account) -> Address: # noqa: D103 return request.param if hasattr(request, "param") else caller_address @@ -234,7 +234,7 @@ def tx_data(timestamp: int) -> bytes: """ Data included in the transaction to call the beacon root contract. """ - return to_hash_bytes(timestamp) + return Hash(timestamp) @pytest.fixture @@ -249,7 +249,7 @@ def tx_type() -> int: @pytest.fixture def tx( - tx_to_address: str, + tx_to_address: Address, tx_data: bytes, tx_type: int, access_list: List[AccessList], @@ -289,7 +289,7 @@ def tx( @pytest.fixture def post( - caller_address: str, + caller_address: Address, beacon_root: bytes, valid_call: bool, valid_input: bool, diff --git a/tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py b/tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py index ee1e7f3665..f36b9dc890 100644 --- a/tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py +++ b/tests/cancun/eip4788_beacon_root/test_beacon_root_contract.py @@ -23,11 +23,11 @@ from ethereum_test_tools import ( Account, + Address, Environment, StateTestFiller, Storage, Transaction, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -266,23 +266,23 @@ def test_beacon_root_selfdestruct( Tests that self destructing the beacon root address transfers actors balance correctly. """ # self destruct actor - pre[to_address(0x1337)] = Account( + pre[Address(0x1337)] = Account( code=Op.SELFDESTRUCT(Spec.BEACON_ROOTS_ADDRESS), balance=0xBA1, ) # self destruct caller - pre[to_address(0xCC)] = Account( - code=Op.CALL(100000, Op.PUSH20(to_address(0x1337)), 0, 0, 0, 0, 0) + pre[Address(0xCC)] = Account( + code=Op.CALL(100000, Address(0x1337), 0, 0, 0, 0, 0) + Op.SSTORE(0, Op.BALANCE(Spec.BEACON_ROOTS_ADDRESS)), ) post = { - to_address(0xCC): Account( + Address(0xCC): Account( storage=Storage({0: 0xBA1}), ) } state_test( env=env, pre=pre, - tx=Transaction(nonce=0, to=to_address(0xCC), gas_limit=100000, gas_price=10), + tx=Transaction(nonce=0, to=Address(0xCC), gas_limit=100000, gas_price=10), post=post, ) diff --git a/tests/cancun/eip4788_beacon_root/test_blocks_beacon_root_contract.py b/tests/cancun/eip4788_beacon_root/test_blocks_beacon_root_contract.py index 5aba3f9694..1dff686ab8 100644 --- a/tests/cancun/eip4788_beacon_root/test_blocks_beacon_root_contract.py +++ b/tests/cancun/eip4788_beacon_root/test_blocks_beacon_root_contract.py @@ -25,14 +25,14 @@ from ethereum_test_forks import Fork from ethereum_test_tools import ( Account, + Address, Block, BlockchainTestFiller, + Hash, Storage, TestAddress, Transaction, Withdrawal, - to_address, - to_hash_bytes, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -131,7 +131,7 @@ def test_multi_block_beacon_root_timestamp_calls( current_call_account_code = bytes() current_call_account_expected_storage = Storage() - current_call_account_address = to_address(0x100 + i) + current_call_account_address = Address(0x100 + i) # We are going to call the beacon roots contract once for every timestamp of the current # and all previous blocks, and check that the returned beacon root is still correct only @@ -173,8 +173,8 @@ def test_multi_block_beacon_root_timestamp_calls( txs=[ tx.with_fields( nonce=i, - to=to_address(0x100 + i), - data=to_hash_bytes(timestamp), + to=Address(0x100 + i), + data=Hash(timestamp), ) ], beacon_root=beacon_root, @@ -255,7 +255,7 @@ def test_beacon_root_transition( current_call_account_code = bytes() current_call_account_expected_storage = Storage() - current_call_account_address = to_address(0x100 + i) + current_call_account_address = Address(0x100 + i) # We are going to call the beacon roots contract once for every timestamp of the current # and all previous blocks, and check that the returned beacon root is correct only @@ -298,8 +298,8 @@ def test_beacon_root_transition( txs=[ tx.with_fields( nonce=i, - to=to_address(0x100 + i), - data=to_hash_bytes(timestamp), + to=Address(0x100 + i), + data=Hash(timestamp), ) ], beacon_root=beacon_root if transitioned else None, @@ -337,7 +337,7 @@ def test_no_beacon_root_contract_at_transition( beacon_roots: Iterator[bytes], tx: Transaction, timestamp: int, - caller_address: str, + caller_address: Address, fork: Fork, ): """ @@ -439,7 +439,7 @@ def test_beacon_root_contract_deploy( ).with_signature_and_sender() deployer_address = deploy_tx.sender assert deployer_address is not None - assert deployer_address == int.to_bytes(Spec.BEACON_ROOTS_DEPLOYER_ADDRESS, 20, "big") + assert Address(deployer_address) == Spec.BEACON_ROOTS_DEPLOYER_ADDRESS blocks: List[Block] = [] beacon_root_contract_storage: Dict = {} diff --git a/tests/cancun/eip4844_blobs/common.py b/tests/cancun/eip4844_blobs/common.py index 4b63233a06..aca1932326 100644 --- a/tests/cancun/eip4844_blobs/common.py +++ b/tests/cancun/eip4844_blobs/common.py @@ -5,12 +5,12 @@ from typing import List, Literal, Tuple, Union from ethereum_test_tools import ( + Address, TestAddress, YulCompiler, add_kzg_version, compute_create2_address, compute_create_address, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -100,14 +100,14 @@ class BlobhashContext: yul_compiler: Union[YulCompiler, None] = None addresses = { - "blobhash_sstore": to_address(0x100), - "blobhash_return": to_address(0x600), - "call": to_address(0x200), - "delegatecall": to_address(0x300), - "callcode": to_address(0x800), - "staticcall": to_address(0x700), - "create": to_address(0x400), - "create2": to_address(0x500), + "blobhash_sstore": Address(0x100), + "blobhash_return": Address(0x600), + "call": Address(0x200), + "delegatecall": Address(0x300), + "callcode": Address(0x800), + "staticcall": Address(0x700), + "create": Address(0x400), + "create2": Address(0x500), } @staticmethod diff --git a/tests/cancun/eip4844_blobs/conftest.py b/tests/cancun/eip4844_blobs/conftest.py index f7bc99da63..64aa7c7de0 100644 --- a/tests/cancun/eip4844_blobs/conftest.py +++ b/tests/cancun/eip4844_blobs/conftest.py @@ -3,14 +3,7 @@ """ import pytest -from ethereum_test_tools import ( - Block, - TestPrivateKey2, - Transaction, - add_kzg_version, - to_address, - to_hash_bytes, -) +from ethereum_test_tools import Address, Block, Hash, TestPrivateKey2, Transaction, add_kzg_version from .spec import BlockHeaderBlobGasFields, Spec @@ -48,7 +41,7 @@ def non_zero_blob_gas_used_genesis_block( Transaction( ty=Spec.BLOB_TX_TYPE, nonce=0, - to=to_address(0x200), + to=Address(0x200), value=1, gas_limit=21000, max_fee_per_gas=tx_max_fee_per_gas, @@ -56,7 +49,7 @@ def non_zero_blob_gas_used_genesis_block( max_fee_per_blob_gas=Spec.get_blob_gasprice(excess_blob_gas=excess_blob_gas), access_list=[], blob_versioned_hashes=add_kzg_version( - [to_hash_bytes(x) for x in range(parent_blobs)], + [Hash(x) for x in range(parent_blobs)], Spec.BLOB_COMMITMENT_VERSION_KZG, ), secret_key=TestPrivateKey2, diff --git a/tests/cancun/eip4844_blobs/test_blob_txs.py b/tests/cancun/eip4844_blobs/test_blob_txs.py index 32cadd6d45..f710ad52ad 100644 --- a/tests/cancun/eip4844_blobs/test_blob_txs.py +++ b/tests/cancun/eip4844_blobs/test_blob_txs.py @@ -24,11 +24,13 @@ from ethereum_test_tools import ( AccessList, Account, + Address, Block, BlockchainTestFiller, BlockException, EngineAPIError, Environment, + Hash, Header, ) from ethereum_test_tools import Opcodes as Op @@ -42,8 +44,6 @@ TransactionException, add_kzg_version, eip_2028_transaction_data_cost, - to_address, - to_hash_bytes, ) from .spec import Spec, SpecHelpers, ref_spec_4844 @@ -56,9 +56,9 @@ @pytest.fixture -def destination_account() -> str: +def destination_account() -> Address: """Default destination account for the blob transactions.""" - return to_address(0x100) + return Address(0x100) @pytest.fixture @@ -188,7 +188,7 @@ def blob_hashes_per_tx(blobs_per_tx: List[int]) -> List[List[bytes]]: """ return [ add_kzg_version( - [to_hash_bytes(x) for x in range(blob_count)], + [Hash(x) for x in range(blob_count)], Spec.BLOB_COMMITMENT_VERSION_KZG, ) for blob_count in blobs_per_tx @@ -293,7 +293,7 @@ def tx_error() -> Optional[TransactionException]: @pytest.fixture(autouse=True) def txs( # noqa: D103 - destination_account: Optional[str], + destination_account: Optional[Address], tx_gas: int, tx_value: int, tx_calldata: bytes, @@ -928,7 +928,7 @@ def test_blob_gas_subtraction_tx( state_env: Environment, pre: Dict, txs: List[Transaction], - destination_account: str, + destination_account: Address, mid_tx_send_amount: int, total_account_transactions_fee: int, ): @@ -1032,16 +1032,10 @@ def test_invalid_tx_blob_count( @pytest.mark.parametrize( "blob_hashes_per_tx", [ - [[to_hash_bytes(1)]], - [[to_hash_bytes(x) for x in range(2)]], - [ - add_kzg_version([to_hash_bytes(1)], Spec.BLOB_COMMITMENT_VERSION_KZG) - + [to_hash_bytes(2)] - ], - [ - [to_hash_bytes(1)] - + add_kzg_version([to_hash_bytes(2)], Spec.BLOB_COMMITMENT_VERSION_KZG) - ], + [[Hash(1)]], + [[Hash(x) for x in range(2)]], + [add_kzg_version([Hash(1)], Spec.BLOB_COMMITMENT_VERSION_KZG) + [Hash(2)]], + [[Hash(1)] + add_kzg_version([Hash(2)], Spec.BLOB_COMMITMENT_VERSION_KZG)], ], ids=[ "single_blob", @@ -1084,22 +1078,21 @@ def test_invalid_blob_hash_versioning_single_tx( "blob_hashes_per_tx", [ [ - add_kzg_version([to_hash_bytes(1)], Spec.BLOB_COMMITMENT_VERSION_KZG), - [to_hash_bytes(2)], + add_kzg_version([Hash(1)], Spec.BLOB_COMMITMENT_VERSION_KZG), + [Hash(2)], ], [ - add_kzg_version([to_hash_bytes(1)], Spec.BLOB_COMMITMENT_VERSION_KZG), - [to_hash_bytes(x) for x in range(1, 3)], + add_kzg_version([Hash(1)], Spec.BLOB_COMMITMENT_VERSION_KZG), + [Hash(x) for x in range(1, 3)], ], [ - add_kzg_version([to_hash_bytes(1)], Spec.BLOB_COMMITMENT_VERSION_KZG), - [to_hash_bytes(2)] - + add_kzg_version([to_hash_bytes(3)], Spec.BLOB_COMMITMENT_VERSION_KZG), + add_kzg_version([Hash(1)], Spec.BLOB_COMMITMENT_VERSION_KZG), + [Hash(2)] + add_kzg_version([Hash(3)], Spec.BLOB_COMMITMENT_VERSION_KZG), ], [ - add_kzg_version([to_hash_bytes(1)], Spec.BLOB_COMMITMENT_VERSION_KZG), - add_kzg_version([to_hash_bytes(2)], Spec.BLOB_COMMITMENT_VERSION_KZG), - [to_hash_bytes(3)], + add_kzg_version([Hash(1)], Spec.BLOB_COMMITMENT_VERSION_KZG), + add_kzg_version([Hash(2)], Spec.BLOB_COMMITMENT_VERSION_KZG), + [Hash(3)], ], ], ids=[ @@ -1240,7 +1233,7 @@ def test_blob_tx_attribute_opcodes( opcode: Tuple[bytes, Storage.StorageDictType], state_env: Environment, txs: List[Transaction], - destination_account: str, + destination_account: Address, ): """ Test opcodes that read transaction attributes work properly for blob type transactions: @@ -1275,7 +1268,7 @@ def test_blob_tx_attribute_value_opcode( state_env: Environment, txs: List[Transaction], tx_value: int, - destination_account: str, + destination_account: Address, ): """ Test the VALUE opcode with different blob type transaction value amounts. @@ -1323,7 +1316,7 @@ def test_blob_tx_attribute_calldata_opcodes( opcode: Tuple[bytes, Storage.StorageDictType], state_env: Environment, txs: List[Transaction], - destination_account: str, + destination_account: Address, ): """ Test calldata related opcodes to verify their behavior is not affected by blobs: @@ -1360,7 +1353,7 @@ def test_blob_tx_attribute_gasprice_opcode( opcode: Tuple[bytes, Storage.StorageDictType], state_env: Environment, txs: List[Transaction], - destination_account: str, + destination_account: Address, ): """ Test GASPRICE opcode to sanity check that the blob gas fee does not affect diff --git a/tests/cancun/eip4844_blobs/test_blob_txs_full.py b/tests/cancun/eip4844_blobs/test_blob_txs_full.py index a51683ba88..66ab210174 100644 --- a/tests/cancun/eip4844_blobs/test_blob_txs_full.py +++ b/tests/cancun/eip4844_blobs/test_blob_txs_full.py @@ -9,6 +9,7 @@ from ethereum_test_tools import ( Account, + Address, Block, BlockchainTestFiller, Environment, @@ -16,7 +17,6 @@ TestAddress, Transaction, TransactionException, - to_address, ) from .common import INF_POINT, Blob @@ -27,9 +27,9 @@ @pytest.fixture -def destination_account() -> str: +def destination_account() -> Address: """Default destination account for the blob transactions.""" - return to_address(0x100) + return Address(0x100) @pytest.fixture @@ -172,7 +172,7 @@ def tx_error() -> Optional[TransactionException]: @pytest.fixture(autouse=True) def txs( # noqa: D103 - destination_account: Optional[str], + destination_account: Optional[Address], tx_gas: int, tx_value: int, tx_calldata: bytes, diff --git a/tests/cancun/eip4844_blobs/test_blobhash_opcode.py b/tests/cancun/eip4844_blobs/test_blobhash_opcode.py index 2dba6a9a40..ba2dde3b47 100644 --- a/tests/cancun/eip4844_blobs/test_blobhash_opcode.py +++ b/tests/cancun/eip4844_blobs/test_blobhash_opcode.py @@ -21,13 +21,13 @@ from ethereum_test_tools import ( Account, + Address, Block, BlockchainTestFiller, CodeGasMeasure, + Hash, TestAddress, Transaction, - to_address, - to_hash_bytes, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -60,7 +60,7 @@ def blocks(): # noqa: D103 @pytest.fixture def template_tx(): # noqa: D103 return Transaction( - data=to_hash_bytes(0), + data=Hash(0), gas_limit=3000000, max_fee_per_gas=10, ) @@ -118,7 +118,7 @@ def test_blobhash_gas_cost( for i in blobhash_index_values ] for i, gas_code in enumerate(gas_measures_code): - address = to_address(0x100 + i * 0x100) + address = Address(0x100 + i * 0x100) pre[address] = Account(code=gas_code) blocks.append( Block( @@ -177,7 +177,7 @@ def test_blobhash_scenarios( b_hashes_list = BlobhashScenario.create_blob_hashes_list(length=TOTAL_BLOCKS) blobhash_calls = BlobhashScenario.generate_blobhash_bytecode(scenario) for i in range(TOTAL_BLOCKS): - address = to_address(0x100 + i * 0x100) + address = Address(0x100 + i * 0x100) pre[address] = Account(code=blobhash_calls) blocks.append( Block( @@ -234,7 +234,7 @@ def test_blobhash_invalid_blob_index( TOTAL_BLOCKS = 5 blobhash_calls = BlobhashScenario.generate_blobhash_bytecode(scenario) for i in range(TOTAL_BLOCKS): - address = to_address(0x100 + i * 0x100) + address = Address(0x100 + i * 0x100) pre[address] = Account(code=blobhash_calls) blob_per_block = (i % SpecHelpers.max_blobs_per_block()) + 1 blobs = [random_blob_hashes[blob] for blob in range(blob_per_block)] @@ -286,32 +286,32 @@ def test_blobhash_multiple_txs_in_block( pre = { **pre, **{ - to_address(address): Account(code=blobhash_bytecode) + Address(address): Account(code=blobhash_bytecode) for address in range(0x100, 0x500, 0x100) }, } blocks = [ Block( txs=[ - blob_tx(address=to_address(0x100), type=3, nonce=0), - blob_tx(address=to_address(0x100), type=2, nonce=1), + blob_tx(address=Address(0x100), type=3, nonce=0), + blob_tx(address=Address(0x100), type=2, nonce=1), ] ), Block( txs=[ - blob_tx(address=to_address(0x200), type=2, nonce=2), - blob_tx(address=to_address(0x200), type=3, nonce=3), + blob_tx(address=Address(0x200), type=2, nonce=2), + blob_tx(address=Address(0x200), type=3, nonce=3), ] ), Block( txs=[ - blob_tx(address=to_address(0x300), type=2, nonce=4), - blob_tx(address=to_address(0x400), type=3, nonce=5), + blob_tx(address=Address(0x300), type=2, nonce=4), + blob_tx(address=Address(0x400), type=3, nonce=5), ], ), ] post = { - to_address(address): Account( + Address(address): Account( storage={i: random_blob_hashes[i] for i in range(SpecHelpers.max_blobs_per_block())} ) if address in (0x200, 0x400) diff --git a/tests/cancun/eip4844_blobs/test_blobhash_opcode_contexts.py b/tests/cancun/eip4844_blobs/test_blobhash_opcode_contexts.py index df2575fe42..749c9a8030 100644 --- a/tests/cancun/eip4844_blobs/test_blobhash_opcode_contexts.py +++ b/tests/cancun/eip4844_blobs/test_blobhash_opcode_contexts.py @@ -11,10 +11,10 @@ Account, Block, BlockchainTestFiller, + Hash, TestAddress, Transaction, YulCompiler, - to_hash_bytes, ) from .common import BlobhashContext, simple_blob_hashes @@ -29,7 +29,7 @@ # Blob transaction template tx_type_3 = Transaction( ty=Spec.BLOB_TX_TYPE, - data=to_hash_bytes(0), + data=Hash(0), gas_limit=3000000, max_fee_per_gas=10, max_priority_fee_per_gas=10, @@ -100,7 +100,7 @@ def opcode_context(yul: YulCompiler, request): ), }, tx_type_3.with_fields( - data=to_hash_bytes(2**256 - 1) + to_hash_bytes(2**256 - 1), + data=Hash(2**256 - 1) + Hash(2**256 - 1), to=BlobhashContext.address("blobhash_sstore"), ), { @@ -116,7 +116,7 @@ def opcode_context(yul: YulCompiler, request): ), }, tx_type_3.with_fields( - data=to_hash_bytes(1) + to_hash_bytes(1), + data=Hash(1) + Hash(1), to=BlobhashContext.address("call"), blob_versioned_hashes=simple_blob_hashes[:2], ), @@ -137,7 +137,7 @@ def opcode_context(yul: YulCompiler, request): ), }, tx_type_3.with_fields( - data=to_hash_bytes(0) + to_hash_bytes(SpecHelpers.max_blobs_per_block() - 1), + data=Hash(0) + Hash(SpecHelpers.max_blobs_per_block() - 1), to=BlobhashContext.address("delegatecall"), ), { @@ -159,7 +159,7 @@ def opcode_context(yul: YulCompiler, request): ), }, tx_type_3.with_fields( - data=to_hash_bytes(0) + to_hash_bytes(SpecHelpers.max_blobs_per_block() - 1), + data=Hash(0) + Hash(SpecHelpers.max_blobs_per_block() - 1), to=BlobhashContext.address("staticcall"), ), { @@ -181,7 +181,7 @@ def opcode_context(yul: YulCompiler, request): ), }, tx_type_3.with_fields( - data=to_hash_bytes(0) + to_hash_bytes(SpecHelpers.max_blobs_per_block() - 1), + data=Hash(0) + Hash(SpecHelpers.max_blobs_per_block() - 1), to=BlobhashContext.address("callcode"), ), { @@ -235,7 +235,7 @@ def opcode_context(yul: YulCompiler, request): }, Transaction( ty=2, - data=to_hash_bytes(0), + data=Hash(0), to=BlobhashContext.address("blobhash_sstore"), gas_limit=3000000, max_fee_per_gas=10, @@ -255,7 +255,7 @@ def opcode_context(yul: YulCompiler, request): }, Transaction( ty=1, - data=to_hash_bytes(0), + data=Hash(0), to=BlobhashContext.address("blobhash_sstore"), gas_limit=3000000, gas_price=10, @@ -274,7 +274,7 @@ def opcode_context(yul: YulCompiler, request): }, Transaction( ty=0, - data=to_hash_bytes(0), + data=Hash(0), to=BlobhashContext.address("blobhash_sstore"), gas_limit=3000000, gas_price=10, diff --git a/tests/cancun/eip4844_blobs/test_excess_blob_gas.py b/tests/cancun/eip4844_blobs/test_excess_blob_gas.py index de4b069ed2..f841e79026 100644 --- a/tests/cancun/eip4844_blobs/test_excess_blob_gas.py +++ b/tests/cancun/eip4844_blobs/test_excess_blob_gas.py @@ -27,22 +27,17 @@ from ethereum_test_tools import ( Account, + Address, Block, BlockchainTestFiller, BlockException, Environment, ExceptionType, + Hash, Header, ) from ethereum_test_tools import Opcodes as Op -from ethereum_test_tools import ( - TestAddress, - TestAddress2, - Transaction, - add_kzg_version, - to_address, - to_hash_bytes, -) +from ethereum_test_tools import TestAddress, TestAddress2, Transaction, add_kzg_version from .spec import Spec, SpecHelpers, ref_spec_4844 @@ -177,14 +172,14 @@ def destination_account_bytecode() -> bytes: # noqa: D103 @pytest.fixture -def destination_account() -> str: # noqa: D103 - return to_address(0x100) +def destination_account() -> Address: # noqa: D103 + return Address(0x100) @pytest.fixture def pre( # noqa: D103 - destination_account: str, destination_account_bytecode: bytes, tx_exact_cost: int -) -> Mapping[str, Account]: + destination_account: Address, destination_account_bytecode: bytes, tx_exact_cost: int +) -> Mapping[Address, Account]: return { TestAddress: Account(balance=tx_exact_cost), TestAddress2: Account(balance=10**40), @@ -194,8 +189,8 @@ def pre( # noqa: D103 @pytest.fixture def post( # noqa: D103 - destination_account: str, tx_value: int, block_fee_per_blob_gas: int -) -> Mapping[str, Account]: + destination_account: Address, tx_value: int, block_fee_per_blob_gas: int +) -> Mapping[Address, Account]: return { destination_account: Account( storage={0: block_fee_per_blob_gas}, @@ -210,7 +205,7 @@ def tx( # noqa: D103 tx_max_fee_per_gas: int, tx_max_fee_per_blob_gas: int, tx_gas_limit: int, - destination_account: str, + destination_account: Address, ): if new_blobs == 0: # Send a normal type two tx instead @@ -236,7 +231,7 @@ def tx( # noqa: D103 max_fee_per_blob_gas=tx_max_fee_per_blob_gas, access_list=[], blob_versioned_hashes=add_kzg_version( - [to_hash_bytes(x) for x in range(new_blobs)], + [Hash(x) for x in range(new_blobs)], Spec.BLOB_COMMITMENT_VERSION_KZG, ), ) @@ -316,9 +311,9 @@ def add_block( def test_correct_excess_blob_gas_calculation( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], blocks: List[Block], - post: Mapping[str, Account], + post: Mapping[Address, Account], correct_excess_blob_gas: int, ): """ @@ -361,9 +356,9 @@ def test_correct_excess_blob_gas_calculation( def test_correct_increasing_blob_gas_costs( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], blocks: List[Block], - post: Mapping[str, Account], + post: Mapping[Address, Account], correct_excess_blob_gas: int, ): """ @@ -395,9 +390,9 @@ def test_correct_increasing_blob_gas_costs( def test_correct_decreasing_blob_gas_costs( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], blocks: List[Block], - post: Mapping[str, Account], + post: Mapping[Address, Account], correct_excess_blob_gas: int, ): """ @@ -421,7 +416,7 @@ def test_correct_decreasing_blob_gas_costs( def test_invalid_zero_excess_blob_gas_in_header( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], blocks: List[Block], correct_excess_blob_gas: int, header_excess_blob_gas: Optional[int], @@ -470,7 +465,7 @@ def all_invalid_blob_gas_used_combinations() -> Iterator[Tuple[int, int]]: def test_invalid_blob_gas_used_in_header( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], blocks: List[Block], new_blobs: int, header_blob_gas_used: Optional[int], @@ -509,7 +504,7 @@ def test_invalid_blob_gas_used_in_header( def test_invalid_excess_blob_gas_above_target_change( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], blocks: List[Block], correct_excess_blob_gas: int, header_excess_blob_gas: Optional[int], @@ -553,7 +548,7 @@ def test_invalid_excess_blob_gas_above_target_change( def test_invalid_static_excess_blob_gas( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], blocks: List[Block], correct_excess_blob_gas: int, parent_excess_blob_gas: int, @@ -588,7 +583,7 @@ def test_invalid_static_excess_blob_gas( def test_invalid_excess_blob_gas_target_blobs_increase_from_zero( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], blocks: List[Block], correct_excess_blob_gas: int, header_excess_blob_gas: Optional[int], @@ -629,7 +624,7 @@ def test_invalid_excess_blob_gas_target_blobs_increase_from_zero( def test_invalid_static_excess_blob_gas_from_zero_on_blobs_above_target( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], blocks: List[Block], correct_excess_blob_gas: int, header_excess_blob_gas: Optional[int], @@ -679,7 +674,7 @@ def test_invalid_static_excess_blob_gas_from_zero_on_blobs_above_target( def test_invalid_excess_blob_gas_change( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], blocks: List[Block], correct_excess_blob_gas: int, header_excess_blob_gas: Optional[int], @@ -722,7 +717,7 @@ def test_invalid_excess_blob_gas_change( def test_invalid_negative_excess_blob_gas( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], blocks: List[Block], correct_excess_blob_gas: int, header_excess_blob_gas: Optional[int], @@ -768,7 +763,7 @@ def test_invalid_negative_excess_blob_gas( def test_invalid_non_multiple_excess_blob_gas( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], blocks: List[Block], correct_excess_blob_gas: int, header_excess_blob_gas: Optional[int], diff --git a/tests/cancun/eip4844_blobs/test_excess_blob_gas_fork_transition.py b/tests/cancun/eip4844_blobs/test_excess_blob_gas_fork_transition.py index 98ea4ec82b..136078342a 100644 --- a/tests/cancun/eip4844_blobs/test_excess_blob_gas_fork_transition.py +++ b/tests/cancun/eip4844_blobs/test_excess_blob_gas_fork_transition.py @@ -9,17 +9,17 @@ from ethereum_test_tools import ( Account, + Address, Block, BlockchainTestFiller, BlockException, EngineAPIError, Environment, + Hash, Header, TestAddress, Transaction, add_kzg_version, - to_address, - to_hash_bytes, ) from .spec import Spec, SpecHelpers, ref_spec_4844 @@ -41,7 +41,7 @@ def env() -> Environment: # noqa: D103 @pytest.fixture -def pre() -> Mapping[str, Account]: # noqa: D103 +def pre() -> Mapping[Address, Account]: # noqa: D103 return { TestAddress: Account(balance=10**40), } @@ -74,13 +74,13 @@ def blob_count_per_block() -> int: @pytest.fixture -def destination_account() -> str: # noqa: D103 - return to_address(0x100) +def destination_account() -> Address: # noqa: D103 + return Address(0x100) @pytest.fixture def post_fork_blocks( - destination_account: str, + destination_account: Address, post_fork_block_count: int, blob_count_per_block: int, ): @@ -101,7 +101,7 @@ def post_fork_blocks( max_fee_per_blob_gas=100, access_list=[], blob_versioned_hashes=add_kzg_version( - [to_hash_bytes(x) for x in range(blob_count_per_block)], + [Hash(x) for x in range(blob_count_per_block)], Spec.BLOB_COMMITMENT_VERSION_KZG, ), ) @@ -125,8 +125,8 @@ def post_fork_blocks( @pytest.fixture def post( # noqa: D103 post_fork_block_count: int, - destination_account: str, -) -> Mapping[str, Account]: + destination_account: Address, +) -> Mapping[Address, Account]: return { destination_account: Account(balance=post_fork_block_count), } @@ -143,7 +143,7 @@ def post( # noqa: D103 def test_invalid_pre_fork_block_with_blob_fields( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], pre_fork_blocks: List[Block], excess_blob_gas_present: bool, blob_gas_used_present: bool, @@ -188,7 +188,7 @@ def test_invalid_pre_fork_block_with_blob_fields( def test_invalid_post_fork_block_without_blob_fields( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], pre_fork_blocks: List[Block], excess_blob_gas_missing: bool, blob_gas_used_missing: bool, @@ -239,10 +239,10 @@ def test_invalid_post_fork_block_without_blob_fields( def test_fork_transition_excess_blob_gas( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Mapping[str, Account], + pre: Mapping[Address, Account], pre_fork_blocks: List[Block], post_fork_blocks: List[Block], - post: Mapping[str, Account], + post: Mapping[Address, Account], ): """ Test `excessBlobGas` calculation in the header when the fork is activated. diff --git a/tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py b/tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py index 6bda221f58..c2443f71d3 100644 --- a/tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py +++ b/tests/cancun/eip4844_blobs/test_point_evaluation_precompile.py @@ -35,6 +35,7 @@ from ethereum_test_tools import ( Account, + Address, Block, BlockchainTestFiller, Environment, @@ -43,7 +44,6 @@ TestAddress, Transaction, eip_2028_transaction_data_cost, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -153,17 +153,17 @@ def precompile_caller_account(call_type: Op, call_gas: int) -> Account: @pytest.fixture -def precompile_caller_address() -> str: +def precompile_caller_address() -> Address: """ Address of the precompile caller account. """ - return to_address(0x100) + return Address(0x100) @pytest.fixture def pre( precompile_caller_account: Account, - precompile_caller_address: str, + precompile_caller_address: Address, ) -> Dict: """ Prepares the pre state of all test cases, by setting the balance of the @@ -180,7 +180,7 @@ def pre( @pytest.fixture def tx( - precompile_caller_address: str, + precompile_caller_address: Address, precompile_input: bytes, ) -> Transaction: """ @@ -201,7 +201,7 @@ def tx( @pytest.fixture def post( success: bool, - precompile_caller_address: str, + precompile_caller_address: Address, precompile_input: bytes, ) -> Dict: """ @@ -541,7 +541,7 @@ def test_point_evaluation_precompile_gas_tx_to( ty=2, nonce=0, data=precompile_input, - to=to_address(Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS), + to=Address(Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS), value=0, gas_limit=call_gas + intrinsic_gas_cost, max_fee_per_gas=7, @@ -589,7 +589,7 @@ def test_point_evaluation_precompile_before_fork( 0, ), ) - precompile_caller_address = to_address(0x100) + precompile_caller_address = Address(0x100) pre = { TestAddress: Account( @@ -608,7 +608,7 @@ def test_point_evaluation_precompile_before_fork( storage={1: 1}, # The call succeeds because precompile is not there yet ), - to_address(Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS): Account( + Address(Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS): Account( balance=1, ), } @@ -648,7 +648,7 @@ def test_point_evaluation_precompile_during_fork( 0, ), ) - precompile_caller_address = to_address(0x100) + precompile_caller_address = Address(0x100) pre = { TestAddress: Account( @@ -683,7 +683,7 @@ def tx_generator() -> Iterator[Transaction]: storage={b: 1 for b in range(1, len(PRE_FORK_BLOCK_RANGE) + 1)}, # Only the call in the last block's tx fails; storage 0 by default. ), - to_address(Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS): Account( + Address(Spec.POINT_EVALUATION_PRECOMPILE_ADDRESS): Account( balance=len(PRE_FORK_BLOCK_RANGE), ), } diff --git a/tests/cancun/eip4844_blobs/test_point_evaluation_precompile_gas.py b/tests/cancun/eip4844_blobs/test_point_evaluation_precompile_gas.py index 6c98c32966..8d8ccf5475 100644 --- a/tests/cancun/eip4844_blobs/test_point_evaluation_precompile_gas.py +++ b/tests/cancun/eip4844_blobs/test_point_evaluation_precompile_gas.py @@ -9,13 +9,13 @@ from ethereum_test_tools import ( Account, + Address, CodeGasMeasure, Environment, StateTestFiller, TestAddress, Transaction, copy_opcode_cost, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -124,17 +124,17 @@ def precompile_caller_account( @pytest.fixture -def precompile_caller_address() -> str: +def precompile_caller_address() -> Address: """ Address of the precompile caller account. """ - return to_address(0x100) + return Address(0x100) @pytest.fixture def pre( precompile_caller_account: Account, - precompile_caller_address: str, + precompile_caller_address: Address, ) -> Dict: """ Prepares the pre state of all test cases, by setting the balance of the @@ -151,7 +151,7 @@ def pre( @pytest.fixture def tx( - precompile_caller_address: str, + precompile_caller_address: Address, precompile_input: bytes, ) -> Transaction: """ @@ -171,7 +171,7 @@ def tx( @pytest.fixture def post( - precompile_caller_address: str, + precompile_caller_address: Address, proof: Literal["correct", "incorrect"], call_gas: int, ) -> Dict: diff --git a/tests/cancun/eip5656_mcopy/test_mcopy.py b/tests/cancun/eip5656_mcopy/test_mcopy.py index 486dcd67bb..1a88ce438b 100644 --- a/tests/cancun/eip5656_mcopy/test_mcopy.py +++ b/tests/cancun/eip5656_mcopy/test_mcopy.py @@ -8,7 +8,7 @@ import pytest from ethereum.crypto.hash import keccak256 -from ethereum_test_tools import Account, Environment +from ethereum_test_tools import Account, Environment, Hash from ethereum_test_tools import Opcodes as Op from ethereum_test_tools import ( StateTestFiller, @@ -16,7 +16,6 @@ TestAddress, Transaction, ceiling_division, - to_hash_bytes, ) from .common import REFERENCE_SPEC_GIT_PATH, REFERENCE_SPEC_VERSION, mcopy @@ -116,7 +115,7 @@ def pre(bytecode_storage: Tuple[bytes, Storage]) -> Mapping: # noqa: D103 def tx(dest: int, src: int, length: int) -> Transaction: # noqa: D103 return Transaction( to=code_address, - data=to_hash_bytes(dest) + to_hash_bytes(src) + to_hash_bytes(length), + data=Hash(dest) + Hash(src) + Hash(length), gas_limit=1_000_000, ) diff --git a/tests/cancun/eip6780_selfdestruct/test_dynamic_create2_selfdestruct_collision.py b/tests/cancun/eip6780_selfdestruct/test_dynamic_create2_selfdestruct_collision.py index 214f46e284..606899a5eb 100644 --- a/tests/cancun/eip6780_selfdestruct/test_dynamic_create2_selfdestruct_collision.py +++ b/tests/cancun/eip6780_selfdestruct/test_dynamic_create2_selfdestruct_collision.py @@ -11,6 +11,7 @@ from ethereum_test_forks import Cancun, Fork from ethereum_test_tools import ( Account, + Address, Block, BlockchainTestFiller, Conditional, @@ -20,7 +21,6 @@ TestAddress, Transaction, compute_create2_address, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -86,19 +86,19 @@ def test_dynamic_create2_selfdestruct_collision( code_worked = 4 # Pre-Existing Addresses - address_zero = to_address(0x00) - address_to = to_address(0x0600) - address_code = to_address(0x0601) - address_create2_storage = to_address(0x0512) - sendall_destination = to_address(0x03E8) + address_zero = Address(0x00) + address_to = Address(0x0600) + address_code = Address(0x0601) + address_create2_storage = Address(0x0512) + sendall_destination = Address(0x03E8) # CREATE2 Initcode create2_salt = 1 - deploy_code = Op.SELFDESTRUCT(Op.PUSH20(sendall_destination)) + deploy_code = Op.SELFDESTRUCT(sendall_destination) initcode = Initcode( deploy_code=deploy_code, initcode_prefix=Op.SSTORE(create2_constructor_worked, 1) - + Op.CALL(Op.GAS(), Op.PUSH20(address_create2_storage), 0, 0, 0, 0, 0), + + Op.CALL(Op.GAS(), address_create2_storage, 0, 0, 0, 0, 0), ) # Created addresses @@ -120,26 +120,22 @@ def test_dynamic_create2_selfdestruct_collision( code=Op.JUMPDEST() # Make a subcall that do CREATE2 and returns its the result + Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE()) - + Op.CALL( - 100000, Op.PUSH20(address_code), first_create2_value, 0, Op.CALLDATASIZE(), 0, 32 - ) + + Op.CALL(100000, address_code, first_create2_value, 0, Op.CALLDATASIZE(), 0, 32) + Op.SSTORE( first_create2_result, Op.MLOAD(0), ) # Call to the created account to trigger selfdestruct - + Op.CALL(100000, Op.PUSH20(call_address_in_between), first_call_value, 0, 0, 0, 0) + + Op.CALL(100000, call_address_in_between, first_call_value, 0, 0, 0, 0) # Make a subcall that do CREATE2 collision and returns its address as the result + Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE()) - + Op.CALL( - 100000, Op.PUSH20(address_code), second_create2_value, 0, Op.CALLDATASIZE(), 0, 32 - ) + + Op.CALL(100000, address_code, second_create2_value, 0, Op.CALLDATASIZE(), 0, 32) + Op.SSTORE( second_create2_result, Op.MLOAD(0), ) # Call to the created account to trigger selfdestruct - + Op.CALL(100000, Op.PUSH20(call_address_in_the_end), second_call_value, 0, 0, 0, 0) + + Op.CALL(100000, call_address_in_the_end, second_call_value, 0, 0, 0, 0) + Op.SSTORE(code_worked, 1), storage={first_create2_result: 0xFF, second_create2_result: 0xFF}, ), @@ -177,7 +173,7 @@ def test_dynamic_create2_selfdestruct_collision( storage={}, ) - post: Dict[str, Union[Account, object]] = {} + post: Dict[Address, Union[Account, object]] = {} # Create2 address only exists if it was pre-existing and after cancun post[create2_address] = ( @@ -271,18 +267,18 @@ def test_dynamic_create2_selfdestruct_collision_multi_tx( part_2_worked = 5 # Pre-Existing Addresses - address_to = to_address(0x0600) - address_code = to_address(0x0601) - address_create2_storage = to_address(0x0512) - sendall_destination = to_address(0x03E8) + address_to = Address(0x0600) + address_code = Address(0x0601) + address_create2_storage = Address(0x0512) + sendall_destination = Address(0x03E8) # CREATE2 Initcode create2_salt = 1 - deploy_code = Op.SELFDESTRUCT(Op.PUSH20(sendall_destination)) + deploy_code = Op.SELFDESTRUCT(sendall_destination) initcode = Initcode( deploy_code=deploy_code, initcode_prefix=Op.SSTORE(create2_constructor_worked, 1) - + Op.CALL(Op.GAS(), Op.PUSH20(address_create2_storage), 0, 0, 0, 0, 0), + + Op.CALL(Op.GAS(), address_create2_storage, 0, 0, 0, 0, 0), ) # Created addresses @@ -302,9 +298,7 @@ def test_dynamic_create2_selfdestruct_collision_multi_tx( Op.JUMPDEST() # Make a subcall that do CREATE2 and returns its the result + Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE()) - + Op.CALL( - 100000, Op.PUSH20(address_code), first_create2_value, 0, Op.CALLDATASIZE(), 0, 32 - ) + + Op.CALL(100000, address_code, first_create2_value, 0, Op.CALLDATASIZE(), 0, 32) + Op.SSTORE( first_create2_result, Op.MLOAD(0), @@ -314,21 +308,19 @@ def test_dynamic_create2_selfdestruct_collision_multi_tx( if selfdestruct_on_first_tx: first_tx_code += ( # Call to the created account to trigger selfdestruct - Op.CALL(100000, Op.PUSH20(create2_address), first_call_value, 0, 0, 0, 0) + Op.CALL(100000, create2_address, first_call_value, 0, 0, 0, 0) ) else: second_tx_code += ( # Call to the created account to trigger selfdestruct - Op.CALL(100000, Op.PUSH20(create2_address), first_call_value, 0, 0, 0, 0) + Op.CALL(100000, create2_address, first_call_value, 0, 0, 0, 0) ) if recreate_on_first_tx: first_tx_code += ( # Make a subcall that do CREATE2 collision and returns its address as the result Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE()) - + Op.CALL( - 100000, Op.PUSH20(address_code), second_create2_value, 0, Op.CALLDATASIZE(), 0, 32 - ) + + Op.CALL(100000, address_code, second_create2_value, 0, Op.CALLDATASIZE(), 0, 32) + Op.SSTORE( second_create2_result, Op.MLOAD(0), @@ -339,9 +331,7 @@ def test_dynamic_create2_selfdestruct_collision_multi_tx( second_tx_code += ( # Make a subcall that do CREATE2 collision and returns its address as the result Op.CALLDATACOPY(0, 0, Op.CALLDATASIZE()) - + Op.CALL( - 100000, Op.PUSH20(address_code), second_create2_value, 0, Op.CALLDATASIZE(), 0, 32 - ) + + Op.CALL(100000, address_code, second_create2_value, 0, Op.CALLDATASIZE(), 0, 32) + Op.SSTORE( second_create2_result, Op.MLOAD(0), @@ -349,7 +339,7 @@ def test_dynamic_create2_selfdestruct_collision_multi_tx( ) # Second tx code always calls the create2 contract at the end - second_tx_code += Op.CALL(100000, Op.PUSH20(create2_address), second_call_value, 0, 0, 0, 0) + second_tx_code += Op.CALL(100000, create2_address, second_call_value, 0, 0, 0, 0) first_tx_code += Op.SSTORE(part_1_worked, 1) second_tx_code += Op.SSTORE(part_2_worked, 1) @@ -391,7 +381,7 @@ def test_dynamic_create2_selfdestruct_collision_multi_tx( ), } - post: Dict[str, Union[Account, object]] = {} + post: Dict[Address, Union[Account, object]] = {} # Create2 address only exists if it was pre-existing and after cancun account_will_exist_with_code = not selfdestruct_on_first_tx and fork >= Cancun diff --git a/tests/cancun/eip6780_selfdestruct/test_reentrancy_selfdestruct_revert.py b/tests/cancun/eip6780_selfdestruct/test_reentrancy_selfdestruct_revert.py index 8b6fe75b1d..d5661cfe90 100644 --- a/tests/cancun/eip6780_selfdestruct/test_reentrancy_selfdestruct_revert.py +++ b/tests/cancun/eip6780_selfdestruct/test_reentrancy_selfdestruct_revert.py @@ -8,12 +8,12 @@ from ethereum_test_forks import Cancun, Fork from ethereum_test_tools import ( Account, + Address, Environment, StateTestFiller, TestAddress, TestAddress2, Transaction, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -54,22 +54,22 @@ def test_reentrancy_selfdestruct_revert( It is expected the S is self destructed after the transaction. """ address_to = TestAddress2 - address_s = to_address(0x1000000000000000000000000000000000000001) - address_r = to_address(0x1000000000000000000000000000000000000002) - suicide_d = to_address(0x03E8) + address_s = Address(0x1000000000000000000000000000000000000001) + address_r = Address(0x1000000000000000000000000000000000000002) + suicide_d = Address(0x03E8) def construct_call_s(call_type: Op, money: int): if call_type in [Op.CALLCODE, Op.CALL]: - return call_type(Op.GAS, Op.PUSH20(address_s), money, 0, 0, 0, 0) + return call_type(Op.GAS, address_s, money, 0, 0, 0, 0) else: - return call_type(Op.GAS, Op.PUSH20(address_s), money, 0, 0, 0) + return call_type(Op.GAS, address_s, money, 0, 0, 0) pre = { address_to: Account( balance=1000000000000000000, nonce=0, code=Op.SSTORE(1, construct_call_s(first_suicide, 0)) - + Op.SSTORE(2, Op.CALL(Op.GAS, Op.PUSH20(address_r), 0, 0, 0, 0, 0)) + + Op.SSTORE(2, Op.CALL(Op.GAS, address_r, 0, 0, 0, 0, 0)) + Op.RETURNDATACOPY(0, 0, Op.RETURNDATASIZE()) + Op.SSTORE(3, Op.MLOAD(0)), storage={0x01: 0x0100, 0x02: 0x0100, 0x03: 0x0100}, diff --git a/tests/cancun/eip6780_selfdestruct/test_selfdestruct.py b/tests/cancun/eip6780_selfdestruct/test_selfdestruct.py index adc6c5bf57..c1c85bec47 100644 --- a/tests/cancun/eip6780_selfdestruct/test_selfdestruct.py +++ b/tests/cancun/eip6780_selfdestruct/test_selfdestruct.py @@ -13,9 +13,11 @@ from ethereum_test_forks import Cancun, Fork from ethereum_test_tools import ( Account, + Address, Block, BlockchainTestFiller, Environment, + Hash, Initcode, StateTestFiller, Storage, @@ -24,8 +26,6 @@ YulCompiler, compute_create2_address, compute_create_address, - to_address, - to_hash_bytes, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -34,16 +34,16 @@ SELFDESTRUCT_ENABLE_FORK = Cancun -PRE_EXISTING_SELFDESTRUCT_ADDRESS = "0x1111111111111111111111111111111111111111" +PRE_EXISTING_SELFDESTRUCT_ADDRESS = Address("0x1111111111111111111111111111111111111111") """ Address of a pre-existing contract that self-destructs. """ # Sentinel value to indicate that the self-destructing contract address should be used, only for # use in `pytest.mark.parametrize`, not for use within the test method itself. -SELF_ADDRESS = "0x1" +SELF_ADDRESS = Address(0x01) # Sentinel value to indicate that the contract should not self-destruct. -NO_SELFDESTRUCT = "0x0" +NO_SELFDESTRUCT = Address(0x00) @pytest.fixture @@ -61,14 +61,14 @@ def env() -> Environment: @pytest.fixture -def sendall_recipient_addresses() -> List[str]: +def sendall_recipient_addresses() -> List[Address]: """List of possible addresses that can receive a SENDALL operation.""" - return [to_address(0x1234)] + return [Address(0x1234)] def selfdestruct_code_preset( *, - sendall_recipient_addresses: List[str], + sendall_recipient_addresses: List[Address], yul: YulCompiler, ) -> SupportsBytes: """Return a bytecode that self-destructs.""" @@ -98,7 +98,9 @@ def selfdestruct_code_preset( assert sendall_recipient != NO_SELFDESTRUCT, "test error" if sendall_recipient == SELF_ADDRESS: # Use the self address of the contract we are creating - sendall_recipient = "address()" + # sendall_recipient = "address()" + # TODO: Fix this + pass return yul( f""" {{ @@ -112,7 +114,7 @@ def selfdestruct_code_preset( @pytest.fixture def selfdestruct_code( - sendall_recipient_addresses: List[str], + sendall_recipient_addresses: List[Address], yul: YulCompiler, ) -> SupportsBytes: """ @@ -146,13 +148,13 @@ def selfdestruct_contract_initcode( @pytest.fixture -def initcode_copy_from_address() -> str: +def initcode_copy_from_address() -> Address: """Address of a pre-existing contract we use to simply copy initcode from.""" - return to_address(0xABCD) + return Address(0xABCD) @pytest.fixture -def entry_code_address() -> str: +def entry_code_address() -> Address: """Address where the entry code will run.""" return compute_create_address(TestAddress, 0) @@ -160,9 +162,9 @@ def entry_code_address() -> str: @pytest.fixture def selfdestruct_contract_address( create_opcode: Op, - entry_code_address: str, + entry_code_address: Address, selfdestruct_contract_initcode: SupportsBytes, -) -> str: +) -> Address: """Returns the address of the self-destructing contract.""" if create_opcode == Op.CREATE: return compute_create_address(entry_code_address, 1) @@ -175,13 +177,13 @@ def selfdestruct_contract_address( @pytest.fixture def pre( - initcode_copy_from_address: str, + initcode_copy_from_address: Address, selfdestruct_contract_initcode: SupportsBytes, - selfdestruct_contract_address: str, + selfdestruct_contract_address: Address, selfdestruct_contract_initial_balance: int, - sendall_recipient_addresses: List[str], + sendall_recipient_addresses: List[Address], yul: YulCompiler, -) -> Dict[str, Account]: +) -> Dict[Address, Account]: """Pre-state of all tests""" pre = { TestAddress: Account(balance=100_000_000_000_000_000_000), @@ -227,7 +229,7 @@ def pre( [ pytest.param( 1, - [to_address(0x1000)], + [Address(0x1000)], id="single_call", ), pytest.param( @@ -237,27 +239,27 @@ def pre( ), pytest.param( 10, - [to_address(0x1000)], + [Address(0x1000)], id="multiple_calls_single_sendall_recipient", ), pytest.param( 10, - [to_address(0x1000), to_address(0x2000), to_address(0x3000)], + [Address(0x1000), Address(0x2000), Address(0x3000)], id="multiple_calls_multiple_sendall_recipients", ), pytest.param( 10, - [SELF_ADDRESS, to_address(0x2000), to_address(0x3000)], + [SELF_ADDRESS, Address(0x2000), Address(0x3000)], id="multiple_calls_multiple_sendall_recipients_including_self", ), pytest.param( 10, - [to_address(0x1000), to_address(0x2000), SELF_ADDRESS], + [Address(0x1000), Address(0x2000), SELF_ADDRESS], id="multiple_calls_multiple_sendall_recipients_including_self_different_order", ), pytest.param( 3, - [to_address(0x1000), to_address(0x2000), SELF_ADDRESS], + [Address(0x1000), Address(0x2000), SELF_ADDRESS], id="multiple_calls_multiple_sendall_recipients_including_self_last", ), ], @@ -267,13 +269,13 @@ def pre( def test_create_selfdestruct_same_tx( state_test: StateTestFiller, env: Environment, - pre: Dict[str, Account], - entry_code_address: str, + pre: Dict[Address, Account], + entry_code_address: Address, selfdestruct_code: SupportsBytes, selfdestruct_contract_initcode: SupportsBytes, - selfdestruct_contract_address: str, - sendall_recipient_addresses: List[str], - initcode_copy_from_address: str, + selfdestruct_contract_address: Address, + sendall_recipient_addresses: List[Address], + initcode_copy_from_address: Address, create_opcode: Op, call_times: int, selfdestruct_contract_initial_balance: int, @@ -313,7 +315,7 @@ def test_create_selfdestruct_same_tx( entry_code = ( # Initcode is already deployed at `initcode_copy_from_address`, so just copy it Op.EXTCODECOPY( - Op.PUSH20(initcode_copy_from_address), + initcode_copy_from_address, 0, 0, len(bytes(selfdestruct_contract_initcode)), @@ -328,23 +330,23 @@ def test_create_selfdestruct_same_tx( # Store the EXTCODE* properties of the created address entry_code += Op.SSTORE( entry_code_storage.store_next(len(bytes(selfdestruct_code))), - Op.EXTCODESIZE(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODESIZE(selfdestruct_contract_address), ) entry_code += Op.SSTORE( entry_code_storage.store_next(keccak256(bytes(selfdestruct_code))), - Op.EXTCODEHASH(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODEHASH(selfdestruct_contract_address), ) # Call the self-destructing contract multiple times as required, increasing the wei sent each # time for i, sendall_recipient in zip(range(call_times), cycle(sendall_recipient_addresses)): - entry_code += Op.MSTORE(0, Op.PUSH20(sendall_recipient)) + entry_code += Op.MSTORE(0, sendall_recipient) entry_code += Op.SSTORE( entry_code_storage.store_next(1), Op.CALL( Op.GASLIMIT, # Gas - Op.PUSH20(selfdestruct_contract_address), # Address + selfdestruct_contract_address, # Address i, # Value 0, 32, @@ -364,25 +366,25 @@ def test_create_selfdestruct_same_tx( entry_code += Op.SSTORE( entry_code_storage.store_next(0), - Op.BALANCE(Op.PUSH20(selfdestruct_contract_address)), + Op.BALANCE(selfdestruct_contract_address), ) # Check the EXTCODE* properties of the self-destructing contract again entry_code += Op.SSTORE( entry_code_storage.store_next(len(bytes(selfdestruct_code))), - Op.EXTCODESIZE(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODESIZE(selfdestruct_contract_address), ) entry_code += Op.SSTORE( entry_code_storage.store_next(keccak256(bytes(selfdestruct_code))), - Op.EXTCODEHASH(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODEHASH(selfdestruct_contract_address), ) # Lastly return zero so the entry point contract is created and we can retain the stored # values for verification. entry_code += Op.RETURN(max(len(bytes(selfdestruct_contract_initcode)), 32), 1) - post: Dict[str, Account] = { + post: Dict[Address, Account] = { entry_code_address: Account( code="0x00", storage=entry_code_storage, @@ -422,12 +424,12 @@ def test_create_selfdestruct_same_tx( def test_self_destructing_initcode( state_test: StateTestFiller, env: Environment, - pre: Dict[str, Account], - entry_code_address: str, + pre: Dict[Address, Account], + entry_code_address: Address, selfdestruct_contract_initcode: SupportsBytes, - selfdestruct_contract_address: str, - sendall_recipient_addresses: List[str], - initcode_copy_from_address: str, + selfdestruct_contract_address: Address, + sendall_recipient_addresses: List[Address], + initcode_copy_from_address: Address, create_opcode: Op, call_times: int, # Number of times to call the self-destructing contract in the same tx selfdestruct_contract_initial_balance: int, @@ -461,7 +463,7 @@ def test_self_destructing_initcode( entry_code = ( # Initcode is already deployed at `initcode_copy_from_address`, so just copy it Op.EXTCODECOPY( - Op.PUSH20(initcode_copy_from_address), + initcode_copy_from_address, 0, 0, len(bytes(selfdestruct_contract_initcode)), @@ -476,12 +478,12 @@ def test_self_destructing_initcode( # Store the EXTCODE* properties of the created address entry_code += Op.SSTORE( entry_code_storage.store_next(0), - Op.EXTCODESIZE(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODESIZE(selfdestruct_contract_address), ) entry_code += Op.SSTORE( entry_code_storage.store_next(keccak256(bytes())), - Op.EXTCODEHASH(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODEHASH(selfdestruct_contract_address), ) # Call the self-destructing contract multiple times as required, increasing the wei sent each @@ -491,7 +493,7 @@ def test_self_destructing_initcode( entry_code_storage.store_next(1), Op.CALL( Op.GASLIMIT, # Gas - Op.PUSH20(selfdestruct_contract_address), # Address + selfdestruct_contract_address, # Address i, # Value 0, 0, @@ -502,7 +504,7 @@ def test_self_destructing_initcode( entry_code += Op.SSTORE( entry_code_storage.store_next(0), - Op.BALANCE(Op.PUSH20(selfdestruct_contract_address)), + Op.BALANCE(selfdestruct_contract_address), ) # Lastly return zero so the entry point contract is created and we can retain the stored @@ -514,7 +516,7 @@ def test_self_destructing_initcode( # which must be included in the send-all operation sendall_amount += selfdestruct_contract_initial_balance - post: Dict[str, Account] = { + post: Dict[Address, Account] = { entry_code_address: Account( code="0x00", storage=entry_code_storage, @@ -550,13 +552,13 @@ def test_self_destructing_initcode( def test_self_destructing_initcode_create_tx( state_test: StateTestFiller, env: Environment, - pre: Dict[str, Account], + pre: Dict[Address, Account], tx_value: int, - entry_code_address: str, + entry_code_address: Address, selfdestruct_contract_initcode: SupportsBytes, - selfdestruct_contract_address: str, - sendall_recipient_addresses: List[str], - initcode_copy_from_address: str, + selfdestruct_contract_address: Address, + sendall_recipient_addresses: List[Address], + initcode_copy_from_address: Address, selfdestruct_contract_initial_balance: int, ): """ @@ -573,7 +575,7 @@ def test_self_destructing_initcode_create_tx( # Our entry point is an initcode that in turn creates a self-destructing contract sendall_amount = selfdestruct_contract_initial_balance + tx_value - post: Dict[str, Account] = { + post: Dict[Address, Account] = { selfdestruct_contract_address: Account.NONEXISTENT, # type: ignore initcode_copy_from_address: Account( code=selfdestruct_contract_initcode, @@ -602,7 +604,7 @@ def test_self_destructing_initcode_create_tx( "sendall_recipient_addresses", [ pytest.param( - [to_address(0x1000)], + [Address(0x1000)], id="selfdestruct_other_address", ), pytest.param( @@ -618,13 +620,13 @@ def test_self_destructing_initcode_create_tx( def test_recreate_self_destructed_contract_different_txs( blockchain_test: BlockchainTestFiller, env: Environment, - pre: Dict[str, Account], - entry_code_address: str, + pre: Dict[Address, Account], + entry_code_address: Address, selfdestruct_contract_initcode: SupportsBytes, - selfdestruct_contract_address: str, + selfdestruct_contract_address: Address, selfdestruct_contract_initial_balance: int, - sendall_recipient_addresses: List[str], - initcode_copy_from_address: str, + sendall_recipient_addresses: List[Address], + initcode_copy_from_address: Address, create_opcode: Op, recreate_times: int, # Number of times to recreate the contract in different transactions call_times: int, # Number of times to call the self-destructing contract in the same tx @@ -650,7 +652,7 @@ def test_recreate_self_destructed_contract_different_txs( entry_code = ( # Initcode is already deployed at initcode_copy_from_address, so just copy it Op.EXTCODECOPY( - Op.PUSH20(initcode_copy_from_address), + initcode_copy_from_address, 0, 0, len(bytes(selfdestruct_contract_initcode)), @@ -664,7 +666,7 @@ def test_recreate_self_destructed_contract_different_txs( for i in range(call_times): entry_code += Op.CALL( Op.GASLIMIT, - Op.PUSH20(selfdestruct_contract_address), + selfdestruct_contract_address, i, 0, 0, @@ -681,7 +683,7 @@ def test_recreate_self_destructed_contract_different_txs( txs.append( Transaction( ty=0x0, - data=to_hash_bytes(i), + data=Hash(i), chain_id=0x0, nonce=next(nonce), to=entry_code_address, @@ -693,7 +695,7 @@ def test_recreate_self_destructed_contract_different_txs( entry_code_storage[i] = selfdestruct_contract_address pre[entry_code_address] = Account(code=entry_code) - post: Dict[str, Account] = { + post: Dict[Address, Account] = { entry_code_address: Account( code=entry_code, storage=entry_code_storage, @@ -714,7 +716,7 @@ def test_recreate_self_destructed_contract_different_txs( [ pytest.param( 1, - [to_address(0x1000)], + [Address(0x1000)], id="single_call", ), pytest.param( @@ -724,27 +726,27 @@ def test_recreate_self_destructed_contract_different_txs( ), pytest.param( 10, - [to_address(0x1000)], + [Address(0x1000)], id="multiple_calls_single_sendall_recipient", ), pytest.param( 10, - [to_address(0x1000), to_address(0x2000), to_address(0x3000)], + [Address(0x1000), Address(0x2000), Address(0x3000)], id="multiple_calls_multiple_sendall_recipients", ), pytest.param( 10, - [PRE_EXISTING_SELFDESTRUCT_ADDRESS, to_address(0x2000), to_address(0x3000)], + [PRE_EXISTING_SELFDESTRUCT_ADDRESS, Address(0x2000), Address(0x3000)], id="multiple_calls_multiple_sendall_recipients_including_self", ), pytest.param( 10, - [to_address(0x1000), to_address(0x2000), PRE_EXISTING_SELFDESTRUCT_ADDRESS], + [Address(0x1000), Address(0x2000), PRE_EXISTING_SELFDESTRUCT_ADDRESS], id="multiple_calls_multiple_sendall_recipients_including_self_different_order", ), pytest.param( 3, - [to_address(0x1000), to_address(0x2000), PRE_EXISTING_SELFDESTRUCT_ADDRESS], + [Address(0x1000), Address(0x2000), PRE_EXISTING_SELFDESTRUCT_ADDRESS], id="multiple_calls_multiple_sendall_recipients_including_self_last", ), ], @@ -758,12 +760,12 @@ def test_selfdestruct_pre_existing( state_test: StateTestFiller, eip_enabled: bool, env: Environment, - pre: Dict[str, Account], - entry_code_address: str, - selfdestruct_contract_address: str, + pre: Dict[Address, Account], + entry_code_address: Address, + selfdestruct_contract_address: Address, selfdestruct_code: SupportsBytes, selfdestruct_contract_initial_balance: int, - sendall_recipient_addresses: List[str], + sendall_recipient_addresses: List[Address], call_times: int, ): """ @@ -792,12 +794,12 @@ def test_selfdestruct_pre_existing( # Call the self-destructing contract multiple times as required, increasing the wei sent each # time for i, sendall_recipient in zip(range(call_times), cycle(sendall_recipient_addresses)): - entry_code += Op.MSTORE(0, Op.PUSH20(sendall_recipient)) + entry_code += Op.MSTORE(0, sendall_recipient) entry_code += Op.SSTORE( entry_code_storage.store_next(1), Op.CALL( Op.GASLIMIT, # Gas - Op.PUSH20(selfdestruct_contract_address), # Address + selfdestruct_contract_address, # Address i, # Value 0, 32, @@ -818,25 +820,25 @@ def test_selfdestruct_pre_existing( entry_code += Op.SSTORE( entry_code_storage.store_next(selfdestruct_contract_current_balance), - Op.BALANCE(Op.PUSH20(selfdestruct_contract_address)), + Op.BALANCE(selfdestruct_contract_address), ) # Check the EXTCODE* properties of the self-destructing contract entry_code += Op.SSTORE( entry_code_storage.store_next(len(bytes(selfdestruct_code))), - Op.EXTCODESIZE(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODESIZE(selfdestruct_contract_address), ) entry_code += Op.SSTORE( entry_code_storage.store_next(keccak256(bytes(selfdestruct_code))), - Op.EXTCODEHASH(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODEHASH(selfdestruct_contract_address), ) # Lastly return zero so the entry point contract is created and we can retain the stored # values for verification. entry_code += Op.RETURN(32, 1) - post: Dict[str, Account] = { + post: Dict[Address, Account] = { entry_code_address: Account( code="0x00", storage=entry_code_storage, @@ -885,13 +887,13 @@ def test_selfdestruct_created_same_block_different_tx( blockchain_test: BlockchainTestFiller, eip_enabled: bool, env: Environment, - pre: Dict[str, Account], - entry_code_address: str, - selfdestruct_contract_address: str, + pre: Dict[Address, Account], + entry_code_address: Address, + selfdestruct_contract_address: Address, selfdestruct_code: SupportsBytes, selfdestruct_contract_initcode: SupportsBytes, selfdestruct_contract_initial_balance: int, - sendall_recipient_addresses: List[str], + sendall_recipient_addresses: List[Address], call_times: int, ): """ @@ -912,7 +914,7 @@ def test_selfdestruct_created_same_block_different_tx( entry_code_storage.store_next(1), Op.CALL( Op.GASLIMIT, # Gas - Op.PUSH20(selfdestruct_contract_address), # Address + selfdestruct_contract_address, # Address i, # Value 0, 0, @@ -925,25 +927,25 @@ def test_selfdestruct_created_same_block_different_tx( entry_code += Op.SSTORE( entry_code_storage.store_next(0), - Op.BALANCE(Op.PUSH20(selfdestruct_contract_address)), + Op.BALANCE(selfdestruct_contract_address), ) # Check the EXTCODE* properties of the self-destructing contract entry_code += Op.SSTORE( entry_code_storage.store_next(len(bytes(selfdestruct_code))), - Op.EXTCODESIZE(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODESIZE(selfdestruct_contract_address), ) entry_code += Op.SSTORE( entry_code_storage.store_next(keccak256(bytes(selfdestruct_code))), - Op.EXTCODEHASH(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODEHASH(selfdestruct_contract_address), ) # Lastly return zero so the entry point contract is created and we can retain the stored # values for verification. entry_code += Op.RETURN(32, 1) - post: Dict[str, Account] = { + post: Dict[Address, Account] = { entry_code_address: Account( code="0x00", storage=entry_code_storage, @@ -993,7 +995,7 @@ def test_selfdestruct_created_same_block_different_tx( pytest.param( Op.DELEGATECALL( Op.GAS, - Op.PUSH20(PRE_EXISTING_SELFDESTRUCT_ADDRESS), + PRE_EXISTING_SELFDESTRUCT_ADDRESS, 0, 0, 0, @@ -1004,7 +1006,7 @@ def test_selfdestruct_created_same_block_different_tx( pytest.param( Op.CALLCODE( Op.GAS, - Op.PUSH20(PRE_EXISTING_SELFDESTRUCT_ADDRESS), + PRE_EXISTING_SELFDESTRUCT_ADDRESS, 0, 0, 0, @@ -1022,13 +1024,13 @@ def test_selfdestruct_created_same_block_different_tx( def test_delegatecall_from_new_contract_to_pre_existing_contract( state_test: StateTestFiller, env: Environment, - pre: Dict[str, Account], - entry_code_address: str, + pre: Dict[Address, Account], + entry_code_address: Address, selfdestruct_code: SupportsBytes, selfdestruct_contract_initcode: SupportsBytes, - selfdestruct_contract_address: str, - sendall_recipient_addresses: List[str], - initcode_copy_from_address: str, + selfdestruct_contract_address: Address, + sendall_recipient_addresses: List[Address], + initcode_copy_from_address: Address, create_opcode: Op, call_times: int, selfdestruct_contract_initial_balance: int, @@ -1056,7 +1058,7 @@ def test_delegatecall_from_new_contract_to_pre_existing_contract( entry_code = ( # Initcode is already deployed at `initcode_copy_from_address`, so just copy it Op.EXTCODECOPY( - Op.PUSH20(initcode_copy_from_address), + initcode_copy_from_address, 0, 0, len(bytes(selfdestruct_contract_initcode)), @@ -1071,12 +1073,12 @@ def test_delegatecall_from_new_contract_to_pre_existing_contract( # Store the EXTCODE* properties of the created address entry_code += Op.SSTORE( entry_code_storage.store_next(len(bytes(selfdestruct_code))), - Op.EXTCODESIZE(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODESIZE(selfdestruct_contract_address), ) entry_code += Op.SSTORE( entry_code_storage.store_next(keccak256(bytes(selfdestruct_code))), - Op.EXTCODEHASH(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODEHASH(selfdestruct_contract_address), ) # Call the self-destructing contract multiple times as required, increasing the wei sent each @@ -1086,7 +1088,7 @@ def test_delegatecall_from_new_contract_to_pre_existing_contract( entry_code_storage.store_next(1), Op.CALL( Op.GASLIMIT, # Gas - Op.PUSH20(selfdestruct_contract_address), # Address + selfdestruct_contract_address, # Address i, # Value 0, 0, @@ -1099,18 +1101,18 @@ def test_delegatecall_from_new_contract_to_pre_existing_contract( entry_code += Op.SSTORE( entry_code_storage.store_next(0), - Op.BALANCE(Op.PUSH20(selfdestruct_contract_address)), + Op.BALANCE(selfdestruct_contract_address), ) # Check the EXTCODE* properties of the self-destructing contract again entry_code += Op.SSTORE( entry_code_storage.store_next(len(bytes(selfdestruct_code))), - Op.EXTCODESIZE(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODESIZE(selfdestruct_contract_address), ) entry_code += Op.SSTORE( entry_code_storage.store_next(keccak256(bytes(selfdestruct_code))), - Op.EXTCODEHASH(Op.PUSH20(selfdestruct_contract_address)), + Op.EXTCODEHASH(selfdestruct_contract_address), ) # Lastly return zero so the entry point contract is created and we can retain the stored @@ -1122,7 +1124,7 @@ def test_delegatecall_from_new_contract_to_pre_existing_contract( # which must be included in the send-all operation sendall_amount += selfdestruct_contract_initial_balance - post: Dict[str, Account] = { + post: Dict[Address, Account] = { entry_code_address: Account( code="0x00", storage=entry_code_storage, @@ -1159,13 +1161,13 @@ def test_delegatecall_from_pre_existing_contract_to_new_contract( state_test: StateTestFiller, eip_enabled: bool, env: Environment, - pre: Dict[str, Account], - entry_code_address: str, + pre: Dict[Address, Account], + entry_code_address: Address, selfdestruct_code: SupportsBytes, selfdestruct_contract_initcode: SupportsBytes, - selfdestruct_contract_address: str, - sendall_recipient_addresses: List[str], - initcode_copy_from_address: str, + selfdestruct_contract_address: Address, + sendall_recipient_addresses: List[Address], + initcode_copy_from_address: Address, call_opcode: Op, create_opcode: Op, call_times: int, @@ -1177,10 +1179,10 @@ def test_delegatecall_from_pre_existing_contract_to_new_contract( is not deleted. """ # Add the contract that delegate calls to the newly created contract - delegate_caller_address = "0x2222222222222222222222222222222222222222" + delegate_caller_address = Address("0x2222222222222222222222222222222222222222") call_args: List[int | bytes] = [ Op.GAS(), - Op.PUSH20(selfdestruct_contract_address), + selfdestruct_contract_address, 0, 0, 0, @@ -1211,7 +1213,7 @@ def test_delegatecall_from_pre_existing_contract_to_new_contract( entry_code = ( # Initcode is already deployed at `initcode_copy_from_address`, so just copy it Op.EXTCODECOPY( - Op.PUSH20(initcode_copy_from_address), + initcode_copy_from_address, 0, 0, len(bytes(selfdestruct_contract_initcode)), @@ -1226,12 +1228,12 @@ def test_delegatecall_from_pre_existing_contract_to_new_contract( # Store the EXTCODE* properties of the pre-existing address entry_code += Op.SSTORE( entry_code_storage.store_next(len(delegate_caller_code)), - Op.EXTCODESIZE(Op.PUSH20(delegate_caller_address)), + Op.EXTCODESIZE(delegate_caller_address), ) entry_code += Op.SSTORE( entry_code_storage.store_next(keccak256(delegate_caller_code)), - Op.EXTCODEHASH(Op.PUSH20(delegate_caller_address)), + Op.EXTCODEHASH(delegate_caller_address), ) # Now instead of calling the newly created contract directly, we delegate call to it @@ -1241,7 +1243,7 @@ def test_delegatecall_from_pre_existing_contract_to_new_contract( entry_code_storage.store_next(1), Op.CALL( Op.GASLIMIT, # Gas - Op.PUSH20(delegate_caller_address), # Address + delegate_caller_address, # Address i, # Value 0, 0, @@ -1254,25 +1256,25 @@ def test_delegatecall_from_pre_existing_contract_to_new_contract( entry_code += Op.SSTORE( entry_code_storage.store_next(0), - Op.BALANCE(Op.PUSH20(delegate_caller_address)), + Op.BALANCE(delegate_caller_address), ) # Check the EXTCODE* properties of the pre-existing address again entry_code += Op.SSTORE( entry_code_storage.store_next(len(bytes(delegate_caller_code))), - Op.EXTCODESIZE(Op.PUSH20(delegate_caller_address)), + Op.EXTCODESIZE(delegate_caller_address), ) entry_code += Op.SSTORE( entry_code_storage.store_next(keccak256(bytes(delegate_caller_code))), - Op.EXTCODEHASH(Op.PUSH20(delegate_caller_address)), + Op.EXTCODEHASH(delegate_caller_address), ) # Lastly return zero so the entry point contract is created and we can retain the stored # values for verification. entry_code += Op.RETURN(max(len(bytes(selfdestruct_contract_initcode)), 32), 1) - post: Dict[str, Account] = { + post: Dict[Address, Account] = { entry_code_address: Account( code="0x00", storage=entry_code_storage, diff --git a/tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py b/tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py index d0951cbe04..e7f05a7bfc 100644 --- a/tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py +++ b/tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py @@ -10,6 +10,7 @@ from ethereum_test_forks import Cancun from ethereum_test_tools import ( Account, + Address, Environment, Initcode, StateTestFiller, @@ -18,7 +19,6 @@ Transaction, YulCompiler, compute_create_address, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -29,15 +29,15 @@ @pytest.fixture -def entry_code_address() -> str: +def entry_code_address() -> Address: """Address where the entry code will run.""" return compute_create_address(TestAddress, 0) @pytest.fixture -def recursive_revert_contract_address(): +def recursive_revert_contract_address() -> Address: """Address where the recursive revert contract address exists""" - return to_address(0xDEADBEEF) + return Address(0xDEADBEEF) @pytest.fixture @@ -49,9 +49,9 @@ def env() -> Environment: @pytest.fixture -def selfdestruct_recipient_address() -> str: +def selfdestruct_recipient_address() -> Address: """List of possible addresses that can receive a SELFDESTRUCT operation.""" - return to_address(0x1234) + return Address(0x1234) @pytest.fixture @@ -65,7 +65,7 @@ def recursive_revert_contract_code( yul: YulCompiler, selfdestruct_on_outer_call: int, selfdestruct_with_transfer_contract_code: SupportsBytes, - selfdestruct_with_transfer_contract_address: str, + selfdestruct_with_transfer_contract_address: Address, ) -> SupportsBytes: """ Contract code that: @@ -129,7 +129,7 @@ def recursive_revert_contract_code( @pytest.fixture -def selfdestruct_with_transfer_contract_address(entry_code_address: str) -> str: +def selfdestruct_with_transfer_contract_address(entry_code_address: Address) -> Address: """Contract address for contract that can selfdestruct and receive value""" res = compute_create_address(entry_code_address, 1) return res @@ -137,7 +137,7 @@ def selfdestruct_with_transfer_contract_address(entry_code_address: str) -> str: @pytest.fixture def selfdestruct_with_transfer_contract_code( - yul: YulCompiler, selfdestruct_recipient_address: str + yul: YulCompiler, selfdestruct_recipient_address: Address ) -> SupportsBytes: """Contract that can selfdestruct and receive value""" return yul( @@ -175,20 +175,20 @@ def selfdestruct_with_transfer_contract_initcode( @pytest.fixture -def selfdestruct_with_transfer_initcode_copy_from_address() -> str: +def selfdestruct_with_transfer_initcode_copy_from_address() -> Address: """Address of a pre-existing contract we use to simply copy initcode from.""" - return to_address(0xABCD) + return Address(0xABCD) @pytest.fixture def pre( - recursive_revert_contract_address: str, + recursive_revert_contract_address: Address, recursive_revert_contract_code: SupportsBytes, - selfdestruct_with_transfer_initcode_copy_from_address: str, + selfdestruct_with_transfer_initcode_copy_from_address: Address, selfdestruct_with_transfer_contract_initcode: SupportsBytes, - selfdestruct_with_transfer_contract_address: str, + selfdestruct_with_transfer_contract_address: Address, yul: YulCompiler, -) -> Dict[str, Account]: +) -> Dict[Address, Account]: """Prestate for test_selfdestruct_not_created_in_same_tx_with_revert""" return { TestAddress: Account(balance=100_000_000_000_000_000_000), @@ -212,15 +212,15 @@ def pre( def test_selfdestruct_created_in_same_tx_with_revert( # noqa SC200 state_test: StateTestFiller, env: Environment, - pre: Dict[str, Account], - entry_code_address: str, + pre: Dict[Address, Account], + entry_code_address: Address, selfdestruct_on_outer_call: int, selfdestruct_with_transfer_contract_code: SupportsBytes, selfdestruct_with_transfer_contract_initcode: SupportsBytes, - selfdestruct_with_transfer_contract_address: str, - selfdestruct_recipient_address: str, - selfdestruct_with_transfer_initcode_copy_from_address: str, - recursive_revert_contract_address: str, + selfdestruct_with_transfer_contract_address: Address, + selfdestruct_recipient_address: Address, + selfdestruct_with_transfer_initcode_copy_from_address: Address, + recursive_revert_contract_address: Address, recursive_revert_contract_code: SupportsBytes, ): """ @@ -231,7 +231,7 @@ def test_selfdestruct_created_in_same_tx_with_revert( # noqa SC200 Recurse into a new call from transfers value to A, calls A.selfdestruct, and reverts. """ # noqa: E501 entry_code = Op.EXTCODECOPY( - Op.PUSH20(selfdestruct_with_transfer_initcode_copy_from_address), + selfdestruct_with_transfer_initcode_copy_from_address, 0, 0, len(bytes(selfdestruct_with_transfer_contract_initcode)), @@ -246,7 +246,7 @@ def test_selfdestruct_created_in_same_tx_with_revert( # noqa SC200 entry_code += Op.CALL( Op.GASLIMIT(), - Op.PUSH20(recursive_revert_contract_address), + recursive_revert_contract_address, 0, # value 0, # arg offset 0, # arg length @@ -254,7 +254,7 @@ def test_selfdestruct_created_in_same_tx_with_revert( # noqa SC200 0, # ret length ) - post: Dict[str, Account] = { + post: Dict[Address, Account] = { entry_code_address: Account( code="0x", storage=Storage({0: selfdestruct_with_transfer_contract_address}) ), @@ -305,13 +305,13 @@ def test_selfdestruct_created_in_same_tx_with_revert( # noqa SC200 @pytest.fixture def pre_with_selfdestructable( # noqa: SC200 - recursive_revert_contract_address: str, + recursive_revert_contract_address: Address, recursive_revert_contract_code: SupportsBytes, - selfdestruct_with_transfer_initcode_copy_from_address: str, + selfdestruct_with_transfer_initcode_copy_from_address: Address, selfdestruct_with_transfer_contract_initcode: SupportsBytes, - selfdestruct_with_transfer_contract_address: str, + selfdestruct_with_transfer_contract_address: Address, yul: YulCompiler, -) -> Dict[str, Account]: +) -> Dict[Address, Account]: """Preset for selfdestruct_not_created_in_same_tx_with_revert""" return { TestAddress: Account(balance=100_000_000_000_000_000_000), @@ -335,12 +335,12 @@ def pre_with_selfdestructable( # noqa: SC200 def test_selfdestruct_not_created_in_same_tx_with_revert( state_test: StateTestFiller, env: Environment, - entry_code_address: str, + entry_code_address: Address, selfdestruct_on_outer_call: int, selfdestruct_with_transfer_contract_code: SupportsBytes, - selfdestruct_with_transfer_contract_address: str, - selfdestruct_recipient_address: str, - recursive_revert_contract_address: str, + selfdestruct_with_transfer_contract_address: Address, + selfdestruct_recipient_address: Address, + recursive_revert_contract_address: Address, recursive_revert_contract_code: SupportsBytes, ): """ @@ -349,7 +349,7 @@ def test_selfdestruct_not_created_in_same_tx_with_revert( """ entry_code = Op.CALL( Op.GASLIMIT(), - Op.PUSH20(recursive_revert_contract_address), + recursive_revert_contract_address, 0, # value 0, # arg offset 0, # arg length @@ -357,7 +357,7 @@ def test_selfdestruct_not_created_in_same_tx_with_revert( 0, # ret length ) - pre: Dict[str, Account] = { + pre: Dict[Address, Account] = { TestAddress: Account(balance=100_000_000_000_000_000_000), selfdestruct_with_transfer_contract_address: Account( code=selfdestruct_with_transfer_contract_code @@ -367,7 +367,7 @@ def test_selfdestruct_not_created_in_same_tx_with_revert( ), } - post: Dict[str, Account] = { + post: Dict[Address, Account] = { entry_code_address: Account(code="0x"), } diff --git a/tests/cancun/eip7516_blobgasfee/test_blobgasfee_opcode.py b/tests/cancun/eip7516_blobgasfee/test_blobgasfee_opcode.py index d6b55b7f95..5a3463bb03 100644 --- a/tests/cancun/eip7516_blobgasfee/test_blobgasfee_opcode.py +++ b/tests/cancun/eip7516_blobgasfee/test_blobgasfee_opcode.py @@ -9,7 +9,7 @@ import pytest -from ethereum_test_tools import Account, Block, BlockchainTestFiller, Environment +from ethereum_test_tools import Account, Address, Block, BlockchainTestFiller, Environment from ethereum_test_tools import Opcodes as Op from ethereum_test_tools import StateTestFiller, Storage, TestAddress, Transaction @@ -17,8 +17,8 @@ REFERENCE_SPEC_VERSION = "2ade0452efe8124378f35284676ddfd16dd56ecd" # Code address used to call the test bytecode on every test case. -code_caller_address = 0x100 -code_callee_address = 0x200 +code_caller_address = Address(0x100) +code_callee_address = Address(0x200) BLOBBASEFEE_GAS = 2 @@ -38,7 +38,7 @@ def caller_code( """ Bytecode used to call the bytecode containing the BLOBBASEFEE opcode. """ - return Op.SSTORE(Op.NUMBER, Op.CALL(call_gas, Op.PUSH20(code_callee_address), 0, 0, 0, 0, 0)) + return Op.SSTORE(Op.NUMBER, Op.CALL(call_gas, code_callee_address, 0, 0, 0, 0, 0)) @pytest.fixture diff --git a/tests/frontier/opcodes/test_call_and_callcode_gas_calculation.py b/tests/frontier/opcodes/test_call_and_callcode_gas_calculation.py index d6b456e2d3..cffb53a624 100644 --- a/tests/frontier/opcodes/test_call_and_callcode_gas_calculation.py +++ b/tests/frontier/opcodes/test_call_and_callcode_gas_calculation.py @@ -30,11 +30,11 @@ from ethereum_test_tools import ( Account, + Address, Environment, StateTestFiller, TestAddress, Transaction, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -103,7 +103,7 @@ def caller_tx() -> Transaction: return Transaction( chain_id=0x01, nonce=0, - to=to_address(Contract.caller), + to=Address(Contract.caller), value=1, gas_limit=500000, gas_price=7, @@ -111,14 +111,14 @@ def caller_tx() -> Transaction: @pytest.fixture -def pre(caller_code: bytes, callee_code: bytes) -> Dict[str, Account]: # noqa: D103 +def pre(caller_code: bytes, callee_code: bytes) -> Dict[Address, Account]: # noqa: D103 return { - to_address(Contract.caller): Account( + Address(Contract.caller): Account( balance=0x03, code=caller_code, nonce=1, ), - to_address(Contract.callee): Account( + Address(Contract.callee): Account( balance=0x03, code=callee_code, nonce=1, @@ -130,9 +130,9 @@ def pre(caller_code: bytes, callee_code: bytes) -> Dict[str, Account]: # noqa: @pytest.fixture -def post(is_sufficient_gas: bool) -> Dict[str, Account]: # noqa: D103 +def post(is_sufficient_gas: bool) -> Dict[Address, Account]: # noqa: D103 return { - to_address(Contract.caller): Account(storage={0x00: 0x01 if is_sufficient_gas else 0x00}), + Address(Contract.caller): Account(storage={0x00: 0x01 if is_sufficient_gas else 0x00}), } diff --git a/tests/frontier/opcodes/test_dup.py b/tests/frontier/opcodes/test_dup.py index c7186c08f2..00894dd284 100644 --- a/tests/frontier/opcodes/test_dup.py +++ b/tests/frontier/opcodes/test_dup.py @@ -6,9 +6,9 @@ import pytest from ethereum_test_forks import Frontier, Homestead -from ethereum_test_tools import Account, Environment +from ethereum_test_tools import Account, Address, Environment from ethereum_test_tools import Opcodes as Op -from ethereum_test_tools import StateTestFiller, Storage, Transaction, to_address +from ethereum_test_tools import StateTestFiller, Storage, TestAddress, Transaction @pytest.mark.parametrize( @@ -47,10 +47,10 @@ def test_dup( by Ori Pomerantz. """ # noqa: E501 env = Environment() - pre = {"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b": Account(balance=1000000000000000000000)} + pre = {TestAddress: Account(balance=1000000000000000000000)} post = {} - account = to_address(0x100) + account = Address(0x100) # Push 0x00 - 0x10 onto the stack account_code = b"".join([Op.PUSH1(i) for i in range(0x11)]) diff --git a/tests/istanbul/eip1344_chainid/test_chainid.py b/tests/istanbul/eip1344_chainid/test_chainid.py index 4b3704ece1..e5a8d127ce 100644 --- a/tests/istanbul/eip1344_chainid/test_chainid.py +++ b/tests/istanbul/eip1344_chainid/test_chainid.py @@ -7,11 +7,11 @@ from ethereum_test_tools import ( Account, + Address, Environment, StateTestFiller, TestAddress, Transaction, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -33,7 +33,7 @@ def test_chainid(state_test: StateTestFiller): ) pre = { - to_address(0x100): Account(code=Op.SSTORE(1, Op.CHAINID) + Op.STOP), + Address(0x100): Account(code=Op.SSTORE(1, Op.CHAINID) + Op.STOP), TestAddress: Account(balance=1000000000000000000000), } @@ -41,13 +41,13 @@ def test_chainid(state_test: StateTestFiller): ty=0x0, chain_id=0x01, nonce=0, - to=to_address(0x100), + to=Address(0x100), gas_limit=100000000, gas_price=10, ) post = { - to_address(0x100): Account(code="0x4660015500", storage={"0x01": "0x01"}), + Address(0x100): Account(code="0x4660015500", storage={"0x01": "0x01"}), } state_test(env=env, pre=pre, post=post, tx=tx) diff --git a/tests/paris/security/test_selfdestruct_balance_bug.py b/tests/paris/security/test_selfdestruct_balance_bug.py index fbeccd9f94..2cb766c135 100644 --- a/tests/paris/security/test_selfdestruct_balance_bug.py +++ b/tests/paris/security/test_selfdestruct_balance_bug.py @@ -14,6 +14,7 @@ from ethereum_test_tools import ( Account, + Address, Block, BlockchainTestFiller, Initcode, @@ -21,7 +22,6 @@ Transaction, YulCompiler, compute_create_address, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -81,23 +81,23 @@ def test_tx_selfdestruct_balance_bug(blockchain_test: BlockchainTestFiller, yul: 0, Op.EXTCODESIZE(0xAA), ) - + Op.SSTORE(0xCA1101, Op.CALL(100000, Op.PUSH20(aa_location), 0, 0, 0, 0, 0)) - + Op.CALL(100000, Op.PUSH20(aa_location), 1, 0, 0, 0, 0) + + Op.SSTORE(0xCA1101, Op.CALL(100000, aa_location, 0, 0, 0, 0, 0)) + + Op.CALL(100000, aa_location, 1, 0, 0, 0, 0) ) - balance_code = Op.SSTORE(0xBA1AA, Op.BALANCE(Op.PUSH20(aa_location))) + balance_code = Op.SSTORE(0xBA1AA, Op.BALANCE(aa_location)) pre = { # sender TestAddress: Account(balance=1000000000), # caller - to_address(0xCC): Account(balance=1000000000, code=cc_code, nonce=1), + Address(0xCC): Account(balance=1000000000, code=cc_code, nonce=1), # stores balance of 0xaa after each tx 1 - to_address(0xBA11): Account(code=balance_code), + Address(0xBA11): Account(code=balance_code), # stores balance of 0xaa after each tx 2 - to_address(0xBA12): Account(code=balance_code), + Address(0xBA12): Account(code=balance_code), # Initcode of the self-destruct contract - to_address(0xAA): Account(code=aa_code), + Address(0xAA): Account(code=aa_code), } blocks = [ @@ -107,14 +107,14 @@ def test_tx_selfdestruct_balance_bug(blockchain_test: BlockchainTestFiller, yul: # calling with 1 wei call Transaction( nonce=0, - to=to_address(0xCC), + to=Address(0xCC), gas_limit=1000000, gas_price=10, ), # Dummy tx to store balance of 0xaa after first TX. Transaction( nonce=1, - to=to_address(0xBA11), + to=Address(0xBA11), gas_limit=100000, gas_price=10, ), @@ -129,7 +129,7 @@ def test_tx_selfdestruct_balance_bug(blockchain_test: BlockchainTestFiller, yul: # Dummy tx to store balance of 0xaa after second TX. Transaction( nonce=3, - to=to_address(0xBA12), + to=Address(0xBA12), gas_limit=100000, gas_price=10, ), @@ -139,13 +139,13 @@ def test_tx_selfdestruct_balance_bug(blockchain_test: BlockchainTestFiller, yul: post = { # Check call from caller has succeeded. - to_address(0xCC): Account(nonce=2, storage={0xCA1101: 1}), + Address(0xCC): Account(nonce=2, storage={0xCA1101: 1}), # Check balance of 0xaa after tx 1 is 0 wei, i.e self-destructed. # Vulnerable versions should return 1 wei. - to_address(0xBA11): Account(storage={0xBA1AA: 0}), + Address(0xBA11): Account(storage={0xBA1AA: 0}), # Check that 0xaa exists and balance after tx 2 is 5 wei. # Vulnerable versions should return 6 wei. - to_address(0xBA12): Account(storage={0xBA1AA: 5}), + Address(0xBA12): Account(storage={0xBA1AA: 5}), aa_location: Account(storage={0: 0}), } diff --git a/tests/shanghai/eip3651_warm_coinbase/test_warm_coinbase.py b/tests/shanghai/eip3651_warm_coinbase/test_warm_coinbase.py index 8fb779ac67..78d7b0dabb 100644 --- a/tests/shanghai/eip3651_warm_coinbase/test_warm_coinbase.py +++ b/tests/shanghai/eip3651_warm_coinbase/test_warm_coinbase.py @@ -11,11 +11,11 @@ from ethereum_test_forks import Shanghai from ethereum_test_tools import ( Account, + Address, CodeGasMeasure, Environment, TestAddress, Transaction, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -101,7 +101,7 @@ def test_warm_coinbase_call_out_of_gas( pre = { TestAddress: Account(balance=1000000000000000000000), caller_address: Account(code=caller_code), - to_address(contract_under_test_address): Account(code=contract_under_test_code), + Address(contract_under_test_address): Account(code=contract_under_test_code), } tx = Transaction( @@ -236,7 +236,7 @@ def test_warm_coinbase_gas_usage(state_test, fork, opcode, code_gas_measure): timestamp=1000, ) - measure_address = to_address(0x100) + measure_address = Address(0x100) pre = { TestAddress: Account(balance=1000000000000000000000), measure_address: Account(code=code_gas_measure, balance=1000000000000000000000), diff --git a/tests/shanghai/eip3855_push0/test_push0.py b/tests/shanghai/eip3855_push0/test_push0.py index 74069f8149..456a0fa8d4 100644 --- a/tests/shanghai/eip3855_push0/test_push0.py +++ b/tests/shanghai/eip3855_push0/test_push0.py @@ -10,12 +10,12 @@ from ethereum_test_tools import ( Account, + Address, CodeGasMeasure, Environment, StateTestFiller, TestAddress, Transaction, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -42,7 +42,7 @@ def post(): # noqa: D103 @pytest.fixture def addr_1(): # noqa: D103 - return to_address(0x100) + return Address(0x100) @pytest.fixture @@ -143,7 +143,7 @@ def test_push0_during_staticcall( """ Test PUSH0 during STATICCALL. """ - addr_2 = to_address(0x200) + addr_2 = Address(0x200) code_1 = ( Op.SSTORE(0, Op.STATICCALL(100000, 0x200, 0, 0, 0, 0)) diff --git a/tests/shanghai/eip3860_initcode/test_initcode.py b/tests/shanghai/eip3860_initcode/test_initcode.py index b618c226c8..d9dae80879 100644 --- a/tests/shanghai/eip3860_initcode/test_initcode.py +++ b/tests/shanghai/eip3860_initcode/test_initcode.py @@ -13,6 +13,7 @@ from ethereum_test_tools import ( Account, + Address, Environment, Initcode, StateTestFiller, @@ -24,7 +25,6 @@ compute_create2_address, compute_create_address, eip_2028_transaction_data_cost, - to_address, ) from ethereum_test_tools.vm.opcode import Opcodes as Op @@ -530,11 +530,11 @@ def test_create_opcode_initcode( pre = { TestAddress: Account(balance=1000000000000000000000), - to_address(0x100): Account( + Address(0x100): Account( code=create_code, nonce=1, ), - to_address(0x200): Account( + Address(0x200): Account( code=call_code, nonce=1, ), @@ -544,7 +544,7 @@ def test_create_opcode_initcode( tx = Transaction( nonce=0, - to=to_address(0x200), + to=Address(0x200), data=initcode, gas_limit=10000000, gas_price=10, @@ -563,7 +563,7 @@ def test_create_opcode_initcode( if len(initcode) > MAX_INITCODE_SIZE and eip_3860_active: # Call returns 0 as out of gas s[0]==1 - post[to_address(0x200)] = Account( + post[Address(0x200)] = Account( nonce=1, storage={ 0: 1, @@ -572,7 +572,7 @@ def test_create_opcode_initcode( ) post[created_contract_address] = Account.NONEXISTENT - post[to_address(0x100)] = Account( + post[Address(0x100)] = Account( nonce=1, storage={ 0: 0, @@ -597,7 +597,7 @@ def test_create_opcode_initcode( expected_gas_usage += calculate_initcode_word_cost(len(initcode)) # Call returns 1 as valid initcode length s[0]==1 && s[1]==1 - post[to_address(0x200)] = Account( + post[Address(0x200)] = Account( nonce=1, storage={ 0: 0, @@ -606,7 +606,7 @@ def test_create_opcode_initcode( ) post[created_contract_address] = Account(code=initcode.deploy_code) - post[to_address(0x100)] = Account( + post[Address(0x100)] = Account( nonce=2, storage={ 0: created_contract_address, diff --git a/tests/shanghai/eip4895_withdrawals/test_withdrawals.py b/tests/shanghai/eip4895_withdrawals/test_withdrawals.py index bb1ade4b4f..9dda997c0e 100644 --- a/tests/shanghai/eip4895_withdrawals/test_withdrawals.py +++ b/tests/shanghai/eip4895_withdrawals/test_withdrawals.py @@ -11,15 +11,15 @@ from ethereum_test_tools import ( Account, + Address, Block, BlockchainTestFiller, + Hash, TestAddress, Transaction, TransactionException, Withdrawal, compute_create_address, - to_address, - to_hash, ) from ethereum_test_tools.vm.opcode import Opcodes as Op from evm_transition_tool import TransitionTool @@ -64,7 +64,7 @@ def tx(self): # noqa: D102 nonce=0, gas_price=ONE_GWEI, gas_limit=21000, - to=to_address(0x100), + to=Address(0x100), data="0x", ) @@ -136,8 +136,8 @@ def test_use_value_in_contract(blockchain_test: BlockchainTestFiller): pre = { TestAddress: Account(balance=1000000000000000000000, nonce=0), - to_address(0x100): Account(balance=0, code=SEND_ONE_GWEI), - to_address(0x200): Account(balance=1), + Address(0x100): Account(balance=0, code=SEND_ONE_GWEI), + Address(0x200): Account(balance=1), } tx = Transaction( # Transaction sent from the `TestAddress`, which has 0 balance at start @@ -145,13 +145,13 @@ def test_use_value_in_contract(blockchain_test: BlockchainTestFiller): value=0, gas_price=10, gas_limit=100000, - to=to_address(0x100), + to=Address(0x100), data="0x", ) withdrawal = Withdrawal( index=0, validator=0, - address=to_address(0x100), + address=Address(0x100), amount=1, ) @@ -165,13 +165,13 @@ def test_use_value_in_contract(blockchain_test: BlockchainTestFiller): ), ] post = { - to_address(0x100): Account( + Address(0x100): Account( storage={ 0x1: 0x0, # Call fails on the first attempt 0x2: 0x1, # Succeeds on the second attempt } ), - to_address(0x200): Account( + Address(0x200): Account( balance=ONE_GWEI + 1, ), } @@ -190,10 +190,10 @@ def test_balance_within_block(blockchain_test: BlockchainTestFiller): ) pre = { TestAddress: Account(balance=1000000000000000000000, nonce=0), - to_address(0x100): Account( + Address(0x100): Account( code=SAVE_BALANCE_ON_BLOCK_NUMBER, ), - to_address(0x200): Account( + Address(0x200): Account( balance=ONE_GWEI, ), } @@ -203,15 +203,15 @@ def test_balance_within_block(blockchain_test: BlockchainTestFiller): Transaction( nonce=0, gas_limit=100000, - to=to_address(0x100), - data=to_hash(0x200), + to=Address(0x100), + data=Hash(0x200), ) ], withdrawals=[ Withdrawal( index=0, validator=0, - address=to_address(0x200), + address=Address(0x200), amount=1, ) ], @@ -221,15 +221,15 @@ def test_balance_within_block(blockchain_test: BlockchainTestFiller): Transaction( nonce=1, gas_limit=100000, - to=to_address(0x100), - data=to_hash(0x200), + to=Address(0x100), + data=Hash(0x200), ) ] ), ] post = { - to_address(0x100): Account( + Address(0x100): Account( storage={ 1: ONE_GWEI, 2: 2 * ONE_GWEI, @@ -251,17 +251,17 @@ class TestMultipleWithdrawalsSameAddress: """ ADDRESSES = [ - to_address(0x0), # Zero address - to_address(0x1), # Pre-compiles - to_address(0x2), - to_address(0x3), - to_address(0x4), - to_address(0x5), - to_address(0x6), - to_address(0x7), - to_address(0x8), - to_address(0x9), - to_address(2**160 - 1), + Address(0x0), # Zero address + Address(0x1), # Pre-compiles + Address(0x2), + Address(0x3), + Address(0x4), + Address(0x5), + Address(0x6), + Address(0x7), + Address(0x8), + Address(0x9), + Address(2**160 - 1), ] @pytest.fixture @@ -343,7 +343,7 @@ def test_many_withdrawals(blockchain_test: BlockchainTestFiller): withdrawals = [] post = {} for i in range(N): - addr = to_address(0x100 * i) + addr = Address(0x100 * i) amount = i * 1 pre[addr] = Account( code=Op.SSTORE(Op.NUMBER, 1), @@ -379,11 +379,11 @@ def test_self_destructing_account(blockchain_test: BlockchainTestFiller): """ pre = { TestAddress: Account(balance=1000000000000000000000, nonce=0), - to_address(0x100): Account( + Address(0x100): Account( code=Op.SELFDESTRUCT(Op.CALLDATALOAD(0)), balance=(100 * ONE_GWEI), ), - to_address(0x200): Account( + Address(0x200): Account( balance=1, ), } @@ -394,14 +394,14 @@ def test_self_destructing_account(blockchain_test: BlockchainTestFiller): nonce=0, gas_price=10, gas_limit=100000, - to=to_address(0x100), - data=to_hash(0x200), + to=Address(0x100), + data=Hash(0x200), ) withdrawal = Withdrawal( index=0, validator=0, - address=to_address(0x100), + address=Address(0x100), amount=(99), ) @@ -411,11 +411,11 @@ def test_self_destructing_account(blockchain_test: BlockchainTestFiller): ) post = { - to_address(0x100): Account( + Address(0x100): Account( code=None, balance=(99 * ONE_GWEI), ), - to_address(0x200): Account( + Address(0x200): Account( code=None, balance=(100 * ONE_GWEI) + 1, ), @@ -487,16 +487,16 @@ def test_no_evm_execution(blockchain_test: BlockchainTestFiller): """ pre = { TestAddress: Account(balance=1000000000000000000000, nonce=0), - to_address(0x100): Account( + Address(0x100): Account( code=Op.SSTORE(Op.NUMBER, 1), ), - to_address(0x200): Account( + Address(0x200): Account( code=Op.SSTORE(Op.NUMBER, 1), ), - to_address(0x300): Account( + Address(0x300): Account( code=Op.SSTORE(Op.NUMBER, 1), ), - to_address(0x400): Account( + Address(0x400): Account( code=Op.SSTORE(Op.NUMBER, 1), ), } @@ -506,25 +506,25 @@ def test_no_evm_execution(blockchain_test: BlockchainTestFiller): Transaction( nonce=0, gas_limit=100000, - to=to_address(0x300), + to=Address(0x300), ), Transaction( nonce=1, gas_limit=100000, - to=to_address(0x400), + to=Address(0x400), ), ], withdrawals=[ Withdrawal( index=0, validator=0, - address=to_address(0x100), + address=Address(0x100), amount=1, ), Withdrawal( index=1, validator=1, - address=to_address(0x200), + address=Address(0x200), amount=1, ), ], @@ -534,25 +534,25 @@ def test_no_evm_execution(blockchain_test: BlockchainTestFiller): Transaction( nonce=2, gas_limit=100000, - to=to_address(0x100), + to=Address(0x100), ), Transaction( nonce=3, gas_limit=100000, - to=to_address(0x200), + to=Address(0x200), ), ], withdrawals=[ Withdrawal( index=0, validator=0, - address=to_address(0x300), + address=Address(0x300), amount=1, ), Withdrawal( index=1, validator=1, - address=to_address(0x400), + address=Address(0x400), amount=1, ), ], @@ -560,10 +560,10 @@ def test_no_evm_execution(blockchain_test: BlockchainTestFiller): ] post = { - to_address(0x100): Account(storage={2: 1}), - to_address(0x200): Account(storage={2: 1}), - to_address(0x300): Account(storage={1: 1}), - to_address(0x400): Account(storage={1: 1}), + Address(0x100): Account(storage={2: 1}), + Address(0x200): Account(storage={2: 1}), + Address(0x300): Account(storage={1: 1}), + Address(0x400): Account(storage={1: 1}), } blockchain_test(pre=pre, post=post, blocks=blocks) @@ -600,7 +600,7 @@ def test_zero_amount( """ pre = { TestAddress: Account(balance=1000000000000000000000, nonce=0), - to_address(0x200): Account( + Address(0x200): Account( code="0x00", balance=0, ), @@ -611,46 +611,46 @@ def test_zero_amount( Withdrawal( index=0, validator=0, - address=to_address(0x100), + address=Address(0x100), amount=0, ), # No value, touched account Withdrawal( index=0, validator=0, - address=to_address(0x200), + address=Address(0x200), amount=0, ), # Withdrawal with value Withdrawal( index=1, validator=0, - address=to_address(0x300), + address=Address(0x300), amount=1, ), # Withdrawal with maximum amount Withdrawal( index=2, validator=0, - address=to_address(0x400), + address=Address(0x400), amount=2**64 - 1, ), ] all_post = { - to_address(0x100): Account.NONEXISTENT, - to_address(0x200): Account(code="0x00", balance=0), - to_address(0x300): Account(balance=ONE_GWEI), - to_address(0x400): Account(balance=(2**64 - 1) * ONE_GWEI), + Address(0x100): Account.NONEXISTENT, + Address(0x200): Account(code="0x00", balance=0), + Address(0x300): Account(balance=ONE_GWEI), + Address(0x400): Account(balance=(2**64 - 1) * ONE_GWEI), } withdrawals: List[Withdrawal] = [] - post: Mapping[str, Account | object] = {} + post: Mapping[Address, Account | object] = {} if test_case == ZeroAmountTestCases.TWO_ZERO: withdrawals = all_withdrawals[0:2] post = { account: all_post[account] for account in post - if account in [to_address(0x100), to_address(0x200)] + if account in [Address(0x100), Address(0x200)] } elif test_case == ZeroAmountTestCases.THREE_ONE_WITH_VALUE: withdrawals = all_withdrawals[0:3] @@ -659,9 +659,9 @@ def test_zero_amount( for account in post if account in [ - to_address(0x100), - to_address(0x200), - to_address(0x300), + Address(0x100), + Address(0x200), + Address(0x300), ] } elif test_case == ZeroAmountTestCases.FOUR_ONE_WITH_MAX: @@ -706,7 +706,7 @@ def test_large_amount(blockchain_test: BlockchainTestFiller): post = {} for i, amount in enumerate(amounts): - addr = to_address(0x100 * (i + 1)) + addr = Address(0x100 * (i + 1)) withdrawals.append( Withdrawal( index=i, @@ -747,7 +747,7 @@ def test_withdrawing_to_precompiles( Withdrawal( index=0, validator=0, - address=to_address(precompile), + address=Address(precompile), amount=amount, ) ] @@ -758,7 +758,7 @@ def test_withdrawing_to_precompiles( Transaction( nonce=0, gas_limit=100000, - to=to_address(precompile), + to=Address(precompile), ), ], ),