pybitcash/bitcash

Fallback to bitcore.io fails when trest.bitcoin.com is returning error

Closed this issue · 12 comments

Boundary conditions:

  • trest.bitcoin.com when queried for the balance of an address ( and some other APIs) returns a json contaning a single key named 'error' with a string value containing a description of the error (seems their system is having some problem in reaching their local fullnode)
  • bitcore.io api is up and running

Test I made (and not made), both from my laptop with crappy connection both from a decent server:

  • tested status of both API
  • changed the timeout: sometimes with a way high timeout (10) it works, HOWEVER this new behavior appeared only when trest.bitcoin.com went completely offline and stopped returning a json with the error.
  • not tested if this library is correctly handling the slightly different way the address is to be formatted (remove bchtest:) when contacting the two APIs, seems it's correctly handled in the library code but I didn't wiresharked the outgoing requests from the library.

Code:

#check the balance given a private key - called as python3 1generatePkey.py YourL0n4PriwaTeKeyiNwifFormaTheRe

from bitcash import wif_to_key
import sys
from bitcash import set_service_timeout
set_service_timeout(8)

arg=sys.argv[1]

key = wif_to_key(arg)

address=key.address


print ("private Key is:")
print(key)
print ("address is:")
print(address)
print("balance is")
print(key.get_balance('bch'))

Error returned:

private Key is:
<PrivateKeyTestnet: bchtest:myawesomekeyfilledwithworthlesstesenetcoins>
address is:
bchtest:qrgmdeufa7fxsrq32r9z3e3c5urp2tj6dctjn39lsg
balance is
Traceback (most recent call last):
  File "2ImportAndCkeckBalance.py", line 20, in <module>
    print(key.get_balance('bch'))
  File "/usr/local/lib/python3.6/dist-packages/bitcash/wallet.py", line 484, in get_balance
    self.unspents[:] = NetworkAPI.get_unspent_testnet(self.address)
  File "/usr/local/lib/python3.6/dist-packages/bitcash/network/services.py", line 567, in get_unspent_testnet
    return api_call(address)
  File "/usr/local/lib/python3.6/dist-packages/bitcash/network/services.py", line 199, in get_unspent_testnet
    r.raise_for_status()  # pragma: no cover
  File "/home/ludovico/.local/lib/python3.6/site-packages/requests/models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 523 Server Error:  for url: https://trest.bitcoin.com/v2/address/utxo/bchtest:qrgmdeufa7fxsrq32r9z3e3c5urp2tj6dctjn39lsg

Personal speculation:

  • When the first API instead of returning an http error returns a json containing the error the library raises an error without fallingback?
  • Timeout refers to the whole operation and not to main and fallback API request separately (this is probably intended behaviour)

It seems that the fallback does not work for key.get_balance('bch')

A temporary way to fix this:

key.get_balance() / 100000000

The get_balance() method's fallback works.

I will investigate the issue with the 'bch' argument.

Edit: get_balance() worked because trest worked for a second for whatever reason, but indeed fallback does not work.

Doesn't work for me.
The few times it works it's the few times the trest.bitcoin.com API works.
Seems to me that the lib fails to call the second API.

I just learned that trest.bitcoin.com was intentionally taken down as no longer maintained.

ouch! I imagine still that this bug propagates to the non- test version on the library tho

I forked you to do some testing. I'll add a different differnet API provider and submit pull request to your repo. May you look in why the fallback to seconday API was failing? Thanks :-)

@selfdrivingmenee Which provider are you looking at? I've found this one, which should have testnet support: https://api.fullstack.cash/docs/

@selfdrivingmenee Which provider are you looking at? I've found this one, which should have testnet support: https://api.fullstack.cash/docs/

I've also been looking at this one, very similar to rest.bitcoin.com and even fast.
My only concern is that the free tier has very low API limits: only 3 requests per minute allowed 🤨

But would be good to include it as a fallback!

I swapped the current fallback API with the main one and things got back in working order.
As a fallback API I'm testing (not in the lib) cryptoapis.io , they require a (free) API key but being (also, with "paid" key) commercial provider they offer an SLA so I think it's interesting to have them supported.

Side note: main chain (and even some nodes on the testnet) accepts max one OP_return per each transaction so the splitting system for long messages should be changed. I'm evaluating the various tx chaining solutions, should I open a separate issue?

@selfdrivingmenee did you start working on a PR already?

If not, I will start working on fixing this.

Not working on a PR yet. As my 2c goes, I'd first take advantage of the non-operative bitcoin.com trest to fix the fallback system and then add another provider :-)
I'll update you on how my search for a provider goes

@selfdrivingmenee could you test #65 on your end?

I tested it in a few different ways:

  • editing the main endpoint's URL to an incorrect URL (ex: https://trest.bitcoin.com/1nc0rrect/)
  • blacklisting trest.bitcoin.com in my Hosts file
  • blacklisting main endpoint domain name in my router's DNS

Edit: Travis-CI and local tests passed

Working on my side, thankyou!!!
I did my testing, I'd suggest cryptoapis.io as the backup one introducing an initialization variable "API KEY". They work great and also allow for a very easy commercial integration.