diff --git a/action/protocol/staking/handlers_test.go b/action/protocol/staking/handlers_test.go index 272a8bc3cd..40b194b1ee 100644 --- a/action/protocol/staking/handlers_test.go +++ b/action/protocol/staking/handlers_test.go @@ -84,7 +84,7 @@ func TestProtocol_HandleCreateStake(t *testing.T) { p, err := NewProtocol(depositGas, &BuilderConfig{ Staking: genesis.Default.Staking, PersistStakingPatchBlock: math.MaxUint64, - }, nil, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight) + }, nil, nil, genesis.Default.GreenlandBlockHeight) require.NoError(err) // set up candidate @@ -2680,7 +2680,7 @@ func initAll(t *testing.T, ctrl *gomock.Controller) (protocol.StateManager, *Pro p, err := NewProtocol(depositGas, &BuilderConfig{ Staking: genesis.Default.Staking, PersistStakingPatchBlock: math.MaxUint64, - }, nil, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight) + }, nil, nil, genesis.Default.GreenlandBlockHeight) require.NoError(err) // set up candidate diff --git a/action/protocol/staking/protocol.go b/action/protocol/staking/protocol.go index f3b0db9267..72b707373a 100644 --- a/action/protocol/staking/protocol.go +++ b/action/protocol/staking/protocol.go @@ -475,7 +475,7 @@ func (p *Protocol) ActiveCandidates(ctx context.Context, sr protocol.StateReader list := c.AllCandidates() cand := make(CandidateList, 0, len(list)) for i := range list { - if ok && featureCtx.AddContractStakingVotes { + if p.contractStakingIndexer != nil && ok && featureCtx.AddContractStakingVotes { list[i].Votes.Add(list[i].Votes, p.contractStakingIndexer.CandidateVotes(list[i].Owner)) } if list[i].SelfStake.Cmp(p.config.RegistrationConsts.MinSelfStake) >= 0 { diff --git a/action/protocol/staking/protocol_test.go b/action/protocol/staking/protocol_test.go index 43cf1ed5f8..1167bce3a3 100644 --- a/action/protocol/staking/protocol_test.go +++ b/action/protocol/staking/protocol_test.go @@ -89,7 +89,7 @@ func TestProtocol(t *testing.T) { stk, err := NewProtocol(nil, &BuilderConfig{ Staking: genesis.Default.Staking, PersistStakingPatchBlock: math.MaxUint64, - }, nil, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight) + }, nil, nil, genesis.Default.GreenlandBlockHeight) r.NotNil(stk) r.NoError(err) buckets, _, err := csr.getAllBuckets() @@ -199,7 +199,7 @@ func TestCreatePreStates(t *testing.T) { p, err := NewProtocol(nil, &BuilderConfig{ Staking: genesis.Default.Staking, PersistStakingPatchBlock: math.MaxUint64, - }, nil, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight, genesis.Default.GreenlandBlockHeight) + }, nil, nil, genesis.Default.GreenlandBlockHeight, genesis.Default.GreenlandBlockHeight) require.NoError(err) ctx := protocol.WithBlockCtx( genesis.WithGenesisContext(context.Background(), genesis.Default), @@ -262,7 +262,7 @@ func Test_CreatePreStatesWithRegisterProtocol(t *testing.T) { p, err := NewProtocol(nil, &BuilderConfig{ Staking: genesis.Default.Staking, PersistStakingPatchBlock: math.MaxUint64, - }, cbi, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight, genesis.Default.GreenlandBlockHeight) + }, cbi, nil, genesis.Default.GreenlandBlockHeight, genesis.Default.GreenlandBlockHeight) require.NoError(err) rol := rolldpos.NewProtocol(23, 4, 3) @@ -378,7 +378,7 @@ func Test_CreateGenesisStates(t *testing.T) { p, err := NewProtocol(nil, &BuilderConfig{ Staking: cfg, PersistStakingPatchBlock: math.MaxUint64, - }, nil, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight) + }, nil, nil, genesis.Default.GreenlandBlockHeight) require.NoError(err) v, err := p.Start(ctx, sm) diff --git a/action/protocol/staking/staking_statereader.go b/action/protocol/staking/staking_statereader.go index 3379edc9cb..0114671573 100644 --- a/action/protocol/staking/staking_statereader.go +++ b/action/protocol/staking/staking_statereader.go @@ -72,6 +72,12 @@ func (c *compositeStakingStateReader) readStateBuckets(ctx context.Context, req return nil, 0, err } } + + if !c.isContractStakingEnabled() { + buckets.Buckets = getPageOfArray(buckets.Buckets, int(req.GetPagination().GetOffset()), int(req.GetPagination().GetLimit())) + return buckets, height, nil + } + // read LSD buckets lsdBuckets, err := c.contractIndexer.Buckets() if err != nil { @@ -81,9 +87,9 @@ func (c *compositeStakingStateReader) readStateBuckets(ctx context.Context, req if err != nil { return nil, 0, err } - // merge native and LSD buckets buckets.Buckets = append(buckets.Buckets, lsdIoTeXBuckets.Buckets...) + buckets.Buckets = getPageOfArray(buckets.Buckets, int(req.GetPagination().GetOffset()), int(req.GetPagination().GetLimit())) return buckets, height, err } @@ -94,6 +100,10 @@ func (c *compositeStakingStateReader) readStateBucketsByVoter(ctx context.Contex if err != nil { return nil, 0, err } + if !c.isContractStakingEnabled() { + buckets.Buckets = getPageOfArray(buckets.Buckets, int(req.GetPagination().GetOffset()), int(req.GetPagination().GetLimit())) + return buckets, height, err + } // read LSD buckets lsdBuckets, err := c.contractIndexer.Buckets() @@ -105,9 +115,9 @@ func (c *compositeStakingStateReader) readStateBucketsByVoter(ctx context.Contex if err != nil { return nil, 0, err } - // merge native and LSD buckets buckets.Buckets = append(buckets.Buckets, lsdIoTeXBuckets.Buckets...) + buckets.Buckets = getPageOfArray(buckets.Buckets, int(req.GetPagination().GetOffset()), int(req.GetPagination().GetLimit())) return buckets, height, err } @@ -118,6 +128,12 @@ func (c *compositeStakingStateReader) readStateBucketsByCandidate(ctx context.Co if err != nil { return nil, 0, err } + + if !c.isContractStakingEnabled() { + buckets.Buckets = getPageOfArray(buckets.Buckets, int(req.GetPagination().GetOffset()), int(req.GetPagination().GetLimit())) + return buckets, height, err + } + // read LSD buckets candidate := c.nativeSR.GetCandidateByName(req.GetCandName()) if candidate == nil { @@ -133,6 +149,7 @@ func (c *compositeStakingStateReader) readStateBucketsByCandidate(ctx context.Co } // merge native and LSD buckets buckets.Buckets = append(buckets.Buckets, lsdIoTeXBuckets.Buckets...) + buckets.Buckets = getPageOfArray(buckets.Buckets, int(req.GetPagination().GetOffset()), int(req.GetPagination().GetLimit())) return buckets, height, err } @@ -143,6 +160,10 @@ func (c *compositeStakingStateReader) readStateBucketByIndices(ctx context.Conte if err != nil { return nil, 0, err } + if !c.isContractStakingEnabled() { + return buckets, height, nil + } + // read LSD buckets lsdBuckets, err := c.contractIndexer.BucketsByIndices(req.GetIndex()) if err != nil { @@ -154,6 +175,7 @@ func (c *compositeStakingStateReader) readStateBucketByIndices(ctx context.Conte } // merge native and LSD buckets buckets.Buckets = append(buckets.Buckets, lsbIoTeXBuckets.Buckets...) + return buckets, height, nil } @@ -162,6 +184,9 @@ func (c *compositeStakingStateReader) readStateBucketCount(ctx context.Context, if err != nil { return nil, 0, err } + if !c.isContractStakingEnabled() { + return bucketCnt, height, nil + } buckets, err := c.contractIndexer.Buckets() if err != nil { return nil, 0, err @@ -204,6 +229,9 @@ func (c *compositeStakingStateReader) readStateCandidates(ctx context.Context, r } } + if !c.isContractStakingEnabled() { + return candidates, height, nil + } for _, candidate := range candidates.Candidates { if err = addContractStakingVotes(candidate, c.contractIndexer); err != nil { return nil, 0, err @@ -217,6 +245,9 @@ func (c *compositeStakingStateReader) readStateCandidateByName(ctx context.Conte if err != nil { return nil, 0, err } + if !c.isContractStakingEnabled() { + return candidate, height, nil + } if !protocol.MustGetFeatureCtx(ctx).AddContractStakingVotes { return candidate, height, nil } @@ -231,6 +262,9 @@ func (c *compositeStakingStateReader) readStateCandidateByAddress(ctx context.Co if err != nil { return nil, 0, err } + if !c.isContractStakingEnabled() { + return candidate, height, nil + } if !protocol.MustGetFeatureCtx(ctx).AddContractStakingVotes { return candidate, height, nil } @@ -250,7 +284,9 @@ func (c *compositeStakingStateReader) readStateTotalStakingAmount(ctx context.Co if !ok { return nil, 0, errors.Errorf("invalid balance %s", accountMeta.Balance) } - + if !c.isContractStakingEnabled() { + return accountMeta, height, nil + } // add contract staking amount buckets, err := c.contractIndexer.Buckets() if err != nil { @@ -265,6 +301,9 @@ func (c *compositeStakingStateReader) readStateTotalStakingAmount(ctx context.Co } func (c *compositeStakingStateReader) readStateContractStakingBucketTypes(ctx context.Context, _ *iotexapi.ReadStakingDataRequest_ContractStakingBucketTypes) (*iotextypes.ContractStakingBucketTypeList, uint64, error) { + if !c.isContractStakingEnabled() { + return &iotextypes.ContractStakingBucketTypeList{}, c.nativeSR.Height(), nil + } bts, err := c.contractIndexer.BucketTypes() if err != nil { return nil, 0, err @@ -279,6 +318,10 @@ func (c *compositeStakingStateReader) readStateContractStakingBucketTypes(ctx co return &iotextypes.ContractStakingBucketTypeList{BucketTypes: pbBts}, c.nativeSR.Height(), nil } +func (c *compositeStakingStateReader) isContractStakingEnabled() bool { + return c.contractIndexer != nil +} + func addContractStakingVotes(candidate *iotextypes.CandidateV2, contractStakingSR ContractStakingIndexer) error { votes, ok := big.NewInt(0).SetString(candidate.TotalWeightedVotes, 10) if !ok { diff --git a/action/protocol/staking/validations_test.go b/action/protocol/staking/validations_test.go index 32c2660966..4dfef6498e 100644 --- a/action/protocol/staking/validations_test.go +++ b/action/protocol/staking/validations_test.go @@ -21,7 +21,7 @@ func initTestProtocol(t *testing.T) (*Protocol, []*Candidate) { p, err := NewProtocol(nil, &BuilderConfig{ Staking: genesis.Default.Staking, PersistStakingPatchBlock: math.MaxUint64, - }, nil, &emptyContractStakingIndexer{}, genesis.Default.GreenlandBlockHeight) + }, nil, nil, genesis.Default.GreenlandBlockHeight) require.NoError(err) var cans []*Candidate diff --git a/action/protocol/staking/vote_reviser_test.go b/action/protocol/staking/vote_reviser_test.go index 7931e47249..b6691c8bec 100644 --- a/action/protocol/staking/vote_reviser_test.go +++ b/action/protocol/staking/vote_reviser_test.go @@ -121,7 +121,7 @@ func TestVoteReviser(t *testing.T) { PersistStakingPatchBlock: math.MaxUint64, }, nil, - &emptyContractStakingIndexer{}, + nil, genesis.Default.OkhotskBlockHeight, genesis.Default.HawaiiBlockHeight, genesis.Default.GreenlandBlockHeight, diff --git a/chainservice/builder.go b/chainservice/builder.go index cd9dd6b535..9ab1b56a87 100644 --- a/chainservice/builder.go +++ b/chainservice/builder.go @@ -294,7 +294,7 @@ func (builder *Builder) buildContractStakingIndexer(forTest bool) error { return nil } if forTest { - builder.cs.contractStakingIndexer = contractstaking.NewDummyContractStakingIndexer() + builder.cs.contractStakingIndexer = nil } else { dbConfig := builder.cfg.DB dbConfig.DbPath = builder.cfg.Chain.ContractStakingIndexDBPath