Skip to content

Commit

Permalink
make contractstakingindexer nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Jun 7, 2023
1 parent fc67863 commit 8adeee9
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 13 deletions.
4 changes: 2 additions & 2 deletions action/protocol/staking/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions action/protocol/staking/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 46 additions & 3 deletions action/protocol/staking/staking_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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()
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/vote_reviser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestVoteReviser(t *testing.T) {
PersistStakingPatchBlock: math.MaxUint64,
},
nil,
&emptyContractStakingIndexer{},
nil,
genesis.Default.OkhotskBlockHeight,
genesis.Default.HawaiiBlockHeight,
genesis.Default.GreenlandBlockHeight,
Expand Down
2 changes: 1 addition & 1 deletion chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8adeee9

Please sign in to comment.