def update_sync_eos(self, session, block_num, block_hash): """ 开奖时,如果再次查询节点上的该区块,如果hash有变化,说明回滚,取最新区块详情,修改该区块 :param session: :param block_num: :param block_hash: :return: """ block_detail = self.ec.http_get_block_detail(block_num) if block_detail: remote_block_hash = block_detail.get("id", "") time_stamp = block_detail.get("timestamp", "") if remote_block_hash: if remote_block_hash != block_hash: sync_eos_model = session.query(SyncEosModel).filter( SyncEosModel.block_num == block_num).first() if sync_eos_model: time_stamp_decimal = get_decimal( str_to_timestamp(time_stamp), digits=3) previous = block_detail.get("previous", "") sync_eos_model.block_hash = remote_block_hash sync_eos_model.time_stamp = time_stamp sync_eos_model.time_stamp_decimal = time_stamp_decimal sync_eos_model.previous = previous sync_eos_model.status = self.sync_success self.slog.info("lottery block_num:", block_num, "lottery block_hash:", remote_block_hash) return { "block_hash": remote_block_hash, "time_stamp": time_stamp, } return {}
def sync_block(self, local_block_num, remote_block_num, interval=8): """ 同步区块,默认每8个块创建一个session scope,防止长连接 同步到SyncEosModel 同步到SyncBlockModel :param local_block_num: 本地block num :param remote_block_num: 远程block num :param interval: 创建MySQL链接的块数间隔 :return: """ for i in range(local_block_num + 1, remote_block_num, interval): temp_remote_block_num = i + interval if temp_remote_block_num > remote_block_num: temp_remote_block_num = remote_block_num sync_eos_model_list = [] temp_last_sync_block_no = None temp_last_sync_block_hash = "" # 调用协程读取 block_data_dict = self.async_block_multi(i, temp_remote_block_num) btime = time.time() with self.mysql_eos_tool.session_scope() as session_eos: with self.mysql_tool.session_scope() as session_select: for block_no_item in range(i, temp_remote_block_num): temp_last_sync_block_no = block_no_item temp_insert_block_data = block_data_dict.get(str(block_no_item), {}) if not temp_insert_block_data: time_stamp_decimal = get_decimal(block_no_item, digits=3) * -1 temp_last_sync_block_hash = "" eos_model = SyncEosModel( block_num=int(block_no_item), time_stamp_decimal=time_stamp_decimal, status=self.sync_fail ) sync_eos_model_list.append(eos_model) else: time_stamp = temp_insert_block_data.get("timestamp", "") time_stamp_decimal = get_decimal(str_to_timestamp(time_stamp), digits=3) temp_last_sync_block_hash = temp_insert_block_data.get("id") eos_model = SyncEosModel( block_num=temp_insert_block_data.get("block_num"), block_hash=temp_last_sync_block_hash, time_stamp=time_stamp, time_stamp_decimal=time_stamp_decimal, previous=temp_insert_block_data.get("previous"), status=self.sync_success ) sync_eos_model_list.append(eos_model) self.slog.info("sync block_num:", temp_last_sync_block_no, "sync block_hash:", temp_last_sync_block_hash) if sync_eos_model_list: session_eos.add_all(sync_eos_model_list) try: session_select.query(SyncBlockModel).filter(SyncBlockModel.coin_id == self.eos_coin_id).update({ SyncBlockModel.block_no: temp_last_sync_block_no, SyncBlockModel.block_hash: temp_last_sync_block_hash}) session_select.commit() session_eos.commit() except Exception as e: self.slog.error(str(e)) print("insert time " + str(time.time() - btime)) return True
def lottery_instant_adduce(cls, base_time_stamp, user_lottery_num_list, imaginary_num, faker=True, times=6): """ 一元夺宝 即时版 :param base_time_stamp: 收到的请求时间 float类型 时间戳 :param user_lottery_num_list: 判断用户是否中奖的号码段 例: [12, 45] :param imaginary_num: hash16进制之后取摩用的总虚数,当前btc价格加上溢价 :param faker: 你猜这个是干嘛的:) 传True就特殊处理,反之不处理 :param times: 查询区块的次数,默认6次 :return: """ ec = TokenNodeConfModel.get_eos_node_script(script_unit=_ZERO_S) node_data = ec.http_get_latest_block() latest_block = node_data.get("latest_block", {}) block_num = None begin, end = user_lottery_num_list counter = _ONE for i in range(_HUNDRED): if latest_block: if counter > int(times): break counter += _ONE block_num = latest_block["block_num"] block_hash = latest_block["id"] timestamp = latest_block["timestamp"] block_timestamp = str_to_timestamp(timestamp) if block_timestamp >= base_time_stamp: lottery_num = int(block_hash, 16) % imaginary_num + _ONE if (end < lottery_num or lottery_num < begin) or not faker: return { "block_num": block_num, "block_hash": block_hash, "timestamp": timestamp, "lottery_num": lottery_num, } if block_num: block_num += _ONE latest_block = ec.http_get_block_detail(block_num) if not latest_block: block_num -= _ONE else: latest_block = ec.http_get_latest_block() time.sleep(0.5) return {}
def composing_right_block(self, block_num=None): """ 查找并组装正确区块 :param block_num: :return: """ block_detail = {} # 如果一个小时都没有找到合适的块,就算了,肯定是其他问题 for i in range(3600): block_detail = self.get_node_block_detail_from_block_num(block_num, ["block_num", "timestamp", "id", "previous"]) if block_detail: right_time_stamp_str = block_detail.get("timestamp") right_time_stamp = str_to_timestamp(right_time_stamp_str) if right_time_stamp: block_detail["time_stamp_decimal"] = right_time_stamp break else: time.sleep(0.5) return block_detail
def get_base_trx_args(self): """ 拼接基础参数 :return: """ base_trx_args = {} try: latest_block = self.ec.http_get_latest_block() base_trx_args["chain_id"] = self.http_get_info() base_trx_args["pub_key"] = self.eos_pub_key base_trx_args["expiration"] = timestamp_to_str( str_to_timestamp(latest_block["timestamp"]) + float(self.expired_second), format_style="%Y-%m-%dT%H:%M:%S.%f") base_trx_args["ref_block_num"] = latest_block["block_num"] base_trx_args["ref_block_prefix"] = latest_block[ "ref_block_prefix"] except Exception as e: raise_logger("Exception get_base_trx_args: " + str(e), "wallet", "error") return base_trx_args
def dice_info(self, dice_part_id): with MysqlTools().session_scope() as session: dice_info = session.query(DiceParticipateInModel). \ filter(DiceParticipateInModel._id == dice_part_id).first() if dice_info is None: self.return_error(40021) if dice_info.block_timestamp != '0000-00-00T00:00:00.000': block_timestamp = timestamp_to_str( str_to_timestamp(dice_info.block_timestamp, format_style="%Y-%m-%dT%H:%M:%S.%f"), format_style="%Y-%m-%d %H:%M:%S") else: block_timestamp = '' return { 'dice_serial': dice_info.dice_serial, 'dice_time': timestamp_to_str(int(float(dice_info.dice_timestamp)), format_style="%Y-%m-%d %H:%M:%S"), 'reward_token': self.get_coin_name(dice_info.reward_token), 'reward_quantity': str(dice_info.reward_quantity), 'bet_number': str(dice_info.bet_number), 'bet_token': self.get_coin_name(dice_info.bet_token), 'user_dice': dice_info.user_dice, 'banker_dice': dice_info.banker_dice, 'dice_result': dice_info.dice_result, # 'calc': int(dice_info.block_hash, 16) / 3, 'eos_block_info': { 'block_no': dice_info.block_no, 'block_hash': dice_info.block_hash, 'block_timestamp': block_timestamp } }
def get_instant_result(self, part_in_id): with MysqlTools().session_scope() as session: part_in_info = session.query(ParticipateInModel). \ filter(ParticipateInModel._id == part_in_id).first() if part_in_info is None: self.return_error(40016) pattern = re.compile(r'[\[\]\s]') award_numbers_list = part_in_info.award_numbers indiana_numbers = pattern.sub('', award_numbers_list).split(',') number_list = [] is_win = False for i in indiana_numbers: if i == part_in_info.win_number: status = True else: status = False number_list.append({'number': i, 'is_win': status}) if part_in_info.win_number in indiana_numbers: is_win = True return { 'is_win': is_win, 'indiana_numbers': number_list, 'win_number': part_in_info.win_number, 'created_at': str(part_in_info.created_at), 'received_time': timestamp_to_str(str_to_timestamp(part_in_info.received_time), format_style="%Y-%m-%d %H:%M:%S"), 'block_no': part_in_info.block_no, 'bet_hash': part_in_info.bet_hash, }
b = [-32, -39, 52, 124, 45, -11, -104, 59] s = "" aa = "2019-01-09T09:05:48.123" import time import datetime datetime.timedelta(hours=0.5) # 格式化字符串输出 # d3 = d2.strftime('%Y-%m-%d %H:%M:%S') # 将字符串转化为时间类型 # res = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] # print(res) dtime = datetime.datetime.strptime(aa, "%Y-%m-%dT%H:%M:%S.%f") print(dtime.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]) print(dtime.timetuple()) print(dtime.microsecond / 1000000) un_time = time.mktime(dtime.timetuple()) + dtime.microsecond / 1000000 print(un_time) times = datetime.datetime.fromtimestamp(un_time) print(times.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]) from utils.time_util import str_to_timestamp, timestamp_to_str print(str_to_timestamp(aa)) ts = 1546995948.123 print(timestamp_to_str(ts))