示例#1
0
    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)
示例#2
0
    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)
示例#3
0
    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
示例#4
0
    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)
示例#5
0
    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)
示例#6
0
    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()))