OSDN Git Service

fix review
[bytom/vapor.git] / toolbar / vote_reward / settlementvotereward / settlementreward.go
index 448fcbf..92d37ee 100644 (file)
@@ -15,16 +15,12 @@ import (
 )
 
 var (
-       errFoundReward   = errors.New("No reward found")
-       errNoStandbyNode = errors.New("No Standby Node")
-       errNoRewardTx    = errors.New("No reward transaction")
+       errNotFoundReward = errors.New("No reward found")
+       errNotStandbyNode = errors.New("No Standby Node")
+       errNotRewardTx    = errors.New("No reward transaction")
 )
 
-var (
-       consensusCycleMin                   = consensus.ActiveNetParams.BlockTimeInterval * consensus.ActiveNetParams.RoundVoteBlockNums / 1000 / 60 // the block time interval(minute) for producting 1200 block
-       consensusCycleNumPerYear            = 365 * 24 * 60 / consensusCycleMin
-       standbyNodesRewardForConsensusCycle = 400000000000000 / consensusCycleNumPerYear
-)
+const standbyNodesRewardForConsensusCycle = 7610350076 // 400000000000000 / (365 * 24 * 60 / (500 * 1200 / 1000 / 60))
 
 type voteResult struct {
        VoteAddress string
@@ -64,19 +60,17 @@ func (s *SettlementReward) getVoteResultFromDB(height uint64) (voteResults []*vo
 
 func (s *SettlementReward) Settlement() error {
        for height := s.startHeight + consensus.ActiveNetParams.RoundVoteBlockNums; height <= s.endHeight; height += consensus.ActiveNetParams.RoundVoteBlockNums {
-               coinbaseHeight := height + 1
-               totalReward, err := s.getCoinbaseReward(coinbaseHeight)
-               if err == errFoundReward {
-                       coinbaseHeight = height - consensus.ActiveNetParams.RoundVoteBlockNums
-                       totalReward, err = s.getStandbyNodeReward(height)
+               totalReward, err := s.getCoinbaseReward(height + 1)
+               if err == errNotFoundReward {
+                       totalReward, err = s.getStandbyNodeReward(height - consensus.ActiveNetParams.RoundVoteBlockNums)
                }
 
-               if err == errNoStandbyNode {
+               if err == errNotStandbyNode {
                        continue
                }
 
                if err != nil {
-                       return errors.Wrapf(err, "get total reward at coinbase_height: %d", coinbaseHeight)
+                       return errors.Wrapf(err, "get total reward at height: %d", height)
                }
 
                voteResults, err := s.getVoteResultFromDB(height)
@@ -88,7 +82,7 @@ func (s *SettlementReward) Settlement() error {
        }
 
        if len(s.rewards) == 0 {
-               return errNoRewardTx
+               return errNotRewardTx
        }
 
        // send transactions
@@ -100,27 +94,26 @@ func (s *SettlementReward) getStandbyNodeReward(height uint64) (uint64, error) {
        if err != nil {
                return 0, errors.Wrapf(err, "get alternative node reward")
        }
-       err = errNoStandbyNode
-       totalVoteNum := uint64(0)
-       xpubVoteNum := uint64(0)
+
+       voteInfos = common.CalcStandByNodes(voteInfos)
+
+       totalVoteNum, xpubVoteNum := uint64(0), uint64(0)
        for _, voteInfo := range voteInfos {
                totalVoteNum += voteInfo.VoteNum
                if s.rewardCfg.XPub == voteInfo.Vote {
                        xpubVoteNum = voteInfo.VoteNum
-                       err = nil
                }
        }
 
-       if err != nil {
-               return 0, err
+       if xpubVoteNum == 0 {
+               return 0, errNotStandbyNode
        }
+
        amount := big.NewInt(0).SetUint64(standbyNodesRewardForConsensusCycle)
        rewardRatio := big.NewInt(0).SetUint64(s.rewardCfg.RewardRatio)
        amount.Mul(amount, rewardRatio).Div(amount, big.NewInt(100))
-
        total := big.NewInt(0).SetUint64(totalVoteNum)
        voteNum := big.NewInt(0).SetUint64(xpubVoteNum)
-
        return amount.Mul(amount, voteNum).Div(amount, total).Uint64(), nil
 }
 
@@ -153,7 +146,7 @@ func (s *SettlementReward) getCoinbaseReward(height uint64) (uint64, error) {
                        return amount.Uint64(), nil
                }
        }
-       return 0, errFoundReward
+       return 0, errNotFoundReward
 }
 
 func (s *SettlementReward) calcVoterRewards(voteResults []*voteResult, totalReward uint64) {