ApeWorX / ape

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

"size" field missing for Blocks from `newHeads` subscription method #1935

Closed fubuloubu closed 3 months ago

fubuloubu commented 7 months ago

Environment information

$ ape --version
0.7.9

$ ape plugins list
Installed Plugins
  alchemy      0.7.1
  etherscan    0.7.1
  infura       0.7.1
  optimism     0.7.2

What went wrong?

The field "size" is not available in the serialized data structure, at least for Optimism using an Infura provider (both up to date):

  File "bin/silverback", line 8, in <module>
    sys.exit(cli())
             ^^^^^
  File "site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/ape/cli/commands.py", line 96, in invoke
    return self._invoke(ctx, provider=provider)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/ape/cli/commands.py", line 133, in _invoke
    return ctx.invoke(self.callback or (lambda: None), **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/click/decorators.py", line 92, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/silverback/_cli.py", line 100, in run
    asyncio.run(runner.run())
  File "python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "site-packages/silverback/runner.py", line 208, in run
    await super().run()
  File "site-packages/silverback/runner.py", line 129, in run
    await asyncio.gather(*tasks)
  File "site-packages/silverback/runner.py", line 160, in _block_task
    block = self.provider.network.ecosystem.decode_block(hexbytes_dict(raw_block))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/ape_ethereum/ecosystem.py", line 540, in decode_block
    return Block.model_validate(data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/pydantic/main.py", line 503, in model_validate
    return cls.__pydantic_validator__.validate_python(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for Block
size
  Field required [type=missing, input_value={'baseFeePerGas': HexByte...cadc001622fb5e363b421')}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.5/v/missing

Unsure if it applies to other networks and/or provider types

How can it be fixed?

size could have a default value, or be set dynamically, but it is strange that this field does not appear when it is a mandatory field on the default BlockAPI structure (hinting at some other issue)

linear[bot] commented 7 months ago

APE-1680 "size" field missing for Blocks on certain networks

fubuloubu commented 7 months ago

Tried using Alchemy instead, no issue, so I think this is an Infura-specific thing

linear[bot] commented 7 months ago

APE-1681 "size" field missing for Blocks on certain networks

linear[bot] commented 7 months ago

APE-1682 "size" field missing for Blocks

fubuloubu commented 7 months ago

What's happening is that trying to parse a block received from a newHeads subscription using eth_subscribe is actually under-specified according to BlockAPI, so we need to add the missing field in order for it to parse. In the future, refactoring is needed to properly account for this situation and "enrich" the block when the missing field(s) are requested by the user