Example #1
0
def context(settable_inv_container: INVContainer):
    prep_engine = PRepEngine()
    prep_engine.prep_address_converter = mock.Mock()
    inv_engine = INVEngine()
    settable_inv_container.set_inv(StepPrice(10**10))
    settable_inv_container.set_inv(StepCosts(STEP_COSTS))
    settable_inv_container.set_inv(
        MaxStepLimits({IconScoreContextType.INVOKE: 2_500_000_000}))
    settable_inv_container.set_inv(RevisionCode(Revision.THREE.value))
    inv_engine._inv_container = settable_inv_container

    IconScoreContext.engine = ContextEngine(prep=prep_engine, inv=inv_engine)
    context_factory = IconScoreContextFactory()

    block = Block(block_height=1,
                  block_hash=b"1" * 40,
                  prev_hash=b"0" * 40,
                  timestamp=0)
    context = context_factory.create(IconScoreContextType.INVOKE, block)

    step_limit = 1_000_000_000
    context.set_step_counter(step_limit)

    ContextContainer._push_context(context)
    yield context
    ContextContainer._pop_context()
Example #2
0
    def test__reset_block_validation_penalty(self):
        engine = PRepEngine()
        engine.term = self.term
        engine.preps = self.preps
        engine.prep_address_converter = Mock()

        self.term.freeze()
        self.preps.freeze()

        assert engine.term.is_frozen()
        assert engine.preps.is_frozen()

        IconScoreContext.engine = Mock()
        IconScoreContext.storage = Mock()
        IconScoreContext.engine.prep = engine
        context = _create_context()

        assert engine.preps.size(active_prep_only=False) == context.preps.size(active_prep_only=False)
        for i in range(engine.preps.size(active_prep_only=True)):
            assert engine.preps.get_by_index(i) == context._preps.get_by_index(i)

        # Impose block validation penalty on 5 Main P-Reps
        indices = set()
        for _ in range(5):
            index = random.randint(0, self.main_prep_count - 1)
            indices.add(index)

        # Impose the block validation penalty on randomly chosen 5 or less than P-Reps
        for i in indices:
            prep = context.preps.get_by_index(i)
            dirty_prep = context.get_prep(prep.address, mutable=True)
            assert not dirty_prep.is_frozen()

            dirty_prep.penalty = PenaltyReason.BLOCK_VALIDATION
            dirty_prep.grade = PRepGrade.CANDIDATE

            context.put_dirty_prep(dirty_prep)

        context.update_dirty_prep_batch()

        for i in indices:
            prep = context.preps.get_by_index(i)
            assert prep.is_frozen()
            assert prep.status == PRepStatus.ACTIVE
            assert prep.penalty == PenaltyReason.BLOCK_VALIDATION
            assert prep.grade == PRepGrade.CANDIDATE

        engine._reset_block_validation_penalty(context)

        # The penalties of P-Reps in context should be reset
        # to PenaltyReason.NONE at the last block of the current term
        for prep in context.preps:
            assert prep.status == PRepStatus.ACTIVE
            assert prep.penalty == PenaltyReason.NONE

        for i in indices:
            prep = context.preps.get_by_index(i)
            assert prep.status == PRepStatus.ACTIVE
            assert prep.penalty == PenaltyReason.NONE