1 package consensusreward
6 log "github.com/sirupsen/logrus"
8 "github.com/vapor/consensus"
9 "github.com/vapor/errors"
10 "github.com/vapor/toolbar/apinode"
11 "github.com/vapor/toolbar/common"
12 "github.com/vapor/toolbar/consensusreward/config"
15 const standbyNodesRewardForConsensusCycle = 7610350076 // 400000000000000 / (365 * 24 * 60 / (500 * 1200 / 1000 / 60))
17 type StandbyNodeReward struct {
20 xpubRewards map[string]uint64
25 func NewStandbyNodeReward(cfg *config.Config, startHeight, endHeight uint64) *StandbyNodeReward {
26 return &StandbyNodeReward{
28 node: apinode.NewNode(cfg.NodeIP),
29 xpubRewards: make(map[string]uint64),
30 startHeight: startHeight,
35 func (s *StandbyNodeReward) getStandbyNodeReward(height uint64) error {
36 voteInfos, err := s.node.GetVoteByHeight(height)
38 return errors.Wrapf(err, "get alternative node reward")
41 voteInfos = common.CalcStandByNodes(voteInfos)
42 totalVoteNum := uint64(0)
43 for _, voteInfo := range voteInfos {
44 totalVoteNum += voteInfo.VoteNum
47 total := big.NewInt(0).SetUint64(totalVoteNum)
48 for _, voteInfo := range voteInfos {
49 amount := big.NewInt(0).SetUint64(standbyNodesRewardForConsensusCycle)
50 voteNum := big.NewInt(0).SetUint64(voteInfo.VoteNum)
51 s.xpubRewards[voteInfo.Vote] += amount.Mul(amount, voteNum).Div(amount, total).Uint64()
56 func (s *StandbyNodeReward) Settlement() error {
57 for height := s.startHeight + consensus.ActiveNetParams.RoundVoteBlockNums; height <= s.endHeight; height += consensus.ActiveNetParams.RoundVoteBlockNums {
58 if err := s.getStandbyNodeReward(height - consensus.ActiveNetParams.RoundVoteBlockNums); err != nil {
63 rewards := map[string]uint64{}
64 for _, item := range s.cfg.RewardConf.Node {
65 if reward, ok := s.xpubRewards[item.XPub]; ok {
66 rewards[item.Address] = reward
70 if len(rewards) == 0 {
74 txID, err := s.node.BatchSendBTM(s.cfg.RewardConf.AccountID, s.cfg.RewardConf.Password, rewards, []byte{})
76 log.WithFields(log.Fields{
78 "start_height": s.startHeight,
79 "end_height": s.endHeight,
80 }).Info("success on submit consensus reward transaction")