forked from klaytn/klaytn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
350 lines (304 loc) · 11.2 KB
/
api.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// 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 governance
import (
"errors"
"math/big"
"strings"
"github.com/klaytn/klaytn/common/hexutil"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/networks/rpc"
"github.com/klaytn/klaytn/params"
"github.com/klaytn/klaytn/reward"
)
type PublicGovernanceAPI struct {
governance Engine // Node interfaced by this API
}
type returnTally struct {
Key string
Value interface{}
ApprovalPercentage float64
}
func NewGovernanceAPI(gov Engine) *PublicGovernanceAPI {
return &PublicGovernanceAPI{governance: gov}
}
type GovernanceKlayAPI struct {
governance Engine
chain blockChain
}
func NewGovernanceKlayAPI(gov Engine, chain blockChain) *GovernanceKlayAPI {
return &GovernanceKlayAPI{governance: gov, chain: chain}
}
var (
errUnknownBlock = errors.New("Unknown block")
errNotAvailableInThisMode = errors.New("In current governance mode, voting power is not available")
errSetDefaultFailure = errors.New("Failed to set a default value")
errPermissionDenied = errors.New("You don't have the right to vote")
errRemoveSelf = errors.New("You can't vote on removing yourself")
errInvalidKeyValue = errors.New("Your vote couldn't be placed. Please check your vote's key and value")
errInvalidLowerBound = errors.New("lowerboundbasefee cannot be set exceeding upperboundbasefee")
errInvalidUpperBound = errors.New("upperboundbasefee cannot be set lower than lowerboundbasefee")
)
// GasPriceAt returns the base fee of the given block in peb,
// or returns unit price by using governance if there is no base fee set in header,
// or returns gas price of txpool if the block is pending block.
func (api *GovernanceKlayAPI) GasPriceAt(num *rpc.BlockNumber) (*hexutil.Big, error) {
if num == nil || *num == rpc.LatestBlockNumber {
header := api.chain.CurrentHeader()
if header.BaseFee == nil {
return (*hexutil.Big)(new(big.Int).SetUint64(api.governance.Params().UnitPrice())), nil
}
return (*hexutil.Big)(header.BaseFee), nil
} else if *num == rpc.PendingBlockNumber {
txpool := api.governance.GetTxPool()
return (*hexutil.Big)(txpool.GasPrice()), nil
} else {
blockNum := num.Uint64()
// Return the BaseFee in header at the block number
header := api.chain.GetHeaderByNumber(blockNum)
if blockNum > api.chain.CurrentHeader().Number.Uint64() || header == nil {
return nil, errUnknownBlock
} else if header.BaseFee != nil {
return (*hexutil.Big)(header.BaseFee), nil
}
// Return the UnitPrice in governance data at the block number
if ret, err := api.GasPriceAtNumber(blockNum); err != nil {
return nil, err
} else {
return (*hexutil.Big)(new(big.Int).SetUint64(ret)), nil
}
}
}
// or returns gas price of txpool if the block is pending block.
func (api *GovernanceKlayAPI) GetRewards(num *rpc.BlockNumber) (*reward.RewardSpec, error) {
blockNumber := uint64(0)
if num == nil || *num == rpc.LatestBlockNumber {
blockNumber = api.governance.BlockChain().CurrentHeader().Number.Uint64()
} else {
blockNumber = uint64(num.Int64())
}
header := api.chain.GetHeaderByNumber(blockNumber)
rules := api.chain.Config().Rules(new(big.Int).SetUint64(blockNumber))
pset, err := api.governance.ParamsAt(blockNumber)
if err != nil {
return nil, err
}
return reward.GetBlockReward(header, rules, pset)
}
// Vote injects a new vote for governance targets such as unitprice and governingnode.
func (api *PublicGovernanceAPI) Vote(key string, val interface{}) (string, error) {
gMode := api.governance.Params().GovernanceModeInt()
gNode := api.governance.Params().GoverningNode()
if gMode == params.GovernanceMode_Single && gNode != api.governance.NodeAddress() {
return "", errPermissionDenied
}
vote, ok := api.governance.ValidateVote(&GovernanceVote{Key: strings.ToLower(key), Value: val})
if !ok {
return "", errInvalidKeyValue
}
if vote.Key == "governance.removevalidator" {
if api.isRemovingSelf(val.(string)) {
return "", errRemoveSelf
}
}
if vote.Key == "kip71.lowerboundbasefee" {
if vote.Value.(uint64) > api.governance.Params().UpperBoundBaseFee() {
return "", errInvalidLowerBound
}
}
if vote.Key == "kip71.upperboundbasefee" {
if vote.Value.(uint64) < api.governance.Params().LowerBoundBaseFee() {
return "", errInvalidUpperBound
}
}
if api.governance.AddVote(key, val) {
return "Your vote is prepared. It will be put into the block header or applied when your node generates a block as a proposer. Note that your vote may be duplicate.", nil
}
return "", errInvalidKeyValue
}
func (api *PublicGovernanceAPI) isRemovingSelf(val string) bool {
for _, str := range strings.Split(val, ",") {
str = strings.Trim(str, " ")
if common.HexToAddress(str) == api.governance.NodeAddress() {
return true
}
}
return false
}
func (api *PublicGovernanceAPI) ShowTally() []*returnTally {
ret := []*returnTally{}
for _, val := range api.governance.GetGovernanceTalliesCopy() {
item := &returnTally{
Key: val.Key,
Value: val.Value,
ApprovalPercentage: float64(val.Votes) / float64(api.governance.TotalVotingPower()) * 100,
}
ret = append(ret, item)
}
return ret
}
func (api *PublicGovernanceAPI) TotalVotingPower() (float64, error) {
if !api.isGovernanceModeBallot() {
return 0, errNotAvailableInThisMode
}
return float64(api.governance.TotalVotingPower()) / 1000.0, nil
}
func (api *PublicGovernanceAPI) ItemsAt(num *rpc.BlockNumber) (map[string]interface{}, error) {
blockNumber := uint64(0)
if num == nil || *num == rpc.LatestBlockNumber || *num == rpc.PendingBlockNumber {
blockNumber = api.governance.BlockChain().CurrentHeader().Number.Uint64()
} else {
blockNumber = uint64(num.Int64())
}
pset, err := api.governance.ParamsAt(blockNumber)
if err != nil {
return nil, err
}
return pset.StrMap(), nil
}
func (api *PublicGovernanceAPI) GetStakingInfo(num *rpc.BlockNumber) (*reward.StakingInfo, error) {
blockNumber := uint64(0)
if num == nil || *num == rpc.LatestBlockNumber || *num == rpc.PendingBlockNumber {
blockNumber = api.governance.BlockChain().CurrentHeader().Number.Uint64()
} else {
blockNumber = uint64(num.Int64())
}
return reward.GetStakingInfo(blockNumber), nil
}
func (api *PublicGovernanceAPI) PendingChanges() map[string]interface{} {
return api.governance.PendingChanges()
}
func (api *PublicGovernanceAPI) Votes() []GovernanceVote {
return api.governance.Votes()
}
func (api *PublicGovernanceAPI) IdxCache() []uint64 {
return api.governance.IdxCache()
}
func (api *PublicGovernanceAPI) IdxCacheFromDb() []uint64 {
return api.governance.IdxCacheFromDb()
}
// TODO-Klaytn: Return error if invalid input is given such as pending or a too big number
func (api *PublicGovernanceAPI) ItemCacheFromDb(num *rpc.BlockNumber) map[string]interface{} {
blockNumber := uint64(0)
if num == nil || *num == rpc.LatestBlockNumber || *num == rpc.PendingBlockNumber {
blockNumber = api.governance.BlockChain().CurrentHeader().Number.Uint64()
} else {
blockNumber = uint64(num.Int64())
}
ret, _ := api.governance.DB().ReadGovernance(blockNumber)
return ret
}
type VoteList struct {
Key string
Value interface{}
Casted bool
BlockNum uint64
}
func (api *PublicGovernanceAPI) MyVotes() []*VoteList {
ret := []*VoteList{}
for k, v := range api.governance.GetVoteMapCopy() {
item := &VoteList{
Key: k,
Value: v.Value,
Casted: v.Casted,
BlockNum: v.Num,
}
ret = append(ret, item)
}
return ret
}
func (api *PublicGovernanceAPI) MyVotingPower() (float64, error) {
if !api.isGovernanceModeBallot() {
return 0, errNotAvailableInThisMode
}
return float64(api.governance.MyVotingPower()) / 1000.0, nil
}
func (api *PublicGovernanceAPI) ChainConfig() *params.ChainConfig {
num := rpc.LatestBlockNumber
return api.chainConfigAt(&num)
}
func (api *PublicGovernanceAPI) ChainConfigAt(num *rpc.BlockNumber) *params.ChainConfig {
return api.chainConfigAt(num)
}
func (api *PublicGovernanceAPI) chainConfigAt(num *rpc.BlockNumber) *params.ChainConfig {
var blocknum uint64
if num == nil || *num == rpc.LatestBlockNumber || *num == rpc.PendingBlockNumber {
blocknum = api.governance.BlockChain().CurrentHeader().Number.Uint64()
} else {
blocknum = num.Uint64()
}
pset, err := api.governance.ParamsAt(blocknum)
if err != nil {
return nil
}
latestConfig := api.governance.BlockChain().Config()
config := pset.ToChainConfig()
config.ChainID = latestConfig.ChainID
config.IstanbulCompatibleBlock = latestConfig.IstanbulCompatibleBlock
config.LondonCompatibleBlock = latestConfig.LondonCompatibleBlock
config.EthTxTypeCompatibleBlock = latestConfig.EthTxTypeCompatibleBlock
config.MagmaCompatibleBlock = latestConfig.MagmaCompatibleBlock
config.KoreCompatibleBlock = latestConfig.KoreCompatibleBlock
return config
}
func (api *PublicGovernanceAPI) NodeAddress() common.Address {
return api.governance.NodeAddress()
}
func (api *PublicGovernanceAPI) isGovernanceModeBallot() bool {
gMode := api.governance.Params().GovernanceModeInt()
return gMode == params.GovernanceMode_Ballot
}
func (api *GovernanceKlayAPI) GasPriceAtNumber(num uint64) (uint64, error) {
pset, err := api.governance.ParamsAt(num)
if err != nil {
logger.Error("Failed to retrieve unit price", "err", err)
return 0, err
}
return pset.UnitPrice(), nil
}
// Disabled APIs
// func (api *GovernanceKlayAPI) GetTxGasHumanReadable(num *rpc.BlockNumber) (uint64, error) {
// if num == nil || *num == rpc.LatestBlockNumber || *num == rpc.PendingBlockNumber {
// // If the value hasn't been set in governance, set it with default value
// if ret := api.governance.GetGovernanceValue(params.ConstTxGasHumanReadable); ret == nil {
// return api.setDefaultTxGasHumanReadable()
// } else {
// return ret.(uint64), nil
// }
// } else {
// blockNum := num.Int64()
//
// if blockNum > api.chain.CurrentHeader().Number.Int64() {
// return 0, errUnknownBlock
// }
//
// if ret, err := api.governance.GetGovernanceItemAtNumber(uint64(blockNum), GovernanceKeyMapReverse[params.ConstTxGasHumanReadable]); err == nil && ret != nil {
// return ret.(uint64), nil
// } else {
// logger.Error("Failed to retrieve TxGasHumanReadable, sending default value", "err", err)
// return api.setDefaultTxGasHumanReadable()
// }
// }
// }
//
// func (api *GovernanceKlayAPI) setDefaultTxGasHumanReadable() (uint64, error) {
// err := api.governance.currentSet.SetValue(params.ConstTxGasHumanReadable, params.TxGasHumanReadable)
// if err != nil {
// return 0, errSetDefaultFailure
// } else {
// return params.TxGasHumanReadable, nil
// }
// }