def retry_action(self, action, *args, **kwargs): """ Perform an action, and if certain suspected-to-be-spurious grapheme bugs occur, instead of bubbling the exception, it is quietly logged (level WARN), and try again tries a fixed number of times (MAX_TRIES) before failing :param action: :return: """ tries = 0 while True: try: return action(*args, **kwargs) except bitsharesapi.exceptions.UnhandledRPCError as exception: if "Assert Exception: amount_to_sell.amount > 0" in str( exception): if tries > MAX_TRIES: raise else: tries += 1 self.log.warning("Ignoring: '{}'".format( str(exception))) self.bitshares.txbuffer.clear() self.account.refresh() time.sleep(2) elif "now <= trx.expiration" in str( exception): # Usually loss of sync to blockchain if tries > MAX_TRIES: raise else: tries += 1 self.log.warning("retrying on '{}'".format( str(exception))) self.bitshares.txbuffer.clear() time.sleep(6) # Wait at least a BitShares block elif "trx.expiration <= now + chain_parameters.maximum_time_until_expiration" in str( exception): if tries > MAX_TRIES: info = self.bitshares.info() raise Exception( 'Too much difference between node block time and trx expiration, please change ' 'the node. Block time: {}, local time: {}'.format( info['time'], formatTime(datetime.datetime.utcnow()))) else: tries += 1 self.log.warning( 'Too much difference between node block time and trx expiration, switching ' 'node') self.bitshares.txbuffer.clear() self.bitshares.rpc.next() elif "Assert Exception: delta.amount > 0: Insufficient Balance" in str( exception): self.log.critical('Insufficient balance of fee asset') raise else: raise
def getMarketBuckets(self, asset_a, asset_b, start=None, stop=None, raw=False): from datetime import datetime, timedelta from bitshares.utils import formatTime if not stop: stop = datetime.now() if not start: start = stop - timedelta(hours=24) interval = (stop - start).total_seconds() bucket_len = interval / 200 bucket_sizes = self.bts.rpc.market_buckets actual_bucket_len = bucket_sizes[-1] #60 #for b in bucket_sizes: # if bucket_len >= b: # actual_bucket_len = b for b in reversed(bucket_sizes): if bucket_len <= b: actual_bucket_len = b #stop = stop + timedelta(seconds=actual_bucket_len) buckets = self.bts.rpc.get_market_history( asset_a["id"], asset_b["id"], int(actual_bucket_len), formatTime(start), formatTime(stop), api="history", ) if raw: return buckets trades = [] for o in buckets: prec1 = self.softAmountStr(int(o["close_base"]), asset_a["symbol"], delim="") prec2 = self.softAmountStr(int(o["close_quote"]), asset_b["symbol"], delim="") price = float(prec1) / float(prec2) t = {"date": o["key"]["open"], "price": price} trades.append(t) return trades
def retry_action(self, action, *args, **kwargs): """ Perform an action, and if certain suspected-to-be-spurious grapheme bugs occur, instead of bubbling the exception, it is quietly logged (level WARN), and try again tries a fixed number of times (MAX_TRIES) before failing. :param action: :return: """ tries = 0 while True: try: ref_block = self.bitshares.txbuffer.get("ref_block_num") ref_block_prefix = self.bitshares.txbuffer.get("ref_block_prefix") self.log.debug('Ref block num: {}, prefix: {}'.format(ref_block, ref_block_prefix)) return action(*args, **kwargs) except bitsharesapi.exceptions.UnhandledRPCError as exception: if "Assert Exception: amount_to_sell.amount > 0" in str(exception): if tries > MAX_TRIES: raise else: tries += 1 self.log.warning("Ignoring: '{}'".format(str(exception))) self.bitshares.txbuffer.clear() self._account.refresh() time.sleep(2) elif "now <= trx.expiration" in str(exception): # Usually loss of sync to blockchain if tries > MAX_TRIES: raise else: tries += 1 self.log.warning("retrying on '{}'".format(str(exception))) self.bitshares.txbuffer.clear() time.sleep(6) # Wait at least a BitShares block elif "trx.expiration <= now + chain_parameters.maximum_time_until_expiration" in str(exception): if tries > MAX_TRIES: info = self.bitshares.info() raise Exception( 'Too much difference between node block time and trx expiration, please change ' 'the node. Block time: {}, local time: {}'.format( info['time'], formatTime(datetime.datetime.utcnow()) ) ) else: tries += 1 self.log.warning( 'Too much difference between node block time and trx expiration, switching node' ) self.bitshares.txbuffer.clear() self.bitshares.rpc.next() elif "Assert Exception: delta.amount > 0: Insufficient Balance" in str(exception): self.log.critical('Insufficient balance of fee asset') raise elif "trx.ref_block_prefix == tapos_block_summary.block_id._hash" in str(exception): if tries > MAX_TRIES: raise else: # TODO: move node switch to a function old = self.bitshares.rpc.url self.log.warning('Got tapos_block_summary exception, switching node') self.bitshares.clear() # reinstantiates txbuilder (it caches ref_block_num) # TODO: Notify still uses old node, needs to be switched! self.bitshares.rpc.next() new = self.bitshares.rpc.url self.log.info('Old: {}, new: {}'.format(old, new)) tries += 1 else: raise