/
reward_distributor.go
151 lines (125 loc) · 4.84 KB
/
reward_distributor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2019 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
package reward
import (
"math/big"
"github.com/klaytn/klaytn/blockchain/types"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/log"
)
var logger = log.NewModuleLogger(log.Reward)
type BalanceAdder interface {
AddBalance(addr common.Address, v *big.Int)
}
type governanceHelper interface {
Epoch() uint64
GetItemAtNumberByIntKey(num uint64, key int) (interface{}, error)
DeferredTxFee() bool
ProposerPolicy() uint64
StakingUpdateInterval() uint64
GetMinimumStakingAtNumber(num uint64) (uint64, error)
}
type RewardDistributor struct {
rcc *rewardConfigCache
gh governanceHelper
}
func NewRewardDistributor(gh governanceHelper) *RewardDistributor {
return &RewardDistributor{
rcc: newRewardConfigCache(gh),
gh: gh,
}
}
// getTotalTxFee returns the total transaction gas fee of the block.
func (rd *RewardDistributor) getTotalTxFee(header *types.Header, rewardConfig *rewardConfig) *big.Int {
totalGasUsed := big.NewInt(0).SetUint64(header.GasUsed)
if header.BaseFee != nil {
// magma hardfork
return totalGasUsed.Mul(totalGasUsed, header.BaseFee)
} else {
return totalGasUsed.Mul(totalGasUsed, rewardConfig.unitPrice)
}
}
func (rd *RewardDistributor) txFeeBurning(txFee *big.Int) *big.Int {
return txFee.Div(txFee, big.NewInt(2))
}
// MintKLAY mints KLAY and gives the KLAY and the total transaction gas fee to the block proposer.
func (rd *RewardDistributor) MintKLAY(b BalanceAdder, header *types.Header) error {
rewardConfig, err := rd.rcc.get(header.Number.Uint64())
if err != nil {
return err
}
totalTxFee := rd.getTotalTxFee(header, rewardConfig)
if header.BaseFee != nil {
// magma hardfork
totalTxFee = totalTxFee.Add(rewardConfig.mintingAmount, rd.txFeeBurning(totalTxFee))
} else {
totalTxFee = totalTxFee.Add(rewardConfig.mintingAmount, totalTxFee)
}
b.AddBalance(header.Rewardbase, totalTxFee)
return nil
}
// DistributeBlockReward distributes block reward to proposer, kirAddr and pocAddr.
func (rd *RewardDistributor) DistributeBlockReward(b BalanceAdder, header *types.Header, pocAddr common.Address, kirAddr common.Address) error {
rewardConfig, err := rd.rcc.get(header.Number.Uint64())
if err != nil {
return err
}
// Calculate total tx fee
totalTxFee := common.Big0
if rd.gh.DeferredTxFee() {
totalTxFee = rd.getTotalTxFee(header, rewardConfig)
// magma hardfork
if header.BaseFee != nil {
totalTxFee = rd.txFeeBurning(totalTxFee)
}
}
rd.distributeBlockReward(b, header, totalTxFee, rewardConfig, pocAddr, kirAddr)
return nil
}
// distributeBlockReward mints KLAY and distributes newly minted KLAY and transaction fee to proposer, kirAddr and pocAddr.
func (rd *RewardDistributor) distributeBlockReward(b BalanceAdder, header *types.Header, totalTxFee *big.Int, rewardConfig *rewardConfig, pocAddr common.Address, kirAddr common.Address) {
proposer := header.Rewardbase
// Block reward
blockReward := big.NewInt(0).Add(rewardConfig.mintingAmount, totalTxFee)
tmpInt := big.NewInt(0)
tmpInt = tmpInt.Mul(blockReward, rewardConfig.cnRatio)
cnReward := big.NewInt(0).Div(tmpInt, rewardConfig.totalRatio)
tmpInt = tmpInt.Mul(blockReward, rewardConfig.pocRatio)
pocIncentive := big.NewInt(0).Div(tmpInt, rewardConfig.totalRatio)
tmpInt = tmpInt.Mul(blockReward, rewardConfig.kirRatio)
kirIncentive := big.NewInt(0).Div(tmpInt, rewardConfig.totalRatio)
remaining := tmpInt.Sub(blockReward, cnReward)
remaining = tmpInt.Sub(remaining, pocIncentive)
remaining = tmpInt.Sub(remaining, kirIncentive)
pocIncentive = pocIncentive.Add(pocIncentive, remaining)
// CN reward
b.AddBalance(proposer, cnReward)
// Proposer gets PoC incentive and KIR incentive, if there is no PoC/KIR address.
// PoC
if common.EmptyAddress(pocAddr) {
pocAddr = proposer
}
b.AddBalance(pocAddr, pocIncentive)
// KIR
if common.EmptyAddress(kirAddr) {
kirAddr = proposer
}
b.AddBalance(kirAddr, kirIncentive)
logger.Debug("Block reward", "blockNumber", header.Number.Uint64(),
"Reward address of a proposer", proposer, "CN reward amount", cnReward,
"PoC address", pocAddr, "Poc incentive", pocIncentive,
"KIR address", kirAddr, "KIR incentive", kirIncentive)
}