github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/state/reward.go (about)

     1  package state
     2  
     3  import (
     4  	"encoding/hex"
     5  
     6  	"github.com/bytom/bytom/consensus"
     7  	"github.com/bytom/bytom/protocol/bc/types"
     8  )
     9  
    10  func (c *Checkpoint) validatorReward() uint64 {
    11  	if pledgeRate := c.pledgeRate(); pledgeRate <= consensus.RewardThreshold {
    12  		return uint64((pledgeRate + consensus.RewardThreshold) * float64(consensus.BlockReward))
    13  	}
    14  
    15  	return consensus.BlockReward
    16  }
    17  
    18  // pledgeRate validator's pledge rate
    19  func (c *Checkpoint) pledgeRate() float64 {
    20  	var totalVotes uint64
    21  	for _, vote := range c.Votes {
    22  		totalVotes += vote
    23  	}
    24  
    25  	totalSupply := c.Height*consensus.BlockReward/2 + consensus.InitBTMSupply
    26  	return float64(totalVotes) / float64(totalSupply)
    27  }
    28  
    29  // applyValidatorReward calculate the coinbase reward for validator
    30  func (c *Checkpoint) applyValidatorReward(block *types.Block) {
    31  	validatorScript := hex.EncodeToString(block.Transactions[0].Outputs[0].ControlProgram)
    32  	for _, tx := range block.Transactions {
    33  		c.Rewards[validatorScript] += tx.Fee()
    34  	}
    35  
    36  	c.Rewards[validatorScript] += c.validatorReward()
    37  }