Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Dec 19, 2024
1 parent 8fa34cc commit 0014c5e
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 80 deletions.
25 changes: 23 additions & 2 deletions src/ethereum_test_base_types/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,33 @@ def to_fixed_size_bytes(input: FixedSizeBytesConvertible, size: int) -> bytes:
return int.to_bytes(input, length=size, byteorder="big", signed=input < 0)
input = to_bytes(input)
if len(input) > size:
raise Exception(f"input is too large for fixed size bytes: {len(input)} > {size}")
raise Exception(
f"input is too large for fixed size bytes: {len(input)} > {size}"
f", input: {to_hex(input)}"
)
if len(input) < size:
raise Exception(f"input is too small for fixed size bytes: {len(input)} < {size}")
raise Exception(
f"input is too small for fixed size bytes: {len(input)} < {size}"
f", input: {to_hex(input)}"
)
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
139 changes: 111 additions & 28 deletions src/ethereum_test_base_types/tests/test_base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,117 @@
@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),
(Hash("0x0"), Hash("0x0"), True),
(Hash("0x0"), Hash("0x1"), False),
(Hash("0x1"), Hash("0x0"), False),
(Hash("0x1"), "0x1", True),
(Hash("0x1"), "0x2", False),
(Hash("0x1"), 1, True),
(Hash("0x1"), 2, False),
(Hash("0x1"), b"\x01", True),
(Hash("0x1"), b"\x02", False),
("0x1", Hash("0x1"), True),
("0x2", Hash("0x1"), False),
(1, Hash("0x1"), True),
(2, Hash("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("0x0000000000000000000000000000000000000000000000000000000000000000"),
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
True,
),
(
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
False,
),
(
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
False,
),
(
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
"0x0000000000000000000000000000000000000000000000000000000000000001",
True,
),
(
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
"0x0000000000000000000000000000000000000000000000000000000000000002",
False,
),
(Hash("0x0000000000000000000000000000000000000000000000000000000000000001"), 1, True),
(Hash("0x0000000000000000000000000000000000000000000000000000000000000001"), 2, False),
(
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",
True,
),
(
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02",
False,
),
(
"0x0000000000000000000000000000000000000000000000000000000000000001",
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
True,
),
(
"0x0000000000000000000000000000000000000000000000000000000000000002",
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
False,
),
(1, Hash("0x0000000000000000000000000000000000000000000000000000000000000001"), True),
(2, Hash("0x0000000000000000000000000000000000000000000000000000000000000001"), False),
],
)
def test_comparisons(a: Any, b: Any, equal: bool):
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
2 changes: 1 addition & 1 deletion src/ethereum_test_specs/tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ def test_fixture_header_join(self, blockchain_test_fixture: BlockchainFixture):

new_state_root = Hash(12345)
# See description of https://github.com/ethereum/execution-spec-tests/pull/398
new_transactions_root = "0x100"
new_transactions_root = 0x100
header_new_fields = Header(
difficulty=new_difficulty,
state_root=new_state_root,
Expand Down
36 changes: 30 additions & 6 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 @@ -74,8 +78,24 @@
),
pytest.param(
fixture_header_ones,
Header(logs_bloom="0x100"),
fixture_header_ones.copy(logs_bloom="0x100"),
Header(
logs_bloom="0x00000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000100"
),
fixture_header_ones.copy(
logs_bloom="0x00000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000100"
),
id="bloom_as_str",
),
pytest.param(
Expand All @@ -86,13 +106,17 @@
),
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="0x0000000000000000000000000000000000000000000000000000000000000100",
logs_bloom=Bloom(200),
difficulty=300,
),
fixture_header_ones.copy(
state_root=0x100,
logs_bloom=200,
Expand Down
10 changes: 8 additions & 2 deletions src/ethereum_test_types/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ def test_address():
"""
Test `ethereum_test.base_types.Address`.
"""
assert Address("0x0") == "0x0000000000000000000000000000000000000000"
assert (
Address("0x0000000000000000000000000000000000000000")
== "0x0000000000000000000000000000000000000000"
)
assert Address(0) == "0x0000000000000000000000000000000000000000"
assert Address(1) == "0x0000000000000000000000000000000000000001"
assert Address(10) == "0x000000000000000000000000000000000000000a"
assert Address("0x10") == "0x0000000000000000000000000000000000000010"
assert (
Address("0x0000000000000000000000000000000000000010")
== "0x0000000000000000000000000000000000000010"
)
assert Address(2 ** (20 * 8) - 1) == "0xffffffffffffffffffffffffffffffffffffffff"
assert Address(0) == Address(0)
assert Address(0) != Address(1)
Expand Down
28 changes: 14 additions & 14 deletions src/ethereum_test_types/tests/test_post_alloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def alloc(request: pytest.FixtureRequest) -> Alloc:
[
# Account should not exist but contained in alloc
(
{"0x0": Account.NONEXISTENT},
{"0x0000000000000000000000000000000000000000": Account.NONEXISTENT},
{
"0x00": {
"0x0000000000000000000000000000000000000000": {
"nonce": "1",
"code": "0x123",
"balance": "1",
Expand All @@ -44,27 +44,27 @@ def alloc(request: pytest.FixtureRequest) -> Alloc:
),
# Account should not exist but contained in alloc
(
{"0x00": Account.NONEXISTENT},
{"0x0": {"nonce": "1"}},
{"0x0000000000000000000000000000000000000000": Account.NONEXISTENT},
{"0x0000000000000000000000000000000000000000": {"nonce": "1"}},
Alloc.UnexpectedAccount,
),
# Account should not exist but contained in alloc
(
{"0x1": Account.NONEXISTENT},
{"0x01": {"balance": "1"}},
{"0x0000000000000000000000000000000000000001": Account.NONEXISTENT},
{"0x0000000000000000000000000000000000000001": {"balance": "1"}},
Alloc.UnexpectedAccount,
),
# Account should not exist but contained in alloc
(
{"0x0a": Account.NONEXISTENT},
{"0x0A": {"code": "0x00"}},
{"0x000000000000000000000000000000000000000a": Account.NONEXISTENT},
{"0x000000000000000000000000000000000000000A": {"code": "0x00"}},
Alloc.UnexpectedAccount,
),
# Account should exist but not in alloc
(
{"0x0A": Account()},
{"0x000000000000000000000000000000000000000A": Account()},
{
"0x0B": {
"0x000000000000000000000000000000000000000B": {
"nonce": "1",
"code": "0x123",
"balance": "1",
Expand All @@ -76,9 +76,9 @@ def alloc(request: pytest.FixtureRequest) -> Alloc:
# Account should exist and contained in alloc, but don't care about
# values
(
{"0x1": Account()},
{"0x0000000000000000000000000000000000000001": Account()},
{
"0x1": {
"0x0000000000000000000000000000000000000001": {
"nonce": "1",
"code": "0x123",
"balance": "1",
Expand All @@ -89,9 +89,9 @@ def alloc(request: pytest.FixtureRequest) -> Alloc:
),
# Account should exist and contained in alloc, single incorrect value
(
{"0x1": Account(nonce=0)},
{"0x0000000000000000000000000000000000000001": Account(nonce=0)},
{
"0x1": {
"0x0000000000000000000000000000000000000001": {
"nonce": "1",
"code": "0x123",
"balance": "1",
Expand Down
Loading

0 comments on commit 0014c5e

Please sign in to comment.