Skip to content

Commit 63b2744

Browse files
committed
unit tests
1 parent 5a0c0c4 commit 63b2744

File tree

12 files changed

+242
-80
lines changed

12 files changed

+242
-80
lines changed

src/ethereum_test_base_types/conversions.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,33 @@ def to_fixed_size_bytes(input: FixedSizeBytesConvertible, size: int) -> bytes:
6363
return int.to_bytes(input, length=size, byteorder="big", signed=input < 0)
6464
input = to_bytes(input)
6565
if len(input) > size:
66-
raise Exception(f"input is too large for fixed size bytes: {len(input)} > {size}")
66+
raise Exception(
67+
f"input is too large for fixed size bytes: {len(input)} > {size}"
68+
f", input: {to_hex(input)}"
69+
)
6770
if len(input) < size:
68-
raise Exception(f"input is too small for fixed size bytes: {len(input)} < {size}")
71+
raise Exception(
72+
f"input is too small for fixed size bytes: {len(input)} < {size}"
73+
f", input: {to_hex(input)}"
74+
)
6975
return bytes(input).rjust(size, b"\x00")
7076

7177

78+
def left_pad_zeros_up_to_size(input: bytes, size: int) -> bytes:
79+
"""
80+
Pads the given data to fit into a size-byte bytes. If the data is longer than
81+
size bytes, it raises a ValueError. If it is shorter, it left pads with zero bytes.
82+
83+
:param data: The input data to pad.
84+
:return: A Hash object of exactly size bytes.
85+
"""
86+
input = to_bytes(input)
87+
if len(input) > size:
88+
raise ValueError(f"Data cannot be longer than {size} bytes.")
89+
padded_data = bytes(input).rjust(size, b"\x00")
90+
return bytes(padded_data)
91+
92+
7293
def to_hex(input: BytesConvertible) -> str:
7394
"""
7495
Converts multiple types into a bytes hex string.

src/ethereum_test_base_types/tests/test_base_types.py

Lines changed: 111 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,117 @@
1414
@pytest.mark.parametrize(
1515
"a, b, equal",
1616
[
17-
(Address("0x0"), Address("0x0"), True),
18-
(Address("0x0"), Address("0x1"), False),
19-
(Address("0x1"), Address("0x0"), False),
20-
(Address("0x1"), "0x1", True),
21-
(Address("0x1"), "0x2", False),
22-
(Address("0x1"), 1, True),
23-
(Address("0x1"), 2, False),
24-
(Address("0x1"), b"\x01", True),
25-
(Address("0x1"), b"\x02", False),
26-
("0x1", Address("0x1"), True),
27-
("0x2", Address("0x1"), False),
28-
(1, Address("0x1"), True),
29-
(2, Address("0x1"), False),
30-
(b"\x01", Address("0x1"), True),
31-
(b"\x02", Address("0x1"), False),
32-
(Hash("0x0"), Hash("0x0"), True),
33-
(Hash("0x0"), Hash("0x1"), False),
34-
(Hash("0x1"), Hash("0x0"), False),
35-
(Hash("0x1"), "0x1", True),
36-
(Hash("0x1"), "0x2", False),
37-
(Hash("0x1"), 1, True),
38-
(Hash("0x1"), 2, False),
39-
(Hash("0x1"), b"\x01", True),
40-
(Hash("0x1"), b"\x02", False),
41-
("0x1", Hash("0x1"), True),
42-
("0x2", Hash("0x1"), False),
43-
(1, Hash("0x1"), True),
44-
(2, Hash("0x1"), False),
17+
(Address(0), Address(0), True),
18+
(
19+
Address("0x0000000000000000000000000000000000000000"),
20+
Address("0x0000000000000000000000000000000000000000"),
21+
True,
22+
),
23+
(
24+
Address("0x0000000000000000000000000000000000000000"),
25+
Address("0x0000000000000000000000000000000000000001"),
26+
False,
27+
),
28+
(
29+
Address("0x0000000000000000000000000000000000000001"),
30+
Address("0x0000000000000000000000000000000000000000"),
31+
False,
32+
),
33+
(
34+
Address("0x0000000000000000000000000000000000000001"),
35+
"0x0000000000000000000000000000000000000001",
36+
True,
37+
),
38+
(
39+
Address("0x0000000000000000000000000000000000000001"),
40+
"0x0000000000000000000000000000000000000002",
41+
False,
42+
),
43+
(Address("0x0000000000000000000000000000000000000001"), 1, True),
44+
(Address("0x0000000000000000000000000000000000000001"), 2, False),
45+
(
46+
Address("0x0000000000000000000000000000000000000001"),
47+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",
48+
True,
49+
),
50+
(
51+
Address("0x0000000000000000000000000000000000000001"),
52+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02",
53+
False,
54+
),
55+
(
56+
"0x0000000000000000000000000000000000000001",
57+
Address("0x0000000000000000000000000000000000000001"),
58+
True,
59+
),
60+
(
61+
"0x0000000000000000000000000000000000000002",
62+
Address("0x0000000000000000000000000000000000000001"),
63+
False,
64+
),
65+
(1, Address("0x0000000000000000000000000000000000000001"), True),
66+
(2, Address("0x0000000000000000000000000000000000000001"), False),
67+
(
68+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",
69+
Address("0x0000000000000000000000000000000000000001"),
70+
True,
71+
),
72+
(
73+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02",
74+
Address("0x0000000000000000000000000000000000000001"),
75+
False,
76+
),
77+
(
78+
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
79+
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
80+
True,
81+
),
82+
(
83+
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
84+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
85+
False,
86+
),
87+
(
88+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
89+
Hash("0x0000000000000000000000000000000000000000000000000000000000000000"),
90+
False,
91+
),
92+
(
93+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
94+
"0x0000000000000000000000000000000000000000000000000000000000000001",
95+
True,
96+
),
97+
(
98+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
99+
"0x0000000000000000000000000000000000000000000000000000000000000002",
100+
False,
101+
),
102+
(Hash("0x0000000000000000000000000000000000000000000000000000000000000001"), 1, True),
103+
(Hash("0x0000000000000000000000000000000000000000000000000000000000000001"), 2, False),
104+
(
105+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
106+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
107+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",
108+
True,
109+
),
110+
(
111+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
112+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
113+
b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02",
114+
False,
115+
),
116+
(
117+
"0x0000000000000000000000000000000000000000000000000000000000000001",
118+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
119+
True,
120+
),
121+
(
122+
"0x0000000000000000000000000000000000000000000000000000000000000002",
123+
Hash("0x0000000000000000000000000000000000000000000000000000000000000001"),
124+
False,
125+
),
126+
(1, Hash("0x0000000000000000000000000000000000000000000000000000000000000001"), True),
127+
(2, Hash("0x0000000000000000000000000000000000000000000000000000000000000001"), False),
45128
],
46129
)
47130
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
@@ -18,8 +18,8 @@
1818
pytest.param(
1919
True,
2020
FixtureForkPost(
21-
state_root="0x00",
22-
logs_hash="0x01",
21+
state_root=0,
22+
logs_hash=1,
2323
tx_bytes="0x02",
2424
),
2525
{
@@ -33,8 +33,8 @@
3333
pytest.param(
3434
True,
3535
FixtureForkPost(
36-
state_root="0x00",
37-
logs_hash="0x01",
36+
state_root=0,
37+
logs_hash=1,
3838
tx_bytes="0x02",
3939
expect_exception=TransactionException.INITCODE_SIZE_EXCEEDED,
4040
),
@@ -51,8 +51,8 @@
5151
False, # Can not be deserialized: A single expect_exception str will not be
5252
# deserialized as a list and therefore will not match the model_instance definition.
5353
FixtureForkPost(
54-
state_root="0x00",
55-
logs_hash="0x01",
54+
state_root=0,
55+
logs_hash=1,
5656
tx_bytes="0x02",
5757
expect_exception=[TransactionException.INITCODE_SIZE_EXCEEDED],
5858
),
@@ -68,8 +68,8 @@
6868
pytest.param(
6969
True,
7070
FixtureForkPost(
71-
state_root="0x00",
72-
logs_hash="0x01",
71+
state_root=0,
72+
logs_hash=1,
7373
tx_bytes="0x02",
7474
expect_exception=[
7575
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
@@ -542,7 +542,7 @@ def test_fixture_header_join(self, blockchain_test_fixture: BlockchainFixture):
542542

543543
new_state_root = Hash(12345)
544544
# See description of https://github.com/ethereum/execution-spec-tests/pull/398
545-
new_transactions_root = "0x100"
545+
new_transactions_root = 0x100
546546
header_new_fields = Header(
547547
difficulty=new_difficulty,
548548
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
@@ -44,8 +44,12 @@
4444
),
4545
pytest.param(
4646
fixture_header_ones,
47-
Header(state_root="0x100"),
48-
fixture_header_ones.copy(state_root="0x100"),
47+
Header(
48+
state_root="0x0000000000000000000000000000000000000000000000000000000000000100"
49+
),
50+
fixture_header_ones.copy(
51+
state_root="0x0000000000000000000000000000000000000000000000000000000000000100"
52+
),
4953
id="state_root_as_str",
5054
),
5155
pytest.param(
@@ -74,8 +78,24 @@
7478
),
7579
pytest.param(
7680
fixture_header_ones,
77-
Header(logs_bloom="0x100"),
78-
fixture_header_ones.copy(logs_bloom="0x100"),
81+
Header(
82+
logs_bloom="0x00000000000000000000000000000000000000000000000000000000000000000000"
83+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
84+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
85+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
86+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
87+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
88+
"000000000000000000000000000000000000100"
89+
),
90+
fixture_header_ones.copy(
91+
logs_bloom="0x00000000000000000000000000000000000000000000000000000000000000000000"
92+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
93+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
94+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
95+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
96+
"000000000000000000000000000000000000000000000000000000000000000000000000000000000"
97+
"000000000000000000000000000000000000100"
98+
),
7999
id="bloom_as_str",
80100
),
81101
pytest.param(
@@ -86,13 +106,17 @@
86106
),
87107
pytest.param(
88108
fixture_header_ones,
89-
Header(logs_bloom=Hash(100)),
109+
Header(logs_bloom=Bloom(100)),
90110
fixture_header_ones.copy(logs_bloom=100),
91111
id="bloom_as_hash",
92112
),
93113
pytest.param(
94114
fixture_header_ones,
95-
Header(state_root="0x100", logs_bloom=Hash(200), difficulty=300),
115+
Header(
116+
state_root="0x0000000000000000000000000000000000000000000000000000000000000100",
117+
logs_bloom=Bloom(200),
118+
difficulty=300,
119+
),
96120
fixture_header_ones.copy(
97121
state_root=0x100,
98122
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
@@ -13,11 +13,17 @@ def test_address():
1313
"""
1414
Test `ethereum_test.base_types.Address`.
1515
"""
16-
assert Address("0x0") == "0x0000000000000000000000000000000000000000"
16+
assert (
17+
Address("0x0000000000000000000000000000000000000000")
18+
== "0x0000000000000000000000000000000000000000"
19+
)
1720
assert Address(0) == "0x0000000000000000000000000000000000000000"
1821
assert Address(1) == "0x0000000000000000000000000000000000000001"
1922
assert Address(10) == "0x000000000000000000000000000000000000000a"
20-
assert Address("0x10") == "0x0000000000000000000000000000000000000010"
23+
assert (
24+
Address("0x0000000000000000000000000000000000000010")
25+
== "0x0000000000000000000000000000000000000010"
26+
)
2127
assert Address(2 ** (20 * 8) - 1) == "0xffffffffffffffffffffffffffffffffffffffff"
2228
assert Address(0) == Address(0)
2329
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
@@ -31,9 +31,9 @@ def alloc(request: pytest.FixtureRequest) -> Alloc:
3131
[
3232
# Account should not exist but contained in alloc
3333
(
34-
{"0x0": Account.NONEXISTENT},
34+
{"0x0000000000000000000000000000000000000000": Account.NONEXISTENT},
3535
{
36-
"0x00": {
36+
"0x0000000000000000000000000000000000000000": {
3737
"nonce": "1",
3838
"code": "0x123",
3939
"balance": "1",
@@ -44,27 +44,27 @@ def alloc(request: pytest.FixtureRequest) -> Alloc:
4444
),
4545
# Account should not exist but contained in alloc
4646
(
47-
{"0x00": Account.NONEXISTENT},
48-
{"0x0": {"nonce": "1"}},
47+
{"0x0000000000000000000000000000000000000000": Account.NONEXISTENT},
48+
{"0x0000000000000000000000000000000000000000": {"nonce": "1"}},
4949
Alloc.UnexpectedAccount,
5050
),
5151
# Account should not exist but contained in alloc
5252
(
53-
{"0x1": Account.NONEXISTENT},
54-
{"0x01": {"balance": "1"}},
53+
{"0x0000000000000000000000000000000000000001": Account.NONEXISTENT},
54+
{"0x0000000000000000000000000000000000000001": {"balance": "1"}},
5555
Alloc.UnexpectedAccount,
5656
),
5757
# Account should not exist but contained in alloc
5858
(
59-
{"0x0a": Account.NONEXISTENT},
60-
{"0x0A": {"code": "0x00"}},
59+
{"0x000000000000000000000000000000000000000a": Account.NONEXISTENT},
60+
{"0x000000000000000000000000000000000000000A": {"code": "0x00"}},
6161
Alloc.UnexpectedAccount,
6262
),
6363
# Account should exist but not in alloc
6464
(
65-
{"0x0A": Account()},
65+
{"0x000000000000000000000000000000000000000A": Account()},
6666
{
67-
"0x0B": {
67+
"0x000000000000000000000000000000000000000B": {
6868
"nonce": "1",
6969
"code": "0x123",
7070
"balance": "1",
@@ -76,9 +76,9 @@ def alloc(request: pytest.FixtureRequest) -> Alloc:
7676
# Account should exist and contained in alloc, but don't care about
7777
# values
7878
(
79-
{"0x1": Account()},
79+
{"0x0000000000000000000000000000000000000001": Account()},
8080
{
81-
"0x1": {
81+
"0x0000000000000000000000000000000000000001": {
8282
"nonce": "1",
8383
"code": "0x123",
8484
"balance": "1",
@@ -89,9 +89,9 @@ def alloc(request: pytest.FixtureRequest) -> Alloc:
8989
),
9090
# Account should exist and contained in alloc, single incorrect value
9191
(
92-
{"0x1": Account(nonce=0)},
92+
{"0x0000000000000000000000000000000000000001": Account(nonce=0)},
9393
{
94-
"0x1": {
94+
"0x0000000000000000000000000000000000000001": {
9595
"nonce": "1",
9696
"code": "0x123",
9797
"balance": "1",

0 commit comments

Comments
 (0)