Beem: Account.history() fails with float division by zero exception

in #utopian-io7 years ago

Project Information

A GitHub issue was created: https://github.com/holgern/beem/issues/34
A PR to fix the bug has been accepted by the PO: https://github.com/holgern/beem/pull/35

Expected behavior

Account.history(start, stop) with start and stop being datetime instances should return all account operations between the given timestamps

Actual behavior

Account.history(start, stop) sometimes fails with a ZeroDivisionError exception:

Traceback (most recent call last):
  File "history.py", line 15, in <module>
    for op in a.history(start, stop):
  File "/usr/local/lib/python3.6/site-packages/beem/account.py", line 1566, in history
    op_est = self.estimate_virtual_op_num(start, stop_diff=1)
  File "/usr/local/lib/python3.6/site-packages/beem/account.py", line 1333, in estimate_virtual_op_num
    op_diff = (blocktime - formatTimeString(trx["timestamp"])).total_seconds() / factor
ZeroDivisionError: float division by zero

How to reproduce

What actually fails is the op number lookup for a given timestamp via Account.estimate_virtual_op_num(). Here's a minimum example to reproduce the problem:

from beem.account import Account
from datetime import datetime, timedelta
from beem.utils import addTzInfo
a = Account("berniesanders")
start = addTzInfo(datetime(2017, 11, 16, 23, 23, 11))
print(a.estimate_virtual_op_num(start))

Output:

# python bug_divbyzero.py 
Traceback (most recent call last):
  File "bug_divbyzero.py", line 9, in <module>
    print(a.estimate_virtual_op_num(start))
  File "/usr/local/lib/python3.6/site-packages/beem/account.py", line 1333, in estimate_virtual_op_num
    op_diff = (blocktime - formatTimeString(trx["timestamp"])).total_seconds() / factor
ZeroDivisionError: float division by zero

The issue was caused by the fact that an account can have several ops in the same block. op_start_last[0][1]["timestamp"]) and trx["timestamp"] can be identical in this case. This makes factor = 0.

Another potential issue about the factor variable used without proper initialization was found close by: if diff_op == 0, the factor assignment was skipped and factor was used later on with the value from the previous iteration or from the initial assignment outside the while loop.

Environment

# beempy --version
beempy, version 0.19.40
# python --version
Python 3.6.5

Fix

A PR has been provided to fix both the division-by-zero exception as well as the possibly wrongly initialized factor variable:
https://github.com/holgern/beem/pull/35

The PR was accepted by the PO and the fix is contained in release beem-0.19.41

GitHub Account

https://github.com/crokkon

Sort:  

Hi @stmdev, thanks for leaving us this contribution.

The error you described has been fixed, by you.
:). That's great of you. It was helpful, and would have been of great help to the PO, how you traced back the logic behind the exception that was caught.

But well, you helped fix it yourself. :)

The post has been reviewed accordingly. We look forward to your next report.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hey @stmdev
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

thanks for the post very useful information

Coin Marketplace

STEEM 0.12
TRX 0.34
JST 0.033
BTC 122349.27
ETH 4493.53
SBD 0.79