def test_block_votes_false(self): ratio = 0.67 block_hash = Hash32(os.urandom(Hash32.size)) block_votes = BlockVotes(self.reps, ratio, 0, 0, block_hash) for i, signer in enumerate(self.signers): if i % 4 == 0: block_vote = BlockVote.new(signer, 0, 0, 0, block_hash) else: block_vote = BlockVote.new(signer, 0, 0, 0, Hash32.empty()) block_votes.add_vote(block_vote) logging.info(block_votes) self.assertEqual(block_votes.quorum, len(self.reps) * ratio) self.assertEqual(block_votes.get_result(), False)
def vote_unconfirmed_block(self, block: Block, round_: int, is_validated): util.logger.debug( f"vote_unconfirmed_block() ({block.header.height}/{block.header.hash}/{is_validated})" ) vote = BlockVote.new( signer=ChannelProperty().peer_auth, block_height=block.header.height, round_=round_, block_hash=block.header.hash if is_validated else Hash32.empty(), timestamp=util.get_time_stamp()) self.candidate_blocks.add_vote(vote) vote_serialized = vote.serialize() vote_dumped = json.dumps(vote_serialized) block_vote = loopchain_pb2.BlockVote(vote=vote_dumped, channel=ChannelProperty().name) target_reps_hash = block.header.reps_hash if not target_reps_hash: target_reps_hash = self.__channel_service.peer_manager.prepared_reps_hash self.__channel_service.broadcast_scheduler.schedule_broadcast( "VoteUnconfirmedBlock", block_vote, reps_hash=target_reps_hash) return vote
def test_block_invalid_vote(self): ratio = 0.67 block_hash = Hash32(os.urandom(Hash32.size)) block_votes = BlockVotes(self.reps, ratio, 0, 0, block_hash) invalid_block_vote = BlockVote.new(self.signers[0], 0, 0, 1, block_hash) self.assertRaises(RuntimeError, block_votes.add_vote, invalid_block_vote) invalid_block_vote = BlockVote.new(self.signers[0], 0, 1, 0, block_hash) self.assertRaises(RuntimeError, block_votes.add_vote, invalid_block_vote) invalid_block_vote = BlockVote.new(self.signers[0], 0, 0, 0, Hash32(os.urandom(32))) self.assertRaises(RuntimeError, block_votes.add_vote, invalid_block_vote) invalid_block_vote = BlockVote(rep=self.reps[0], timestamp=0, signature=Signature(os.urandom(65)), block_height=0, round_=0, block_hash=block_hash) self.assertRaises(RuntimeError, block_votes.add_vote, invalid_block_vote) block_vote = BlockVote.new(self.signers[0], 0, 0, 0, block_hash) block_votes.add_vote(block_vote) duplicate_block_vote = BlockVote.new(self.signers[0], 0, 0, 0, Hash32.empty()) self.assertRaises(votes.VoteDuplicateError, block_votes.add_vote, duplicate_block_vote)
def test_block_votes_true(self): ratio = 0.67 block_hash = Hash32(os.urandom(Hash32.size)) block_votes = BlockVotes(self.reps, ratio, 0, 0, block_hash) for i, signer in enumerate(self.signers): if i == 66: break block_vote = BlockVote.new(signer, 0, 0, 0, block_hash) block_votes.add_vote(block_vote) self.assertEqual(block_votes.quorum, len(self.reps) * ratio) logging.info(block_votes) self.assertEqual(block_votes.is_completed(), False) self.assertEqual(block_votes.get_result(), None) block_vote = BlockVote.new(self.signers[99], 0, 0, 0, block_hash) block_votes.add_vote(block_vote) self.assertEqual(block_votes.is_completed(), True) self.assertEqual(block_votes.get_result(), True)
def test_block_votes_completed(self): ratio = 0.67 block_hash = Hash32(os.urandom(Hash32.size)) block_votes = BlockVotes(self.reps, ratio, 0, 0, block_hash) signers = list(enumerate(self.signers)) for i, signer in signers[:25]: block_vote = BlockVote.new(signer, 0, 0, 0, block_hash) block_votes.add_vote(block_vote) logging.info(block_votes) self.assertEqual(block_votes.is_completed(), False) self.assertEqual(block_votes.get_result(), None) for i, signer in signers[25:50]: block_vote = BlockVote.new(signer, 0, 0, 0, block_hash) block_votes.add_vote(block_vote) logging.info(block_votes) self.assertEqual(block_votes.is_completed(), False) self.assertEqual(block_votes.get_result(), None) for i, signer in signers[50:75]: block_vote = BlockVote.new(signer, 0, 0, 0, Hash32.empty()) block_votes.add_vote(block_vote) logging.info(block_votes) self.assertEqual(block_votes.is_completed(), False) self.assertEqual(block_votes.get_result(), None) for i, signer in signers[75:90]: block_vote = BlockVote.new(signer, 0, 0, 0, Hash32.empty()) block_votes.add_vote(block_vote) logging.info(block_votes) self.assertEqual(block_votes.is_completed(), True) self.assertEqual(block_votes.get_result(), False)
def test_block_vote(self): signer = self.signers[0] block_hash = Hash32(os.urandom(Hash32.size)) block_vote = BlockVote.new(signer, 0, 0, 0, block_hash) block_vote.verify() origin = f"icx_vote.blockHash.{block_vote.block_hash.hex_0x()}.blockHeight.{hex(block_vote.block_height)}." origin += f"rep.{block_vote.rep.hex_hx()}.round_.{block_vote.round_}.timestamp.{hex(block_vote.timestamp)}" origin_data = block_vote.to_origin_data(**block_vote.origin_args()) self.assertEqual( origin, vote.hash_generator.generate_salted_origin(origin_data)) self.assertEqual( Hash32(hashlib.sha3_256(origin.encode('utf-8')).digest()), block_vote.to_hash(**block_vote.origin_args()))
def vote_unconfirmed_block(self, vote_dumped: str) -> None: try: vote_serialized = json.loads(vote_dumped) except json.decoder.JSONDecodeError: util.logger.warning( f"This vote({vote_dumped}) may be from old version.") else: vote = BlockVote.deserialize(vote_serialized) util.logger.debug( f"Peer vote to : {vote.block_height}({vote.round_}) {vote.block_hash} from {vote.rep.hex_hx()}" ) self._block_manager.candidate_blocks.add_vote(vote) if self._channel_service.state_machine.state == "BlockGenerate" and \ self._block_manager.consensus_algorithm: self._block_manager.consensus_algorithm.vote(vote)