Skip to content

Commit de486ba

Browse files
committed
unit tests
1 parent a4adea6 commit de486ba

File tree

12 files changed

+230
-76
lines changed

12 files changed

+230
-76
lines changed

src/ethereum_test_base_types/conversions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ def to_fixed_size_bytes(input_bytes: FixedSizeBytesConvertible, size: int) -> by
6060
return bytes(input_bytes).rjust(size, b"\x00")
6161

6262

63+
def left_pad_zeros_up_to_size(input: bytes, size: int) -> bytes:
64+
"""
65+
Pads the given data to fit into a size-byte bytes. If the data is longer than
66+
size bytes, it raises a ValueError. If it is shorter, it left pads with zero bytes.
67+
68+
:param data: The input data to pad.
69+
:return: A Hash object of exactly size bytes.
70+
"""
71+
input = to_bytes(input)
72+
if len(input) > size:
73+
raise ValueError(f"Data cannot be longer than {size} bytes.")
74+
padded_data = bytes(input).rjust(size, b"\x00")
75+
return bytes(padded_data)
76+
77+
6378
def to_hex(input_bytes: BytesConvertible) -> str:
6479
"""Convert multiple types into a bytes hex string."""
6580
return "0x" + to_bytes(input_bytes).hex()

src/ethereum_test_base_types/tests/test_base_types.py

Lines changed: 111 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,117 @@
1212
@pytest.mark.parametrize(
1313
"a, b, equal",
1414
[
15-
(Address("0x0"), Address("0x0"), True),
16-
(Address("0x0"), Address("0x1"), False),
17-
(Address("0x1"), Address("0x0"), False),
18-
(Address("0x1"), "0x1", True),
19-
(Address("0x1"), "0x2", False),
20-
(Address("0x1"), 1, True),
21-
(Address("0x1"), 2, False),
22-
(Address("0x1"), b"\x01", True),
23-
(Address("0x1"), b"\x02", False),
24-
("0x1", Address("0x1"), True),
25-
("0x2", Address("0x1"), False),
26-
(1, Address("0x1"), True),
27-
(2, Address("0x1"), False),
28-
(b"\x01", Address("0x1"), True),
29-
(b"\x02", Address("0x1"), False),
30-
(Hash("0x0"), Hash("0x0"), True),
31-
(Hash("0x0"), Hash("0x1"), False),
32-
(Hash("0x1"), Hash("0x0"), False),
33-
(Hash("0x1"), "0x1", True),
34-
(Hash("0x1"), "0x2", False),
35-
(Hash("0x1"), 1, True),
36-
(Hash("0x1"), 2, False),
37-
(Hash("0x1"), b"\x01", True),
38-
(Hash("0x1"), b"\x02", False),
39-
("0x1", Hash("0x1"), True),
40-
("0x2", Hash("0x1"), False),
41-
(1, Hash("0x1"), True),
42-
(2, Hash("0x1"), False),
15+
(Address(0), Address(0), True),
16+
(
17+
Address("0x0000000000000000000000000000000000000000"),
18+
Address("0x0000000000000000000000000000000000000000"),
19+
True,
20+
),
21+
(
22+
Address("0x0000000000000000000000000000000000000000"),
23+
Address("0x0000000000000000000000000000000000000001"),
24+
False,
25+
),
26+
(
27+
Address("0x0000000000000000000000000000000000000001"),
28+
Address("0x0000000000000000000000000000000000000000"),
29+
False,
30+
),
31+
(
32+
Address("0x0000000000000000000000000000000000000001"),
33+
"0x0000000000000000000000000000000000000001",
34+
True,
35+
),
36+
(
37+
Address("0x0000000000000000000000000000000000000001"),
38+
"0x0000000000000000000000000000000000000002",
39+
False,
40+
),
41+
(Address("0x0000000000000000000000000000000000000001"), 1, True),
42+
(Address("0x0000000000000000000000000000000000000001"), 2, False),
43+
(
44+
Address("0x0000000000000000000000000000000000000001"),
45+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",
46+
True,
47+
),
48+
(
49+
Address("0x0000000000000000000000000000000000000001"),
50+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02",
51+
False,
52+
),
53+
(
54+
"0x0000000000000000000000000000000000000001",
55+
Address("0x0000000000000000000000000000000000000001"),
56+
True,
57+
),
58+
(
59+
"0x0000000000000000000000000000000000000002",
60+
Address("0x0000000000000000000000000000000000000001"),
61+
False,
62+
),
63+
(1, Address("0x0000000000000000000000000000000000000001"), True),
64+
(2, Address("0x0000000000000000000000000000000000000001"), False),
65+
(
66+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",
67+
Address("0x0000000000000000000000000000000000000001"),
68+
True,
69+
),
70+
(
71+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02",
72+
Address("0x0000000000000000000000000000000000000001"),
73+
False,
74+
),
75+
(
76+
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
77+
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
78+
True,
79+
),
80+
(
81+
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
82+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
83+
False,
84+
),
85+
(
86+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
87+
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
88+
False,
89+
),
90+
(
91+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
92+
"0x0000000000000000000000000000000000000000000000000000000000000001",
93+
True,
94+
),
95+
(
96+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
97+
"0x0000000000000000000000000000000000000000000000000000000000000002",
98+
False,
99+
),
100+
(Hash("0x0000000000000000000000000000000000000000000000000000000000000001"), 1, True),
101+
(Hash("0x0000000000000000000000000000000000000000000000000000000000000001"), 2, False),
102+
(
103+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
104+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
105+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",
106+
True,
107+
),
108+
(
109+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
110+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
111+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02",
112+
False,
113+
),
114+
(
115+
"0x0000000000000000000000000000000000000000000000000000000000000001",
116+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
117+
True,
118+
),
119+
(
120+
"0x0000000000000000000000000000000000000000000000000000000000000002",
121+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
122+
False,
123+
),
124+
(1, Hash("0x0000000000000000000000000000000000000000000000000000000000000001"), True),
125+
(2, Hash("0x0000000000000000000000000000000000000000000000000000000000000001"), False),
43126
],
44127
)
45128
def test_comparisons(a: Any, b: Any, equal: bool):

