-
Notifications
You must be signed in to change notification settings - Fork 320
/
candidate_statemanager.go
338 lines (286 loc) · 9.43 KB
/
candidate_statemanager.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
// 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 (
"math/big"
"github.com/pkg/errors"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/state"
)
// const
const (
_stakingCandCenter = "candCenter"
)
type (
// BucketSet related to setting bucket
BucketSet interface {
updateBucket(index uint64, bucket *VoteBucket) error
putBucket(bucket *VoteBucket) (uint64, error)
delBucket(index uint64) error
putBucketAndIndex(bucket *VoteBucket) (uint64, error)
delBucketAndIndex(owner, cand address.Address, index uint64) error
putBucketIndex(addr address.Address, prefix byte, index uint64) error
delBucketIndex(addr address.Address, prefix byte, index uint64) error
}
// CandidateSet related to setting candidates
CandidateSet interface {
putCandidate(d *Candidate) error
delCandidate(name address.Address) error
putVoterBucketIndex(addr address.Address, index uint64) error
delVoterBucketIndex(addr address.Address, index uint64) error
putCandBucketIndex(addr address.Address, index uint64) error
delCandBucketIndex(addr address.Address, index uint64) error
}
// CandidateStateManager is candidate state manager on top of StateManager
CandidateStateManager interface {
BucketSet
BucketGetByIndex
CandidateSet
// candidate and bucket pool related
DirtyView() *ViewData
ContainsName(string) bool
ContainsOwner(address.Address) bool
ContainsOperator(address.Address) bool
ContainsSelfStakingBucket(uint64) bool
GetByName(string) *Candidate
GetByOwner(address.Address) *Candidate
GetBySelfStakingIndex(uint64) *Candidate
Upsert(*Candidate) error
CreditBucketPool(*big.Int) error
DebitBucketPool(*big.Int, bool) error
Commit() error
SM() protocol.StateManager
}
candSM struct {
protocol.StateManager
candCenter *CandidateCenter
bucketPool *BucketPool
}
)
// NewCandidateStateManager returns a new CandidateStateManager instance
func NewCandidateStateManager(sm protocol.StateManager, enableSMStorage bool) (CandidateStateManager, error) {
// TODO: we can store csm in a local cache, just as how statedb store the workingset
// b/c most time the sm is used before, no need to create another clone
csr, err := ConstructBaseView(sm)
if err != nil {
return nil, err
}
// make a copy of candidate center and bucket pool, so they can be modified by csm
// and won't affect base view until being committed
view := csr.BaseView()
csm := &candSM{
StateManager: sm,
candCenter: view.candCenter.Base(),
bucketPool: view.bucketPool.Copy(enableSMStorage),
}
// extract view change from SM
if err := csm.bucketPool.Sync(sm); err != nil {
return nil, errors.Wrap(err, "failed to sync bucket pool")
}
if err := csm.candCenter.Sync(sm); err != nil {
return nil, errors.Wrap(err, "failed to sync candidate center")
}
return csm, nil
}
func newCandidateStateManager(sm protocol.StateManager) CandidateStateManager {
return &candSM{
StateManager: sm,
}
}
func (csm *candSM) SM() protocol.StateManager {
return csm.StateManager
}
// DirtyView is csm's current state, which reflects base view + applying delta saved in csm's dock
func (csm *candSM) DirtyView() *ViewData {
return &ViewData{
candCenter: csm.candCenter,
bucketPool: csm.bucketPool,
}
}
func (csm *candSM) ContainsName(name string) bool {
return csm.candCenter.ContainsName(name)
}
func (csm *candSM) ContainsOwner(addr address.Address) bool {
return csm.candCenter.ContainsOwner(addr)
}
func (csm *candSM) ContainsOperator(addr address.Address) bool {
return csm.candCenter.ContainsOperator(addr)
}
func (csm *candSM) ContainsSelfStakingBucket(index uint64) bool {
return csm.candCenter.ContainsSelfStakingBucket(index)
}
func (csm *candSM) GetByName(name string) *Candidate {
return csm.candCenter.GetByName(name)
}
func (csm *candSM) GetByOwner(addr address.Address) *Candidate {
return csm.candCenter.GetByOwner(addr)
}
func (csm *candSM) GetBySelfStakingIndex(index uint64) *Candidate {
return csm.candCenter.GetBySelfStakingIndex(index)
}
// Upsert writes the candidate into state manager and cand center
func (csm *candSM) Upsert(d *Candidate) error {
if err := csm.candCenter.Upsert(d); err != nil {
return err
}
if err := csm.putCandidate(d); err != nil {
return err
}
delta := csm.candCenter.Delta()
if len(delta) == 0 {
return nil
}
// load change to sm
return csm.StateManager.Load(_protocolID, _stakingCandCenter, &delta)
}
func (csm *candSM) CreditBucketPool(amount *big.Int) error {
return csm.bucketPool.CreditPool(csm.StateManager, amount)
}
func (csm *candSM) DebitBucketPool(amount *big.Int, newBucket bool) error {
return csm.bucketPool.DebitPool(csm, amount, newBucket)
}
func (csm *candSM) Commit() error {
if err := csm.candCenter.Commit(); err != nil {
return err
}
if err := csm.bucketPool.Commit(csm); err != nil {
return err
}
// write updated view back to state factory
return csm.WriteView(_protocolID, csm.DirtyView())
}
func (csm *candSM) getBucket(index uint64) (*VoteBucket, error) {
return newCandidateStateReader(csm).getBucket(index)
}
func (csm *candSM) updateBucket(index uint64, bucket *VoteBucket) error {
if _, err := csm.getBucket(index); err != nil {
return err
}
_, err := csm.PutState(
bucket,
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(bucketKey(index)))
return err
}
func (csm *candSM) putBucket(bucket *VoteBucket) (uint64, error) {
var tc totalBucketCount
if _, err := csm.State(
&tc,
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(TotalBucketKey)); err != nil && errors.Cause(err) != state.ErrStateNotExist {
return 0, err
}
index := tc.Count()
// Add index inside bucket
bucket.Index = index
if _, err := csm.PutState(
bucket,
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(bucketKey(index))); err != nil {
return 0, err
}
tc.count++
_, err := csm.PutState(
&tc,
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(TotalBucketKey))
return index, err
}
func (csm *candSM) delBucket(index uint64) error {
_, err := csm.DelState(
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(bucketKey(index)))
return err
}
func (csm *candSM) putBucketAndIndex(bucket *VoteBucket) (uint64, error) {
index, err := csm.putBucket(bucket)
if err != nil {
return 0, errors.Wrap(err, "failed to put bucket")
}
if err := csm.putVoterBucketIndex(bucket.Owner, index); err != nil {
return 0, errors.Wrap(err, "failed to put bucket index")
}
if err := csm.putCandBucketIndex(bucket.Candidate, index); err != nil {
return 0, errors.Wrap(err, "failed to put candidate index")
}
return index, nil
}
func (csm *candSM) delBucketAndIndex(owner, cand address.Address, index uint64) error {
if err := csm.delBucket(index); err != nil {
return errors.Wrap(err, "failed to delete bucket")
}
if err := csm.delVoterBucketIndex(owner, index); err != nil {
return errors.Wrap(err, "failed to delete bucket index")
}
if err := csm.delCandBucketIndex(cand, index); err != nil {
return errors.Wrap(err, "failed to delete candidate index")
}
return nil
}
func (csm *candSM) putBucketIndex(addr address.Address, prefix byte, index uint64) error {
var (
bis BucketIndices
key = AddrKeyWithPrefix(addr, prefix)
)
if _, err := csm.State(
&bis,
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(key)); err != nil && errors.Cause(err) != state.ErrStateNotExist {
return err
}
bis.addBucketIndex(index)
_, err := csm.PutState(
&bis,
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(key))
return err
}
func (csm *candSM) putVoterBucketIndex(addr address.Address, index uint64) error {
return csm.putBucketIndex(addr, _voterIndex, index)
}
func (csm *candSM) delBucketIndex(addr address.Address, prefix byte, index uint64) error {
var (
bis BucketIndices
key = AddrKeyWithPrefix(addr, prefix)
)
if _, err := csm.State(
&bis,
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(key)); err != nil {
return err
}
bis.deleteBucketIndex(index)
var err error
if len(bis) == 0 {
_, err = csm.DelState(
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(key))
} else {
_, err = csm.PutState(
&bis,
protocol.NamespaceOption(StakingNameSpace),
protocol.KeyOption(key))
}
return err
}
func (csm *candSM) delVoterBucketIndex(addr address.Address, index uint64) error {
return csm.delBucketIndex(addr, _voterIndex, index)
}
func (csm *candSM) putCandidate(d *Candidate) error {
_, err := csm.PutState(d, protocol.NamespaceOption(CandidateNameSpace), protocol.KeyOption(d.Owner.Bytes()))
return err
}
func (csm *candSM) putCandBucketIndex(addr address.Address, index uint64) error {
return csm.putBucketIndex(addr, _candIndex, index)
}
func (csm *candSM) delCandidate(name address.Address) error {
_, err := csm.DelState(protocol.NamespaceOption(CandidateNameSpace), protocol.KeyOption(name.Bytes()))
return err
}
func (csm *candSM) delCandBucketIndex(addr address.Address, index uint64) error {
return csm.delBucketIndex(addr, _candIndex, index)
}