ApeWorX / ape

The smart contract development tool for Pythonistas, Data Scientists, and Security Professionals
https://apeworx.io
Apache License 2.0
866 stars 133 forks source link

Failed to a decode NodeOperatorAdded log (string field?) [APE-1289] #1604

Closed arwer13 closed 11 months ago

arwer13 commented 1 year ago

Environment information

ape --version
0.6.15

ape plugins list
Installed Plugins:
  infura       0.6.3
  etherscan    0.6.8
  solidity     0.6.7
  ganache      0.6.8
  hardhat      0.6.12
  foundry      0.6.14

What went wrong?

Failed to decode a log with known ABI. See event number 136 in https://etherscan.io/tx/0xa4629245311d93a11cedb9143d8b7530057685b4b568a026bac194e162002c13#eventlog

from ape import chain
from ethpm_types.abi import EventABI, EventABIType

operator_added_log = {'address': '0x55032650b14df07b85bF18A3a3eC8E0Af2e028d5',
 'blockHash': HexBytes('0x5f4a2d91be2fbd7c740e279a8061c36d65214e6d3e993e61ebf8f1448c7656b3'),
 'blockNumber': 17885248,
 'data': HexBytes('0x000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000800000000000000000000000005a8b929edbf3ce44526465dd2087ec7efb59a5610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b4c61756e63686e6f646573'),
 'logIndex': 10,
 'removed': False,
 'topics': [HexBytes('0xc52ec0ad7872dae440d886040390c13677df7bf3cca136d8d81e5e5e7dd62ff1')],
 'transactionHash': HexBytes('0xa328d01f475108ee2ae271475491a48dadb68ebf6c749277d78336206939f63e'),
 'transactionIndex': 0}

event_abi = EventABI(type='event', name='NodeOperatorAdded', inputs=[EventABIType(name='nodeOperatorId', type='uint256', components=None, internalType=None, indexed=False), EventABIType(name='name', type='string', components=None, internalType=None, indexed=False), EventABIType(name='rewardAddress', type='address', components=None, internalType=None, indexed=False), EventABIType(name='stakingLimit', type='uint64', components=None, internalType=None, indexed=False)], anonymous=False)

ContractLogContainer(chain.provider.network.ecosystem.decode_logs([operator_added_log], event_abi))
DEBUG: failed to decode log data for {'address': '0x55032650b14df07b85bF18A3a3eC8E0Af2e028d5', 'blockHash': HexBytes('0x5f4a2d91be2fbd7c740e279a8061c36d65214e6d3e993e61ebf8f1448c7656b3'), 'blockNumber': 17885248, 'data': HexBytes('0x000000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000800000000000000000000000005a8b929edbf3ce44526465dd2087ec7efb59a5610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b4c61756e63686e6f646573'), 'logIndex': 10, 'removed': False, 'topics': [HexBytes('0xc52ec0ad7872dae440d886040390c13677df7bf3cca136d8d81e5e5e7dd62ff1')], 'transactionHash': HexBytes('0xa328d01f475108ee2ae271475491a48dadb68ebf6c749277d78336206939f63e'), 'transactionIndex': 0}

Traceback (most recent call last):
  File "/Users/arwer/projects/ape/src/ape_ethereum/ecosystem.py", line 655, in decode_logs
    event_arguments = abi.decode(topics, log["data"])
  File "/Users/arwer/projects/ape/src/ape/utils/abi.py", line 340, in decode
    data_values = decode(data_abi_types, hex_data)
  File "/Users/arwer/Library/Caches/pypoetry/virtualenvs/ape-playground-ObaGhRsh-py3.10/lib/python3.10/site-packages/eth_abi/codec.py", line 149, in decode
    return decoder(stream)
  ...
eth_abi.exceptions.InsufficientDataBytes: Tried to read 32 bytes.  Only got 11 bytes

Maybe the problem happens when there is a string event field.

How can it be fixed?

I'd like to mention a few more details. The issue was discovered after I found ReceiptAPI.events lack of the NodeOperatorAdded event. At first the events list was quite misleading, because I expected to see all the logs there. And to locate the problem I had to dive in the Ape sources.

I'd propose to consider:

  1. Keep all logs in ReceiptAPI.events, where the ones failed to decode are represented as raw stubs
  2. Print the message "DEBUG: failed to decode log data for ..." as WARNING.
fubuloubu commented 1 year ago

Thanks for the detailed bug report!

I'd propose to consider:

  1. Keep all logs in ReceiptAPI.events, where the ones failed to decode are represented as raw stubs
  2. Print the message "DEBUG: failed to decode log data for ..." as WARNING.

These are great suggestions to make it easier to debug in the future, really appreciate that!

antazoey commented 11 months ago

I created the same exact event type locally, made a tx with pretty much the same-ish data, analyzed both the data from the my local test and the data on the Etherscan linked event and I found the one I had locally had the correct number of trailing zeroes whereas the one on Etherscan is truncated.

So I noticed the eth_abi decoders have a kwarg for strict which is defaulted to True that requires the correct trailing zeroes and such. So what I did in my linked PR (along with your other suggestions) is try again with strict=False with a unique warning in this situation to mention that this was required.