Exemplo n.º 1
0
    def test_leader_votes_completed_with_out_of_round(self):
        ratio = 0.51
        old_leader = self.reps[0]
        next_leader = self.reps[1]
        by_higher_rounder = ExternalAddress.empty()

        leader_votes = LeaderVotes(self.reps, ratio, 0, 0, old_leader)
        for i, (rep,
                signer) in enumerate(zip(self.reps[:26], self.signers[:26])):
            leader_vote = LeaderVote.new(signer, 0, 0, 0, old_leader,
                                         next_leader)
            leader_votes.add_vote(leader_vote)

        leader_votes.get_summary()
        print(f"leader_votes.is_completed(): {leader_votes.is_completed()}")
        print(f"leader_votes.get_result(): {leader_votes.get_result()}")
        self.assertEqual(leader_votes.is_completed(), False)
        self.assertEqual(leader_votes.get_result(), None)

        for i, (rep,
                signer) in enumerate(zip(self.reps[26:55],
                                         self.signers[26:55])):
            leader_vote = LeaderVote.new(signer, 0, 0, 0, old_leader,
                                         by_higher_rounder)
            leader_votes.add_vote(leader_vote)

        leader_votes.get_summary()
        print(f"leader_votes.is_completed(): {leader_votes.is_completed()}")
        print(f"leader_votes.get_result(): {leader_votes.get_result()}")
        self.assertEqual(leader_votes.is_completed(), True)
        self.assertEqual(leader_votes.get_result(), next_leader)
Exemplo n.º 2
0
    def test_leader_votes_completed(self):
        ratio = 0.67
        old_leader = self.reps[0]
        new_leaders = [self.reps[1], self.reps[2]]

        leader_votes = LeaderVotes(self.reps, ratio, 0, 0, old_leader)
        for i, (rep,
                signer) in enumerate(zip(self.reps[:25], self.signers[:25])):
            new_leader = new_leaders[0]
            leader_vote = LeaderVote.new(signer, 0, 0, 0, old_leader,
                                         new_leader)
            leader_votes.add_vote(leader_vote)

        self.assertEqual(leader_votes.is_completed(), False)
        self.assertEqual(leader_votes.get_result(), None)

        for i, (rep,
                signer) in enumerate(zip(self.reps[25:50],
                                         self.signers[25:50])):
            new_leader = new_leaders[1]
            leader_vote = LeaderVote.new(signer, 0, 0, 0, old_leader,
                                         new_leader)
            leader_votes.add_vote(leader_vote)

        self.assertEqual(leader_votes.is_completed(), False)
        self.assertEqual(leader_votes.get_result(), None)

        for i, (rep,
                signer) in enumerate(zip(self.reps[50:75],
                                         self.signers[50:75])):
            new_leader = new_leaders[0]
            leader_vote = LeaderVote.new(signer, 0, 0, 0, old_leader,
                                         new_leader)
            leader_votes.add_vote(leader_vote)

        self.assertEqual(leader_votes.is_completed(), False)
        self.assertEqual(leader_votes.get_result(), None)

        for i, (rep,
                signer) in enumerate(zip(self.reps[75:90],
                                         self.signers[75:90])):
            new_leader = new_leaders[1]
            leader_vote = LeaderVote.new(signer, 0, 0, 0, old_leader,
                                         new_leader)
            leader_votes.add_vote(leader_vote)

        self.assertEqual(leader_votes.is_completed(), True)
        self.assertEqual(leader_votes.get_result(), None)
Exemplo n.º 3
0
    def leader_complain(self):
        complained_leader_id, new_leader_id = self.get_leader_ids_for_complaint(
        )
        leader_vote = LeaderVote.new(
            signer=ChannelProperty().peer_auth,
            block_height=self.epoch.height,
            round_=self.epoch.round,
            old_leader=ExternalAddress.fromhex_address(complained_leader_id),
            new_leader=ExternalAddress.fromhex_address(new_leader_id),
            timestamp=util.get_time_stamp())
        util.logger.info(
            f"LeaderVote : old_leader({complained_leader_id}), new_leader({new_leader_id}), round({self.epoch.round})"
        )
        self.add_complain(leader_vote)

        leader_vote_serialized = leader_vote.serialize()
        leader_vote_dumped = json.dumps(leader_vote_serialized)
        request = loopchain_pb2.ComplainLeaderRequest(
            complain_vote=leader_vote_dumped, channel=self.channel_name)

        util.logger.debug(f"leader complain "
                          f"complained_leader_id({complained_leader_id}), "
                          f"new_leader_id({new_leader_id})")

        reps_hash = self.blockchain.get_next_reps_hash_by_header(
            self.blockchain.last_block.header)
        self.__channel_service.broadcast_scheduler.schedule_broadcast(
            "ComplainLeader", request, reps_hash=reps_hash)
