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

Failure connecting to geth [APE-815] #1379

Closed LinuxIsCool closed 1 year ago

LinuxIsCool commented 1 year ago

Environment information

$ ape --version
0.6.7

$ ape plugins list
  alchemy      0.6.1
  etherscan    0.6.3
$ cat ape-config.yaml

name: ape-query-examples

What went wrong?

I'm trying to connect to geth.

I initialize geth with the following command:

geth --syncmode light --http --http.api "eth,debug"

Then I try to connect to geth with ape:

ape console --network 'ethereum:mainnet:geth'

Resulting in the following:

Traceback (most recent call last):                                                                                                                                                            
  File "/home/ygg/.cache/pypoetry/virtualenvs/ape-query-examples-MbtC-2rg-py3.10/bin/ape", line 8, in <module>
    sys.exit(cli())
  File "/home/ygg/.cache/pypoetry/virtualenvs/ape-query-examples-MbtC-2rg-py3.10/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/home/ygg/.cache/pypoetry/virtualenvs/ape-query-examples-MbtC-2rg-py3.10/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/ygg/.cache/pypoetry/virtualenvs/ape-query-examples-MbtC-2rg-py3.10/lib/python3.10/site-packages/ape/_cli.py", line 36, in invoke
    return super().invoke(ctx)
  File "/home/ygg/.cache/pypoetry/virtualenvs/ape-query-examples-MbtC-2rg-py3.10/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/ygg/.cache/pypoetry/virtualenvs/ape-query-examples-MbtC-2rg-py3.10/lib/python3.10/site-packages/ape/cli/commands.py", line 17, in invoke
    with networks.parse_network_choice(value):
  File "/home/ygg/.cache/pypoetry/virtualenvs/ape-query-examples-MbtC-2rg-py3.10/lib/python3.10/site-packages/ape/api/networks.py", line 544, in __enter__
    return self.push_provider()
  File "/home/ygg/.cache/pypoetry/virtualenvs/ape-query-examples-MbtC-2rg-py3.10/lib/python3.10/site-packages/ape/api/networks.py", line 552, in push_provider
    self._provider.connect()
  File "/home/ygg/.cache/pypoetry/virtualenvs/ape-query-examples-MbtC-2rg-py3.10/lib/python3.10/site-packages/ape_geth/provider.py", line 494, in connect
    if not self.is_connected:
  File "/home/ygg/.cache/pypoetry/virtualenvs/ape-query-examples-MbtC-2rg-py3.10/lib/python3.10/site-packages/ape/api/providers.py", line 657, in is_connected
    return run_until_complete(self._web3.is_connected())
  File "/home/ygg/.cache/pypoetry/virtualenvs/ape-query-examples-MbtC-2rg-py3.10/lib/python3.10/site-packages/web3/main.py", line 445, in is_connected
    return self.provider.is_connected()
  File "/home/ygg/.cache/pypoetry/virtualenvs/ape-query-examples-MbtC-2rg-py3.10/lib/python3.10/site-packages/web3/providers/base.py", line 115, in is_connected
    assert "error" not in response
AssertionError

But web3 connects no problem:

In [1]: from web3 import Web3

In [2]: w = Web3()

In [3]: w.is_connected()
Out[3]: True

In [4]: w.client_version
Out[4]: 'Geth/v1.11.5-stable-a38f4108/linux-amd64/go1.20.2'

In [5]: 

How can it be fixed?

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

antazoey commented 1 year ago

I was able to reproduce and am investigating.

To add more info, the part where it's like assert "error" not in response (for some reason doesnt show us the error), but the error it is giving is: {'jsonrpc': '2.0', 'id': 0, 'error': {'code': -32601, 'message': 'the method web3_clientVersion does not exist/is not available'}}

Odd indeed!

Also thanks for reporting.

antazoey commented 1 year ago

For some reason, I am also getting the same issue when I try:

In [1]: from web3 import Web3

In [2]: w = Web3()

In [3]: w.is_connected()
-> assert "error" not in response

Edit: maybe was a fluke or no longer is happening, but when it does work, I notice it is using IPC instead of HTTP, which is interesting:

>>> w.provider
<web3.providers.auto.AutoProvider object at 0x10bce5c40>
>>> w.provider._active_provider
<web3.providers.ipc.IPCProvider object at 0x10bce5a90>
antazoey commented 1 year ago

OK @LinuxIsCool - to use HTTP, you also need the web3 API, so you can start your geth process like this:

geth --syncmode light --http --http.api "eth,debug,web3"

with the additional web3 API specified.

I found that out by looking at this PR: https://github.com/eth-educators/eth-docker/pull/226/files

I can look to see if we can use IPC when certain APIs are missing however.

antazoey commented 1 year ago

In [4]: w.client_version Out[4]: 'Geth/v1.11.5-stable-a38f4108/linux-amd64/go1.20.2'

Are you actually connected to mainnet here? what is your web3.eth.chain_id?

It will help to know what your main goal. Are you trying to use IPC? Are you trying to use eth mainnet or a local chain? I think you need to change how you are running geth to get the best support from ape. However, we can add support for certain things but it would helpful to know the full use-case.

LinuxIsCool commented 1 year ago

Hi @unparalleled-js thanks for your responses!

I am trying to connect to local mainnet.

Either IPC or HTTP should be fine, although I was kind of leaning towards IPC as I read somewhere that it should be the fastest.

I'll try running geth with the web3 argument. And I'll also report back the web3 chain_id.

LinuxIsCool commented 1 year ago

So, running everything as I originally did, web3.eth.chain_id is 1.

LinuxIsCool commented 1 year ago

geth --syncmode light --http --http.api "eth,debug,web3"

This worked thank you :pray:

antazoey commented 1 year ago

... although I was kind of leaning towards IPC as I read somewhere that it should be the fastest.

Thanks to your issue, we are now adding support for IPC! It will use it by default if it can, otherwise it will resort back to the normal HTTP.

https://github.com/ApeWorX/ape/pull/1384