def count_votes(self, block_hash: Hash32): # count votes vote = self._blockmanager.candidate_blocks.get_vote(block_hash) if not vote.get_result(block_hash.hex(), conf.VOTING_RATIO): return True # vote not complete yet self.__stop_broadcast_send_unconfirmed_block_timer()
def __init__(self, block_hash: Hash32): """Recommend use factory methods(from_*) instead direct this. """ if ObjectManager().channel_service: audience = ObjectManager().channel_service.peer_manager else: audience = None self.start_time = util.get_time_stamp() # timestamp self.hash = block_hash self.vote = Vote(block_hash.hex(), audience) self.__block = None
def prove_transaction(self, tx_hash: Hash32, proof: list): try: tx_info = self.find_tx_info(tx_hash.hex()) except KeyError: raise RuntimeError(f"Tx does not exist.") block_hash = tx_info["block_hash"] block = self.find_block_by_hash(block_hash) if block.header.version == "0.1a": raise RuntimeError(f"Block version({block.header.version}) of the Tx does not support proof.") block_prover = BlockProver.new(block.header.version, None, BlockProverType.Transaction) # Do not need txs return block_prover.prove(tx_hash, block.header.transaction_hash, proof)
def get_transaction_proof(self, tx_hash: Hash32): try: tx_info = self.find_tx_info(tx_hash.hex()) except KeyError: raise RuntimeError(f"Tx does not exist.") block_hash = tx_info["block_hash"] block = self.find_block_by_hash(block_hash) if block.header.version == "0.1a": raise RuntimeError(f"Block version({block.header.version}) of the Tx does not support proof.") block_prover = BlockProver.new(block.header.version, block.body.transactions, BlockProverType.Transaction) return block_prover.get_proof(tx_hash)
def prove_receipt(self, tx_hash: Hash32, proof: list): try: tx_info = self.find_tx_info(tx_hash.hex()) except KeyError: raise RuntimeError(f"Tx does not exist.") tx_result = tx_info["result"] block_hash = tx_info["block_hash"] block = self.find_block_by_hash(block_hash) if block.header.version == "0.1a": raise RuntimeError(f"Block version({block.header.version}) of the Tx does not support proof.") block_prover = BlockProver.new(block.header.version, None, BlockProverType.Receipt) # Do not need receipts receipt_hash = block_prover.to_hash32(tx_result) return block_prover.prove(receipt_hash, block.header.receipt_hash, proof)
def get_receipt_proof(self, tx_hash: Hash32): try: tx_info = self.find_tx_info(tx_hash.hex()) except KeyError: raise RuntimeError(f"Tx does not exist.") tx_result = tx_info["result"] block_hash = tx_info["block_hash"] block = self.find_block_by_hash(block_hash) if block.header.version == "0.1a": raise RuntimeError(f"Block version({block.header.version}) of the Tx does not support proof.") tx_results = (self.find_tx_info(tx_hash)["result"] for tx_hash in block.body.transactions) block_prover = BlockProver.new(block.header.version, tx_results, BlockProverType.Receipt) receipt_hash = block_prover.to_hash32(tx_result) return block_prover.get_proof(receipt_hash)
def vote_unconfirmed_block(self, peer_id, group_id, block_hash: Hash32, vote_code) -> None: block_manager = self._channel_service.block_manager util.logger.spam(f"channel_inner_service:VoteUnconfirmedBlock " f"({ChannelProperty().name}) block_hash({block_hash})") util.logger.debug("Peer vote to : " + block_hash.hex()[:8] + " " + str(vote_code) + f"from {peer_id[:8]}") self._channel_service.block_manager.candidate_blocks.add_vote( block_hash, group_id, peer_id, (False, True)[vote_code == message_code.Response.success_validate_block] ) consensus = block_manager.consensus_algorithm if isinstance(consensus, ConsensusSiever) and self._channel_service.state_machine.state == "BlockGenerate": consensus.count_votes(block_hash)