jgarzik / python-bitcoinrpc

Python interface to bitcoin's JSON-RPC API
GNU Lesser General Public License v2.1
642 stars 307 forks source link

decimal.InvalidOperation exception #92

Open gallizoltan opened 5 years ago

gallizoltan commented 5 years ago

I'm regularly calling getnetworkhashps(-1) to keep track of the network's estimated hash power for the current difficulty adjustment period. At the latest difficulty adjustment, around block 566,496 it temporarily peeked at "3.385206289794399E+20". Function EncodeDecimal couldn't handle numbers this big, and raised an exception:

Traceback (most recent call last):
  File "my_btc_test.py", line 19, in <module>
    networkhashps = rpc_connection.getnetworkhashps(-1)
  File "bitcoinrpc/authproxy.py", line 139, in __call__
    response = self._get_response()
  File "bitcoinrpc/authproxy.py", line 197, in _get_response
    log.debug("<-%s- %s"%(response["id"], json.dumps(response["result"], default=EncodeDecimal)))
  File "/usr/lib/python3.5/json/__init__.py", line 237, in dumps
    **kw).encode(obj)
  File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
    return _iterencode(o, 0)
  File "bitcoinrpc/authproxy.py", line 77, in EncodeDecimal
    return float(round(o, 8))
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

I've reproduced the error with a simple example. The following code throws the same exception demonstrating the problem:

#!/usr/bin/python3
import decimal
o = decimal.Decimal("3.385206289794399E+20")
print(float(round(o, 8)))

A possible solution

Please consider merging @luke-jr's #63, that solves the issue.

qishuo commented 8 months ago

decimal.getcontext().prec = 28