Skip to content
This repository was archived by the owner on Jan 20, 2026. It is now read-only.

Commit d9b9f96

Browse files
committed
Fix history item with index 0 for https://api.hive.blog
1 parent 5075fd9 commit d9b9f96

8 files changed

Lines changed: 116 additions & 29 deletions

File tree

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Changelog
22
=========
3+
0.24.19
4+
-------
5+
* Fix history item with index 0 for https://api.hive.blog
6+
37
0.24.18
48
-------
59
* Adapt account history on api changes and fixes issue #267

beem/account.py

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,8 @@ def virtual_op_count(self, until=None):
18351835
if op_count is None or len(op_count) == 0:
18361836
op_count = self._get_account_history(start=-1, limit=1)
18371837
if isinstance(op_count, list) and len(op_count) > 0 and len(op_count[0]) > 0:
1838+
if self.blockchain.rpc.url == "https://api.hive.blog":
1839+
return op_count[-1][0] + 1
18381840
return op_count[-1][0]
18391841
else:
18401842
return 0
@@ -1847,6 +1849,8 @@ def _get_account_history(self, account=None, start=-1, limit=1, operation_filter
18471849
account = extract_account_name(account)
18481850
if limit < 1:
18491851
limit = 1
1852+
elif limit > 1000:
1853+
limit = 1000
18501854
if not self.blockchain.is_connected():
18511855
raise OfflineHasNoRPCException("No RPC available in offline mode!")
18521856
self.blockchain.rpc.set_next_node_on_empty_reply(False)
@@ -1879,7 +1883,24 @@ def _get_account_history(self, account=None, start=-1, limit=1, operation_filter
18791883
api="database")
18801884
return ret
18811885

1882-
def estimate_virtual_op_num(self, blocktime, stop_diff=0, max_count=100):
1886+
def _get_blocknum_from_hist(self, index, min_index=1):
1887+
if index >= 0 and index < min_index:
1888+
index = min_index
1889+
op = self._get_account_history(start=(index))
1890+
if len(op) == 0:
1891+
return None
1892+
return op[0][1]['block']
1893+
1894+
def _get_first_blocknum(self):
1895+
min_index = 0
1896+
try:
1897+
created = self._get_blocknum_from_hist(0, min_index=min_index)
1898+
except:
1899+
min_index = 1
1900+
created = self._get_blocknum_from_hist(0, min_index=min_index)
1901+
return created, min_index
1902+
1903+
def estimate_virtual_op_num(self, blocktime, stop_diff=0, max_count=100, min_index=None):
18831904
""" Returns an estimation of an virtual operation index for a given time or blockindex
18841905
18851906
:param blocktime: start time or start block index from which account
@@ -1921,20 +1942,15 @@ def estimate_virtual_op_num(self, blocktime, stop_diff=0, max_count=100):
19211942
print(block_est - block_num)
19221943
19231944
"""
1924-
def get_blocknum(index):
1925-
if index == 0:
1926-
index = 1
1927-
op = self._get_account_history(start=(index))
1928-
if len(op) == 0:
1929-
return None
1930-
return op[0][1]['block']
1931-
19321945
max_index = self.virtual_op_count()
19331946
if max_index < stop_diff:
19341947
return 0
19351948

19361949
# calculate everything with block numbers
1937-
created = get_blocknum(1)
1950+
if min_index is None:
1951+
created, min_index = self._get_first_blocknum()
1952+
else:
1953+
created = self._get_blocknum_from_hist(0, min_index=min_index)
19381954

19391955
# convert blocktime to block number if given as a datetime/date/time
19401956
if isinstance(blocktime, (datetime, date, time)):
@@ -1948,7 +1964,7 @@ def get_blocknum(index):
19481964
return 0
19491965

19501966
# get the block number from the account's latest operation
1951-
latest_blocknum = get_blocknum(-1)
1967+
latest_blocknum = self._get_blocknum_from_hist(-1, min_index=min_index)
19521968

19531969
# requested blocknum/timestamp is after the latest account operation
19541970
if target_blocknum >= latest_blocknum:
@@ -1985,10 +2001,10 @@ def get_blocknum(index):
19852001

19862002
# get block number for current op number estimation
19872003
if op_num != last_op_num:
1988-
block_num = get_blocknum(op_num)
2004+
block_num = self._get_blocknum_from_hist(op_num, min_index=min_index)
19892005
while block_num is None and op_num < max_index:
19902006
op_num += 1
1991-
block_num = get_blocknum(op_num)
2007+
block_num = self._get_blocknum_from_hist(op_num, min_index=min_index)
19922008
last_op_num = op_num
19932009

19942010
# check if the required accuracy was reached
@@ -2267,9 +2283,10 @@ def history(
22672283
if start is not None and not use_block_num and not isinstance(start, (datetime, date, time)):
22682284
start_index = start
22692285
elif start is not None and max_index > batch_size:
2270-
op_est = self.estimate_virtual_op_num(start, stop_diff=1)
2271-
if op_est == 0:
2272-
op_est = 1
2286+
created, min_index = self._get_first_blocknum()
2287+
op_est = self.estimate_virtual_op_num(start, stop_diff=1, min_index=min_index)
2288+
if op_est < min_index:
2289+
op_est = min_index
22732290
est_diff = 0
22742291
if isinstance(start, (datetime, date, time)):
22752292
for h in self.get_account_history(op_est, 0):
@@ -2301,7 +2318,9 @@ def history(
23012318
if first > max_index:
23022319
_limit = max_index - start_index
23032320
first = start_index + _limit - 1
2304-
elif first < _limit:
2321+
elif first < _limit and self.blockchain.rpc.url == "https://api.hive.blog":
2322+
first = _limit - 1
2323+
elif first < _limit and self.blockchain.rpc.url != "https://api.hive.blog":
23052324
first = _limit
23062325
last_round = False
23072326

@@ -2316,7 +2335,9 @@ def history(
23162335

23172336
while True:
23182337
# RPC call
2319-
if first < _limit:
2338+
if first < _limit - 1 and self.blockchain.rpc.url == "https://api.hive.blog":
2339+
first = _limit - 1
2340+
elif first < _limit and self.blockchain.rpc.url != "https://api.hive.blog":
23202341
first = _limit
23212342
batch_count = 0
23222343
for item in self.get_account_history(first, _limit, start=None, stop=None, order=1, only_ops=only_ops, exclude_ops=exclude_ops, raw_output=raw_output):
@@ -2462,10 +2483,11 @@ def history_reverse(
24622483
elif start is not None and isinstance(start, int) and not use_block_num:
24632484
first = start
24642485
elif start is not None and first > batch_size:
2465-
op_est = self.estimate_virtual_op_num(start, stop_diff=1)
2486+
created, min_index = self._get_first_blocknum()
2487+
op_est = self.estimate_virtual_op_num(start, stop_diff=1, min_index=min_index)
24662488
est_diff = 0
2467-
if op_est == 0:
2468-
op_est = 1
2489+
if op_est < min_index:
2490+
op_est = min_index
24692491
if isinstance(start, (datetime, date, time)):
24702492
for h in self.get_account_history(op_est, 0):
24712493
block_date = formatTimeString(h["timestamp"])
@@ -2496,7 +2518,9 @@ def history_reverse(
24962518
last_item_index = first + 1
24972519
while True:
24982520
# RPC call
2499-
if first - _limit < 0:
2521+
if first - _limit < 0 and self.blockchain.rpc.url == 'https://api.hive.blog':
2522+
_limit = first + 1
2523+
elif first - _limit < 0 and self.blockchain.rpc.url != 'https://api.hive.blog':
25002524
_limit = first
25012525
batch_count = 0
25022526
for item in self.get_account_history(first, _limit, start=None, stop=None, order=-1, only_ops=only_ops, exclude_ops=exclude_ops, raw_output=raw_output):

beem/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""THIS FILE IS GENERATED FROM beem SETUP.PY."""
2-
version = '0.24.17'
2+
version = '0.24.19'

beemapi/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""THIS FILE IS GENERATED FROM beem SETUP.PY."""
2-
version = '0.24.17'
2+
version = '0.24.19'

beembase/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""THIS FILE IS GENERATED FROM beem SETUP.PY."""
2-
version = '0.24.17'
2+
version = '0.24.19'

beemgraphenebase/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
"""THIS FILE IS GENERATED FROM beem SETUP.PY."""
2-
version = '0.24.17'
2+
version = '0.24.19'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
ascii = codecs.lookup('ascii')
1717
codecs.register(lambda name, enc=ascii: {True: enc}.get(name == 'mbcs'))
1818

19-
VERSION = '0.24.18'
19+
VERSION = '0.24.19'
2020

2121
tests_require = ['mock >= 2.0.0', 'pytest', 'pytest-mock', 'parameterized']
2222

tests/beem/test_account.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,14 @@ def test_estimate_virtual_op_num(self):
493493
self.assertTrue(abs(op_num1 - op_num3) < 200)
494494
block_diff1 = 0
495495
block_diff2 = 0
496-
for h in account.get_account_history(op_num4 - 1, 0):
496+
for h in account.get_account_history(op_num4 - 1, 1):
497497
block_diff1 = (block_num - h["block"])
498-
for h in account.get_account_history(op_num4 + 1, 0):
498+
for h in account.get_account_history(op_num4 + 1, 1):
499499
block_diff2 = (block_num - h["block"])
500500
self.assertTrue(block_diff1 > 0)
501+
self.assertTrue(block_diff1 < 1000)
501502
self.assertTrue(block_diff2 <= 0)
503+
self.assertTrue(block_diff2 > -1000)
502504

503505
def test_estimate_virtual_op_num2(self):
504506
account = self.account
@@ -542,6 +544,47 @@ def test_history_votes(self):
542544
self.assertEqual(votes_list[0]["voter"], votes_list2[-1]["voter"])
543545
self.assertEqual(votes_list[-1]["voter"], votes_list2[0]["voter"])
544546

547+
def test_history_op_filter(self):
548+
stm = Hive("https://api.hive.blog")
549+
account = Account("beembot", blockchain_instance=stm)
550+
votes_list = list(account.history(only_ops=["vote"]))
551+
other_list = list(account.history(exclude_ops=["vote"]))
552+
all_list = list(account.history())
553+
self.assertEqual(len(all_list), len(votes_list) + len(other_list))
554+
index = 0
555+
for h in sorted((votes_list + other_list), key=lambda h: h["index"]):
556+
self.assertEqual(index, h["index"])
557+
index += 1
558+
votes_list = list(account.history_reverse(only_ops=["vote"]))
559+
other_list = list(account.history_reverse(exclude_ops=["vote"]))
560+
all_list = list(account.history_reverse())
561+
self.assertEqual(len(all_list), len(votes_list) + len(other_list))
562+
index = 0
563+
for h in sorted((votes_list + other_list), key=lambda h: h["index"]):
564+
self.assertEqual(index, h["index"])
565+
index += 1
566+
567+
def test_history_op_filter2(self):
568+
stm = Hive("https://api.hive.blog")
569+
batch_size = 100
570+
account = Account("beembot", blockchain_instance=stm)
571+
votes_list = list(account.history(only_ops=["vote"], batch_size=batch_size))
572+
other_list = list(account.history(exclude_ops=["vote"], batch_size=batch_size))
573+
all_list = list(account.history(batch_size=batch_size))
574+
self.assertEqual(len(all_list), len(votes_list) + len(other_list))
575+
index = 0
576+
for h in sorted((votes_list + other_list), key=lambda h: h["index"]):
577+
self.assertEqual(index, h["index"])
578+
index += 1
579+
votes_list = list(account.history_reverse(only_ops=["vote"], batch_size=batch_size))
580+
other_list = list(account.history_reverse(exclude_ops=["vote"], batch_size=batch_size))
581+
all_list = list(account.history_reverse(batch_size=batch_size))
582+
self.assertEqual(len(all_list), len(votes_list) + len(other_list))
583+
index = 0
584+
for h in sorted((votes_list + other_list), key=lambda h: h["index"]):
585+
self.assertEqual(index, h["index"])
586+
index += 1
587+
545588
def test_comment_history(self):
546589
account = self.account
547590
comments = []
@@ -601,3 +644,19 @@ def test_extract_account_name(self):
601644
self.assertEqual(extract_account_name("holger80"), "holger80")
602645
self.assertEqual(extract_account_name({"name": "holger80"}), "holger80")
603646
self.assertEqual(extract_account_name(""), "")
647+
648+
def test_get_blocknum_from_hist(self):
649+
stm = Hive("https://api.hive.blog")
650+
account = Account("beembot", blockchain_instance=stm)
651+
created, min_index = account._get_first_blocknum()
652+
if min_index == 0:
653+
self.assertEqual(created, 23687631)
654+
block = account._get_blocknum_from_hist(0, min_index=min_index)
655+
self.assertEqual(block, 23687631)
656+
hist_num = account.estimate_virtual_op_num(block, min_index=min_index)
657+
self.assertEqual(hist_num, 0)
658+
else:
659+
self.assertEqual(created, 23721519)
660+
min_index = 1
661+
block = account._get_blocknum_from_hist(0, min_index=min_index)
662+
self.assertEqual(block, 23721519)

0 commit comments

Comments
 (0)