Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #390 from icon-project/IS-932-impose-low-productiv…
Browse files Browse the repository at this point in the history
…ity-penalty-when-prep-get-block-validation-and-low-productivity-at-the-same-times

IS-932: Impose low productivity penalty when prep get block validation and low productivity penalty at the same time
  • Loading branch information
Chiwon Cho authored Nov 28, 2019
2 parents 02512b0 + daf8a68 commit c89d89d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
10 changes: 5 additions & 5 deletions iconservice/prep/penalty_imposer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions tests/prep/test_penalty_imposer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit c89d89d

Please sign in to comment.