Skip to content

Commit 8d5871a

Browse files
committed
Put back bit size validation to the strict bytes encoder
1 parent c4c15b3 commit 8d5871a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

tests/core/utilities/test_abi.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
)
66

77
from web3._utils.abi import (
8+
ExactLengthBytesEncoder,
89
abi_data_tree,
910
get_aligned_abi_inputs,
1011
get_tuple_type_str_parts,
@@ -350,3 +351,9 @@ def test_abi_data_tree(types, data, expected):
350351
)
351352
def test_map_abi_data(types, data, funcs, expected):
352353
assert map_abi_data(funcs, types, data) == expected
354+
355+
356+
@pytest.mark.parametrize("arg", (6, 7, 9, 12, 20, 30))
357+
def test_exact_length_bytes_encoder_raises_on_non_multiples_of_8_bit_size(arg):
358+
with pytest.raises(ValueError, match="multiple of 8"):
359+
_ = ExactLengthBytesEncoder(None, data_byte_size=2, value_bit_size=arg)

web3/_utils/abi.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ class AcceptsHexStrEncoder(encoding.BaseEncoder):
189189
is_strict: bool = None
190190
is_big_endian: bool = False
191191
data_byte_size: int = None
192+
value_bit_size: int = None
192193

193194
def __init__(
194195
self,
@@ -280,11 +281,22 @@ class ExactLengthBytesEncoder(BytesEncoder):
280281

281282
def validate(self) -> None:
282283
super().validate()
284+
if self.value_bit_size is None:
285+
raise ValueError("`value_bit_size` may not be none")
283286
if self.data_byte_size is None:
284287
raise ValueError("`data_byte_size` may not be none")
285288
if self.is_big_endian is None:
286289
raise ValueError("`is_big_endian` may not be none")
287290

291+
if self.value_bit_size % 8 != 0:
292+
raise ValueError(
293+
f"Invalid value bit size: {self.value_bit_size}. "
294+
"Must be a multiple of 8"
295+
)
296+
297+
if self.value_bit_size > self.data_byte_size * 8:
298+
raise ValueError("Value byte size exceeds data size")
299+
288300
@parse_type_str("bytes")
289301
def from_type_str(cls, abi_type: BasicType, registry: ABIRegistry) -> bytes:
290302
subencoder_cls = cls.get_subencoder_class()
@@ -298,6 +310,7 @@ def from_type_str(cls, abi_type: BasicType, registry: ABIRegistry) -> bytes:
298310
# Unexpected keyword argument "value_bit_size" for "__call__" of "BaseEncoder"
299311
return cls( # type: ignore
300312
subencoder,
313+
value_bit_size=abi_type.sub * 8,
301314
data_byte_size=abi_type.sub,
302315
)
303316

0 commit comments

Comments
 (0)