Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decimal.InvalidOperation exception #92

Open
gallizoltan opened this issue Mar 10, 2019 · 1 comment
Open

decimal.InvalidOperation exception #92

gallizoltan opened this issue Mar 10, 2019 · 1 comment

Comments

@gallizoltan
Copy link

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
Copy link

qishuo commented Feb 28, 2024

decimal.getcontext().prec = 28

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants