Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Dec 12, 2024
1 parent 8fa34cc commit 301dd8b
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 32 deletions.
15 changes: 15 additions & 0 deletions src/ethereum_test_base_types/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
75 changes: 60 additions & 15 deletions src/ethereum_test_base_types/tests/test_base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
16 changes: 8 additions & 8 deletions src/ethereum_test_fixtures/tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
pytest.param(
True,
FixtureForkPost(
state_root="0x00",
logs_hash="0x01",
state_root=0,
logs_hash=1,
tx_bytes="0x02",
),
{
Expand All @@ -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,
),
Expand All @@ -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],
),
Expand All @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions src/ethereum_test_specs/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion src/ethereum_test_types/tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 23 additions & 2 deletions src/ethereum_test_types/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
)

Expand Down

0 comments on commit 301dd8b

Please sign in to comment.