fiatjaf / bitcoin-requests

Simplest Bitcoin Core RPC interface.
Other
2 stars 3 forks source link

Json Decoding Issue #1

Closed blockchainOSINT closed 3 years ago

blockchainOSINT commented 3 years ago

Having some trouble getting calls to work properly. Any idea what I'm doing wrong? Bitcoin Core is running in background.

>>> username = '<my_username>'
>>> password = '<password>'
>>> rpc = BitcoinRPC('http://127.0.0.1:8332', username, password)
>>> rpc
<bitcoin_requests.bitcoin.BitcoinRPC object at 0x0000029F23B13B20>
>>> blockhash='000000000000000082ccf8f1557c5d40b21edabb18d2d691cfbf87118bac7254'
>>> block = rpc.getblock(blockhash)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\bitcoin_requests\bitcoin.py", line 30, in call
    resp = json.loads(v, parse_float=decimal.Decimal)
  File "C:\Python39\lib\json\__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "C:\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
fiatjaf commented 3 years ago

Does that call work if you do it on bitcoin-cli?

blockchainOSINT commented 3 years ago

@fiatjaf yes, bitcoin-cli getblock 000000000000000082ccf8f1557c5d40b21edabb18d2d691cfbf87118bac7254 2 works.

I should mention that I am currently syncing a node from scratch, it's about ~45% finished (I am testing RPCs on blocks I already have, and the CLI commands work on these blocks) however I have read elsewhere that when doing a full sync the RPC may get 'overloaded' and not function properly. Something to do with bitcoind rpcworkqueue setting.

Also wondering, if I can get this working, how to pass the additional parameter verbosity=2 in the getblockcall? Assuming it would just be rpc.getblock(blockhash, 2)?

Thanks for reply.

fiatjaf commented 3 years ago

So I couldn't check but I think that if you call getblock without parameters it returns the raw block in hex format. You must use rpc.getblock(blockhash, 2) as you mentioned to get JSON.

I'm not sure if this library should handle the raw hex block. Maybe it should accept it and return bytes.

blockchainOSINT commented 3 years ago

I'm getting the same JSONDecodeError with all calls. I also tried the syntax rpc.call("method", "param") and still got the JSONDecodeError.

>>> hash = rpc.getblockhash(10000)

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\bitcoin_requests\bitcoin.py", line 30, in call resp = json.loads(v, parse_float=decimal.Decimal) File "C:\Python39\lib\json\__init__.py", line 359, in loads return cls(**kw).decode(s) File "C:\Python39\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python39\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

>>> hash = rpc.call("getblockhash", "10000")

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\bitcoin_requests\bitcoin.py", line 30, in call resp = json.loads(v, parse_float=decimal.Decimal) File "C:\Python39\lib\json\__init__.py", line 359, in loads return cls(**kw).decode(s) File "C:\Python39\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python39\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

>>> block = rpc.getblock(blockhash, 2)

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\bitcoin_requests\bitcoin.py", line 30, in call resp = json.loads(v, parse_float=decimal.Decimal) File "C:\Python39\lib\json\__init__.py", line 359, in loads return cls(**kw).decode(s) File "C:\Python39\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python39\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

fiatjaf commented 3 years ago

Can you call rpc.getblockchaininfo()?

Maybe there's something wrong with your RPC connection. We need better errors to debug and warn users of these.

blockchainOSINT commented 3 years ago

No, rpc.getblockchaininfo() returns the same JSONDecodeError. My RPC connection looks okay, at least it's is returning a valid object. I tried changing the port just to see if it would return a connection error and it did. When I use the correct port, it starts returning the JSON errors, so I think the RPC connection is working and it's a code issue probably, but not sure why.

>>> username = '<my_username>'
>>> password = '<password>'
>>> rpc = BitcoinRPC('http://127.0.0.1:8332', username, password)
>>> rpc
<bitcoin_requests.bitcoin.BitcoinRPC object at 0x0000029F23B13B20>
fiatjaf commented 3 years ago

There are no checkes on the actual validity of your parameters when creating the BitcoinRPC object. It could be that your password is wrong, but you probably didn't commit that mistake so I don't know what to say.

blockchainOSINT commented 3 years ago

Yeah I double-checked all that stuff. I'll keep tinkering with it, will post if I can fix. Thx for replies.

blockchainOSINT commented 3 years ago

@fiatjaf It was a simple mistake on my part. I was using the hashed password generated by rpcauth.py when initiating rpc = BitcoinRPC('http://127.0.0.1:8332', username, password). When I changed it to the plaintext password, it worked. Live and learn. Maybe this thread will prevent someone else from making the same mistake.