/
util.go
371 lines (348 loc) · 13.2 KB
/
util.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
// 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 poll
import (
"context"
"fmt"
"math"
"math/big"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
"github.com/iotexproject/iotex-core/action/protocol/rolldpos"
"github.com/iotexproject/iotex-core/action/protocol/vote"
"github.com/iotexproject/iotex-core/action/protocol/vote/candidatesutil"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/state"
)
func validateDelegates(cs state.CandidateList) error {
zero := big.NewInt(0)
addrs := map[string]bool{}
lastVotes := zero
for _, candidate := range cs {
if _, exists := addrs[candidate.Address]; exists {
return errors.Errorf("duplicate candidate %s", candidate.Address)
}
addrs[candidate.Address] = true
if candidate.Votes.Cmp(zero) < 0 {
return errors.New("votes for candidate cannot be negative")
}
if lastVotes.Cmp(zero) > 0 && lastVotes.Cmp(candidate.Votes) < 0 {
return errors.New("candidate list is not sorted")
}
}
return nil
}
func handle(ctx context.Context, act action.Action, sm protocol.StateManager, indexer *CandidateIndexer, protocolAddr string) (*action.Receipt, error) {
actionCtx := protocol.MustGetActionCtx(ctx)
blkCtx := protocol.MustGetBlockCtx(ctx)
r, ok := act.(*action.PutPollResult)
if !ok {
return nil, nil
}
zap.L().Debug("Handle PutPollResult Action", zap.Uint64("height", r.Height()))
if err := setCandidates(ctx, sm, indexer, r.Candidates(), r.Height()); err != nil {
return nil, errors.Wrap(err, "failed to set candidates")
}
return &action.Receipt{
Status: uint64(iotextypes.ReceiptStatus_Success),
ActionHash: actionCtx.ActionHash,
BlockHeight: blkCtx.BlockHeight,
GasConsumed: actionCtx.IntrinsicGas,
ContractAddress: protocolAddr,
}, nil
}
func validate(ctx context.Context, sr protocol.StateReader, p Protocol, act action.Action) error {
ppr, ok := act.(*action.PutPollResult)
if !ok {
return nil
}
actionCtx := protocol.MustGetActionCtx(ctx)
blkCtx := protocol.MustGetBlockCtx(ctx)
if blkCtx.Producer.String() != actionCtx.Caller.String() {
return errors.New("Only producer could create this protocol")
}
proposedDelegates := ppr.Candidates()
if err := validateDelegates(proposedDelegates); err != nil {
return err
}
ds, err := p.CalculateCandidatesByHeight(ctx, sr, blkCtx.BlockHeight)
if err != nil {
return err
}
if len(ds) != len(proposedDelegates) {
msg := fmt.Sprintf(", %d, is not as expected, %d",
len(proposedDelegates),
len(ds))
return errors.Wrap(ErrProposedDelegatesLength, msg)
}
for i, d := range ds {
if !proposedDelegates[i].Equal(d) {
msg := fmt.Sprintf(", %v vs %v (expected)",
proposedDelegates,
ds)
return errors.Wrap(ErrDelegatesNotAsExpected, msg)
}
}
return nil
}
func createPostSystemActions(ctx context.Context, sr protocol.StateReader, p Protocol) ([]action.Envelope, error) {
blkCtx := protocol.MustGetBlockCtx(ctx)
featureCtx := protocol.MustGetFeatureWithHeightCtx(ctx)
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
epochNum := rp.GetEpochNum(blkCtx.BlockHeight)
lastBlkHeight := rp.GetEpochLastBlockHeight(epochNum)
epochHeight := rp.GetEpochHeight(epochNum)
nextEpochHeight := rp.GetEpochHeight(epochNum + 1)
// make sure that putpollresult action is created around half of each epoch
if blkCtx.BlockHeight < epochHeight+(nextEpochHeight-epochHeight)/2 {
return nil, nil
}
loadCandidatesLegacy := featureCtx.LoadCandidatesLegacy(nextEpochHeight)
if _, _, err := candidatesutil.CandidatesFromDB(sr, nextEpochHeight, loadCandidatesLegacy, true); errors.Cause(err) != state.ErrStateNotExist {
return nil, err
}
log.L().Debug(
"createPutPollResultAction",
zap.Uint64("height", blkCtx.BlockHeight),
zap.Uint64("epochNum", epochNum),
zap.Uint64("epochHeight", epochHeight),
zap.Uint64("nextEpochHeight", nextEpochHeight),
)
l, err := p.CalculateCandidatesByHeight(ctx, sr, epochHeight)
if err == nil && len(l) == 0 {
err = errors.Wrapf(
ErrDelegatesNotExist,
"failed to fetch delegates by epoch height %d, empty list",
epochHeight,
)
}
if err != nil && blkCtx.BlockHeight == lastBlkHeight {
return nil, errors.Wrapf(
err,
"failed to prepare delegates for next epoch %d",
epochNum+1,
)
}
nonce := uint64(0)
pollAction := action.NewPutPollResult(nonce, nextEpochHeight, l)
builder := action.EnvelopeBuilder{}
return []action.Envelope{builder.SetNonce(nonce).SetAction(pollAction).Build()}, nil
}
// setCandidates sets the candidates for the given state manager
func setCandidates(
ctx context.Context,
sm protocol.StateManager,
indexer *CandidateIndexer,
candidates state.CandidateList,
height uint64, // epoch start height
) error {
featureCtx := protocol.MustGetFeatureWithHeightCtx(ctx)
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
epochNum := rp.GetEpochNum(height)
if height != rp.GetEpochHeight(epochNum) {
return errors.New("put poll result height should be epoch start height")
}
loadCandidatesLegacy := featureCtx.LoadCandidatesLegacy(height)
accountCreationOpts := []state.AccountCreationOption{}
if protocol.MustGetFeatureCtx(ctx).CreateLegacyNonceAccount {
accountCreationOpts = append(accountCreationOpts, state.LegacyNonceAccountTypeOption())
} else {
accountCreationOpts = append(accountCreationOpts, state.DelegateCandidateOption())
}
for _, candidate := range candidates {
addr, err := address.FromString(candidate.Address)
if err != nil {
return errors.Wrapf(err, "failed to decode delegate address %s", candidate.Address)
}
delegate, err := accountutil.LoadOrCreateAccount(sm, addr, accountCreationOpts...)
if err != nil {
return errors.Wrapf(err, "failed to load or create the account for delegate %s", candidate.Address)
}
if protocol.MustGetFeatureCtx(ctx).CreateLegacyNonceAccount {
delegate.MarkAsCandidate()
}
if loadCandidatesLegacy {
if err := candidatesutil.LoadAndAddCandidates(sm, height, candidate.Address); err != nil {
return err
}
}
candAddr, err := address.FromString(candidate.Address)
if err != nil && protocol.MustGetFeatureCtx(ctx).FixUnproductiveDelegates {
return errors.Wrap(err, "failed to convert candidate address")
}
if err := accountutil.StoreAccount(sm, candAddr, delegate); err != nil {
return errors.Wrap(err, "failed to update pending account changes to trie")
}
log.L().Debug(
"add candidate",
zap.String("address", candidate.Address),
zap.String("rewardAddress", candidate.RewardAddress),
zap.String("score", candidate.Votes.String()),
)
}
if indexer != nil {
if err := indexer.PutCandidateList(height, &candidates); err != nil {
return errors.Wrapf(err, "failed to put candidatelist into indexer at height %d", height)
}
}
if loadCandidatesLegacy {
_, err := sm.PutState(&candidates, protocol.LegacyKeyOption(candidatesutil.ConstructLegacyKey(height)))
return err
}
nextKey := candidatesutil.ConstructKey(candidatesutil.NxtCandidateKey)
_, err := sm.PutState(&candidates, protocol.KeyOption(nextKey[:]), protocol.NamespaceOption(protocol.SystemNamespace))
return err
}
// setNextEpochProbationList sets the probation list with next key
func setNextEpochProbationList(
sm protocol.StateManager,
indexer *CandidateIndexer,
height uint64,
probationlist *vote.ProbationList,
) error {
if indexer != nil {
if err := indexer.PutProbationList(height, probationlist); err != nil {
return errors.Wrapf(err, "failed to put probationlist into indexer at height %d", height)
}
}
probationListKey := candidatesutil.ConstructKey(candidatesutil.NxtProbationKey)
_, err := sm.PutState(probationlist, protocol.KeyOption(probationListKey[:]), protocol.NamespaceOption(protocol.SystemNamespace))
return err
}
// setUnproductiveDelegates sets the upd struct with updkey
func setUnproductiveDelegates(
sm protocol.StateManager,
upd *vote.UnproductiveDelegate,
) error {
updKey := candidatesutil.ConstructKey(candidatesutil.UnproductiveDelegateKey)
_, err := sm.PutState(upd, protocol.KeyOption(updKey[:]), protocol.NamespaceOption(protocol.SystemNamespace))
return err
}
// shiftCandidates updates current data with next data of candidate list
func shiftCandidates(sm protocol.StateManager) (uint64, error) {
zap.L().Debug("Shift candidatelist from next key to current key")
var next state.CandidateList
var err error
var stateHeight, putStateHeight, delStateHeight uint64
nextKey := candidatesutil.ConstructKey(candidatesutil.NxtCandidateKey)
if stateHeight, err = sm.State(&next, protocol.KeyOption(nextKey[:]), protocol.NamespaceOption(protocol.SystemNamespace)); err != nil {
return 0, errors.Wrap(
err,
"failed to read next candidateList when shifting to current candidateList",
)
}
curKey := candidatesutil.ConstructKey(candidatesutil.CurCandidateKey)
if putStateHeight, err = sm.PutState(&next, protocol.KeyOption(curKey[:]), protocol.NamespaceOption(protocol.SystemNamespace)); err != nil {
return 0, errors.Wrap(
err,
"failed to write current candidateList when shifting from next candidateList to current candidateList",
)
}
if stateHeight != putStateHeight {
return 0, errors.Wrap(ErrInconsistentHeight, "failed to shift candidates")
}
if delStateHeight, err = sm.DelState(protocol.KeyOption(nextKey[:]), protocol.NamespaceOption(protocol.SystemNamespace)); err != nil {
return 0, errors.Wrap(
err,
"failed to delete next candidatelist after shifting",
)
}
if stateHeight != delStateHeight {
return 0, errors.Wrap(ErrInconsistentHeight, "failed to shift candidates")
}
return stateHeight, nil
}
// shiftProbationList updates current data with next data of probation list
func shiftProbationList(sm protocol.StateManager) (uint64, error) {
zap.L().Debug("Shift probationList from next key to current key")
var err error
var stateHeight, putStateHeight, delStateHeight uint64
next := &vote.ProbationList{}
nextKey := candidatesutil.ConstructKey(candidatesutil.NxtProbationKey)
if stateHeight, err = sm.State(next, protocol.KeyOption(nextKey[:]), protocol.NamespaceOption(protocol.SystemNamespace)); err != nil {
return 0, errors.Wrap(
err,
"failed to read next probationlist when shifting to current probationlist",
)
}
curKey := candidatesutil.ConstructKey(candidatesutil.CurProbationKey)
if putStateHeight, err = sm.PutState(next, protocol.KeyOption(curKey[:]), protocol.NamespaceOption(protocol.SystemNamespace)); err != nil {
return 0, errors.Wrap(
err,
"failed to write current probationlist when shifting from next probationlist to current probationlist",
)
}
if stateHeight != putStateHeight {
return 0, errors.Wrap(ErrInconsistentHeight, "failed to shift candidates")
}
if delStateHeight, err = sm.DelState(protocol.KeyOption(nextKey[:]), protocol.NamespaceOption(protocol.SystemNamespace)); err != nil {
return 0, errors.Wrap(
err,
"failed to delete next probationlist after shifting",
)
}
if stateHeight != delStateHeight {
return 0, errors.Wrap(ErrInconsistentHeight, "failed to shift probationlist")
}
return stateHeight, nil
}
// setCurrentBlockMeta sets the block meta struct with block meta key
func setCurrentBlockMeta(
sm protocol.StateManager,
blockmeta *BlockMeta,
height uint64,
blocksInEpoch uint64,
) error {
key := blockMetaKey(height, blocksInEpoch)
_, err := sm.PutState(blockmeta, protocol.KeyOption(key), protocol.NamespaceOption(protocol.SystemNamespace))
return err
}
// allBlockMetasFromDB returns all latest block meta structs
func allBlockMetasFromDB(sr protocol.StateReader, blocksInEpoch uint64) ([]*BlockMeta, error) {
keys := make([][]byte, blocksInEpoch)
for i := uint64(0); i < blocksInEpoch; i++ {
keys[i] = blockMetaKey(i, blocksInEpoch)
}
stateHeight, iter, err := sr.States(
protocol.NamespaceOption(protocol.SystemNamespace),
protocol.KeysOption(func() ([][]byte, error) {
return keys, nil
}),
)
if err != nil {
return nil, err
}
log.L().Debug(
"allBockMetasFromDB",
zap.Uint64("state height", stateHeight),
zap.Error(err),
)
blockmetas := make([]*BlockMeta, 0, iter.Size())
for i := 0; i < iter.Size(); i++ {
bm := &BlockMeta{}
switch err := iter.Next(bm); errors.Cause(err) {
case nil:
blockmetas = append(blockmetas, bm)
case state.ErrNilValue:
default:
return nil, errors.Wrapf(err, "failed to deserialize block meta")
}
}
return blockmetas, nil
}
// blockMetaKey returns key for storing block meta with prefix
func blockMetaKey(blkHeight uint64, blocksInEpoch uint64) []byte {
prefixKey := candidatesutil.ConstructKey(_blockMetaPrefix)
if blkHeight == math.MaxUint64 {
return append(prefixKey[:], byteutil.Uint64ToBytesBigEndian(blocksInEpoch)...)
}
return append(prefixKey[:], byteutil.Uint64ToBytesBigEndian(blkHeight%blocksInEpoch)...)
}