From daf8a6897e4064395a0d41b53ff99a308f22c11b Mon Sep 17 00:00:00 2001 From: soobokjin Date: Thu, 28 Nov 2019 12:36:19 +0900 Subject: [PATCH] Impose low productivity penalty when prep get block validation and low productivity penalty at the same time --- iconservice/prep/penalty_imposer.py | 10 +++++----- tests/prep/test_penalty_imposer.py | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/iconservice/prep/penalty_imposer.py b/iconservice/prep/penalty_imposer.py index 94eede77f..b2224e0fa 100644 --- a/iconservice/prep/penalty_imposer.py +++ b/iconservice/prep/penalty_imposer.py @@ -52,16 +52,16 @@ def run(self, on_penalty_imposed: Callable[['IconScoreContext', 'Address', 'PenaltyReason'], None]) -> 'PenaltyReason': reason: 'PenaltyReason' = PenaltyReason.NONE - if self._check_low_productivity_penalty(prep): - Logger.info(f"PenaltyImposer statistics({PenaltyReason.LOW_PRODUCTIVITY}): " - f"prep_total_blocks: {prep.total_blocks} " - f"prep_unvalidated_sequence_blocks: {prep.unvalidated_sequence_blocks}") - reason = PenaltyReason.LOW_PRODUCTIVITY if self._check_block_validation_penalty(prep): Logger.info(f"PenaltyImposer statistics({PenaltyReason.BLOCK_VALIDATION}): " f"prep_total_blocks: {prep.total_blocks} " f"prep_block_validation_proportion: {prep.block_validation_proportion}") reason = PenaltyReason.BLOCK_VALIDATION + if self._check_low_productivity_penalty(prep): + Logger.info(f"PenaltyImposer statistics({PenaltyReason.LOW_PRODUCTIVITY}): " + f"prep_total_blocks: {prep.total_blocks} " + f"prep_unvalidated_sequence_blocks: {prep.unvalidated_sequence_blocks}") + reason = PenaltyReason.LOW_PRODUCTIVITY if on_penalty_imposed and reason != PenaltyReason.NONE: on_penalty_imposed(context, prep.address, reason) diff --git a/tests/prep/test_penalty_imposer.py b/tests/prep/test_penalty_imposer.py index 8736f69f0..d07fe8ed4 100644 --- a/tests/prep/test_penalty_imposer.py +++ b/tests/prep/test_penalty_imposer.py @@ -176,3 +176,28 @@ def test_low_productivity_penalty(self): context=self.context, prep=prep, on_penalty_imposed=on_penalty_imposed) on_penalty_imposed.assert_called_with( self.context, prep.address, PenaltyReason.LOW_PRODUCTIVITY) + + def test_block_validation_and_low_productivity_penalty(self): + # Success case: when prep get block validation and low productivity penalty at the same time, + # should impose low productivity penalty + penalty_grace_period = 43120 * 2 + block_validation_penalty_threshold = 660 + low_productivity_penalty_threshold = 85 + + total_blocks = penalty_grace_period + 1 + unvalidated_sequence_blocks = block_validation_penalty_threshold + validated_blocks = penalty_grace_period * low_productivity_penalty_threshold // 100 + + prep = create_prep(total_blocks, validated_blocks, unvalidated_sequence_blocks) + + on_penalty_imposed = MagicMock() + penalty_imposer = PenaltyImposer( + penalty_grace_period=penalty_grace_period, + low_productivity_penalty_threshold=low_productivity_penalty_threshold, + block_validation_penalty_threshold=block_validation_penalty_threshold + ) + actual_reason: 'PenaltyReason' = penalty_imposer.run( + context=self.context, prep=prep, on_penalty_imposed=on_penalty_imposed) + on_penalty_imposed.assert_called_with( + self.context, prep.address, PenaltyReason.LOW_PRODUCTIVITY) + self.assertEqual(PenaltyReason.LOW_PRODUCTIVITY, actual_reason)