Esempio n. 1
0
    def test_update_preps_2(self):
        # Remove all main P-Reps
        term = self.term.copy()
        revision: int = 0
        _check_prep_snapshots_in_term(term)

        invalid_elected_preps: List['PRep'] = [
            prep for prep in self.preps[:PREP_MAIN_PREPS]
        ]
        term.update_invalid_elected_preps(invalid_elected_preps)
        assert len(term.main_preps) == PREP_MAIN_PREPS
        assert len(
            term.sub_preps) == PREP_MAIN_AND_SUB_PREPS - PREP_MAIN_PREPS * 2
        assert isinstance(term.root_hash, bytes)
        assert term.root_hash != self.term.root_hash

        # Remove all P-Reps except for a P-Rep
        term = self.term.copy()
        _check_prep_snapshots_in_term(term)

        invalid_elected_preps: List['PRep'] = [
            prep for prep in self.preps[1:PREP_MAIN_AND_SUB_PREPS]
        ]
        term.update_invalid_elected_preps(invalid_elected_preps)
        assert len(term.main_preps) == 1
        assert len(term.sub_preps) == 0
        assert isinstance(term.root_hash, bytes)
        assert term.root_hash != self.term.root_hash
        assert term.flags == term.flags, TermFlag.MAIN_PREPS | TermFlag.SUB_PREPS
        assert not term.flags & TermFlag.MAIN_PREP_P2P_ENDPOINT
        assert utils.is_all_flag_on(term.flags,
                                    TermFlag.MAIN_PREPS | TermFlag.SUB_PREPS)
        assert term.is_dirty()
Esempio n. 2
0
    def test_update_preps_with_sub_preps_only(self):
        revision: int = 0
        # Remove all sub P-Reps
        self.term.set_preps(self.preps, PREP_MAIN_PREPS,
                            PREP_MAIN_AND_SUB_PREPS)
        _check_prep_snapshots_in_term(self.term)

        term = self.term.copy()
        invalid_elected_preps: List['PRep'] = []
        for i in range(PREP_MAIN_PREPS, PREP_MAIN_AND_SUB_PREPS):
            prep = self.preps[i]
            prep.penalty = PenaltyReason.LOW_PRODUCTIVITY
            invalid_elected_preps.append(prep)
        assert len(
            invalid_elected_preps) == PREP_MAIN_AND_SUB_PREPS - PREP_MAIN_PREPS

        term.update_invalid_elected_preps(invalid_elected_preps)
        assert len(term.main_preps) == PREP_MAIN_PREPS
        assert len(term.sub_preps) == 0
        assert isinstance(term.root_hash, bytes)
        assert term.root_hash == self.term.root_hash
        assert not term.flags & TermFlag.MAIN_PREP_P2P_ENDPOINT
        assert not term.flags & TermFlag.MAIN_PREPS
        assert utils.is_all_flag_on(term.flags, TermFlag.SUB_PREPS)
        assert term.is_dirty()
Esempio n. 3
0
    def test_update_preps_with_block_validation_penalty(self):
        revision: int = 0
        # Remove an invalid Main P-Rep which gets a block validation penalty
        self.term.set_preps(self.preps, PREP_MAIN_PREPS,
                            PREP_MAIN_AND_SUB_PREPS)
        _check_prep_snapshots_in_term(self.term)
        assert isinstance(self.term.root_hash, bytes)
        assert len(self.term.root_hash) == 32

        term = self.term.copy()
        _check_prep_snapshots_in_term(term)
        invalid_main_prep = copy.deepcopy(self.preps[0])
        invalid_main_prep.penalty = PenaltyReason.BLOCK_VALIDATION
        invalid_elected_preps: List['PRep'] = [invalid_main_prep]

        term.update_invalid_elected_preps(invalid_elected_preps)
        _check_prep_snapshots_in_term(term)
        assert len(term.main_preps) == PREP_MAIN_PREPS
        assert len(
            term.sub_preps) == PREP_MAIN_AND_SUB_PREPS - PREP_MAIN_PREPS - len(
                invalid_elected_preps)
        assert isinstance(term.root_hash, bytes)
        assert term.root_hash != self.term.root_hash
        assert term.total_elected_prep_delegated == self.total_elected_prep_delegated - invalid_main_prep.delegated
        assert not term.flags & TermFlag.MAIN_PREP_P2P_ENDPOINT
        assert utils.is_all_flag_on(term.flags,
                                    TermFlag.MAIN_PREPS | TermFlag.SUB_PREPS)
        assert term.is_dirty()
