OSDN Git Service

Alternate nodes assign rewards
authormars <mars@bytom.io>
Wed, 24 Jul 2019 08:16:02 +0000 (16:16 +0800)
committermars <mars@bytom.io>
Wed, 24 Jul 2019 08:16:02 +0000 (16:16 +0800)
cmd/votereward/README.md
toolbar/vote_reward/config/config.go
toolbar/vote_reward/settlementvotereward/settlementreward.go

index ee6c5d3..8db7237 100644 (file)
@@ -19,7 +19,8 @@ A `reward.json` would look like this:
     "account_id": "bd775113-49e0-4678-94bf-2b853f1afe80",
     "password": "123456",
     "reward_ratio": 20,
-    "mining_address": "sp1qfpgjve27gx0r9t7vud8vypplkzytgrvqr74rwz"
+    "mining_address": "sp1qfpgjve27gx0r9t7vud8vypplkzytgrvqr74rwz",
+    "alternative_nodes_reward_consensus_cycle": 0
   }
 }
 ```
index d15b3c6..576f73e 100644 (file)
@@ -22,11 +22,12 @@ func ConfigFile() string {
 }
 
 type RewardConfig struct {
-       XPub          string `json:"xpub"`
-       AccountID     string `json:"account_id"`
-       Password      string `json:"password"`
-       MiningAddress string `json:"mining_address"`
-       RewardRatio   uint64 `json:"reward_ratio"`
+       XPub                                    string `json:"xpub"`
+       AccountID                               string `json:"account_id"`
+       Password                                string `json:"password"`
+       MiningAddress                           string `json:"mining_address"`
+       RewardRatio                             uint64 `json:"reward_ratio"`
+       AlternativeNodesRewardForConsensusCycle uint64 `json:"alternative_nodes_reward_consensus_cycle"`
 }
 
 func ExportConfigFile(configFile string, config *Config) error {
index c367dae..8f5dff1 100644 (file)
@@ -14,6 +14,8 @@ import (
        "github.com/vapor/toolbar/vote_reward/config"
 )
 
+var ErrFoundReward = errors.New("No reward found")
+
 type voteResult struct {
        VoteAddress string
        VoteNum     uint64
@@ -54,6 +56,11 @@ 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 {
+                       totalReward, err = s.getAlternativeNodeReward(height)
+                       coinbaseHeight = height
+               }
+
                if err != nil {
                        return errors.Wrapf(err, "get total reward at coinbase_height: %d", coinbaseHeight)
                }
@@ -70,6 +77,27 @@ func (s *SettlementReward) Settlement() error {
        return s.node.BatchSendBTM(s.rewardCfg.AccountID, s.rewardCfg.Password, s.rewards)
 }
 
+func (s *SettlementReward) getAlternativeNodeReward(height uint64) (uint64, error) {
+       voteInfos, err := s.node.GetVoteByHeight(height)
+       if err != nil {
+               return 0, errors.Wrapf(err, "get alternative node reward")
+       }
+       totalVoteNum := uint64(0)
+       xpubVoteNum := uint64(0)
+       for _, voteInfo := range voteInfos {
+               totalVoteNum += voteInfo.VoteNum
+               if s.rewardCfg.XPub == voteInfo.Vote {
+                       xpubVoteNum = voteInfo.VoteNum
+               }
+       }
+
+       total := big.NewInt(0).SetUint64(totalVoteNum)
+       voteNum := big.NewInt(0).SetUint64(xpubVoteNum)
+       totalReward := big.NewInt(0).SetUint64(s.rewardCfg.AlternativeNodesRewardForConsensusCycle)
+
+       return total.Mul(total, totalReward).Div(total, voteNum).Uint64(), nil
+}
+
 func (s *SettlementReward) getCoinbaseReward(height uint64) (uint64, error) {
        block, err := s.node.GetBlockByHeight(height)
        if err != nil {
@@ -99,7 +127,7 @@ func (s *SettlementReward) getCoinbaseReward(height uint64) (uint64, error) {
                        return amount.Uint64(), nil
                }
        }
-       return 0, errors.New("No reward found")
+       return 0, ErrFoundReward
 }
 
 func (s *SettlementReward) calcVoterRewards(voteResults []*voteResult, totalReward uint64) {