Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
winsvega committed Dec 10, 2024
1 parent 8fa34cc commit 07aee0a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
20 changes: 19 additions & 1 deletion src/ethereum_test_base_types/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,34 @@ def to_fixed_size_bytes(input: FixedSizeBytesConvertible, size: int) -> bytes:
"""
Converts multiple types into fixed-size bytes.
"""
isString: bool = False
if isinstance(input, int):
return int.to_bytes(input, length=size, byteorder="big", signed=input < 0)
if isinstance(input, str):
isString = True
input = to_bytes(input)
if len(input) > size:
raise Exception(f"input is too large for fixed size bytes: {len(input)} > {size}")
if len(input) < size:
if len(input) < size and not isString:
raise Exception(f"input is too small for fixed size bytes: {len(input)} < {size}")
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
4 changes: 2 additions & 2 deletions src/ethereum_test_specs/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,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
21 changes: 21 additions & 0 deletions src/ethereum_test_types/tests/test_types.py
Original file line number Diff line number Diff line change
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 07aee0a

Please sign in to comment.