Skip to content

Commit

Permalink
Merge pull request #200 from icon-project/199-panic-division-by-zero-…
Browse files Browse the repository at this point in the history
…error

Fix #199 panic: division by zero error
  • Loading branch information
eunsoo-icon authored Jan 24, 2024
2 parents 7263acb + f7e6eef commit d3aa5a8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
14 changes: 9 additions & 5 deletions icon/iiss/calculator/iiss4.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ func (r *iiss4Reward) Calculate() error {
return err
}

if err = r.processPrepReward(); err != nil {
return err
}
if r.g.GetElectedPRepCount() == 0 {
r.Logger().Debugf("there is no elected PRep. skip reward calculation")
} else {
if err = r.processPrepReward(); err != nil {
return err
}

if err = r.processVoterReward(); err != nil {
return err
if err = r.processVoterReward(); err != nil {
return err
}
}

if err = processBTP(r); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions icon/iiss/calculator/iiss4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,18 @@ func TestReward(t *testing.T) {
}
})

// no elected PReps
org := r.pi.electedPRepCount
r.pi.electedPRepCount = 0
err = r.processPrepReward()
assert.NoError(t, err)
for _, p := range r.pi.rank {
assert.Equal(t, 0, p.commission.Sign())
assert.Equal(t, 0, p.voterReward.Sign())
assert.Equal(t, 0, p.wage.Sign())
}
r.pi.electedPRepCount = org

// processPrepReward()
err = r.processPrepReward()
assert.NoError(t, err)
Expand Down
4 changes: 4 additions & 0 deletions icon/iiss/calculator/prep.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ func fundToPeriodIScore(reward *big.Int, period int64) *big.Int {
// CalculateReward calculates commission, wage and voter reward of the PRep.
func (p *PRepInfo) CalculateReward(totalReward, totalMinWage, minBond *big.Int) error {
p.log.Debugf("CalculateReward()")
if p.electedPRepCount == 0 {
p.log.Debugf("skip PRepInfo.CalculateReward()")
return nil
}
tReward := fundToPeriodIScore(totalReward, p.GetTermPeriod())
minWage := fundToPeriodIScore(totalMinWage, p.GetTermPeriod())
p.log.Debugf("RewardFund: PRep: %d, wage: %d", tReward, minWage)
Expand Down
8 changes: 8 additions & 0 deletions icon/iiss/calculator/prep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,11 @@ func prepReward(prep *PRep, totalReward, totalPower int64, offsetLimit int) (rew
commission = prep.commissionRate.MulInt64(reward)
return
}

func TestPRepInfo_CalculateReward_without_elected_prep(t *testing.T) {
pInfo := newTestPRepInfo(nil, icmodule.ToRate(5), 100, 0)
assert.NotPanics(t, func() {
err := pInfo.CalculateReward(big.NewInt(1_000), big.NewInt(1_000), big.NewInt(300))
assert.NoError(t, err)
})
}

0 comments on commit d3aa5a8

Please sign in to comment.