Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(framework): disallow assign of fixedbytes of size X to bytes of size Y when X != Y #1010

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -60,6 +60,21 @@ def to_fixed_size_bytes(input_bytes: FixedSizeBytesConvertible, size: int) -> by
return bytes(input_bytes).rjust(size, b"\x00")


def left_pad_zeros_up_to_size(input_bytes: bytes, size: int) -> bytes:
"""
Pad 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_bytes = to_bytes(input_bytes)
if len(input_bytes) > size:
raise ValueError(f"Data cannot be longer than {size} bytes.")
padded_data = bytes(input_bytes).rjust(size, b"\x00")
return bytes(padded_data)


def to_hex(input_bytes: BytesConvertible) -> str:
"""Convert multiple types into a bytes hex string."""
return "0x" + to_bytes(input_bytes).hex()
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 @@ -12,34 +12,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 @@ -16,8 +16,8 @@
pytest.param(
True,
FixtureForkPost(
state_root="0x00",
logs_hash="0x01",
state_root=0,
logs_hash=1,
tx_bytes="0x02",
),
{
Expand All @@ -31,8 +31,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 @@ -49,8 +49,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 @@ -66,8 +66,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 @@ -532,7 +532,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 @@ -42,8 +42,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 @@ -72,8 +76,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 @@ -84,13 +104,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 @@ -9,11 +9,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 @@ -25,9 +25,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 @@ -38,27 +38,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.UnexpectedAccountError,
),
# Account should not exist but contained in alloc
(
{"0x1": Account.NONEXISTENT},
{"0x01": {"balance": "1"}},
{"0x0000000000000000000000000000000000000001": Account.NONEXISTENT},
{"0x0000000000000000000000000000000000000001": {"balance": "1"}},
Alloc.UnexpectedAccountError,
),
# Account should not exist but contained in alloc
(
{"0x0a": Account.NONEXISTENT},
{"0x0A": {"code": "0x00"}},
{"0x000000000000000000000000000000000000000a": Account.NONEXISTENT},
{"0x000000000000000000000000000000000000000A": {"code": "0x00"}},
Alloc.UnexpectedAccountError,
),
# Account should exist but not in alloc
(
{"0x0A": Account()},
{"0x000000000000000000000000000000000000000A": Account()},
{
"0x0B": {
"0x000000000000000000000000000000000000000B": {
"nonce": "1",
"code": "0x123",
"balance": "1",
Expand All @@ -70,9 +70,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 @@ -83,9 +83,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
Loading