1 package consensusreward
6 "github.com/vapor/consensus"
7 "github.com/vapor/errors"
8 "github.com/vapor/toolbar/apinode"
9 "github.com/vapor/toolbar/common"
10 "github.com/vapor/toolbar/consensusreward/config"
13 const standbyNodesRewardForConsensusCycle = 7610350076 // 400000000000000 / (365 * 24 * 60 / (500 * 1200 / 1000 / 60))
15 type StandbyNodeReward struct {
18 xpubRewards map[string]uint64
23 func NewStandbyNodeReward(cfg *config.Config, startHeight, endHeight uint64) *StandbyNodeReward {
24 return &StandbyNodeReward{
26 node: apinode.NewNode(cfg.NodeIP),
27 xpubRewards: make(map[string]uint64),
28 startHeight: startHeight,
33 func (s *StandbyNodeReward) getStandbyNodeReward(height uint64) error {
34 voteInfos, err := s.node.GetVoteByHeight(height)
36 return errors.Wrapf(err, "get alternative node reward")
39 voteInfos = common.CalcStandByNodes(voteInfos)
40 totalVoteNum := uint64(0)
41 for _, voteInfo := range voteInfos {
42 totalVoteNum += voteInfo.VoteNum
45 total := big.NewInt(0).SetUint64(totalVoteNum)
46 for _, voteInfo := range voteInfos {
47 amount := big.NewInt(0).SetUint64(standbyNodesRewardForConsensusCycle)
48 voteNum := big.NewInt(0).SetUint64(voteInfo.VoteNum)
49 s.xpubRewards[voteInfo.Vote] += amount.Mul(amount, voteNum).Div(amount, total).Uint64()
54 func (s *StandbyNodeReward) Settlement() error {
55 for height := s.startHeight; height < s.endHeight; height += consensus.ActiveNetParams.RoundVoteBlockNums {
56 if err := s.getStandbyNodeReward(height); err != nil {
60 rewards := map[string]uint64{}
61 for _, item := range s.cfg.RewardConf.Node {
62 if reward, ok := s.xpubRewards[item.XPub]; ok {
63 rewards[item.Address] = reward
67 if len(rewards) == 0 {
70 return s.node.BatchSendBTM(s.cfg.RewardConf.AccountID, s.cfg.RewardConf.Password, rewards)