OSDN Git Service

versoin1.1.9 (#594)
[bytom/vapor.git] / toolbar / consensusreward / consensus_reward.go
1 package consensusreward
2
3 import (
4         log "github.com/sirupsen/logrus"
5
6         "github.com/bytom/vapor/consensus"
7         "github.com/bytom/vapor/errors"
8         "github.com/bytom/vapor/toolbar/apinode"
9         "github.com/bytom/vapor/toolbar/common"
10         "github.com/bytom/vapor/toolbar/consensusreward/config"
11 )
12
13 const (
14         standbyNodesRewardForConsensusCycle = 7610350076 // 400000000000000 / (365 * 24 * 60 / (500 * 1200 / 1000 / 60))
15         StandbyNodeNum                      = 32
16 )
17
18 type StandbyNodeReward struct {
19         cfg         *config.Config
20         node        *apinode.Node
21         xpubRewards map[string]uint64
22         startHeight uint64
23         endHeight   uint64
24 }
25
26 func NewStandbyNodeReward(cfg *config.Config, startHeight, endHeight uint64) *StandbyNodeReward {
27         return &StandbyNodeReward{
28                 cfg:         cfg,
29                 node:        apinode.NewNode(cfg.NodeIP),
30                 xpubRewards: make(map[string]uint64),
31                 startHeight: startHeight,
32                 endHeight:   endHeight,
33         }
34 }
35
36 func (s *StandbyNodeReward) getStandbyNodeReward(height uint64) error {
37         voteInfos, err := s.node.GetVoteByHeight(height)
38         if err != nil {
39                 return errors.Wrapf(err, "get alternative node reward")
40         }
41
42         voteInfos = common.CalcStandByNodes(voteInfos)
43         for _, voteInfo := range voteInfos {
44                 s.xpubRewards[voteInfo.Vote] += standbyNodesRewardForConsensusCycle / StandbyNodeNum
45         }
46         return nil
47 }
48
49 func (s *StandbyNodeReward) Settlement() error {
50         for height := s.startHeight + consensus.ActiveNetParams.RoundVoteBlockNums; height <= s.endHeight; height += consensus.ActiveNetParams.RoundVoteBlockNums {
51                 if err := s.getStandbyNodeReward(height - consensus.ActiveNetParams.RoundVoteBlockNums); err != nil {
52                         return err
53                 }
54         }
55
56         rewards := map[string]uint64{}
57         for _, item := range s.cfg.RewardConf.Node {
58                 if reward, ok := s.xpubRewards[item.XPub]; ok {
59                         rewards[item.Address] = reward
60                 }
61         }
62
63         if len(rewards) == 0 {
64                 return nil
65         }
66
67         txID, err := s.node.BatchSendBTM(s.cfg.RewardConf.AccountID, s.cfg.RewardConf.Password, rewards, []byte{})
68         if err == nil {
69                 log.WithFields(log.Fields{
70                         "tx_hash":      txID,
71                         "start_height": s.startHeight,
72                         "end_height":   s.endHeight,
73                 }).Info("success on submit consensus reward transaction")
74         }
75         return err
76 }