Fixed div by zero for calculate reward.

This commit is contained in:
DinhLN 2018-08-10 14:13:46 +07:00
parent e96a1819c5
commit 682b462b4f

View file

@ -143,6 +143,7 @@ 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.
if totalSigner > 0 {
for signer, rLog := range signers { for signer, rLog := range signers {
// Add reward for signer. // Add reward for signer.
calcReward := new(big.Int) calcReward := new(big.Int)
@ -152,6 +153,7 @@ func CalculateRewardForSigner(chainReward *big.Int, signers map[common.Address]*
resultSigners[signer] = calcReward resultSigners[signer] = calcReward
} }
}
jsonSigners, err := json.Marshal(signers) jsonSigners, err := json.Marshal(signers)
if err != nil { if err != nil {
log.Error("Fail to parse json signers", "error", err) log.Error("Fail to parse json signers", "error", err)
@ -220,7 +222,10 @@ func GetRewardBalancesRate(masterAddr common.Address, totalReward *big.Int, vali
totalCap.Add(totalCap, voterCap) totalCap.Add(totalCap, voterCap)
voterCaps[voteAddr] = voterCap voterCaps[voteAddr] = voterCap
} }
if totalCap.Cmp(new(big.Int).SetInt64(0)) > 0 {
for addr, voteCap := range voterCaps { for addr, voteCap := range voterCaps {
// Only valid voter has cap > 0.
if voteCap.Cmp(new(big.Int).SetInt64(0)) > 0 {
rcap := new(big.Int).Mul(totalVoterReward, voteCap) rcap := new(big.Int).Mul(totalVoterReward, voteCap)
rcap = new(big.Int).Div(rcap, totalCap) rcap = new(big.Int).Div(rcap, totalCap)
if balances[addr] != nil { if balances[addr] != nil {
@ -230,6 +235,8 @@ func GetRewardBalancesRate(masterAddr common.Address, totalReward *big.Int, vali
} }
} }
} }
}
}
foudationReward := new(big.Int).Mul(totalReward, new(big.Int).SetInt64(RewardFoundationPercent)) foudationReward := new(big.Int).Mul(totalReward, new(big.Int).SetInt64(RewardFoundationPercent))
foudationReward = new(big.Int).Div(foudationReward, new(big.Int).SetInt64(100)) foudationReward = new(big.Int).Div(foudationReward, new(big.Int).SetInt64(100))