Exemplo n.º 4
0
    def __send_fail_leader_vote(self, leader_vote: LeaderVote):
        fail_vote = LeaderVote.new(signer=ChannelProperty().peer_auth,
                                   block_height=leader_vote.block_height,
                                   round_=leader_vote.round_,
                                   old_leader=leader_vote.old_leader,
                                   new_leader=ExternalAddress.empty(),
                                   timestamp=util.get_time_stamp())

        util.logger.info(
            f"FailLeaderVote : to {leader_vote.old_leader}, round({leader_vote.round_})"
        )
        fail_vote_dumped = json.dumps(fail_vote.serialize())
        request = loopchain_pb2.ComplainLeaderRequest(
            complain_vote=fail_vote_dumped, channel=self.channel_name)

        reps_hash = (self.blockchain.last_block.header.revealed_next_reps_hash
                     or self.__channel_service.peer_manager.prepared_reps_hash)
        rep_id = leader_vote.rep.hex_hx()
        target = self.blockchain.find_preps_targets_by_roothash(
            reps_hash)[rep_id]

        util.logger.debug(f"fail leader complain "
                          f"complained_leader_id({leader_vote.old_leader}), "
                          f"new_leader_id({ExternalAddress.empty()}),"
                          f"round({leader_vote.round_}),"
                          f"target({target})")

        self.__channel_service.broadcast_scheduler.schedule_send_failed_leader_complain(
            "ComplainLeader", request, target=target)
Exemplo n.º 5
0
 def deserialize(cls, votes_data: List[Dict], voting_ratio: float):
     if votes_data:
         votes = [LeaderVote.deserialize(vote_data) for vote_data in votes_data]
         reps = [vote.rep for vote in votes]
         votes_instance = cls(reps, voting_ratio, votes[0].block_height, votes[0].round_, votes[0].old_leader)
         for vote in votes:
             index = reps.index(vote.rep)
             votes_instance.votes[index] = vote
         return votes_instance
     else:
         return cls([], voting_ratio, -1, -1, ExternalAddress.empty())
Exemplo n.º 6
0
    def test_leader_vote(self):
        signer = self.signers[0]
        leader_vote = LeaderVote.new(signer, 0, 0, 0, self.reps[0],
                                     self.reps[1])
        leader_vote.verify()

        origin = f"icx_vote.blockHeight.{hex(leader_vote.block_height)}."
        origin += f"newLeader.{leader_vote.new_leader.hex_hx()}.oldLeader.{leader_vote.old_leader.hex_hx()}."
        origin += f"rep.{leader_vote.rep.hex_hx()}.round_.{leader_vote.round_}.timestamp.{hex(leader_vote.timestamp)}"

        origin_data = leader_vote.to_origin_data(**leader_vote.origin_args())
        print(str(vote.hash_generator.generate_salted_origin(origin_data)))
        self.assertEqual(
            origin, vote.hash_generator.generate_salted_origin(origin_data))

        self.assertEqual(
            Hash32(hashlib.sha3_256(origin.encode('utf-8')).digest()),
            leader_vote.to_hash(**leader_vote.origin_args()))
Exemplo n.º 7
0
    def test_leader_votes(self):
        ratio = 0.67
        old_leader = self.reps[0]
        new_leaders = [self.reps[1], self.reps[2], self.reps[3], self.reps[4]]

        leader_votes = LeaderVotes(self.reps, ratio, 0, 0, old_leader)
        for i, (rep, signer) in enumerate(zip(self.reps, self.signers)):
            mod = i % 10
            if mod < 1:
                new_leader = new_leaders[1]
            elif mod < 2:
                new_leader = new_leaders[2]
            elif mod < 3:
                new_leader = new_leaders[3]
            else:
                new_leader = new_leaders[0]
            leader_vote = LeaderVote.new(signer, 0, 0, 0, old_leader,
                                         new_leader)
            leader_votes.add_vote(leader_vote)

        logging.info(leader_votes)
        self.assertEqual(leader_votes.is_completed(), True)
        self.assertEqual(leader_votes.get_result(), new_leaders[0])
Exemplo n.º 8
0
    def test_leader_invalid_vote(self):
        ratio = 0.67

        old_leader = self.reps[0]
        new_leader = self.reps[1]
        leader_votes = LeaderVotes(self.reps, ratio, 0, 0, old_leader)

        invalid_leader_vote = LeaderVote.new(self.signers[0], 0, 1, 0,
                                             old_leader, new_leader)
        self.assertRaises(RuntimeError, leader_votes.add_vote,
                          invalid_leader_vote)

        invalid_leader_vote = LeaderVote.new(self.signers[0], 0, 0, 1,
                                             old_leader, new_leader)
        self.assertRaises(RuntimeError, leader_votes.add_vote,
                          invalid_leader_vote)

        invalid_leader_vote = LeaderVote.new(self.signers[0], 0, 0, 0,
                                             new_leader, new_leader)
        self.assertRaises(RuntimeError, leader_votes.add_vote,
                          invalid_leader_vote)

        invalid_leader_vote = LeaderVote(rep=self.reps[0],
                                         timestamp=0,
                                         signature=Signature(os.urandom(65)),
                                         block_height=0,
                                         round_=0,
                                         new_leader=new_leader,
                                         old_leader=old_leader)
        self.assertRaises(RuntimeError, leader_votes.add_vote,
                          invalid_leader_vote)

        leader_vote = LeaderVote.new(self.signers[0], 0, 0, 0, old_leader,
                                     new_leader)
        leader_votes.add_vote(leader_vote)
        duplicate_leader_vote = LeaderVote.new(self.signers[0], 0, 0, 0,
                                               old_leader, self.reps[2])
        self.assertRaises(votes.VoteDuplicateError, leader_votes.add_vote,
                          duplicate_leader_vote)
Exemplo n.º 9
0
 async def complain_leader(self, vote_dumped: str) -> None:
     vote_serialized = json.loads(vote_dumped)
     vote = LeaderVote.deserialize(vote_serialized)
     self._block_manager.add_complain(vote)