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

Commit

Permalink
Impose low productivity penalty when prep get block validation and lo…
Browse files Browse the repository at this point in the history
…w productivity penalty at the same time
  • Loading branch information
soobokjin committed Nov 28, 2019
1 parent 698996a commit daf8a68
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 daf8a68

Please sign in to comment.