Fixed div by zero for calculate reward.

This commit is contained in:
AnilChinchawale 2018-10-27 10:08:45 +05:30
parent 9a6c1c5382
commit 73b55f9815

View file

@ -128,14 +128,16 @@ func GetRewardForCheckpoint(chain consensus.ChainReader, blockSignerAddr common.
func CalculateRewardForSigner(chainReward *big.Int, signers map[common.Address]*rewardLog, totalSigner uint64) (map[common.Address]*big.Int, error) { func CalculateRewardForSigner(chainReward *big.Int, signers map[common.Address]*rewardLog, totalSigner uint64) (map[common.Address]*big.Int, error) {
resultSigners := make(map[common.Address]*big.Int) resultSigners := make(map[common.Address]*big.Int)
// Add reward for signers. // Add reward for signers.
for signer, rLog := range signers { if totalSigner > 0 {
// Add reward for signer. for signer, rLog := range signers {
calcReward := new(big.Int) // Add reward for signer.
calcReward.Div(chainReward, new(big.Int).SetUint64(totalSigner)) calcReward := new(big.Int)
calcReward.Mul(calcReward, new(big.Int).SetUint64(rLog.Sign)) calcReward.Div(chainReward, new(big.Int).SetUint64(totalSigner))
rLog.Reward = calcReward calcReward.Mul(calcReward, new(big.Int).SetUint64(rLog.Sign))
rLog.Reward = calcReward
resultSigners[signer] = calcReward resultSigners[signer] = calcReward
}
} }
jsonSigners, err := json.Marshal(signers) jsonSigners, err := json.Marshal(signers)
if err != nil { if err != nil {
@ -205,13 +207,18 @@ func GetRewardBalancesRate(masterAddr common.Address, totalReward *big.Int, vali
totalCap.Add(totalCap, voterCap) totalCap.Add(totalCap, voterCap)
voterCaps[voteAddr] = voterCap voterCaps[voteAddr] = voterCap
} }
for addr, voteCap := range voterCaps { if totalCap.Cmp(new(big.Int).SetInt64(0)) > 0 {
rcap := new(big.Int).Mul(totalVoterReward, voteCap) for addr, voteCap := range voterCaps {
rcap = new(big.Int).Div(rcap, totalCap) // Only valid voter has cap > 0.
if balances[addr] != nil { if voteCap.Cmp(new(big.Int).SetInt64(0)) > 0 {
balances[addr].Add(balances[addr], rcap) rcap := new(big.Int).Mul(totalVoterReward, voteCap)
} else { rcap = new(big.Int).Div(rcap, totalCap)
balances[addr] = rcap if balances[addr] != nil {
balances[addr].Add(balances[addr], rcap)
} else {
balances[addr] = rcap
}
}
} }
} }
} }