-
Notifications
You must be signed in to change notification settings - Fork 321
/
candidate_statereader.go
496 lines (439 loc) · 14.8 KB
/
candidate_statereader.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Copyright (c) 2022 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. 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 staking
import (
"context"
"math/big"
"github.com/pkg/errors"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/state"
)
type (
// BucketGetByIndex related to obtaining bucket by index
BucketGetByIndex interface {
getBucket(index uint64) (*VoteBucket, error)
}
// BucketGet related to obtaining bucket
BucketGet interface {
BucketGetByIndex
getTotalBucketCount() (uint64, error)
getAllBuckets() ([]*VoteBucket, uint64, error)
getBucketsWithIndices(indices BucketIndices) ([]*VoteBucket, error)
getBucketIndices(addr address.Address, prefix byte) (*BucketIndices, uint64, error)
voterBucketIndices(addr address.Address) (*BucketIndices, uint64, error)
candBucketIndices(addr address.Address) (*BucketIndices, uint64, error)
}
// CandidateGet related to obtaining Candidate
CandidateGet interface {
getCandidate(name address.Address) (*Candidate, uint64, error)
getAllCandidates() (CandidateList, uint64, error)
}
// ReadState related to read bucket and candidate by request
ReadState interface {
readStateBuckets(ctx context.Context, req *iotexapi.ReadStakingDataRequest_VoteBuckets) (*iotextypes.VoteBucketList, uint64, error)
readStateBucketsByVoter(ctx context.Context, req *iotexapi.ReadStakingDataRequest_VoteBucketsByVoter) (*iotextypes.VoteBucketList, uint64, error)
readStateBucketsByCandidate(ctx context.Context, req *iotexapi.ReadStakingDataRequest_VoteBucketsByCandidate) (*iotextypes.VoteBucketList, uint64, error)
readStateBucketByIndices(ctx context.Context, req *iotexapi.ReadStakingDataRequest_VoteBucketsByIndexes) (*iotextypes.VoteBucketList, uint64, error)
readStateBucketCount(ctx context.Context, _ *iotexapi.ReadStakingDataRequest_BucketsCount) (*iotextypes.BucketsCount, uint64, error)
readStateCandidates(ctx context.Context, req *iotexapi.ReadStakingDataRequest_Candidates) (*iotextypes.CandidateListV2, uint64, error)
readStateCandidateByName(ctx context.Context, req *iotexapi.ReadStakingDataRequest_CandidateByName) (*iotextypes.CandidateV2, uint64, error)
readStateCandidateByAddress(ctx context.Context, req *iotexapi.ReadStakingDataRequest_CandidateByAddress) (*iotextypes.CandidateV2, uint64, error)
readStateTotalStakingAmount(ctx context.Context, _ *iotexapi.ReadStakingDataRequest_TotalStakingAmount) (*iotextypes.AccountMeta, uint64, error)
}
// CandidateStateReader contains candidate center and bucket pool
CandidateStateReader interface {
BucketGet
CandidateGet
ReadState
Height() uint64
SR() protocol.StateReader
BaseView() *ViewData
NewBucketPool(enableSMStorage bool) (*BucketPool, error)
GetCandidateByName(string) *Candidate
GetCandidateByOwner(address.Address) *Candidate
AllCandidates() CandidateList
TotalStakedAmount() *big.Int
ActiveBucketsCount() uint64
}
candSR struct {
protocol.StateReader
height uint64
view *ViewData
}
// ViewData is the data that need to be stored in protocol's view
ViewData struct {
candCenter *CandidateCenter
bucketPool *BucketPool
}
)
func newCandidateStateReader(sr protocol.StateReader) CandidateStateReader {
return &candSR{
StateReader: sr,
}
}
func (c *candSR) Height() uint64 {
return c.height
}
func (c *candSR) SR() protocol.StateReader {
return c.StateReader
}
func (c *candSR) BaseView() *ViewData {
return c.view
}
func (c *candSR) GetCandidateByName(name string) *Candidate {
return c.view.candCenter.GetByName(name)
}
func (c *candSR) GetCandidateByOwner(owner address.Address) *Candidate {
return c.view.candCenter.GetByOwner(owner)
}
func (c *candSR) AllCandidates() CandidateList {
return c.view.candCenter.All()
}
func (c *candSR) TotalStakedAmount() *big.Int {
return c.view.bucketPool.Total()
}
func (c *candSR) ActiveBucketsCount() uint64 {
return c.view.bucketPool.Count()
}
// GetStakingStateReader returns a candidate state reader that reflects the base view
func GetStakingStateReader(sr protocol.StateReader) (CandidateStateReader, error) {
c, err := ConstructBaseView(sr)
if err != nil {
if errors.Cause(err) == protocol.ErrNoName {
// the view does not exist yet, create it
view, height, err := CreateBaseView(sr, true)
if err != nil {
return nil, err
}
return &candSR{
StateReader: sr,
height: height,
view: view,
}, nil
}
return nil, err
}
return c, nil
}
// ConstructBaseView returns a candidate state reader that reflects the base view
// it will be used read-only
func ConstructBaseView(sr protocol.StateReader) (CandidateStateReader, error) {
if sr == nil {
return nil, ErrMissingField
}
height, err := sr.Height()
if err != nil {
return nil, err
}
v, err := sr.ReadView(_protocolID)
if err != nil {
return nil, err
}
view, ok := v.(*ViewData)
if !ok {
return nil, errors.Wrap(ErrTypeAssertion, "expecting *ViewData")
}
return &candSR{
StateReader: sr,
height: height,
view: &ViewData{
candCenter: view.candCenter,
bucketPool: view.bucketPool,
},
}, nil
}
// CreateBaseView creates the base view from state reader
func CreateBaseView(sr protocol.StateReader, enableSMStorage bool) (*ViewData, uint64, error) {
if sr == nil {
return nil, 0, ErrMissingField
}
csr := newCandidateStateReader(sr)
all, height, err := csr.getAllCandidates()
if err != nil && errors.Cause(err) != state.ErrStateNotExist {
return nil, height, err
}
center, err := NewCandidateCenter(all)
if err != nil {
return nil, height, err
}
pool, err := csr.NewBucketPool(enableSMStorage)
if err != nil {
return nil, height, err
}
return &ViewData{
candCenter: center,
bucketPool: pool,
}, height, nil
}
func (c *candSR) getTotalBucketCount() (uint64, error) {
var tc totalBucketCount
_, err := c.State(
&tc,
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(TotalBucketKey))
return tc.count, err
}
func (c *candSR) getBucket(index uint64) (*VoteBucket, error) {
var (
vb VoteBucket
err error
)
if _, err = c.State(
&vb,
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(bucketKey(index))); err != nil {
return nil, err
}
var tc totalBucketCount
if _, err := c.State(
&tc,
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(TotalBucketKey)); err != nil && errors.Cause(err) != state.ErrStateNotExist {
return nil, err
}
if errors.Cause(err) == state.ErrStateNotExist && index < tc.Count() {
return nil, ErrWithdrawnBucket
}
return &vb, nil
}
func (c *candSR) getAllBuckets() ([]*VoteBucket, uint64, error) {
height, iter, err := c.States(
protocol.NamespaceOption(StakingNameSpace),
protocol.KeysOption(func() ([][]byte, error) {
// TODO (zhi): fix potential racing issue
count, err := c.getTotalBucketCount()
if err != nil {
return nil, err
}
keys := [][]byte{}
for i := uint64(0); i < count; i++ {
keys = append(keys, bucketKey(i))
}
return keys, nil
}),
)
if err != nil {
return nil, height, err
}
buckets := make([]*VoteBucket, 0, iter.Size())
for i := 0; i < iter.Size(); i++ {
vb := &VoteBucket{}
switch err := iter.Next(vb); errors.Cause(err) {
case nil:
buckets = append(buckets, vb)
case state.ErrNilValue:
default:
return nil, height, errors.Wrapf(err, "failed to deserialize bucket")
}
}
return buckets, height, nil
}
func (c *candSR) getBucketsWithIndices(indices BucketIndices) ([]*VoteBucket, error) {
buckets := make([]*VoteBucket, 0, len(indices))
for _, i := range indices {
b, err := c.getBucket(i)
if err != nil && err != ErrWithdrawnBucket {
return buckets, err
}
buckets = append(buckets, b)
}
return buckets, nil
}
func (c *candSR) getBucketIndices(addr address.Address, prefix byte) (*BucketIndices, uint64, error) {
var (
bis BucketIndices
key = AddrKeyWithPrefix(addr, prefix)
)
height, err := c.State(
&bis,
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(key))
if err != nil {
return nil, height, err
}
return &bis, height, nil
}
func (c *candSR) voterBucketIndices(addr address.Address) (*BucketIndices, uint64, error) {
return c.getBucketIndices(addr, _voterIndex)
}
func (c *candSR) candBucketIndices(addr address.Address) (*BucketIndices, uint64, error) {
return c.getBucketIndices(addr, _candIndex)
}
func (c *candSR) getCandidate(name address.Address) (*Candidate, uint64, error) {
if name == nil {
return nil, 0, ErrNilParameters
}
var d Candidate
height, err := c.State(&d, protocol.NamespaceOption(CandidateNameSpace), protocol.KeyOption(name.Bytes()))
return &d, height, err
}
func (c *candSR) getAllCandidates() (CandidateList, uint64, error) {
height, iter, err := c.States(protocol.NamespaceOption(CandidateNameSpace))
if err != nil {
return nil, height, err
}
cands := make(CandidateList, 0, iter.Size())
for i := 0; i < iter.Size(); i++ {
c := &Candidate{}
if err := iter.Next(c); err != nil {
return nil, height, errors.Wrapf(err, "failed to deserialize candidate")
}
cands = append(cands, c)
}
return cands, height, nil
}
func (c *candSR) NewBucketPool(enableSMStorage bool) (*BucketPool, error) {
bp := BucketPool{
enableSMStorage: enableSMStorage,
total: &totalAmount{
amount: big.NewInt(0),
},
}
if bp.enableSMStorage {
switch _, err := c.State(bp.total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(_bucketPoolAddrKey)); errors.Cause(err) {
case nil:
return &bp, nil
case state.ErrStateNotExist:
// fall back to load all buckets
default:
return nil, err
}
}
// sum up all existing buckets
all, _, err := c.getAllBuckets()
if err != nil && errors.Cause(err) != state.ErrStateNotExist {
return nil, err
}
for _, v := range all {
if v.StakedAmount.Cmp(big.NewInt(0)) <= 0 {
return nil, state.ErrNotEnoughBalance
}
bp.total.amount.Add(bp.total.amount, v.StakedAmount)
}
bp.total.count = uint64(len(all))
return &bp, nil
}
func (c *candSR) readStateBuckets(ctx context.Context, req *iotexapi.ReadStakingDataRequest_VoteBuckets) (*iotextypes.VoteBucketList, uint64, error) {
all, height, err := c.getAllBuckets()
if err != nil {
return nil, height, err
}
offset := int(req.GetPagination().GetOffset())
limit := int(req.GetPagination().GetLimit())
buckets := getPageOfBuckets(all, offset, limit)
pbBuckets, err := toIoTeXTypesVoteBucketList(buckets)
return pbBuckets, height, err
}
func (c *candSR) readStateBucketsByVoter(ctx context.Context, req *iotexapi.ReadStakingDataRequest_VoteBucketsByVoter) (*iotextypes.VoteBucketList, uint64, error) {
voter, err := address.FromString(req.GetVoterAddress())
if err != nil {
return nil, 0, err
}
indices, height, err := c.voterBucketIndices(voter)
if errors.Cause(err) == state.ErrStateNotExist {
return &iotextypes.VoteBucketList{}, height, nil
}
if indices == nil || err != nil {
return nil, height, err
}
buckets, err := c.getBucketsWithIndices(*indices)
if err != nil {
return nil, height, err
}
offset := int(req.GetPagination().GetOffset())
limit := int(req.GetPagination().GetLimit())
buckets = getPageOfBuckets(buckets, offset, limit)
pbBuckets, err := toIoTeXTypesVoteBucketList(buckets)
return pbBuckets, height, err
}
func (c *candSR) readStateBucketsByCandidate(ctx context.Context, req *iotexapi.ReadStakingDataRequest_VoteBucketsByCandidate) (*iotextypes.VoteBucketList, uint64, error) {
cand := c.GetCandidateByName(req.GetCandName())
if c == nil {
return &iotextypes.VoteBucketList{}, 0, nil
}
indices, height, err := c.candBucketIndices(cand.Owner)
if errors.Cause(err) == state.ErrStateNotExist {
return &iotextypes.VoteBucketList{}, height, nil
}
if indices == nil || err != nil {
return nil, height, err
}
buckets, err := c.getBucketsWithIndices(*indices)
if err != nil {
return nil, height, err
}
offset := int(req.GetPagination().GetOffset())
limit := int(req.GetPagination().GetLimit())
buckets = getPageOfBuckets(buckets, offset, limit)
pbBuckets, err := toIoTeXTypesVoteBucketList(buckets)
return pbBuckets, height, err
}
func (c *candSR) readStateBucketByIndices(ctx context.Context, req *iotexapi.ReadStakingDataRequest_VoteBucketsByIndexes) (*iotextypes.VoteBucketList, uint64, error) {
height, err := c.SR().Height()
if err != nil {
return &iotextypes.VoteBucketList{}, height, err
}
buckets, err := c.getBucketsWithIndices(BucketIndices(req.GetIndex()))
if err != nil {
return nil, height, err
}
pbBuckets, err := toIoTeXTypesVoteBucketList(buckets)
return pbBuckets, height, err
}
func (c *candSR) readStateBucketCount(ctx context.Context, _ *iotexapi.ReadStakingDataRequest_BucketsCount) (*iotextypes.BucketsCount, uint64, error) {
total, err := c.getTotalBucketCount()
if errors.Cause(err) == state.ErrStateNotExist {
return &iotextypes.BucketsCount{}, c.Height(), nil
}
if err != nil {
return nil, 0, err
}
active, h, err := getActiveBucketsCount(ctx, c)
if err != nil {
return nil, h, err
}
return &iotextypes.BucketsCount{
Total: total,
Active: active,
}, h, nil
}
func (c *candSR) readStateCandidates(ctx context.Context, req *iotexapi.ReadStakingDataRequest_Candidates) (*iotextypes.CandidateListV2, uint64, error) {
offset := int(req.GetPagination().GetOffset())
limit := int(req.GetPagination().GetLimit())
candidates := getPageOfCandidates(c.AllCandidates(), offset, limit)
return toIoTeXTypesCandidateListV2(candidates), c.Height(), nil
}
func (c *candSR) readStateCandidateByName(ctx context.Context, req *iotexapi.ReadStakingDataRequest_CandidateByName) (*iotextypes.CandidateV2, uint64, error) {
cand := c.GetCandidateByName(req.GetCandName())
if c == nil {
return &iotextypes.CandidateV2{}, c.Height(), nil
}
return cand.toIoTeXTypes(), c.Height(), nil
}
func (c *candSR) readStateCandidateByAddress(ctx context.Context, req *iotexapi.ReadStakingDataRequest_CandidateByAddress) (*iotextypes.CandidateV2, uint64, error) {
owner, err := address.FromString(req.GetOwnerAddr())
if err != nil {
return nil, 0, err
}
cand := c.GetCandidateByOwner(owner)
if c == nil {
return &iotextypes.CandidateV2{}, c.Height(), nil
}
return cand.toIoTeXTypes(), c.Height(), nil
}
func (c *candSR) readStateTotalStakingAmount(ctx context.Context, _ *iotexapi.ReadStakingDataRequest_TotalStakingAmount) (*iotextypes.AccountMeta, uint64, error) {
meta := iotextypes.AccountMeta{}
meta.Address = address.StakingBucketPoolAddr
total, h, err := getTotalStakedAmount(ctx, c)
if err != nil {
return nil, h, err
}
meta.Balance = total.String()
return &meta, h, nil
}