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

[ioctl] support querying delegate by operator address #3904

Merged
merged 6 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
42 changes: 39 additions & 3 deletions ioctl/cmd/bc/bcdelegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/exp/slices"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"

Expand All @@ -30,8 +32,8 @@ var (
config.Chinese: "在IoTeX区块链上读取代表信息",
}
_bcDelegateUses = map[config.Language]string{
config.English: "delegate [name]",
config.Chinese: "delegate [名字]",
config.English: "delegate [name|address]",
config.Chinese: "delegate [名字|地址]",
}
)

Expand Down Expand Up @@ -79,7 +81,13 @@ func (m *delegateMessage) String() string {
}

func getDelegate(arg string) error {
d, err := getDelegateByName(arg)
var d *iotextypes.CandidateV2
addr, err := util.Address(arg)
if err == nil {
d, err = getDelegateByAddress(addr)
} else {
d, err = getDelegateByName(arg)
}
if err != nil {
return err
}
Expand Down Expand Up @@ -142,3 +150,31 @@ func getDelegateByName(name string) (*iotextypes.CandidateV2, error) {
}
return &delegate, nil
}

func getDelegateByAddress(addr string) (*iotextypes.CandidateV2, error) {
conn, err := util.ConnectToEndpoint(config.ReadConfig.SecureConnect && !config.Insecure)
if err != nil {
return nil, output.NewError(output.NetworkError, "failed to connect to endpoint", err)
}
defer conn.Close()
cli := iotexapi.NewAPIServiceClient(conn)

readCandidatesLimit := 20000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

200 works and is maybe better?

for i := uint32(0); ; i++ {
offset := i * uint32(readCandidatesLimit)
size := uint32(readCandidatesLimit)
candidateList, err := util.GetStakingCandidates(cli, offset, size)
if err != nil {
return nil, errors.Wrap(err, "failed to get candidates")
}
if idx := slices.IndexFunc(candidateList.Candidates, func(cand *iotextypes.CandidateV2) bool {
return cand.OperatorAddress == addr
}); idx >= 0 {
return candidateList.Candidates[idx], nil
}
if len(candidateList.Candidates) < readCandidatesLimit {
break
}
}
return nil, output.NewError(output.UndefinedError, "failed to find delegate", nil)
}
39 changes: 1 addition & 38 deletions ioctl/cmd/node/nodedelegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
Expand Down Expand Up @@ -355,7 +354,7 @@ func getAllStakingCandidates(chainClient iotexapi.APIServiceClient) (candidateLi
for i := uint32(0); ; i++ {
offset := i * _readCandidatesLimit
size := uint32(_readCandidatesLimit)
candidateList, err := getStakingCandidates(chainClient, offset, size)
candidateList, err := util.GetStakingCandidates(chainClient, offset, size)
if err != nil {
return nil, errors.Wrap(err, "failed to get candidates")
}
Expand All @@ -366,39 +365,3 @@ func getAllStakingCandidates(chainClient iotexapi.APIServiceClient) (candidateLi
}
return
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to util pkg to reuse it

func getStakingCandidates(chainClient iotexapi.APIServiceClient, offset, limit uint32) (candidateList *iotextypes.CandidateListV2, err error) {
methodName, err := proto.Marshal(&iotexapi.ReadStakingDataMethod{
Method: iotexapi.ReadStakingDataMethod_CANDIDATES,
})
if err != nil {
return nil, err
}
arg, err := proto.Marshal(&iotexapi.ReadStakingDataRequest{
Request: &iotexapi.ReadStakingDataRequest_Candidates_{
Candidates: &iotexapi.ReadStakingDataRequest_Candidates{
Pagination: &iotexapi.PaginationParam{
Offset: offset,
Limit: limit,
},
},
},
})
if err != nil {
return nil, err
}
readStateRequest := &iotexapi.ReadStateRequest{
ProtocolID: []byte(_protocolID),
MethodName: methodName,
Arguments: [][]byte{arg},
}
readStateRes, err := chainClient.ReadState(context.Background(), readStateRequest)
if err != nil {
return
}
candidateList = &iotextypes.CandidateListV2{}
if err := proto.Unmarshal(readStateRes.GetData(), candidateList); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal VoteBucketList")
}
return
}
50 changes: 50 additions & 0 deletions ioctl/util/chain_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package util

import (
"context"

"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)

const (
_stakingProtocol = "staking"
)

func GetStakingCandidates(chainClient iotexapi.APIServiceClient, offset, limit uint32) (candidateList *iotextypes.CandidateListV2, err error) {
methodName, err := proto.Marshal(&iotexapi.ReadStakingDataMethod{
Method: iotexapi.ReadStakingDataMethod_CANDIDATES,
})
if err != nil {
return nil, err
}
arg, err := proto.Marshal(&iotexapi.ReadStakingDataRequest{
Request: &iotexapi.ReadStakingDataRequest_Candidates_{
Candidates: &iotexapi.ReadStakingDataRequest_Candidates{
Pagination: &iotexapi.PaginationParam{
Offset: offset,
Limit: limit,
},
},
},
})
if err != nil {
return nil, err
}
readStateRequest := &iotexapi.ReadStateRequest{
ProtocolID: []byte(_stakingProtocol),
MethodName: methodName,
Arguments: [][]byte{arg},
}
readStateRes, err := chainClient.ReadState(context.Background(), readStateRequest)
if err != nil {
return
}
candidateList = &iotextypes.CandidateListV2{}
if err := proto.Unmarshal(readStateRes.GetData(), candidateList); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal VoteBucketList")
}
return
}
27 changes: 27 additions & 0 deletions ioctl/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-core/ioctl/config"
)

func TestStringToRau(t *testing.T) {
Expand Down Expand Up @@ -93,3 +95,28 @@ func TestParseHdwPath(t *testing.T) {
}
}
}

func TestAddress(t *testing.T) {
require := require.New(t)

// Test valid address
addr, err := Address("io1xpq62aw85uqzrccg9y5hnryv8ld2nkpycc3gza")
require.NoError(err)
require.Equal("io1xpq62aw85uqzrccg9y5hnryv8ld2nkpycc3gza", addr)

// Test valid alias
config.ReadConfig.Aliases["myalias"] = "io1xpq62aw85uqzrccg9y5hnryv8ld2nkpycc3gza"
addr, err = Address("myalias")
require.NoError(err)
require.Equal("io1xpq62aw85uqzrccg9y5hnryv8ld2nkpycc3gza", addr)

// Test invalid address
_, err = Address("invalidaddress")
require.Error(err)
require.ErrorContains(err, "cannot find address for alias")

// Test invalid alias
_, err = Address("invalidalias")
require.Error(err)
require.ErrorContains(err, "cannot find address for alias")
}