Esempio n. 4
0
    def test_update_preps_with_unregistered_prep(self):
        revision: int = 0
        self.term.set_preps(self.preps, PREP_MAIN_PREPS,
                            PREP_MAIN_AND_SUB_PREPS)
        _check_prep_snapshots_in_term(self.term)

        assert isinstance(self.term.root_hash, bytes)
        assert len(self.term.root_hash) == 32

        term = self.term.copy()
        _check_prep_snapshots_in_term(term)
        invalid_main_prep = copy.deepcopy(self.preps[0])
        invalid_main_prep.status = PRepStatus.UNREGISTERED
        invalid_elected_preps: List['PRep'] = [invalid_main_prep]

        term.update_invalid_elected_preps(invalid_elected_preps)
        _check_prep_snapshots_in_term(term)
        assert len(term.main_preps) == PREP_MAIN_PREPS
        assert len(
            term.sub_preps) == PREP_MAIN_AND_SUB_PREPS - PREP_MAIN_PREPS - len(
                invalid_elected_preps)
        assert isinstance(term.root_hash, bytes)
        assert term.root_hash != self.term.root_hash
        assert term.total_elected_prep_delegated == self.total_elected_prep_delegated - invalid_main_prep.delegated
        assert not term.flags & TermFlag.MAIN_PREP_P2P_ENDPOINT
        assert utils.is_all_flag_on(term.flags,
                                    TermFlag.MAIN_PREPS | TermFlag.SUB_PREPS)
        assert term.is_dirty()
Esempio n. 5
0
    def test_update_preps_with_critical_penalty(self):
        revision: int = 0
        # Remove an invalid Main P-Rep which gets a penalty
        penalties = [
            PenaltyReason.LOW_PRODUCTIVITY, PenaltyReason.PREP_DISQUALIFICATION
        ]

        for penalty in penalties:
            self.term.set_preps(self.preps, PREP_MAIN_PREPS,
                                PREP_MAIN_AND_SUB_PREPS)
            _check_prep_snapshots_in_term(self.term)
            assert isinstance(self.term.root_hash, bytes)
            assert len(self.term.root_hash) == 32

            term = self.term.copy()
            _check_prep_snapshots_in_term(term)
            invalid_main_prep = copy.deepcopy(self.preps[0])
            invalid_main_prep.penalty = penalty
            invalid_elected_preps: List['PRep'] = [invalid_main_prep]

            term.update_invalid_elected_preps(invalid_elected_preps)

            _check_prep_snapshots_in_term(term)
            assert len(term.main_preps) == PREP_MAIN_PREPS
            assert len(term.sub_preps
                       ) == PREP_MAIN_AND_SUB_PREPS - PREP_MAIN_PREPS - len(
                           invalid_elected_preps)
            assert isinstance(term.root_hash, bytes)
            assert term.root_hash != self.term.root_hash
            assert term.total_elected_prep_delegated == self.total_elected_prep_delegated - invalid_main_prep.delegated
            assert not term.flags & TermFlag.MAIN_PREP_P2P_ENDPOINT
            assert utils.is_all_flag_on(
                term.flags, TermFlag.MAIN_PREPS | TermFlag.SUB_PREPS)
            assert term.is_dirty()