def handle(self): """ :return: """ old_block = get_block_count() - 1 _event_coroutine = self.timer_event() next(_event_coroutine) self.prepare_handle_event() while WebSocketConnection._websocket_listening: try: response = self.receive() block_height = get_block_count() # handle response self.handle_event(block_height, response) # handle timer event if old_block != block_height: for block in range(old_block + 1, block_height + 1): ucoro_event(_event_coroutine, block) # set new value to old_block old_block = block_height time.sleep(0.5) except Exception as error: LOG.exception('websocket handle: {}'.format(error)) time.sleep(1) ucoro_event(_event_coroutine, None)
def monitorblock(): """""" old_blockheight_onchain = 0 while EventMonitor.GoOn: blockheight_onchain = get_block_count() EventMonitor.update_block_height(blockheight_onchain) blockheight = EventMonitor.get_wallet_block_height() block_delta = int(blockheight_onchain) - int(blockheight) if 1 != blockheight else 0 need_update = block_delta > 0 execute_event_machine = old_blockheight_onchain != blockheight_onchain # reset event machine event_machine.reset_polling() end_time = time.time() + 15 # sleep 15 second according to the chain update block time trigger_per_block = False while True: try: if 0 < block_delta < 2010: if EventMonitor.BlockPause: pass else: blockheight += 1 if blockheight > blockheight_onchain: need_update = False elif 2010 <= block_delta and need_update: # use magic number blockheight = int(blockheight_onchain) - 2000 # only update per 15 seconds when the delta is verify large trigger_per_block = True need_update = False # update wallet block height if need_update or trigger_per_block: EventMonitor.update_wallet_block_height(blockheight) # only execute the event machine when the block chain height is updated if execute_event_machine: event_machine.handle(blockheight_onchain) except Exception as error: pass finally: # check whether the wallet is updated and the event is pooled if need_update is False and (event_machine.is_polling_finished or execute_event_machine is False): left_loop_time = end_time - time.time() # suspend this thread until timeout time.sleep(left_loop_time) else: # poll per 100 ms time.sleep(0.1) # exit this loop for this blockheight if end_time - time.time() <= 0.15: # 150 ms old_blockheight_onchain = blockheight_onchain break
def do_create(self, arguments): super().do_create(arguments) if self.Wallet: CurrentLiveWallet.update_current_wallet(self.Wallet) blockheight = get_block_count() self.Wallet.BlockHeight = blockheight self.Wallet.SaveStoredData("BlockHeight", blockheight) EventMonitor.start_monitor(self.Wallet) self.retry_channel_enable()
def get_locked_block_height(cls, total_routers, jumps): """ :param total_routers: :param jumps: :return: """ if not (isinstance(jumps, int) and isinstance(total_routers, int)): return 0 locked_block_height = get_block_count( ) + HtlcMessage._block_per_day * (total_routers - jumps) return locked_block_height
def monitorWithdrawUpdate(self, message): if not message: LOG.error('Invalid message: {}'.format(message)) return try: invoker = message.get('invoker').strip() channel_name = message.get('channelId') except Exception as error: LOG.exception('Invalid message: {}. Exception: {}'.format( message, error)) else: if invoker.lower() != self.wallet_address.lower() and channel_name: ChannelStateManageEvent(channel_name).execute( get_block_count(), channel_name)
def get_unlocked_block_height(cls, total_routers): """ :param total_routers: :return: """ if not isinstance(total_routers, int): return 0 lock_day = total_routers - 1 if 0 >= lock_day: return 0 unlocked_block_height = get_block_count( ) + HtlcMessage._block_per_day * lock_day return unlocked_block_height
def register_event(self, event, end_block=None): if not end_block: block_height = get_block_count() + 1 else: block_height = end_block try: self.event_lock.acquire() event_list = self.__monitor_queue.get(block_height, []) event_list.append(event) self.__monitor_queue.update({block_height: event_list}) except Exception as error: LOG.error('websocket register_event exception: {}'.format(error)) finally: self.event_lock.release() return
def monitorWithdrawSettle(self, message): """ :param message: :return: """ try: channel_name = message.get('channelId') hashcode = message.get('lockHash') # update the channel to settled state if channel_name: channel_event = ChannelHtlcUnlockedEvent(channel_name) channel_event.execute(get_block_count(), channel_name, hashcode) except Exception as error: LOG.exception('Invalid message: {}. Exception: {}'.format( message, error)) return
def monitorSettle(self, message): """ :param message: :return: """ if not message: LOG.error('monitorSettle: Invalid message: {}'.format(message)) return try: channel_name = message.get('channelId') # update the channel to settled state if channel_name: channel_event = ChannelSettledEvent(channel_name) channel_event.execute(get_block_count(), channel_name) except Exception as error: LOG.exception('Invalid message: {}. Exception: {}'.format( message, error)) return