-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathnodedelegate.go
292 lines (272 loc) · 9.37 KB
/
nodedelegate.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) 2022 IoTeX
// This is an alpha (internal) release and is not suitable for production. 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 node
import (
"context"
"fmt"
"math/big"
"strconv"
"strings"
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/iotexproject/iotex-core/action/protocol/vote"
"github.com/iotexproject/iotex-core/ioctl"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/newcmd/bc"
"github.com/iotexproject/iotex-core/ioctl/util"
"github.com/iotexproject/iotex-core/state"
)
// Multi-language support
var (
_delegateUses = map[config.Language]string{
config.English: "delegate [-e epoch-num|-n]",
config.Chinese: "delegate [-e epoch数|-n]",
}
_delegateShorts = map[config.Language]string{
config.English: "Print consensus delegates information in certain epoch",
config.Chinese: "打印在特定epoch内的共识委托信息",
}
_flagEpochNumUsages = map[config.Language]string{
config.English: "specify specific epoch",
config.Chinese: "指定特定epoch",
}
_flagNextEpochUsages = map[config.Language]string{
config.English: "query delegate of upcoming epoch",
config.Chinese: "查询即将到来的epoch的委托",
}
)
var (
_nodeStatus map[bool]string
_probatedStatus map[bool]string
)
type nextDelegatesMessage struct {
Epoch int `json:"epoch"`
Determined bool `json:"determined"`
Delegates []delegate `json:"delegates"`
}
type delegate struct {
Address string `json:"address"`
Rank int `json:"rank"`
Alias string `json:"alias"`
Active bool `json:"active"`
Production int `json:"production"`
Votes string `json:"votes"`
ProbatedStatus bool `json:"_probatedStatus"`
}
type delegatesMessage struct {
Epoch int `json:"epoch"`
StartBlock int `json:"startBlock"`
TotalBlocks int `json:"totalBlocks"`
Delegates []delegate `json:"delegates"`
}
// NewNodeDelegateCmd represents the node delegate command
func NewNodeDelegateCmd(client ioctl.Client) *cobra.Command {
var (
epochNum uint64
nextEpoch bool
endpoint string
insecure bool
)
_nodeStatus = map[bool]string{true: "active", false: "false"}
_probatedStatus = map[bool]string{true: "probated", false: ""}
use, _ := client.SelectTranslation(_delegateUses)
short, _ := client.SelectTranslation(_delegateShorts)
flagEpochNumUsage, _ := client.SelectTranslation(_flagEpochNumUsages)
flagNextEpochUsage, _ := client.SelectTranslation(_flagNextEpochUsages)
cmd := &cobra.Command{
Use: use,
Short: short,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
var err error
if nextEpoch {
//nextDelegates
//deprecated: It won't be able to query next delegate after Easter height, because it will be determined at the end of the epoch.
apiServiceClient, err := client.APIServiceClient(ioctl.APIServiceConfig{
Endpoint: endpoint,
Insecure: insecure,
})
if err != nil {
return err
}
chainMeta, err := bc.GetChainMeta(client)
if err != nil {
return errors.Wrap(err, "failed to get chain meta")
}
epochNum = chainMeta.GetEpoch().GetNum() + 1
message := nextDelegatesMessage{Epoch: int(epochNum)}
ctx := context.Background()
jwtMD, err := util.JwtAuth()
if err == nil {
ctx = metautils.NiceMD(jwtMD).ToOutgoing(ctx)
}
abpResponse, err := apiServiceClient.ReadState(
ctx,
&iotexapi.ReadStateRequest{
ProtocolID: []byte("poll"),
MethodName: []byte("ActiveBlockProducersByEpoch"),
Arguments: [][]byte{[]byte(strconv.FormatUint(epochNum, 10))},
},
)
if err != nil {
sta, ok := status.FromError(err)
if ok && sta.Code() == codes.NotFound {
message.Determined = false
cmd.Println(message.String(epochNum))
return nil
} else if ok {
return errors.New(sta.Message())
}
return errors.Wrap(err, "failed to invoke ReadState api")
}
message.Determined = true
var abps state.CandidateList
if err := abps.Deserialize(abpResponse.Data); err != nil {
return errors.Wrap(err, "failed to deserialize active bps")
}
bpResponse, err := apiServiceClient.ReadState(
ctx,
&iotexapi.ReadStateRequest{
ProtocolID: []byte("poll"),
MethodName: []byte("BlockProducersByEpoch"),
Arguments: [][]byte{[]byte(strconv.FormatUint(epochNum, 10))},
},
)
if err != nil {
sta, ok := status.FromError(err)
if ok {
return errors.New(sta.Message())
}
return errors.Wrap(err, "failed to invoke ReadState api")
}
var bps state.CandidateList
if err := bps.Deserialize(bpResponse.Data); err != nil {
return errors.Wrap(err, "failed to deserialize bps")
}
isActive := make(map[string]bool)
for _, abp := range abps {
isActive[abp.Address] = true
}
aliases := client.AliasMap()
for rank, bp := range bps {
votes := big.NewInt(0).SetBytes(bp.Votes.Bytes())
message.Delegates = append(message.Delegates, delegate{
Address: bp.Address,
Rank: rank + 1,
Alias: aliases[bp.Address],
Active: isActive[bp.Address],
Votes: util.RauToString(votes, util.IotxDecimalNum),
})
}
cmd.Println(message.String(epochNum))
} else {
if epochNum == 0 {
chainMeta, err := bc.GetChainMeta(client)
if err != nil {
return errors.Wrap(err, "failed to get chain meta")
}
epochNum = chainMeta.GetEpoch().GetNum()
}
response, err := bc.GetEpochMeta(client, epochNum)
if err != nil {
return errors.Wrap(err, "failed to get epoch meta")
}
epochData := response.EpochData
aliases := client.AliasMap()
message := delegatesMessage{
Epoch: int(epochData.Num),
StartBlock: int(epochData.Height),
TotalBlocks: int(response.TotalBlocks),
}
probationListRes, err := bc.GetProbationList(client, epochNum)
if err != nil {
return errors.Wrap(err, "failed to get probation list")
}
probationList := &vote.ProbationList{}
if probationListRes != nil {
if err := probationList.Deserialize(probationListRes.Data); err != nil {
return errors.Wrap(err, "failed to deserialize probation list")
}
}
for rank, bp := range response.BlockProducersInfo {
votes, ok := new(big.Int).SetString(bp.Votes, 10)
if !ok {
return errors.New("failed to convert votes into big int")
}
isProbated := false
if _, ok := probationList.ProbationInfo[bp.Address]; ok {
// if it exists in probation list
isProbated = true
}
delegate := delegate{
Address: bp.Address,
Rank: rank + 1,
Alias: aliases[bp.Address],
Active: bp.Active,
Production: int(bp.Production),
Votes: util.RauToString(votes, util.IotxDecimalNum),
ProbatedStatus: isProbated,
}
message.Delegates = append(message.Delegates, delegate)
}
cmd.Println(message.String())
}
if err != nil {
cmd.Println(err)
}
return nil
},
}
cmd.Flags().Uint64VarP(&epochNum, "epoch-num", "e", 0,
flagEpochNumUsage)
cmd.Flags().BoolVarP(&nextEpoch, "next-epoch", "n", false,
flagNextEpochUsage)
return cmd
}
func (m *nextDelegatesMessage) String(epochNum uint64) string {
if !m.Determined {
return fmt.Sprintf("delegates of upcoming epoch #%d are not determined", epochNum)
}
aliasLen := 5
for _, bp := range m.Delegates {
if len(bp.Alias) > aliasLen {
aliasLen = len(bp.Alias)
}
}
lines := []string{fmt.Sprintf("Epoch: %d\n", epochNum)}
formatTitleString := "%-41s %-4s %-" + strconv.Itoa(aliasLen) + "s %-6s %s"
formatDataString := "%-41s %4d %-" + strconv.Itoa(aliasLen) + "s %-6s %s"
lines = append(lines, fmt.Sprintf(formatTitleString, "Address", "Rank", "Alias", "Status", "Votes"))
for _, bp := range m.Delegates {
lines = append(lines, fmt.Sprintf(formatDataString, bp.Address, bp.Rank,
bp.Alias, _nodeStatus[bp.Active], bp.Votes))
}
return strings.Join(lines, "\n")
}
func (m *delegatesMessage) String() string {
aliasLen := 5
for _, bp := range m.Delegates {
if len(bp.Alias) > aliasLen {
aliasLen = len(bp.Alias)
}
}
lines := []string{fmt.Sprintf("Epoch: %d, Start block height: %d,Total blocks in epoch: %d\n",
m.Epoch, m.StartBlock, m.TotalBlocks)}
formatTitleString := "%-41s %-4s %-" + strconv.Itoa(aliasLen) + "s %-6s %-6s %-12s %s"
formatDataString := "%-41s %4d %-" + strconv.Itoa(aliasLen) + "s %-6s %-6d %-12s %s"
lines = append(lines, fmt.Sprintf(formatTitleString,
"Address", "Rank", "Alias", "Status", "Blocks", "ProbatedStatus", "Votes"))
for _, bp := range m.Delegates {
lines = append(lines, fmt.Sprintf(formatDataString, bp.Address, bp.Rank,
bp.Alias, _nodeStatus[bp.Active], bp.Production, _probatedStatus[bp.ProbatedStatus], bp.Votes))
}
return strings.Join(lines, "\n")
}