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

add abi.decode strictness flag and catch InsufficientDataBytes error … #3256

Open
wants to merge 8 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/web3.contract.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,20 @@ For example:
>>> assert processed_logs == ()
True

In the case of an ``InsufficientDataBytes`` error, it may be possible to decode the
log by setting the ``strict_bytes_type_checking`` flag to ``False`` on the Web3
instance before decoding.

.. code-block:: python

>>> w3.strict_bytes_type_checking = False


This will attempt to decode the log by reading only the data size specified in the
ABI. This is useful when the log data is not padded to the correct size, but the data
is still valid. Because any data past the specified size is ignored, this may result
in data loss. It is not recommended for general use, but may be useful in some cases.

.. py:method:: ContractEvents.myEvent(*args, **kwargs).process_log(log)

Similar to process_receipt_, but only processes one log at a time, instead of a whole transaction receipt.
Expand Down
1 change: 1 addition & 0 deletions newsfragments/3256.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Catch ``InsufficientDataBytes`` errors and respect ``w3.strict_bytes_type_checking`` flag in log receipt processing
52 changes: 52 additions & 0 deletions tests/core/contracts/test_extracting_event_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import copy
import pytest
import re

from eth_abi.exceptions import (
InsufficientDataBytes,
)
from eth_utils import (
is_same_address,
)
Expand Down Expand Up @@ -1180,6 +1184,54 @@ def test_receipt_processing_with_no_flag(indexed_event_contract, dup_txn_receipt
assert len(returned_log) == 0


def test_receipt_processing_catches_insufficientdatabytes_error_by_default(
w3, emitter, wait_for_transaction
):
txn_hash = emitter.functions.logListArgs([b"13"], [b"54"]).transact()
txn_receipt = wait_for_transaction(w3, txn_hash)
event_instance = emitter.events.LogListArgs()

# web3 doesn't generate logs with non-standard lengths, so we have to do it manually
txn_receipt_dict = copy.deepcopy(txn_receipt)
txn_receipt_dict["logs"][0] = dict(txn_receipt_dict["logs"][0])
txn_receipt_dict["logs"][0]["data"] = txn_receipt_dict["logs"][0]["data"][:-8]

# WARN is default
assert len(event_instance.process_receipt(txn_receipt_dict)) == 0
assert len(event_instance.process_receipt(txn_receipt_dict, errors=WARN)) == 0
assert len(event_instance.process_receipt(txn_receipt_dict, errors=DISCARD)) == 0

# IGNORE includes the InsufficientDataBytes error in the log
assert len(event_instance.process_receipt(txn_receipt_dict, errors=IGNORE)) == 1

# STRICT raises an error to be caught
with pytest.raises(InsufficientDataBytes):
returned_log = event_instance.process_receipt(txn_receipt_dict, errors=STRICT)
assert len(returned_log) == 0


def test_receipt_processing_respects_w3_strict_bytes_type_checking_flag(
w3, emitter, wait_for_transaction
):
txn_hash = emitter.functions.logListArgs([b"13"], [b"54"]).transact()
txn_receipt = wait_for_transaction(w3, txn_hash)
event_instance = emitter.events.LogListArgs()

# web3 doesn't generate logs with non-standard lengths, so we have to do it manually
txn_receipt_dict = copy.deepcopy(txn_receipt)
txn_receipt_dict["logs"][0] = dict(txn_receipt_dict["logs"][0])
txn_receipt_dict["logs"][0]["data"] = txn_receipt_dict["logs"][0]["data"][:-4]

# STRICT raises an error to be caught
with pytest.raises(InsufficientDataBytes):
returned_log = event_instance.process_receipt(txn_receipt_dict, errors=STRICT)
assert len(returned_log) == 0

w3.strict_bytes_type_checking = False
returned_log = event_instance.process_receipt(txn_receipt_dict, errors=STRICT)
assert len(returned_log) == 1


def test_single_log_processing_with_errors(indexed_event_contract, dup_txn_receipt):
event_instance = indexed_event_contract.events.LogSingleWithIndex()

Expand Down
8 changes: 6 additions & 2 deletions web3/_utils/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ def get_event_data(
abi_codec: ABICodec,
event_abi: ABIEvent,
log_entry: LogReceipt,
abi_decode_strict: bool = True,
) -> EventData:
"""
Given an event ABI and a log entry for that event, return the decoded
Expand Down Expand Up @@ -266,8 +267,11 @@ def get_event_data(
"The following argument names are duplicated "
f"between event inputs: '{', '.join(duplicate_names)}'"
)
breakpoint()

decoded_log_data = abi_codec.decode(log_data_types, log_data)
decoded_log_data = abi_codec.decode(
log_data_types, log_data, strict=abi_decode_strict
)
normalized_log_data = map_abi_data(
BASE_RETURN_NORMALIZERS, log_data_types, decoded_log_data
)
Expand All @@ -277,7 +281,7 @@ def get_event_data(
)

decoded_topic_data = [
abi_codec.decode([topic_type], topic_data)[0]
abi_codec.decode([topic_type], topic_data, strict=abi_decode_strict)[0]
for topic_type, topic_data in zip(log_topic_types, log_topics_bytes)
]
normalized_topic_data = map_abi_data(
Expand Down
15 changes: 12 additions & 3 deletions web3/contract/async_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ async def get_logs(

# convert raw binary data to Python proxy objects as described by ABI:
all_event_logs = tuple(
get_event_data(self.w3.codec, event_abi, entry) for entry in logs
get_event_data(
self.w3.codec, event_abi, entry, self.w3.strict_bytes_type_checking
)
for entry in logs
)
filtered_logs = self._process_get_logs_argument_filters(
event_abi,
Expand Down Expand Up @@ -223,7 +226,9 @@ async def create_filter(
)
log_filter = await filter_builder.deploy(self.w3)
log_filter.log_entry_formatter = get_event_data(
self.w3.codec, self._get_event_abi()
self.w3.codec,
self._get_event_abi(),
abi_decode_strict=self.w3.strict_bytes_type_checking,
)
log_filter.builder = filter_builder

Expand All @@ -234,7 +239,11 @@ def build_filter(self) -> AsyncEventFilterBuilder:
builder = AsyncEventFilterBuilder(
self._get_event_abi(),
self.w3.codec,
formatter=get_event_data(self.w3.codec, self._get_event_abi()),
formatter=get_event_data(
self.w3.codec,
self._get_event_abi(),
abi_decode_strict=self.w3.strict_bytes_type_checking,
),
)
builder.address = self.address
return builder
Expand Down
33 changes: 28 additions & 5 deletions web3/contract/base_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
)
import warnings

from eth_abi.exceptions import (
InsufficientDataBytes,
)
from eth_typing import (
Address,
ChecksumAddress,
Expand Down Expand Up @@ -156,13 +159,17 @@ def _get_event_abi(cls) -> ABIEvent:

@combomethod
def process_receipt(
self, txn_receipt: TxReceipt, errors: EventLogErrorFlags = WARN
self,
txn_receipt: TxReceipt,
errors: EventLogErrorFlags = WARN,
) -> Iterable[EventData]:
return self._parse_logs(txn_receipt, errors)

@to_tuple
def _parse_logs(
self, txn_receipt: TxReceipt, errors: EventLogErrorFlags
self,
txn_receipt: TxReceipt,
errors: EventLogErrorFlags,
) -> Iterable[EventData]:
try:
errors.name
Expand All @@ -173,8 +180,19 @@ def _parse_logs(

for log in txn_receipt["logs"]:
try:
rich_log = get_event_data(self.w3.codec, self.abi, log)
except (MismatchedABI, LogTopicError, InvalidEventABI, TypeError) as e:
rich_log = get_event_data(
self.w3.codec,
self.abi,
log,
abi_decode_strict=self.w3.strict_bytes_type_checking,
)
except (
MismatchedABI,
LogTopicError,
InvalidEventABI,
TypeError,
InsufficientDataBytes,
) as e:
if errors == DISCARD:
continue
elif errors == IGNORE:
Expand All @@ -197,7 +215,12 @@ def _parse_logs(

@combomethod
def process_log(self, log: HexStr) -> EventData:
return get_event_data(self.w3.codec, self.abi, log)
return get_event_data(
self.w3.codec,
self.abi,
log,
abi_decode_strict=self.w3.strict_bytes_type_checking,
)

@combomethod
def _get_event_filter_params(
Expand Down
18 changes: 15 additions & 3 deletions web3/contract/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,13 @@ def get_logs(

# convert raw binary data to Python proxy objects as described by ABI:
all_event_logs = tuple(
get_event_data(self.w3.codec, event_abi, entry) for entry in logs
get_event_data(
self.w3.codec,
event_abi,
entry,
abi_decode_strict=self.w3.strict_bytes_type_checking,
)
for entry in logs
)
filtered_logs = self._process_get_logs_argument_filters(
event_abi,
Expand Down Expand Up @@ -224,7 +230,9 @@ def create_filter(
)
log_filter = filter_builder.deploy(self.w3)
log_filter.log_entry_formatter = get_event_data(
self.w3.codec, self._get_event_abi()
self.w3.codec,
self._get_event_abi(),
abi_decode_strict=self.w3.strict_bytes_type_checking,
)
log_filter.builder = filter_builder

Expand All @@ -235,7 +243,11 @@ def build_filter(self) -> EventFilterBuilder:
builder = EventFilterBuilder(
self._get_event_abi(),
self.w3.codec,
formatter=get_event_data(self.w3.codec, self._get_event_abi()),
formatter=get_event_data(
self.w3.codec,
self._get_event_abi(),
abi_decode_strict=self.w3.strict_bytes_type_checking,
),
)
builder.address = self.address
return builder
Expand Down