/
candidate_buckets_indexer.go
249 lines (225 loc) · 7.36 KB
/
candidate_buckets_indexer.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
// Copyright (c) 2020 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 staking
import (
"context"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/db/batch"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)
const (
// StakingCandidatesNamespace is a namespace to store candidates with epoch start height
StakingCandidatesNamespace = "stakingCandidates"
// StakingBucketsNamespace is a namespace to store vote buckets with epoch start height
StakingBucketsNamespace = "stakingBuckets"
// StakingMetaNamespace is a namespace to store metadata
StakingMetaNamespace = "stakingMeta"
)
var (
_candHeightKey = []byte("cht")
_bucketHeightKey = []byte("bht")
_latestCandidatesHash = []byte("lch")
_latestBucketsHash = []byte("lbh")
)
// CandidatesBucketsIndexer is an indexer to store candidates by given height
type CandidatesBucketsIndexer struct {
latestCandidatesHeight uint64
latestBucketsHeight uint64
latestCandidatesHash hash.Hash160
latestBucketsHash hash.Hash160
kvStore db.KVStoreForRangeIndex
}
// NewStakingCandidatesBucketsIndexer creates a new StakingCandidatesIndexer
func NewStakingCandidatesBucketsIndexer(kv db.KVStoreForRangeIndex) (*CandidatesBucketsIndexer, error) {
if kv == nil {
return nil, ErrMissingField
}
return &CandidatesBucketsIndexer{
kvStore: kv,
}, nil
}
// Start starts the indexer
func (cbi *CandidatesBucketsIndexer) Start(ctx context.Context) error {
if err := cbi.kvStore.Start(ctx); err != nil {
return err
}
ret, err := cbi.kvStore.Get(StakingMetaNamespace, _candHeightKey)
switch errors.Cause(err) {
case nil:
cbi.latestCandidatesHeight = byteutil.BytesToUint64BigEndian(ret)
case db.ErrNotExist:
cbi.latestCandidatesHeight = 0
default:
return err
}
ret, err = cbi.kvStore.Get(StakingMetaNamespace, _latestCandidatesHash)
switch errors.Cause(err) {
case nil:
cbi.latestCandidatesHash = hash.BytesToHash160(ret)
case db.ErrNotExist:
cbi.latestCandidatesHash = hash.ZeroHash160
default:
return err
}
ret, err = cbi.kvStore.Get(StakingMetaNamespace, _bucketHeightKey)
switch errors.Cause(err) {
case nil:
cbi.latestBucketsHeight = byteutil.BytesToUint64BigEndian(ret)
case db.ErrNotExist:
cbi.latestBucketsHeight = 0
default:
return err
}
ret, err = cbi.kvStore.Get(StakingMetaNamespace, _latestBucketsHash)
switch errors.Cause(err) {
case nil:
cbi.latestBucketsHash = hash.BytesToHash160(ret)
case db.ErrNotExist:
cbi.latestBucketsHash = hash.ZeroHash160
default:
return err
}
return nil
}
// Stop stops the indexer
func (cbi *CandidatesBucketsIndexer) Stop(ctx context.Context) error {
return cbi.kvStore.Stop(ctx)
}
// PutCandidates puts candidates into indexer
func (cbi *CandidatesBucketsIndexer) PutCandidates(height uint64, candidates *iotextypes.CandidateListV2) error {
candidatesBytes, err := proto.Marshal(candidates)
if err != nil {
return err
}
if err := cbi.putToIndexer(StakingCandidatesNamespace, height, candidatesBytes); err != nil {
return err
}
cbi.latestCandidatesHeight = height
return nil
}
// GetCandidates gets candidates from indexer given epoch start height
func (cbi *CandidatesBucketsIndexer) GetCandidates(height uint64, offset, limit uint32) (*iotextypes.CandidateListV2, uint64, error) {
if height > cbi.latestCandidatesHeight {
height = cbi.latestCandidatesHeight
}
candidateList := &iotextypes.CandidateListV2{}
ret, err := getFromIndexer(cbi.kvStore, StakingCandidatesNamespace, height)
cause := errors.Cause(err)
if cause == db.ErrNotExist || cause == db.ErrBucketNotExist {
return candidateList, height, nil
}
if err != nil {
return nil, height, err
}
if err := proto.Unmarshal(ret, candidateList); err != nil {
return nil, height, err
}
length := uint32(len(candidateList.Candidates))
if offset >= length {
return &iotextypes.CandidateListV2{}, height, nil
}
end := offset + limit
if end > uint32(len(candidateList.Candidates)) {
end = uint32(len(candidateList.Candidates))
}
candidateList.Candidates = candidateList.Candidates[offset:end]
return candidateList, height, nil
}
// PutBuckets puts vote buckets into indexer
func (cbi *CandidatesBucketsIndexer) PutBuckets(height uint64, buckets *iotextypes.VoteBucketList) error {
bucketsBytes, err := proto.Marshal(buckets)
if err != nil {
return err
}
if err := cbi.putToIndexer(StakingBucketsNamespace, height, bucketsBytes); err != nil {
return err
}
cbi.latestBucketsHeight = height
return nil
}
// GetBuckets gets vote buckets from indexer given epoch start height
func (cbi *CandidatesBucketsIndexer) GetBuckets(height uint64, offset, limit uint32) (*iotextypes.VoteBucketList, uint64, error) {
if height > cbi.latestBucketsHeight {
height = cbi.latestBucketsHeight
}
buckets := &iotextypes.VoteBucketList{}
ret, err := getFromIndexer(cbi.kvStore, StakingBucketsNamespace, height)
cause := errors.Cause(err)
if cause == db.ErrNotExist || cause == db.ErrBucketNotExist {
return buckets, height, nil
}
if err != nil {
return nil, height, err
}
if err := proto.Unmarshal(ret, buckets); err != nil {
return nil, height, err
}
length := uint32(len(buckets.Buckets))
if offset >= length {
return &iotextypes.VoteBucketList{}, height, nil
}
end := offset + limit
if end > uint32(len(buckets.Buckets)) {
end = uint32(len(buckets.Buckets))
}
buckets.Buckets = buckets.Buckets[offset:end]
return buckets, height, nil
}
func (cbi *CandidatesBucketsIndexer) putToIndexer(ns string, height uint64, data []byte) error {
var (
h = hash.Hash160b(data)
dataExist bool
heightKey []byte
latestHash []byte
)
switch ns {
case StakingCandidatesNamespace:
dataExist = (h == cbi.latestCandidatesHash)
heightKey = _candHeightKey
latestHash = _latestCandidatesHash
case StakingBucketsNamespace:
dataExist = (h == cbi.latestBucketsHash)
heightKey = _bucketHeightKey
latestHash = _latestBucketsHash
default:
return ErrTypeAssertion
}
heightBytes := byteutil.Uint64ToBytesBigEndian(height)
if dataExist {
// same bytes already exist, do nothing
return cbi.kvStore.Put(StakingMetaNamespace, heightKey, heightBytes)
}
// update latest height
b := batch.NewBatch()
b.Put(ns, heightBytes, data, "failed to write data bytes")
b.Put(StakingMetaNamespace, heightKey, heightBytes, "failed to update indexer height")
b.Put(StakingMetaNamespace, latestHash, h[:], "failed to update latest hash")
if err := cbi.kvStore.WriteBatch(b); err != nil {
return err
}
// update latest hash
if ns == StakingCandidatesNamespace {
cbi.latestCandidatesHash = h
} else {
cbi.latestBucketsHash = h
}
return nil
}
func getFromIndexer(kv db.KVStoreForRangeIndex, ns string, height uint64) ([]byte, error) {
b, err := kv.Get(ns, byteutil.Uint64ToBytesBigEndian(height))
switch errors.Cause(err) {
case nil:
return b, nil
case db.ErrNotExist:
// height does not exist, fallback to previous height
return kv.SeekPrev([]byte(ns), height)
default:
return nil, err
}
}