/
bcdelegate.go
180 lines (164 loc) · 5.87 KB
/
bcdelegate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) 2022 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package bc
import (
"context"
"fmt"
"math/big"
"strings"
"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"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
)
// Multi-language support
var (
_bcDelegateCmdShorts = map[config.Language]string{
config.English: "Get delegate information on IoTeX blockchain",
config.Chinese: "在IoTeX区块链上读取代表信息",
}
_bcDelegateUses = map[config.Language]string{
config.English: "delegate [name|address]",
config.Chinese: "delegate [名字|地址]",
}
)
// _bcBucketCmd represents the bc Bucket command
var _bcDelegateCmd = &cobra.Command{
Use: config.TranslateInLang(_bcDelegateUses, config.UILanguage),
Short: config.TranslateInLang(_bcDelegateCmdShorts, config.UILanguage),
Args: cobra.ExactArgs(1),
Example: `ioctl bc delegate name, to read delegate information by name`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
cmd.SilenceUsage = true
err = getDelegate(args[0])
return output.PrintError(err)
},
}
type delegateMessage struct {
Delegate *iotextypes.CandidateV2 `json:"delegate"`
}
func (m *delegateMessage) delegateString() string {
var (
d = m.Delegate
vote, _ = new(big.Int).SetString(d.TotalWeightedVotes, 10)
token, _ = new(big.Int).SetString(d.SelfStakingTokens, 10)
lines []string
)
lines = append(lines, "{")
lines = append(lines, fmt.Sprintf(" name: %s", d.Name))
lines = append(lines, fmt.Sprintf(" ownerAddress: %s", d.OwnerAddress))
lines = append(lines, fmt.Sprintf(" operatorAddress: %s", d.OperatorAddress))
lines = append(lines, fmt.Sprintf(" rewardAddress: %s", d.RewardAddress))
lines = append(lines, fmt.Sprintf(" totalWeightedVotes: %s", util.RauToString(vote, util.IotxDecimalNum)))
lines = append(lines, fmt.Sprintf(" selfStakeBucketIdx: %d", d.SelfStakeBucketIdx))
lines = append(lines, fmt.Sprintf(" selfStakingTokens: %s IOTX", util.RauToString(token, util.IotxDecimalNum)))
lines = append(lines, "}")
return strings.Join(lines, "\n")
}
func (m *delegateMessage) String() string {
if output.Format == "" {
return m.delegateString()
}
return output.FormatString(output.Result, m)
}
func getDelegate(arg string) error {
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
}
message := delegateMessage{
Delegate: d,
}
fmt.Println(message.String())
return nil
}
func getDelegateByName(name 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)
method := &iotexapi.ReadStakingDataMethod{
Method: iotexapi.ReadStakingDataMethod_CANDIDATE_BY_NAME,
}
methodData, err := proto.Marshal(method)
if err != nil {
return nil, output.NewError(output.SerializationError, "failed to marshal read staking data method", err)
}
readStakingdataRequest := &iotexapi.ReadStakingDataRequest{
Request: &iotexapi.ReadStakingDataRequest_CandidateByName_{
CandidateByName: &iotexapi.ReadStakingDataRequest_CandidateByName{
CandName: name,
},
},
}
requestData, err := proto.Marshal(readStakingdataRequest)
if err != nil {
return nil, output.NewError(output.SerializationError, "failed to marshal read staking data request", err)
}
request := &iotexapi.ReadStateRequest{
ProtocolID: []byte("staking"),
MethodName: methodData,
Arguments: [][]byte{requestData},
}
ctx := context.Background()
jwtMD, err := util.JwtAuth()
if err == nil {
ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx)
}
response, err := cli.ReadState(ctx, request)
if err != nil {
sta, ok := status.FromError(err)
if ok {
return nil, output.NewError(output.APIError, sta.Message(), nil)
}
return nil, output.NewError(output.NetworkError, "failed to invoke ReadState api", err)
}
delegate := iotextypes.CandidateV2{}
if err := proto.Unmarshal(response.Data, &delegate); err != nil {
return nil, output.NewError(output.SerializationError, "failed to unmarshal response", err)
}
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 := 200
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)
}