@@ -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 ):
0 commit comments