Skip to content

Commit

Permalink
[staking] remove unnecessary call from GetStakingStateReader (#3695)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Nov 16, 2022
1 parent c5d9761 commit e0578da
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 27 deletions.
21 changes: 0 additions & 21 deletions action/protocol/staking/candidate_statereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,27 +119,6 @@ func (c *candSR) ActiveBucketsCount() uint64 {
return c.view.bucketPool.Count()
}

// GetStakingStateReader returns a candidate state reader that reflects the base view
func GetStakingStateReader(sr protocol.StateReader) (CandidateStateReader, error) {
c, err := ConstructBaseView(sr)
if err != nil {
if errors.Cause(err) == protocol.ErrNoName {
// the view does not exist yet, create it
view, height, err := CreateBaseView(sr, true)
if err != nil {
return nil, err
}
return &candSR{
StateReader: sr,
height: height,
view: view,
}, nil
}
return nil, err
}
return c, nil
}

// ConstructBaseView returns a candidate state reader that reflects the base view
// it will be used read-only
func ConstructBaseView(sr protocol.StateReader) (CandidateStateReader, error) {
Expand Down
14 changes: 11 additions & 3 deletions action/protocol/staking/candidate_statereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/golang/mock/gomock"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/testutil/testdb"
"github.com/stretchr/testify/require"
)
Expand All @@ -20,11 +21,18 @@ func Test_CandidateStateReader(t *testing.T) {

ctrl := gomock.NewController(t)
sm := testdb.NewMockStateManager(ctrl)
csr, err := GetStakingStateReader(sm)
require.NoError(err)

h, err := sm.Height()
require.NoError(err)
csr, err := ConstructBaseView(sm)
require.Equal(err, protocol.ErrNoName)
view, _, err := CreateBaseView(sm, false)
require.NoError(err)
csr = &candSR{
StateReader: sm,
height: h,
view: view,
}

require.Equal(csr.Height(), h)
require.Equal(csr.SR(), sm)
require.Equal(len(csr.AllCandidates()), 0)
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 @@ -458,7 +458,7 @@ func (p *Protocol) Validate(ctx context.Context, act action.Action, sr protocol.

// ActiveCandidates returns all active candidates in candidate center
func (p *Protocol) ActiveCandidates(ctx context.Context, sr protocol.StateReader, height uint64) (state.CandidateList, error) {
c, err := GetStakingStateReader(sr)
c, err := ConstructBaseView(sr)
if err != nil {
return nil, errors.Wrap(err, "failed to get ActiveCandidates")
}
Expand Down
2 changes: 1 addition & 1 deletion api/grpcserver_integrity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ func TestGrpcServer_SendActionIntegrity(t *testing.T) {

for _, test := range tests {
request := &iotexapi.SendActionRequest{Action: test.action}
cfg:=test.cfg()
cfg := test.cfg()
cfg.API.GRPCPort = testutil.RandomPort()
svr, _, _, _, _, _, file, err := createServerV2(cfg, true)
require.NoError(err)
Expand Down
2 changes: 1 addition & 1 deletion ioctl/newcmd/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func TestExecute(t *testing.T) {
require.Error(err, expectedErr)
})
}

func TestRead(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
Expand Down

0 comments on commit e0578da

Please sign in to comment.