-
Notifications
You must be signed in to change notification settings - Fork 319
/
admin.go
275 lines (249 loc) · 7.64 KB
/
admin.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
// Copyright (c) 2019 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package rewarding
import (
"context"
"math/big"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/rewarding/rewardingpb"
"github.com/iotexproject/iotex-core/blockchain/genesis"
)
// admin stores the admin data of the rewarding protocol
type admin struct {
blockReward *big.Int
epochReward *big.Int
numDelegatesForEpochReward uint64
foundationBonus *big.Int
numDelegatesForFoundationBonus uint64
foundationBonusLastEpoch uint64
productivityThreshold uint64
}
// Serialize serializes admin state into bytes
func (a admin) Serialize() ([]byte, error) {
gen := rewardingpb.Admin{
BlockReward: a.blockReward.String(),
EpochReward: a.epochReward.String(),
NumDelegatesForEpochReward: a.numDelegatesForEpochReward,
FoundationBonus: a.foundationBonus.String(),
NumDelegatesForFoundationBonus: a.numDelegatesForFoundationBonus,
FoundationBonusLastEpoch: a.foundationBonusLastEpoch,
ProductivityThreshold: a.productivityThreshold,
}
return proto.Marshal(&gen)
}
// Deserialize deserializes bytes into admin state
func (a *admin) Deserialize(data []byte) error {
gen := rewardingpb.Admin{}
if err := proto.Unmarshal(data, &gen); err != nil {
return err
}
blockReward, ok := new(big.Int).SetString(gen.BlockReward, 10)
if !ok {
return errors.New("failed to set block reward")
}
epochReward, ok := new(big.Int).SetString(gen.EpochReward, 10)
if !ok {
return errors.New("failed to set epoch reward")
}
foundationBonus, ok := new(big.Int).SetString(gen.FoundationBonus, 10)
if !ok {
return errors.New("failed to set bootstrap bonus")
}
a.blockReward = blockReward
a.epochReward = epochReward
a.numDelegatesForEpochReward = gen.NumDelegatesForEpochReward
a.foundationBonus = foundationBonus
a.numDelegatesForFoundationBonus = gen.NumDelegatesForFoundationBonus
a.foundationBonusLastEpoch = gen.FoundationBonusLastEpoch
a.productivityThreshold = gen.ProductivityThreshold
return nil
}
func (a *admin) grantFoundationBonus(epoch uint64) bool {
return epoch <= a.foundationBonusLastEpoch
}
// exempt stores the addresses that exempt from epoch reward
type exempt struct {
addrs []address.Address
}
// Serialize serializes exempt state into bytes
func (e *exempt) Serialize() ([]byte, error) {
epb := rewardingpb.Exempt{}
for _, addr := range e.addrs {
epb.Addrs = append(epb.Addrs, addr.Bytes())
}
return proto.Marshal(&epb)
}
// Deserialize deserializes bytes into exempt state
func (e *exempt) Deserialize(data []byte) error {
epb := rewardingpb.Exempt{}
if err := proto.Unmarshal(data, &epb); err != nil {
return err
}
e.addrs = nil
for _, addrBytes := range epb.Addrs {
addr, err := address.FromBytes(addrBytes)
if err != nil {
return err
}
e.addrs = append(e.addrs, addr)
}
return nil
}
// CreateGenesisStates initializes the rewarding protocol by setting the original admin, block and epoch reward
func (p *Protocol) CreateGenesisStates(
ctx context.Context,
sm protocol.StateManager,
) error {
blkCtx := protocol.MustGetBlockCtx(ctx)
g := genesis.MustExtractGenesisContext(ctx)
if err := p.assertZeroBlockHeight(blkCtx.BlockHeight); err != nil {
return err
}
blockReward := g.BlockReward()
if err := p.assertAmount(blockReward); err != nil {
return err
}
epochReward := g.EpochReward()
if err := p.assertAmount(epochReward); err != nil {
return err
}
if err := p.putState(
ctx,
sm,
_adminKey,
&admin{
blockReward: blockReward,
epochReward: epochReward,
numDelegatesForEpochReward: g.NumDelegatesForEpochReward,
foundationBonus: g.FoundationBonus(),
numDelegatesForFoundationBonus: g.NumDelegatesForFoundationBonus,
foundationBonusLastEpoch: g.FoundationBonusLastEpoch,
productivityThreshold: g.ProductivityThreshold,
},
); err != nil {
return err
}
initBalance := g.InitBalance()
if err := p.putState(
ctx,
sm,
_fundKey,
&fund{
totalBalance: initBalance,
unclaimedBalance: initBalance,
},
); err != nil {
return err
}
return p.putState(
ctx,
sm,
_exemptKey,
&exempt{
addrs: g.ExemptAddrsFromEpochReward(),
},
)
}
// BlockReward returns the block reward amount
func (p *Protocol) BlockReward(
ctx context.Context,
sm protocol.StateReader,
) (*big.Int, error) {
a := admin{}
if _, err := p.state(ctx, sm, _adminKey, &a); err != nil {
return nil, err
}
return a.blockReward, nil
}
// EpochReward returns the epoch reward amount
func (p *Protocol) EpochReward(
ctx context.Context,
sm protocol.StateReader,
) (*big.Int, error) {
a := admin{}
if _, err := p.state(ctx, sm, _adminKey, &a); err != nil {
return nil, err
}
return a.epochReward, nil
}
// NumDelegatesForEpochReward returns the number of candidates sharing an epoch reward
func (p *Protocol) NumDelegatesForEpochReward(
ctx context.Context,
sm protocol.StateManager,
) (uint64, error) {
a := admin{}
if _, err := p.state(ctx, sm, _adminKey, &a); err != nil {
return 0, err
}
return a.numDelegatesForEpochReward, nil
}
// FoundationBonus returns the foundation bonus amount
func (p *Protocol) FoundationBonus(ctx context.Context, sm protocol.StateReader) (*big.Int, error) {
a := admin{}
if _, err := p.state(ctx, sm, _adminKey, &a); err != nil {
return nil, err
}
return a.foundationBonus, nil
}
// FoundationBonusLastEpoch returns the last epoch when the foundation bonus will still be granted
func (p *Protocol) FoundationBonusLastEpoch(ctx context.Context, sm protocol.StateReader) (uint64, error) {
a := admin{}
if _, err := p.state(ctx, sm, _adminKey, &a); err != nil {
return 0, err
}
return a.foundationBonusLastEpoch, nil
}
// NumDelegatesForFoundationBonus returns the number of delegates that will get foundation bonus
func (p *Protocol) NumDelegatesForFoundationBonus(ctx context.Context, sm protocol.StateReader) (uint64, error) {
a := admin{}
if _, err := p.state(ctx, sm, _adminKey, &a); err != nil {
return 0, err
}
return a.numDelegatesForFoundationBonus, nil
}
// ProductivityThreshold returns the productivity threshold
func (p *Protocol) ProductivityThreshold(ctx context.Context, sm protocol.StateManager) (uint64, error) {
a := admin{}
if _, err := p.state(ctx, sm, _adminKey, &a); err != nil {
return 0, err
}
return a.productivityThreshold, nil
}
// SetReward updates block or epoch reward amount
func (p *Protocol) SetReward(
ctx context.Context,
sm protocol.StateManager,
amount *big.Int,
blockLevel bool,
) error {
if err := p.assertAmount(amount); err != nil {
return err
}
a := admin{}
if _, err := p.state(ctx, sm, _adminKey, &a); err != nil {
return err
}
if blockLevel {
a.blockReward = amount
} else {
a.epochReward = amount
}
return p.putState(ctx, sm, _adminKey, &a)
}
func (p *Protocol) assertAmount(amount *big.Int) error {
if amount.Cmp(big.NewInt(0)) >= 0 {
return nil
}
return errors.Errorf("amount %s shouldn't be negative", amount.String())
}
func (p *Protocol) assertZeroBlockHeight(height uint64) error {
if height != 0 {
return errors.Errorf("current block height %d is not zero", height)
}
return nil
}