@@ -23,6 +23,7 @@ import (
23
23
"github.com/iotexproject/iotex-core/action"
24
24
"github.com/iotexproject/iotex-core/action/protocol"
25
25
accountutil "github.com/iotexproject/iotex-core/action/protocol/account/util"
26
+ "github.com/iotexproject/iotex-core/action/protocol/rolldpos"
26
27
"github.com/iotexproject/iotex-core/blockchain/genesis"
27
28
"github.com/iotexproject/iotex-core/config"
28
29
"github.com/iotexproject/iotex-core/pkg/log"
@@ -65,10 +66,11 @@ type (
65
66
66
67
// Protocol defines the protocol of handling staking
67
68
Protocol struct {
68
- addr address.Address
69
- depositGas DepositGas
70
- config Configuration
71
- hu config.HeightUpgrade
69
+ addr address.Address
70
+ depositGas DepositGas
71
+ config Configuration
72
+ hu config.HeightUpgrade
73
+ candBucketsIndexer * CandidatesBucketsIndexer
72
74
}
73
75
74
76
// Configuration is the staking protocol configuration.
85
87
)
86
88
87
89
// NewProtocol instantiates the protocol of staking
88
- func NewProtocol (depositGas DepositGas , cfg genesis.Staking ) (* Protocol , error ) {
90
+ func NewProtocol (depositGas DepositGas , cfg genesis.Staking , candBucketsIndexer * CandidatesBucketsIndexer ) (* Protocol , error ) {
89
91
h := hash .Hash160b ([]byte (protocolID ))
90
92
addr , err := address .FromBytes (h [:])
91
93
if err != nil {
@@ -119,7 +121,8 @@ func NewProtocol(depositGas DepositGas, cfg genesis.Staking) (*Protocol, error)
119
121
MinStakeAmount : minStakeAmount ,
120
122
BootstrapCandidates : cfg .BootstrapCandidates ,
121
123
},
122
- depositGas : depositGas ,
124
+ depositGas : depositGas ,
125
+ candBucketsIndexer : candBucketsIndexer ,
123
126
}, nil
124
127
}
125
128
@@ -206,15 +209,51 @@ func (p *Protocol) CreatePreStates(ctx context.Context, sm protocol.StateManager
206
209
bcCtx := protocol .MustGetBlockchainCtx (ctx )
207
210
blkCtx := protocol .MustGetBlockCtx (ctx )
208
211
hu := config .NewHeightUpgrade (& bcCtx .Genesis )
209
- if blkCtx .BlockHeight != hu .GreenlandBlockHeight () {
212
+ if blkCtx .BlockHeight == hu .GreenlandBlockHeight () {
213
+ csr , err := ConstructBaseView (sm )
214
+ if err != nil {
215
+ return err
216
+ }
217
+ if _ , err = sm .PutState (csr .BaseView ().bucketPool .total , protocol .NamespaceOption (StakingNameSpace ), protocol .KeyOption (bucketPoolAddrKey )); err != nil {
218
+ return err
219
+ }
220
+ }
221
+
222
+ if p .candBucketsIndexer == nil {
210
223
return nil
211
224
}
212
- csr , err := ConstructBaseView (sm )
225
+ rp := rolldpos .MustGetProtocol (protocol .MustGetRegistry (ctx ))
226
+ currentEpochNum := rp .GetEpochNum (blkCtx .BlockHeight )
227
+ if currentEpochNum == 0 {
228
+ return nil
229
+ }
230
+ epochStartHeight := rp .GetEpochHeight (currentEpochNum )
231
+ if epochStartHeight != blkCtx .BlockHeight || hu .IsPre (config .Fairbank , epochStartHeight ) {
232
+ return nil
233
+ }
234
+
235
+ return p .handleStakingIndexer (rp .GetEpochHeight (currentEpochNum - 1 ), sm )
236
+ }
237
+
238
+ func (p * Protocol ) handleStakingIndexer (epochStartHeight uint64 , sm protocol.StateManager ) error {
239
+ allBuckets , _ , err := getAllBuckets (sm )
240
+ if err != nil && errors .Cause (err ) != state .ErrStateNotExist {
241
+ return err
242
+ }
243
+ buckets , err := toIoTeXTypesVoteBucketList (allBuckets )
213
244
if err != nil {
214
245
return err
215
246
}
216
- _ , err = sm .PutState (csr .BaseView ().bucketPool .total , protocol .NamespaceOption (StakingNameSpace ), protocol .KeyOption (bucketPoolAddrKey ))
217
- return err
247
+ err = p .candBucketsIndexer .PutBuckets (epochStartHeight , buckets )
248
+ if err != nil {
249
+ return err
250
+ }
251
+ all , _ , err := getAllCandidates (sm )
252
+ if err != nil && errors .Cause (err ) != state .ErrStateNotExist {
253
+ return err
254
+ }
255
+ candidateList := toIoTeXTypesCandidateListV2 (all )
256
+ return p .candBucketsIndexer .PutCandidates (epochStartHeight , candidateList )
218
257
}
219
258
220
259
// Commit commits the last change
@@ -370,12 +409,24 @@ func (p *Protocol) ReadState(ctx context.Context, sr protocol.StateReader, metho
370
409
return nil , 0 , err
371
410
}
372
411
412
+ // get height arg
413
+ inputHeight , err := sr .Height ()
414
+ if err != nil {
415
+ return nil , 0 , err
416
+ }
417
+ rp := rolldpos .MustGetProtocol (protocol .MustGetRegistry (ctx ))
418
+ epochStartHeight := rp .GetEpochHeight (rp .GetEpochNum (inputHeight ))
419
+
373
420
var (
374
421
height uint64
375
422
resp proto.Message
376
423
)
377
424
switch m .GetMethod () {
378
425
case iotexapi .ReadStakingDataMethod_BUCKETS :
426
+ if epochStartHeight != 0 && p .candBucketsIndexer != nil {
427
+ ret , err := p .candBucketsIndexer .GetBuckets (epochStartHeight , r .GetBuckets ().GetPagination ().GetOffset (), r .GetBuckets ().GetPagination ().GetLimit ())
428
+ return ret , epochStartHeight , err
429
+ }
379
430
resp , height , err = readStateBuckets (ctx , sr , r .GetBuckets ())
380
431
case iotexapi .ReadStakingDataMethod_BUCKETS_BY_VOTER :
381
432
resp , height , err = readStateBucketsByVoter (ctx , sr , r .GetBucketsByVoter ())
@@ -384,6 +435,10 @@ func (p *Protocol) ReadState(ctx context.Context, sr protocol.StateReader, metho
384
435
case iotexapi .ReadStakingDataMethod_BUCKETS_BY_INDEXES :
385
436
resp , height , err = readStateBucketByIndices (ctx , sr , r .GetBucketsByIndexes ())
386
437
case iotexapi .ReadStakingDataMethod_CANDIDATES :
438
+ if epochStartHeight != 0 && p .candBucketsIndexer != nil {
439
+ ret , err := p .candBucketsIndexer .GetCandidates (epochStartHeight , r .GetCandidates ().GetPagination ().GetOffset (), r .GetCandidates ().GetPagination ().GetLimit ())
440
+ return ret , epochStartHeight , err
441
+ }
387
442
resp , height , err = readStateCandidates (ctx , csr , r .GetCandidates ())
388
443
case iotexapi .ReadStakingDataMethod_CANDIDATE_BY_NAME :
389
444
resp , height , err = readStateCandidateByName (ctx , csr , r .GetCandidateByName ())
0 commit comments