Skip to content

Commit

Permalink
Merge bf485f5 into 020f965
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Jul 6, 2023
2 parents 020f965 + bf485f5 commit 7037b26
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 159 deletions.
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
3 changes: 1 addition & 2 deletions action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,10 @@ 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))
for i := range list {
if ok && featureCtx.AddContractStakingVotes {
if p.contractStakingIndexer != nil && protocol.MustGetFeatureCtx(ctx).AddContractStakingVotes {
list[i].Votes.Add(list[i].Votes, p.contractStakingIndexer.CandidateVotes(list[i].Owner))

Check warning on line 478 in action/protocol/staking/protocol.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/protocol.go#L478

Added line #L478 was not covered by tests
}
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

Check warning on line 72 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L71-L72

Added lines #L71 - L72 were not covered by tests
}

// 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

Check warning on line 98 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L97-L98

Added lines #L97 - L98 were not covered by tests
}

// 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

Check warning on line 126 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L125-L126

Added lines #L125 - L126 were not covered by tests
}

// 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

Check warning on line 155 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L155

Added line #L155 was not covered by tests
}

// 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

Check warning on line 178 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L178

Added line #L178 was not covered by tests
}
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

Check warning on line 217 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L217

Added line #L217 was not covered by tests
}

if !c.isContractStakingEnabled() {
return candidates, height, nil

Check warning on line 220 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L220

Added line #L220 was not covered by tests
}
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

Check warning on line 236 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L236

Added line #L236 was not covered by tests
}
if !protocol.MustGetFeatureCtx(ctx).AddContractStakingVotes {
return candidate, height, nil

Check warning on line 239 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L239

Added line #L239 was not covered by tests
}
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

Check warning on line 253 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L253

Added line #L253 was not covered by tests
}
if !protocol.MustGetFeatureCtx(ctx).AddContractStakingVotes {
return candidate, height, nil

Check warning on line 256 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L256

Added line #L256 was not covered by tests
}
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)

Check warning on line 272 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L272

Added line #L272 was not covered by tests
}

if !c.isContractStakingEnabled() {
return accountMeta, height, nil

Check warning on line 275 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L275

Added line #L275 was not covered by tests
}
// 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

Check warning on line 292 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L290-L292

Added lines #L290 - L292 were not covered by tests
}
bts, err := c.contractIndexer.BucketTypes()
if err != nil {

Check warning on line 295 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L294-L295

Added lines #L294 - L295 were not covered by tests
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

Check warning on line 305 in action/protocol/staking/staking_statereader.go

View check run for this annotation

Codecov / codecov/patch

action/protocol/staking/staking_statereader.go#L305

Added line #L305 was not covered by tests
}

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

0 comments on commit 7037b26

Please sign in to comment.