src/ethereum_test_fixtures/tests/test_state.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
pytest.param(
1717
True,
1818
FixtureForkPost(
19-
state_root="0x00",
20-
logs_hash="0x01",
19+
state_root=0,
20+
logs_hash=1,
2121
tx_bytes="0x02",
2222
),
2323
{
@@ -31,8 +31,8 @@
3131
pytest.param(
3232
True,
3333
FixtureForkPost(
34-
state_root="0x00",
35-
logs_hash="0x01",
34+
state_root=0,
35+
logs_hash=1,
3636
tx_bytes="0x02",
3737
expect_exception=TransactionException.INITCODE_SIZE_EXCEEDED,
3838
),
@@ -49,8 +49,8 @@
4949
False, # Can not be deserialized: A single expect_exception str will not be
5050
# deserialized as a list and therefore will not match the model_instance definition.
5151
FixtureForkPost(
52-
state_root="0x00",
53-
logs_hash="0x01",
52+
state_root=0,
53+
logs_hash=1,
5454
tx_bytes="0x02",
5555
expect_exception=[TransactionException.INITCODE_SIZE_EXCEEDED],
5656
),
@@ -66,8 +66,8 @@
6666
pytest.param(
6767
True,
6868
FixtureForkPost(
69-
state_root="0x00",
70-
logs_hash="0x01",
69+
state_root=0,
70+
logs_hash=1,
7171
tx_bytes="0x02",
7272
expect_exception=[
7373
TransactionException.INITCODE_SIZE_EXCEEDED,

src/ethereum_test_specs/tests/test_fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def test_fixture_header_join(self, blockchain_test_fixture: BlockchainFixture):
532532

533533
new_state_root = Hash(12345)
534534
# See description of https://github.com/ethereum/execution-spec-tests/pull/398
535-
new_transactions_root = "0x100"
535+
new_transactions_root = 0x100
536536
header_new_fields = Header(
537537
difficulty=new_difficulty,
538538
state_root=new_state_root,

src/ethereum_test_specs/tests/test_types.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@
4242
),
4343
pytest.param(
4444
fixture_header_ones,
45-
Header(state_root="0x100"),
46-
fixture_header_ones.copy(state_root="0x100"),
45+
Header(
46+
state_root="0x0000000000000000000000000000000000000000000000000000000000000100"
47+
),
48+
fixture_header_ones.copy(
49+
state_root="0x0000000000000000000000000000000000000000000000000000000000000100"
50+
),
4751
id="state_root_as_str",
4852
),
4953
pytest.param(
@@ -72,8 +76,24 @@
7276
),
7377
pytest.param(
7478
fixture_header_ones,
75-
Header(logs_bloom="0x100"),
76-
fixture_header_ones.copy(logs_bloom="0x100"),
79+
Header(
80+
logs_bloom="0x00000000000000000000000000000000000000000000000000000000000000000000"
81+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
82+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
83+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
84+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
85+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
86+
"000000000000000000000000000000000000100"
87+
),
88+
fixture_header_ones.copy(
89+
logs_bloom="0x00000000000000000000000000000000000000000000000000000000000000000000"
90+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
91+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
92+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
93+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
94+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
95+
"000000000000000000000000000000000000100"
96+
),
7797
id="bloom_as_str",
7898
),
7999
pytest.param(
@@ -84,13 +104,17 @@
84104
),
85105
pytest.param(
86106
fixture_header_ones,
87-
Header(logs_bloom=Hash(100)),
107+
Header(logs_bloom=Bloom(100)),
88108
fixture_header_ones.copy(logs_bloom=100),
89109
id="bloom_as_hash",
90110
),
91111
pytest.param(
92112
fixture_header_ones,
93-
Header(state_root="0x100", logs_bloom=Hash(200), difficulty=300),
113+
Header(
114+
state_root="0x0000000000000000000000000000000000000000000000000000000000000100",
115+
logs_bloom=Bloom(200),
116+
difficulty=300,
117+
),
94118
fixture_header_ones.copy(
95119
state_root=0x100,
96120
logs_bloom=200,

src/ethereum_test_types/tests/test_helpers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99

1010
def test_address():
1111
"""Test `ethereum_test.base_types.Address`."""
12-
assert Address("0x0") == "0x0000000000000000000000000000000000000000"
12+
assert (
13+
Address("0x0000000000000000000000000000000000000000")
14+
== "0x0000000000000000000000000000000000000000"
15+
)
1316
assert Address(0) == "0x0000000000000000000000000000000000000000"
1417
assert Address(1) == "0x0000000000000000000000000000000000000001"
1518
assert Address(10) == "0x000000000000000000000000000000000000000a"
16-
assert Address("0x10") == "0x0000000000000000000000000000000000000010"
19+
assert (
20+
Address("0x0000000000000000000000000000000000000010")
21+
== "0x0000000000000000000000000000000000000010"
22+
)
1723
assert Address(2 ** (20 * 8) - 1) == "0xffffffffffffffffffffffffffffffffffffffff"
1824
assert Address(0) == Address(0)
1925
assert Address(0) != Address(1)

src/ethereum_test_types/tests/test_post_alloc.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def alloc(request: pytest.FixtureRequest) -> Alloc:
2525
[
2626
# Account should not exist but contained in alloc
2727
(
28-
{"0x0": Account.NONEXISTENT},
28+
{"0x0000000000000000000000000000000000000000": Account.NONEXISTENT},
2929
{
30-
"0x00": {
30+
"0x0000000000000000000000000000000000000000": {
3131
"nonce": "1",
3232
"code": "0x123",
3333
"balance": "1",
@@ -38,27 +38,27 @@ def alloc(request: pytest.FixtureRequest) -> Alloc:
3838
),
3939
# Account should not exist but contained in alloc
4040
(
41-
{"0x00": Account.NONEXISTENT},
42-
{"0x0": {"nonce": "1"}},
41+
{"0x0000000000000000000000000000000000000000": Account.NONEXISTENT},
42+
{"0x0000000000000000000000000000000000000000": {"nonce": "1"}},
4343
Alloc.UnexpectedAccountError,
4444
),
4545
# Account should not exist but contained in alloc
4646
(
47-
{"0x1": Account.NONEXISTENT},
48-
{"0x01": {"balance": "1"}},
47+
{"0x0000000000000000000000000000000000000001": Account.NONEXISTENT},
48+
{"0x0000000000000000000000000000000000000001": {"balance": "1"}},
4949
Alloc.UnexpectedAccountError,
5050
),
5151
# Account should not exist but contained in alloc
5252
(
53-
{"0x0a": Account.NONEXISTENT},
54-
{"0x0A": {"code": "0x00"}},
53+
{"0x000000000000000000000000000000000000000a": Account.NONEXISTENT},
54+
{"0x000000000000000000000000000000000000000A": {"code": "0x00"}},
5555
Alloc.UnexpectedAccountError,
5656
),
5757
# Account should exist but not in alloc
5858
(
59-
{"0x0A": Account()},
59+
{"0x000000000000000000000000000000000000000A": Account()},
6060
{
61-
"0x0B": {
61+
"0x000000000000000000000000000000000000000B": {
6262
"nonce": "1",
6363
"code": "0x123",
6464
"balance": "1",
@@ -70,9 +70,9 @@ def alloc(request: pytest.FixtureRequest) -> Alloc:
7070
# Account should exist and contained in alloc, but don't care about
7171
# values
7272
(
73-
{"0x1": Account()},
73+
{"0x0000000000000000000000000000000000000001": Account()},
7474
{
75-
"0x1": {
75+
"0x0000000000000000000000000000000000000001": {
7676
"nonce": "1",
7777
"code": "0x123",
7878
"balance": "1",
@@ -83,9 +83,9 @@ def alloc(request: pytest.FixtureRequest) -> Alloc:
8383
),
8484
# Account should exist and contained in alloc, single incorrect value
8585
(
86-
{"0x1": Account(nonce=0)},
86+
{"0x0000000000000000000000000000000000000001": Account(nonce=0)},
8787
{
88-
"0x1": {
88+
"0x0000000000000000000000000000000000000001": {
8989
"nonce": "1",
9090
"code": "0x123",
9191
"balance": "1",

0 commit comments

Comments
 (0)