pybitcash / bitcash

BitCash: Python Bitcoin Cash Library (fork of ofek's Bit)
https://bitcash.dev
MIT License
97 stars 39 forks source link

ChaingraphAPI issue #146

Open cromachina opened 1 month ago

cromachina commented 1 month ago

An error is thrown when trying to extract data from a ChaingraphAPI response, seemingly. Minimal example:

import bitcash
k = bitcash.PrivateKeyTestnet()
k.get_balance()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/cro/aux/Projects/crypto-bot/venv/lib/python3.12/site-packages/bitcash/wallet.py", line 216, in get_balance
    _ = self.get_unspents()
        ^^^^^^^^^^^^^^^^^^^
  File "/home/cro/aux/Projects/crypto-bot/venv/lib/python3.12/site-packages/bitcash/wallet.py", line 234, in get_unspents
    self.unspents[:] = NetworkAPI.get_unspent(
                       ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cro/aux/Projects/crypto-bot/venv/lib/python3.12/site-packages/bitcash/network/services.py", line 247, in get_unspent
    for endpoint in get_sanitized_endpoints_for(network):
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cro/aux/Projects/crypto-bot/venv/lib/python3.12/site-packages/bitcash/utils.py", line 97, in _wrapped
    return_value = cache_fn(*args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cro/aux/Projects/crypto-bot/venv/lib/python3.12/site-packages/bitcash/utils.py", line 91, in cache_fn
    value = fn(*args, **kwargs)
            ^^^^^^^^^^^^^^^^^^^
  File "/home/cro/aux/Projects/crypto-bot/venv/lib/python3.12/site-packages/bitcash/network/services.py", line 126, in get_sanitized_endpoints_for
    endpoints_blockheight[i] = endpoint.get_blockheight(timeout=DEFAULT_TIMEOUT)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/cro/aux/Projects/crypto-bot/venv/lib/python3.12/site-packages/bitcash/network/APIs/ChaingraphAPI.py", line 77, in get_blockheight
    blockheight = int(json["data"]["block"][0]["height"])
                      ~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
merc1er commented 1 month ago

It looks like the chaingraph testnet instance returns invalid data... I am getting various errors (mainly ConnectionResetError: [Errno 104] Connection reset by peer) when running your code snippet.

Sadly this is the single main recurring issue that BitCash has been dealing with pretty much forever: none of the third party services used to interact with the blockchain are reliable.

However, the mainnet instance seems to be working fine:

import bitcash
k = bitcash.PrivateKey()
k.get_balance()

# Returns '0'
merc1er commented 1 month ago

FYI @cromachina here is the response from ChainGraph when querrying the block height:

Request:

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"query": "query GetBlockheight($node: String!) { block(limit: 1, order_by: { height: desc }, where: { accepted_by: { node: { name: { _like: $node } } } }) { height } }", "variables": { "node": "%testnet" }}' \
  https://demo.chaingraph.cash/v1/graphql

Response:

{
  "errors": [
    {
      "extensions": {
        "code": "unexpected",
        "path": "$"
      },
      "message": "database query error"
    }
  ]
}

and the worst thing is it's a 200 status code... Again, it looks like this is specific to testnet.

So that doesn't seem to be an issue with BitCash, though it is breaking BitCash as a result. I suggest raising an issue with Chaingraph directly so hopefully they can fix it: https://github.com/bitauth/chaingraph

merc1er commented 1 month ago

Actually I'll leave this open even though it's an issue with Chaingraph, in case anyone is having the same problem.

Hopefully this is fixed soon 🤞🏻