diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 90ccc0acebb8a0a9620a4cfd16a9c20182ca07bd..8e9c9a885c680f63d1d0b4453e5639f472f88bbe 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,7 @@ Changelog * Adapt account history on api changes and fixes issue #267 * Speed up history call, when limit is below 1000 * Improve unit tests for account history +* Fix estimate_virtual_op_num, when get_account_history returns an empty entry for an index 0.24.17 ------- diff --git a/beem/account.py b/beem/account.py index ed84b3b97cd2812f2aba0ebef8287bf97131dfba..e981923fc94340c71cbbc0ac1eed676500be1cf7 100644 --- a/beem/account.py +++ b/beem/account.py @@ -653,10 +653,10 @@ class Account(BlockchainObject): """ if self['mined']: return None - ops = list(self.get_account_history(0, 0)) - if not ops or 'creator' not in ops[0]: + ops = list(self.get_account_history(1, 1)) + if not ops or 'creator' not in ops[-1]: return None - return ops[0]['creator'] + return ops[-1]['creator'] def get_recharge_time_str(self, voting_power_goal=100, starting_voting_power=None): """ Returns the account recharge time as string @@ -1841,7 +1841,7 @@ class Account(BlockchainObject): except IndexError: return 0 - def _get_account_history(self, account=None, start=-1, limit=1): + def _get_account_history(self, account=None, start=-1, limit=1, operation_filter_low=None, operation_filter_high=None): if account is None: account = self["name"] account = extract_account_name(account) @@ -1850,15 +1850,33 @@ class Account(BlockchainObject): if not self.blockchain.is_connected(): raise OfflineHasNoRPCException("No RPC available in offline mode!") self.blockchain.rpc.set_next_node_on_empty_reply(False) - if self.blockchain.rpc.get_use_appbase(): - try: - ret = self.blockchain.rpc.get_account_history({'account': account, 'start': start, 'limit': limit}, api="account_history") - if ret is not None: - ret = ret["history"] - except ApiNotSupported: - ret = self.blockchain.rpc.get_account_history(account, start, limit, api="condenser") + if operation_filter_low is None and operation_filter_high is None: + if self.blockchain.rpc.get_use_appbase(): + try: + ret = self.blockchain.rpc.get_account_history({'account': account, 'start': start, 'limit': limit}, api="account_history") + if ret is not None: + ret = ret["history"] + except ApiNotSupported: + ret = self.blockchain.rpc.get_account_history(account, start, limit, api="condenser") + else: + ret = self.blockchain.rpc.get_account_history(account, start, limit, api="database") else: - ret = self.blockchain.rpc.get_account_history(account, start, limit, api="database") + if self.blockchain.rpc.get_use_appbase(): + try: + ret = self.blockchain.rpc.get_account_history({'account': account, 'start': start, 'limit': limit, + 'operation_filter_low': operation_filter_low, + 'operation_filter_high': operation_filter_low}, api="account_history") + if ret is not None: + ret = ret["history"] + except ApiNotSupported: + ret = self.blockchain.rpc.get_account_history(account, start, limit, + operation_filter_low, + operation_filter_high, api="condenser") + else: + ret = self.blockchain.rpc.get_account_history(account, start, limit, + operation_filter_low, + operation_filter_high, + api="database") return ret def estimate_virtual_op_num(self, blocktime, stop_diff=0, max_count=100): @@ -1907,6 +1925,8 @@ class Account(BlockchainObject): if index == 0: index = 1 op = self._get_account_history(start=(index)) + if len(op) == 0: + return None return op[0][1]['block'] max_index = self.virtual_op_count() @@ -1966,6 +1986,9 @@ class Account(BlockchainObject): # get block number for current op number estimation if op_num != last_op_num: block_num = get_blocknum(op_num) + while block_num is None and op_num < max_index: + op_num += 1 + block_num = get_blocknum(op_num) last_op_num = op_num # check if the required accuracy was reached diff --git a/tests/beem/test_instance.py b/tests/beem/test_instance.py index c1b7706886db8f63c94f99c4942abbce161a1c64..ba57e217d43717c68c5aa8ab5caa1e469be92a05 100644 --- a/tests/beem/test_instance.py +++ b/tests/beem/test_instance.py @@ -4,7 +4,7 @@ import unittest import random from parameterized import parameterized from pprint import pprint -from beem import Steem +from beem import Steem, Hive from beem.amount import Amount from beem.witness import Witness from beem.account import Account @@ -32,20 +32,20 @@ core_unit = "STM" class Testcases(unittest.TestCase): @classmethod def setUpClass(cls): - stm = Steem(node=get_hive_nodes()) + stm = Hive(node=get_hive_nodes()) stm.config.refreshBackup() stm.set_default_nodes(["xyz"]) del stm cls.urls = get_hive_nodes() - cls.bts = Steem( + cls.bts = Hive( node=cls.urls, nobroadcast=True, num_retries=10 ) set_shared_steem_instance(cls.bts) acc = Account("fullnodeupdate", steem_instance=cls.bts) - comment = Comment(acc.get_blog_entries(limit=10)[1], steem_instance=cls.bts) + comment = Comment(acc.get_blog_entries(limit=5)[1], steem_instance=cls.bts) cls.authorperm = comment.authorperm votes = comment.get_votes(raw_data=True) last_vote = votes[-1] @@ -53,7 +53,7 @@ class Testcases(unittest.TestCase): @classmethod def tearDownClass(cls): - stm = Steem(node=get_hive_nodes()) + stm = Hive(node=get_hive_nodes()) stm.config.recover_with_latest_backup() @parameterized.expand([