Example #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)
Example #2
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)
Example #3
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)
Example #4
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])