Skip to content
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
4 changes: 2 additions & 2 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ type (
NotSlashUnproductiveDelegates bool
CandidateBLSPublicKey bool
NotUseMinSelfStakeToBeActive bool
LoadContractStakingFromIndexer bool
StoreVoteOfNFTBucketIntoView bool
}

// FeatureWithHeightCtx provides feature check functions.
Expand Down Expand Up @@ -328,7 +328,7 @@ func WithFeatureCtx(ctx context.Context) context.Context {
NotSlashUnproductiveDelegates: !g.IsToBeEnabled(height),
CandidateBLSPublicKey: g.IsToBeEnabled(height),
NotUseMinSelfStakeToBeActive: !g.IsToBeEnabled(height),
LoadContractStakingFromIndexer: !g.IsToBeEnabled(height),
StoreVoteOfNFTBucketIntoView: !g.IsToBeEnabled(height),
},
)
}
Expand Down
28 changes: 14 additions & 14 deletions action/protocol/mock_protocol.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions action/protocol/staking/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ type (

// BuilderConfig returns the configuration of the builder
BuilderConfig struct {
Staking genesis.Staking
PersistStakingPatchBlock uint64
FixAliasForNonStopHeight uint64
StakingPatchDir string
Revise ReviseConfig
Staking genesis.Staking
PersistStakingPatchBlock uint64
FixAliasForNonStopHeight uint64
SkipContractStakingViewHeight uint64
StakingPatchDir string
Revise ReviseConfig
}
)
37 changes: 20 additions & 17 deletions action/protocol/staking/candidate_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type (
NativeBucketIndicesByVoter(addr address.Address) (*BucketIndices, uint64, error)
NativeBucketIndicesByCandidate(addr address.Address) (*BucketIndices, uint64, error)
CandidateByAddress(name address.Address) (*Candidate, uint64, error)
getAllCandidates() (CandidateList, uint64, error)
CreateCandidateCenter() (*CandidateCenter, uint64, error)
ReadState
Height() uint64
SR() protocol.StateReader
Expand Down Expand Up @@ -152,12 +152,7 @@ func CreateBaseView(sr protocol.StateReader, enableSMStorage bool) (*viewData, u
}

csr := newCandidateStateReader(sr)
all, height, err := csr.getAllCandidates()
if err != nil && errors.Cause(err) != state.ErrStateNotExist {
return nil, height, err
}

center, err := NewCandidateCenter(all)
center, height, err := csr.CreateCandidateCenter()
if err != nil {
return nil, height, err
}
Expand Down Expand Up @@ -301,21 +296,29 @@ func (c *candSR) CandidateByAddress(name address.Address) (*Candidate, uint64, e
return &d, height, err
}

func (c *candSR) getAllCandidates() (CandidateList, uint64, error) {
func (c *candSR) CreateCandidateCenter() (*CandidateCenter, uint64, error) {
height, iter, err := c.States(protocol.NamespaceOption(_candidateNameSpace))
var cands CandidateList
switch errors.Cause(err) {
case nil:
cands = make(CandidateList, 0, iter.Size())
for i := 0; i < iter.Size(); i++ {
c := &Candidate{}
if _, err := iter.Next(c); err != nil {
return nil, height, errors.Wrapf(err, "failed to deserialize candidate")
}
cands = append(cands, c)
}
case state.ErrStateNotExist:
default:
return nil, height, err
}
center, err := NewCandidateCenter(cands)
if err != nil {
return nil, height, err
}

cands := make(CandidateList, 0, iter.Size())
for i := 0; i < iter.Size(); i++ {
c := &Candidate{}
if _, err := iter.Next(c); err != nil {
return nil, height, errors.Wrapf(err, "failed to deserialize candidate")
}
cands = append(cands, c)
}
return cands, height, nil
return center, height, nil
}

func (c *candSR) NewBucketPool(enableSMStorage bool) (*BucketPool, error) {
Expand Down
4 changes: 2 additions & 2 deletions action/protocol/staking/candidate_statereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func Test_CandidateStateReader(t *testing.T) {
sm := testdb.NewMockStateManager(ctrl)
h, err := sm.Height()
require.NoError(err)
csr, err := ConstructBaseView(sm)
_, err = ConstructBaseView(sm)
require.Equal(err, protocol.ErrNoName)
view, _, err := CreateBaseView(sm, false)
require.NoError(err)
csr = &candSR{
csr := &candSR{
StateReader: sm,
height: h,
view: view,
Expand Down
3 changes: 2 additions & 1 deletion action/protocol/staking/candidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ func TestGetPutCandidate(t *testing.T) {
}

// get all candidates
all, _, err := csr.getAllCandidates()
cc, _, err := csr.CreateCandidateCenter()
require.NoError(err)
all := cc.All()
require.Equal(len(testCandidates), len(all))
for _, e := range testCandidates {
for i := range all {
Expand Down
17 changes: 16 additions & 1 deletion action/protocol/staking/contractstake_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/v2/action"
"github.com/iotexproject/iotex-core/v2/action/protocol"
"github.com/iotexproject/iotex-core/v2/action/protocol/staking/contractstaking"
)

var (
Expand All @@ -25,7 +27,18 @@ var (
)

type (

// EventHandler is the interface for handling staking events
EventHandler interface {
PutBucketType(address.Address, *ContractStakingBucketType) error
DeductBucket(address.Address, uint64) (*contractstaking.Bucket, error)
PutBucket(address.Address, uint64, *contractstaking.Bucket) error
DeleteBucket(address.Address, uint64) error
}
// EventProcessor is the interface for processing staking events
EventProcessor interface {
// ProcessReceipts processes receipts
ProcessReceipts(context.Context, ...*action.Receipt) error
}
// ContractStakingIndexer defines the interface of contract staking reader
ContractStakingIndexer interface {
Height() (uint64, error)
Expand All @@ -41,6 +54,8 @@ type (
ContractAddress() address.Address
// LoadStakeView loads the contract stake view from state reader
LoadStakeView(context.Context, protocol.StateReader) (ContractStakeView, error)
// CreateEventProcessor creates a new event processor
CreateEventProcessor(context.Context, EventHandler) EventProcessor
}
// ContractStakingIndexerWithBucketType defines the interface of contract staking reader with bucket type
ContractStakingIndexerWithBucketType interface {
Expand Down
154 changes: 154 additions & 0 deletions action/protocol/staking/contractstake_indexer_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions action/protocol/staking/contractstaking/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type (
}
)

// ErrBucketNotExist is the error when bucket does not exist
var ErrBucketNotExist = errors.New("bucket does not exist")

func (b *Bucket) toProto() *stakingpb.SystemStakingBucket {
if b == nil {
return nil
Expand Down
Loading
Loading