Skip to content

Commit

Permalink
refactor: decode strict=False right away
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Dec 23, 2024
1 parent 59011c1 commit 66867d2
Showing 1 changed file with 13 additions and 27 deletions.
40 changes: 13 additions & 27 deletions src/ape/utils/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
NATSPEC_KEY_PATTERN = re.compile(r"(@\w+)")


class ApeUnsignedIntegerDecoder(UnsignedIntegerDecoder):
class _ApeUnsignedIntegerDecoder(UnsignedIntegerDecoder):
"""
This class exists because uint256 values when not-padded
always cause issues, even with strict=False.
"""

def read_data_from_stream(self, stream):
"""
Override to pad the value instead of raise an error.
Override to pad the value instead of raising an error.
"""
data_byte_size: int = self.data_byte_size # type: ignore
data = stream.read(data_byte_size)
Expand All @@ -39,7 +44,7 @@ def read_data_from_stream(self, stream):
registry.register(
BaseEquals("uint"),
UnsignedIntegerEncoder,
ApeUnsignedIntegerDecoder,
_ApeUnsignedIntegerDecoder,
label="uint",
)

Expand Down Expand Up @@ -458,42 +463,23 @@ def decode(
hex_value = decode_hex(topic_value)

try:
value = decode([abi_type], hex_value)[0]
value = decode([abi_type], hex_value, strict=False)[0]
except InsufficientDataBytes as err:
warning_message = f"Failed to decode log topic '{self.event_name}'."

# Try again with strict=False
try:
value = decode([abi_type], hex_value, strict=False)[0]
except Exception:
# Even with strict=False, we failed to decode.
# This should be a rare occasion, if it ever happens.
logger.warn_from_exception(err, warning_message)
if use_hex_on_fail:
if abi.name not in decoded:
# This allow logs to still be findable on the receipt.
decoded[abi.name] = hex_value
if use_hex_on_fail:
if abi.name not in decoded:
# This allow logs to still be findable on the receipt.
decoded[abi.name] = hex_value

else:
raise DecodingError(str(err)) from err

else:
# This happens when providers accidentally leave off trailing zeroes.
warning_message = (
f"{warning_message} "
"However, we are able to get a value using decode(strict=False)"
)
logger.warn_from_exception(err, warning_message)
decoded[abi.name] = self.decode_value(abi_type, value)

else:
# The data was formatted correctly and we were able to decode logs.
result = self.decode_value(abi_type, value)
decoded[abi.name] = result

data_abi_types = [abi.canonical_type for abi in self.data_abi_types]
hex_data = decode_hex(data) if isinstance(data, str) else data

try:
data_values = decode(data_abi_types, hex_data)
except InsufficientDataBytes as err:
Expand Down

0 comments on commit 66867d2

Please sign in to comment.