/
read_state.go
251 lines (224 loc) · 7.9 KB
/
read_state.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
// Copyright (c) 2020 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/blockchain/genesis"
"github.com/iotexproject/iotex-core/state"
)
func readStateBuckets(ctx context.Context, sr protocol.StateReader,
req *iotexapi.ReadStakingDataRequest_VoteBuckets) (*iotextypes.VoteBucketList, uint64, error) {
all, height, err := getAllBuckets(sr)
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 readStateBucketsByVoter(ctx context.Context, sr protocol.StateReader,
req *iotexapi.ReadStakingDataRequest_VoteBucketsByVoter) (*iotextypes.VoteBucketList, uint64, error) {
voter, err := address.FromString(req.GetVoterAddress())
if err != nil {
return nil, 0, err
}
indices, height, err := getVoterBucketIndices(sr, voter)
if errors.Cause(err) == state.ErrStateNotExist {
return &iotextypes.VoteBucketList{}, height, nil
}
if indices == nil || err != nil {
return nil, height, err
}
buckets, err := getBucketsWithIndices(sr, *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 readStateBucketsByCandidate(ctx context.Context, csr CandidateStateReader,
req *iotexapi.ReadStakingDataRequest_VoteBucketsByCandidate) (*iotextypes.VoteBucketList, uint64, error) {
c := csr.GetCandidateByName(req.GetCandName())
if c == nil {
return &iotextypes.VoteBucketList{}, 0, nil
}
indices, height, err := getCandBucketIndices(csr.SR(), c.Owner)
if errors.Cause(err) == state.ErrStateNotExist {
return &iotextypes.VoteBucketList{}, height, nil
}
if indices == nil || err != nil {
return nil, height, err
}
buckets, err := getBucketsWithIndices(csr.SR(), *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 readStateBucketByIndices(ctx context.Context, sr protocol.StateReader,
req *iotexapi.ReadStakingDataRequest_VoteBucketsByIndexes) (*iotextypes.VoteBucketList, uint64, error) {
height, err := sr.Height()
if err != nil {
return &iotextypes.VoteBucketList{}, height, err
}
buckets, err := getBucketsWithIndices(sr, BucketIndices(req.GetIndex()))
if err != nil {
return nil, height, err
}
pbBuckets, err := toIoTeXTypesVoteBucketList(buckets)
return pbBuckets, height, err
}
func readStateBucketCount(ctx context.Context, csr CandidateStateReader,
_ *iotexapi.ReadStakingDataRequest_BucketsCount) (*iotextypes.BucketsCount, uint64, error) {
total, err := getTotalBucketCount(csr.SR())
if errors.Cause(err) == state.ErrStateNotExist {
return &iotextypes.BucketsCount{}, csr.Height(), nil
}
if err != nil {
return nil, 0, err
}
active, h, err := getActiveBucketsCount(ctx, csr)
if err != nil {
return nil, h, err
}
return &iotextypes.BucketsCount{
Total: total,
Active: active,
}, h, nil
}
func readStateCandidates(ctx context.Context, csr CandidateStateReader,
req *iotexapi.ReadStakingDataRequest_Candidates) (*iotextypes.CandidateListV2, uint64, error) {
offset := int(req.GetPagination().GetOffset())
limit := int(req.GetPagination().GetLimit())
candidates := getPageOfCandidates(csr.AllCandidates(), offset, limit)
return toIoTeXTypesCandidateListV2(candidates), csr.Height(), nil
}
func readStateCandidateByName(ctx context.Context, csr CandidateStateReader,
req *iotexapi.ReadStakingDataRequest_CandidateByName) (*iotextypes.CandidateV2, uint64, error) {
c := csr.GetCandidateByName(req.GetCandName())
if c == nil {
return &iotextypes.CandidateV2{}, csr.Height(), nil
}
return c.toIoTeXTypes(), csr.Height(), nil
}
func readStateCandidateByAddress(ctx context.Context, csr CandidateStateReader,
req *iotexapi.ReadStakingDataRequest_CandidateByAddress) (*iotextypes.CandidateV2, uint64, error) {
owner, err := address.FromString(req.GetOwnerAddr())
if err != nil {
return nil, 0, err
}
c := csr.GetCandidateByOwner(owner)
if c == nil {
return &iotextypes.CandidateV2{}, csr.Height(), nil
}
return c.toIoTeXTypes(), csr.Height(), nil
}
func readStateTotalStakingAmount(ctx context.Context, csr CandidateStateReader,
_ *iotexapi.ReadStakingDataRequest_TotalStakingAmount) (*iotextypes.AccountMeta, uint64, error) {
meta := iotextypes.AccountMeta{}
meta.Address = address.StakingBucketPoolAddr
total, h, err := getTotalStakedAmount(ctx, csr)
if err != nil {
return nil, h, err
}
meta.Balance = total.String()
return &meta, h, nil
}
func toIoTeXTypesVoteBucketList(buckets []*VoteBucket) (*iotextypes.VoteBucketList, error) {
res := iotextypes.VoteBucketList{
Buckets: make([]*iotextypes.VoteBucket, 0, len(buckets)),
}
for _, b := range buckets {
typBucket, err := b.toIoTeXTypes()
if err != nil {
return nil, err
}
res.Buckets = append(res.Buckets, typBucket)
}
return &res, nil
}
func getPageOfBuckets(buckets []*VoteBucket, offset, limit int) []*VoteBucket {
var res []*VoteBucket
if offset >= len(buckets) {
return res
}
buckets = buckets[offset:]
if limit > len(buckets) {
limit = len(buckets)
}
res = make([]*VoteBucket, 0, limit)
for i := 0; i < limit; i++ {
res = append(res, buckets[i])
}
return res
}
func toIoTeXTypesCandidateListV2(candidates CandidateList) *iotextypes.CandidateListV2 {
res := iotextypes.CandidateListV2{
Candidates: make([]*iotextypes.CandidateV2, 0, len(candidates)),
}
for _, c := range candidates {
res.Candidates = append(res.Candidates, c.toIoTeXTypes())
}
return &res
}
func getPageOfCandidates(candidates CandidateList, offset, limit int) CandidateList {
var res CandidateList
if offset >= len(candidates) {
return res
}
candidates = candidates[offset:]
if limit > len(candidates) {
limit = len(candidates)
}
res = make([]*Candidate, 0, limit)
for i := 0; i < limit; i++ {
res = append(res, candidates[i])
}
return res
}
func getTotalStakedAmount(ctx context.Context, csr CandidateStateReader) (*big.Int, uint64, error) {
g := genesis.MustExtractGenesisContext(ctx)
if g.IsGreenland(csr.Height()) {
// after Greenland, read state from db
var total totalAmount
h, err := csr.SR().State(&total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(bucketPoolAddrKey))
if err != nil {
return nil, h, err
}
return total.amount, h, nil
}
// otherwise read from bucket pool
return csr.TotalStakedAmount(), csr.Height(), nil
}
func getActiveBucketsCount(ctx context.Context, csr CandidateStateReader) (uint64, uint64, error) {
g := genesis.MustExtractGenesisContext(ctx)
if g.IsGreenland(csr.Height()) {
// after Greenland, read state from db
var total totalAmount
h, err := csr.SR().State(&total, protocol.NamespaceOption(StakingNameSpace), protocol.KeyOption(bucketPoolAddrKey))
if err != nil {
return 0, h, err
}
return total.count, h, nil
}
// otherwise read from bucket pool
return csr.ActiveBucketsCount(), csr.Height(), nil
}