Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[staking] make contract staking indexer nullable #3883

Merged
merged 8 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions action/protocol/staking/contractstake_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,4 @@ type (
// BucketTypes returns the active bucket types
BucketTypes() ([]*ContractStakingBucketType, error)
}

// TODO (iip-13): remove this empty contract staking indexer
emptyContractStakingIndexer struct{}
)

var _ ContractStakingIndexer = (*emptyContractStakingIndexer)(nil)

// NewEmptyContractStakingIndexer creates an empty contract staking indexer
func NewEmptyContractStakingIndexer() ContractStakingIndexer {
return &emptyContractStakingIndexer{}
}

func (f *emptyContractStakingIndexer) CandidateVotes(ownerAddr address.Address) *big.Int {
return big.NewInt(0)
}

func (f *emptyContractStakingIndexer) Buckets() ([]*VoteBucket, error) {
return nil, nil
}

func (f *emptyContractStakingIndexer) BucketsByIndices([]uint64) ([]*VoteBucket, error) {
return nil, nil
}

func (f *emptyContractStakingIndexer) TotalBucketCount() uint64 {
return 0
}

func (f *emptyContractStakingIndexer) BucketTypes() ([]*ContractStakingBucketType, error) {
return nil, nil
}

func (f *emptyContractStakingIndexer) BucketsByCandidate(ownerAddr address.Address) ([]*VoteBucket, error) {
return nil, nil
}
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
4 changes: 2 additions & 2 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,11 @@ func (p *Protocol) ActiveCandidates(ctx context.Context, sr protocol.StateReader
if err != nil {
return nil, errors.Wrap(err, "failed to get ActiveCandidates")
}
featureCtx, ok := protocol.GetFeatureCtx(ctx)
list := c.AllCandidates()
cand := make(CandidateList, 0, len(list))
featureCtx := protocol.MustGetFeatureCtx(ctx)
for i := range list {
if ok && featureCtx.AddContractStakingVotes {
if p.contractStakingIndexer != nil && 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
47 changes: 42 additions & 5 deletions action/protocol/staking/staking_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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 @@ -75,7 +81,6 @@ 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()))
Expand All @@ -88,6 +93,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 @@ -99,7 +108,6 @@ 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()))
Expand All @@ -112,6 +120,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 @@ -137,6 +151,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 @@ -156,6 +174,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 @@ -192,11 +213,12 @@ func (c *compositeStakingStateReader) readStateCandidates(ctx context.Context, r
return nil, 0, err
}
}

if !protocol.MustGetFeatureCtx(ctx).AddContractStakingVotes {
return candidates, height, nil
}

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 @@ -210,6 +232,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 @@ -224,6 +249,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 @@ -243,7 +271,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 @@ -258,6 +288,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 @@ -272,6 +305,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
86 changes: 0 additions & 86 deletions blockindex/contractstaking/dummy_indexer.go

This file was deleted.

20 changes: 0 additions & 20 deletions blockindex/contractstaking/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/pkg/errors"

"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/blockchain/blockdao"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)
Expand All @@ -25,25 +24,6 @@ const (
)

type (
// ContractIndexer defines the interface of contract staking reader
ContractIndexer interface {
blockdao.BlockIndexerWithStart

// CandidateVotes returns the total staked votes of a candidate
// candidate identified by owner address
CandidateVotes(ownerAddr address.Address) *big.Int
// Buckets returns active buckets
Buckets() ([]*Bucket, error)
// BucketsByIndices returns active buckets by indices
BucketsByIndices([]uint64) ([]*Bucket, error)
// BucketsByCandidate returns active buckets by candidate
BucketsByCandidate(ownerAddr address.Address) ([]*Bucket, error)
// TotalBucketCount returns the total number of buckets including burned buckets
TotalBucketCount() uint64
// BucketTypes returns the active bucket types
BucketTypes() ([]*BucketType, error)
}

// Indexer is the contract staking indexer
// Main functions:
// 1. handle contract staking contract events when new block comes to generate index data
Expand Down
Loading