From 301dd8bf478fbf73a97721a99fe5c1e77e7cae13 Mon Sep 17 00:00:00 2001 From: Dimitry Kh Date: Mon, 9 Dec 2024 22:02:02 +0100 Subject: [PATCH] tests --- src/ethereum_test_base_types/conversions.py | 15 ++++ .../tests/test_base_types.py | 75 +++++++++++++++---- .../tests/test_state.py | 16 ++-- src/ethereum_test_specs/tests/test_types.py | 12 ++- .../tests/test_transactions.py | 7 +- src/ethereum_test_types/tests/test_types.py | 25 ++++++- .../test_tstorage_execution_contexts.py | 5 +- 7 files changed, 123 insertions(+), 32 deletions(-) diff --git a/src/ethereum_test_base_types/conversions.py b/src/ethereum_test_base_types/conversions.py index 43ec7ffd93..38c7e0c46c 100644 --- a/src/ethereum_test_base_types/conversions.py +++ b/src/ethereum_test_base_types/conversions.py @@ -69,6 +69,21 @@ def to_fixed_size_bytes(input: FixedSizeBytesConvertible, size: int) -> bytes: return bytes(input).rjust(size, b"\x00") +def left_pad_zeros_up_to_size(input: bytes, size: int) -> bytes: + """ + Pads the given data to fit into a size-byte bytes. If the data is longer than + size bytes, it raises a ValueError. If it is shorter, it left pads with zero bytes. + + :param data: The input data to pad. + :return: A Hash object of exactly size bytes. + """ + input = to_bytes(input) + if len(input) > size: + raise ValueError(f"Data cannot be longer than {size} bytes.") + padded_data = bytes(input).rjust(size, b"\x00") + return bytes(padded_data) + + def to_hex(input: BytesConvertible) -> str: """ Converts multiple types into a bytes hex string. diff --git a/src/ethereum_test_base_types/tests/test_base_types.py b/src/ethereum_test_base_types/tests/test_base_types.py index e4a8fa5ebb..f79a306688 100644 --- a/src/ethereum_test_base_types/tests/test_base_types.py +++ b/src/ethereum_test_base_types/tests/test_base_types.py @@ -14,21 +14,66 @@ @pytest.mark.parametrize( "a, b, equal", [ - (Address("0x0"), Address("0x0"), True), - (Address("0x0"), Address("0x1"), False), - (Address("0x1"), Address("0x0"), False), - (Address("0x1"), "0x1", True), - (Address("0x1"), "0x2", False), - (Address("0x1"), 1, True), - (Address("0x1"), 2, False), - (Address("0x1"), b"\x01", True), - (Address("0x1"), b"\x02", False), - ("0x1", Address("0x1"), True), - ("0x2", Address("0x1"), False), - (1, Address("0x1"), True), - (2, Address("0x1"), False), - (b"\x01", Address("0x1"), True), - (b"\x02", Address("0x1"), False), + (Address(0), Address(0), True), + ( + Address("0x0000000000000000000000000000000000000000"), + Address("0x0000000000000000000000000000000000000000"), + True, + ), + ( + Address("0x0000000000000000000000000000000000000000"), + Address("0x0000000000000000000000000000000000000001"), + False, + ), + ( + Address("0x0000000000000000000000000000000000000001"), + Address("0x0000000000000000000000000000000000000000"), + False, + ), + ( + Address("0x0000000000000000000000000000000000000001"), + "0x0000000000000000000000000000000000000001", + True, + ), + ( + Address("0x0000000000000000000000000000000000000001"), + "0x0000000000000000000000000000000000000002", + False, + ), + (Address("0x0000000000000000000000000000000000000001"), 1, True), + (Address("0x0000000000000000000000000000000000000001"), 2, False), + ( + Address("0x0000000000000000000000000000000000000001"), + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", + True, + ), + ( + Address("0x0000000000000000000000000000000000000001"), + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02", + False, + ), + ( + "0x0000000000000000000000000000000000000001", + Address("0x0000000000000000000000000000000000000001"), + True, + ), + ( + "0x0000000000000000000000000000000000000002", + Address("0x0000000000000000000000000000000000000001"), + False, + ), + (1, Address("0x0000000000000000000000000000000000000001"), True), + (2, Address("0x0000000000000000000000000000000000000001"), False), + ( + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", + Address("0x0000000000000000000000000000000000000001"), + True, + ), + ( + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02", + Address("0x0000000000000000000000000000000000000001"), + False, + ), (Hash("0x0"), Hash("0x0"), True), (Hash("0x0"), Hash("0x1"), False), (Hash("0x1"), Hash("0x0"), False), diff --git a/src/ethereum_test_fixtures/tests/test_state.py b/src/ethereum_test_fixtures/tests/test_state.py index c7b4cb584f..30f4863cf5 100644 --- a/src/ethereum_test_fixtures/tests/test_state.py +++ b/src/ethereum_test_fixtures/tests/test_state.py @@ -18,8 +18,8 @@ pytest.param( True, FixtureForkPost( - state_root="0x00", - logs_hash="0x01", + state_root=0, + logs_hash=1, tx_bytes="0x02", ), { @@ -33,8 +33,8 @@ pytest.param( True, FixtureForkPost( - state_root="0x00", - logs_hash="0x01", + state_root=0, + logs_hash=1, tx_bytes="0x02", expect_exception=TransactionException.INITCODE_SIZE_EXCEEDED, ), @@ -51,8 +51,8 @@ False, # Can not be deserialized: A single expect_exception str will not be # deserialized as a list and therefore will not match the model_instance definition. FixtureForkPost( - state_root="0x00", - logs_hash="0x01", + state_root=0, + logs_hash=1, tx_bytes="0x02", expect_exception=[TransactionException.INITCODE_SIZE_EXCEEDED], ), @@ -68,8 +68,8 @@ pytest.param( True, FixtureForkPost( - state_root="0x00", - logs_hash="0x01", + state_root=0, + logs_hash=1, tx_bytes="0x02", expect_exception=[ TransactionException.INITCODE_SIZE_EXCEEDED, diff --git a/src/ethereum_test_specs/tests/test_types.py b/src/ethereum_test_specs/tests/test_types.py index 72aff9eeaa..8ebdc37f83 100644 --- a/src/ethereum_test_specs/tests/test_types.py +++ b/src/ethereum_test_specs/tests/test_types.py @@ -44,8 +44,12 @@ ), pytest.param( fixture_header_ones, - Header(state_root="0x100"), - fixture_header_ones.copy(state_root="0x100"), + Header( + state_root="0x0000000000000000000000000000000000000000000000000000000000000100" + ), + fixture_header_ones.copy( + state_root="0x0000000000000000000000000000000000000000000000000000000000000100" + ), id="state_root_as_str", ), pytest.param( @@ -86,13 +90,13 @@ ), pytest.param( fixture_header_ones, - Header(logs_bloom=Hash(100)), + Header(logs_bloom=Bloom(100)), fixture_header_ones.copy(logs_bloom=100), id="bloom_as_hash", ), pytest.param( fixture_header_ones, - Header(state_root="0x100", logs_bloom=Hash(200), difficulty=300), + Header(state_root="0x100", logs_bloom=Bloom(200), difficulty=300), fixture_header_ones.copy( state_root=0x100, logs_bloom=200, diff --git a/src/ethereum_test_types/tests/test_transactions.py b/src/ethereum_test_types/tests/test_transactions.py index bbe61b476c..5b397d1f20 100644 --- a/src/ethereum_test_types/tests/test_transactions.py +++ b/src/ethereum_test_types/tests/test_transactions.py @@ -107,7 +107,12 @@ ty=1, nonce=0, gas_price=1000000000, - access_list=[AccessList(address="0x123", storage_keys=["0x456", "0x789"])], + access_list=[ + AccessList( + address="0x0000000000000000000000000000000000000123", + storage_keys=["0x456", "0x789"], + ) + ], ), ( 0, diff --git a/src/ethereum_test_types/tests/test_types.py b/src/ethereum_test_types/tests/test_types.py index 770ee6c42c..1e91492535 100644 --- a/src/ethereum_test_types/tests/test_types.py +++ b/src/ethereum_test_types/tests/test_types.py @@ -335,13 +335,13 @@ def test_account_check_alloc(account: Account, alloc_dict: Dict[Any, Any], shoul ), pytest.param( Alloc({0x2: {"nonce": 1}}), # type: ignore - Alloc({"0x02": {"nonce": 2}}), # type: ignore + Alloc({"0x0000000000000000000000000000000000000002": {"nonce": 2}}), # type: ignore Alloc({0x2: Account(nonce=2)}), # type: ignore id="overwrite_account", ), pytest.param( Alloc({0x2: {"balance": 1}}), # type: ignore - Alloc({"0x02": {"nonce": 1}}), # type: ignore + Alloc({"0x0000000000000000000000000000000000000002": {"nonce": 1}}), # type: ignore Alloc({0x2: Account(balance=1, nonce=1)}), # type: ignore id="mix_account", ), @@ -562,6 +562,27 @@ def test_account_merge( }, id="transaction_t8n_to_none", ), + pytest.param( + True, + Transaction( + to="", + ).with_signature_and_sender(), + { + "type": "0x0", + "chainId": "0x1", + "nonce": "0x0", + "to": None, + "value": "0x0", + "input": "0x", + "gas": "0x5208", + "gasPrice": "0xa", + "v": "0x25", + "r": "0x1cfe2cbb0c3577f74d9ae192a7f1ee2d670fe806a040f427af9cb768be3d07ce", + "s": "0xcbe2d029f52dbf93ade486625bed0603945d2c7358b31de99fe8786c00f13da", + "sender": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", + }, + id="transaction_t8n_to_empty_str", + ), pytest.param( True, Transaction( diff --git a/tests/cancun/eip1153_tstore/test_tstorage_execution_contexts.py b/tests/cancun/eip1153_tstore/test_tstorage_execution_contexts.py index b01dcdc705..d0b603b8c9 100644 --- a/tests/cancun/eip1153_tstore/test_tstorage_execution_contexts.py +++ b/tests/cancun/eip1153_tstore/test_tstorage_execution_contexts.py @@ -8,7 +8,8 @@ import pytest -from ethereum_test_tools import Account, Address, Alloc, Bytecode, Environment, Hash +from ethereum_test_base_types.conversions import left_pad_zeros_up_to_size +from ethereum_test_tools import Account, Address, Alloc, Bytecode, Environment from ethereum_test_tools import Opcodes as Op from ethereum_test_tools import StateTestFiller, Transaction @@ -301,7 +302,7 @@ def tx(pre: Alloc, caller_address: Address, callee_address: Address) -> Transact return Transaction( sender=pre.fund_eoa(), to=caller_address, - data=Hash(callee_address), + data=left_pad_zeros_up_to_size(callee_address, 32), gas_limit=1_000_000, )