Beem: Cannot fetch account history of early created accounts, block number estimation gives negative values

Project Information

Expected behavior

blockchain.history() and blockchain.history_reverse() are expected to accept block numbers, virtual operation count numbers or dates for the start and stop parameters and only return account operations that happend within this range for any existing Steem account:

            :param int/datetime start: start number/date of transactions to
                return (*optional*)
            :param int/datetime stop: stop number/date of transactions to
                return (*optional*)
            :param bool use_block_num: if true, start and stop are block numbers,
                otherwise virtual operation count numbers.

Actual behavior

account.history() and account.history_reverse() with block numbers as start and stop parameters tries to fetch blocks with a negative block number if the account was created in the first days of Steem. The call fails with a beem.exceptions.BlockDoesNotExistsException. Using the corresponding time stamps for start and stop as datetime instances in the history()/history_reverse() calls gives the correct results.

How to reproduce

Picking an early created account (e.g. berniesanders: created on '2016-03-26 08:26:21+00:00') and a random transaction (e.g. Block 22487722):

#!/usr/bin/python
from beem.account import Account

a = Account("berniesanders")
start = 22487721
stop = 22487723

for op in a.history(start=start, stop=stop, use_block_num=True):
    print(op)

or

#!/usr/bin/python
from beem.account import Account

a = Account("berniesanders")
start = 22487723
stop = 22487721

for op in a.history_reverse(start=start, stop=stop, use_block_num=True):
    print(op)

Result:

Screenshot_2018-05-30_13-21-33.png

The cause of the error is that blockchain.get_estimated_block_num() tries to fetch a block with a negative block number. This function is used to look up the block number of the account creation date.

>>> b.block_time(1)
datetime.datetime(2016, 3, 24, 16, 5, tzinfo=<UTC>)
>>> b.get_estimated_block_num(addTzInfo(datetime(2016, 3, 26, 8, 26, 21)))
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/beem/blockchain.py", line 163, in get_estimated_block_num
    block = Block(block_number, steem_instance=self.steem)
  File "/usr/local/lib/python3.6/site-packages/beem/block.py", line 68, in __init__
    steem_instance=steem_instance
  File "/usr/local/lib/python3.6/site-packages/beem/blockchainobject.py", line 114, in __init__
    self.refresh()
  File "/usr/local/lib/python3.6/site-packages/beem/block.py", line 96, in refresh
    raise BlockDoesNotExistsException(str(self.identifier))
beem.exceptions.BlockDoesNotExistsException: -17607

The smallest timestamp with a positive block number is 2016-03-26T23:06:42, any earlier timestamp raises an exception:

>>> b.get_estimated_block_num(addTzInfo(datetime(2016, 3, 26, 23, 6, 42)))
65330
>>> b.get_estimated_block_num(addTzInfo(datetime(2016, 3, 26, 23, 6, 41)))
Traceback (most recent call last):
  File "", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/beem/blockchain.py", line 163, in get_estimated_block_num
    block = Block(block_number, steem_instance=self.steem)
  File "/usr/local/lib/python3.6/site-packages/beem/block.py", line 68, in __init__
    steem_instance=steem_instance
  File "/usr/local/lib/python3.6/site-packages/beem/blockchainobject.py", line 114, in __init__
    self.refresh()
  File "/usr/local/lib/python3.6/site-packages/beem/block.py", line 96, in refresh
    raise BlockDoesNotExistsException(str(self.identifier))
beem.exceptions.BlockDoesNotExistsException: 0

Environment

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

GitHub Account

https://github.com/crokkon
A Github issue has been created:
https://github.com/holgern/beem/issues/17

H2
H3
H4
3 columns
2 columns
1 column
6 Comments
Ecency