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

in #utopian-io6 years ago (edited)

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 "<stdin>", 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 "<stdin>", 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

Sort:  

Hey @stmdev
Thanks for contributing on Utopian.
Congratulations! Your contribution was Staff Picked to receive a maximum vote for the bug-hunting category on Utopian for being of significant value to the project and the open source community.

We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

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

Vote for Utopian Witness!

Thank you for your contribution. Due to the provided scripts, I could easily reproduce the error and finally fix it. The provided error log was also very helpful.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

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]

Thanks! Great to see you are a moderator as well now - Welcome on board! :)

I was asked as project owner to review, so I'm not a moderator :).

Thanks for finding this issue. I can confirm the bug.

I already fixed beem in this commit.
I added a lower bound for the block number:

       if block_number < 1:
           block_number = 1

That was fast, thanks! I can confirm it's working correctly now:

# python bug_neg_blocknum.py 
{'voter': 'thecyclist', 'author': 'berniesanders', 'permlink': 're-cryptoctopus-re-crypto-econom1st-re-cryptoctopus-getting-ready-for-steem-1-0-and-the-end-of-beta-20180516t183042764z', 'weight': 800, 'trx_id': '73e00ce7bde88e86b2ed468c39ed2e31cbb23c25', 'block': 22487722, 'trx_in_block': 33, 'op_in_trx': 0, 'virtual_op': 0, 'timestamp': '2018-05-16T18:30:57', 'account': 'berniesanders', 'type': 'vote', '_id': '0c6034ce648db568e2fc1ed733c188f0595686eb', 'index': 286221}
{'voter': 'yougotflagged', 'author': 'berniesanders', 'permlink': 're-cryptoctopus-re-crypto-econom1st-re-cryptoctopus-getting-ready-for-steem-1-0-and-the-end-of-beta-20180516t183042764z', 'weight': 800, 'trx_id': '73e00ce7bde88e86b2ed468c39ed2e31cbb23c25', 'block': 22487722, 'trx_in_block': 33, 'op_in_trx': 1, 'virtual_op': 0, 'timestamp': '2018-05-16T18:30:57', 'account': 'berniesanders', 'type': 'vote', '_id': '8c22c5def2f5873698d42c53fbed9f24420cc69c', 'index': 286222}
{'voter': 'steemservices', 'author': 'berniesanders', 'permlink': 're-cryptoctopus-re-crypto-econom1st-re-cryptoctopus-getting-ready-for-steem-1-0-and-the-end-of-beta-20180516t183042764z', 'weight': 800, 'trx_id': '73e00ce7bde88e86b2ed468c39ed2e31cbb23c25', 'block': 22487722, 'trx_in_block': 33, 'op_in_trx': 2, 'virtual_op': 0, 'timestamp': '2018-05-16T18:30:57', 'account': 'berniesanders', 'type': 'vote', '_id': '7d5b58a054f562c436747173828146b4e557bc2e', 'index': 286223}
{'voter': 'thedelegator', 'author': 'berniesanders', 'permlink': 're-cryptoctopus-re-crypto-econom1st-re-cryptoctopus-getting-ready-for-steem-1-0-and-the-end-of-beta-20180516t183042764z', 'weight': 800, 'trx_id': '73e00ce7bde88e86b2ed468c39ed2e31cbb23c25', 'block': 22487722, 'trx_in_block': 33, 'op_in_trx': 3, 'virtual_op': 0, 'timestamp': '2018-05-16T18:30:57', 'account': 'berniesanders', 'type': 'vote', '_id': '4a38bb0ac99bee28cffd1b2836670cfa0eb6cba8', 'index': 286224}
{'voter': 'nextgencrypto', 'author': 'berniesanders', 'permlink': 're-cryptoctopus-re-crypto-econom1st-re-cryptoctopus-getting-ready-for-steem-1-0-and-the-end-of-beta-20180516t183042764z', 'weight': 800, 'trx_id': '73e00ce7bde88e86b2ed468c39ed2e31cbb23c25', 'block': 22487722, 'trx_in_block': 33, 'op_in_trx': 4, 'virtual_op': 0, 'timestamp': '2018-05-16T18:30:57', 'account': 'berniesanders', 'type': 'vote', '_id': '03b0eae366d90cd804777b64b09e3915b5475e6f', 'index': 286225}
{'voter': 'ngc', 'author': 'berniesanders', 'permlink': 're-cryptoctopus-re-crypto-econom1st-re-cryptoctopus-getting-ready-for-steem-1-0-and-the-end-of-beta-20180516t183042764z', 'weight': 800, 'trx_id': '73e00ce7bde88e86b2ed468c39ed2e31cbb23c25', 'block': 22487722, 'trx_in_block': 33, 'op_in_trx': 5, 'virtual_op': 0, 'timestamp': '2018-05-16T18:30:57', 'account': 'berniesanders', 'type': 'vote', '_id': '7269876f6616cfc8ef2dba41aa46a74dd2e4fea7', 'index': 286226}
{'voter': 'iflagtrash', 'author': 'berniesanders', 'permlink': 're-cryptoctopus-re-crypto-econom1st-re-cryptoctopus-getting-ready-for-steem-1-0-and-the-end-of-beta-20180516t183042764z', 'weight': 800, 'trx_id': '73e00ce7bde88e86b2ed468c39ed2e31cbb23c25', 'block': 22487722, 'trx_in_block': 33, 'op_in_trx': 6, 'virtual_op': 0, 'timestamp': '2018-05-16T18:30:57', 'account': 'berniesanders', 'type': 'vote', '_id': 'e303538389d1b9aacfa42e2511e3b80aca5e1870', 'index': 286227}
{'voter': 'engagement', 'author': 'berniesanders', 'permlink': 're-cryptoctopus-re-crypto-econom1st-re-cryptoctopus-getting-ready-for-steem-1-0-and-the-end-of-beta-20180516t183042764z', 'weight': 800, 'trx_id': '73e00ce7bde88e86b2ed468c39ed2e31cbb23c25', 'block': 22487722, 'trx_in_block': 33, 'op_in_trx': 7, 'virtual_op': 0, 'timestamp': '2018-05-16T18:30:57', 'account': 'berniesanders', 'type': 'vote', '_id': '4cc15251be94ddccfd63cc43cb71f90788df9d92', 'index': 286228}

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.029
BTC 57491.44
ETH 3031.27
USDT 1.00
SBD 2.38