Skip to content

decimal.InvalidOperation exception #92

@gallizoltan

Description

@gallizoltan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions