ApeWorX / ape-foundry

Foundry network provider for the Ape Framework
https://www.apeworx.io/
Apache License 2.0
33 stars 13 forks source link

goerli-fork: (BlockNotFoundError) Block with ID '0' not found [APE-1589] #78

Closed gcc2ge closed 9 months ago

gcc2ge commented 11 months ago

Environment information

$ ape --version
# 0.6.26

$ ape plugins list
# Installed Plugins
  alchemy      0.6.5
  etherscan    0.6.10
  fantom       0.6.3
  foundry      0.6.19
  notebook     0.6.0
  solidity     0.6.6
  vyper        0.6.13

ape-config.yaml

name: ape-learn

default_ecosystem: ethereum

plugins:
  - solidity
  - foundry
  - etherscan
  - alchemy

ethereum:
  default_network: goerli-fork
  goerli_fork:
    default_provider: foundry
    default_transaction_type: 0
    transaction_acceptance_timeout: 600 # 5 minutes

geth:
  ethereum:
    goerli:
      uri:  https://eth-goerli.g.alchemy.com/v2/{KEY}

foundry:
  fork:
    ethereum:
      goerli:
        upstream_provider: geth
        block_number: 10165884

What went wrong?

Please include information like: command: ape console --network ethereum:goerli-fork:foundry

error output:

ERROR (ape-learn): (BlockNotFoundError) Block with ID '0' not found. Reason: The field extraData is 117 bytes, but should be 32. It is quite likely that you are connected to a POA chain. Refer to http://web3py.readthedocs.io/en/stable/middleware.html#proof-of-authority for more details. The full extraData is: HexBytes('0x22466c6578692069732061207468696e6722202d204166726900000000000000e0a2bd4258d2768837baa26a28fe71dc079f84c70000000000000000000000000000000000000000000000000000000000000000000000000000000000000

How can it be fixed?

Fill this in if you have ideas on how the bug could be fixed.

fix:

ape_foundry/provider.py", line 952, in connect

with self.forked_network.use_upstream_provider() as upstream_provider:
    try:
        upstream_genesis_block_hash = upstream_provider.get_block('latest').hash # change get_block(0) to get_block('latest')
    except ExtraDataLengthError as err:
        if isinstance(upstream_provider, Web3Provider):
            logger.error(
                f"Upstream provider '{upstream_provider.name}' missing Geth PoA middleware."
            )
            upstream_provider.web3.middleware_onion.inject(geth_poa_middleware, layer=0)
            upstream_genesis_block_hash = upstream_provider.get_block(0).hash
        else:
            raise FoundryProviderError(f"Unable to get genesis block: {err}.") from err
antazoey commented 9 months ago

We want to check for 0 because if the chain ever was PoA, this middleware is needed.

The actual issue is the get_block fails in a different error than the one we want to catch to add the middlware. Note: you wouldn't be hitting this issue if you were using the ape-alchemy plugin instead of using geth to connect to Alchemy. Nonetheless, I can fix the bug!

antazoey commented 9 months ago

the bug is actually in the upstream provider, the core Geth plugin so that fix-PR is against core. Thank you for raising this issue btw!