async def _wait_for_voting(self, candidate_block: 'Block'): """Waiting validator's vote for the candidate_block. :param candidate_block: :return: vote_result or None """ # util.logger.notice(f"_wait_for_voting block({candidate_block.header.hash})") while True: vote = self._block_manager.candidate_blocks.get_vote( candidate_block.header.hash) vote_result = vote.get_result(candidate_block.header.hash.hex(), conf.VOTING_RATIO) if vote_result: self.__stop_broadcast_send_unconfirmed_block_timer() return vote_result await asyncio.sleep(conf.WAIT_SECONDS_FOR_VOTE) timeout_timestamp = candidate_block.header.timestamp + conf.BLOCK_VOTE_TIMEOUT * 1_000_000 timeout = -util.diff_in_seconds(timeout_timestamp) try: if timeout < 0: raise asyncio.TimeoutError if await asyncio.wait_for(self._vote_queue.get(), timeout=timeout) is None: # sentinel return None except asyncio.TimeoutError: util.logger.warning( "Timed Out Block not confirmed duration: " + str(util.diff_in_seconds(candidate_block.header.timestamp)) ) return None
async def _wait_for_voting(self, candidate_block: 'Block', vote: 'Vote'): while True: result = vote.get_result(candidate_block.header.hash.hex(), conf.VOTING_RATIO) if result: return True timeout_timestamp = candidate_block.header.timestamp + conf.BLOCK_VOTE_TIMEOUT * 1_000_000 timeout = -util.diff_in_seconds(timeout_timestamp) try: if timeout < 0: raise asyncio.TimeoutError vote_result = await asyncio.wait_for(self._vote_queue.get(), timeout=timeout) if vote_result is None: # sentinel return False vote_block_hash, vote_code, peer_id, group_id = vote_result if vote.target_hash == vote_block_hash: vote.add_vote(group_id, peer_id, vote_code) except asyncio.TimeoutError: logging.warning( "Timed Out Block not confirmed duration: " + str(util.diff_in_seconds(candidate_block.header.timestamp)) ) return False
def __check_timeout(self, block): timeout_timestamp = block.header.timestamp + conf.BLOCK_VOTE_TIMEOUT * 1_000_000 timeout = -util.diff_in_seconds(timeout_timestamp) if timeout < 0: raise TimeoutError return timeout
async def _wait_for_voting(self, block: 'Block'): """Waiting validator's vote for the candidate_block. :param block: :return: vote_result or None """ while True: vote = self._block_manager.candidate_blocks.get_votes( block.header.hash, self._block_manager.epoch.round) if not vote: raise ThereIsNoCandidateBlock util.logger.info(f"Votes : {vote.get_summary()}") if vote.is_completed(): self._block_manager.epoch.complained_result = None self.stop_broadcast_send_unconfirmed_block_timer() return vote await asyncio.sleep(conf.WAIT_SECONDS_FOR_VOTE) try: timeout = self.__check_timeout(block) if not await asyncio.wait_for(self._vote_queue.get(), timeout=timeout): # sentinel raise NotEnoughVotes except (TimeoutError, asyncio.TimeoutError): util.logger.warning( "Timed Out Block not confirmed duration: " + str(util.diff_in_seconds(block.header.timestamp))) raise NotEnoughVotes
def remove_block(self, block_hash): if block_hash in self.blocks and self.blocks[block_hash].block: prev_block_hash = self.blocks[block_hash].block.header.prev_hash for _block_hash in list(self.blocks.keys()): if self.blocks[_block_hash].block: if self.blocks[_block_hash].block.header.prev_hash == prev_block_hash: self.blocks.pop(_block_hash, None) continue if util.diff_in_seconds(self.blocks[_block_hash].start_time) >= conf.CANDIDATE_BLOCK_TIMEOUT: self.blocks.pop(_block_hash, None)